genetlink.c 25 KB

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