genetlink.c 21 KB

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