netlink.c 39 KB

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