netlink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 ": " fmt
  24. #include <net/genetlink.h>
  25. #include <linux/nfc.h>
  26. #include <linux/slab.h>
  27. #include "nfc.h"
  28. static struct genl_multicast_group nfc_genl_event_mcgrp = {
  29. .name = NFC_GENL_MCAST_EVENT_NAME,
  30. };
  31. struct genl_family nfc_genl_family = {
  32. .id = GENL_ID_GENERATE,
  33. .hdrsize = 0,
  34. .name = NFC_GENL_NAME,
  35. .version = NFC_GENL_VERSION,
  36. .maxattr = NFC_ATTR_MAX,
  37. };
  38. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  39. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  40. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  41. .len = NFC_DEVICE_NAME_MAXSIZE },
  42. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  43. };
  44. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  45. struct netlink_callback *cb, int flags)
  46. {
  47. void *hdr;
  48. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  49. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  50. if (!hdr)
  51. return -EMSGSIZE;
  52. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  53. NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
  54. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS,
  55. target->supported_protocols);
  56. NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
  57. NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
  58. return genlmsg_end(msg, hdr);
  59. nla_put_failure:
  60. genlmsg_cancel(msg, hdr);
  61. return -EMSGSIZE;
  62. }
  63. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  64. {
  65. struct nfc_dev *dev;
  66. int rc;
  67. u32 idx;
  68. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  69. nfc_genl_family.attrbuf,
  70. nfc_genl_family.maxattr,
  71. nfc_genl_policy);
  72. if (rc < 0)
  73. return ERR_PTR(rc);
  74. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  75. return ERR_PTR(-EINVAL);
  76. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  77. dev = nfc_get_device(idx);
  78. if (!dev)
  79. return ERR_PTR(-ENODEV);
  80. return dev;
  81. }
  82. static int nfc_genl_dump_targets(struct sk_buff *skb,
  83. struct netlink_callback *cb)
  84. {
  85. int i = cb->args[0];
  86. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  87. int rc;
  88. if (!dev) {
  89. dev = __get_device_from_cb(cb);
  90. if (IS_ERR(dev))
  91. return PTR_ERR(dev);
  92. cb->args[1] = (long) dev;
  93. }
  94. spin_lock_bh(&dev->targets_lock);
  95. cb->seq = dev->targets_generation;
  96. while (i < dev->n_targets) {
  97. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  98. NLM_F_MULTI);
  99. if (rc < 0)
  100. break;
  101. i++;
  102. }
  103. spin_unlock_bh(&dev->targets_lock);
  104. cb->args[0] = i;
  105. return skb->len;
  106. }
  107. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  108. {
  109. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  110. if (dev)
  111. nfc_put_device(dev);
  112. return 0;
  113. }
  114. int nfc_genl_targets_found(struct nfc_dev *dev)
  115. {
  116. struct sk_buff *msg;
  117. void *hdr;
  118. dev->genl_data.poll_req_pid = 0;
  119. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  120. if (!msg)
  121. return -ENOMEM;
  122. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  123. NFC_EVENT_TARGETS_FOUND);
  124. if (!hdr)
  125. goto free_msg;
  126. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  127. genlmsg_end(msg, hdr);
  128. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  129. nla_put_failure:
  130. genlmsg_cancel(msg, hdr);
  131. free_msg:
  132. nlmsg_free(msg);
  133. return -EMSGSIZE;
  134. }
  135. int nfc_genl_device_added(struct nfc_dev *dev)
  136. {
  137. struct sk_buff *msg;
  138. void *hdr;
  139. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  140. if (!msg)
  141. return -ENOMEM;
  142. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  143. NFC_EVENT_DEVICE_ADDED);
  144. if (!hdr)
  145. goto free_msg;
  146. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  147. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  148. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  149. genlmsg_end(msg, hdr);
  150. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  151. return 0;
  152. nla_put_failure:
  153. genlmsg_cancel(msg, hdr);
  154. free_msg:
  155. nlmsg_free(msg);
  156. return -EMSGSIZE;
  157. }
  158. int nfc_genl_device_removed(struct nfc_dev *dev)
  159. {
  160. struct sk_buff *msg;
  161. void *hdr;
  162. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  163. if (!msg)
  164. return -ENOMEM;
  165. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  166. NFC_EVENT_DEVICE_REMOVED);
  167. if (!hdr)
  168. goto free_msg;
  169. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  170. genlmsg_end(msg, hdr);
  171. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  172. return 0;
  173. nla_put_failure:
  174. genlmsg_cancel(msg, hdr);
  175. free_msg:
  176. nlmsg_free(msg);
  177. return -EMSGSIZE;
  178. }
  179. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  180. u32 pid, u32 seq,
  181. struct netlink_callback *cb,
  182. int flags)
  183. {
  184. void *hdr;
  185. hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
  186. NFC_CMD_GET_DEVICE);
  187. if (!hdr)
  188. return -EMSGSIZE;
  189. if (cb)
  190. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  191. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  192. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  193. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  194. return genlmsg_end(msg, hdr);
  195. nla_put_failure:
  196. genlmsg_cancel(msg, hdr);
  197. return -EMSGSIZE;
  198. }
  199. static int nfc_genl_dump_devices(struct sk_buff *skb,
  200. struct netlink_callback *cb)
  201. {
  202. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  203. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  204. bool first_call = false;
  205. if (!iter) {
  206. first_call = true;
  207. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  208. if (!iter)
  209. return -ENOMEM;
  210. cb->args[0] = (long) iter;
  211. }
  212. mutex_lock(&nfc_devlist_mutex);
  213. cb->seq = nfc_devlist_generation;
  214. if (first_call) {
  215. nfc_device_iter_init(iter);
  216. dev = nfc_device_iter_next(iter);
  217. }
  218. while (dev) {
  219. int rc;
  220. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
  221. cb->nlh->nlmsg_seq,
  222. cb, NLM_F_MULTI);
  223. if (rc < 0)
  224. break;
  225. dev = nfc_device_iter_next(iter);
  226. }
  227. mutex_unlock(&nfc_devlist_mutex);
  228. cb->args[1] = (long) dev;
  229. return skb->len;
  230. }
  231. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  232. {
  233. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  234. nfc_device_iter_exit(iter);
  235. kfree(iter);
  236. return 0;
  237. }
  238. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  239. {
  240. struct sk_buff *msg;
  241. struct nfc_dev *dev;
  242. u32 idx;
  243. int rc = -ENOBUFS;
  244. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  245. return -EINVAL;
  246. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  247. dev = nfc_get_device(idx);
  248. if (!dev)
  249. return -ENODEV;
  250. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  251. if (!msg) {
  252. rc = -ENOMEM;
  253. goto out_putdev;
  254. }
  255. rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
  256. NULL, 0);
  257. if (rc < 0)
  258. goto out_free;
  259. nfc_put_device(dev);
  260. return genlmsg_reply(msg, info);
  261. out_free:
  262. nlmsg_free(msg);
  263. out_putdev:
  264. nfc_put_device(dev);
  265. return rc;
  266. }
  267. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  268. {
  269. struct nfc_dev *dev;
  270. int rc;
  271. u32 idx;
  272. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  273. return -EINVAL;
  274. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  275. dev = nfc_get_device(idx);
  276. if (!dev)
  277. return -ENODEV;
  278. rc = nfc_dev_up(dev);
  279. nfc_put_device(dev);
  280. return rc;
  281. }
  282. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  283. {
  284. struct nfc_dev *dev;
  285. int rc;
  286. u32 idx;
  287. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  288. return -EINVAL;
  289. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  290. dev = nfc_get_device(idx);
  291. if (!dev)
  292. return -ENODEV;
  293. rc = nfc_dev_down(dev);
  294. nfc_put_device(dev);
  295. return rc;
  296. }
  297. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  298. {
  299. struct nfc_dev *dev;
  300. int rc;
  301. u32 idx;
  302. u32 protocols;
  303. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  304. !info->attrs[NFC_ATTR_PROTOCOLS])
  305. return -EINVAL;
  306. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  307. protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  308. dev = nfc_get_device(idx);
  309. if (!dev)
  310. return -ENODEV;
  311. mutex_lock(&dev->genl_data.genl_data_mutex);
  312. rc = nfc_start_poll(dev, protocols);
  313. if (!rc)
  314. dev->genl_data.poll_req_pid = info->snd_pid;
  315. mutex_unlock(&dev->genl_data.genl_data_mutex);
  316. nfc_put_device(dev);
  317. return rc;
  318. }
  319. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  320. {
  321. struct nfc_dev *dev;
  322. int rc;
  323. u32 idx;
  324. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  325. return -EINVAL;
  326. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  327. dev = nfc_get_device(idx);
  328. if (!dev)
  329. return -ENODEV;
  330. mutex_lock(&dev->genl_data.genl_data_mutex);
  331. if (dev->genl_data.poll_req_pid != info->snd_pid) {
  332. rc = -EBUSY;
  333. goto out;
  334. }
  335. rc = nfc_stop_poll(dev);
  336. dev->genl_data.poll_req_pid = 0;
  337. out:
  338. mutex_unlock(&dev->genl_data.genl_data_mutex);
  339. nfc_put_device(dev);
  340. return rc;
  341. }
  342. static struct genl_ops nfc_genl_ops[] = {
  343. {
  344. .cmd = NFC_CMD_GET_DEVICE,
  345. .doit = nfc_genl_get_device,
  346. .dumpit = nfc_genl_dump_devices,
  347. .done = nfc_genl_dump_devices_done,
  348. .policy = nfc_genl_policy,
  349. },
  350. {
  351. .cmd = NFC_CMD_DEV_UP,
  352. .doit = nfc_genl_dev_up,
  353. .policy = nfc_genl_policy,
  354. },
  355. {
  356. .cmd = NFC_CMD_DEV_DOWN,
  357. .doit = nfc_genl_dev_down,
  358. .policy = nfc_genl_policy,
  359. },
  360. {
  361. .cmd = NFC_CMD_START_POLL,
  362. .doit = nfc_genl_start_poll,
  363. .policy = nfc_genl_policy,
  364. },
  365. {
  366. .cmd = NFC_CMD_STOP_POLL,
  367. .doit = nfc_genl_stop_poll,
  368. .policy = nfc_genl_policy,
  369. },
  370. {
  371. .cmd = NFC_CMD_GET_TARGET,
  372. .dumpit = nfc_genl_dump_targets,
  373. .done = nfc_genl_dump_targets_done,
  374. .policy = nfc_genl_policy,
  375. },
  376. };
  377. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  378. unsigned long event, void *ptr)
  379. {
  380. struct netlink_notify *n = ptr;
  381. struct class_dev_iter iter;
  382. struct nfc_dev *dev;
  383. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  384. goto out;
  385. pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
  386. nfc_device_iter_init(&iter);
  387. dev = nfc_device_iter_next(&iter);
  388. while (dev) {
  389. mutex_lock(&dev->genl_data.genl_data_mutex);
  390. if (dev->genl_data.poll_req_pid == n->pid) {
  391. nfc_stop_poll(dev);
  392. dev->genl_data.poll_req_pid = 0;
  393. }
  394. mutex_unlock(&dev->genl_data.genl_data_mutex);
  395. dev = nfc_device_iter_next(&iter);
  396. }
  397. nfc_device_iter_exit(&iter);
  398. out:
  399. return NOTIFY_DONE;
  400. }
  401. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  402. {
  403. genl_data->poll_req_pid = 0;
  404. mutex_init(&genl_data->genl_data_mutex);
  405. }
  406. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  407. {
  408. mutex_destroy(&genl_data->genl_data_mutex);
  409. }
  410. static struct notifier_block nl_notifier = {
  411. .notifier_call = nfc_genl_rcv_nl_event,
  412. };
  413. /**
  414. * nfc_genl_init() - Initialize netlink interface
  415. *
  416. * This initialization function registers the nfc netlink family.
  417. */
  418. int __init nfc_genl_init(void)
  419. {
  420. int rc;
  421. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  422. ARRAY_SIZE(nfc_genl_ops));
  423. if (rc)
  424. return rc;
  425. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  426. netlink_register_notifier(&nl_notifier);
  427. return rc;
  428. }
  429. /**
  430. * nfc_genl_exit() - Deinitialize netlink interface
  431. *
  432. * This exit function unregisters the nfc netlink family.
  433. */
  434. void nfc_genl_exit(void)
  435. {
  436. netlink_unregister_notifier(&nl_notifier);
  437. genl_unregister_family(&nfc_genl_family);
  438. }