genetlink.c 21 KB

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