genetlink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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((family->maxattr+1) *
  305. sizeof(struct nlattr *), GFP_KERNEL);
  306. if (family->attrbuf == NULL) {
  307. err = -ENOMEM;
  308. goto errout_locked;
  309. }
  310. } else
  311. family->attrbuf = NULL;
  312. family->id = idr_alloc(&genl_fam_idr, family,
  313. start, end + 1, GFP_KERNEL);
  314. if (family->id < 0) {
  315. err = family->id;
  316. goto errout_free;
  317. }
  318. err = genl_validate_assign_mc_groups(family);
  319. if (err)
  320. goto errout_remove;
  321. genl_unlock_all();
  322. /* send all events */
  323. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
  324. for (i = 0; i < family->n_mcgrps; i++)
  325. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
  326. &family->mcgrps[i], family->mcgrp_offset + i);
  327. return 0;
  328. errout_remove:
  329. idr_remove(&genl_fam_idr, family->id);
  330. errout_free:
  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((family->maxattr+1) *
  481. sizeof(struct nlattr *), GFP_KERNEL);
  482. if (attrbuf == NULL)
  483. return -ENOMEM;
  484. } else
  485. attrbuf = family->attrbuf;
  486. if (attrbuf) {
  487. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  488. ops->policy, extack);
  489. if (err < 0)
  490. goto out;
  491. }
  492. info.snd_seq = nlh->nlmsg_seq;
  493. info.snd_portid = NETLINK_CB(skb).portid;
  494. info.nlhdr = nlh;
  495. info.genlhdr = nlmsg_data(nlh);
  496. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  497. info.attrs = attrbuf;
  498. info.extack = extack;
  499. genl_info_net_set(&info, net);
  500. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  501. if (family->pre_doit) {
  502. err = family->pre_doit(ops, skb, &info);
  503. if (err)
  504. goto out;
  505. }
  506. err = ops->doit(skb, &info);
  507. if (family->post_doit)
  508. family->post_doit(ops, skb, &info);
  509. out:
  510. if (family->parallel_ops)
  511. kfree(attrbuf);
  512. return err;
  513. }
  514. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  515. struct netlink_ext_ack *extack)
  516. {
  517. const struct genl_family *family;
  518. int err;
  519. family = genl_family_find_byid(nlh->nlmsg_type);
  520. if (family == NULL)
  521. return -ENOENT;
  522. if (!family->parallel_ops)
  523. genl_lock();
  524. err = genl_family_rcv_msg(family, skb, nlh, extack);
  525. if (!family->parallel_ops)
  526. genl_unlock();
  527. return err;
  528. }
  529. static void genl_rcv(struct sk_buff *skb)
  530. {
  531. down_read(&cb_lock);
  532. netlink_rcv_skb(skb, &genl_rcv_msg);
  533. up_read(&cb_lock);
  534. }
  535. /**************************************************************************
  536. * Controller
  537. **************************************************************************/
  538. static struct genl_family genl_ctrl;
  539. static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
  540. u32 flags, struct sk_buff *skb, u8 cmd)
  541. {
  542. void *hdr;
  543. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  544. if (hdr == NULL)
  545. return -1;
  546. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  547. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  548. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  549. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  550. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  551. goto nla_put_failure;
  552. if (family->n_ops) {
  553. struct nlattr *nla_ops;
  554. int i;
  555. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  556. if (nla_ops == NULL)
  557. goto nla_put_failure;
  558. for (i = 0; i < family->n_ops; i++) {
  559. struct nlattr *nest;
  560. const struct genl_ops *ops = &family->ops[i];
  561. u32 op_flags = ops->flags;
  562. if (ops->dumpit)
  563. op_flags |= GENL_CMD_CAP_DUMP;
  564. if (ops->doit)
  565. op_flags |= GENL_CMD_CAP_DO;
  566. if (ops->policy)
  567. op_flags |= GENL_CMD_CAP_HASPOL;
  568. nest = nla_nest_start(skb, i + 1);
  569. if (nest == NULL)
  570. goto nla_put_failure;
  571. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  572. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  573. goto nla_put_failure;
  574. nla_nest_end(skb, nest);
  575. }
  576. nla_nest_end(skb, nla_ops);
  577. }
  578. if (family->n_mcgrps) {
  579. struct nlattr *nla_grps;
  580. int i;
  581. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  582. if (nla_grps == NULL)
  583. goto nla_put_failure;
  584. for (i = 0; i < family->n_mcgrps; i++) {
  585. struct nlattr *nest;
  586. const struct genl_multicast_group *grp;
  587. grp = &family->mcgrps[i];
  588. nest = nla_nest_start(skb, i + 1);
  589. if (nest == NULL)
  590. goto nla_put_failure;
  591. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  592. family->mcgrp_offset + i) ||
  593. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  594. grp->name))
  595. goto nla_put_failure;
  596. nla_nest_end(skb, nest);
  597. }
  598. nla_nest_end(skb, nla_grps);
  599. }
  600. genlmsg_end(skb, hdr);
  601. return 0;
  602. nla_put_failure:
  603. genlmsg_cancel(skb, hdr);
  604. return -EMSGSIZE;
  605. }
  606. static int ctrl_fill_mcgrp_info(const struct genl_family *family,
  607. const struct genl_multicast_group *grp,
  608. int grp_id, u32 portid, u32 seq, u32 flags,
  609. struct sk_buff *skb, u8 cmd)
  610. {
  611. void *hdr;
  612. struct nlattr *nla_grps;
  613. struct nlattr *nest;
  614. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  615. if (hdr == NULL)
  616. return -1;
  617. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  618. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  619. goto nla_put_failure;
  620. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  621. if (nla_grps == NULL)
  622. goto nla_put_failure;
  623. nest = nla_nest_start(skb, 1);
  624. if (nest == NULL)
  625. goto nla_put_failure;
  626. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  627. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  628. grp->name))
  629. goto nla_put_failure;
  630. nla_nest_end(skb, nest);
  631. nla_nest_end(skb, nla_grps);
  632. genlmsg_end(skb, hdr);
  633. return 0;
  634. nla_put_failure:
  635. genlmsg_cancel(skb, hdr);
  636. return -EMSGSIZE;
  637. }
  638. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  639. {
  640. int n = 0;
  641. struct genl_family *rt;
  642. struct net *net = sock_net(skb->sk);
  643. int fams_to_skip = cb->args[0];
  644. unsigned int id;
  645. idr_for_each_entry(&genl_fam_idr, rt, id) {
  646. if (!rt->netnsok && !net_eq(net, &init_net))
  647. continue;
  648. if (n++ < fams_to_skip)
  649. continue;
  650. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  651. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  652. skb, CTRL_CMD_NEWFAMILY) < 0) {
  653. n--;
  654. break;
  655. }
  656. }
  657. cb->args[0] = n;
  658. return skb->len;
  659. }
  660. static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
  661. u32 portid, int seq, u8 cmd)
  662. {
  663. struct sk_buff *skb;
  664. int err;
  665. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  666. if (skb == NULL)
  667. return ERR_PTR(-ENOBUFS);
  668. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  669. if (err < 0) {
  670. nlmsg_free(skb);
  671. return ERR_PTR(err);
  672. }
  673. return skb;
  674. }
  675. static struct sk_buff *
  676. ctrl_build_mcgrp_msg(const struct genl_family *family,
  677. const struct genl_multicast_group *grp,
  678. int grp_id, u32 portid, int seq, u8 cmd)
  679. {
  680. struct sk_buff *skb;
  681. int err;
  682. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  683. if (skb == NULL)
  684. return ERR_PTR(-ENOBUFS);
  685. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  686. seq, 0, skb, cmd);
  687. if (err < 0) {
  688. nlmsg_free(skb);
  689. return ERR_PTR(err);
  690. }
  691. return skb;
  692. }
  693. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  694. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  695. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  696. .len = GENL_NAMSIZ - 1 },
  697. };
  698. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  699. {
  700. struct sk_buff *msg;
  701. const struct genl_family *res = NULL;
  702. int err = -EINVAL;
  703. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  704. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  705. res = genl_family_find_byid(id);
  706. err = -ENOENT;
  707. }
  708. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  709. char *name;
  710. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  711. res = genl_family_find_byname(name);
  712. #ifdef CONFIG_MODULES
  713. if (res == NULL) {
  714. genl_unlock();
  715. up_read(&cb_lock);
  716. request_module("net-pf-%d-proto-%d-family-%s",
  717. PF_NETLINK, NETLINK_GENERIC, name);
  718. down_read(&cb_lock);
  719. genl_lock();
  720. res = genl_family_find_byname(name);
  721. }
  722. #endif
  723. err = -ENOENT;
  724. }
  725. if (res == NULL)
  726. return err;
  727. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  728. /* family doesn't exist here */
  729. return -ENOENT;
  730. }
  731. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  732. CTRL_CMD_NEWFAMILY);
  733. if (IS_ERR(msg))
  734. return PTR_ERR(msg);
  735. return genlmsg_reply(msg, info);
  736. }
  737. static int genl_ctrl_event(int event, const struct genl_family *family,
  738. const struct genl_multicast_group *grp,
  739. int grp_id)
  740. {
  741. struct sk_buff *msg;
  742. /* genl is still initialising */
  743. if (!init_net.genl_sock)
  744. return 0;
  745. switch (event) {
  746. case CTRL_CMD_NEWFAMILY:
  747. case CTRL_CMD_DELFAMILY:
  748. WARN_ON(grp);
  749. msg = ctrl_build_family_msg(family, 0, 0, event);
  750. break;
  751. case CTRL_CMD_NEWMCAST_GRP:
  752. case CTRL_CMD_DELMCAST_GRP:
  753. BUG_ON(!grp);
  754. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  755. break;
  756. default:
  757. return -EINVAL;
  758. }
  759. if (IS_ERR(msg))
  760. return PTR_ERR(msg);
  761. if (!family->netnsok) {
  762. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  763. 0, GFP_KERNEL);
  764. } else {
  765. rcu_read_lock();
  766. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  767. 0, GFP_ATOMIC);
  768. rcu_read_unlock();
  769. }
  770. return 0;
  771. }
  772. static const struct genl_ops genl_ctrl_ops[] = {
  773. {
  774. .cmd = CTRL_CMD_GETFAMILY,
  775. .doit = ctrl_getfamily,
  776. .dumpit = ctrl_dumpfamily,
  777. .policy = ctrl_policy,
  778. },
  779. };
  780. static const struct genl_multicast_group genl_ctrl_groups[] = {
  781. { .name = "notify", },
  782. };
  783. static struct genl_family genl_ctrl __ro_after_init = {
  784. .module = THIS_MODULE,
  785. .ops = genl_ctrl_ops,
  786. .n_ops = ARRAY_SIZE(genl_ctrl_ops),
  787. .mcgrps = genl_ctrl_groups,
  788. .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
  789. .id = GENL_ID_CTRL,
  790. .name = "nlctrl",
  791. .version = 0x2,
  792. .maxattr = CTRL_ATTR_MAX,
  793. .netnsok = true,
  794. };
  795. static int __net_init genl_pernet_init(struct net *net)
  796. {
  797. struct netlink_kernel_cfg cfg = {
  798. .input = genl_rcv,
  799. .flags = NL_CFG_F_NONROOT_RECV,
  800. };
  801. /* we'll bump the group number right afterwards */
  802. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  803. if (!net->genl_sock && net_eq(net, &init_net))
  804. panic("GENL: Cannot initialize generic netlink\n");
  805. if (!net->genl_sock)
  806. return -ENOMEM;
  807. return 0;
  808. }
  809. static void __net_exit genl_pernet_exit(struct net *net)
  810. {
  811. netlink_kernel_release(net->genl_sock);
  812. net->genl_sock = NULL;
  813. }
  814. static struct pernet_operations genl_pernet_ops = {
  815. .init = genl_pernet_init,
  816. .exit = genl_pernet_exit,
  817. };
  818. static int __init genl_init(void)
  819. {
  820. int err;
  821. err = genl_register_family(&genl_ctrl);
  822. if (err < 0)
  823. goto problem;
  824. err = register_pernet_subsys(&genl_pernet_ops);
  825. if (err)
  826. goto problem;
  827. return 0;
  828. problem:
  829. panic("GENL: Cannot register controller: %d\n", err);
  830. }
  831. subsys_initcall(genl_init);
  832. /**
  833. * genl_family_attrbuf - return family's attrbuf
  834. * @family: the family
  835. *
  836. * Return the family's attrbuf, while validating that it's
  837. * actually valid to access it.
  838. *
  839. * You cannot use this function with a family that has parallel_ops
  840. * and you can only use it within (pre/post) doit/dumpit callbacks.
  841. */
  842. struct nlattr **genl_family_attrbuf(const struct genl_family *family)
  843. {
  844. if (!WARN_ON(family->parallel_ops))
  845. lockdep_assert_held(&genl_mutex);
  846. return family->attrbuf;
  847. }
  848. EXPORT_SYMBOL(genl_family_attrbuf);
  849. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  850. gfp_t flags)
  851. {
  852. struct sk_buff *tmp;
  853. struct net *net, *prev = NULL;
  854. bool delivered = false;
  855. int err;
  856. for_each_net_rcu(net) {
  857. if (prev) {
  858. tmp = skb_clone(skb, flags);
  859. if (!tmp) {
  860. err = -ENOMEM;
  861. goto error;
  862. }
  863. err = nlmsg_multicast(prev->genl_sock, tmp,
  864. portid, group, flags);
  865. if (!err)
  866. delivered = true;
  867. else if (err != -ESRCH)
  868. goto error;
  869. }
  870. prev = net;
  871. }
  872. err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  873. if (!err)
  874. delivered = true;
  875. else if (err != -ESRCH)
  876. return err;
  877. return delivered ? 0 : -ESRCH;
  878. error:
  879. kfree_skb(skb);
  880. return err;
  881. }
  882. int genlmsg_multicast_allns(const struct genl_family *family,
  883. struct sk_buff *skb, u32 portid,
  884. unsigned int group, gfp_t flags)
  885. {
  886. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  887. return -EINVAL;
  888. group = family->mcgrp_offset + group;
  889. return genlmsg_mcast(skb, portid, group, flags);
  890. }
  891. EXPORT_SYMBOL(genlmsg_multicast_allns);
  892. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  893. struct genl_info *info, u32 group, gfp_t flags)
  894. {
  895. struct net *net = genl_info_net(info);
  896. struct sock *sk = net->genl_sock;
  897. int report = 0;
  898. if (info->nlhdr)
  899. report = nlmsg_report(info->nlhdr);
  900. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  901. return;
  902. group = family->mcgrp_offset + group;
  903. nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
  904. }
  905. EXPORT_SYMBOL(genl_notify);