genetlink.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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_free;
  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_free:
  354. kfree(family->attrbuf);
  355. errout_locked:
  356. genl_unlock_all();
  357. errout:
  358. return err;
  359. }
  360. EXPORT_SYMBOL(__genl_register_family);
  361. /**
  362. * genl_unregister_family - unregister generic netlink family
  363. * @family: generic netlink family
  364. *
  365. * Unregisters the specified family.
  366. *
  367. * Returns 0 on success or a negative error code.
  368. */
  369. int genl_unregister_family(struct genl_family *family)
  370. {
  371. struct genl_family *rc;
  372. genl_lock_all();
  373. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  374. if (family->id != rc->id || strcmp(rc->name, family->name))
  375. continue;
  376. genl_unregister_mc_groups(family);
  377. list_del(&rc->family_list);
  378. family->n_ops = 0;
  379. up_write(&cb_lock);
  380. wait_event(genl_sk_destructing_waitq,
  381. atomic_read(&genl_sk_destructing_cnt) == 0);
  382. genl_unlock();
  383. kfree(family->attrbuf);
  384. genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
  385. return 0;
  386. }
  387. genl_unlock_all();
  388. return -ENOENT;
  389. }
  390. EXPORT_SYMBOL(genl_unregister_family);
  391. /**
  392. * genlmsg_put - Add generic netlink header to netlink message
  393. * @skb: socket buffer holding the message
  394. * @portid: netlink portid the message is addressed to
  395. * @seq: sequence number (usually the one of the sender)
  396. * @family: generic netlink family
  397. * @flags: netlink message flags
  398. * @cmd: generic netlink command
  399. *
  400. * Returns pointer to user specific header
  401. */
  402. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  403. struct genl_family *family, int flags, u8 cmd)
  404. {
  405. struct nlmsghdr *nlh;
  406. struct genlmsghdr *hdr;
  407. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  408. family->hdrsize, flags);
  409. if (nlh == NULL)
  410. return NULL;
  411. hdr = nlmsg_data(nlh);
  412. hdr->cmd = cmd;
  413. hdr->version = family->version;
  414. hdr->reserved = 0;
  415. return (char *) hdr + GENL_HDRLEN;
  416. }
  417. EXPORT_SYMBOL(genlmsg_put);
  418. static int genl_lock_start(struct netlink_callback *cb)
  419. {
  420. /* our ops are always const - netlink API doesn't propagate that */
  421. const struct genl_ops *ops = cb->data;
  422. int rc = 0;
  423. if (ops->start) {
  424. genl_lock();
  425. rc = ops->start(cb);
  426. genl_unlock();
  427. }
  428. return rc;
  429. }
  430. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  431. {
  432. /* our ops are always const - netlink API doesn't propagate that */
  433. const struct genl_ops *ops = cb->data;
  434. int rc;
  435. genl_lock();
  436. rc = ops->dumpit(skb, cb);
  437. genl_unlock();
  438. return rc;
  439. }
  440. static int genl_lock_done(struct netlink_callback *cb)
  441. {
  442. /* our ops are always const - netlink API doesn't propagate that */
  443. const struct genl_ops *ops = cb->data;
  444. int rc = 0;
  445. if (ops->done) {
  446. genl_lock();
  447. rc = ops->done(cb);
  448. genl_unlock();
  449. }
  450. return rc;
  451. }
  452. static int genl_family_rcv_msg(struct genl_family *family,
  453. struct sk_buff *skb,
  454. struct nlmsghdr *nlh)
  455. {
  456. const struct genl_ops *ops;
  457. struct net *net = sock_net(skb->sk);
  458. struct genl_info info;
  459. struct genlmsghdr *hdr = nlmsg_data(nlh);
  460. struct nlattr **attrbuf;
  461. int hdrlen, err;
  462. /* this family doesn't exist in this netns */
  463. if (!family->netnsok && !net_eq(net, &init_net))
  464. return -ENOENT;
  465. hdrlen = GENL_HDRLEN + family->hdrsize;
  466. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  467. return -EINVAL;
  468. ops = genl_get_cmd(hdr->cmd, family);
  469. if (ops == NULL)
  470. return -EOPNOTSUPP;
  471. if ((ops->flags & GENL_ADMIN_PERM) &&
  472. !netlink_capable(skb, CAP_NET_ADMIN))
  473. return -EPERM;
  474. if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
  475. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  476. return -EPERM;
  477. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
  478. int rc;
  479. if (ops->dumpit == NULL)
  480. return -EOPNOTSUPP;
  481. if (!family->parallel_ops) {
  482. struct netlink_dump_control c = {
  483. .module = family->module,
  484. /* we have const, but the netlink API doesn't */
  485. .data = (void *)ops,
  486. .start = genl_lock_start,
  487. .dump = genl_lock_dumpit,
  488. .done = genl_lock_done,
  489. };
  490. genl_unlock();
  491. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  492. genl_lock();
  493. } else {
  494. struct netlink_dump_control c = {
  495. .module = family->module,
  496. .start = ops->start,
  497. .dump = ops->dumpit,
  498. .done = ops->done,
  499. };
  500. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  501. }
  502. return rc;
  503. }
  504. if (ops->doit == NULL)
  505. return -EOPNOTSUPP;
  506. if (family->maxattr && family->parallel_ops) {
  507. attrbuf = kmalloc((family->maxattr+1) *
  508. sizeof(struct nlattr *), GFP_KERNEL);
  509. if (attrbuf == NULL)
  510. return -ENOMEM;
  511. } else
  512. attrbuf = family->attrbuf;
  513. if (attrbuf) {
  514. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  515. ops->policy);
  516. if (err < 0)
  517. goto out;
  518. }
  519. info.snd_seq = nlh->nlmsg_seq;
  520. info.snd_portid = NETLINK_CB(skb).portid;
  521. info.nlhdr = nlh;
  522. info.genlhdr = nlmsg_data(nlh);
  523. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  524. info.attrs = attrbuf;
  525. genl_info_net_set(&info, net);
  526. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  527. if (family->pre_doit) {
  528. err = family->pre_doit(ops, skb, &info);
  529. if (err)
  530. goto out;
  531. }
  532. err = ops->doit(skb, &info);
  533. if (family->post_doit)
  534. family->post_doit(ops, skb, &info);
  535. out:
  536. if (family->parallel_ops)
  537. kfree(attrbuf);
  538. return err;
  539. }
  540. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  541. {
  542. struct genl_family *family;
  543. int err;
  544. family = genl_family_find_byid(nlh->nlmsg_type);
  545. if (family == NULL)
  546. return -ENOENT;
  547. if (!family->parallel_ops)
  548. genl_lock();
  549. err = genl_family_rcv_msg(family, skb, nlh);
  550. if (!family->parallel_ops)
  551. genl_unlock();
  552. return err;
  553. }
  554. static void genl_rcv(struct sk_buff *skb)
  555. {
  556. down_read(&cb_lock);
  557. netlink_rcv_skb(skb, &genl_rcv_msg);
  558. up_read(&cb_lock);
  559. }
  560. /**************************************************************************
  561. * Controller
  562. **************************************************************************/
  563. static struct genl_family genl_ctrl = {
  564. .id = GENL_ID_CTRL,
  565. .name = "nlctrl",
  566. .version = 0x2,
  567. .maxattr = CTRL_ATTR_MAX,
  568. .netnsok = true,
  569. };
  570. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  571. u32 flags, struct sk_buff *skb, u8 cmd)
  572. {
  573. void *hdr;
  574. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  575. if (hdr == NULL)
  576. return -1;
  577. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  578. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  579. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  580. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  581. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  582. goto nla_put_failure;
  583. if (family->n_ops) {
  584. struct nlattr *nla_ops;
  585. int i;
  586. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  587. if (nla_ops == NULL)
  588. goto nla_put_failure;
  589. for (i = 0; i < family->n_ops; i++) {
  590. struct nlattr *nest;
  591. const struct genl_ops *ops = &family->ops[i];
  592. u32 op_flags = ops->flags;
  593. if (ops->dumpit)
  594. op_flags |= GENL_CMD_CAP_DUMP;
  595. if (ops->doit)
  596. op_flags |= GENL_CMD_CAP_DO;
  597. if (ops->policy)
  598. op_flags |= GENL_CMD_CAP_HASPOL;
  599. nest = nla_nest_start(skb, i + 1);
  600. if (nest == NULL)
  601. goto nla_put_failure;
  602. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  603. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  604. goto nla_put_failure;
  605. nla_nest_end(skb, nest);
  606. }
  607. nla_nest_end(skb, nla_ops);
  608. }
  609. if (family->n_mcgrps) {
  610. struct nlattr *nla_grps;
  611. int i;
  612. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  613. if (nla_grps == NULL)
  614. goto nla_put_failure;
  615. for (i = 0; i < family->n_mcgrps; i++) {
  616. struct nlattr *nest;
  617. const struct genl_multicast_group *grp;
  618. grp = &family->mcgrps[i];
  619. nest = nla_nest_start(skb, i + 1);
  620. if (nest == NULL)
  621. goto nla_put_failure;
  622. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  623. family->mcgrp_offset + i) ||
  624. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  625. grp->name))
  626. goto nla_put_failure;
  627. nla_nest_end(skb, nest);
  628. }
  629. nla_nest_end(skb, nla_grps);
  630. }
  631. genlmsg_end(skb, hdr);
  632. return 0;
  633. nla_put_failure:
  634. genlmsg_cancel(skb, hdr);
  635. return -EMSGSIZE;
  636. }
  637. static int ctrl_fill_mcgrp_info(struct genl_family *family,
  638. const struct genl_multicast_group *grp,
  639. int grp_id, u32 portid, u32 seq, u32 flags,
  640. struct sk_buff *skb, u8 cmd)
  641. {
  642. void *hdr;
  643. struct nlattr *nla_grps;
  644. struct nlattr *nest;
  645. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  646. if (hdr == NULL)
  647. return -1;
  648. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  649. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  650. goto nla_put_failure;
  651. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  652. if (nla_grps == NULL)
  653. goto nla_put_failure;
  654. nest = nla_nest_start(skb, 1);
  655. if (nest == NULL)
  656. goto nla_put_failure;
  657. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  658. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  659. grp->name))
  660. goto nla_put_failure;
  661. nla_nest_end(skb, nest);
  662. nla_nest_end(skb, nla_grps);
  663. genlmsg_end(skb, hdr);
  664. return 0;
  665. nla_put_failure:
  666. genlmsg_cancel(skb, hdr);
  667. return -EMSGSIZE;
  668. }
  669. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  670. {
  671. int i, n = 0;
  672. struct genl_family *rt;
  673. struct net *net = sock_net(skb->sk);
  674. int chains_to_skip = cb->args[0];
  675. int fams_to_skip = cb->args[1];
  676. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  677. n = 0;
  678. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  679. if (!rt->netnsok && !net_eq(net, &init_net))
  680. continue;
  681. if (++n < fams_to_skip)
  682. continue;
  683. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  684. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  685. skb, CTRL_CMD_NEWFAMILY) < 0)
  686. goto errout;
  687. }
  688. fams_to_skip = 0;
  689. }
  690. errout:
  691. cb->args[0] = i;
  692. cb->args[1] = n;
  693. return skb->len;
  694. }
  695. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  696. u32 portid, int seq, u8 cmd)
  697. {
  698. struct sk_buff *skb;
  699. int err;
  700. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  701. if (skb == NULL)
  702. return ERR_PTR(-ENOBUFS);
  703. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  704. if (err < 0) {
  705. nlmsg_free(skb);
  706. return ERR_PTR(err);
  707. }
  708. return skb;
  709. }
  710. static struct sk_buff *
  711. ctrl_build_mcgrp_msg(struct genl_family *family,
  712. const struct genl_multicast_group *grp,
  713. int grp_id, u32 portid, int seq, u8 cmd)
  714. {
  715. struct sk_buff *skb;
  716. int err;
  717. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  718. if (skb == NULL)
  719. return ERR_PTR(-ENOBUFS);
  720. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  721. seq, 0, skb, cmd);
  722. if (err < 0) {
  723. nlmsg_free(skb);
  724. return ERR_PTR(err);
  725. }
  726. return skb;
  727. }
  728. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  729. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  730. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  731. .len = GENL_NAMSIZ - 1 },
  732. };
  733. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  734. {
  735. struct sk_buff *msg;
  736. struct genl_family *res = NULL;
  737. int err = -EINVAL;
  738. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  739. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  740. res = genl_family_find_byid(id);
  741. err = -ENOENT;
  742. }
  743. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  744. char *name;
  745. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  746. res = genl_family_find_byname(name);
  747. #ifdef CONFIG_MODULES
  748. if (res == NULL) {
  749. genl_unlock();
  750. up_read(&cb_lock);
  751. request_module("net-pf-%d-proto-%d-family-%s",
  752. PF_NETLINK, NETLINK_GENERIC, name);
  753. down_read(&cb_lock);
  754. genl_lock();
  755. res = genl_family_find_byname(name);
  756. }
  757. #endif
  758. err = -ENOENT;
  759. }
  760. if (res == NULL)
  761. return err;
  762. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  763. /* family doesn't exist here */
  764. return -ENOENT;
  765. }
  766. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  767. CTRL_CMD_NEWFAMILY);
  768. if (IS_ERR(msg))
  769. return PTR_ERR(msg);
  770. return genlmsg_reply(msg, info);
  771. }
  772. static int genl_ctrl_event(int event, struct genl_family *family,
  773. const struct genl_multicast_group *grp,
  774. int grp_id)
  775. {
  776. struct sk_buff *msg;
  777. /* genl is still initialising */
  778. if (!init_net.genl_sock)
  779. return 0;
  780. switch (event) {
  781. case CTRL_CMD_NEWFAMILY:
  782. case CTRL_CMD_DELFAMILY:
  783. WARN_ON(grp);
  784. msg = ctrl_build_family_msg(family, 0, 0, event);
  785. break;
  786. case CTRL_CMD_NEWMCAST_GRP:
  787. case CTRL_CMD_DELMCAST_GRP:
  788. BUG_ON(!grp);
  789. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  790. break;
  791. default:
  792. return -EINVAL;
  793. }
  794. if (IS_ERR(msg))
  795. return PTR_ERR(msg);
  796. if (!family->netnsok) {
  797. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  798. 0, GFP_KERNEL);
  799. } else {
  800. rcu_read_lock();
  801. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  802. 0, GFP_ATOMIC);
  803. rcu_read_unlock();
  804. }
  805. return 0;
  806. }
  807. static const struct genl_ops genl_ctrl_ops[] = {
  808. {
  809. .cmd = CTRL_CMD_GETFAMILY,
  810. .doit = ctrl_getfamily,
  811. .dumpit = ctrl_dumpfamily,
  812. .policy = ctrl_policy,
  813. },
  814. };
  815. static const struct genl_multicast_group genl_ctrl_groups[] = {
  816. { .name = "notify", },
  817. };
  818. static int genl_bind(struct net *net, int group)
  819. {
  820. int i, err = -ENOENT;
  821. down_read(&cb_lock);
  822. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  823. struct genl_family *f;
  824. list_for_each_entry(f, genl_family_chain(i), family_list) {
  825. if (group >= f->mcgrp_offset &&
  826. group < f->mcgrp_offset + f->n_mcgrps) {
  827. int fam_grp = group - f->mcgrp_offset;
  828. if (!f->netnsok && net != &init_net)
  829. err = -ENOENT;
  830. else if (f->mcast_bind)
  831. err = f->mcast_bind(net, fam_grp);
  832. else
  833. err = 0;
  834. break;
  835. }
  836. }
  837. }
  838. up_read(&cb_lock);
  839. return err;
  840. }
  841. static void genl_unbind(struct net *net, int group)
  842. {
  843. int i;
  844. down_read(&cb_lock);
  845. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  846. struct genl_family *f;
  847. list_for_each_entry(f, genl_family_chain(i), family_list) {
  848. if (group >= f->mcgrp_offset &&
  849. group < f->mcgrp_offset + f->n_mcgrps) {
  850. int fam_grp = group - f->mcgrp_offset;
  851. if (f->mcast_unbind)
  852. f->mcast_unbind(net, fam_grp);
  853. break;
  854. }
  855. }
  856. }
  857. up_read(&cb_lock);
  858. }
  859. static int __net_init genl_pernet_init(struct net *net)
  860. {
  861. struct netlink_kernel_cfg cfg = {
  862. .input = genl_rcv,
  863. .flags = NL_CFG_F_NONROOT_RECV,
  864. .bind = genl_bind,
  865. .unbind = genl_unbind,
  866. };
  867. /* we'll bump the group number right afterwards */
  868. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  869. if (!net->genl_sock && net_eq(net, &init_net))
  870. panic("GENL: Cannot initialize generic netlink\n");
  871. if (!net->genl_sock)
  872. return -ENOMEM;
  873. return 0;
  874. }
  875. static void __net_exit genl_pernet_exit(struct net *net)
  876. {
  877. netlink_kernel_release(net->genl_sock);
  878. net->genl_sock = NULL;
  879. }
  880. static struct pernet_operations genl_pernet_ops = {
  881. .init = genl_pernet_init,
  882. .exit = genl_pernet_exit,
  883. };
  884. static int __init genl_init(void)
  885. {
  886. int i, err;
  887. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  888. INIT_LIST_HEAD(&family_ht[i]);
  889. err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops,
  890. genl_ctrl_groups);
  891. if (err < 0)
  892. goto problem;
  893. err = register_pernet_subsys(&genl_pernet_ops);
  894. if (err)
  895. goto problem;
  896. return 0;
  897. problem:
  898. panic("GENL: Cannot register controller: %d\n", err);
  899. }
  900. subsys_initcall(genl_init);
  901. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  902. gfp_t flags)
  903. {
  904. struct sk_buff *tmp;
  905. struct net *net, *prev = NULL;
  906. bool delivered = false;
  907. int err;
  908. for_each_net_rcu(net) {
  909. if (prev) {
  910. tmp = skb_clone(skb, flags);
  911. if (!tmp) {
  912. err = -ENOMEM;
  913. goto error;
  914. }
  915. err = nlmsg_multicast(prev->genl_sock, tmp,
  916. portid, group, flags);
  917. if (!err)
  918. delivered = true;
  919. else if (err != -ESRCH)
  920. goto error;
  921. }
  922. prev = net;
  923. }
  924. err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  925. if (!err)
  926. delivered = true;
  927. else if (err != -ESRCH)
  928. return err;
  929. return delivered ? 0 : -ESRCH;
  930. error:
  931. kfree_skb(skb);
  932. return err;
  933. }
  934. int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
  935. u32 portid, unsigned int group, gfp_t flags)
  936. {
  937. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  938. return -EINVAL;
  939. group = family->mcgrp_offset + group;
  940. return genlmsg_mcast(skb, portid, group, flags);
  941. }
  942. EXPORT_SYMBOL(genlmsg_multicast_allns);
  943. void genl_notify(struct genl_family *family, struct sk_buff *skb,
  944. struct genl_info *info, u32 group, gfp_t flags)
  945. {
  946. struct net *net = genl_info_net(info);
  947. struct sock *sk = net->genl_sock;
  948. int report = 0;
  949. if (info->nlhdr)
  950. report = nlmsg_report(info->nlhdr);
  951. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  952. return;
  953. group = family->mcgrp_offset + group;
  954. nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
  955. }
  956. EXPORT_SYMBOL(genl_notify);