genetlink.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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_add_ops(struct genl_family *family,
  244. const struct genl_ops *ops,
  245. unsigned int n_ops)
  246. {
  247. int i, j;
  248. for (i = 0; i < n_ops; i++) {
  249. if (ops[i].dumpit == NULL && ops[i].doit == NULL)
  250. return -EINVAL;
  251. for (j = i + 1; j < n_ops; j++)
  252. if (ops[i].cmd == ops[j].cmd)
  253. return -EINVAL;
  254. }
  255. /* family is not registered yet, so no locking needed */
  256. family->ops = ops;
  257. family->n_ops = n_ops;
  258. return 0;
  259. }
  260. /**
  261. * __genl_register_family - register a generic netlink family
  262. * @family: generic netlink family
  263. *
  264. * Registers the specified family after validating it first. Only one
  265. * family may be registered with the same family name or identifier.
  266. * The family id may equal GENL_ID_GENERATE causing an unique id to
  267. * be automatically generated and assigned.
  268. *
  269. * Return 0 on success or a negative error code.
  270. */
  271. int __genl_register_family(struct genl_family *family)
  272. {
  273. int err = -EINVAL;
  274. if (family->id && family->id < GENL_MIN_ID)
  275. goto errout;
  276. if (family->id > GENL_MAX_ID)
  277. goto errout;
  278. INIT_LIST_HEAD(&family->mcast_groups);
  279. genl_lock_all();
  280. if (genl_family_find_byname(family->name)) {
  281. err = -EEXIST;
  282. goto errout_locked;
  283. }
  284. if (family->id == GENL_ID_GENERATE) {
  285. u16 newid = genl_generate_id();
  286. if (!newid) {
  287. err = -ENOMEM;
  288. goto errout_locked;
  289. }
  290. family->id = newid;
  291. } else if (genl_family_find_byid(family->id)) {
  292. err = -EEXIST;
  293. goto errout_locked;
  294. }
  295. if (family->maxattr && !family->parallel_ops) {
  296. family->attrbuf = kmalloc((family->maxattr+1) *
  297. sizeof(struct nlattr *), GFP_KERNEL);
  298. if (family->attrbuf == NULL) {
  299. err = -ENOMEM;
  300. goto errout_locked;
  301. }
  302. } else
  303. family->attrbuf = NULL;
  304. list_add_tail(&family->family_list, genl_family_chain(family->id));
  305. genl_unlock_all();
  306. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  307. return 0;
  308. errout_locked:
  309. genl_unlock_all();
  310. errout:
  311. return err;
  312. }
  313. EXPORT_SYMBOL(__genl_register_family);
  314. /**
  315. * __genl_register_family_with_ops - register a generic netlink family
  316. * @family: generic netlink family
  317. * @ops: operations to be registered
  318. * @n_ops: number of elements to register
  319. *
  320. * Registers the specified family and operations from the specified table.
  321. * Only one family may be registered with the same family name or identifier.
  322. *
  323. * The family id may equal GENL_ID_GENERATE causing an unique id to
  324. * be automatically generated and assigned.
  325. *
  326. * Either a doit or dumpit callback must be specified for every registered
  327. * operation or the function will fail. Only one operation structure per
  328. * command identifier may be registered.
  329. *
  330. * See include/net/genetlink.h for more documenation on the operations
  331. * structure.
  332. *
  333. * Return 0 on success or a negative error code.
  334. */
  335. int __genl_register_family_with_ops(struct genl_family *family,
  336. const struct genl_ops *ops, size_t n_ops)
  337. {
  338. int err;
  339. err = genl_validate_add_ops(family, ops, n_ops);
  340. if (err)
  341. return err;
  342. return __genl_register_family(family);
  343. }
  344. EXPORT_SYMBOL(__genl_register_family_with_ops);
  345. /**
  346. * genl_unregister_family - unregister generic netlink family
  347. * @family: generic netlink family
  348. *
  349. * Unregisters the specified family.
  350. *
  351. * Returns 0 on success or a negative error code.
  352. */
  353. int genl_unregister_family(struct genl_family *family)
  354. {
  355. struct genl_family *rc;
  356. genl_lock_all();
  357. genl_unregister_mc_groups(family);
  358. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  359. if (family->id != rc->id || strcmp(rc->name, family->name))
  360. continue;
  361. list_del(&rc->family_list);
  362. family->n_ops = 0;
  363. genl_unlock_all();
  364. kfree(family->attrbuf);
  365. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  366. return 0;
  367. }
  368. genl_unlock_all();
  369. return -ENOENT;
  370. }
  371. EXPORT_SYMBOL(genl_unregister_family);
  372. /**
  373. * genlmsg_put - Add generic netlink header to netlink message
  374. * @skb: socket buffer holding the message
  375. * @portid: netlink portid the message is addressed to
  376. * @seq: sequence number (usually the one of the sender)
  377. * @family: generic netlink family
  378. * @flags: netlink message flags
  379. * @cmd: generic netlink command
  380. *
  381. * Returns pointer to user specific header
  382. */
  383. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  384. struct genl_family *family, int flags, u8 cmd)
  385. {
  386. struct nlmsghdr *nlh;
  387. struct genlmsghdr *hdr;
  388. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  389. family->hdrsize, flags);
  390. if (nlh == NULL)
  391. return NULL;
  392. hdr = nlmsg_data(nlh);
  393. hdr->cmd = cmd;
  394. hdr->version = family->version;
  395. hdr->reserved = 0;
  396. return (char *) hdr + GENL_HDRLEN;
  397. }
  398. EXPORT_SYMBOL(genlmsg_put);
  399. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  400. {
  401. /* our ops are always const - netlink API doesn't propagate that */
  402. const struct genl_ops *ops = cb->data;
  403. int rc;
  404. genl_lock();
  405. rc = ops->dumpit(skb, cb);
  406. genl_unlock();
  407. return rc;
  408. }
  409. static int genl_lock_done(struct netlink_callback *cb)
  410. {
  411. /* our ops are always const - netlink API doesn't propagate that */
  412. const struct genl_ops *ops = cb->data;
  413. int rc = 0;
  414. if (ops->done) {
  415. genl_lock();
  416. rc = ops->done(cb);
  417. genl_unlock();
  418. }
  419. return rc;
  420. }
  421. static int genl_family_rcv_msg(struct genl_family *family,
  422. struct sk_buff *skb,
  423. struct nlmsghdr *nlh)
  424. {
  425. const struct genl_ops *ops;
  426. struct net *net = sock_net(skb->sk);
  427. struct genl_info info;
  428. struct genlmsghdr *hdr = nlmsg_data(nlh);
  429. struct nlattr **attrbuf;
  430. int hdrlen, err;
  431. /* this family doesn't exist in this netns */
  432. if (!family->netnsok && !net_eq(net, &init_net))
  433. return -ENOENT;
  434. hdrlen = GENL_HDRLEN + family->hdrsize;
  435. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  436. return -EINVAL;
  437. ops = genl_get_cmd(hdr->cmd, family);
  438. if (ops == NULL)
  439. return -EOPNOTSUPP;
  440. if ((ops->flags & GENL_ADMIN_PERM) &&
  441. !capable(CAP_NET_ADMIN))
  442. return -EPERM;
  443. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
  444. int rc;
  445. if (ops->dumpit == NULL)
  446. return -EOPNOTSUPP;
  447. if (!family->parallel_ops) {
  448. struct netlink_dump_control c = {
  449. .module = family->module,
  450. /* we have const, but the netlink API doesn't */
  451. .data = (void *)ops,
  452. .dump = genl_lock_dumpit,
  453. .done = genl_lock_done,
  454. };
  455. genl_unlock();
  456. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  457. genl_lock();
  458. } else {
  459. struct netlink_dump_control c = {
  460. .module = family->module,
  461. .dump = ops->dumpit,
  462. .done = ops->done,
  463. };
  464. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  465. }
  466. return rc;
  467. }
  468. if (ops->doit == NULL)
  469. return -EOPNOTSUPP;
  470. if (family->maxattr && family->parallel_ops) {
  471. attrbuf = kmalloc((family->maxattr+1) *
  472. sizeof(struct nlattr *), GFP_KERNEL);
  473. if (attrbuf == NULL)
  474. return -ENOMEM;
  475. } else
  476. attrbuf = family->attrbuf;
  477. if (attrbuf) {
  478. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  479. ops->policy);
  480. if (err < 0)
  481. goto out;
  482. }
  483. info.snd_seq = nlh->nlmsg_seq;
  484. info.snd_portid = NETLINK_CB(skb).portid;
  485. info.nlhdr = nlh;
  486. info.genlhdr = nlmsg_data(nlh);
  487. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  488. info.attrs = attrbuf;
  489. genl_info_net_set(&info, net);
  490. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  491. if (family->pre_doit) {
  492. err = family->pre_doit(ops, skb, &info);
  493. if (err)
  494. goto out;
  495. }
  496. err = ops->doit(skb, &info);
  497. if (family->post_doit)
  498. family->post_doit(ops, skb, &info);
  499. out:
  500. if (family->parallel_ops)
  501. kfree(attrbuf);
  502. return err;
  503. }
  504. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  505. {
  506. struct genl_family *family;
  507. int err;
  508. family = genl_family_find_byid(nlh->nlmsg_type);
  509. if (family == NULL)
  510. return -ENOENT;
  511. if (!family->parallel_ops)
  512. genl_lock();
  513. err = genl_family_rcv_msg(family, skb, nlh);
  514. if (!family->parallel_ops)
  515. genl_unlock();
  516. return err;
  517. }
  518. static void genl_rcv(struct sk_buff *skb)
  519. {
  520. down_read(&cb_lock);
  521. netlink_rcv_skb(skb, &genl_rcv_msg);
  522. up_read(&cb_lock);
  523. }
  524. /**************************************************************************
  525. * Controller
  526. **************************************************************************/
  527. static struct genl_family genl_ctrl = {
  528. .id = GENL_ID_CTRL,
  529. .name = "nlctrl",
  530. .version = 0x2,
  531. .maxattr = CTRL_ATTR_MAX,
  532. .netnsok = true,
  533. };
  534. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  535. u32 flags, struct sk_buff *skb, u8 cmd)
  536. {
  537. void *hdr;
  538. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  539. if (hdr == NULL)
  540. return -1;
  541. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  542. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  543. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  544. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  545. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  546. goto nla_put_failure;
  547. if (family->n_ops) {
  548. struct nlattr *nla_ops;
  549. int i;
  550. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  551. if (nla_ops == NULL)
  552. goto nla_put_failure;
  553. for (i = 0; i < family->n_ops; i++) {
  554. struct nlattr *nest;
  555. const struct genl_ops *ops = &family->ops[i];
  556. u32 flags = ops->flags;
  557. if (ops->dumpit)
  558. flags |= GENL_CMD_CAP_DUMP;
  559. if (ops->doit)
  560. flags |= GENL_CMD_CAP_DO;
  561. if (ops->policy)
  562. flags |= GENL_CMD_CAP_HASPOL;
  563. nest = nla_nest_start(skb, i + 1);
  564. if (nest == NULL)
  565. goto nla_put_failure;
  566. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  567. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, flags))
  568. goto nla_put_failure;
  569. nla_nest_end(skb, nest);
  570. }
  571. nla_nest_end(skb, nla_ops);
  572. }
  573. if (!list_empty(&family->mcast_groups)) {
  574. struct genl_multicast_group *grp;
  575. struct nlattr *nla_grps;
  576. int idx = 1;
  577. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  578. if (nla_grps == NULL)
  579. goto nla_put_failure;
  580. list_for_each_entry(grp, &family->mcast_groups, list) {
  581. struct nlattr *nest;
  582. nest = nla_nest_start(skb, idx++);
  583. if (nest == NULL)
  584. goto nla_put_failure;
  585. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  586. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  587. grp->name))
  588. goto nla_put_failure;
  589. nla_nest_end(skb, nest);
  590. }
  591. nla_nest_end(skb, nla_grps);
  592. }
  593. return genlmsg_end(skb, hdr);
  594. nla_put_failure:
  595. genlmsg_cancel(skb, hdr);
  596. return -EMSGSIZE;
  597. }
  598. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
  599. u32 seq, u32 flags, struct sk_buff *skb,
  600. u8 cmd)
  601. {
  602. void *hdr;
  603. struct nlattr *nla_grps;
  604. struct nlattr *nest;
  605. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  606. if (hdr == NULL)
  607. return -1;
  608. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
  609. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
  610. goto nla_put_failure;
  611. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  612. if (nla_grps == NULL)
  613. goto nla_put_failure;
  614. nest = nla_nest_start(skb, 1);
  615. if (nest == NULL)
  616. goto nla_put_failure;
  617. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  618. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  619. grp->name))
  620. goto nla_put_failure;
  621. nla_nest_end(skb, nest);
  622. nla_nest_end(skb, nla_grps);
  623. return genlmsg_end(skb, hdr);
  624. nla_put_failure:
  625. genlmsg_cancel(skb, hdr);
  626. return -EMSGSIZE;
  627. }
  628. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  629. {
  630. int i, n = 0;
  631. struct genl_family *rt;
  632. struct net *net = sock_net(skb->sk);
  633. int chains_to_skip = cb->args[0];
  634. int fams_to_skip = cb->args[1];
  635. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  636. n = 0;
  637. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  638. if (!rt->netnsok && !net_eq(net, &init_net))
  639. continue;
  640. if (++n < fams_to_skip)
  641. continue;
  642. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  643. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  644. skb, CTRL_CMD_NEWFAMILY) < 0)
  645. goto errout;
  646. }
  647. fams_to_skip = 0;
  648. }
  649. errout:
  650. cb->args[0] = i;
  651. cb->args[1] = n;
  652. return skb->len;
  653. }
  654. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  655. u32 portid, int seq, u8 cmd)
  656. {
  657. struct sk_buff *skb;
  658. int err;
  659. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  660. if (skb == NULL)
  661. return ERR_PTR(-ENOBUFS);
  662. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  663. if (err < 0) {
  664. nlmsg_free(skb);
  665. return ERR_PTR(err);
  666. }
  667. return skb;
  668. }
  669. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  670. u32 portid, int seq, u8 cmd)
  671. {
  672. struct sk_buff *skb;
  673. int err;
  674. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  675. if (skb == NULL)
  676. return ERR_PTR(-ENOBUFS);
  677. err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
  678. if (err < 0) {
  679. nlmsg_free(skb);
  680. return ERR_PTR(err);
  681. }
  682. return skb;
  683. }
  684. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  685. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  686. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  687. .len = GENL_NAMSIZ - 1 },
  688. };
  689. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  690. {
  691. struct sk_buff *msg;
  692. struct genl_family *res = NULL;
  693. int err = -EINVAL;
  694. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  695. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  696. res = genl_family_find_byid(id);
  697. err = -ENOENT;
  698. }
  699. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  700. char *name;
  701. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  702. res = genl_family_find_byname(name);
  703. #ifdef CONFIG_MODULES
  704. if (res == NULL) {
  705. genl_unlock();
  706. up_read(&cb_lock);
  707. request_module("net-pf-%d-proto-%d-family-%s",
  708. PF_NETLINK, NETLINK_GENERIC, name);
  709. down_read(&cb_lock);
  710. genl_lock();
  711. res = genl_family_find_byname(name);
  712. }
  713. #endif
  714. err = -ENOENT;
  715. }
  716. if (res == NULL)
  717. return err;
  718. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  719. /* family doesn't exist here */
  720. return -ENOENT;
  721. }
  722. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  723. CTRL_CMD_NEWFAMILY);
  724. if (IS_ERR(msg))
  725. return PTR_ERR(msg);
  726. return genlmsg_reply(msg, info);
  727. }
  728. static int genl_ctrl_event(int event, void *data)
  729. {
  730. struct sk_buff *msg;
  731. struct genl_family *family;
  732. struct genl_multicast_group *grp;
  733. /* genl is still initialising */
  734. if (!init_net.genl_sock)
  735. return 0;
  736. switch (event) {
  737. case CTRL_CMD_NEWFAMILY:
  738. case CTRL_CMD_DELFAMILY:
  739. family = data;
  740. msg = ctrl_build_family_msg(family, 0, 0, event);
  741. break;
  742. case CTRL_CMD_NEWMCAST_GRP:
  743. case CTRL_CMD_DELMCAST_GRP:
  744. grp = data;
  745. family = grp->family;
  746. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  747. break;
  748. default:
  749. return -EINVAL;
  750. }
  751. if (IS_ERR(msg))
  752. return PTR_ERR(msg);
  753. if (!family->netnsok) {
  754. genlmsg_multicast_netns(&init_net, msg, 0,
  755. GENL_ID_CTRL, GFP_KERNEL);
  756. } else {
  757. rcu_read_lock();
  758. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  759. rcu_read_unlock();
  760. }
  761. return 0;
  762. }
  763. static struct genl_ops genl_ctrl_ops = {
  764. .cmd = CTRL_CMD_GETFAMILY,
  765. .doit = ctrl_getfamily,
  766. .dumpit = ctrl_dumpfamily,
  767. .policy = ctrl_policy,
  768. };
  769. static struct genl_multicast_group notify_grp = {
  770. .name = "notify",
  771. };
  772. static int __net_init genl_pernet_init(struct net *net)
  773. {
  774. struct netlink_kernel_cfg cfg = {
  775. .input = genl_rcv,
  776. .flags = NL_CFG_F_NONROOT_RECV,
  777. };
  778. /* we'll bump the group number right afterwards */
  779. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  780. if (!net->genl_sock && net_eq(net, &init_net))
  781. panic("GENL: Cannot initialize generic netlink\n");
  782. if (!net->genl_sock)
  783. return -ENOMEM;
  784. return 0;
  785. }
  786. static void __net_exit genl_pernet_exit(struct net *net)
  787. {
  788. netlink_kernel_release(net->genl_sock);
  789. net->genl_sock = NULL;
  790. }
  791. static struct pernet_operations genl_pernet_ops = {
  792. .init = genl_pernet_init,
  793. .exit = genl_pernet_exit,
  794. };
  795. static int __init genl_init(void)
  796. {
  797. int i, err;
  798. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  799. INIT_LIST_HEAD(&family_ht[i]);
  800. err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
  801. if (err < 0)
  802. goto problem;
  803. err = register_pernet_subsys(&genl_pernet_ops);
  804. if (err)
  805. goto problem;
  806. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  807. if (err < 0)
  808. goto problem;
  809. return 0;
  810. problem:
  811. panic("GENL: Cannot register controller: %d\n", err);
  812. }
  813. subsys_initcall(genl_init);
  814. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  815. gfp_t flags)
  816. {
  817. struct sk_buff *tmp;
  818. struct net *net, *prev = NULL;
  819. int err;
  820. for_each_net_rcu(net) {
  821. if (prev) {
  822. tmp = skb_clone(skb, flags);
  823. if (!tmp) {
  824. err = -ENOMEM;
  825. goto error;
  826. }
  827. err = nlmsg_multicast(prev->genl_sock, tmp,
  828. portid, group, flags);
  829. if (err)
  830. goto error;
  831. }
  832. prev = net;
  833. }
  834. return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  835. error:
  836. kfree_skb(skb);
  837. return err;
  838. }
  839. int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
  840. gfp_t flags)
  841. {
  842. return genlmsg_mcast(skb, portid, group, flags);
  843. }
  844. EXPORT_SYMBOL(genlmsg_multicast_allns);
  845. void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
  846. struct nlmsghdr *nlh, gfp_t flags)
  847. {
  848. struct sock *sk = net->genl_sock;
  849. int report = 0;
  850. if (nlh)
  851. report = nlmsg_report(nlh);
  852. nlmsg_notify(sk, skb, portid, group, report, flags);
  853. }
  854. EXPORT_SYMBOL(genl_notify);