PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/net/nfc/netlink.c

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