netlink.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <[email protected]>
  6. * Aloisio Almeida Jr <[email protected]>
  7. *
  8. * Vendor commands implementation based on net/wireless/nl80211.c
  9. * which is:
  10. *
  11. * Copyright 2006-2010 Johannes Berg <[email protected]>
  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. int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
  429. {
  430. struct nfc_se *se;
  431. struct sk_buff *msg;
  432. void *hdr;
  433. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  434. if (!msg)
  435. return -ENOMEM;
  436. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  437. NFC_EVENT_SE_CONNECTIVITY);
  438. if (!hdr)
  439. goto free_msg;
  440. se = nfc_find_se(dev, se_idx);
  441. if (!se)
  442. goto free_msg;
  443. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  444. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
  445. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
  446. goto nla_put_failure;
  447. genlmsg_end(msg, hdr);
  448. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  449. return 0;
  450. nla_put_failure:
  451. genlmsg_cancel(msg, hdr);
  452. free_msg:
  453. nlmsg_free(msg);
  454. return -EMSGSIZE;
  455. }
  456. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  457. u32 portid, u32 seq,
  458. struct netlink_callback *cb,
  459. int flags)
  460. {
  461. void *hdr;
  462. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  463. NFC_CMD_GET_DEVICE);
  464. if (!hdr)
  465. return -EMSGSIZE;
  466. if (cb)
  467. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  468. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  469. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  470. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  471. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  472. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  473. goto nla_put_failure;
  474. genlmsg_end(msg, hdr);
  475. return 0;
  476. nla_put_failure:
  477. genlmsg_cancel(msg, hdr);
  478. return -EMSGSIZE;
  479. }
  480. static int nfc_genl_dump_devices(struct sk_buff *skb,
  481. struct netlink_callback *cb)
  482. {
  483. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  484. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  485. bool first_call = false;
  486. if (!iter) {
  487. first_call = true;
  488. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  489. if (!iter)
  490. return -ENOMEM;
  491. cb->args[0] = (long) iter;
  492. }
  493. mutex_lock(&nfc_devlist_mutex);
  494. cb->seq = nfc_devlist_generation;
  495. if (first_call) {
  496. nfc_device_iter_init(iter);
  497. dev = nfc_device_iter_next(iter);
  498. }
  499. while (dev) {
  500. int rc;
  501. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  502. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  503. if (rc < 0)
  504. break;
  505. dev = nfc_device_iter_next(iter);
  506. }
  507. mutex_unlock(&nfc_devlist_mutex);
  508. cb->args[1] = (long) dev;
  509. return skb->len;
  510. }
  511. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  512. {
  513. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  514. nfc_device_iter_exit(iter);
  515. kfree(iter);
  516. return 0;
  517. }
  518. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  519. u8 comm_mode, u8 rf_mode)
  520. {
  521. struct sk_buff *msg;
  522. void *hdr;
  523. pr_debug("DEP link is up\n");
  524. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  525. if (!msg)
  526. return -ENOMEM;
  527. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  528. if (!hdr)
  529. goto free_msg;
  530. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  531. goto nla_put_failure;
  532. if (rf_mode == NFC_RF_INITIATOR &&
  533. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  534. goto nla_put_failure;
  535. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  536. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  537. goto nla_put_failure;
  538. genlmsg_end(msg, hdr);
  539. dev->dep_link_up = true;
  540. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  541. return 0;
  542. nla_put_failure:
  543. genlmsg_cancel(msg, hdr);
  544. free_msg:
  545. nlmsg_free(msg);
  546. return -EMSGSIZE;
  547. }
  548. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  549. {
  550. struct sk_buff *msg;
  551. void *hdr;
  552. pr_debug("DEP link is down\n");
  553. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  554. if (!msg)
  555. return -ENOMEM;
  556. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  557. NFC_CMD_DEP_LINK_DOWN);
  558. if (!hdr)
  559. goto free_msg;
  560. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  561. goto nla_put_failure;
  562. genlmsg_end(msg, hdr);
  563. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  564. return 0;
  565. nla_put_failure:
  566. genlmsg_cancel(msg, hdr);
  567. free_msg:
  568. nlmsg_free(msg);
  569. return -EMSGSIZE;
  570. }
  571. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  572. {
  573. struct sk_buff *msg;
  574. struct nfc_dev *dev;
  575. u32 idx;
  576. int rc = -ENOBUFS;
  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. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  584. if (!msg) {
  585. rc = -ENOMEM;
  586. goto out_putdev;
  587. }
  588. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  589. NULL, 0);
  590. if (rc < 0)
  591. goto out_free;
  592. nfc_put_device(dev);
  593. return genlmsg_reply(msg, info);
  594. out_free:
  595. nlmsg_free(msg);
  596. out_putdev:
  597. nfc_put_device(dev);
  598. return rc;
  599. }
  600. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  601. {
  602. struct nfc_dev *dev;
  603. int rc;
  604. u32 idx;
  605. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  606. return -EINVAL;
  607. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  608. dev = nfc_get_device(idx);
  609. if (!dev)
  610. return -ENODEV;
  611. rc = nfc_dev_up(dev);
  612. nfc_put_device(dev);
  613. return rc;
  614. }
  615. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  616. {
  617. struct nfc_dev *dev;
  618. int rc;
  619. u32 idx;
  620. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  621. return -EINVAL;
  622. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  623. dev = nfc_get_device(idx);
  624. if (!dev)
  625. return -ENODEV;
  626. rc = nfc_dev_down(dev);
  627. nfc_put_device(dev);
  628. return rc;
  629. }
  630. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  631. {
  632. struct nfc_dev *dev;
  633. int rc;
  634. u32 idx;
  635. u32 im_protocols = 0, tm_protocols = 0;
  636. pr_debug("Poll start\n");
  637. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  638. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  639. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  640. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  641. return -EINVAL;
  642. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  643. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  644. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  645. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  646. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  647. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  648. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  649. dev = nfc_get_device(idx);
  650. if (!dev)
  651. return -ENODEV;
  652. mutex_lock(&dev->genl_data.genl_data_mutex);
  653. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  654. if (!rc)
  655. dev->genl_data.poll_req_portid = info->snd_portid;
  656. mutex_unlock(&dev->genl_data.genl_data_mutex);
  657. nfc_put_device(dev);
  658. return rc;
  659. }
  660. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  661. {
  662. struct nfc_dev *dev;
  663. int rc;
  664. u32 idx;
  665. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  666. return -EINVAL;
  667. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  668. dev = nfc_get_device(idx);
  669. if (!dev)
  670. return -ENODEV;
  671. device_lock(&dev->dev);
  672. if (!dev->polling) {
  673. device_unlock(&dev->dev);
  674. return -EINVAL;
  675. }
  676. device_unlock(&dev->dev);
  677. mutex_lock(&dev->genl_data.genl_data_mutex);
  678. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  679. rc = -EBUSY;
  680. goto out;
  681. }
  682. rc = nfc_stop_poll(dev);
  683. dev->genl_data.poll_req_portid = 0;
  684. out:
  685. mutex_unlock(&dev->genl_data.genl_data_mutex);
  686. nfc_put_device(dev);
  687. return rc;
  688. }
  689. static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
  690. {
  691. struct nfc_dev *dev;
  692. u32 device_idx, target_idx, protocol;
  693. int rc;
  694. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  695. !info->attrs[NFC_ATTR_TARGET_INDEX] ||
  696. !info->attrs[NFC_ATTR_PROTOCOLS])
  697. return -EINVAL;
  698. device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  699. dev = nfc_get_device(device_idx);
  700. if (!dev)
  701. return -ENODEV;
  702. target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  703. protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  704. nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
  705. rc = nfc_activate_target(dev, target_idx, protocol);
  706. nfc_put_device(dev);
  707. return 0;
  708. }
  709. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  710. {
  711. struct nfc_dev *dev;
  712. int rc, tgt_idx;
  713. u32 idx;
  714. u8 comm;
  715. pr_debug("DEP link up\n");
  716. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  717. !info->attrs[NFC_ATTR_COMM_MODE])
  718. return -EINVAL;
  719. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  720. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  721. tgt_idx = NFC_TARGET_IDX_ANY;
  722. else
  723. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  724. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  725. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  726. return -EINVAL;
  727. dev = nfc_get_device(idx);
  728. if (!dev)
  729. return -ENODEV;
  730. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  731. nfc_put_device(dev);
  732. return rc;
  733. }
  734. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  735. {
  736. struct nfc_dev *dev;
  737. int rc;
  738. u32 idx;
  739. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  740. !info->attrs[NFC_ATTR_TARGET_INDEX])
  741. return -EINVAL;
  742. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  743. dev = nfc_get_device(idx);
  744. if (!dev)
  745. return -ENODEV;
  746. rc = nfc_dep_link_down(dev);
  747. nfc_put_device(dev);
  748. return rc;
  749. }
  750. static int nfc_genl_send_params(struct sk_buff *msg,
  751. struct nfc_llcp_local *local,
  752. u32 portid, u32 seq)
  753. {
  754. void *hdr;
  755. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  756. NFC_CMD_LLC_GET_PARAMS);
  757. if (!hdr)
  758. return -EMSGSIZE;
  759. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  760. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  761. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  762. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  763. goto nla_put_failure;
  764. genlmsg_end(msg, hdr);
  765. return 0;
  766. nla_put_failure:
  767. genlmsg_cancel(msg, hdr);
  768. return -EMSGSIZE;
  769. }
  770. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  771. {
  772. struct nfc_dev *dev;
  773. struct nfc_llcp_local *local;
  774. int rc = 0;
  775. struct sk_buff *msg = NULL;
  776. u32 idx;
  777. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  778. !info->attrs[NFC_ATTR_FIRMWARE_NAME])
  779. return -EINVAL;
  780. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  781. dev = nfc_get_device(idx);
  782. if (!dev)
  783. return -ENODEV;
  784. device_lock(&dev->dev);
  785. local = nfc_llcp_find_local(dev);
  786. if (!local) {
  787. rc = -ENODEV;
  788. goto exit;
  789. }
  790. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  791. if (!msg) {
  792. rc = -ENOMEM;
  793. goto exit;
  794. }
  795. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  796. exit:
  797. device_unlock(&dev->dev);
  798. nfc_put_device(dev);
  799. if (rc < 0) {
  800. if (msg)
  801. nlmsg_free(msg);
  802. return rc;
  803. }
  804. return genlmsg_reply(msg, info);
  805. }
  806. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  807. {
  808. struct nfc_dev *dev;
  809. struct nfc_llcp_local *local;
  810. u8 rw = 0;
  811. u16 miux = 0;
  812. u32 idx;
  813. int rc = 0;
  814. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  815. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  816. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  817. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  818. return -EINVAL;
  819. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  820. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  821. if (rw > LLCP_MAX_RW)
  822. return -EINVAL;
  823. }
  824. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  825. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  826. if (miux > LLCP_MAX_MIUX)
  827. return -EINVAL;
  828. }
  829. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  830. dev = nfc_get_device(idx);
  831. if (!dev)
  832. return -ENODEV;
  833. device_lock(&dev->dev);
  834. local = nfc_llcp_find_local(dev);
  835. if (!local) {
  836. nfc_put_device(dev);
  837. rc = -ENODEV;
  838. goto exit;
  839. }
  840. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  841. if (dev->dep_link_up) {
  842. rc = -EINPROGRESS;
  843. goto exit;
  844. }
  845. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  846. }
  847. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  848. local->rw = rw;
  849. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  850. local->miux = cpu_to_be16(miux);
  851. exit:
  852. device_unlock(&dev->dev);
  853. nfc_put_device(dev);
  854. return rc;
  855. }
  856. static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
  857. {
  858. struct nfc_dev *dev;
  859. struct nfc_llcp_local *local;
  860. struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
  861. u32 idx;
  862. u8 tid;
  863. char *uri;
  864. int rc = 0, rem;
  865. size_t uri_len, tlvs_len;
  866. struct hlist_head sdreq_list;
  867. struct nfc_llcp_sdp_tlv *sdreq;
  868. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  869. !info->attrs[NFC_ATTR_LLC_SDP])
  870. return -EINVAL;
  871. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  872. dev = nfc_get_device(idx);
  873. if (!dev)
  874. return -ENODEV;
  875. device_lock(&dev->dev);
  876. if (dev->dep_link_up == false) {
  877. rc = -ENOLINK;
  878. goto exit;
  879. }
  880. local = nfc_llcp_find_local(dev);
  881. if (!local) {
  882. nfc_put_device(dev);
  883. rc = -ENODEV;
  884. goto exit;
  885. }
  886. INIT_HLIST_HEAD(&sdreq_list);
  887. tlvs_len = 0;
  888. nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
  889. rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
  890. nfc_sdp_genl_policy);
  891. if (rc != 0) {
  892. rc = -EINVAL;
  893. goto exit;
  894. }
  895. if (!sdp_attrs[NFC_SDP_ATTR_URI])
  896. continue;
  897. uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
  898. if (uri_len == 0)
  899. continue;
  900. uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
  901. if (uri == NULL || *uri == 0)
  902. continue;
  903. tid = local->sdreq_next_tid++;
  904. sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
  905. if (sdreq == NULL) {
  906. rc = -ENOMEM;
  907. goto exit;
  908. }
  909. tlvs_len += sdreq->tlv_len;
  910. hlist_add_head(&sdreq->node, &sdreq_list);
  911. }
  912. if (hlist_empty(&sdreq_list)) {
  913. rc = -EINVAL;
  914. goto exit;
  915. }
  916. rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
  917. exit:
  918. device_unlock(&dev->dev);
  919. nfc_put_device(dev);
  920. return rc;
  921. }
  922. static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
  923. {
  924. struct nfc_dev *dev;
  925. int rc;
  926. u32 idx;
  927. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  928. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  929. return -EINVAL;
  930. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  931. dev = nfc_get_device(idx);
  932. if (!dev)
  933. return -ENODEV;
  934. nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
  935. sizeof(firmware_name));
  936. rc = nfc_fw_download(dev, firmware_name);
  937. nfc_put_device(dev);
  938. return rc;
  939. }
  940. int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  941. u32 result)
  942. {
  943. struct sk_buff *msg;
  944. void *hdr;
  945. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  946. if (!msg)
  947. return -ENOMEM;
  948. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  949. NFC_CMD_FW_DOWNLOAD);
  950. if (!hdr)
  951. goto free_msg;
  952. if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
  953. nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
  954. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  955. goto nla_put_failure;
  956. genlmsg_end(msg, hdr);
  957. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  958. return 0;
  959. nla_put_failure:
  960. genlmsg_cancel(msg, hdr);
  961. free_msg:
  962. nlmsg_free(msg);
  963. return -EMSGSIZE;
  964. }
  965. static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
  966. {
  967. struct nfc_dev *dev;
  968. int rc;
  969. u32 idx, se_idx;
  970. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  971. !info->attrs[NFC_ATTR_SE_INDEX])
  972. return -EINVAL;
  973. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  974. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  975. dev = nfc_get_device(idx);
  976. if (!dev)
  977. return -ENODEV;
  978. rc = nfc_enable_se(dev, se_idx);
  979. nfc_put_device(dev);
  980. return rc;
  981. }
  982. static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
  983. {
  984. struct nfc_dev *dev;
  985. int rc;
  986. u32 idx, se_idx;
  987. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  988. !info->attrs[NFC_ATTR_SE_INDEX])
  989. return -EINVAL;
  990. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  991. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  992. dev = nfc_get_device(idx);
  993. if (!dev)
  994. return -ENODEV;
  995. rc = nfc_disable_se(dev, se_idx);
  996. nfc_put_device(dev);
  997. return rc;
  998. }
  999. static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
  1000. u32 portid, u32 seq,
  1001. struct netlink_callback *cb,
  1002. int flags)
  1003. {
  1004. void *hdr;
  1005. struct nfc_se *se, *n;
  1006. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  1007. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  1008. NFC_CMD_GET_SE);
  1009. if (!hdr)
  1010. goto nla_put_failure;
  1011. if (cb)
  1012. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  1013. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  1014. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
  1015. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
  1016. goto nla_put_failure;
  1017. genlmsg_end(msg, hdr);
  1018. }
  1019. return 0;
  1020. nla_put_failure:
  1021. genlmsg_cancel(msg, hdr);
  1022. return -EMSGSIZE;
  1023. }
  1024. static int nfc_genl_dump_ses(struct sk_buff *skb,
  1025. struct netlink_callback *cb)
  1026. {
  1027. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  1028. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  1029. bool first_call = false;
  1030. if (!iter) {
  1031. first_call = true;
  1032. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  1033. if (!iter)
  1034. return -ENOMEM;
  1035. cb->args[0] = (long) iter;
  1036. }
  1037. mutex_lock(&nfc_devlist_mutex);
  1038. cb->seq = nfc_devlist_generation;
  1039. if (first_call) {
  1040. nfc_device_iter_init(iter);
  1041. dev = nfc_device_iter_next(iter);
  1042. }
  1043. while (dev) {
  1044. int rc;
  1045. rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
  1046. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  1047. if (rc < 0)
  1048. break;
  1049. dev = nfc_device_iter_next(iter);
  1050. }
  1051. mutex_unlock(&nfc_devlist_mutex);
  1052. cb->args[1] = (long) dev;
  1053. return skb->len;
  1054. }
  1055. static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
  1056. {
  1057. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  1058. nfc_device_iter_exit(iter);
  1059. kfree(iter);
  1060. return 0;
  1061. }
  1062. static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
  1063. u8 *apdu, size_t apdu_length,
  1064. se_io_cb_t cb, void *cb_context)
  1065. {
  1066. struct nfc_se *se;
  1067. int rc;
  1068. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  1069. device_lock(&dev->dev);
  1070. if (!device_is_registered(&dev->dev)) {
  1071. rc = -ENODEV;
  1072. goto error;
  1073. }
  1074. if (!dev->dev_up) {
  1075. rc = -ENODEV;
  1076. goto error;
  1077. }
  1078. if (!dev->ops->se_io) {
  1079. rc = -EOPNOTSUPP;
  1080. goto error;
  1081. }
  1082. se = nfc_find_se(dev, se_idx);
  1083. if (!se) {
  1084. rc = -EINVAL;
  1085. goto error;
  1086. }
  1087. if (se->state != NFC_SE_ENABLED) {
  1088. rc = -ENODEV;
  1089. goto error;
  1090. }
  1091. rc = dev->ops->se_io(dev, se_idx, apdu,
  1092. apdu_length, cb, cb_context);
  1093. error:
  1094. device_unlock(&dev->dev);
  1095. return rc;
  1096. }
  1097. struct se_io_ctx {
  1098. u32 dev_idx;
  1099. u32 se_idx;
  1100. };
  1101. static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
  1102. {
  1103. struct se_io_ctx *ctx = context;
  1104. struct sk_buff *msg;
  1105. void *hdr;
  1106. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1107. if (!msg) {
  1108. kfree(ctx);
  1109. return;
  1110. }
  1111. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  1112. NFC_CMD_SE_IO);
  1113. if (!hdr)
  1114. goto free_msg;
  1115. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
  1116. nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
  1117. nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
  1118. goto nla_put_failure;
  1119. genlmsg_end(msg, hdr);
  1120. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  1121. kfree(ctx);
  1122. return;
  1123. nla_put_failure:
  1124. genlmsg_cancel(msg, hdr);
  1125. free_msg:
  1126. nlmsg_free(msg);
  1127. kfree(ctx);
  1128. return;
  1129. }
  1130. static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
  1131. {
  1132. struct nfc_dev *dev;
  1133. struct se_io_ctx *ctx;
  1134. u32 dev_idx, se_idx;
  1135. u8 *apdu;
  1136. size_t apdu_len;
  1137. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1138. !info->attrs[NFC_ATTR_SE_INDEX] ||
  1139. !info->attrs[NFC_ATTR_SE_APDU])
  1140. return -EINVAL;
  1141. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1142. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  1143. dev = nfc_get_device(dev_idx);
  1144. if (!dev)
  1145. return -ENODEV;
  1146. if (!dev->ops || !dev->ops->se_io)
  1147. return -ENOTSUPP;
  1148. apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
  1149. if (apdu_len == 0)
  1150. return -EINVAL;
  1151. apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
  1152. if (!apdu)
  1153. return -EINVAL;
  1154. ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
  1155. if (!ctx)
  1156. return -ENOMEM;
  1157. ctx->dev_idx = dev_idx;
  1158. ctx->se_idx = se_idx;
  1159. return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
  1160. }
  1161. static int nfc_genl_vendor_cmd(struct sk_buff *skb,
  1162. struct genl_info *info)
  1163. {
  1164. struct nfc_dev *dev;
  1165. struct nfc_vendor_cmd *cmd;
  1166. u32 dev_idx, vid, subcmd;
  1167. u8 *data;
  1168. size_t data_len;
  1169. int i, err;
  1170. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1171. !info->attrs[NFC_ATTR_VENDOR_ID] ||
  1172. !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
  1173. return -EINVAL;
  1174. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1175. vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
  1176. subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
  1177. dev = nfc_get_device(dev_idx);
  1178. if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
  1179. return -ENODEV;
  1180. if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
  1181. data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1182. data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1183. if (data_len == 0)
  1184. return -EINVAL;
  1185. } else {
  1186. data = NULL;
  1187. data_len = 0;
  1188. }
  1189. for (i = 0; i < dev->n_vendor_cmds; i++) {
  1190. cmd = &dev->vendor_cmds[i];
  1191. if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
  1192. continue;
  1193. dev->cur_cmd_info = info;
  1194. err = cmd->doit(dev, data, data_len);
  1195. dev->cur_cmd_info = NULL;
  1196. return err;
  1197. }
  1198. return -EOPNOTSUPP;
  1199. }
  1200. /* message building helper */
  1201. static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
  1202. int flags, u8 cmd)
  1203. {
  1204. /* since there is no private header just add the generic one */
  1205. return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
  1206. }
  1207. static struct sk_buff *
  1208. __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
  1209. u32 portid, u32 seq,
  1210. enum nfc_attrs attr,
  1211. u32 oui, u32 subcmd, gfp_t gfp)
  1212. {
  1213. struct sk_buff *skb;
  1214. void *hdr;
  1215. skb = nlmsg_new(approxlen + 100, gfp);
  1216. if (!skb)
  1217. return NULL;
  1218. hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
  1219. if (!hdr) {
  1220. kfree_skb(skb);
  1221. return NULL;
  1222. }
  1223. if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
  1224. goto nla_put_failure;
  1225. if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
  1226. goto nla_put_failure;
  1227. if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
  1228. goto nla_put_failure;
  1229. ((void **)skb->cb)[0] = dev;
  1230. ((void **)skb->cb)[1] = hdr;
  1231. return skb;
  1232. nla_put_failure:
  1233. kfree_skb(skb);
  1234. return NULL;
  1235. }
  1236. struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
  1237. enum nfc_attrs attr,
  1238. u32 oui, u32 subcmd,
  1239. int approxlen)
  1240. {
  1241. if (WARN_ON(!dev->cur_cmd_info))
  1242. return NULL;
  1243. return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
  1244. dev->cur_cmd_info->snd_portid,
  1245. dev->cur_cmd_info->snd_seq, attr,
  1246. oui, subcmd, GFP_KERNEL);
  1247. }
  1248. EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
  1249. int nfc_vendor_cmd_reply(struct sk_buff *skb)
  1250. {
  1251. struct nfc_dev *dev = ((void **)skb->cb)[0];
  1252. void *hdr = ((void **)skb->cb)[1];
  1253. /* clear CB data for netlink core to own from now on */
  1254. memset(skb->cb, 0, sizeof(skb->cb));
  1255. if (WARN_ON(!dev->cur_cmd_info)) {
  1256. kfree_skb(skb);
  1257. return -EINVAL;
  1258. }
  1259. genlmsg_end(skb, hdr);
  1260. return genlmsg_reply(skb, dev->cur_cmd_info);
  1261. }
  1262. EXPORT_SYMBOL(nfc_vendor_cmd_reply);
  1263. static const struct genl_ops nfc_genl_ops[] = {
  1264. {
  1265. .cmd = NFC_CMD_GET_DEVICE,
  1266. .doit = nfc_genl_get_device,
  1267. .dumpit = nfc_genl_dump_devices,
  1268. .done = nfc_genl_dump_devices_done,
  1269. .policy = nfc_genl_policy,
  1270. },
  1271. {
  1272. .cmd = NFC_CMD_DEV_UP,
  1273. .doit = nfc_genl_dev_up,
  1274. .policy = nfc_genl_policy,
  1275. },
  1276. {
  1277. .cmd = NFC_CMD_DEV_DOWN,
  1278. .doit = nfc_genl_dev_down,
  1279. .policy = nfc_genl_policy,
  1280. },
  1281. {
  1282. .cmd = NFC_CMD_START_POLL,
  1283. .doit = nfc_genl_start_poll,
  1284. .policy = nfc_genl_policy,
  1285. },
  1286. {
  1287. .cmd = NFC_CMD_STOP_POLL,
  1288. .doit = nfc_genl_stop_poll,
  1289. .policy = nfc_genl_policy,
  1290. },
  1291. {
  1292. .cmd = NFC_CMD_DEP_LINK_UP,
  1293. .doit = nfc_genl_dep_link_up,
  1294. .policy = nfc_genl_policy,
  1295. },
  1296. {
  1297. .cmd = NFC_CMD_DEP_LINK_DOWN,
  1298. .doit = nfc_genl_dep_link_down,
  1299. .policy = nfc_genl_policy,
  1300. },
  1301. {
  1302. .cmd = NFC_CMD_GET_TARGET,
  1303. .dumpit = nfc_genl_dump_targets,
  1304. .done = nfc_genl_dump_targets_done,
  1305. .policy = nfc_genl_policy,
  1306. },
  1307. {
  1308. .cmd = NFC_CMD_LLC_GET_PARAMS,
  1309. .doit = nfc_genl_llc_get_params,
  1310. .policy = nfc_genl_policy,
  1311. },
  1312. {
  1313. .cmd = NFC_CMD_LLC_SET_PARAMS,
  1314. .doit = nfc_genl_llc_set_params,
  1315. .policy = nfc_genl_policy,
  1316. },
  1317. {
  1318. .cmd = NFC_CMD_LLC_SDREQ,
  1319. .doit = nfc_genl_llc_sdreq,
  1320. .policy = nfc_genl_policy,
  1321. },
  1322. {
  1323. .cmd = NFC_CMD_FW_DOWNLOAD,
  1324. .doit = nfc_genl_fw_download,
  1325. .policy = nfc_genl_policy,
  1326. },
  1327. {
  1328. .cmd = NFC_CMD_ENABLE_SE,
  1329. .doit = nfc_genl_enable_se,
  1330. .policy = nfc_genl_policy,
  1331. },
  1332. {
  1333. .cmd = NFC_CMD_DISABLE_SE,
  1334. .doit = nfc_genl_disable_se,
  1335. .policy = nfc_genl_policy,
  1336. },
  1337. {
  1338. .cmd = NFC_CMD_GET_SE,
  1339. .dumpit = nfc_genl_dump_ses,
  1340. .done = nfc_genl_dump_ses_done,
  1341. .policy = nfc_genl_policy,
  1342. },
  1343. {
  1344. .cmd = NFC_CMD_SE_IO,
  1345. .doit = nfc_genl_se_io,
  1346. .policy = nfc_genl_policy,
  1347. },
  1348. {
  1349. .cmd = NFC_CMD_ACTIVATE_TARGET,
  1350. .doit = nfc_genl_activate_target,
  1351. .policy = nfc_genl_policy,
  1352. },
  1353. {
  1354. .cmd = NFC_CMD_VENDOR,
  1355. .doit = nfc_genl_vendor_cmd,
  1356. .policy = nfc_genl_policy,
  1357. },
  1358. };
  1359. struct urelease_work {
  1360. struct work_struct w;
  1361. u32 portid;
  1362. };
  1363. static void nfc_urelease_event_work(struct work_struct *work)
  1364. {
  1365. struct urelease_work *w = container_of(work, struct urelease_work, w);
  1366. struct class_dev_iter iter;
  1367. struct nfc_dev *dev;
  1368. pr_debug("portid %d\n", w->portid);
  1369. mutex_lock(&nfc_devlist_mutex);
  1370. nfc_device_iter_init(&iter);
  1371. dev = nfc_device_iter_next(&iter);
  1372. while (dev) {
  1373. mutex_lock(&dev->genl_data.genl_data_mutex);
  1374. if (dev->genl_data.poll_req_portid == w->portid) {
  1375. nfc_stop_poll(dev);
  1376. dev->genl_data.poll_req_portid = 0;
  1377. }
  1378. mutex_unlock(&dev->genl_data.genl_data_mutex);
  1379. dev = nfc_device_iter_next(&iter);
  1380. }
  1381. nfc_device_iter_exit(&iter);
  1382. mutex_unlock(&nfc_devlist_mutex);
  1383. kfree(w);
  1384. }
  1385. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  1386. unsigned long event, void *ptr)
  1387. {
  1388. struct netlink_notify *n = ptr;
  1389. struct urelease_work *w;
  1390. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  1391. goto out;
  1392. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  1393. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  1394. if (w) {
  1395. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  1396. w->portid = n->portid;
  1397. schedule_work((struct work_struct *) w);
  1398. }
  1399. out:
  1400. return NOTIFY_DONE;
  1401. }
  1402. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  1403. {
  1404. genl_data->poll_req_portid = 0;
  1405. mutex_init(&genl_data->genl_data_mutex);
  1406. }
  1407. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  1408. {
  1409. mutex_destroy(&genl_data->genl_data_mutex);
  1410. }
  1411. static struct notifier_block nl_notifier = {
  1412. .notifier_call = nfc_genl_rcv_nl_event,
  1413. };
  1414. /**
  1415. * nfc_genl_init() - Initialize netlink interface
  1416. *
  1417. * This initialization function registers the nfc netlink family.
  1418. */
  1419. int __init nfc_genl_init(void)
  1420. {
  1421. int rc;
  1422. rc = genl_register_family_with_ops_groups(&nfc_genl_family,
  1423. nfc_genl_ops,
  1424. nfc_genl_mcgrps);
  1425. if (rc)
  1426. return rc;
  1427. netlink_register_notifier(&nl_notifier);
  1428. return 0;
  1429. }
  1430. /**
  1431. * nfc_genl_exit() - Deinitialize netlink interface
  1432. *
  1433. * This exit function unregisters the nfc netlink family.
  1434. */
  1435. void nfc_genl_exit(void)
  1436. {
  1437. netlink_unregister_notifier(&nl_notifier);
  1438. genl_unregister_family(&nfc_genl_family);
  1439. }