genetlink.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. /**
  262. * genl_unregister_ops - unregister generic netlink operations
  263. * @family: generic netlink family
  264. * @ops: operations to be unregistered
  265. *
  266. * Unregisters the specified operations and unassigns them from the
  267. * specified family. The operation blocks until the current message
  268. * processing has finished and doesn't start again until the
  269. * unregister process has finished.
  270. *
  271. * Note: It is not necessary to unregister all operations before
  272. * unregistering the family, unregistering the family will cause
  273. * all assigned operations to be unregistered automatically.
  274. *
  275. * Returns 0 on success or a negative error code.
  276. */
  277. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  278. {
  279. struct genl_ops *rc;
  280. genl_lock();
  281. list_for_each_entry(rc, &family->ops_list, ops_list) {
  282. if (rc == ops) {
  283. list_del(&ops->ops_list);
  284. genl_unlock();
  285. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  286. return 0;
  287. }
  288. }
  289. genl_unlock();
  290. return -ENOENT;
  291. }
  292. /**
  293. * genl_register_family - register a generic netlink family
  294. * @family: generic netlink family
  295. *
  296. * Registers the specified family after validating it first. Only one
  297. * family may be registered with the same family name or identifier.
  298. * The family id may equal GENL_ID_GENERATE causing an unique id to
  299. * be automatically generated and assigned.
  300. *
  301. * Return 0 on success or a negative error code.
  302. */
  303. int genl_register_family(struct genl_family *family)
  304. {
  305. int err = -EINVAL;
  306. if (family->id && family->id < GENL_MIN_ID)
  307. goto errout;
  308. if (family->id > GENL_MAX_ID)
  309. goto errout;
  310. INIT_LIST_HEAD(&family->ops_list);
  311. INIT_LIST_HEAD(&family->mcast_groups);
  312. genl_lock();
  313. if (genl_family_find_byname(family->name)) {
  314. err = -EEXIST;
  315. goto errout_locked;
  316. }
  317. if (family->id == GENL_ID_GENERATE) {
  318. u16 newid = genl_generate_id();
  319. if (!newid) {
  320. err = -ENOMEM;
  321. goto errout_locked;
  322. }
  323. family->id = newid;
  324. } else if (genl_family_find_byid(family->id)) {
  325. err = -EEXIST;
  326. goto errout_locked;
  327. }
  328. if (family->maxattr) {
  329. family->attrbuf = kmalloc((family->maxattr+1) *
  330. sizeof(struct nlattr *), GFP_KERNEL);
  331. if (family->attrbuf == NULL) {
  332. err = -ENOMEM;
  333. goto errout_locked;
  334. }
  335. } else
  336. family->attrbuf = NULL;
  337. list_add_tail(&family->family_list, genl_family_chain(family->id));
  338. genl_unlock();
  339. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  340. return 0;
  341. errout_locked:
  342. genl_unlock();
  343. errout:
  344. return err;
  345. }
  346. /**
  347. * genl_register_family_with_ops - register a generic netlink family
  348. * @family: generic netlink family
  349. * @ops: operations to be registered
  350. * @n_ops: number of elements to register
  351. *
  352. * Registers the specified family and operations from the specified table.
  353. * Only one family may be registered with the same family name or identifier.
  354. *
  355. * The family id may equal GENL_ID_GENERATE causing an unique id to
  356. * be automatically generated and assigned.
  357. *
  358. * Either a doit or dumpit callback must be specified for every registered
  359. * operation or the function will fail. Only one operation structure per
  360. * command identifier may be registered.
  361. *
  362. * See include/net/genetlink.h for more documenation on the operations
  363. * structure.
  364. *
  365. * This is equivalent to calling genl_register_family() followed by
  366. * genl_register_ops() for every operation entry in the table taking
  367. * care to unregister the family on error path.
  368. *
  369. * Return 0 on success or a negative error code.
  370. */
  371. int genl_register_family_with_ops(struct genl_family *family,
  372. struct genl_ops *ops, size_t n_ops)
  373. {
  374. int err, i;
  375. err = genl_register_family(family);
  376. if (err)
  377. return err;
  378. for (i = 0; i < n_ops; ++i, ++ops) {
  379. err = genl_register_ops(family, ops);
  380. if (err)
  381. goto err_out;
  382. }
  383. return 0;
  384. err_out:
  385. genl_unregister_family(family);
  386. return err;
  387. }
  388. EXPORT_SYMBOL(genl_register_family_with_ops);
  389. /**
  390. * genl_unregister_family - unregister generic netlink family
  391. * @family: generic netlink family
  392. *
  393. * Unregisters the specified family.
  394. *
  395. * Returns 0 on success or a negative error code.
  396. */
  397. int genl_unregister_family(struct genl_family *family)
  398. {
  399. struct genl_family *rc;
  400. genl_lock();
  401. genl_unregister_mc_groups(family);
  402. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  403. if (family->id != rc->id || strcmp(rc->name, family->name))
  404. continue;
  405. list_del(&rc->family_list);
  406. INIT_LIST_HEAD(&family->ops_list);
  407. genl_unlock();
  408. kfree(family->attrbuf);
  409. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  410. return 0;
  411. }
  412. genl_unlock();
  413. return -ENOENT;
  414. }
  415. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  416. {
  417. struct genl_ops *ops;
  418. struct genl_family *family;
  419. struct net *net = sock_net(skb->sk);
  420. struct genl_info info;
  421. struct genlmsghdr *hdr = nlmsg_data(nlh);
  422. int hdrlen, err;
  423. family = genl_family_find_byid(nlh->nlmsg_type);
  424. if (family == NULL)
  425. return -ENOENT;
  426. /* this family doesn't exist in this netns */
  427. if (!family->netnsok && !net_eq(net, &init_net))
  428. return -ENOENT;
  429. hdrlen = GENL_HDRLEN + family->hdrsize;
  430. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  431. return -EINVAL;
  432. ops = genl_get_cmd(hdr->cmd, family);
  433. if (ops == NULL)
  434. return -EOPNOTSUPP;
  435. if ((ops->flags & GENL_ADMIN_PERM) &&
  436. security_netlink_recv(skb, CAP_NET_ADMIN))
  437. return -EPERM;
  438. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  439. if (ops->dumpit == NULL)
  440. return -EOPNOTSUPP;
  441. genl_unlock();
  442. err = netlink_dump_start(net->genl_sock, skb, nlh,
  443. ops->dumpit, ops->done);
  444. genl_lock();
  445. return err;
  446. }
  447. if (ops->doit == NULL)
  448. return -EOPNOTSUPP;
  449. if (family->attrbuf) {
  450. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  451. ops->policy);
  452. if (err < 0)
  453. return err;
  454. }
  455. info.snd_seq = nlh->nlmsg_seq;
  456. info.snd_pid = NETLINK_CB(skb).pid;
  457. info.nlhdr = nlh;
  458. info.genlhdr = nlmsg_data(nlh);
  459. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  460. info.attrs = family->attrbuf;
  461. genl_info_net_set(&info, net);
  462. return ops->doit(skb, &info);
  463. }
  464. static void genl_rcv(struct sk_buff *skb)
  465. {
  466. genl_lock();
  467. netlink_rcv_skb(skb, &genl_rcv_msg);
  468. genl_unlock();
  469. }
  470. /**************************************************************************
  471. * Controller
  472. **************************************************************************/
  473. static struct genl_family genl_ctrl = {
  474. .id = GENL_ID_CTRL,
  475. .name = "nlctrl",
  476. .version = 0x2,
  477. .maxattr = CTRL_ATTR_MAX,
  478. .netnsok = true,
  479. };
  480. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  481. u32 flags, struct sk_buff *skb, u8 cmd)
  482. {
  483. void *hdr;
  484. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  485. if (hdr == NULL)
  486. return -1;
  487. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  488. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  489. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  490. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  491. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  492. if (!list_empty(&family->ops_list)) {
  493. struct nlattr *nla_ops;
  494. struct genl_ops *ops;
  495. int idx = 1;
  496. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  497. if (nla_ops == NULL)
  498. goto nla_put_failure;
  499. list_for_each_entry(ops, &family->ops_list, ops_list) {
  500. struct nlattr *nest;
  501. nest = nla_nest_start(skb, idx++);
  502. if (nest == NULL)
  503. goto nla_put_failure;
  504. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  505. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  506. nla_nest_end(skb, nest);
  507. }
  508. nla_nest_end(skb, nla_ops);
  509. }
  510. if (!list_empty(&family->mcast_groups)) {
  511. struct genl_multicast_group *grp;
  512. struct nlattr *nla_grps;
  513. int idx = 1;
  514. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  515. if (nla_grps == NULL)
  516. goto nla_put_failure;
  517. list_for_each_entry(grp, &family->mcast_groups, list) {
  518. struct nlattr *nest;
  519. nest = nla_nest_start(skb, idx++);
  520. if (nest == NULL)
  521. goto nla_put_failure;
  522. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  523. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  524. grp->name);
  525. nla_nest_end(skb, nest);
  526. }
  527. nla_nest_end(skb, nla_grps);
  528. }
  529. return genlmsg_end(skb, hdr);
  530. nla_put_failure:
  531. genlmsg_cancel(skb, hdr);
  532. return -EMSGSIZE;
  533. }
  534. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  535. u32 seq, u32 flags, struct sk_buff *skb,
  536. u8 cmd)
  537. {
  538. void *hdr;
  539. struct nlattr *nla_grps;
  540. struct nlattr *nest;
  541. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  542. if (hdr == NULL)
  543. return -1;
  544. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  545. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  546. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  547. if (nla_grps == NULL)
  548. goto nla_put_failure;
  549. nest = nla_nest_start(skb, 1);
  550. if (nest == NULL)
  551. goto nla_put_failure;
  552. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  553. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  554. grp->name);
  555. nla_nest_end(skb, nest);
  556. nla_nest_end(skb, nla_grps);
  557. return genlmsg_end(skb, hdr);
  558. nla_put_failure:
  559. genlmsg_cancel(skb, hdr);
  560. return -EMSGSIZE;
  561. }
  562. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  563. {
  564. int i, n = 0;
  565. struct genl_family *rt;
  566. struct net *net = sock_net(skb->sk);
  567. int chains_to_skip = cb->args[0];
  568. int fams_to_skip = cb->args[1];
  569. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  570. n = 0;
  571. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  572. if (!rt->netnsok && !net_eq(net, &init_net))
  573. continue;
  574. if (++n < fams_to_skip)
  575. continue;
  576. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  577. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  578. skb, CTRL_CMD_NEWFAMILY) < 0)
  579. goto errout;
  580. }
  581. fams_to_skip = 0;
  582. }
  583. errout:
  584. cb->args[0] = i;
  585. cb->args[1] = n;
  586. return skb->len;
  587. }
  588. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  589. u32 pid, int seq, u8 cmd)
  590. {
  591. struct sk_buff *skb;
  592. int err;
  593. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  594. if (skb == NULL)
  595. return ERR_PTR(-ENOBUFS);
  596. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  597. if (err < 0) {
  598. nlmsg_free(skb);
  599. return ERR_PTR(err);
  600. }
  601. return skb;
  602. }
  603. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  604. u32 pid, int seq, u8 cmd)
  605. {
  606. struct sk_buff *skb;
  607. int err;
  608. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  609. if (skb == NULL)
  610. return ERR_PTR(-ENOBUFS);
  611. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  612. if (err < 0) {
  613. nlmsg_free(skb);
  614. return ERR_PTR(err);
  615. }
  616. return skb;
  617. }
  618. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  619. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  620. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  621. .len = GENL_NAMSIZ - 1 },
  622. };
  623. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  624. {
  625. struct sk_buff *msg;
  626. struct genl_family *res = NULL;
  627. int err = -EINVAL;
  628. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  629. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  630. res = genl_family_find_byid(id);
  631. err = -ENOENT;
  632. }
  633. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  634. char *name;
  635. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  636. res = genl_family_find_byname(name);
  637. err = -ENOENT;
  638. }
  639. if (res == NULL)
  640. return err;
  641. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  642. /* family doesn't exist here */
  643. return -ENOENT;
  644. }
  645. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  646. CTRL_CMD_NEWFAMILY);
  647. if (IS_ERR(msg))
  648. return PTR_ERR(msg);
  649. return genlmsg_reply(msg, info);
  650. }
  651. static int genl_ctrl_event(int event, void *data)
  652. {
  653. struct sk_buff *msg;
  654. struct genl_family *family;
  655. struct genl_multicast_group *grp;
  656. /* genl is still initialising */
  657. if (!init_net.genl_sock)
  658. return 0;
  659. switch (event) {
  660. case CTRL_CMD_NEWFAMILY:
  661. case CTRL_CMD_DELFAMILY:
  662. family = data;
  663. msg = ctrl_build_family_msg(family, 0, 0, event);
  664. break;
  665. case CTRL_CMD_NEWMCAST_GRP:
  666. case CTRL_CMD_DELMCAST_GRP:
  667. grp = data;
  668. family = grp->family;
  669. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. if (IS_ERR(msg))
  675. return PTR_ERR(msg);
  676. if (!family->netnsok) {
  677. genlmsg_multicast_netns(&init_net, msg, 0,
  678. GENL_ID_CTRL, GFP_KERNEL);
  679. } else {
  680. rcu_read_lock();
  681. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  682. rcu_read_unlock();
  683. }
  684. return 0;
  685. }
  686. static struct genl_ops genl_ctrl_ops = {
  687. .cmd = CTRL_CMD_GETFAMILY,
  688. .doit = ctrl_getfamily,
  689. .dumpit = ctrl_dumpfamily,
  690. .policy = ctrl_policy,
  691. };
  692. static struct genl_multicast_group notify_grp = {
  693. .name = "notify",
  694. };
  695. static int __net_init genl_pernet_init(struct net *net)
  696. {
  697. /* we'll bump the group number right afterwards */
  698. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
  699. genl_rcv, &genl_mutex,
  700. THIS_MODULE);
  701. if (!net->genl_sock && net_eq(net, &init_net))
  702. panic("GENL: Cannot initialize generic netlink\n");
  703. if (!net->genl_sock)
  704. return -ENOMEM;
  705. return 0;
  706. }
  707. static void __net_exit genl_pernet_exit(struct net *net)
  708. {
  709. netlink_kernel_release(net->genl_sock);
  710. net->genl_sock = NULL;
  711. }
  712. static struct pernet_operations genl_pernet_ops = {
  713. .init = genl_pernet_init,
  714. .exit = genl_pernet_exit,
  715. };
  716. static int __init genl_init(void)
  717. {
  718. int i, err;
  719. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  720. INIT_LIST_HEAD(&family_ht[i]);
  721. err = genl_register_family(&genl_ctrl);
  722. if (err < 0)
  723. goto problem;
  724. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  725. if (err < 0)
  726. goto problem;
  727. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  728. err = register_pernet_subsys(&genl_pernet_ops);
  729. if (err)
  730. goto problem;
  731. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  732. if (err < 0)
  733. goto problem;
  734. return 0;
  735. problem:
  736. panic("GENL: Cannot register controller: %d\n", err);
  737. }
  738. subsys_initcall(genl_init);
  739. EXPORT_SYMBOL(genl_register_ops);
  740. EXPORT_SYMBOL(genl_unregister_ops);
  741. EXPORT_SYMBOL(genl_register_family);
  742. EXPORT_SYMBOL(genl_unregister_family);
  743. static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
  744. gfp_t flags)
  745. {
  746. struct sk_buff *tmp;
  747. struct net *net, *prev = NULL;
  748. int err;
  749. for_each_net_rcu(net) {
  750. if (prev) {
  751. tmp = skb_clone(skb, flags);
  752. if (!tmp) {
  753. err = -ENOMEM;
  754. goto error;
  755. }
  756. err = nlmsg_multicast(prev->genl_sock, tmp,
  757. pid, group, flags);
  758. if (err)
  759. goto error;
  760. }
  761. prev = net;
  762. }
  763. return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
  764. error:
  765. kfree_skb(skb);
  766. return err;
  767. }
  768. int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
  769. gfp_t flags)
  770. {
  771. return genlmsg_mcast(skb, pid, group, flags);
  772. }
  773. EXPORT_SYMBOL(genlmsg_multicast_allns);