netlink.c 39 KB

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