genetlink.c 24 KB

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