genetlink.c 21 KB

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