genetlink.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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(&genl_fam_idr, family,
  314. start, end + 1, GFP_KERNEL);
  315. if (family->id < 0) {
  316. err = family->id;
  317. goto errout_locked;
  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. kfree(family->attrbuf);
  332. errout_locked:
  333. genl_unlock_all();
  334. return err;
  335. }
  336. EXPORT_SYMBOL(genl_register_family);
  337. /**
  338. * genl_unregister_family - unregister generic netlink family
  339. * @family: generic netlink family
  340. *
  341. * Unregisters the specified family.
  342. *
  343. * Returns 0 on success or a negative error code.
  344. */
  345. int genl_unregister_family(const struct genl_family *family)
  346. {
  347. genl_lock_all();
  348. if (!genl_family_find_byid(family->id)) {
  349. genl_unlock_all();
  350. return -ENOENT;
  351. }
  352. genl_unregister_mc_groups(family);
  353. idr_remove(&genl_fam_idr, family->id);
  354. up_write(&cb_lock);
  355. wait_event(genl_sk_destructing_waitq,
  356. atomic_read(&genl_sk_destructing_cnt) == 0);
  357. genl_unlock();
  358. kfree(family->attrbuf);
  359. genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(genl_unregister_family);
  363. /**
  364. * genlmsg_put - Add generic netlink header to netlink message
  365. * @skb: socket buffer holding the message
  366. * @portid: netlink portid the message is addressed to
  367. * @seq: sequence number (usually the one of the sender)
  368. * @family: generic netlink family
  369. * @flags: netlink message flags
  370. * @cmd: generic netlink command
  371. *
  372. * Returns pointer to user specific header
  373. */
  374. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  375. const struct genl_family *family, int flags, u8 cmd)
  376. {
  377. struct nlmsghdr *nlh;
  378. struct genlmsghdr *hdr;
  379. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  380. family->hdrsize, flags);
  381. if (nlh == NULL)
  382. return NULL;
  383. hdr = nlmsg_data(nlh);
  384. hdr->cmd = cmd;
  385. hdr->version = family->version;
  386. hdr->reserved = 0;
  387. return (char *) hdr + GENL_HDRLEN;
  388. }
  389. EXPORT_SYMBOL(genlmsg_put);
  390. static int genl_lock_start(struct netlink_callback *cb)
  391. {
  392. /* our ops are always const - netlink API doesn't propagate that */
  393. const struct genl_ops *ops = cb->data;
  394. int rc = 0;
  395. if (ops->start) {
  396. genl_lock();
  397. rc = ops->start(cb);
  398. genl_unlock();
  399. }
  400. return rc;
  401. }
  402. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  403. {
  404. /* our ops are always const - netlink API doesn't propagate that */
  405. const struct genl_ops *ops = cb->data;
  406. int rc;
  407. genl_lock();
  408. rc = ops->dumpit(skb, cb);
  409. genl_unlock();
  410. return rc;
  411. }
  412. static int genl_lock_done(struct netlink_callback *cb)
  413. {
  414. /* our ops are always const - netlink API doesn't propagate that */
  415. const struct genl_ops *ops = cb->data;
  416. int rc = 0;
  417. if (ops->done) {
  418. genl_lock();
  419. rc = ops->done(cb);
  420. genl_unlock();
  421. }
  422. return rc;
  423. }
  424. static int genl_family_rcv_msg(const struct genl_family *family,
  425. struct sk_buff *skb,
  426. struct nlmsghdr *nlh,
  427. struct netlink_ext_ack *extack)
  428. {
  429. const struct genl_ops *ops;
  430. struct net *net = sock_net(skb->sk);
  431. struct genl_info info;
  432. struct genlmsghdr *hdr = nlmsg_data(nlh);
  433. struct nlattr **attrbuf;
  434. int hdrlen, err;
  435. /* this family doesn't exist in this netns */
  436. if (!family->netnsok && !net_eq(net, &init_net))
  437. return -ENOENT;
  438. hdrlen = GENL_HDRLEN + family->hdrsize;
  439. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  440. return -EINVAL;
  441. ops = genl_get_cmd(hdr->cmd, family);
  442. if (ops == NULL)
  443. return -EOPNOTSUPP;
  444. if ((ops->flags & GENL_ADMIN_PERM) &&
  445. !netlink_capable(skb, CAP_NET_ADMIN))
  446. return -EPERM;
  447. if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
  448. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  449. return -EPERM;
  450. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
  451. int rc;
  452. if (ops->dumpit == NULL)
  453. return -EOPNOTSUPP;
  454. if (!family->parallel_ops) {
  455. struct netlink_dump_control c = {
  456. .module = family->module,
  457. /* we have const, but the netlink API doesn't */
  458. .data = (void *)ops,
  459. .start = genl_lock_start,
  460. .dump = genl_lock_dumpit,
  461. .done = genl_lock_done,
  462. };
  463. genl_unlock();
  464. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  465. genl_lock();
  466. } else {
  467. struct netlink_dump_control c = {
  468. .module = family->module,
  469. .start = ops->start,
  470. .dump = ops->dumpit,
  471. .done = ops->done,
  472. };
  473. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  474. }
  475. return rc;
  476. }
  477. if (ops->doit == NULL)
  478. return -EOPNOTSUPP;
  479. if (family->maxattr && family->parallel_ops) {
  480. attrbuf = kmalloc_array(family->maxattr + 1,
  481. sizeof(struct nlattr *),
  482. GFP_KERNEL);
  483. if (attrbuf == NULL)
  484. return -ENOMEM;
  485. } else
  486. attrbuf = family->attrbuf;
  487. if (attrbuf) {
  488. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  489. ops->policy, extack);
  490. if (err < 0)
  491. goto out;
  492. }
  493. info.snd_seq = nlh->nlmsg_seq;
  494. info.snd_portid = NETLINK_CB(skb).portid;
  495. info.nlhdr = nlh;
  496. info.genlhdr = nlmsg_data(nlh);
  497. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  498. info.attrs = attrbuf;
  499. info.extack = extack;
  500. genl_info_net_set(&info, net);
  501. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  502. if (family->pre_doit) {
  503. err = family->pre_doit(ops, skb, &info);
  504. if (err)
  505. goto out;
  506. }
  507. err = ops->doit(skb, &info);
  508. if (family->post_doit)
  509. family->post_doit(ops, skb, &info);
  510. out:
  511. if (family->parallel_ops)
  512. kfree(attrbuf);
  513. return err;
  514. }
  515. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  516. struct netlink_ext_ack *extack)
  517. {
  518. const struct genl_family *family;
  519. int err;
  520. family = genl_family_find_byid(nlh->nlmsg_type);
  521. if (family == NULL)
  522. return -ENOENT;
  523. if (!family->parallel_ops)
  524. genl_lock();
  525. err = genl_family_rcv_msg(family, skb, nlh, extack);
  526. if (!family->parallel_ops)
  527. genl_unlock();
  528. return err;
  529. }
  530. static void genl_rcv(struct sk_buff *skb)
  531. {
  532. down_read(&cb_lock);
  533. netlink_rcv_skb(skb, &genl_rcv_msg);
  534. up_read(&cb_lock);
  535. }
  536. /**************************************************************************
  537. * Controller
  538. **************************************************************************/
  539. static struct genl_family genl_ctrl;
  540. static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
  541. u32 flags, struct sk_buff *skb, u8 cmd)
  542. {
  543. void *hdr;
  544. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  545. if (hdr == NULL)
  546. return -1;
  547. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  548. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  549. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  550. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  551. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  552. goto nla_put_failure;
  553. if (family->n_ops) {
  554. struct nlattr *nla_ops;
  555. int i;
  556. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  557. if (nla_ops == NULL)
  558. goto nla_put_failure;
  559. for (i = 0; i < family->n_ops; i++) {
  560. struct nlattr *nest;
  561. const struct genl_ops *ops = &family->ops[i];
  562. u32 op_flags = ops->flags;
  563. if (ops->dumpit)
  564. op_flags |= GENL_CMD_CAP_DUMP;
  565. if (ops->doit)
  566. op_flags |= GENL_CMD_CAP_DO;
  567. if (ops->policy)
  568. op_flags |= GENL_CMD_CAP_HASPOL;
  569. nest = nla_nest_start(skb, i + 1);
  570. if (nest == NULL)
  571. goto nla_put_failure;
  572. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  573. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  574. goto nla_put_failure;
  575. nla_nest_end(skb, nest);
  576. }
  577. nla_nest_end(skb, nla_ops);
  578. }
  579. if (family->n_mcgrps) {
  580. struct nlattr *nla_grps;
  581. int i;
  582. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  583. if (nla_grps == NULL)
  584. goto nla_put_failure;
  585. for (i = 0; i < family->n_mcgrps; i++) {
  586. struct nlattr *nest;
  587. const struct genl_multicast_group *grp;
  588. grp = &family->mcgrps[i];
  589. nest = nla_nest_start(skb, i + 1);
  590. if (nest == NULL)
  591. goto nla_put_failure;
  592. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  593. family->mcgrp_offset + i) ||
  594. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  595. grp->name))
  596. goto nla_put_failure;
  597. nla_nest_end(skb, nest);
  598. }
  599. nla_nest_end(skb, nla_grps);
  600. }
  601. genlmsg_end(skb, hdr);
  602. return 0;
  603. nla_put_failure:
  604. genlmsg_cancel(skb, hdr);
  605. return -EMSGSIZE;
  606. }
  607. static int ctrl_fill_mcgrp_info(const struct genl_family *family,
  608. const struct genl_multicast_group *grp,
  609. int grp_id, u32 portid, u32 seq, u32 flags,
  610. struct sk_buff *skb, u8 cmd)
  611. {
  612. void *hdr;
  613. struct nlattr *nla_grps;
  614. struct nlattr *nest;
  615. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  616. if (hdr == NULL)
  617. return -1;
  618. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  619. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  620. goto nla_put_failure;
  621. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  622. if (nla_grps == NULL)
  623. goto nla_put_failure;
  624. nest = nla_nest_start(skb, 1);
  625. if (nest == NULL)
  626. goto nla_put_failure;
  627. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  628. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  629. grp->name))
  630. goto nla_put_failure;
  631. nla_nest_end(skb, nest);
  632. nla_nest_end(skb, nla_grps);
  633. genlmsg_end(skb, hdr);
  634. return 0;
  635. nla_put_failure:
  636. genlmsg_cancel(skb, hdr);
  637. return -EMSGSIZE;
  638. }
  639. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  640. {
  641. int n = 0;
  642. struct genl_family *rt;
  643. struct net *net = sock_net(skb->sk);
  644. int fams_to_skip = cb->args[0];
  645. unsigned int id;
  646. idr_for_each_entry(&genl_fam_idr, rt, id) {
  647. if (!rt->netnsok && !net_eq(net, &init_net))
  648. continue;
  649. if (n++ < fams_to_skip)
  650. continue;
  651. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  652. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  653. skb, CTRL_CMD_NEWFAMILY) < 0) {
  654. n--;
  655. break;
  656. }
  657. }
  658. cb->args[0] = n;
  659. return skb->len;
  660. }
  661. static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
  662. u32 portid, int seq, u8 cmd)
  663. {
  664. struct sk_buff *skb;
  665. int err;
  666. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  667. if (skb == NULL)
  668. return ERR_PTR(-ENOBUFS);
  669. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  670. if (err < 0) {
  671. nlmsg_free(skb);
  672. return ERR_PTR(err);
  673. }
  674. return skb;
  675. }
  676. static struct sk_buff *
  677. ctrl_build_mcgrp_msg(const struct genl_family *family,
  678. const struct genl_multicast_group *grp,
  679. int grp_id, u32 portid, int seq, u8 cmd)
  680. {
  681. struct sk_buff *skb;
  682. int err;
  683. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  684. if (skb == NULL)
  685. return ERR_PTR(-ENOBUFS);
  686. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  687. seq, 0, skb, cmd);
  688. if (err < 0) {
  689. nlmsg_free(skb);
  690. return ERR_PTR(err);
  691. }
  692. return skb;
  693. }
  694. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  695. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  696. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  697. .len = GENL_NAMSIZ - 1 },
  698. };
  699. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  700. {
  701. struct sk_buff *msg;
  702. const struct genl_family *res = NULL;
  703. int err = -EINVAL;
  704. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  705. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  706. res = genl_family_find_byid(id);
  707. err = -ENOENT;
  708. }
  709. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  710. char *name;
  711. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  712. res = genl_family_find_byname(name);
  713. #ifdef CONFIG_MODULES
  714. if (res == NULL) {
  715. genl_unlock();
  716. up_read(&cb_lock);
  717. request_module("net-pf-%d-proto-%d-family-%s",
  718. PF_NETLINK, NETLINK_GENERIC, name);
  719. down_read(&cb_lock);
  720. genl_lock();
  721. res = genl_family_find_byname(name);
  722. }
  723. #endif
  724. err = -ENOENT;
  725. }
  726. if (res == NULL)
  727. return err;
  728. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  729. /* family doesn't exist here */
  730. return -ENOENT;
  731. }
  732. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  733. CTRL_CMD_NEWFAMILY);
  734. if (IS_ERR(msg))
  735. return PTR_ERR(msg);
  736. return genlmsg_reply(msg, info);
  737. }
  738. static int genl_ctrl_event(int event, const struct genl_family *family,
  739. const struct genl_multicast_group *grp,
  740. int grp_id)
  741. {
  742. struct sk_buff *msg;
  743. /* genl is still initialising */
  744. if (!init_net.genl_sock)
  745. return 0;
  746. switch (event) {
  747. case CTRL_CMD_NEWFAMILY:
  748. case CTRL_CMD_DELFAMILY:
  749. WARN_ON(grp);
  750. msg = ctrl_build_family_msg(family, 0, 0, event);
  751. break;
  752. case CTRL_CMD_NEWMCAST_GRP:
  753. case CTRL_CMD_DELMCAST_GRP:
  754. BUG_ON(!grp);
  755. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  756. break;
  757. default:
  758. return -EINVAL;
  759. }
  760. if (IS_ERR(msg))
  761. return PTR_ERR(msg);
  762. if (!family->netnsok) {
  763. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  764. 0, GFP_KERNEL);
  765. } else {
  766. rcu_read_lock();
  767. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  768. 0, GFP_ATOMIC);
  769. rcu_read_unlock();
  770. }
  771. return 0;
  772. }
  773. static const struct genl_ops genl_ctrl_ops[] = {
  774. {
  775. .cmd = CTRL_CMD_GETFAMILY,
  776. .doit = ctrl_getfamily,
  777. .dumpit = ctrl_dumpfamily,
  778. .policy = ctrl_policy,
  779. },
  780. };
  781. static const struct genl_multicast_group genl_ctrl_groups[] = {
  782. { .name = "notify", },
  783. };
  784. static struct genl_family genl_ctrl __ro_after_init = {
  785. .module = THIS_MODULE,
  786. .ops = genl_ctrl_ops,
  787. .n_ops = ARRAY_SIZE(genl_ctrl_ops),
  788. .mcgrps = genl_ctrl_groups,
  789. .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
  790. .id = GENL_ID_CTRL,
  791. .name = "nlctrl",
  792. .version = 0x2,
  793. .maxattr = CTRL_ATTR_MAX,
  794. .netnsok = true,
  795. };
  796. static int genl_bind(struct net *net, int group)
  797. {
  798. struct genl_family *f;
  799. int err = -ENOENT;
  800. unsigned int id;
  801. down_read(&cb_lock);
  802. idr_for_each_entry(&genl_fam_idr, f, id) {
  803. if (group >= f->mcgrp_offset &&
  804. group < f->mcgrp_offset + f->n_mcgrps) {
  805. int fam_grp = group - f->mcgrp_offset;
  806. if (!f->netnsok && net != &init_net)
  807. err = -ENOENT;
  808. else if (f->mcast_bind)
  809. err = f->mcast_bind(net, fam_grp);
  810. else
  811. err = 0;
  812. break;
  813. }
  814. }
  815. up_read(&cb_lock);
  816. return err;
  817. }
  818. static void genl_unbind(struct net *net, int group)
  819. {
  820. struct genl_family *f;
  821. unsigned int id;
  822. down_read(&cb_lock);
  823. idr_for_each_entry(&genl_fam_idr, f, id) {
  824. if (group >= f->mcgrp_offset &&
  825. group < f->mcgrp_offset + f->n_mcgrps) {
  826. int fam_grp = group - f->mcgrp_offset;
  827. if (f->mcast_unbind)
  828. f->mcast_unbind(net, fam_grp);
  829. break;
  830. }
  831. }
  832. up_read(&cb_lock);
  833. }
  834. static int __net_init genl_pernet_init(struct net *net)
  835. {
  836. struct netlink_kernel_cfg cfg = {
  837. .input = genl_rcv,
  838. .flags = NL_CFG_F_NONROOT_RECV,
  839. .bind = genl_bind,
  840. .unbind = genl_unbind,
  841. };
  842. /* we'll bump the group number right afterwards */
  843. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  844. if (!net->genl_sock && net_eq(net, &init_net))
  845. panic("GENL: Cannot initialize generic netlink\n");
  846. if (!net->genl_sock)
  847. return -ENOMEM;
  848. return 0;
  849. }
  850. static void __net_exit genl_pernet_exit(struct net *net)
  851. {
  852. netlink_kernel_release(net->genl_sock);
  853. net->genl_sock = NULL;
  854. }
  855. static struct pernet_operations genl_pernet_ops = {
  856. .init = genl_pernet_init,
  857. .exit = genl_pernet_exit,
  858. };
  859. static int __init genl_init(void)
  860. {
  861. int err;
  862. err = genl_register_family(&genl_ctrl);
  863. if (err < 0)
  864. goto problem;
  865. err = register_pernet_subsys(&genl_pernet_ops);
  866. if (err)
  867. goto problem;
  868. return 0;
  869. problem:
  870. panic("GENL: Cannot register controller: %d\n", err);
  871. }
  872. subsys_initcall(genl_init);
  873. /**
  874. * genl_family_attrbuf - return family's attrbuf
  875. * @family: the family
  876. *
  877. * Return the family's attrbuf, while validating that it's
  878. * actually valid to access it.
  879. *
  880. * You cannot use this function with a family that has parallel_ops
  881. * and you can only use it within (pre/post) doit/dumpit callbacks.
  882. */
  883. struct nlattr **genl_family_attrbuf(const struct genl_family *family)
  884. {
  885. if (!WARN_ON(family->parallel_ops))
  886. lockdep_assert_held(&genl_mutex);
  887. return family->attrbuf;
  888. }
  889. EXPORT_SYMBOL(genl_family_attrbuf);
  890. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  891. gfp_t flags)
  892. {
  893. struct sk_buff *tmp;
  894. struct net *net, *prev = NULL;
  895. bool delivered = false;
  896. int err;
  897. for_each_net_rcu(net) {
  898. if (prev) {
  899. tmp = skb_clone(skb, flags);
  900. if (!tmp) {
  901. err = -ENOMEM;
  902. goto error;
  903. }
  904. err = nlmsg_multicast(prev->genl_sock, tmp,
  905. portid, group, flags);
  906. if (!err)
  907. delivered = true;
  908. else if (err != -ESRCH)
  909. goto error;
  910. }
  911. prev = net;
  912. }
  913. err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  914. if (!err)
  915. delivered = true;
  916. else if (err != -ESRCH)
  917. return err;
  918. return delivered ? 0 : -ESRCH;
  919. error:
  920. kfree_skb(skb);
  921. return err;
  922. }
  923. int genlmsg_multicast_allns(const struct genl_family *family,
  924. struct sk_buff *skb, u32 portid,
  925. unsigned int group, gfp_t flags)
  926. {
  927. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  928. return -EINVAL;
  929. group = family->mcgrp_offset + group;
  930. return genlmsg_mcast(skb, portid, group, flags);
  931. }
  932. EXPORT_SYMBOL(genlmsg_multicast_allns);
  933. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  934. struct genl_info *info, u32 group, gfp_t flags)
  935. {
  936. struct net *net = genl_info_net(info);
  937. struct sock *sk = net->genl_sock;
  938. int report = 0;
  939. if (info->nlhdr)
  940. report = nlmsg_report(info->nlhdr);
  941. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  942. return;
  943. group = family->mcgrp_offset + group;
  944. nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
  945. }
  946. EXPORT_SYMBOL(genl_notify);