genetlink.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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(const 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. return 0;
  290. }
  291. /**
  292. * __genl_register_family - register a generic netlink family
  293. * @family: generic netlink family
  294. *
  295. * Registers the specified family after validating it first. Only one
  296. * family may be registered with the same family name or identifier.
  297. * The family id may equal GENL_ID_GENERATE causing an unique id to
  298. * be automatically generated and assigned.
  299. *
  300. * The family's ops array must already be assigned, you can use the
  301. * genl_register_family_with_ops() helper function.
  302. *
  303. * Return 0 on success or a negative error code.
  304. */
  305. int __genl_register_family(struct genl_family *family)
  306. {
  307. int err = -EINVAL, i;
  308. if (family->id && family->id < GENL_MIN_ID)
  309. goto errout;
  310. if (family->id > GENL_MAX_ID)
  311. goto errout;
  312. err = genl_validate_ops(family);
  313. if (err)
  314. return err;
  315. genl_lock_all();
  316. if (genl_family_find_byname(family->name)) {
  317. err = -EEXIST;
  318. goto errout_locked;
  319. }
  320. if (family->id == GENL_ID_GENERATE) {
  321. u16 newid = genl_generate_id();
  322. if (!newid) {
  323. err = -ENOMEM;
  324. goto errout_locked;
  325. }
  326. family->id = newid;
  327. } else if (genl_family_find_byid(family->id)) {
  328. err = -EEXIST;
  329. goto errout_locked;
  330. }
  331. if (family->maxattr && !family->parallel_ops) {
  332. family->attrbuf = kmalloc((family->maxattr+1) *
  333. sizeof(struct nlattr *), GFP_KERNEL);
  334. if (family->attrbuf == NULL) {
  335. err = -ENOMEM;
  336. goto errout_locked;
  337. }
  338. } else
  339. family->attrbuf = NULL;
  340. err = genl_validate_assign_mc_groups(family);
  341. if (err)
  342. goto errout_locked;
  343. list_add_tail(&family->family_list, genl_family_chain(family->id));
  344. genl_unlock_all();
  345. /* send all events */
  346. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
  347. for (i = 0; i < family->n_mcgrps; i++)
  348. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
  349. &family->mcgrps[i], family->mcgrp_offset + i);
  350. return 0;
  351. errout_locked:
  352. genl_unlock_all();
  353. errout:
  354. return err;
  355. }
  356. EXPORT_SYMBOL(__genl_register_family);
  357. /**
  358. * genl_unregister_family - unregister generic netlink family
  359. * @family: generic netlink family
  360. *
  361. * Unregisters the specified family.
  362. *
  363. * Returns 0 on success or a negative error code.
  364. */
  365. int genl_unregister_family(struct genl_family *family)
  366. {
  367. struct genl_family *rc;
  368. genl_lock_all();
  369. genl_unregister_mc_groups(family);
  370. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  371. if (family->id != rc->id || strcmp(rc->name, family->name))
  372. continue;
  373. list_del(&rc->family_list);
  374. family->n_ops = 0;
  375. genl_unlock_all();
  376. kfree(family->attrbuf);
  377. genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
  378. return 0;
  379. }
  380. genl_unlock_all();
  381. return -ENOENT;
  382. }
  383. EXPORT_SYMBOL(genl_unregister_family);
  384. /**
  385. * genlmsg_new_unicast - Allocate generic netlink message for unicast
  386. * @payload: size of the message payload
  387. * @info: information on destination
  388. * @flags: the type of memory to allocate
  389. *
  390. * Allocates a new sk_buff large enough to cover the specified payload
  391. * plus required Netlink headers. Will check receiving socket for
  392. * memory mapped i/o capability and use it if enabled. Will fall back
  393. * to non-mapped skb if message size exceeds the frame size of the ring.
  394. */
  395. struct sk_buff *genlmsg_new_unicast(size_t payload, struct genl_info *info,
  396. gfp_t flags)
  397. {
  398. size_t len = nlmsg_total_size(genlmsg_total_size(payload));
  399. return netlink_alloc_skb(info->dst_sk, len, info->snd_portid, flags);
  400. }
  401. EXPORT_SYMBOL_GPL(genlmsg_new_unicast);
  402. /**
  403. * genlmsg_put - Add generic netlink header to netlink message
  404. * @skb: socket buffer holding the message
  405. * @portid: netlink portid the message is addressed to
  406. * @seq: sequence number (usually the one of the sender)
  407. * @family: generic netlink family
  408. * @flags: netlink message flags
  409. * @cmd: generic netlink command
  410. *
  411. * Returns pointer to user specific header
  412. */
  413. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  414. struct genl_family *family, int flags, u8 cmd)
  415. {
  416. struct nlmsghdr *nlh;
  417. struct genlmsghdr *hdr;
  418. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  419. family->hdrsize, flags);
  420. if (nlh == NULL)
  421. return NULL;
  422. hdr = nlmsg_data(nlh);
  423. hdr->cmd = cmd;
  424. hdr->version = family->version;
  425. hdr->reserved = 0;
  426. return (char *) hdr + GENL_HDRLEN;
  427. }
  428. EXPORT_SYMBOL(genlmsg_put);
  429. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  430. {
  431. /* our ops are always const - netlink API doesn't propagate that */
  432. const struct genl_ops *ops = cb->data;
  433. int rc;
  434. genl_lock();
  435. rc = ops->dumpit(skb, cb);
  436. genl_unlock();
  437. return rc;
  438. }
  439. static int genl_lock_done(struct netlink_callback *cb)
  440. {
  441. /* our ops are always const - netlink API doesn't propagate that */
  442. const struct genl_ops *ops = cb->data;
  443. int rc = 0;
  444. if (ops->done) {
  445. genl_lock();
  446. rc = ops->done(cb);
  447. genl_unlock();
  448. }
  449. return rc;
  450. }
  451. static int genl_family_rcv_msg(struct genl_family *family,
  452. struct sk_buff *skb,
  453. struct nlmsghdr *nlh)
  454. {
  455. const struct genl_ops *ops;
  456. struct net *net = sock_net(skb->sk);
  457. struct genl_info info;
  458. struct genlmsghdr *hdr = nlmsg_data(nlh);
  459. struct nlattr **attrbuf;
  460. int hdrlen, err;
  461. /* this family doesn't exist in this netns */
  462. if (!family->netnsok && !net_eq(net, &init_net))
  463. return -ENOENT;
  464. hdrlen = GENL_HDRLEN + family->hdrsize;
  465. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  466. return -EINVAL;
  467. ops = genl_get_cmd(hdr->cmd, family);
  468. if (ops == NULL)
  469. return -EOPNOTSUPP;
  470. if ((ops->flags & GENL_ADMIN_PERM) &&
  471. !netlink_capable(skb, CAP_NET_ADMIN))
  472. return -EPERM;
  473. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
  474. int rc;
  475. if (ops->dumpit == NULL)
  476. return -EOPNOTSUPP;
  477. if (!family->parallel_ops) {
  478. struct netlink_dump_control c = {
  479. .module = family->module,
  480. /* we have const, but the netlink API doesn't */
  481. .data = (void *)ops,
  482. .dump = genl_lock_dumpit,
  483. .done = genl_lock_done,
  484. };
  485. genl_unlock();
  486. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  487. genl_lock();
  488. } else {
  489. struct netlink_dump_control c = {
  490. .module = family->module,
  491. .dump = ops->dumpit,
  492. .done = ops->done,
  493. };
  494. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  495. }
  496. return rc;
  497. }
  498. if (ops->doit == NULL)
  499. return -EOPNOTSUPP;
  500. if (family->maxattr && family->parallel_ops) {
  501. attrbuf = kmalloc((family->maxattr+1) *
  502. sizeof(struct nlattr *), GFP_KERNEL);
  503. if (attrbuf == NULL)
  504. return -ENOMEM;
  505. } else
  506. attrbuf = family->attrbuf;
  507. if (attrbuf) {
  508. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  509. ops->policy);
  510. if (err < 0)
  511. goto out;
  512. }
  513. info.snd_seq = nlh->nlmsg_seq;
  514. info.snd_portid = NETLINK_CB(skb).portid;
  515. info.nlhdr = nlh;
  516. info.genlhdr = nlmsg_data(nlh);
  517. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  518. info.attrs = attrbuf;
  519. info.dst_sk = skb->sk;
  520. genl_info_net_set(&info, net);
  521. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  522. if (family->pre_doit) {
  523. err = family->pre_doit(ops, skb, &info);
  524. if (err)
  525. goto out;
  526. }
  527. err = ops->doit(skb, &info);
  528. if (family->post_doit)
  529. family->post_doit(ops, skb, &info);
  530. out:
  531. if (family->parallel_ops)
  532. kfree(attrbuf);
  533. return err;
  534. }
  535. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  536. {
  537. struct genl_family *family;
  538. int err;
  539. family = genl_family_find_byid(nlh->nlmsg_type);
  540. if (family == NULL)
  541. return -ENOENT;
  542. if (!family->parallel_ops)
  543. genl_lock();
  544. err = genl_family_rcv_msg(family, skb, nlh);
  545. if (!family->parallel_ops)
  546. genl_unlock();
  547. return err;
  548. }
  549. static void genl_rcv(struct sk_buff *skb)
  550. {
  551. down_read(&cb_lock);
  552. netlink_rcv_skb(skb, &genl_rcv_msg);
  553. up_read(&cb_lock);
  554. }
  555. /**************************************************************************
  556. * Controller
  557. **************************************************************************/
  558. static struct genl_family genl_ctrl = {
  559. .id = GENL_ID_CTRL,
  560. .name = "nlctrl",
  561. .version = 0x2,
  562. .maxattr = CTRL_ATTR_MAX,
  563. .netnsok = true,
  564. };
  565. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  566. u32 flags, struct sk_buff *skb, u8 cmd)
  567. {
  568. void *hdr;
  569. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  570. if (hdr == NULL)
  571. return -1;
  572. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  573. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  574. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  575. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  576. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  577. goto nla_put_failure;
  578. if (family->n_ops) {
  579. struct nlattr *nla_ops;
  580. int i;
  581. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  582. if (nla_ops == NULL)
  583. goto nla_put_failure;
  584. for (i = 0; i < family->n_ops; i++) {
  585. struct nlattr *nest;
  586. const struct genl_ops *ops = &family->ops[i];
  587. u32 op_flags = ops->flags;
  588. if (ops->dumpit)
  589. op_flags |= GENL_CMD_CAP_DUMP;
  590. if (ops->doit)
  591. op_flags |= GENL_CMD_CAP_DO;
  592. if (ops->policy)
  593. op_flags |= GENL_CMD_CAP_HASPOL;
  594. nest = nla_nest_start(skb, i + 1);
  595. if (nest == NULL)
  596. goto nla_put_failure;
  597. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  598. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  599. goto nla_put_failure;
  600. nla_nest_end(skb, nest);
  601. }
  602. nla_nest_end(skb, nla_ops);
  603. }
  604. if (family->n_mcgrps) {
  605. struct nlattr *nla_grps;
  606. int i;
  607. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  608. if (nla_grps == NULL)
  609. goto nla_put_failure;
  610. for (i = 0; i < family->n_mcgrps; i++) {
  611. struct nlattr *nest;
  612. const struct genl_multicast_group *grp;
  613. grp = &family->mcgrps[i];
  614. nest = nla_nest_start(skb, i + 1);
  615. if (nest == NULL)
  616. goto nla_put_failure;
  617. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  618. family->mcgrp_offset + i) ||
  619. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  620. grp->name))
  621. goto nla_put_failure;
  622. nla_nest_end(skb, nest);
  623. }
  624. nla_nest_end(skb, nla_grps);
  625. }
  626. return genlmsg_end(skb, hdr);
  627. nla_put_failure:
  628. genlmsg_cancel(skb, hdr);
  629. return -EMSGSIZE;
  630. }
  631. static int ctrl_fill_mcgrp_info(struct genl_family *family,
  632. const struct genl_multicast_group *grp,
  633. int grp_id, u32 portid, u32 seq, u32 flags,
  634. struct sk_buff *skb, u8 cmd)
  635. {
  636. void *hdr;
  637. struct nlattr *nla_grps;
  638. struct nlattr *nest;
  639. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  640. if (hdr == NULL)
  641. return -1;
  642. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  643. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  644. goto nla_put_failure;
  645. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  646. if (nla_grps == NULL)
  647. goto nla_put_failure;
  648. nest = nla_nest_start(skb, 1);
  649. if (nest == NULL)
  650. goto nla_put_failure;
  651. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  652. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  653. grp->name))
  654. goto nla_put_failure;
  655. nla_nest_end(skb, nest);
  656. nla_nest_end(skb, nla_grps);
  657. return genlmsg_end(skb, hdr);
  658. nla_put_failure:
  659. genlmsg_cancel(skb, hdr);
  660. return -EMSGSIZE;
  661. }
  662. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  663. {
  664. int i, n = 0;
  665. struct genl_family *rt;
  666. struct net *net = sock_net(skb->sk);
  667. int chains_to_skip = cb->args[0];
  668. int fams_to_skip = cb->args[1];
  669. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  670. n = 0;
  671. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  672. if (!rt->netnsok && !net_eq(net, &init_net))
  673. continue;
  674. if (++n < fams_to_skip)
  675. continue;
  676. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  677. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  678. skb, CTRL_CMD_NEWFAMILY) < 0)
  679. goto errout;
  680. }
  681. fams_to_skip = 0;
  682. }
  683. errout:
  684. cb->args[0] = i;
  685. cb->args[1] = n;
  686. return skb->len;
  687. }
  688. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  689. u32 portid, int seq, u8 cmd)
  690. {
  691. struct sk_buff *skb;
  692. int err;
  693. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  694. if (skb == NULL)
  695. return ERR_PTR(-ENOBUFS);
  696. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  697. if (err < 0) {
  698. nlmsg_free(skb);
  699. return ERR_PTR(err);
  700. }
  701. return skb;
  702. }
  703. static struct sk_buff *
  704. ctrl_build_mcgrp_msg(struct genl_family *family,
  705. const struct genl_multicast_group *grp,
  706. int grp_id, u32 portid, int seq, u8 cmd)
  707. {
  708. struct sk_buff *skb;
  709. int err;
  710. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  711. if (skb == NULL)
  712. return ERR_PTR(-ENOBUFS);
  713. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  714. seq, 0, skb, cmd);
  715. if (err < 0) {
  716. nlmsg_free(skb);
  717. return ERR_PTR(err);
  718. }
  719. return skb;
  720. }
  721. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  722. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  723. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  724. .len = GENL_NAMSIZ - 1 },
  725. };
  726. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  727. {
  728. struct sk_buff *msg;
  729. struct genl_family *res = NULL;
  730. int err = -EINVAL;
  731. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  732. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  733. res = genl_family_find_byid(id);
  734. err = -ENOENT;
  735. }
  736. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  737. char *name;
  738. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  739. res = genl_family_find_byname(name);
  740. #ifdef CONFIG_MODULES
  741. if (res == NULL) {
  742. genl_unlock();
  743. up_read(&cb_lock);
  744. request_module("net-pf-%d-proto-%d-family-%s",
  745. PF_NETLINK, NETLINK_GENERIC, name);
  746. down_read(&cb_lock);
  747. genl_lock();
  748. res = genl_family_find_byname(name);
  749. }
  750. #endif
  751. err = -ENOENT;
  752. }
  753. if (res == NULL)
  754. return err;
  755. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  756. /* family doesn't exist here */
  757. return -ENOENT;
  758. }
  759. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  760. CTRL_CMD_NEWFAMILY);
  761. if (IS_ERR(msg))
  762. return PTR_ERR(msg);
  763. return genlmsg_reply(msg, info);
  764. }
  765. static int genl_ctrl_event(int event, struct genl_family *family,
  766. const struct genl_multicast_group *grp,
  767. int grp_id)
  768. {
  769. struct sk_buff *msg;
  770. /* genl is still initialising */
  771. if (!init_net.genl_sock)
  772. return 0;
  773. switch (event) {
  774. case CTRL_CMD_NEWFAMILY:
  775. case CTRL_CMD_DELFAMILY:
  776. WARN_ON(grp);
  777. msg = ctrl_build_family_msg(family, 0, 0, event);
  778. break;
  779. case CTRL_CMD_NEWMCAST_GRP:
  780. case CTRL_CMD_DELMCAST_GRP:
  781. BUG_ON(!grp);
  782. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  783. break;
  784. default:
  785. return -EINVAL;
  786. }
  787. if (IS_ERR(msg))
  788. return PTR_ERR(msg);
  789. if (!family->netnsok) {
  790. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  791. 0, GFP_KERNEL);
  792. } else {
  793. rcu_read_lock();
  794. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  795. 0, GFP_ATOMIC);
  796. rcu_read_unlock();
  797. }
  798. return 0;
  799. }
  800. static struct genl_ops genl_ctrl_ops[] = {
  801. {
  802. .cmd = CTRL_CMD_GETFAMILY,
  803. .doit = ctrl_getfamily,
  804. .dumpit = ctrl_dumpfamily,
  805. .policy = ctrl_policy,
  806. },
  807. };
  808. static struct genl_multicast_group genl_ctrl_groups[] = {
  809. { .name = "notify", },
  810. };
  811. static int __net_init genl_pernet_init(struct net *net)
  812. {
  813. struct netlink_kernel_cfg cfg = {
  814. .input = genl_rcv,
  815. .flags = NL_CFG_F_NONROOT_RECV,
  816. };
  817. /* we'll bump the group number right afterwards */
  818. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  819. if (!net->genl_sock && net_eq(net, &init_net))
  820. panic("GENL: Cannot initialize generic netlink\n");
  821. if (!net->genl_sock)
  822. return -ENOMEM;
  823. return 0;
  824. }
  825. static void __net_exit genl_pernet_exit(struct net *net)
  826. {
  827. netlink_kernel_release(net->genl_sock);
  828. net->genl_sock = NULL;
  829. }
  830. static struct pernet_operations genl_pernet_ops = {
  831. .init = genl_pernet_init,
  832. .exit = genl_pernet_exit,
  833. };
  834. static int __init genl_init(void)
  835. {
  836. int i, err;
  837. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  838. INIT_LIST_HEAD(&family_ht[i]);
  839. err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops,
  840. genl_ctrl_groups);
  841. if (err < 0)
  842. goto problem;
  843. err = register_pernet_subsys(&genl_pernet_ops);
  844. if (err)
  845. goto problem;
  846. return 0;
  847. problem:
  848. panic("GENL: Cannot register controller: %d\n", err);
  849. }
  850. subsys_initcall(genl_init);
  851. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  852. gfp_t flags)
  853. {
  854. struct sk_buff *tmp;
  855. struct net *net, *prev = NULL;
  856. int err;
  857. for_each_net_rcu(net) {
  858. if (prev) {
  859. tmp = skb_clone(skb, flags);
  860. if (!tmp) {
  861. err = -ENOMEM;
  862. goto error;
  863. }
  864. err = nlmsg_multicast(prev->genl_sock, tmp,
  865. portid, group, flags);
  866. if (err)
  867. goto error;
  868. }
  869. prev = net;
  870. }
  871. return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  872. error:
  873. kfree_skb(skb);
  874. return err;
  875. }
  876. int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
  877. u32 portid, unsigned int group, gfp_t flags)
  878. {
  879. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  880. return -EINVAL;
  881. group = family->mcgrp_offset + group;
  882. return genlmsg_mcast(skb, portid, group, flags);
  883. }
  884. EXPORT_SYMBOL(genlmsg_multicast_allns);
  885. void genl_notify(struct genl_family *family,
  886. struct sk_buff *skb, struct net *net, u32 portid, u32 group,
  887. struct nlmsghdr *nlh, gfp_t flags)
  888. {
  889. struct sock *sk = net->genl_sock;
  890. int report = 0;
  891. if (nlh)
  892. report = nlmsg_report(nlh);
  893. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  894. return;
  895. group = family->mcgrp_offset + group;
  896. nlmsg_notify(sk, skb, portid, group, report, flags);
  897. }
  898. EXPORT_SYMBOL(genl_notify);