genetlink.c 22 KB

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