netlink.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  24. #include <net/genetlink.h>
  25. #include <linux/nfc.h>
  26. #include <linux/slab.h>
  27. #include "nfc.h"
  28. #include "llcp.h"
  29. static struct genl_multicast_group nfc_genl_event_mcgrp = {
  30. .name = NFC_GENL_MCAST_EVENT_NAME,
  31. };
  32. static struct genl_family nfc_genl_family = {
  33. .id = GENL_ID_GENERATE,
  34. .hdrsize = 0,
  35. .name = NFC_GENL_NAME,
  36. .version = NFC_GENL_VERSION,
  37. .maxattr = NFC_ATTR_MAX,
  38. };
  39. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  40. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  41. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  42. .len = NFC_DEVICE_NAME_MAXSIZE },
  43. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  44. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  45. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  46. [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  47. [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
  48. [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
  49. [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
  50. [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
  51. [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
  52. [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
  53. [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
  54. .len = NFC_FIRMWARE_NAME_MAXSIZE },
  55. };
  56. static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
  57. [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
  58. [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
  59. };
  60. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  61. struct netlink_callback *cb, int flags)
  62. {
  63. void *hdr;
  64. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  65. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  66. if (!hdr)
  67. return -EMSGSIZE;
  68. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  69. if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
  70. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
  71. nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
  72. nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
  73. goto nla_put_failure;
  74. if (target->nfcid1_len > 0 &&
  75. nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  76. target->nfcid1))
  77. goto nla_put_failure;
  78. if (target->sensb_res_len > 0 &&
  79. nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
  80. target->sensb_res))
  81. goto nla_put_failure;
  82. if (target->sensf_res_len > 0 &&
  83. nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
  84. target->sensf_res))
  85. goto nla_put_failure;
  86. return genlmsg_end(msg, hdr);
  87. nla_put_failure:
  88. genlmsg_cancel(msg, hdr);
  89. return -EMSGSIZE;
  90. }
  91. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  92. {
  93. struct nfc_dev *dev;
  94. int rc;
  95. u32 idx;
  96. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  97. nfc_genl_family.attrbuf,
  98. nfc_genl_family.maxattr,
  99. nfc_genl_policy);
  100. if (rc < 0)
  101. return ERR_PTR(rc);
  102. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  103. return ERR_PTR(-EINVAL);
  104. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  105. dev = nfc_get_device(idx);
  106. if (!dev)
  107. return ERR_PTR(-ENODEV);
  108. return dev;
  109. }
  110. static int nfc_genl_dump_targets(struct sk_buff *skb,
  111. struct netlink_callback *cb)
  112. {
  113. int i = cb->args[0];
  114. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  115. int rc;
  116. if (!dev) {
  117. dev = __get_device_from_cb(cb);
  118. if (IS_ERR(dev))
  119. return PTR_ERR(dev);
  120. cb->args[1] = (long) dev;
  121. }
  122. device_lock(&dev->dev);
  123. cb->seq = dev->targets_generation;
  124. while (i < dev->n_targets) {
  125. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  126. NLM_F_MULTI);
  127. if (rc < 0)
  128. break;
  129. i++;
  130. }
  131. device_unlock(&dev->dev);
  132. cb->args[0] = i;
  133. return skb->len;
  134. }
  135. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  136. {
  137. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  138. if (dev)
  139. nfc_put_device(dev);
  140. return 0;
  141. }
  142. int nfc_genl_targets_found(struct nfc_dev *dev)
  143. {
  144. struct sk_buff *msg;
  145. void *hdr;
  146. dev->genl_data.poll_req_portid = 0;
  147. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  148. if (!msg)
  149. return -ENOMEM;
  150. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  151. NFC_EVENT_TARGETS_FOUND);
  152. if (!hdr)
  153. goto free_msg;
  154. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  155. goto nla_put_failure;
  156. genlmsg_end(msg, hdr);
  157. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  158. nla_put_failure:
  159. genlmsg_cancel(msg, hdr);
  160. free_msg:
  161. nlmsg_free(msg);
  162. return -EMSGSIZE;
  163. }
  164. int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
  165. {
  166. struct sk_buff *msg;
  167. void *hdr;
  168. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  169. if (!msg)
  170. return -ENOMEM;
  171. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  172. NFC_EVENT_TARGET_LOST);
  173. if (!hdr)
  174. goto free_msg;
  175. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  176. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  177. goto nla_put_failure;
  178. genlmsg_end(msg, hdr);
  179. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  180. return 0;
  181. nla_put_failure:
  182. genlmsg_cancel(msg, hdr);
  183. free_msg:
  184. nlmsg_free(msg);
  185. return -EMSGSIZE;
  186. }
  187. int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
  188. {
  189. struct sk_buff *msg;
  190. void *hdr;
  191. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  192. if (!msg)
  193. return -ENOMEM;
  194. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  195. NFC_EVENT_TM_ACTIVATED);
  196. if (!hdr)
  197. goto free_msg;
  198. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  199. goto nla_put_failure;
  200. if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
  201. goto nla_put_failure;
  202. genlmsg_end(msg, hdr);
  203. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  204. return 0;
  205. nla_put_failure:
  206. genlmsg_cancel(msg, hdr);
  207. free_msg:
  208. nlmsg_free(msg);
  209. return -EMSGSIZE;
  210. }
  211. int nfc_genl_tm_deactivated(struct nfc_dev *dev)
  212. {
  213. struct sk_buff *msg;
  214. void *hdr;
  215. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  216. if (!msg)
  217. return -ENOMEM;
  218. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  219. NFC_EVENT_TM_DEACTIVATED);
  220. if (!hdr)
  221. goto free_msg;
  222. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  223. goto nla_put_failure;
  224. genlmsg_end(msg, hdr);
  225. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  226. return 0;
  227. nla_put_failure:
  228. genlmsg_cancel(msg, hdr);
  229. free_msg:
  230. nlmsg_free(msg);
  231. return -EMSGSIZE;
  232. }
  233. int nfc_genl_device_added(struct nfc_dev *dev)
  234. {
  235. struct sk_buff *msg;
  236. void *hdr;
  237. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  238. if (!msg)
  239. return -ENOMEM;
  240. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  241. NFC_EVENT_DEVICE_ADDED);
  242. if (!hdr)
  243. goto free_msg;
  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. goto nla_put_failure;
  249. genlmsg_end(msg, hdr);
  250. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  251. return 0;
  252. nla_put_failure:
  253. genlmsg_cancel(msg, hdr);
  254. free_msg:
  255. nlmsg_free(msg);
  256. return -EMSGSIZE;
  257. }
  258. int nfc_genl_device_removed(struct nfc_dev *dev)
  259. {
  260. struct sk_buff *msg;
  261. void *hdr;
  262. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  263. if (!msg)
  264. return -ENOMEM;
  265. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  266. NFC_EVENT_DEVICE_REMOVED);
  267. if (!hdr)
  268. goto free_msg;
  269. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  270. goto nla_put_failure;
  271. genlmsg_end(msg, hdr);
  272. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  273. return 0;
  274. nla_put_failure:
  275. genlmsg_cancel(msg, hdr);
  276. free_msg:
  277. nlmsg_free(msg);
  278. return -EMSGSIZE;
  279. }
  280. int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
  281. {
  282. struct sk_buff *msg;
  283. struct nlattr *sdp_attr, *uri_attr;
  284. struct nfc_llcp_sdp_tlv *sdres;
  285. struct hlist_node *n;
  286. void *hdr;
  287. int rc = -EMSGSIZE;
  288. int i;
  289. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  290. if (!msg)
  291. return -ENOMEM;
  292. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  293. NFC_EVENT_LLC_SDRES);
  294. if (!hdr)
  295. goto free_msg;
  296. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  297. goto nla_put_failure;
  298. sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
  299. if (sdp_attr == NULL) {
  300. rc = -ENOMEM;
  301. goto nla_put_failure;
  302. }
  303. i = 1;
  304. hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
  305. pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
  306. uri_attr = nla_nest_start(msg, i++);
  307. if (uri_attr == NULL) {
  308. rc = -ENOMEM;
  309. goto nla_put_failure;
  310. }
  311. if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
  312. goto nla_put_failure;
  313. if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
  314. goto nla_put_failure;
  315. nla_nest_end(msg, uri_attr);
  316. hlist_del(&sdres->node);
  317. nfc_llcp_free_sdp_tlv(sdres);
  318. }
  319. nla_nest_end(msg, sdp_attr);
  320. genlmsg_end(msg, hdr);
  321. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  322. nla_put_failure:
  323. genlmsg_cancel(msg, hdr);
  324. free_msg:
  325. nlmsg_free(msg);
  326. nfc_llcp_free_sdp_tlv_list(sdres_list);
  327. return rc;
  328. }
  329. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  330. u32 portid, u32 seq,
  331. struct netlink_callback *cb,
  332. int flags)
  333. {
  334. void *hdr;
  335. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  336. NFC_CMD_GET_DEVICE);
  337. if (!hdr)
  338. return -EMSGSIZE;
  339. if (cb)
  340. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  341. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  342. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  343. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  344. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  345. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  346. goto nla_put_failure;
  347. return genlmsg_end(msg, hdr);
  348. nla_put_failure:
  349. genlmsg_cancel(msg, hdr);
  350. return -EMSGSIZE;
  351. }
  352. static int nfc_genl_dump_devices(struct sk_buff *skb,
  353. struct netlink_callback *cb)
  354. {
  355. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  356. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  357. bool first_call = false;
  358. if (!iter) {
  359. first_call = true;
  360. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  361. if (!iter)
  362. return -ENOMEM;
  363. cb->args[0] = (long) iter;
  364. }
  365. mutex_lock(&nfc_devlist_mutex);
  366. cb->seq = nfc_devlist_generation;
  367. if (first_call) {
  368. nfc_device_iter_init(iter);
  369. dev = nfc_device_iter_next(iter);
  370. }
  371. while (dev) {
  372. int rc;
  373. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  374. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  375. if (rc < 0)
  376. break;
  377. dev = nfc_device_iter_next(iter);
  378. }
  379. mutex_unlock(&nfc_devlist_mutex);
  380. cb->args[1] = (long) dev;
  381. return skb->len;
  382. }
  383. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  384. {
  385. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  386. nfc_device_iter_exit(iter);
  387. kfree(iter);
  388. return 0;
  389. }
  390. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  391. u8 comm_mode, u8 rf_mode)
  392. {
  393. struct sk_buff *msg;
  394. void *hdr;
  395. pr_debug("DEP link is up\n");
  396. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  397. if (!msg)
  398. return -ENOMEM;
  399. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  400. if (!hdr)
  401. goto free_msg;
  402. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  403. goto nla_put_failure;
  404. if (rf_mode == NFC_RF_INITIATOR &&
  405. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  406. goto nla_put_failure;
  407. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  408. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  409. goto nla_put_failure;
  410. genlmsg_end(msg, hdr);
  411. dev->dep_link_up = true;
  412. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  413. return 0;
  414. nla_put_failure:
  415. genlmsg_cancel(msg, hdr);
  416. free_msg:
  417. nlmsg_free(msg);
  418. return -EMSGSIZE;
  419. }
  420. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  421. {
  422. struct sk_buff *msg;
  423. void *hdr;
  424. pr_debug("DEP link is down\n");
  425. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  426. if (!msg)
  427. return -ENOMEM;
  428. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  429. NFC_CMD_DEP_LINK_DOWN);
  430. if (!hdr)
  431. goto free_msg;
  432. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  433. goto nla_put_failure;
  434. genlmsg_end(msg, hdr);
  435. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  436. return 0;
  437. nla_put_failure:
  438. genlmsg_cancel(msg, hdr);
  439. free_msg:
  440. nlmsg_free(msg);
  441. return -EMSGSIZE;
  442. }
  443. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  444. {
  445. struct sk_buff *msg;
  446. struct nfc_dev *dev;
  447. u32 idx;
  448. int rc = -ENOBUFS;
  449. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  450. return -EINVAL;
  451. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  452. dev = nfc_get_device(idx);
  453. if (!dev)
  454. return -ENODEV;
  455. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  456. if (!msg) {
  457. rc = -ENOMEM;
  458. goto out_putdev;
  459. }
  460. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  461. NULL, 0);
  462. if (rc < 0)
  463. goto out_free;
  464. nfc_put_device(dev);
  465. return genlmsg_reply(msg, info);
  466. out_free:
  467. nlmsg_free(msg);
  468. out_putdev:
  469. nfc_put_device(dev);
  470. return rc;
  471. }
  472. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  473. {
  474. struct nfc_dev *dev;
  475. int rc;
  476. u32 idx;
  477. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  478. return -EINVAL;
  479. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  480. dev = nfc_get_device(idx);
  481. if (!dev)
  482. return -ENODEV;
  483. rc = nfc_dev_up(dev);
  484. nfc_put_device(dev);
  485. return rc;
  486. }
  487. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  488. {
  489. struct nfc_dev *dev;
  490. int rc;
  491. u32 idx;
  492. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  493. return -EINVAL;
  494. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  495. dev = nfc_get_device(idx);
  496. if (!dev)
  497. return -ENODEV;
  498. rc = nfc_dev_down(dev);
  499. nfc_put_device(dev);
  500. return rc;
  501. }
  502. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  503. {
  504. struct nfc_dev *dev;
  505. int rc;
  506. u32 idx;
  507. u32 im_protocols = 0, tm_protocols = 0;
  508. pr_debug("Poll start\n");
  509. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  510. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  511. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  512. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  513. return -EINVAL;
  514. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  515. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  516. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  517. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  518. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  519. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  520. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  521. dev = nfc_get_device(idx);
  522. if (!dev)
  523. return -ENODEV;
  524. mutex_lock(&dev->genl_data.genl_data_mutex);
  525. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  526. if (!rc)
  527. dev->genl_data.poll_req_portid = info->snd_portid;
  528. mutex_unlock(&dev->genl_data.genl_data_mutex);
  529. nfc_put_device(dev);
  530. return rc;
  531. }
  532. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  533. {
  534. struct nfc_dev *dev;
  535. int rc;
  536. u32 idx;
  537. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  538. return -EINVAL;
  539. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  540. dev = nfc_get_device(idx);
  541. if (!dev)
  542. return -ENODEV;
  543. device_lock(&dev->dev);
  544. if (!dev->polling) {
  545. device_unlock(&dev->dev);
  546. return -EINVAL;
  547. }
  548. device_unlock(&dev->dev);
  549. mutex_lock(&dev->genl_data.genl_data_mutex);
  550. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  551. rc = -EBUSY;
  552. goto out;
  553. }
  554. rc = nfc_stop_poll(dev);
  555. dev->genl_data.poll_req_portid = 0;
  556. out:
  557. mutex_unlock(&dev->genl_data.genl_data_mutex);
  558. nfc_put_device(dev);
  559. return rc;
  560. }
  561. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  562. {
  563. struct nfc_dev *dev;
  564. int rc, tgt_idx;
  565. u32 idx;
  566. u8 comm;
  567. pr_debug("DEP link up\n");
  568. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  569. !info->attrs[NFC_ATTR_COMM_MODE])
  570. return -EINVAL;
  571. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  572. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  573. tgt_idx = NFC_TARGET_IDX_ANY;
  574. else
  575. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  576. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  577. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  578. return -EINVAL;
  579. dev = nfc_get_device(idx);
  580. if (!dev)
  581. return -ENODEV;
  582. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  583. nfc_put_device(dev);
  584. return rc;
  585. }
  586. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  587. {
  588. struct nfc_dev *dev;
  589. int rc;
  590. u32 idx;
  591. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  592. return -EINVAL;
  593. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  594. dev = nfc_get_device(idx);
  595. if (!dev)
  596. return -ENODEV;
  597. rc = nfc_dep_link_down(dev);
  598. nfc_put_device(dev);
  599. return rc;
  600. }
  601. static int nfc_genl_send_params(struct sk_buff *msg,
  602. struct nfc_llcp_local *local,
  603. u32 portid, u32 seq)
  604. {
  605. void *hdr;
  606. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  607. NFC_CMD_LLC_GET_PARAMS);
  608. if (!hdr)
  609. return -EMSGSIZE;
  610. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  611. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  612. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  613. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  614. goto nla_put_failure;
  615. return genlmsg_end(msg, hdr);
  616. nla_put_failure:
  617. genlmsg_cancel(msg, hdr);
  618. return -EMSGSIZE;
  619. }
  620. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  621. {
  622. struct nfc_dev *dev;
  623. struct nfc_llcp_local *local;
  624. int rc = 0;
  625. struct sk_buff *msg = NULL;
  626. u32 idx;
  627. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  628. return -EINVAL;
  629. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  630. dev = nfc_get_device(idx);
  631. if (!dev)
  632. return -ENODEV;
  633. device_lock(&dev->dev);
  634. local = nfc_llcp_find_local(dev);
  635. if (!local) {
  636. rc = -ENODEV;
  637. goto exit;
  638. }
  639. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  640. if (!msg) {
  641. rc = -ENOMEM;
  642. goto exit;
  643. }
  644. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  645. exit:
  646. device_unlock(&dev->dev);
  647. nfc_put_device(dev);
  648. if (rc < 0) {
  649. if (msg)
  650. nlmsg_free(msg);
  651. return rc;
  652. }
  653. return genlmsg_reply(msg, info);
  654. }
  655. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  656. {
  657. struct nfc_dev *dev;
  658. struct nfc_llcp_local *local;
  659. u8 rw = 0;
  660. u16 miux = 0;
  661. u32 idx;
  662. int rc = 0;
  663. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  664. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  665. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  666. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  667. return -EINVAL;
  668. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  669. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  670. if (rw > LLCP_MAX_RW)
  671. return -EINVAL;
  672. }
  673. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  674. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  675. if (miux > LLCP_MAX_MIUX)
  676. return -EINVAL;
  677. }
  678. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  679. dev = nfc_get_device(idx);
  680. if (!dev)
  681. return -ENODEV;
  682. device_lock(&dev->dev);
  683. local = nfc_llcp_find_local(dev);
  684. if (!local) {
  685. nfc_put_device(dev);
  686. rc = -ENODEV;
  687. goto exit;
  688. }
  689. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  690. if (dev->dep_link_up) {
  691. rc = -EINPROGRESS;
  692. goto exit;
  693. }
  694. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  695. }
  696. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  697. local->rw = rw;
  698. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  699. local->miux = cpu_to_be16(miux);
  700. exit:
  701. device_unlock(&dev->dev);
  702. nfc_put_device(dev);
  703. return rc;
  704. }
  705. static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
  706. {
  707. struct nfc_dev *dev;
  708. struct nfc_llcp_local *local;
  709. struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
  710. u32 idx;
  711. u8 tid;
  712. char *uri;
  713. int rc = 0, rem;
  714. size_t uri_len, tlvs_len;
  715. struct hlist_head sdreq_list;
  716. struct nfc_llcp_sdp_tlv *sdreq;
  717. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  718. !info->attrs[NFC_ATTR_LLC_SDP])
  719. return -EINVAL;
  720. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  721. dev = nfc_get_device(idx);
  722. if (!dev) {
  723. rc = -ENODEV;
  724. goto exit;
  725. }
  726. device_lock(&dev->dev);
  727. if (dev->dep_link_up == false) {
  728. rc = -ENOLINK;
  729. goto exit;
  730. }
  731. local = nfc_llcp_find_local(dev);
  732. if (!local) {
  733. nfc_put_device(dev);
  734. rc = -ENODEV;
  735. goto exit;
  736. }
  737. INIT_HLIST_HEAD(&sdreq_list);
  738. tlvs_len = 0;
  739. nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
  740. rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
  741. nfc_sdp_genl_policy);
  742. if (rc != 0) {
  743. rc = -EINVAL;
  744. goto exit;
  745. }
  746. if (!sdp_attrs[NFC_SDP_ATTR_URI])
  747. continue;
  748. uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
  749. if (uri_len == 0)
  750. continue;
  751. uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
  752. if (uri == NULL || *uri == 0)
  753. continue;
  754. tid = local->sdreq_next_tid++;
  755. sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
  756. if (sdreq == NULL) {
  757. rc = -ENOMEM;
  758. goto exit;
  759. }
  760. tlvs_len += sdreq->tlv_len;
  761. hlist_add_head(&sdreq->node, &sdreq_list);
  762. }
  763. if (hlist_empty(&sdreq_list)) {
  764. rc = -EINVAL;
  765. goto exit;
  766. }
  767. rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
  768. exit:
  769. device_unlock(&dev->dev);
  770. nfc_put_device(dev);
  771. return rc;
  772. }
  773. static int nfc_genl_fw_upload(struct sk_buff *skb, struct genl_info *info)
  774. {
  775. struct nfc_dev *dev;
  776. int rc;
  777. u32 idx;
  778. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  779. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  780. return -EINVAL;
  781. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  782. dev = nfc_get_device(idx);
  783. if (!dev)
  784. return -ENODEV;
  785. nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
  786. sizeof(firmware_name));
  787. rc = nfc_fw_upload(dev, firmware_name);
  788. nfc_put_device(dev);
  789. return rc;
  790. }
  791. int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
  792. {
  793. struct sk_buff *msg;
  794. void *hdr;
  795. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  796. if (!msg)
  797. return -ENOMEM;
  798. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  799. NFC_CMD_FW_UPLOAD);
  800. if (!hdr)
  801. goto free_msg;
  802. if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
  803. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  804. goto nla_put_failure;
  805. genlmsg_end(msg, hdr);
  806. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  807. return 0;
  808. nla_put_failure:
  809. genlmsg_cancel(msg, hdr);
  810. free_msg:
  811. nlmsg_free(msg);
  812. return -EMSGSIZE;
  813. }
  814. static struct genl_ops nfc_genl_ops[] = {
  815. {
  816. .cmd = NFC_CMD_GET_DEVICE,
  817. .doit = nfc_genl_get_device,
  818. .dumpit = nfc_genl_dump_devices,
  819. .done = nfc_genl_dump_devices_done,
  820. .policy = nfc_genl_policy,
  821. },
  822. {
  823. .cmd = NFC_CMD_DEV_UP,
  824. .doit = nfc_genl_dev_up,
  825. .policy = nfc_genl_policy,
  826. },
  827. {
  828. .cmd = NFC_CMD_DEV_DOWN,
  829. .doit = nfc_genl_dev_down,
  830. .policy = nfc_genl_policy,
  831. },
  832. {
  833. .cmd = NFC_CMD_START_POLL,
  834. .doit = nfc_genl_start_poll,
  835. .policy = nfc_genl_policy,
  836. },
  837. {
  838. .cmd = NFC_CMD_STOP_POLL,
  839. .doit = nfc_genl_stop_poll,
  840. .policy = nfc_genl_policy,
  841. },
  842. {
  843. .cmd = NFC_CMD_DEP_LINK_UP,
  844. .doit = nfc_genl_dep_link_up,
  845. .policy = nfc_genl_policy,
  846. },
  847. {
  848. .cmd = NFC_CMD_DEP_LINK_DOWN,
  849. .doit = nfc_genl_dep_link_down,
  850. .policy = nfc_genl_policy,
  851. },
  852. {
  853. .cmd = NFC_CMD_GET_TARGET,
  854. .dumpit = nfc_genl_dump_targets,
  855. .done = nfc_genl_dump_targets_done,
  856. .policy = nfc_genl_policy,
  857. },
  858. {
  859. .cmd = NFC_CMD_LLC_GET_PARAMS,
  860. .doit = nfc_genl_llc_get_params,
  861. .policy = nfc_genl_policy,
  862. },
  863. {
  864. .cmd = NFC_CMD_LLC_SET_PARAMS,
  865. .doit = nfc_genl_llc_set_params,
  866. .policy = nfc_genl_policy,
  867. },
  868. {
  869. .cmd = NFC_CMD_LLC_SDREQ,
  870. .doit = nfc_genl_llc_sdreq,
  871. .policy = nfc_genl_policy,
  872. },
  873. {
  874. .cmd = NFC_CMD_FW_UPLOAD,
  875. .doit = nfc_genl_fw_upload,
  876. .policy = nfc_genl_policy,
  877. },
  878. };
  879. struct urelease_work {
  880. struct work_struct w;
  881. int portid;
  882. };
  883. static void nfc_urelease_event_work(struct work_struct *work)
  884. {
  885. struct urelease_work *w = container_of(work, struct urelease_work, w);
  886. struct class_dev_iter iter;
  887. struct nfc_dev *dev;
  888. pr_debug("portid %d\n", w->portid);
  889. mutex_lock(&nfc_devlist_mutex);
  890. nfc_device_iter_init(&iter);
  891. dev = nfc_device_iter_next(&iter);
  892. while (dev) {
  893. mutex_lock(&dev->genl_data.genl_data_mutex);
  894. if (dev->genl_data.poll_req_portid == w->portid) {
  895. nfc_stop_poll(dev);
  896. dev->genl_data.poll_req_portid = 0;
  897. }
  898. mutex_unlock(&dev->genl_data.genl_data_mutex);
  899. dev = nfc_device_iter_next(&iter);
  900. }
  901. nfc_device_iter_exit(&iter);
  902. mutex_unlock(&nfc_devlist_mutex);
  903. kfree(w);
  904. }
  905. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  906. unsigned long event, void *ptr)
  907. {
  908. struct netlink_notify *n = ptr;
  909. struct urelease_work *w;
  910. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  911. goto out;
  912. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  913. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  914. if (w) {
  915. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  916. w->portid = n->portid;
  917. schedule_work((struct work_struct *) w);
  918. }
  919. out:
  920. return NOTIFY_DONE;
  921. }
  922. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  923. {
  924. genl_data->poll_req_portid = 0;
  925. mutex_init(&genl_data->genl_data_mutex);
  926. }
  927. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  928. {
  929. mutex_destroy(&genl_data->genl_data_mutex);
  930. }
  931. static struct notifier_block nl_notifier = {
  932. .notifier_call = nfc_genl_rcv_nl_event,
  933. };
  934. /**
  935. * nfc_genl_init() - Initialize netlink interface
  936. *
  937. * This initialization function registers the nfc netlink family.
  938. */
  939. int __init nfc_genl_init(void)
  940. {
  941. int rc;
  942. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  943. ARRAY_SIZE(nfc_genl_ops));
  944. if (rc)
  945. return rc;
  946. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  947. netlink_register_notifier(&nl_notifier);
  948. return rc;
  949. }
  950. /**
  951. * nfc_genl_exit() - Deinitialize netlink interface
  952. *
  953. * This exit function unregisters the nfc netlink family.
  954. */
  955. void nfc_genl_exit(void)
  956. {
  957. netlink_unregister_notifier(&nl_notifier);
  958. genl_unregister_family(&nfc_genl_family);
  959. }