netlink.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * Vendor commands implementation based on net/wireless/nl80211.c
  9. * which is:
  10. *
  11. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  12. * Copyright 2013-2014 Intel Mobile Communications GmbH
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  28. #include <net/genetlink.h>
  29. #include <linux/nfc.h>
  30. #include <linux/slab.h>
  31. #include "nfc.h"
  32. #include "llcp.h"
  33. static const struct genl_multicast_group nfc_genl_mcgrps[] = {
  34. { .name = NFC_GENL_MCAST_EVENT_NAME, },
  35. };
  36. static struct genl_family nfc_genl_family = {
  37. .id = GENL_ID_GENERATE,
  38. .hdrsize = 0,
  39. .name = NFC_GENL_NAME,
  40. .version = NFC_GENL_VERSION,
  41. .maxattr = NFC_ATTR_MAX,
  42. };
  43. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  44. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  45. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  46. .len = NFC_DEVICE_NAME_MAXSIZE },
  47. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  48. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  49. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  50. [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  51. [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
  52. [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
  53. [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
  54. [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
  55. [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
  56. [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
  57. [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
  58. .len = NFC_FIRMWARE_NAME_MAXSIZE },
  59. [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
  60. [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
  61. };
  62. static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
  63. [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
  64. .len = U8_MAX - 4 },
  65. [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
  66. };
  67. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  68. struct netlink_callback *cb, int flags)
  69. {
  70. void *hdr;
  71. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  72. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  73. if (!hdr)
  74. return -EMSGSIZE;
  75. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  76. if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
  77. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
  78. nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
  79. nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
  80. goto nla_put_failure;
  81. if (target->nfcid1_len > 0 &&
  82. nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  83. target->nfcid1))
  84. goto nla_put_failure;
  85. if (target->sensb_res_len > 0 &&
  86. nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
  87. target->sensb_res))
  88. goto nla_put_failure;
  89. if (target->sensf_res_len > 0 &&
  90. nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
  91. target->sensf_res))
  92. goto nla_put_failure;
  93. if (target->is_iso15693) {
  94. if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
  95. target->iso15693_dsfid) ||
  96. nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
  97. sizeof(target->iso15693_uid), target->iso15693_uid))
  98. goto nla_put_failure;
  99. }
  100. genlmsg_end(msg, hdr);
  101. return 0;
  102. nla_put_failure:
  103. genlmsg_cancel(msg, hdr);
  104. return -EMSGSIZE;
  105. }
  106. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  107. {
  108. struct nfc_dev *dev;
  109. int rc;
  110. u32 idx;
  111. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  112. nfc_genl_family.attrbuf,
  113. nfc_genl_family.maxattr,
  114. nfc_genl_policy);
  115. if (rc < 0)
  116. return ERR_PTR(rc);
  117. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  118. return ERR_PTR(-EINVAL);
  119. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  120. dev = nfc_get_device(idx);
  121. if (!dev)
  122. return ERR_PTR(-ENODEV);
  123. return dev;
  124. }
  125. static int nfc_genl_dump_targets(struct sk_buff *skb,
  126. struct netlink_callback *cb)
  127. {
  128. int i = cb->args[0];
  129. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  130. int rc;
  131. if (!dev) {
  132. dev = __get_device_from_cb(cb);
  133. if (IS_ERR(dev))
  134. return PTR_ERR(dev);
  135. cb->args[1] = (long) dev;
  136. }
  137. device_lock(&dev->dev);
  138. cb->seq = dev->targets_generation;
  139. while (i < dev->n_targets) {
  140. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  141. NLM_F_MULTI);
  142. if (rc < 0)
  143. break;
  144. i++;
  145. }
  146. device_unlock(&dev->dev);
  147. cb->args[0] = i;
  148. return skb->len;
  149. }
  150. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  151. {
  152. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  153. if (dev)
  154. nfc_put_device(dev);
  155. return 0;
  156. }
  157. int nfc_genl_targets_found(struct nfc_dev *dev)
  158. {
  159. struct sk_buff *msg;
  160. void *hdr;
  161. dev->genl_data.poll_req_portid = 0;
  162. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  163. if (!msg)
  164. return -ENOMEM;
  165. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  166. NFC_EVENT_TARGETS_FOUND);
  167. if (!hdr)
  168. goto free_msg;
  169. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  170. goto nla_put_failure;
  171. genlmsg_end(msg, hdr);
  172. return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  173. nla_put_failure:
  174. genlmsg_cancel(msg, hdr);
  175. free_msg:
  176. nlmsg_free(msg);
  177. return -EMSGSIZE;
  178. }
  179. int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
  180. {
  181. struct sk_buff *msg;
  182. void *hdr;
  183. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  184. if (!msg)
  185. return -ENOMEM;
  186. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  187. NFC_EVENT_TARGET_LOST);
  188. if (!hdr)
  189. goto free_msg;
  190. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  191. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  192. goto nla_put_failure;
  193. genlmsg_end(msg, hdr);
  194. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  195. return 0;
  196. nla_put_failure:
  197. genlmsg_cancel(msg, hdr);
  198. free_msg:
  199. nlmsg_free(msg);
  200. return -EMSGSIZE;
  201. }
  202. int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
  203. {
  204. struct sk_buff *msg;
  205. void *hdr;
  206. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  207. if (!msg)
  208. return -ENOMEM;
  209. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  210. NFC_EVENT_TM_ACTIVATED);
  211. if (!hdr)
  212. goto free_msg;
  213. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  214. goto nla_put_failure;
  215. if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
  216. goto nla_put_failure;
  217. genlmsg_end(msg, hdr);
  218. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  219. return 0;
  220. nla_put_failure:
  221. genlmsg_cancel(msg, hdr);
  222. free_msg:
  223. nlmsg_free(msg);
  224. return -EMSGSIZE;
  225. }
  226. int nfc_genl_tm_deactivated(struct nfc_dev *dev)
  227. {
  228. struct sk_buff *msg;
  229. void *hdr;
  230. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  231. if (!msg)
  232. return -ENOMEM;
  233. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  234. NFC_EVENT_TM_DEACTIVATED);
  235. if (!hdr)
  236. goto free_msg;
  237. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  238. goto nla_put_failure;
  239. genlmsg_end(msg, hdr);
  240. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  241. return 0;
  242. nla_put_failure:
  243. genlmsg_cancel(msg, hdr);
  244. free_msg:
  245. nlmsg_free(msg);
  246. return -EMSGSIZE;
  247. }
  248. int nfc_genl_device_added(struct nfc_dev *dev)
  249. {
  250. struct sk_buff *msg;
  251. void *hdr;
  252. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  253. if (!msg)
  254. return -ENOMEM;
  255. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  256. NFC_EVENT_DEVICE_ADDED);
  257. if (!hdr)
  258. goto free_msg;
  259. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  260. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  261. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  262. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
  263. goto nla_put_failure;
  264. genlmsg_end(msg, hdr);
  265. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  266. return 0;
  267. nla_put_failure:
  268. genlmsg_cancel(msg, hdr);
  269. free_msg:
  270. nlmsg_free(msg);
  271. return -EMSGSIZE;
  272. }
  273. int nfc_genl_device_removed(struct nfc_dev *dev)
  274. {
  275. struct sk_buff *msg;
  276. void *hdr;
  277. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  278. if (!msg)
  279. return -ENOMEM;
  280. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  281. NFC_EVENT_DEVICE_REMOVED);
  282. if (!hdr)
  283. goto free_msg;
  284. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  285. goto nla_put_failure;
  286. genlmsg_end(msg, hdr);
  287. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  288. return 0;
  289. nla_put_failure:
  290. genlmsg_cancel(msg, hdr);
  291. free_msg:
  292. nlmsg_free(msg);
  293. return -EMSGSIZE;
  294. }
  295. int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
  296. {
  297. struct sk_buff *msg;
  298. struct nlattr *sdp_attr, *uri_attr;
  299. struct nfc_llcp_sdp_tlv *sdres;
  300. struct hlist_node *n;
  301. void *hdr;
  302. int rc = -EMSGSIZE;
  303. int i;
  304. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  305. if (!msg)
  306. return -ENOMEM;
  307. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  308. NFC_EVENT_LLC_SDRES);
  309. if (!hdr)
  310. goto free_msg;
  311. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  312. goto nla_put_failure;
  313. sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
  314. if (sdp_attr == NULL) {
  315. rc = -ENOMEM;
  316. goto nla_put_failure;
  317. }
  318. i = 1;
  319. hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
  320. pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
  321. uri_attr = nla_nest_start(msg, i++);
  322. if (uri_attr == NULL) {
  323. rc = -ENOMEM;
  324. goto nla_put_failure;
  325. }
  326. if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
  327. goto nla_put_failure;
  328. if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
  329. goto nla_put_failure;
  330. nla_nest_end(msg, uri_attr);
  331. hlist_del(&sdres->node);
  332. nfc_llcp_free_sdp_tlv(sdres);
  333. }
  334. nla_nest_end(msg, sdp_attr);
  335. genlmsg_end(msg, hdr);
  336. return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  337. nla_put_failure:
  338. genlmsg_cancel(msg, hdr);
  339. free_msg:
  340. nlmsg_free(msg);
  341. nfc_llcp_free_sdp_tlv_list(sdres_list);
  342. return rc;
  343. }
  344. int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
  345. {
  346. struct sk_buff *msg;
  347. void *hdr;
  348. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  349. if (!msg)
  350. return -ENOMEM;
  351. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  352. NFC_EVENT_SE_ADDED);
  353. if (!hdr)
  354. goto free_msg;
  355. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  356. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
  357. nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
  358. goto nla_put_failure;
  359. genlmsg_end(msg, hdr);
  360. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  361. return 0;
  362. nla_put_failure:
  363. genlmsg_cancel(msg, hdr);
  364. free_msg:
  365. nlmsg_free(msg);
  366. return -EMSGSIZE;
  367. }
  368. int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
  369. {
  370. struct sk_buff *msg;
  371. void *hdr;
  372. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  373. if (!msg)
  374. return -ENOMEM;
  375. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  376. NFC_EVENT_SE_REMOVED);
  377. if (!hdr)
  378. goto free_msg;
  379. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  380. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
  381. goto nla_put_failure;
  382. genlmsg_end(msg, hdr);
  383. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  384. return 0;
  385. nla_put_failure:
  386. genlmsg_cancel(msg, hdr);
  387. free_msg:
  388. nlmsg_free(msg);
  389. return -EMSGSIZE;
  390. }
  391. int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
  392. struct nfc_evt_transaction *evt_transaction)
  393. {
  394. struct nfc_se *se;
  395. struct sk_buff *msg;
  396. void *hdr;
  397. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  398. if (!msg)
  399. return -ENOMEM;
  400. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  401. NFC_EVENT_SE_TRANSACTION);
  402. if (!hdr)
  403. goto free_msg;
  404. se = nfc_find_se(dev, se_idx);
  405. if (!se)
  406. goto free_msg;
  407. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  408. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
  409. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
  410. nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
  411. evt_transaction->aid) ||
  412. nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
  413. evt_transaction->params))
  414. goto nla_put_failure;
  415. /* evt_transaction is no more used */
  416. devm_kfree(&dev->dev, evt_transaction);
  417. genlmsg_end(msg, hdr);
  418. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  419. return 0;
  420. nla_put_failure:
  421. genlmsg_cancel(msg, hdr);
  422. free_msg:
  423. /* evt_transaction is no more used */
  424. devm_kfree(&dev->dev, evt_transaction);
  425. nlmsg_free(msg);
  426. return -EMSGSIZE;
  427. }
  428. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  429. u32 portid, u32 seq,
  430. struct netlink_callback *cb,
  431. int flags)
  432. {
  433. void *hdr;
  434. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  435. NFC_CMD_GET_DEVICE);
  436. if (!hdr)
  437. return -EMSGSIZE;
  438. if (cb)
  439. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  440. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  441. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  442. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  443. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  444. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  445. goto nla_put_failure;
  446. genlmsg_end(msg, hdr);
  447. return 0;
  448. nla_put_failure:
  449. genlmsg_cancel(msg, hdr);
  450. return -EMSGSIZE;
  451. }
  452. static int nfc_genl_dump_devices(struct sk_buff *skb,
  453. struct netlink_callback *cb)
  454. {
  455. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  456. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  457. bool first_call = false;
  458. if (!iter) {
  459. first_call = true;
  460. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  461. if (!iter)
  462. return -ENOMEM;
  463. cb->args[0] = (long) iter;
  464. }
  465. mutex_lock(&nfc_devlist_mutex);
  466. cb->seq = nfc_devlist_generation;
  467. if (first_call) {
  468. nfc_device_iter_init(iter);
  469. dev = nfc_device_iter_next(iter);
  470. }
  471. while (dev) {
  472. int rc;
  473. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  474. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  475. if (rc < 0)
  476. break;
  477. dev = nfc_device_iter_next(iter);
  478. }
  479. mutex_unlock(&nfc_devlist_mutex);
  480. cb->args[1] = (long) dev;
  481. return skb->len;
  482. }
  483. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  484. {
  485. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  486. nfc_device_iter_exit(iter);
  487. kfree(iter);
  488. return 0;
  489. }
  490. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  491. u8 comm_mode, u8 rf_mode)
  492. {
  493. struct sk_buff *msg;
  494. void *hdr;
  495. pr_debug("DEP link is up\n");
  496. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  497. if (!msg)
  498. return -ENOMEM;
  499. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  500. if (!hdr)
  501. goto free_msg;
  502. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  503. goto nla_put_failure;
  504. if (rf_mode == NFC_RF_INITIATOR &&
  505. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  506. goto nla_put_failure;
  507. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  508. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  509. goto nla_put_failure;
  510. genlmsg_end(msg, hdr);
  511. dev->dep_link_up = true;
  512. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  513. return 0;
  514. nla_put_failure:
  515. genlmsg_cancel(msg, hdr);
  516. free_msg:
  517. nlmsg_free(msg);
  518. return -EMSGSIZE;
  519. }
  520. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  521. {
  522. struct sk_buff *msg;
  523. void *hdr;
  524. pr_debug("DEP link is down\n");
  525. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  526. if (!msg)
  527. return -ENOMEM;
  528. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  529. NFC_CMD_DEP_LINK_DOWN);
  530. if (!hdr)
  531. goto free_msg;
  532. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  533. goto nla_put_failure;
  534. genlmsg_end(msg, hdr);
  535. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  536. return 0;
  537. nla_put_failure:
  538. genlmsg_cancel(msg, hdr);
  539. free_msg:
  540. nlmsg_free(msg);
  541. return -EMSGSIZE;
  542. }
  543. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  544. {
  545. struct sk_buff *msg;
  546. struct nfc_dev *dev;
  547. u32 idx;
  548. int rc = -ENOBUFS;
  549. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  550. return -EINVAL;
  551. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  552. dev = nfc_get_device(idx);
  553. if (!dev)
  554. return -ENODEV;
  555. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  556. if (!msg) {
  557. rc = -ENOMEM;
  558. goto out_putdev;
  559. }
  560. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  561. NULL, 0);
  562. if (rc < 0)
  563. goto out_free;
  564. nfc_put_device(dev);
  565. return genlmsg_reply(msg, info);
  566. out_free:
  567. nlmsg_free(msg);
  568. out_putdev:
  569. nfc_put_device(dev);
  570. return rc;
  571. }
  572. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  573. {
  574. struct nfc_dev *dev;
  575. int rc;
  576. u32 idx;
  577. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  578. return -EINVAL;
  579. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  580. dev = nfc_get_device(idx);
  581. if (!dev)
  582. return -ENODEV;
  583. rc = nfc_dev_up(dev);
  584. nfc_put_device(dev);
  585. return rc;
  586. }
  587. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  588. {
  589. struct nfc_dev *dev;
  590. int rc;
  591. u32 idx;
  592. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  593. return -EINVAL;
  594. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  595. dev = nfc_get_device(idx);
  596. if (!dev)
  597. return -ENODEV;
  598. rc = nfc_dev_down(dev);
  599. nfc_put_device(dev);
  600. return rc;
  601. }
  602. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  603. {
  604. struct nfc_dev *dev;
  605. int rc;
  606. u32 idx;
  607. u32 im_protocols = 0, tm_protocols = 0;
  608. pr_debug("Poll start\n");
  609. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  610. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  611. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  612. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  613. return -EINVAL;
  614. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  615. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  616. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  617. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  618. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  619. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  620. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  621. dev = nfc_get_device(idx);
  622. if (!dev)
  623. return -ENODEV;
  624. mutex_lock(&dev->genl_data.genl_data_mutex);
  625. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  626. if (!rc)
  627. dev->genl_data.poll_req_portid = info->snd_portid;
  628. mutex_unlock(&dev->genl_data.genl_data_mutex);
  629. nfc_put_device(dev);
  630. return rc;
  631. }
  632. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  633. {
  634. struct nfc_dev *dev;
  635. int rc;
  636. u32 idx;
  637. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  638. return -EINVAL;
  639. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  640. dev = nfc_get_device(idx);
  641. if (!dev)
  642. return -ENODEV;
  643. device_lock(&dev->dev);
  644. if (!dev->polling) {
  645. device_unlock(&dev->dev);
  646. return -EINVAL;
  647. }
  648. device_unlock(&dev->dev);
  649. mutex_lock(&dev->genl_data.genl_data_mutex);
  650. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  651. rc = -EBUSY;
  652. goto out;
  653. }
  654. rc = nfc_stop_poll(dev);
  655. dev->genl_data.poll_req_portid = 0;
  656. out:
  657. mutex_unlock(&dev->genl_data.genl_data_mutex);
  658. nfc_put_device(dev);
  659. return rc;
  660. }
  661. static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
  662. {
  663. struct nfc_dev *dev;
  664. u32 device_idx, target_idx, protocol;
  665. int rc;
  666. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  667. !info->attrs[NFC_ATTR_TARGET_INDEX] ||
  668. !info->attrs[NFC_ATTR_PROTOCOLS])
  669. return -EINVAL;
  670. device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  671. dev = nfc_get_device(device_idx);
  672. if (!dev)
  673. return -ENODEV;
  674. target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  675. protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  676. nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
  677. rc = nfc_activate_target(dev, target_idx, protocol);
  678. nfc_put_device(dev);
  679. return 0;
  680. }
  681. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  682. {
  683. struct nfc_dev *dev;
  684. int rc, tgt_idx;
  685. u32 idx;
  686. u8 comm;
  687. pr_debug("DEP link up\n");
  688. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  689. !info->attrs[NFC_ATTR_COMM_MODE])
  690. return -EINVAL;
  691. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  692. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  693. tgt_idx = NFC_TARGET_IDX_ANY;
  694. else
  695. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  696. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  697. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  698. return -EINVAL;
  699. dev = nfc_get_device(idx);
  700. if (!dev)
  701. return -ENODEV;
  702. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  703. nfc_put_device(dev);
  704. return rc;
  705. }
  706. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  707. {
  708. struct nfc_dev *dev;
  709. int rc;
  710. u32 idx;
  711. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  712. return -EINVAL;
  713. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  714. dev = nfc_get_device(idx);
  715. if (!dev)
  716. return -ENODEV;
  717. rc = nfc_dep_link_down(dev);
  718. nfc_put_device(dev);
  719. return rc;
  720. }
  721. static int nfc_genl_send_params(struct sk_buff *msg,
  722. struct nfc_llcp_local *local,
  723. u32 portid, u32 seq)
  724. {
  725. void *hdr;
  726. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  727. NFC_CMD_LLC_GET_PARAMS);
  728. if (!hdr)
  729. return -EMSGSIZE;
  730. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  731. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  732. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  733. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  734. goto nla_put_failure;
  735. genlmsg_end(msg, hdr);
  736. return 0;
  737. nla_put_failure:
  738. genlmsg_cancel(msg, hdr);
  739. return -EMSGSIZE;
  740. }
  741. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  742. {
  743. struct nfc_dev *dev;
  744. struct nfc_llcp_local *local;
  745. int rc = 0;
  746. struct sk_buff *msg = NULL;
  747. u32 idx;
  748. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  749. return -EINVAL;
  750. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  751. dev = nfc_get_device(idx);
  752. if (!dev)
  753. return -ENODEV;
  754. device_lock(&dev->dev);
  755. local = nfc_llcp_find_local(dev);
  756. if (!local) {
  757. rc = -ENODEV;
  758. goto exit;
  759. }
  760. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  761. if (!msg) {
  762. rc = -ENOMEM;
  763. goto exit;
  764. }
  765. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  766. exit:
  767. device_unlock(&dev->dev);
  768. nfc_put_device(dev);
  769. if (rc < 0) {
  770. if (msg)
  771. nlmsg_free(msg);
  772. return rc;
  773. }
  774. return genlmsg_reply(msg, info);
  775. }
  776. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  777. {
  778. struct nfc_dev *dev;
  779. struct nfc_llcp_local *local;
  780. u8 rw = 0;
  781. u16 miux = 0;
  782. u32 idx;
  783. int rc = 0;
  784. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  785. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  786. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  787. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  788. return -EINVAL;
  789. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  790. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  791. if (rw > LLCP_MAX_RW)
  792. return -EINVAL;
  793. }
  794. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  795. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  796. if (miux > LLCP_MAX_MIUX)
  797. return -EINVAL;
  798. }
  799. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  800. dev = nfc_get_device(idx);
  801. if (!dev)
  802. return -ENODEV;
  803. device_lock(&dev->dev);
  804. local = nfc_llcp_find_local(dev);
  805. if (!local) {
  806. nfc_put_device(dev);
  807. rc = -ENODEV;
  808. goto exit;
  809. }
  810. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  811. if (dev->dep_link_up) {
  812. rc = -EINPROGRESS;
  813. goto exit;
  814. }
  815. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  816. }
  817. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  818. local->rw = rw;
  819. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  820. local->miux = cpu_to_be16(miux);
  821. exit:
  822. device_unlock(&dev->dev);
  823. nfc_put_device(dev);
  824. return rc;
  825. }
  826. static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
  827. {
  828. struct nfc_dev *dev;
  829. struct nfc_llcp_local *local;
  830. struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
  831. u32 idx;
  832. u8 tid;
  833. char *uri;
  834. int rc = 0, rem;
  835. size_t uri_len, tlvs_len;
  836. struct hlist_head sdreq_list;
  837. struct nfc_llcp_sdp_tlv *sdreq;
  838. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  839. !info->attrs[NFC_ATTR_LLC_SDP])
  840. return -EINVAL;
  841. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  842. dev = nfc_get_device(idx);
  843. if (!dev)
  844. return -ENODEV;
  845. device_lock(&dev->dev);
  846. if (dev->dep_link_up == false) {
  847. rc = -ENOLINK;
  848. goto exit;
  849. }
  850. local = nfc_llcp_find_local(dev);
  851. if (!local) {
  852. nfc_put_device(dev);
  853. rc = -ENODEV;
  854. goto exit;
  855. }
  856. INIT_HLIST_HEAD(&sdreq_list);
  857. tlvs_len = 0;
  858. nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
  859. rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
  860. nfc_sdp_genl_policy);
  861. if (rc != 0) {
  862. rc = -EINVAL;
  863. goto exit;
  864. }
  865. if (!sdp_attrs[NFC_SDP_ATTR_URI])
  866. continue;
  867. uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
  868. if (uri_len == 0)
  869. continue;
  870. uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
  871. if (uri == NULL || *uri == 0)
  872. continue;
  873. tid = local->sdreq_next_tid++;
  874. sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
  875. if (sdreq == NULL) {
  876. rc = -ENOMEM;
  877. goto exit;
  878. }
  879. tlvs_len += sdreq->tlv_len;
  880. hlist_add_head(&sdreq->node, &sdreq_list);
  881. }
  882. if (hlist_empty(&sdreq_list)) {
  883. rc = -EINVAL;
  884. goto exit;
  885. }
  886. rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
  887. exit:
  888. device_unlock(&dev->dev);
  889. nfc_put_device(dev);
  890. return rc;
  891. }
  892. static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
  893. {
  894. struct nfc_dev *dev;
  895. int rc;
  896. u32 idx;
  897. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  898. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  899. return -EINVAL;
  900. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  901. dev = nfc_get_device(idx);
  902. if (!dev)
  903. return -ENODEV;
  904. nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
  905. sizeof(firmware_name));
  906. rc = nfc_fw_download(dev, firmware_name);
  907. nfc_put_device(dev);
  908. return rc;
  909. }
  910. int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  911. u32 result)
  912. {
  913. struct sk_buff *msg;
  914. void *hdr;
  915. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  916. if (!msg)
  917. return -ENOMEM;
  918. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  919. NFC_CMD_FW_DOWNLOAD);
  920. if (!hdr)
  921. goto free_msg;
  922. if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
  923. nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
  924. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  925. goto nla_put_failure;
  926. genlmsg_end(msg, hdr);
  927. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  928. return 0;
  929. nla_put_failure:
  930. genlmsg_cancel(msg, hdr);
  931. free_msg:
  932. nlmsg_free(msg);
  933. return -EMSGSIZE;
  934. }
  935. static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
  936. {
  937. struct nfc_dev *dev;
  938. int rc;
  939. u32 idx, se_idx;
  940. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  941. !info->attrs[NFC_ATTR_SE_INDEX])
  942. return -EINVAL;
  943. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  944. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  945. dev = nfc_get_device(idx);
  946. if (!dev)
  947. return -ENODEV;
  948. rc = nfc_enable_se(dev, se_idx);
  949. nfc_put_device(dev);
  950. return rc;
  951. }
  952. static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
  953. {
  954. struct nfc_dev *dev;
  955. int rc;
  956. u32 idx, se_idx;
  957. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  958. !info->attrs[NFC_ATTR_SE_INDEX])
  959. return -EINVAL;
  960. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  961. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  962. dev = nfc_get_device(idx);
  963. if (!dev)
  964. return -ENODEV;
  965. rc = nfc_disable_se(dev, se_idx);
  966. nfc_put_device(dev);
  967. return rc;
  968. }
  969. static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
  970. u32 portid, u32 seq,
  971. struct netlink_callback *cb,
  972. int flags)
  973. {
  974. void *hdr;
  975. struct nfc_se *se, *n;
  976. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  977. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  978. NFC_CMD_GET_SE);
  979. if (!hdr)
  980. goto nla_put_failure;
  981. if (cb)
  982. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  983. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  984. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
  985. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
  986. goto nla_put_failure;
  987. genlmsg_end(msg, hdr);
  988. }
  989. return 0;
  990. nla_put_failure:
  991. genlmsg_cancel(msg, hdr);
  992. return -EMSGSIZE;
  993. }
  994. static int nfc_genl_dump_ses(struct sk_buff *skb,
  995. struct netlink_callback *cb)
  996. {
  997. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  998. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  999. bool first_call = false;
  1000. if (!iter) {
  1001. first_call = true;
  1002. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  1003. if (!iter)
  1004. return -ENOMEM;
  1005. cb->args[0] = (long) iter;
  1006. }
  1007. mutex_lock(&nfc_devlist_mutex);
  1008. cb->seq = nfc_devlist_generation;
  1009. if (first_call) {
  1010. nfc_device_iter_init(iter);
  1011. dev = nfc_device_iter_next(iter);
  1012. }
  1013. while (dev) {
  1014. int rc;
  1015. rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
  1016. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  1017. if (rc < 0)
  1018. break;
  1019. dev = nfc_device_iter_next(iter);
  1020. }
  1021. mutex_unlock(&nfc_devlist_mutex);
  1022. cb->args[1] = (long) dev;
  1023. return skb->len;
  1024. }
  1025. static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
  1026. {
  1027. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  1028. nfc_device_iter_exit(iter);
  1029. kfree(iter);
  1030. return 0;
  1031. }
  1032. static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
  1033. u8 *apdu, size_t apdu_length,
  1034. se_io_cb_t cb, void *cb_context)
  1035. {
  1036. struct nfc_se *se;
  1037. int rc;
  1038. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  1039. device_lock(&dev->dev);
  1040. if (!device_is_registered(&dev->dev)) {
  1041. rc = -ENODEV;
  1042. goto error;
  1043. }
  1044. if (!dev->dev_up) {
  1045. rc = -ENODEV;
  1046. goto error;
  1047. }
  1048. if (!dev->ops->se_io) {
  1049. rc = -EOPNOTSUPP;
  1050. goto error;
  1051. }
  1052. se = nfc_find_se(dev, se_idx);
  1053. if (!se) {
  1054. rc = -EINVAL;
  1055. goto error;
  1056. }
  1057. if (se->state != NFC_SE_ENABLED) {
  1058. rc = -ENODEV;
  1059. goto error;
  1060. }
  1061. rc = dev->ops->se_io(dev, se_idx, apdu,
  1062. apdu_length, cb, cb_context);
  1063. error:
  1064. device_unlock(&dev->dev);
  1065. return rc;
  1066. }
  1067. struct se_io_ctx {
  1068. u32 dev_idx;
  1069. u32 se_idx;
  1070. };
  1071. static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
  1072. {
  1073. struct se_io_ctx *ctx = context;
  1074. struct sk_buff *msg;
  1075. void *hdr;
  1076. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1077. if (!msg) {
  1078. kfree(ctx);
  1079. return;
  1080. }
  1081. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  1082. NFC_CMD_SE_IO);
  1083. if (!hdr)
  1084. goto free_msg;
  1085. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
  1086. nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
  1087. nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
  1088. goto nla_put_failure;
  1089. genlmsg_end(msg, hdr);
  1090. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  1091. kfree(ctx);
  1092. return;
  1093. nla_put_failure:
  1094. genlmsg_cancel(msg, hdr);
  1095. free_msg:
  1096. nlmsg_free(msg);
  1097. kfree(ctx);
  1098. return;
  1099. }
  1100. static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
  1101. {
  1102. struct nfc_dev *dev;
  1103. struct se_io_ctx *ctx;
  1104. u32 dev_idx, se_idx;
  1105. u8 *apdu;
  1106. size_t apdu_len;
  1107. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1108. !info->attrs[NFC_ATTR_SE_INDEX] ||
  1109. !info->attrs[NFC_ATTR_SE_APDU])
  1110. return -EINVAL;
  1111. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1112. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  1113. dev = nfc_get_device(dev_idx);
  1114. if (!dev)
  1115. return -ENODEV;
  1116. if (!dev->ops || !dev->ops->se_io)
  1117. return -ENOTSUPP;
  1118. apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
  1119. if (apdu_len == 0)
  1120. return -EINVAL;
  1121. apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
  1122. if (!apdu)
  1123. return -EINVAL;
  1124. ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
  1125. if (!ctx)
  1126. return -ENOMEM;
  1127. ctx->dev_idx = dev_idx;
  1128. ctx->se_idx = se_idx;
  1129. return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
  1130. }
  1131. static int nfc_genl_vendor_cmd(struct sk_buff *skb,
  1132. struct genl_info *info)
  1133. {
  1134. struct nfc_dev *dev;
  1135. struct nfc_vendor_cmd *cmd;
  1136. u32 dev_idx, vid, subcmd;
  1137. u8 *data;
  1138. size_t data_len;
  1139. int i, err;
  1140. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1141. !info->attrs[NFC_ATTR_VENDOR_ID] ||
  1142. !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
  1143. return -EINVAL;
  1144. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1145. vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
  1146. subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
  1147. dev = nfc_get_device(dev_idx);
  1148. if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
  1149. return -ENODEV;
  1150. if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
  1151. data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1152. data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1153. if (data_len == 0)
  1154. return -EINVAL;
  1155. } else {
  1156. data = NULL;
  1157. data_len = 0;
  1158. }
  1159. for (i = 0; i < dev->n_vendor_cmds; i++) {
  1160. cmd = &dev->vendor_cmds[i];
  1161. if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
  1162. continue;
  1163. dev->cur_cmd_info = info;
  1164. err = cmd->doit(dev, data, data_len);
  1165. dev->cur_cmd_info = NULL;
  1166. return err;
  1167. }
  1168. return -EOPNOTSUPP;
  1169. }
  1170. /* message building helper */
  1171. static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
  1172. int flags, u8 cmd)
  1173. {
  1174. /* since there is no private header just add the generic one */
  1175. return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
  1176. }
  1177. static struct sk_buff *
  1178. __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
  1179. u32 portid, u32 seq,
  1180. enum nfc_attrs attr,
  1181. u32 oui, u32 subcmd, gfp_t gfp)
  1182. {
  1183. struct sk_buff *skb;
  1184. void *hdr;
  1185. skb = nlmsg_new(approxlen + 100, gfp);
  1186. if (!skb)
  1187. return NULL;
  1188. hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
  1189. if (!hdr) {
  1190. kfree_skb(skb);
  1191. return NULL;
  1192. }
  1193. if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
  1194. goto nla_put_failure;
  1195. if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
  1196. goto nla_put_failure;
  1197. if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
  1198. goto nla_put_failure;
  1199. ((void **)skb->cb)[0] = dev;
  1200. ((void **)skb->cb)[1] = hdr;
  1201. return skb;
  1202. nla_put_failure:
  1203. kfree_skb(skb);
  1204. return NULL;
  1205. }
  1206. struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
  1207. enum nfc_attrs attr,
  1208. u32 oui, u32 subcmd,
  1209. int approxlen)
  1210. {
  1211. if (WARN_ON(!dev->cur_cmd_info))
  1212. return NULL;
  1213. return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
  1214. dev->cur_cmd_info->snd_portid,
  1215. dev->cur_cmd_info->snd_seq, attr,
  1216. oui, subcmd, GFP_KERNEL);
  1217. }
  1218. EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
  1219. int nfc_vendor_cmd_reply(struct sk_buff *skb)
  1220. {
  1221. struct nfc_dev *dev = ((void **)skb->cb)[0];
  1222. void *hdr = ((void **)skb->cb)[1];
  1223. /* clear CB data for netlink core to own from now on */
  1224. memset(skb->cb, 0, sizeof(skb->cb));
  1225. if (WARN_ON(!dev->cur_cmd_info)) {
  1226. kfree_skb(skb);
  1227. return -EINVAL;
  1228. }
  1229. genlmsg_end(skb, hdr);
  1230. return genlmsg_reply(skb, dev->cur_cmd_info);
  1231. }
  1232. EXPORT_SYMBOL(nfc_vendor_cmd_reply);
  1233. static const struct genl_ops nfc_genl_ops[] = {
  1234. {
  1235. .cmd = NFC_CMD_GET_DEVICE,
  1236. .doit = nfc_genl_get_device,
  1237. .dumpit = nfc_genl_dump_devices,
  1238. .done = nfc_genl_dump_devices_done,
  1239. .policy = nfc_genl_policy,
  1240. },
  1241. {
  1242. .cmd = NFC_CMD_DEV_UP,
  1243. .doit = nfc_genl_dev_up,
  1244. .policy = nfc_genl_policy,
  1245. },
  1246. {
  1247. .cmd = NFC_CMD_DEV_DOWN,
  1248. .doit = nfc_genl_dev_down,
  1249. .policy = nfc_genl_policy,
  1250. },
  1251. {
  1252. .cmd = NFC_CMD_START_POLL,
  1253. .doit = nfc_genl_start_poll,
  1254. .policy = nfc_genl_policy,
  1255. },
  1256. {
  1257. .cmd = NFC_CMD_STOP_POLL,
  1258. .doit = nfc_genl_stop_poll,
  1259. .policy = nfc_genl_policy,
  1260. },
  1261. {
  1262. .cmd = NFC_CMD_DEP_LINK_UP,
  1263. .doit = nfc_genl_dep_link_up,
  1264. .policy = nfc_genl_policy,
  1265. },
  1266. {
  1267. .cmd = NFC_CMD_DEP_LINK_DOWN,
  1268. .doit = nfc_genl_dep_link_down,
  1269. .policy = nfc_genl_policy,
  1270. },
  1271. {
  1272. .cmd = NFC_CMD_GET_TARGET,
  1273. .dumpit = nfc_genl_dump_targets,
  1274. .done = nfc_genl_dump_targets_done,
  1275. .policy = nfc_genl_policy,
  1276. },
  1277. {
  1278. .cmd = NFC_CMD_LLC_GET_PARAMS,
  1279. .doit = nfc_genl_llc_get_params,
  1280. .policy = nfc_genl_policy,
  1281. },
  1282. {
  1283. .cmd = NFC_CMD_LLC_SET_PARAMS,
  1284. .doit = nfc_genl_llc_set_params,
  1285. .policy = nfc_genl_policy,
  1286. },
  1287. {
  1288. .cmd = NFC_CMD_LLC_SDREQ,
  1289. .doit = nfc_genl_llc_sdreq,
  1290. .policy = nfc_genl_policy,
  1291. },
  1292. {
  1293. .cmd = NFC_CMD_FW_DOWNLOAD,
  1294. .doit = nfc_genl_fw_download,
  1295. .policy = nfc_genl_policy,
  1296. },
  1297. {
  1298. .cmd = NFC_CMD_ENABLE_SE,
  1299. .doit = nfc_genl_enable_se,
  1300. .policy = nfc_genl_policy,
  1301. },
  1302. {
  1303. .cmd = NFC_CMD_DISABLE_SE,
  1304. .doit = nfc_genl_disable_se,
  1305. .policy = nfc_genl_policy,
  1306. },
  1307. {
  1308. .cmd = NFC_CMD_GET_SE,
  1309. .dumpit = nfc_genl_dump_ses,
  1310. .done = nfc_genl_dump_ses_done,
  1311. .policy = nfc_genl_policy,
  1312. },
  1313. {
  1314. .cmd = NFC_CMD_SE_IO,
  1315. .doit = nfc_genl_se_io,
  1316. .policy = nfc_genl_policy,
  1317. },
  1318. {
  1319. .cmd = NFC_CMD_ACTIVATE_TARGET,
  1320. .doit = nfc_genl_activate_target,
  1321. .policy = nfc_genl_policy,
  1322. },
  1323. {
  1324. .cmd = NFC_CMD_VENDOR,
  1325. .doit = nfc_genl_vendor_cmd,
  1326. .policy = nfc_genl_policy,
  1327. },
  1328. };
  1329. struct urelease_work {
  1330. struct work_struct w;
  1331. u32 portid;
  1332. };
  1333. static void nfc_urelease_event_work(struct work_struct *work)
  1334. {
  1335. struct urelease_work *w = container_of(work, struct urelease_work, w);
  1336. struct class_dev_iter iter;
  1337. struct nfc_dev *dev;
  1338. pr_debug("portid %d\n", w->portid);
  1339. mutex_lock(&nfc_devlist_mutex);
  1340. nfc_device_iter_init(&iter);
  1341. dev = nfc_device_iter_next(&iter);
  1342. while (dev) {
  1343. mutex_lock(&dev->genl_data.genl_data_mutex);
  1344. if (dev->genl_data.poll_req_portid == w->portid) {
  1345. nfc_stop_poll(dev);
  1346. dev->genl_data.poll_req_portid = 0;
  1347. }
  1348. mutex_unlock(&dev->genl_data.genl_data_mutex);
  1349. dev = nfc_device_iter_next(&iter);
  1350. }
  1351. nfc_device_iter_exit(&iter);
  1352. mutex_unlock(&nfc_devlist_mutex);
  1353. kfree(w);
  1354. }
  1355. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  1356. unsigned long event, void *ptr)
  1357. {
  1358. struct netlink_notify *n = ptr;
  1359. struct urelease_work *w;
  1360. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  1361. goto out;
  1362. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  1363. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  1364. if (w) {
  1365. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  1366. w->portid = n->portid;
  1367. schedule_work((struct work_struct *) w);
  1368. }
  1369. out:
  1370. return NOTIFY_DONE;
  1371. }
  1372. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  1373. {
  1374. genl_data->poll_req_portid = 0;
  1375. mutex_init(&genl_data->genl_data_mutex);
  1376. }
  1377. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  1378. {
  1379. mutex_destroy(&genl_data->genl_data_mutex);
  1380. }
  1381. static struct notifier_block nl_notifier = {
  1382. .notifier_call = nfc_genl_rcv_nl_event,
  1383. };
  1384. /**
  1385. * nfc_genl_init() - Initialize netlink interface
  1386. *
  1387. * This initialization function registers the nfc netlink family.
  1388. */
  1389. int __init nfc_genl_init(void)
  1390. {
  1391. int rc;
  1392. rc = genl_register_family_with_ops_groups(&nfc_genl_family,
  1393. nfc_genl_ops,
  1394. nfc_genl_mcgrps);
  1395. if (rc)
  1396. return rc;
  1397. netlink_register_notifier(&nl_notifier);
  1398. return 0;
  1399. }
  1400. /**
  1401. * nfc_genl_exit() - Deinitialize netlink interface
  1402. *
  1403. * This exit function unregisters the nfc netlink family.
  1404. */
  1405. void nfc_genl_exit(void)
  1406. {
  1407. netlink_unregister_notifier(&nl_notifier);
  1408. genl_unregister_family(&nfc_genl_family);
  1409. }