genetlink.c 21 KB

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