genetlink.c 23 KB

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