genetlink.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. bool 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_start(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 = 0;
  439. if (ops->start) {
  440. genl_lock();
  441. rc = ops->start(cb);
  442. genl_unlock();
  443. }
  444. return rc;
  445. }
  446. static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  447. {
  448. /* our ops are always const - netlink API doesn't propagate that */
  449. const struct genl_ops *ops = cb->data;
  450. int rc;
  451. genl_lock();
  452. rc = ops->dumpit(skb, cb);
  453. genl_unlock();
  454. return rc;
  455. }
  456. static int genl_lock_done(struct netlink_callback *cb)
  457. {
  458. /* our ops are always const - netlink API doesn't propagate that */
  459. const struct genl_ops *ops = cb->data;
  460. int rc = 0;
  461. if (ops->done) {
  462. genl_lock();
  463. rc = ops->done(cb);
  464. genl_unlock();
  465. }
  466. return rc;
  467. }
  468. static int genl_family_rcv_msg(struct genl_family *family,
  469. struct sk_buff *skb,
  470. struct nlmsghdr *nlh)
  471. {
  472. const struct genl_ops *ops;
  473. struct net *net = sock_net(skb->sk);
  474. struct genl_info info;
  475. struct genlmsghdr *hdr = nlmsg_data(nlh);
  476. struct nlattr **attrbuf;
  477. int hdrlen, err;
  478. /* this family doesn't exist in this netns */
  479. if (!family->netnsok && !net_eq(net, &init_net))
  480. return -ENOENT;
  481. hdrlen = GENL_HDRLEN + family->hdrsize;
  482. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  483. return -EINVAL;
  484. ops = genl_get_cmd(hdr->cmd, family);
  485. if (ops == NULL)
  486. return -EOPNOTSUPP;
  487. if ((ops->flags & GENL_ADMIN_PERM) &&
  488. !netlink_capable(skb, CAP_NET_ADMIN))
  489. return -EPERM;
  490. if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
  491. int rc;
  492. if (ops->dumpit == NULL)
  493. return -EOPNOTSUPP;
  494. if (!family->parallel_ops) {
  495. struct netlink_dump_control c = {
  496. .module = family->module,
  497. /* we have const, but the netlink API doesn't */
  498. .data = (void *)ops,
  499. .start = genl_lock_start,
  500. .dump = genl_lock_dumpit,
  501. .done = genl_lock_done,
  502. };
  503. genl_unlock();
  504. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  505. genl_lock();
  506. } else {
  507. struct netlink_dump_control c = {
  508. .module = family->module,
  509. .start = ops->start,
  510. .dump = ops->dumpit,
  511. .done = ops->done,
  512. };
  513. rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
  514. }
  515. return rc;
  516. }
  517. if (ops->doit == NULL)
  518. return -EOPNOTSUPP;
  519. if (family->maxattr && family->parallel_ops) {
  520. attrbuf = kmalloc((family->maxattr+1) *
  521. sizeof(struct nlattr *), GFP_KERNEL);
  522. if (attrbuf == NULL)
  523. return -ENOMEM;
  524. } else
  525. attrbuf = family->attrbuf;
  526. if (attrbuf) {
  527. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  528. ops->policy);
  529. if (err < 0)
  530. goto out;
  531. }
  532. info.snd_seq = nlh->nlmsg_seq;
  533. info.snd_portid = NETLINK_CB(skb).portid;
  534. info.nlhdr = nlh;
  535. info.genlhdr = nlmsg_data(nlh);
  536. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  537. info.attrs = attrbuf;
  538. info.dst_sk = skb->sk;
  539. genl_info_net_set(&info, net);
  540. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  541. if (family->pre_doit) {
  542. err = family->pre_doit(ops, skb, &info);
  543. if (err)
  544. goto out;
  545. }
  546. err = ops->doit(skb, &info);
  547. if (family->post_doit)
  548. family->post_doit(ops, skb, &info);
  549. out:
  550. if (family->parallel_ops)
  551. kfree(attrbuf);
  552. return err;
  553. }
  554. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  555. {
  556. struct genl_family *family;
  557. int err;
  558. family = genl_family_find_byid(nlh->nlmsg_type);
  559. if (family == NULL)
  560. return -ENOENT;
  561. if (!family->parallel_ops)
  562. genl_lock();
  563. err = genl_family_rcv_msg(family, skb, nlh);
  564. if (!family->parallel_ops)
  565. genl_unlock();
  566. return err;
  567. }
  568. static void genl_rcv(struct sk_buff *skb)
  569. {
  570. down_read(&cb_lock);
  571. netlink_rcv_skb(skb, &genl_rcv_msg);
  572. up_read(&cb_lock);
  573. }
  574. /**************************************************************************
  575. * Controller
  576. **************************************************************************/
  577. static struct genl_family genl_ctrl = {
  578. .id = GENL_ID_CTRL,
  579. .name = "nlctrl",
  580. .version = 0x2,
  581. .maxattr = CTRL_ATTR_MAX,
  582. .netnsok = true,
  583. };
  584. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  585. u32 flags, struct sk_buff *skb, u8 cmd)
  586. {
  587. void *hdr;
  588. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  589. if (hdr == NULL)
  590. return -1;
  591. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  592. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  593. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  594. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  595. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  596. goto nla_put_failure;
  597. if (family->n_ops) {
  598. struct nlattr *nla_ops;
  599. int i;
  600. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  601. if (nla_ops == NULL)
  602. goto nla_put_failure;
  603. for (i = 0; i < family->n_ops; i++) {
  604. struct nlattr *nest;
  605. const struct genl_ops *ops = &family->ops[i];
  606. u32 op_flags = ops->flags;
  607. if (ops->dumpit)
  608. op_flags |= GENL_CMD_CAP_DUMP;
  609. if (ops->doit)
  610. op_flags |= GENL_CMD_CAP_DO;
  611. if (ops->policy)
  612. op_flags |= GENL_CMD_CAP_HASPOL;
  613. nest = nla_nest_start(skb, i + 1);
  614. if (nest == NULL)
  615. goto nla_put_failure;
  616. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  617. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
  618. goto nla_put_failure;
  619. nla_nest_end(skb, nest);
  620. }
  621. nla_nest_end(skb, nla_ops);
  622. }
  623. if (family->n_mcgrps) {
  624. struct nlattr *nla_grps;
  625. int i;
  626. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  627. if (nla_grps == NULL)
  628. goto nla_put_failure;
  629. for (i = 0; i < family->n_mcgrps; i++) {
  630. struct nlattr *nest;
  631. const struct genl_multicast_group *grp;
  632. grp = &family->mcgrps[i];
  633. nest = nla_nest_start(skb, i + 1);
  634. if (nest == NULL)
  635. goto nla_put_failure;
  636. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
  637. family->mcgrp_offset + i) ||
  638. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  639. grp->name))
  640. goto nla_put_failure;
  641. nla_nest_end(skb, nest);
  642. }
  643. nla_nest_end(skb, nla_grps);
  644. }
  645. genlmsg_end(skb, hdr);
  646. return 0;
  647. nla_put_failure:
  648. genlmsg_cancel(skb, hdr);
  649. return -EMSGSIZE;
  650. }
  651. static int ctrl_fill_mcgrp_info(struct genl_family *family,
  652. const struct genl_multicast_group *grp,
  653. int grp_id, u32 portid, u32 seq, u32 flags,
  654. struct sk_buff *skb, u8 cmd)
  655. {
  656. void *hdr;
  657. struct nlattr *nla_grps;
  658. struct nlattr *nest;
  659. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  660. if (hdr == NULL)
  661. return -1;
  662. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  663. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
  664. goto nla_put_failure;
  665. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  666. if (nla_grps == NULL)
  667. goto nla_put_failure;
  668. nest = nla_nest_start(skb, 1);
  669. if (nest == NULL)
  670. goto nla_put_failure;
  671. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
  672. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  673. grp->name))
  674. goto nla_put_failure;
  675. nla_nest_end(skb, nest);
  676. nla_nest_end(skb, nla_grps);
  677. genlmsg_end(skb, hdr);
  678. return 0;
  679. nla_put_failure:
  680. genlmsg_cancel(skb, hdr);
  681. return -EMSGSIZE;
  682. }
  683. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  684. {
  685. int i, n = 0;
  686. struct genl_family *rt;
  687. struct net *net = sock_net(skb->sk);
  688. int chains_to_skip = cb->args[0];
  689. int fams_to_skip = cb->args[1];
  690. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  691. n = 0;
  692. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  693. if (!rt->netnsok && !net_eq(net, &init_net))
  694. continue;
  695. if (++n < fams_to_skip)
  696. continue;
  697. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  698. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  699. skb, CTRL_CMD_NEWFAMILY) < 0)
  700. goto errout;
  701. }
  702. fams_to_skip = 0;
  703. }
  704. errout:
  705. cb->args[0] = i;
  706. cb->args[1] = n;
  707. return skb->len;
  708. }
  709. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  710. u32 portid, int seq, u8 cmd)
  711. {
  712. struct sk_buff *skb;
  713. int err;
  714. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  715. if (skb == NULL)
  716. return ERR_PTR(-ENOBUFS);
  717. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  718. if (err < 0) {
  719. nlmsg_free(skb);
  720. return ERR_PTR(err);
  721. }
  722. return skb;
  723. }
  724. static struct sk_buff *
  725. ctrl_build_mcgrp_msg(struct genl_family *family,
  726. const struct genl_multicast_group *grp,
  727. int grp_id, u32 portid, int seq, u8 cmd)
  728. {
  729. struct sk_buff *skb;
  730. int err;
  731. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  732. if (skb == NULL)
  733. return ERR_PTR(-ENOBUFS);
  734. err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
  735. seq, 0, skb, cmd);
  736. if (err < 0) {
  737. nlmsg_free(skb);
  738. return ERR_PTR(err);
  739. }
  740. return skb;
  741. }
  742. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  743. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  744. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  745. .len = GENL_NAMSIZ - 1 },
  746. };
  747. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  748. {
  749. struct sk_buff *msg;
  750. struct genl_family *res = NULL;
  751. int err = -EINVAL;
  752. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  753. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  754. res = genl_family_find_byid(id);
  755. err = -ENOENT;
  756. }
  757. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  758. char *name;
  759. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  760. res = genl_family_find_byname(name);
  761. #ifdef CONFIG_MODULES
  762. if (res == NULL) {
  763. genl_unlock();
  764. up_read(&cb_lock);
  765. request_module("net-pf-%d-proto-%d-family-%s",
  766. PF_NETLINK, NETLINK_GENERIC, name);
  767. down_read(&cb_lock);
  768. genl_lock();
  769. res = genl_family_find_byname(name);
  770. }
  771. #endif
  772. err = -ENOENT;
  773. }
  774. if (res == NULL)
  775. return err;
  776. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  777. /* family doesn't exist here */
  778. return -ENOENT;
  779. }
  780. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  781. CTRL_CMD_NEWFAMILY);
  782. if (IS_ERR(msg))
  783. return PTR_ERR(msg);
  784. return genlmsg_reply(msg, info);
  785. }
  786. static int genl_ctrl_event(int event, struct genl_family *family,
  787. const struct genl_multicast_group *grp,
  788. int grp_id)
  789. {
  790. struct sk_buff *msg;
  791. /* genl is still initialising */
  792. if (!init_net.genl_sock)
  793. return 0;
  794. switch (event) {
  795. case CTRL_CMD_NEWFAMILY:
  796. case CTRL_CMD_DELFAMILY:
  797. WARN_ON(grp);
  798. msg = ctrl_build_family_msg(family, 0, 0, event);
  799. break;
  800. case CTRL_CMD_NEWMCAST_GRP:
  801. case CTRL_CMD_DELMCAST_GRP:
  802. BUG_ON(!grp);
  803. msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
  804. break;
  805. default:
  806. return -EINVAL;
  807. }
  808. if (IS_ERR(msg))
  809. return PTR_ERR(msg);
  810. if (!family->netnsok) {
  811. genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
  812. 0, GFP_KERNEL);
  813. } else {
  814. rcu_read_lock();
  815. genlmsg_multicast_allns(&genl_ctrl, msg, 0,
  816. 0, GFP_ATOMIC);
  817. rcu_read_unlock();
  818. }
  819. return 0;
  820. }
  821. static struct genl_ops genl_ctrl_ops[] = {
  822. {
  823. .cmd = CTRL_CMD_GETFAMILY,
  824. .doit = ctrl_getfamily,
  825. .dumpit = ctrl_dumpfamily,
  826. .policy = ctrl_policy,
  827. },
  828. };
  829. static struct genl_multicast_group genl_ctrl_groups[] = {
  830. { .name = "notify", },
  831. };
  832. static int genl_bind(struct net *net, int group)
  833. {
  834. int i, err = -ENOENT;
  835. down_read(&cb_lock);
  836. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  837. struct genl_family *f;
  838. list_for_each_entry(f, genl_family_chain(i), family_list) {
  839. if (group >= f->mcgrp_offset &&
  840. group < f->mcgrp_offset + f->n_mcgrps) {
  841. int fam_grp = group - f->mcgrp_offset;
  842. if (!f->netnsok && net != &init_net)
  843. err = -ENOENT;
  844. else if (f->mcast_bind)
  845. err = f->mcast_bind(net, fam_grp);
  846. else
  847. err = 0;
  848. break;
  849. }
  850. }
  851. }
  852. up_read(&cb_lock);
  853. return err;
  854. }
  855. static void genl_unbind(struct net *net, int group)
  856. {
  857. int i;
  858. down_read(&cb_lock);
  859. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  860. struct genl_family *f;
  861. list_for_each_entry(f, genl_family_chain(i), family_list) {
  862. if (group >= f->mcgrp_offset &&
  863. group < f->mcgrp_offset + f->n_mcgrps) {
  864. int fam_grp = group - f->mcgrp_offset;
  865. if (f->mcast_unbind)
  866. f->mcast_unbind(net, fam_grp);
  867. break;
  868. }
  869. }
  870. }
  871. up_read(&cb_lock);
  872. }
  873. static int __net_init genl_pernet_init(struct net *net)
  874. {
  875. struct netlink_kernel_cfg cfg = {
  876. .input = genl_rcv,
  877. .flags = NL_CFG_F_NONROOT_RECV,
  878. .bind = genl_bind,
  879. .unbind = genl_unbind,
  880. };
  881. /* we'll bump the group number right afterwards */
  882. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  883. if (!net->genl_sock && net_eq(net, &init_net))
  884. panic("GENL: Cannot initialize generic netlink\n");
  885. if (!net->genl_sock)
  886. return -ENOMEM;
  887. return 0;
  888. }
  889. static void __net_exit genl_pernet_exit(struct net *net)
  890. {
  891. netlink_kernel_release(net->genl_sock);
  892. net->genl_sock = NULL;
  893. }
  894. static struct pernet_operations genl_pernet_ops = {
  895. .init = genl_pernet_init,
  896. .exit = genl_pernet_exit,
  897. };
  898. static int __init genl_init(void)
  899. {
  900. int i, err;
  901. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  902. INIT_LIST_HEAD(&family_ht[i]);
  903. err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops,
  904. genl_ctrl_groups);
  905. if (err < 0)
  906. goto problem;
  907. err = register_pernet_subsys(&genl_pernet_ops);
  908. if (err)
  909. goto problem;
  910. return 0;
  911. problem:
  912. panic("GENL: Cannot register controller: %d\n", err);
  913. }
  914. subsys_initcall(genl_init);
  915. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  916. gfp_t flags)
  917. {
  918. struct sk_buff *tmp;
  919. struct net *net, *prev = NULL;
  920. bool delivered = false;
  921. int err;
  922. for_each_net_rcu(net) {
  923. if (prev) {
  924. tmp = skb_clone(skb, flags);
  925. if (!tmp) {
  926. err = -ENOMEM;
  927. goto error;
  928. }
  929. err = nlmsg_multicast(prev->genl_sock, tmp,
  930. portid, group, flags);
  931. if (!err)
  932. delivered = true;
  933. else if (err != -ESRCH)
  934. goto error;
  935. }
  936. prev = net;
  937. }
  938. err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  939. if (!err)
  940. delivered = true;
  941. else if (err != -ESRCH)
  942. return err;
  943. return delivered ? 0 : -ESRCH;
  944. error:
  945. kfree_skb(skb);
  946. return err;
  947. }
  948. int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
  949. u32 portid, unsigned int group, gfp_t flags)
  950. {
  951. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  952. return -EINVAL;
  953. group = family->mcgrp_offset + group;
  954. return genlmsg_mcast(skb, portid, group, flags);
  955. }
  956. EXPORT_SYMBOL(genlmsg_multicast_allns);
  957. void genl_notify(struct genl_family *family, struct sk_buff *skb,
  958. struct genl_info *info, u32 group, gfp_t flags)
  959. {
  960. struct net *net = genl_info_net(info);
  961. struct sock *sk = net->genl_sock;
  962. int report = 0;
  963. if (info->nlhdr)
  964. report = nlmsg_report(info->nlhdr);
  965. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  966. return;
  967. group = family->mcgrp_offset + group;
  968. nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
  969. }
  970. EXPORT_SYMBOL(genl_notify);