genetlink.c 25 KB

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