genetlink.c 21 KB

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