genetlink.c 26 KB

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