genetlink.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 <net/sock.h>
  19. #include <net/genetlink.h>
  20. static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
  21. void genl_lock(void)
  22. {
  23. mutex_lock(&genl_mutex);
  24. }
  25. EXPORT_SYMBOL(genl_lock);
  26. void genl_unlock(void)
  27. {
  28. mutex_unlock(&genl_mutex);
  29. }
  30. EXPORT_SYMBOL(genl_unlock);
  31. #define GENL_FAM_TAB_SIZE 16
  32. #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
  33. static struct list_head family_ht[GENL_FAM_TAB_SIZE];
  34. /*
  35. * Bitmap of multicast groups that are currently in use.
  36. *
  37. * To avoid an allocation at boot of just one unsigned long,
  38. * declare it global instead.
  39. * Bit 0 is marked as already used since group 0 is invalid.
  40. */
  41. static unsigned long mc_group_start = 0x1;
  42. static unsigned long *mc_groups = &mc_group_start;
  43. static unsigned long mc_groups_longs = 1;
  44. static int genl_ctrl_event(int event, void *data);
  45. static inline unsigned int genl_family_hash(unsigned int id)
  46. {
  47. return id & GENL_FAM_TAB_MASK;
  48. }
  49. static inline struct list_head *genl_family_chain(unsigned int id)
  50. {
  51. return &family_ht[genl_family_hash(id)];
  52. }
  53. static struct genl_family *genl_family_find_byid(unsigned int id)
  54. {
  55. struct genl_family *f;
  56. list_for_each_entry(f, genl_family_chain(id), family_list)
  57. if (f->id == id)
  58. return f;
  59. return NULL;
  60. }
  61. static struct genl_family *genl_family_find_byname(char *name)
  62. {
  63. struct genl_family *f;
  64. int i;
  65. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  66. list_for_each_entry(f, genl_family_chain(i), family_list)
  67. if (strcmp(f->name, name) == 0)
  68. return f;
  69. return NULL;
  70. }
  71. static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
  72. {
  73. struct genl_ops *ops;
  74. list_for_each_entry(ops, &family->ops_list, ops_list)
  75. if (ops->cmd == cmd)
  76. return ops;
  77. return NULL;
  78. }
  79. /* Of course we are going to have problems once we hit
  80. * 2^16 alive types, but that can only happen by year 2K
  81. */
  82. static inline u16 genl_generate_id(void)
  83. {
  84. static u16 id_gen_idx = GENL_MIN_ID;
  85. int i;
  86. for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
  87. if (!genl_family_find_byid(id_gen_idx))
  88. return id_gen_idx;
  89. if (++id_gen_idx > GENL_MAX_ID)
  90. id_gen_idx = GENL_MIN_ID;
  91. }
  92. return 0;
  93. }
  94. static struct genl_multicast_group notify_grp;
  95. /**
  96. * genl_register_mc_group - register a multicast group
  97. *
  98. * Registers the specified multicast group and notifies userspace
  99. * about the new group.
  100. *
  101. * Returns 0 on success or a negative error code.
  102. *
  103. * @family: The generic netlink family the group shall be registered for.
  104. * @grp: The group to register, must have a name.
  105. */
  106. int genl_register_mc_group(struct genl_family *family,
  107. struct genl_multicast_group *grp)
  108. {
  109. int id;
  110. unsigned long *new_groups;
  111. int err = 0;
  112. BUG_ON(grp->name[0] == '\0');
  113. genl_lock();
  114. /* special-case our own group */
  115. if (grp == &notify_grp)
  116. id = GENL_ID_CTRL;
  117. else
  118. id = find_first_zero_bit(mc_groups,
  119. mc_groups_longs * BITS_PER_LONG);
  120. if (id >= mc_groups_longs * BITS_PER_LONG) {
  121. size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
  122. if (mc_groups == &mc_group_start) {
  123. new_groups = kzalloc(nlen, GFP_KERNEL);
  124. if (!new_groups) {
  125. err = -ENOMEM;
  126. goto out;
  127. }
  128. mc_groups = new_groups;
  129. *mc_groups = mc_group_start;
  130. } else {
  131. new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
  132. if (!new_groups) {
  133. err = -ENOMEM;
  134. goto out;
  135. }
  136. mc_groups = new_groups;
  137. mc_groups[mc_groups_longs] = 0;
  138. }
  139. mc_groups_longs++;
  140. }
  141. if (family->netnsok) {
  142. struct net *net;
  143. netlink_table_grab();
  144. rcu_read_lock();
  145. for_each_net_rcu(net) {
  146. err = __netlink_change_ngroups(net->genl_sock,
  147. mc_groups_longs * BITS_PER_LONG);
  148. if (err) {
  149. /*
  150. * No need to roll back, can only fail if
  151. * memory allocation fails and then the
  152. * number of _possible_ groups has been
  153. * increased on some sockets which is ok.
  154. */
  155. rcu_read_unlock();
  156. netlink_table_ungrab();
  157. goto out;
  158. }
  159. }
  160. rcu_read_unlock();
  161. netlink_table_ungrab();
  162. } else {
  163. err = netlink_change_ngroups(init_net.genl_sock,
  164. mc_groups_longs * BITS_PER_LONG);
  165. if (err)
  166. goto out;
  167. }
  168. grp->id = id;
  169. set_bit(id, mc_groups);
  170. list_add_tail(&grp->list, &family->mcast_groups);
  171. grp->family = family;
  172. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
  173. out:
  174. genl_unlock();
  175. return err;
  176. }
  177. EXPORT_SYMBOL(genl_register_mc_group);
  178. static void __genl_unregister_mc_group(struct genl_family *family,
  179. struct genl_multicast_group *grp)
  180. {
  181. struct net *net;
  182. BUG_ON(grp->family != family);
  183. netlink_table_grab();
  184. rcu_read_lock();
  185. for_each_net_rcu(net)
  186. __netlink_clear_multicast_users(net->genl_sock, grp->id);
  187. rcu_read_unlock();
  188. netlink_table_ungrab();
  189. clear_bit(grp->id, mc_groups);
  190. list_del(&grp->list);
  191. genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
  192. grp->id = 0;
  193. grp->family = NULL;
  194. }
  195. /**
  196. * genl_unregister_mc_group - unregister a multicast group
  197. *
  198. * Unregisters the specified multicast group and notifies userspace
  199. * about it. All current listeners on the group are removed.
  200. *
  201. * Note: It is not necessary to unregister all multicast groups before
  202. * unregistering the family, unregistering the family will cause
  203. * all assigned multicast groups to be unregistered automatically.
  204. *
  205. * @family: Generic netlink family the group belongs to.
  206. * @grp: The group to unregister, must have been registered successfully
  207. * previously.
  208. */
  209. void genl_unregister_mc_group(struct genl_family *family,
  210. struct genl_multicast_group *grp)
  211. {
  212. genl_lock();
  213. __genl_unregister_mc_group(family, grp);
  214. genl_unlock();
  215. }
  216. EXPORT_SYMBOL(genl_unregister_mc_group);
  217. static void genl_unregister_mc_groups(struct genl_family *family)
  218. {
  219. struct genl_multicast_group *grp, *tmp;
  220. list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
  221. __genl_unregister_mc_group(family, grp);
  222. }
  223. /**
  224. * genl_register_ops - register generic netlink operations
  225. * @family: generic netlink family
  226. * @ops: operations to be registered
  227. *
  228. * Registers the specified operations and assigns them to the specified
  229. * family. Either a doit or dumpit callback must be specified or the
  230. * operation will fail. Only one operation structure per command
  231. * identifier may be registered.
  232. *
  233. * See include/net/genetlink.h for more documenation on the operations
  234. * structure.
  235. *
  236. * Returns 0 on success or a negative error code.
  237. */
  238. int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
  239. {
  240. int err = -EINVAL;
  241. if (ops->dumpit == NULL && ops->doit == NULL)
  242. goto errout;
  243. if (genl_get_cmd(ops->cmd, family)) {
  244. err = -EEXIST;
  245. goto errout;
  246. }
  247. if (ops->dumpit)
  248. ops->flags |= GENL_CMD_CAP_DUMP;
  249. if (ops->doit)
  250. ops->flags |= GENL_CMD_CAP_DO;
  251. if (ops->policy)
  252. ops->flags |= GENL_CMD_CAP_HASPOL;
  253. genl_lock();
  254. list_add_tail(&ops->ops_list, &family->ops_list);
  255. genl_unlock();
  256. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  257. err = 0;
  258. errout:
  259. return err;
  260. }
  261. EXPORT_SYMBOL(genl_register_ops);
  262. /**
  263. * genl_unregister_ops - unregister generic netlink operations
  264. * @family: generic netlink family
  265. * @ops: operations to be unregistered
  266. *
  267. * Unregisters the specified operations and unassigns them from the
  268. * specified family. The operation blocks until the current message
  269. * processing has finished and doesn't start again until the
  270. * unregister process has finished.
  271. *
  272. * Note: It is not necessary to unregister all operations before
  273. * unregistering the family, unregistering the family will cause
  274. * all assigned operations to be unregistered automatically.
  275. *
  276. * Returns 0 on success or a negative error code.
  277. */
  278. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  279. {
  280. struct genl_ops *rc;
  281. genl_lock();
  282. list_for_each_entry(rc, &family->ops_list, ops_list) {
  283. if (rc == ops) {
  284. list_del(&ops->ops_list);
  285. genl_unlock();
  286. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  287. return 0;
  288. }
  289. }
  290. genl_unlock();
  291. return -ENOENT;
  292. }
  293. EXPORT_SYMBOL(genl_unregister_ops);
  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. * Return 0 on success or a negative error code.
  304. */
  305. int genl_register_family(struct genl_family *family)
  306. {
  307. int err = -EINVAL;
  308. if (family->id && family->id < GENL_MIN_ID)
  309. goto errout;
  310. if (family->id > GENL_MAX_ID)
  311. goto errout;
  312. INIT_LIST_HEAD(&family->ops_list);
  313. INIT_LIST_HEAD(&family->mcast_groups);
  314. genl_lock();
  315. if (genl_family_find_byname(family->name)) {
  316. err = -EEXIST;
  317. goto errout_locked;
  318. }
  319. if (family->id == GENL_ID_GENERATE) {
  320. u16 newid = genl_generate_id();
  321. if (!newid) {
  322. err = -ENOMEM;
  323. goto errout_locked;
  324. }
  325. family->id = newid;
  326. } else if (genl_family_find_byid(family->id)) {
  327. err = -EEXIST;
  328. goto errout_locked;
  329. }
  330. if (family->maxattr) {
  331. family->attrbuf = kmalloc((family->maxattr+1) *
  332. sizeof(struct nlattr *), GFP_KERNEL);
  333. if (family->attrbuf == NULL) {
  334. err = -ENOMEM;
  335. goto errout_locked;
  336. }
  337. } else
  338. family->attrbuf = NULL;
  339. list_add_tail(&family->family_list, genl_family_chain(family->id));
  340. genl_unlock();
  341. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  342. return 0;
  343. errout_locked:
  344. genl_unlock();
  345. errout:
  346. return err;
  347. }
  348. EXPORT_SYMBOL(genl_register_family);
  349. /**
  350. * genl_register_family_with_ops - register a generic netlink family
  351. * @family: generic netlink family
  352. * @ops: operations to be registered
  353. * @n_ops: number of elements to register
  354. *
  355. * Registers the specified family and operations from the specified table.
  356. * Only one family may be registered with the same family name or identifier.
  357. *
  358. * The family id may equal GENL_ID_GENERATE causing an unique id to
  359. * be automatically generated and assigned.
  360. *
  361. * Either a doit or dumpit callback must be specified for every registered
  362. * operation or the function will fail. Only one operation structure per
  363. * command identifier may be registered.
  364. *
  365. * See include/net/genetlink.h for more documenation on the operations
  366. * structure.
  367. *
  368. * This is equivalent to calling genl_register_family() followed by
  369. * genl_register_ops() for every operation entry in the table taking
  370. * care to unregister the family on error path.
  371. *
  372. * Return 0 on success or a negative error code.
  373. */
  374. int genl_register_family_with_ops(struct genl_family *family,
  375. struct genl_ops *ops, size_t n_ops)
  376. {
  377. int err, i;
  378. err = genl_register_family(family);
  379. if (err)
  380. return err;
  381. for (i = 0; i < n_ops; ++i, ++ops) {
  382. err = genl_register_ops(family, ops);
  383. if (err)
  384. goto err_out;
  385. }
  386. return 0;
  387. err_out:
  388. genl_unregister_family(family);
  389. return err;
  390. }
  391. EXPORT_SYMBOL(genl_register_family_with_ops);
  392. /**
  393. * genl_unregister_family - unregister generic netlink family
  394. * @family: generic netlink family
  395. *
  396. * Unregisters the specified family.
  397. *
  398. * Returns 0 on success or a negative error code.
  399. */
  400. int genl_unregister_family(struct genl_family *family)
  401. {
  402. struct genl_family *rc;
  403. genl_lock();
  404. genl_unregister_mc_groups(family);
  405. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  406. if (family->id != rc->id || strcmp(rc->name, family->name))
  407. continue;
  408. list_del(&rc->family_list);
  409. INIT_LIST_HEAD(&family->ops_list);
  410. genl_unlock();
  411. kfree(family->attrbuf);
  412. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  413. return 0;
  414. }
  415. genl_unlock();
  416. return -ENOENT;
  417. }
  418. EXPORT_SYMBOL(genl_unregister_family);
  419. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  420. {
  421. struct genl_ops *ops;
  422. struct genl_family *family;
  423. struct net *net = sock_net(skb->sk);
  424. struct genl_info info;
  425. struct genlmsghdr *hdr = nlmsg_data(nlh);
  426. int hdrlen, err;
  427. family = genl_family_find_byid(nlh->nlmsg_type);
  428. if (family == NULL)
  429. return -ENOENT;
  430. /* this family doesn't exist in this netns */
  431. if (!family->netnsok && !net_eq(net, &init_net))
  432. return -ENOENT;
  433. hdrlen = GENL_HDRLEN + family->hdrsize;
  434. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  435. return -EINVAL;
  436. ops = genl_get_cmd(hdr->cmd, family);
  437. if (ops == NULL)
  438. return -EOPNOTSUPP;
  439. if ((ops->flags & GENL_ADMIN_PERM) &&
  440. security_netlink_recv(skb, CAP_NET_ADMIN))
  441. return -EPERM;
  442. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  443. if (ops->dumpit == NULL)
  444. return -EOPNOTSUPP;
  445. genl_unlock();
  446. err = netlink_dump_start(net->genl_sock, skb, nlh,
  447. ops->dumpit, ops->done);
  448. genl_lock();
  449. return err;
  450. }
  451. if (ops->doit == NULL)
  452. return -EOPNOTSUPP;
  453. if (family->attrbuf) {
  454. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  455. ops->policy);
  456. if (err < 0)
  457. return err;
  458. }
  459. info.snd_seq = nlh->nlmsg_seq;
  460. info.snd_pid = NETLINK_CB(skb).pid;
  461. info.nlhdr = nlh;
  462. info.genlhdr = nlmsg_data(nlh);
  463. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  464. info.attrs = family->attrbuf;
  465. genl_info_net_set(&info, net);
  466. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  467. if (family->pre_doit) {
  468. err = family->pre_doit(ops, skb, &info);
  469. if (err)
  470. return err;
  471. }
  472. err = ops->doit(skb, &info);
  473. if (family->post_doit)
  474. family->post_doit(ops, skb, &info);
  475. return err;
  476. }
  477. static void genl_rcv(struct sk_buff *skb)
  478. {
  479. genl_lock();
  480. netlink_rcv_skb(skb, &genl_rcv_msg);
  481. genl_unlock();
  482. }
  483. /**************************************************************************
  484. * Controller
  485. **************************************************************************/
  486. static struct genl_family genl_ctrl = {
  487. .id = GENL_ID_CTRL,
  488. .name = "nlctrl",
  489. .version = 0x2,
  490. .maxattr = CTRL_ATTR_MAX,
  491. .netnsok = true,
  492. };
  493. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  494. u32 flags, struct sk_buff *skb, u8 cmd)
  495. {
  496. void *hdr;
  497. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  498. if (hdr == NULL)
  499. return -1;
  500. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  501. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  502. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  503. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  504. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  505. if (!list_empty(&family->ops_list)) {
  506. struct nlattr *nla_ops;
  507. struct genl_ops *ops;
  508. int idx = 1;
  509. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  510. if (nla_ops == NULL)
  511. goto nla_put_failure;
  512. list_for_each_entry(ops, &family->ops_list, ops_list) {
  513. struct nlattr *nest;
  514. nest = nla_nest_start(skb, idx++);
  515. if (nest == NULL)
  516. goto nla_put_failure;
  517. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  518. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  519. nla_nest_end(skb, nest);
  520. }
  521. nla_nest_end(skb, nla_ops);
  522. }
  523. if (!list_empty(&family->mcast_groups)) {
  524. struct genl_multicast_group *grp;
  525. struct nlattr *nla_grps;
  526. int idx = 1;
  527. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  528. if (nla_grps == NULL)
  529. goto nla_put_failure;
  530. list_for_each_entry(grp, &family->mcast_groups, list) {
  531. struct nlattr *nest;
  532. nest = nla_nest_start(skb, idx++);
  533. if (nest == NULL)
  534. goto nla_put_failure;
  535. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  536. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  537. grp->name);
  538. nla_nest_end(skb, nest);
  539. }
  540. nla_nest_end(skb, nla_grps);
  541. }
  542. return genlmsg_end(skb, hdr);
  543. nla_put_failure:
  544. genlmsg_cancel(skb, hdr);
  545. return -EMSGSIZE;
  546. }
  547. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  548. u32 seq, u32 flags, struct sk_buff *skb,
  549. u8 cmd)
  550. {
  551. void *hdr;
  552. struct nlattr *nla_grps;
  553. struct nlattr *nest;
  554. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  555. if (hdr == NULL)
  556. return -1;
  557. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  558. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  559. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  560. if (nla_grps == NULL)
  561. goto nla_put_failure;
  562. nest = nla_nest_start(skb, 1);
  563. if (nest == NULL)
  564. goto nla_put_failure;
  565. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  566. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  567. grp->name);
  568. nla_nest_end(skb, nest);
  569. nla_nest_end(skb, nla_grps);
  570. return genlmsg_end(skb, hdr);
  571. nla_put_failure:
  572. genlmsg_cancel(skb, hdr);
  573. return -EMSGSIZE;
  574. }
  575. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  576. {
  577. int i, n = 0;
  578. struct genl_family *rt;
  579. struct net *net = sock_net(skb->sk);
  580. int chains_to_skip = cb->args[0];
  581. int fams_to_skip = cb->args[1];
  582. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  583. n = 0;
  584. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  585. if (!rt->netnsok && !net_eq(net, &init_net))
  586. continue;
  587. if (++n < fams_to_skip)
  588. continue;
  589. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  590. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  591. skb, CTRL_CMD_NEWFAMILY) < 0)
  592. goto errout;
  593. }
  594. fams_to_skip = 0;
  595. }
  596. errout:
  597. cb->args[0] = i;
  598. cb->args[1] = n;
  599. return skb->len;
  600. }
  601. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  602. u32 pid, int seq, u8 cmd)
  603. {
  604. struct sk_buff *skb;
  605. int err;
  606. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  607. if (skb == NULL)
  608. return ERR_PTR(-ENOBUFS);
  609. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  610. if (err < 0) {
  611. nlmsg_free(skb);
  612. return ERR_PTR(err);
  613. }
  614. return skb;
  615. }
  616. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  617. u32 pid, int seq, u8 cmd)
  618. {
  619. struct sk_buff *skb;
  620. int err;
  621. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  622. if (skb == NULL)
  623. return ERR_PTR(-ENOBUFS);
  624. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  625. if (err < 0) {
  626. nlmsg_free(skb);
  627. return ERR_PTR(err);
  628. }
  629. return skb;
  630. }
  631. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  632. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  633. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  634. .len = GENL_NAMSIZ - 1 },
  635. };
  636. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  637. {
  638. struct sk_buff *msg;
  639. struct genl_family *res = NULL;
  640. int err = -EINVAL;
  641. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  642. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  643. res = genl_family_find_byid(id);
  644. err = -ENOENT;
  645. }
  646. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  647. char *name;
  648. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  649. res = genl_family_find_byname(name);
  650. err = -ENOENT;
  651. }
  652. if (res == NULL)
  653. return err;
  654. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  655. /* family doesn't exist here */
  656. return -ENOENT;
  657. }
  658. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  659. CTRL_CMD_NEWFAMILY);
  660. if (IS_ERR(msg))
  661. return PTR_ERR(msg);
  662. return genlmsg_reply(msg, info);
  663. }
  664. static int genl_ctrl_event(int event, void *data)
  665. {
  666. struct sk_buff *msg;
  667. struct genl_family *family;
  668. struct genl_multicast_group *grp;
  669. /* genl is still initialising */
  670. if (!init_net.genl_sock)
  671. return 0;
  672. switch (event) {
  673. case CTRL_CMD_NEWFAMILY:
  674. case CTRL_CMD_DELFAMILY:
  675. family = data;
  676. msg = ctrl_build_family_msg(family, 0, 0, event);
  677. break;
  678. case CTRL_CMD_NEWMCAST_GRP:
  679. case CTRL_CMD_DELMCAST_GRP:
  680. grp = data;
  681. family = grp->family;
  682. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  683. break;
  684. default:
  685. return -EINVAL;
  686. }
  687. if (IS_ERR(msg))
  688. return PTR_ERR(msg);
  689. if (!family->netnsok) {
  690. genlmsg_multicast_netns(&init_net, msg, 0,
  691. GENL_ID_CTRL, GFP_KERNEL);
  692. } else {
  693. rcu_read_lock();
  694. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  695. rcu_read_unlock();
  696. }
  697. return 0;
  698. }
  699. static struct genl_ops genl_ctrl_ops = {
  700. .cmd = CTRL_CMD_GETFAMILY,
  701. .doit = ctrl_getfamily,
  702. .dumpit = ctrl_dumpfamily,
  703. .policy = ctrl_policy,
  704. };
  705. static struct genl_multicast_group notify_grp = {
  706. .name = "notify",
  707. };
  708. static int __net_init genl_pernet_init(struct net *net)
  709. {
  710. /* we'll bump the group number right afterwards */
  711. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
  712. genl_rcv, &genl_mutex,
  713. THIS_MODULE);
  714. if (!net->genl_sock && net_eq(net, &init_net))
  715. panic("GENL: Cannot initialize generic netlink\n");
  716. if (!net->genl_sock)
  717. return -ENOMEM;
  718. return 0;
  719. }
  720. static void __net_exit genl_pernet_exit(struct net *net)
  721. {
  722. netlink_kernel_release(net->genl_sock);
  723. net->genl_sock = NULL;
  724. }
  725. static struct pernet_operations genl_pernet_ops = {
  726. .init = genl_pernet_init,
  727. .exit = genl_pernet_exit,
  728. };
  729. static int __init genl_init(void)
  730. {
  731. int i, err;
  732. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  733. INIT_LIST_HEAD(&family_ht[i]);
  734. err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
  735. if (err < 0)
  736. goto problem;
  737. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  738. err = register_pernet_subsys(&genl_pernet_ops);
  739. if (err)
  740. goto problem;
  741. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  742. if (err < 0)
  743. goto problem;
  744. return 0;
  745. problem:
  746. panic("GENL: Cannot register controller: %d\n", err);
  747. }
  748. subsys_initcall(genl_init);
  749. static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
  750. gfp_t flags)
  751. {
  752. struct sk_buff *tmp;
  753. struct net *net, *prev = NULL;
  754. int err;
  755. for_each_net_rcu(net) {
  756. if (prev) {
  757. tmp = skb_clone(skb, flags);
  758. if (!tmp) {
  759. err = -ENOMEM;
  760. goto error;
  761. }
  762. err = nlmsg_multicast(prev->genl_sock, tmp,
  763. pid, group, flags);
  764. if (err)
  765. goto error;
  766. }
  767. prev = net;
  768. }
  769. return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
  770. error:
  771. kfree_skb(skb);
  772. return err;
  773. }
  774. int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
  775. gfp_t flags)
  776. {
  777. return genlmsg_mcast(skb, pid, group, flags);
  778. }
  779. EXPORT_SYMBOL(genlmsg_multicast_allns);