genetlink.c 26 KB

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