genetlink.c 23 KB

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