/net/netlink/genetlink.c

http://github.com/mirrors/linux · C · 1235 lines · 942 code · 211 blank · 82 comment · 197 complexity · 1e879788457c6a89ec7e13b2a7f329ab MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NETLINK Generic Netlink Family
  4. *
  5. * Authors: Jamal Hadi Salim
  6. * Thomas Graf <tgraf@suug.ch>
  7. * Johannes Berg <johannes@sipsolutions.net>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/string.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/idr.h>
  21. #include <net/sock.h>
  22. #include <net/genetlink.h>
  23. static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
  24. static DECLARE_RWSEM(cb_lock);
  25. atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0);
  26. DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq);
  27. void genl_lock(void)
  28. {
  29. mutex_lock(&genl_mutex);
  30. }
  31. EXPORT_SYMBOL(genl_lock);
  32. void genl_unlock(void)
  33. {
  34. mutex_unlock(&genl_mutex);
  35. }
  36. EXPORT_SYMBOL(genl_unlock);
  37. #ifdef CONFIG_LOCKDEP
  38. bool lockdep_genl_is_held(void)
  39. {
  40. return lockdep_is_held(&genl_mutex);
  41. }
  42. EXPORT_SYMBOL(lockdep_genl_is_held);
  43. #endif
  44. static void genl_lock_all(void)
  45. {
  46. down_write(&cb_lock);
  47. genl_lock();
  48. }
  49. static void genl_unlock_all(void)
  50. {
  51. genl_unlock();
  52. up_write(&cb_lock);
  53. }
  54. static DEFINE_IDR(genl_fam_idr);
  55. /*
  56. * Bitmap of multicast groups that are currently in use.
  57. *
  58. * To avoid an allocation at boot of just one unsigned long,
  59. * declare it global instead.
  60. * Bit 0 is marked as already used since group 0 is invalid.
  61. * Bit 1 is marked as already used since the drop-monitor code
  62. * abuses the API and thinks it can statically use group 1.
  63. * That group will typically conflict with other groups that
  64. * any proper users use.
  65. * Bit 16 is marked as used since it's used for generic netlink
  66. * and the code no longer marks pre-reserved IDs as used.
  67. * Bit 17 is marked as already used since the VFS quota code
  68. * also abused this API and relied on family == group ID, we
  69. * cater to that by giving it a static family and group ID.
  70. * Bit 18 is marked as already used since the PMCRAID driver
  71. * did the same thing as the VFS quota code (maybe copied?)
  72. */
  73. static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
  74. BIT(GENL_ID_VFS_DQUOT) |
  75. BIT(GENL_ID_PMCRAID);
  76. static unsigned long *mc_groups = &mc_group_start;
  77. static unsigned long mc_groups_longs = 1;
  78. static int genl_ctrl_event(int event, const struct genl_family *family,
  79. const struct genl_multicast_group *grp,
  80. int grp_id);
  81. static const struct genl_family *genl_family_find_byid(unsigned int id)
  82. {
  83. return idr_find(&genl_fam_idr, id);
  84. }
  85. static const struct genl_family *genl_family_find_byname(char *name)
  86. {
  87. const struct genl_family *family;
  88. unsigned int id;
  89. idr_for_each_entry(&genl_fam_idr, family, id)
  90. if (strcmp(family->name, name) == 0)
  91. return family;
  92. return NULL;
  93. }
  94. static const struct genl_ops *genl_get_cmd(u8 cmd,
  95. const struct genl_family *family)
  96. {
  97. int i;
  98. for (i = 0; i < family->n_ops; i++)
  99. if (family->ops[i].cmd == cmd)
  100. return &family->ops[i];
  101. return NULL;
  102. }
  103. static int genl_allocate_reserve_groups(int n_groups, int *first_id)
  104. {
  105. unsigned long *new_groups;
  106. int start = 0;
  107. int i;
  108. int id;
  109. bool fits;
  110. do {
  111. if (start == 0)
  112. id = find_first_zero_bit(mc_groups,
  113. mc_groups_longs *
  114. BITS_PER_LONG);
  115. else
  116. id = find_next_zero_bit(mc_groups,
  117. mc_groups_longs * BITS_PER_LONG,
  118. start);
  119. fits = true;
  120. for (i = id;
  121. i < min_t(int, id + n_groups,
  122. mc_groups_longs * BITS_PER_LONG);
  123. i++) {
  124. if (test_bit(i, mc_groups)) {
  125. start = i;
  126. fits = false;
  127. break;
  128. }
  129. }
  130. if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
  131. unsigned long new_longs = mc_groups_longs +
  132. BITS_TO_LONGS(n_groups);
  133. size_t nlen = new_longs * sizeof(unsigned long);
  134. if (mc_groups == &mc_group_start) {
  135. new_groups = kzalloc(nlen, GFP_KERNEL);
  136. if (!new_groups)
  137. return -ENOMEM;
  138. mc_groups = new_groups;
  139. *mc_groups = mc_group_start;
  140. } else {
  141. new_groups = krealloc(mc_groups, nlen,
  142. GFP_KERNEL);
  143. if (!new_groups)
  144. return -ENOMEM;
  145. mc_groups = new_groups;
  146. for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
  147. mc_groups[mc_groups_longs + i] = 0;
  148. }
  149. mc_groups_longs = new_longs;
  150. }
  151. } while (!fits);
  152. for (i = id; i < id + n_groups; i++)
  153. set_bit(i, mc_groups);
  154. *first_id = id;
  155. return 0;
  156. }
  157. static struct genl_family genl_ctrl;
  158. static int genl_validate_assign_mc_groups(struct genl_family *family)
  159. {
  160. int first_id;
  161. int n_groups = family->n_mcgrps;
  162. int err = 0, i;
  163. bool groups_allocated = false;
  164. if (!n_groups)
  165. return 0;
  166. for (i = 0; i < n_groups; i++) {
  167. const struct genl_multicast_group *grp = &family->mcgrps[i];
  168. if (WARN_ON(grp->name[0] == '\0'))
  169. return -EINVAL;
  170. if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
  171. return -EINVAL;
  172. }
  173. /* special-case our own group and hacks */
  174. if (family == &genl_ctrl) {
  175. first_id = GENL_ID_CTRL;
  176. BUG_ON(n_groups != 1);
  177. } else if (strcmp(family->name, "NET_DM") == 0) {
  178. first_id = 1;
  179. BUG_ON(n_groups != 1);
  180. } else if (family->id == GENL_ID_VFS_DQUOT) {
  181. first_id = GENL_ID_VFS_DQUOT;
  182. BUG_ON(n_groups != 1);
  183. } else if (family->id == GENL_ID_PMCRAID) {
  184. first_id = GENL_ID_PMCRAID;
  185. BUG_ON(n_groups != 1);
  186. } else {
  187. groups_allocated = true;
  188. err = genl_allocate_reserve_groups(n_groups, &first_id);
  189. if (err)
  190. return err;
  191. }
  192. family->mcgrp_offset = first_id;
  193. /* if still initializing, can't and don't need to to realloc bitmaps */
  194. if (!init_net.genl_sock)
  195. return 0;
  196. if (family->netnsok) {
  197. struct net *net;
  198. netlink_table_grab();
  199. rcu_read_lock();
  200. for_each_net_rcu(net) {
  201. err = __netlink_change_ngroups(net->genl_sock,
  202. mc_groups_longs * BITS_PER_LONG);
  203. if (err) {
  204. /*
  205. * No need to roll back, can only fail if
  206. * memory allocation fails and then the
  207. * number of _possible_ groups has been
  208. * increased on some sockets which is ok.
  209. */
  210. break;
  211. }
  212. }
  213. rcu_read_unlock();
  214. netlink_table_ungrab();
  215. } else {
  216. err = netlink_change_ngroups(init_net.genl_sock,
  217. mc_groups_longs * BITS_PER_LONG);
  218. }
  219. if (groups_allocated && err) {
  220. for (i = 0; i < family->n_mcgrps; i++)
  221. clear_bit(family->mcgrp_offset + i, mc_groups);
  222. }
  223. return err;
  224. }
  225. static void genl_unregister_mc_groups(const struct genl_family *family)
  226. {
  227. struct net *net;
  228. int i;
  229. netlink_table_grab();
  230. rcu_read_lock();
  231. for_each_net_rcu(net) {
  232. for (i = 0; i < family->n_mcgrps; i++)
  233. __netlink_clear_multicast_users(
  234. net->genl_sock, family->mcgrp_offset + i);
  235. }
  236. rcu_read_unlock();
  237. netlink_table_ungrab();
  238. for (i = 0; i < family->n_mcgrps; i++) {
  239. int grp_id = family->mcgrp_offset + i;
  240. if (grp_id != 1)
  241. clear_bit(grp_id, mc_groups);
  242. genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
  243. &family->mcgrps[i], grp_id);
  244. }
  245. }
  246. static int genl_validate_ops(const struct genl_family *family)
  247. {
  248. const struct genl_ops *ops = family->ops;
  249. unsigned int n_ops = family->n_ops;
  250. int i, j;
  251. if (WARN_ON(n_ops && !ops))
  252. return -EINVAL;
  253. if (!n_ops)
  254. return 0;
  255. for (i = 0; i < n_ops; i++) {
  256. if (ops[i].dumpit == NULL && ops[i].doit == NULL)
  257. return -EINVAL;
  258. for (j = i + 1; j < n_ops; j++)
  259. if (ops[i].cmd == ops[j].cmd)
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. /**
  265. * genl_register_family - register a generic netlink family
  266. * @family: generic netlink family
  267. *
  268. * Registers the specified family after validating it first. Only one
  269. * family may be registered with the same family name or identifier.
  270. *
  271. * The family's ops, multicast groups and module pointer must already
  272. * be assigned.
  273. *
  274. * Return 0 on success or a negative error code.
  275. */
  276. int genl_register_family(struct genl_family *family)
  277. {
  278. int err, i;
  279. int start = GENL_START_ALLOC, end = GENL_MAX_ID;
  280. err = genl_validate_ops(family);
  281. if (err)
  282. return err;
  283. genl_lock_all();
  284. if (genl_family_find_byname(family->name)) {
  285. err = -EEXIST;
  286. goto errout_locked;
  287. }
  288. /*
  289. * Sadly, a few cases need to be special-cased
  290. * due to them having previously abused the API
  291. * and having used their family ID also as their
  292. * multicast group ID, so we use reserved IDs
  293. * for both to be sure we can do that mapping.
  294. */
  295. if (family == &genl_ctrl) {
  296. /* and this needs to be special for initial family lookups */
  297. start = end = GENL_ID_CTRL;
  298. } else if (strcmp(family->name, "pmcraid") == 0) {
  299. start = end = GENL_ID_PMCRAID;
  300. } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
  301. start = end = GENL_ID_VFS_DQUOT;
  302. }
  303. if (family->maxattr && !family->parallel_ops) {
  304. family->attrbuf = kmalloc_array(family->maxattr + 1,
  305. sizeof(struct nlattr *),
  306. GFP_KERNEL);
  307. if (family->attrbuf == NULL) {
  308. err = -ENOMEM;
  309. goto errout_locked;
  310. }
  311. } else
  312. family->attrbuf = NULL;
  313. family->id = idr_alloc_cyclic(&genl_fam_idr, family,
  314. start, end + 1, GFP_KERNEL);
  315. if (family->id < 0) {
  316. err = family->id;
  317. goto errout_free;
  318. }
  319. err = genl_validate_assign_mc_groups(family);
  320. if (err)
  321. goto errout_remove;
  322. genl_unlock_all();
  323. /* send all events */
  324. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
  325. for (i = 0; i < family->n_mcgrps; i++)
  326. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
  327. &family->mcgrps[i], family->mcgrp_offset + i);
  328. return 0;
  329. errout_remove:
  330. idr_remove(&genl_fam_idr, family->id);
  331. errout_free:
  332. kfree(family->attrbuf);
  333. errout_locked:
  334. genl_unlock_all();
  335. return err;
  336. }
  337. EXPORT_SYMBOL(genl_register_family);
  338. /**
  339. * genl_unregister_family - unregister generic netlink family
  340. * @family: generic netlink family
  341. *
  342. * Unregisters the specified family.
  343. *
  344. * Returns 0 on success or a negative error code.
  345. */
  346. int genl_unregister_family(const struct genl_family *family)
  347. {
  348. genl_lock_all();
  349. if (!genl_family_find_byid(family->id)) {
  350. genl_unlock_all();
  351. return -ENOENT;
  352. }
  353. genl_unregister_mc_groups(family);
  354. idr_remove(&genl_fam_idr, family->id);
  355. up_write(&cb_lock);
  356. wait_event(genl_sk_destructing_waitq,
  357. atomic_read(&genl_sk_destructing_cnt) == 0);
  358. genl_unlock();
  359. kfree(family->attrbuf);
  360. genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(genl_unregister_family);
  364. /**
  365. * genlmsg_put - Add generic netlink header to netlink message
  366. * @skb: socket buffer holding the message
  367. * @portid: netlink portid the message is addressed to
  368. * @seq: sequence number (usually the one of the sender)
  369. * @family: generic netlink family
  370. * @flags: netlink message flags
  371. * @cmd: generic netlink command
  372. *
  373. * Returns pointer to user specific header
  374. */
  375. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  376. const struct genl_family *family, int flags, u8 cmd)
  377. {
  378. struct nlmsghdr *nlh;
  379. struct genlmsghdr *hdr;
  380. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  381. family->hdrsize, flags);
  382. if (nlh == NULL)
  383. return NULL;
  384. hdr = nlmsg_data(nlh);
  385. hdr->cmd = cmd;
  386. hdr->version = family->version;
  387. hdr->reserved = 0;
  388. return (char *) hdr + GENL_HDRLEN;
  389. }
  390. EXPORT_SYMBOL(genlmsg_put);
  391. static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
  392. {
  393. return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
  394. }
  395. static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
  396. {
  397. kfree(info);
  398. }
  399. static struct nlattr **
  400. genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
  401. struct nlmsghdr *nlh,
  402. struct netlink_ext_ack *extack,
  403. const struct genl_ops *ops,
  404. int hdrlen,
  405. enum genl_validate_flags no_strict_flag,
  406. bool parallel)
  407. {
  408. enum netlink_validation validate = ops->validate & no_strict_flag ?
  409. NL_VALIDATE_LIBERAL :
  410. NL_VALIDATE_STRICT;
  411. struct nlattr **attrbuf;
  412. int err;
  413. if (!family->maxattr)
  414. return NULL;
  415. if (parallel) {
  416. attrbuf = kmalloc_array(family->maxattr + 1,
  417. sizeof(struct nlattr *), GFP_KERNEL);
  418. if (!attrbuf)
  419. return ERR_PTR(-ENOMEM);
  420. } else {
  421. attrbuf = family->attrbuf;
  422. }
  423. err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  424. family->policy, validate, extack);
  425. if (err) {
  426. if (parallel)
  427. kfree(attrbuf);
  428. return ERR_PTR(err);
  429. }
  430. return attrbuf;
  431. }
  432. static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
  433. struct nlattr **attrbuf,
  434. bool parallel)
  435. {
  436. if (parallel)
  437. kfree(attrbuf);
  438. }
  439. static int genl_lock_start(struct netlink_callback *cb)
  440. {
  441. const struct genl_ops *ops = genl_dumpit_info(cb)->ops;
  442. int rc = 0;
  443. if (ops->start) {
  444. genl_lock();
  445. rc = ops->start(cb);
  446. genl_unlock();
  447. }
  448. return rc;
  449. }
  450. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  451. {
  452. const struct genl_ops *ops = genl_dumpit_info(cb)->ops;
  453. int rc;
  454. genl_lock();
  455. rc = ops->dumpit(skb, cb);
  456. genl_unlock();
  457. return rc;
  458. }
  459. static int genl_lock_done(struct netlink_callback *cb)
  460. {
  461. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  462. const struct genl_ops *ops = info->ops;
  463. int rc = 0;
  464. if (ops->done) {
  465. genl_lock();
  466. rc = ops->done(cb);
  467. genl_unlock();
  468. }
  469. genl_family_rcv_msg_attrs_free(info->family, info->attrs, true);
  470. genl_dumpit_info_free(info);
  471. return rc;
  472. }
  473. static int genl_parallel_done(struct netlink_callback *cb)
  474. {
  475. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  476. const struct genl_ops *ops = info->ops;
  477. int rc = 0;
  478. if (ops->done)
  479. rc = ops->done(cb);
  480. genl_family_rcv_msg_attrs_free(info->family, info->attrs, true);
  481. genl_dumpit_info_free(info);
  482. return rc;
  483. }
  484. static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
  485. struct sk_buff *skb,
  486. struct nlmsghdr *nlh,
  487. struct netlink_ext_ack *extack,
  488. const struct genl_ops *ops,
  489. int hdrlen, struct net *net)
  490. {
  491. struct genl_dumpit_info *info;
  492. struct nlattr **attrs = NULL;
  493. int err;
  494. if (!ops->dumpit)
  495. return -EOPNOTSUPP;
  496. if (ops->validate & GENL_DONT_VALIDATE_DUMP)
  497. goto no_attrs;
  498. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  499. return -EINVAL;
  500. attrs = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
  501. ops, hdrlen,
  502. GENL_DONT_VALIDATE_DUMP_STRICT,
  503. true);
  504. if (IS_ERR(attrs))
  505. return PTR_ERR(attrs);
  506. no_attrs:
  507. /* Allocate dumpit info. It is going to be freed by done() callback. */
  508. info = genl_dumpit_info_alloc();
  509. if (!info) {
  510. genl_family_rcv_msg_attrs_free(family, attrs, true);
  511. return -ENOMEM;
  512. }
  513. info->family = family;
  514. info->ops = ops;
  515. info->attrs = attrs;
  516. if (!family->parallel_ops) {
  517. struct netlink_dump_control c = {
  518. .module = family->module,
  519. .data = info,
  520. .start = genl_lock_start,
  521. .dump = genl_lock_dumpit,
  522. .done = genl_lock_done,
  523. };
  524. genl_unlock();
  525. err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  526. genl_lock();
  527. } else {
  528. struct netlink_dump_control c = {
  529. .module = family->module,
  530. .data = info,
  531. .start = ops->start,
  532. .dump = ops->dumpit,
  533. .done = genl_parallel_done,
  534. };
  535. err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  536. }
  537. return err;
  538. }
  539. static int genl_family_rcv_msg_doit(const struct genl_family *family,
  540. struct sk_buff *skb,
  541. struct nlmsghdr *nlh,
  542. struct netlink_ext_ack *extack,
  543. const struct genl_ops *ops,
  544. int hdrlen, struct net *net)
  545. {
  546. struct nlattr **attrbuf;
  547. struct genl_info info;
  548. int err;
  549. if (!ops->doit)
  550. return -EOPNOTSUPP;
  551. attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
  552. ops, hdrlen,
  553. GENL_DONT_VALIDATE_STRICT,
  554. family->parallel_ops);
  555. if (IS_ERR(attrbuf))
  556. return PTR_ERR(attrbuf);
  557. info.snd_seq = nlh->nlmsg_seq;
  558. info.snd_portid = NETLINK_CB(skb).portid;
  559. info.nlhdr = nlh;
  560. info.genlhdr = nlmsg_data(nlh);
  561. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  562. info.attrs = attrbuf;
  563. info.extack = extack;
  564. genl_info_net_set(&info, net);
  565. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  566. if (family->pre_doit) {
  567. err = family->pre_doit(ops, skb, &info);
  568. if (err)
  569. goto out;
  570. }
  571. err = ops->doit(skb, &info);
  572. if (family->post_doit)
  573. family->post_doit(ops, skb, &info);
  574. out:
  575. genl_family_rcv_msg_attrs_free(family, attrbuf, family->parallel_ops);
  576. return err;
  577. }
  578. static int genl_family_rcv_msg(const struct genl_family *family,
  579. struct sk_buff *skb,
  580. struct nlmsghdr *nlh,
  581. struct netlink_ext_ack *extack)
  582. {
  583. const struct genl_ops *ops;
  584. struct net *net = sock_net(skb->sk);
  585. struct genlmsghdr *hdr = nlmsg_data(nlh);
  586. int hdrlen;
  587. /* this family doesn't exist in this netns */
  588. if (!family->netnsok && !net_eq(net, &init_net))
  589. return -ENOENT;
  590. hdrlen = GENL_HDRLEN + family->hdrsize;
  591. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  592. return -EINVAL;
  593. ops = genl_get_cmd(hdr->cmd, family);
  594. if (ops == NULL)
  595. return -EOPNOTSUPP;
  596. if ((ops->flags & GENL_ADMIN_PERM) &&
  597. !netlink_capable(skb, CAP_NET_ADMIN))
  598. return -EPERM;
  599. if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
  600. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  601. return -EPERM;
  602. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
  603. return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
  604. ops, hdrlen, net);
  605. else
  606. return genl_family_rcv_msg_doit(family, skb, nlh, extack,
  607. ops, hdrlen, net);
  608. }
  609. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  610. struct netlink_ext_ack *extack)
  611. {
  612. const struct genl_family *family;
  613. int err;
  614. family = genl_family_find_byid(nlh->nlmsg_type);
  615. if (family == NULL)
  616. return -ENOENT;
  617. if (!family->parallel_ops)
  618. genl_lock();
  619. err = genl_family_rcv_msg(family, skb, nlh, extack);
  620. if (!family->parallel_ops)
  621. genl_unlock();
  622. return err;
  623. }
  624. static void genl_rcv(struct sk_buff *skb)
  625. {
  626. down_read(&cb_lock);
  627. netlink_rcv_skb(skb, &genl_rcv_msg);
  628. up_read(&cb_lock);
  629. }
  630. /**************************************************************************
  631. * Controller
  632. **************************************************************************/
  633. static struct genl_family genl_ctrl;
  634. static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
  635. u32 flags, struct sk_buff *skb, u8 cmd)
  636. {
  637. void *hdr;
  638. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  639. if (hdr == NULL)
  640. return -1;
  641. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  642. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  643. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  644. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  645. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  646. goto nla_put_failure;
  647. if (family->n_ops) {
  648. struct nlattr *nla_ops;
  649. int i;
  650. nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
  651. if (nla_ops == NULL)
  652. goto nla_put_failure;
  653. for (i = 0; i < family->n_ops; i++) {
  654. struct nlattr *nest;
  655. const struct genl_ops *ops = &family->ops[i];
  656. u32 op_flags = ops->flags;
  657. if (ops->dumpit)
  658. op_flags |= GENL_CMD_CAP_DUMP;
  659. if (ops->doit)
  660. op_flags |= GENL_CMD_CAP_DO;
  661. if (family->policy)
  662. op_flags |= GENL_CMD_CAP_HASPOL;
  663. nest = nla_nest_start_noflag(skb, i + 1);
  664. if (nest == NULL)
  665. goto nla_put_failure;
  666. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  667. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  668. goto nla_put_failure;
  669. nla_nest_end(skb, nest);
  670. }
  671. nla_nest_end(skb, nla_ops);
  672. }
  673. if (family->n_mcgrps) {
  674. struct nlattr *nla_grps;
  675. int i;
  676. nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
  677. if (nla_grps == NULL)
  678. goto nla_put_failure;
  679. for (i = 0; i < family->n_mcgrps; i++) {
  680. struct nlattr *nest;
  681. const struct genl_multicast_group *grp;
  682. grp = &family->mcgrps[i];
  683. nest = nla_nest_start_noflag(skb, i + 1);
  684. if (nest == NULL)
  685. goto nla_put_failure;
  686. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  687. family->mcgrp_offset + i) ||
  688. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  689. grp->name))
  690. goto nla_put_failure;
  691. nla_nest_end(skb, nest);
  692. }
  693. nla_nest_end(skb, nla_grps);
  694. }
  695. genlmsg_end(skb, hdr);
  696. return 0;
  697. nla_put_failure:
  698. genlmsg_cancel(skb, hdr);
  699. return -EMSGSIZE;
  700. }
  701. static int ctrl_fill_mcgrp_info(const struct genl_family *family,
  702. const struct genl_multicast_group *grp,
  703. int grp_id, u32 portid, u32 seq, u32 flags,
  704. struct sk_buff *skb, u8 cmd)
  705. {
  706. void *hdr;
  707. struct nlattr *nla_grps;
  708. struct nlattr *nest;
  709. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  710. if (hdr == NULL)
  711. return -1;
  712. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  713. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  714. goto nla_put_failure;
  715. nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
  716. if (nla_grps == NULL)
  717. goto nla_put_failure;
  718. nest = nla_nest_start_noflag(skb, 1);
  719. if (nest == NULL)
  720. goto nla_put_failure;
  721. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  722. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  723. grp->name))
  724. goto nla_put_failure;
  725. nla_nest_end(skb, nest);
  726. nla_nest_end(skb, nla_grps);
  727. genlmsg_end(skb, hdr);
  728. return 0;
  729. nla_put_failure:
  730. genlmsg_cancel(skb, hdr);
  731. return -EMSGSIZE;
  732. }
  733. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  734. {
  735. int n = 0;
  736. struct genl_family *rt;
  737. struct net *net = sock_net(skb->sk);
  738. int fams_to_skip = cb->args[0];
  739. unsigned int id;
  740. idr_for_each_entry(&genl_fam_idr, rt, id) {
  741. if (!rt->netnsok && !net_eq(net, &init_net))
  742. continue;
  743. if (n++ < fams_to_skip)
  744. continue;
  745. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  746. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  747. skb, CTRL_CMD_NEWFAMILY) < 0) {
  748. n--;
  749. break;
  750. }
  751. }
  752. cb->args[0] = n;
  753. return skb->len;
  754. }
  755. static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
  756. u32 portid, int seq, u8 cmd)
  757. {
  758. struct sk_buff *skb;
  759. int err;
  760. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  761. if (skb == NULL)
  762. return ERR_PTR(-ENOBUFS);
  763. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  764. if (err < 0) {
  765. nlmsg_free(skb);
  766. return ERR_PTR(err);
  767. }
  768. return skb;
  769. }
  770. static struct sk_buff *
  771. ctrl_build_mcgrp_msg(const struct genl_family *family,
  772. const struct genl_multicast_group *grp,
  773. int grp_id, u32 portid, int seq, u8 cmd)
  774. {
  775. struct sk_buff *skb;
  776. int err;
  777. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  778. if (skb == NULL)
  779. return ERR_PTR(-ENOBUFS);
  780. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  781. seq, 0, skb, cmd);
  782. if (err < 0) {
  783. nlmsg_free(skb);
  784. return ERR_PTR(err);
  785. }
  786. return skb;
  787. }
  788. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  789. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  790. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  791. .len = GENL_NAMSIZ - 1 },
  792. };
  793. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  794. {
  795. struct sk_buff *msg;
  796. const struct genl_family *res = NULL;
  797. int err = -EINVAL;
  798. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  799. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  800. res = genl_family_find_byid(id);
  801. err = -ENOENT;
  802. }
  803. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  804. char *name;
  805. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  806. res = genl_family_find_byname(name);
  807. #ifdef CONFIG_MODULES
  808. if (res == NULL) {
  809. genl_unlock();
  810. up_read(&cb_lock);
  811. request_module("net-pf-%d-proto-%d-family-%s",
  812. PF_NETLINK, NETLINK_GENERIC, name);
  813. down_read(&cb_lock);
  814. genl_lock();
  815. res = genl_family_find_byname(name);
  816. }
  817. #endif
  818. err = -ENOENT;
  819. }
  820. if (res == NULL)
  821. return err;
  822. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  823. /* family doesn't exist here */
  824. return -ENOENT;
  825. }
  826. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  827. CTRL_CMD_NEWFAMILY);
  828. if (IS_ERR(msg))
  829. return PTR_ERR(msg);
  830. return genlmsg_reply(msg, info);
  831. }
  832. static int genl_ctrl_event(int event, const struct genl_family *family,
  833. const struct genl_multicast_group *grp,
  834. int grp_id)
  835. {
  836. struct sk_buff *msg;
  837. /* genl is still initialising */
  838. if (!init_net.genl_sock)
  839. return 0;
  840. switch (event) {
  841. case CTRL_CMD_NEWFAMILY:
  842. case CTRL_CMD_DELFAMILY:
  843. WARN_ON(grp);
  844. msg = ctrl_build_family_msg(family, 0, 0, event);
  845. break;
  846. case CTRL_CMD_NEWMCAST_GRP:
  847. case CTRL_CMD_DELMCAST_GRP:
  848. BUG_ON(!grp);
  849. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  850. break;
  851. default:
  852. return -EINVAL;
  853. }
  854. if (IS_ERR(msg))
  855. return PTR_ERR(msg);
  856. if (!family->netnsok) {
  857. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  858. 0, GFP_KERNEL);
  859. } else {
  860. rcu_read_lock();
  861. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  862. 0, GFP_ATOMIC);
  863. rcu_read_unlock();
  864. }
  865. return 0;
  866. }
  867. static const struct genl_ops genl_ctrl_ops[] = {
  868. {
  869. .cmd = CTRL_CMD_GETFAMILY,
  870. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  871. .doit = ctrl_getfamily,
  872. .dumpit = ctrl_dumpfamily,
  873. },
  874. };
  875. static const struct genl_multicast_group genl_ctrl_groups[] = {
  876. { .name = "notify", },
  877. };
  878. static struct genl_family genl_ctrl __ro_after_init = {
  879. .module = THIS_MODULE,
  880. .ops = genl_ctrl_ops,
  881. .n_ops = ARRAY_SIZE(genl_ctrl_ops),
  882. .mcgrps = genl_ctrl_groups,
  883. .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
  884. .id = GENL_ID_CTRL,
  885. .name = "nlctrl",
  886. .version = 0x2,
  887. .maxattr = CTRL_ATTR_MAX,
  888. .policy = ctrl_policy,
  889. .netnsok = true,
  890. };
  891. static int genl_bind(struct net *net, int group)
  892. {
  893. struct genl_family *f;
  894. int err = -ENOENT;
  895. unsigned int id;
  896. down_read(&cb_lock);
  897. idr_for_each_entry(&genl_fam_idr, f, id) {
  898. if (group >= f->mcgrp_offset &&
  899. group < f->mcgrp_offset + f->n_mcgrps) {
  900. int fam_grp = group - f->mcgrp_offset;
  901. if (!f->netnsok && net != &init_net)
  902. err = -ENOENT;
  903. else if (f->mcast_bind)
  904. err = f->mcast_bind(net, fam_grp);
  905. else
  906. err = 0;
  907. break;
  908. }
  909. }
  910. up_read(&cb_lock);
  911. return err;
  912. }
  913. static void genl_unbind(struct net *net, int group)
  914. {
  915. struct genl_family *f;
  916. unsigned int id;
  917. down_read(&cb_lock);
  918. idr_for_each_entry(&genl_fam_idr, f, id) {
  919. if (group >= f->mcgrp_offset &&
  920. group < f->mcgrp_offset + f->n_mcgrps) {
  921. int fam_grp = group - f->mcgrp_offset;
  922. if (f->mcast_unbind)
  923. f->mcast_unbind(net, fam_grp);
  924. break;
  925. }
  926. }
  927. up_read(&cb_lock);
  928. }
  929. static int __net_init genl_pernet_init(struct net *net)
  930. {
  931. struct netlink_kernel_cfg cfg = {
  932. .input = genl_rcv,
  933. .flags = NL_CFG_F_NONROOT_RECV,
  934. .bind = genl_bind,
  935. .unbind = genl_unbind,
  936. };
  937. /* we'll bump the group number right afterwards */
  938. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  939. if (!net->genl_sock && net_eq(net, &init_net))
  940. panic("GENL: Cannot initialize generic netlink\n");
  941. if (!net->genl_sock)
  942. return -ENOMEM;
  943. return 0;
  944. }
  945. static void __net_exit genl_pernet_exit(struct net *net)
  946. {
  947. netlink_kernel_release(net->genl_sock);
  948. net->genl_sock = NULL;
  949. }
  950. static struct pernet_operations genl_pernet_ops = {
  951. .init = genl_pernet_init,
  952. .exit = genl_pernet_exit,
  953. };
  954. static int __init genl_init(void)
  955. {
  956. int err;
  957. err = genl_register_family(&genl_ctrl);
  958. if (err < 0)
  959. goto problem;
  960. err = register_pernet_subsys(&genl_pernet_ops);
  961. if (err)
  962. goto problem;
  963. return 0;
  964. problem:
  965. panic("GENL: Cannot register controller: %d\n", err);
  966. }
  967. subsys_initcall(genl_init);
  968. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  969. gfp_t flags)
  970. {
  971. struct sk_buff *tmp;
  972. struct net *net, *prev = NULL;
  973. bool delivered = false;
  974. int err;
  975. for_each_net_rcu(net) {
  976. if (prev) {
  977. tmp = skb_clone(skb, flags);
  978. if (!tmp) {
  979. err = -ENOMEM;
  980. goto error;
  981. }
  982. err = nlmsg_multicast(prev->genl_sock, tmp,
  983. portid, group, flags);
  984. if (!err)
  985. delivered = true;
  986. else if (err != -ESRCH)
  987. goto error;
  988. }
  989. prev = net;
  990. }
  991. err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  992. if (!err)
  993. delivered = true;
  994. else if (err != -ESRCH)
  995. return err;
  996. return delivered ? 0 : -ESRCH;
  997. error:
  998. kfree_skb(skb);
  999. return err;
  1000. }
  1001. int genlmsg_multicast_allns(const struct genl_family *family,
  1002. struct sk_buff *skb, u32 portid,
  1003. unsigned int group, gfp_t flags)
  1004. {
  1005. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  1006. return -EINVAL;
  1007. group = family->mcgrp_offset + group;
  1008. return genlmsg_mcast(skb, portid, group, flags);
  1009. }
  1010. EXPORT_SYMBOL(genlmsg_multicast_allns);
  1011. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  1012. struct genl_info *info, u32 group, gfp_t flags)
  1013. {
  1014. struct net *net = genl_info_net(info);
  1015. struct sock *sk = net->genl_sock;
  1016. int report = 0;
  1017. if (info->nlhdr)
  1018. report = nlmsg_report(info->nlhdr);
  1019. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  1020. return;
  1021. group = family->mcgrp_offset + group;
  1022. nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
  1023. }
  1024. EXPORT_SYMBOL(genl_notify);