genetlink.c 26 KB

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