genetlink.c 26 KB

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