genetlink.c 26 KB

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