genetlink.c 25 KB

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