genetlink.c 23 KB

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