netlink.c 35 KB

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