genetlink.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. */
  60. static unsigned long mc_group_start = 0x1;
  61. static unsigned long *mc_groups = &mc_group_start;
  62. static unsigned long mc_groups_longs = 1;
  63. static int genl_ctrl_event(int event, void *data);
  64. static inline unsigned int genl_family_hash(unsigned int id)
  65. {
  66. return id & GENL_FAM_TAB_MASK;
  67. }
  68. static inline struct list_head *genl_family_chain(unsigned int id)
  69. {
  70. return &family_ht[genl_family_hash(id)];
  71. }
  72. static struct genl_family *genl_family_find_byid(unsigned int id)
  73. {
  74. struct genl_family *f;
  75. list_for_each_entry(f, genl_family_chain(id), family_list)
  76. if (f->id == id)
  77. return f;
  78. return NULL;
  79. }
  80. static struct genl_family *genl_family_find_byname(char *name)
  81. {
  82. struct genl_family *f;
  83. int i;
  84. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  85. list_for_each_entry(f, genl_family_chain(i), family_list)
  86. if (strcmp(f->name, name) == 0)
  87. return f;
  88. return NULL;
  89. }
  90. static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
  91. {
  92. struct genl_ops *ops;
  93. list_for_each_entry(ops, &family->ops_list, ops_list)
  94. if (ops->cmd == cmd)
  95. return ops;
  96. return NULL;
  97. }
  98. /* Of course we are going to have problems once we hit
  99. * 2^16 alive types, but that can only happen by year 2K
  100. */
  101. static u16 genl_generate_id(void)
  102. {
  103. static u16 id_gen_idx = GENL_MIN_ID;
  104. int i;
  105. for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
  106. if (!genl_family_find_byid(id_gen_idx))
  107. return id_gen_idx;
  108. if (++id_gen_idx > GENL_MAX_ID)
  109. id_gen_idx = GENL_MIN_ID;
  110. }
  111. return 0;
  112. }
  113. static struct genl_multicast_group notify_grp;
  114. /**
  115. * genl_register_mc_group - register a multicast group
  116. *
  117. * Registers the specified multicast group and notifies userspace
  118. * about the new group.
  119. *
  120. * Returns 0 on success or a negative error code.
  121. *
  122. * @family: The generic netlink family the group shall be registered for.
  123. * @grp: The group to register, must have a name.
  124. */
  125. int genl_register_mc_group(struct genl_family *family,
  126. struct genl_multicast_group *grp)
  127. {
  128. int id;
  129. unsigned long *new_groups;
  130. int err = 0;
  131. BUG_ON(grp->name[0] == '\0');
  132. BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
  133. genl_lock_all();
  134. /* special-case our own group */
  135. if (grp == &notify_grp)
  136. id = GENL_ID_CTRL;
  137. else
  138. id = find_first_zero_bit(mc_groups,
  139. mc_groups_longs * BITS_PER_LONG);
  140. if (id >= mc_groups_longs * BITS_PER_LONG) {
  141. size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
  142. if (mc_groups == &mc_group_start) {
  143. new_groups = kzalloc(nlen, GFP_KERNEL);
  144. if (!new_groups) {
  145. err = -ENOMEM;
  146. goto out;
  147. }
  148. mc_groups = new_groups;
  149. *mc_groups = mc_group_start;
  150. } else {
  151. new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
  152. if (!new_groups) {
  153. err = -ENOMEM;
  154. goto out;
  155. }
  156. mc_groups = new_groups;
  157. mc_groups[mc_groups_longs] = 0;
  158. }
  159. mc_groups_longs++;
  160. }
  161. if (family->netnsok) {
  162. struct net *net;
  163. netlink_table_grab();
  164. rcu_read_lock();
  165. for_each_net_rcu(net) {
  166. err = __netlink_change_ngroups(net->genl_sock,
  167. mc_groups_longs * BITS_PER_LONG);
  168. if (err) {
  169. /*
  170. * No need to roll back, can only fail if
  171. * memory allocation fails and then the
  172. * number of _possible_ groups has been
  173. * increased on some sockets which is ok.
  174. */
  175. rcu_read_unlock();
  176. netlink_table_ungrab();
  177. goto out;
  178. }
  179. }
  180. rcu_read_unlock();
  181. netlink_table_ungrab();
  182. } else {
  183. err = netlink_change_ngroups(init_net.genl_sock,
  184. mc_groups_longs * BITS_PER_LONG);
  185. if (err)
  186. goto out;
  187. }
  188. grp->id = id;
  189. set_bit(id, mc_groups);
  190. list_add_tail(&grp->list, &family->mcast_groups);
  191. grp->family = family;
  192. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
  193. out:
  194. genl_unlock_all();
  195. return err;
  196. }
  197. EXPORT_SYMBOL(genl_register_mc_group);
  198. static void __genl_unregister_mc_group(struct genl_family *family,
  199. struct genl_multicast_group *grp)
  200. {
  201. struct net *net;
  202. BUG_ON(grp->family != family);
  203. netlink_table_grab();
  204. rcu_read_lock();
  205. for_each_net_rcu(net)
  206. __netlink_clear_multicast_users(net->genl_sock, grp->id);
  207. rcu_read_unlock();
  208. netlink_table_ungrab();
  209. clear_bit(grp->id, mc_groups);
  210. list_del(&grp->list);
  211. genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
  212. grp->id = 0;
  213. grp->family = NULL;
  214. }
  215. /**
  216. * genl_unregister_mc_group - unregister a multicast group
  217. *
  218. * Unregisters the specified multicast group and notifies userspace
  219. * about it. All current listeners on the group are removed.
  220. *
  221. * Note: It is not necessary to unregister all multicast groups before
  222. * unregistering the family, unregistering the family will cause
  223. * all assigned multicast groups to be unregistered automatically.
  224. *
  225. * @family: Generic netlink family the group belongs to.
  226. * @grp: The group to unregister, must have been registered successfully
  227. * previously.
  228. */
  229. void genl_unregister_mc_group(struct genl_family *family,
  230. struct genl_multicast_group *grp)
  231. {
  232. genl_lock_all();
  233. __genl_unregister_mc_group(family, grp);
  234. genl_unlock_all();
  235. }
  236. EXPORT_SYMBOL(genl_unregister_mc_group);
  237. static void genl_unregister_mc_groups(struct genl_family *family)
  238. {
  239. struct genl_multicast_group *grp, *tmp;
  240. list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
  241. __genl_unregister_mc_group(family, grp);
  242. }
  243. /**
  244. * genl_register_ops - register generic netlink operations
  245. * @family: generic netlink family
  246. * @ops: operations to be registered
  247. *
  248. * Registers the specified operations and assigns them to the specified
  249. * family. Either a doit or dumpit callback must be specified or the
  250. * operation will fail. Only one operation structure per command
  251. * identifier may be registered.
  252. *
  253. * See include/net/genetlink.h for more documenation on the operations
  254. * structure.
  255. *
  256. * Returns 0 on success or a negative error code.
  257. */
  258. int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
  259. {
  260. int err = -EINVAL;
  261. if (ops->dumpit == NULL && ops->doit == NULL)
  262. goto errout;
  263. if (genl_get_cmd(ops->cmd, family)) {
  264. err = -EEXIST;
  265. goto errout;
  266. }
  267. if (ops->dumpit)
  268. ops->flags |= GENL_CMD_CAP_DUMP;
  269. if (ops->doit)
  270. ops->flags |= GENL_CMD_CAP_DO;
  271. if (ops->policy)
  272. ops->flags |= GENL_CMD_CAP_HASPOL;
  273. genl_lock_all();
  274. list_add_tail(&ops->ops_list, &family->ops_list);
  275. genl_unlock_all();
  276. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  277. err = 0;
  278. errout:
  279. return err;
  280. }
  281. EXPORT_SYMBOL(genl_register_ops);
  282. /**
  283. * genl_unregister_ops - unregister generic netlink operations
  284. * @family: generic netlink family
  285. * @ops: operations to be unregistered
  286. *
  287. * Unregisters the specified operations and unassigns them from the
  288. * specified family. The operation blocks until the current message
  289. * processing has finished and doesn't start again until the
  290. * unregister process has finished.
  291. *
  292. * Note: It is not necessary to unregister all operations before
  293. * unregistering the family, unregistering the family will cause
  294. * all assigned operations to be unregistered automatically.
  295. *
  296. * Returns 0 on success or a negative error code.
  297. */
  298. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  299. {
  300. struct genl_ops *rc;
  301. genl_lock_all();
  302. list_for_each_entry(rc, &family->ops_list, ops_list) {
  303. if (rc == ops) {
  304. list_del(&ops->ops_list);
  305. genl_unlock_all();
  306. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  307. return 0;
  308. }
  309. }
  310. genl_unlock_all();
  311. return -ENOENT;
  312. }
  313. EXPORT_SYMBOL(genl_unregister_ops);
  314. /**
  315. * genl_register_family - register a generic netlink family
  316. * @family: generic netlink family
  317. *
  318. * Registers the specified family after validating it first. Only one
  319. * family may be registered with the same family name or identifier.
  320. * The family id may equal GENL_ID_GENERATE causing an unique id to
  321. * be automatically generated and assigned.
  322. *
  323. * Return 0 on success or a negative error code.
  324. */
  325. int genl_register_family(struct genl_family *family)
  326. {
  327. int err = -EINVAL;
  328. if (family->id && family->id < GENL_MIN_ID)
  329. goto errout;
  330. if (family->id > GENL_MAX_ID)
  331. goto errout;
  332. INIT_LIST_HEAD(&family->ops_list);
  333. INIT_LIST_HEAD(&family->mcast_groups);
  334. genl_lock_all();
  335. if (genl_family_find_byname(family->name)) {
  336. err = -EEXIST;
  337. goto errout_locked;
  338. }
  339. if (family->id == GENL_ID_GENERATE) {
  340. u16 newid = genl_generate_id();
  341. if (!newid) {
  342. err = -ENOMEM;
  343. goto errout_locked;
  344. }
  345. family->id = newid;
  346. } else if (genl_family_find_byid(family->id)) {
  347. err = -EEXIST;
  348. goto errout_locked;
  349. }
  350. if (family->maxattr && !family->parallel_ops) {
  351. family->attrbuf = kmalloc((family->maxattr+1) *
  352. sizeof(struct nlattr *), GFP_KERNEL);
  353. if (family->attrbuf == NULL) {
  354. err = -ENOMEM;
  355. goto errout_locked;
  356. }
  357. } else
  358. family->attrbuf = NULL;
  359. list_add_tail(&family->family_list, genl_family_chain(family->id));
  360. genl_unlock_all();
  361. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  362. return 0;
  363. errout_locked:
  364. genl_unlock_all();
  365. errout:
  366. return err;
  367. }
  368. EXPORT_SYMBOL(genl_register_family);
  369. /**
  370. * genl_register_family_with_ops - register a generic netlink family
  371. * @family: generic netlink family
  372. * @ops: operations to be registered
  373. * @n_ops: number of elements to register
  374. *
  375. * Registers the specified family and operations from the specified table.
  376. * Only one family may be registered with the same family name or identifier.
  377. *
  378. * The family id may equal GENL_ID_GENERATE causing an unique id to
  379. * be automatically generated and assigned.
  380. *
  381. * Either a doit or dumpit callback must be specified for every registered
  382. * operation or the function will fail. Only one operation structure per
  383. * command identifier may be registered.
  384. *
  385. * See include/net/genetlink.h for more documenation on the operations
  386. * structure.
  387. *
  388. * This is equivalent to calling genl_register_family() followed by
  389. * genl_register_ops() for every operation entry in the table taking
  390. * care to unregister the family on error path.
  391. *
  392. * Return 0 on success or a negative error code.
  393. */
  394. int genl_register_family_with_ops(struct genl_family *family,
  395. struct genl_ops *ops, size_t n_ops)
  396. {
  397. int err, i;
  398. err = genl_register_family(family);
  399. if (err)
  400. return err;
  401. for (i = 0; i < n_ops; ++i, ++ops) {
  402. err = genl_register_ops(family, ops);
  403. if (err)
  404. goto err_out;
  405. }
  406. return 0;
  407. err_out:
  408. genl_unregister_family(family);
  409. return err;
  410. }
  411. EXPORT_SYMBOL(genl_register_family_with_ops);
  412. /**
  413. * genl_unregister_family - unregister generic netlink family
  414. * @family: generic netlink family
  415. *
  416. * Unregisters the specified family.
  417. *
  418. * Returns 0 on success or a negative error code.
  419. */
  420. int genl_unregister_family(struct genl_family *family)
  421. {
  422. struct genl_family *rc;
  423. genl_lock_all();
  424. genl_unregister_mc_groups(family);
  425. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  426. if (family->id != rc->id || strcmp(rc->name, family->name))
  427. continue;
  428. list_del(&rc->family_list);
  429. INIT_LIST_HEAD(&family->ops_list);
  430. genl_unlock_all();
  431. kfree(family->attrbuf);
  432. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  433. return 0;
  434. }
  435. genl_unlock_all();
  436. return -ENOENT;
  437. }
  438. EXPORT_SYMBOL(genl_unregister_family);
  439. /**
  440. * genlmsg_put - Add generic netlink header to netlink message
  441. * @skb: socket buffer holding the message
  442. * @portid: netlink portid the message is addressed to
  443. * @seq: sequence number (usually the one of the sender)
  444. * @family: generic netlink family
  445. * @flags: netlink message flags
  446. * @cmd: generic netlink command
  447. *
  448. * Returns pointer to user specific header
  449. */
  450. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  451. struct genl_family *family, int flags, u8 cmd)
  452. {
  453. struct nlmsghdr *nlh;
  454. struct genlmsghdr *hdr;
  455. nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
  456. family->hdrsize, flags);
  457. if (nlh == NULL)
  458. return NULL;
  459. hdr = nlmsg_data(nlh);
  460. hdr->cmd = cmd;
  461. hdr->version = family->version;
  462. hdr->reserved = 0;
  463. return (char *) hdr + GENL_HDRLEN;
  464. }
  465. EXPORT_SYMBOL(genlmsg_put);
  466. static int genl_family_rcv_msg(struct genl_family *family,
  467. struct sk_buff *skb,
  468. struct nlmsghdr *nlh)
  469. {
  470. struct genl_ops *ops;
  471. struct net *net = sock_net(skb->sk);
  472. struct genl_info info;
  473. struct genlmsghdr *hdr = nlmsg_data(nlh);
  474. struct nlattr **attrbuf;
  475. int hdrlen, err;
  476. /* this family doesn't exist in this netns */
  477. if (!family->netnsok && !net_eq(net, &init_net))
  478. return -ENOENT;
  479. hdrlen = GENL_HDRLEN + family->hdrsize;
  480. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  481. return -EINVAL;
  482. ops = genl_get_cmd(hdr->cmd, family);
  483. if (ops == NULL)
  484. return -EOPNOTSUPP;
  485. if ((ops->flags & GENL_ADMIN_PERM) &&
  486. !capable(CAP_NET_ADMIN))
  487. return -EPERM;
  488. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  489. struct netlink_dump_control c = {
  490. .dump = ops->dumpit,
  491. .done = ops->done,
  492. };
  493. if (ops->dumpit == NULL)
  494. return -EOPNOTSUPP;
  495. return netlink_dump_start(net->genl_sock, skb, nlh, &c);
  496. }
  497. if (ops->doit == NULL)
  498. return -EOPNOTSUPP;
  499. if (family->maxattr && family->parallel_ops) {
  500. attrbuf = kmalloc((family->maxattr+1) *
  501. sizeof(struct nlattr *), GFP_KERNEL);
  502. if (attrbuf == NULL)
  503. return -ENOMEM;
  504. } else
  505. attrbuf = family->attrbuf;
  506. if (attrbuf) {
  507. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  508. ops->policy);
  509. if (err < 0)
  510. goto out;
  511. }
  512. info.snd_seq = nlh->nlmsg_seq;
  513. info.snd_portid = NETLINK_CB(skb).portid;
  514. info.nlhdr = nlh;
  515. info.genlhdr = nlmsg_data(nlh);
  516. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  517. info.attrs = attrbuf;
  518. genl_info_net_set(&info, net);
  519. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  520. if (family->pre_doit) {
  521. err = family->pre_doit(ops, skb, &info);
  522. if (err)
  523. goto out;
  524. }
  525. err = ops->doit(skb, &info);
  526. if (family->post_doit)
  527. family->post_doit(ops, skb, &info);
  528. out:
  529. if (family->parallel_ops)
  530. kfree(attrbuf);
  531. return err;
  532. }
  533. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  534. {
  535. struct genl_family *family;
  536. int err;
  537. family = genl_family_find_byid(nlh->nlmsg_type);
  538. if (family == NULL)
  539. return -ENOENT;
  540. if (!family->parallel_ops)
  541. genl_lock();
  542. err = genl_family_rcv_msg(family, skb, nlh);
  543. if (!family->parallel_ops)
  544. genl_unlock();
  545. return err;
  546. }
  547. static void genl_rcv(struct sk_buff *skb)
  548. {
  549. down_read(&cb_lock);
  550. netlink_rcv_skb(skb, &genl_rcv_msg);
  551. up_read(&cb_lock);
  552. }
  553. /**************************************************************************
  554. * Controller
  555. **************************************************************************/
  556. static struct genl_family genl_ctrl = {
  557. .id = GENL_ID_CTRL,
  558. .name = "nlctrl",
  559. .version = 0x2,
  560. .maxattr = CTRL_ATTR_MAX,
  561. .netnsok = true,
  562. };
  563. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  564. u32 flags, struct sk_buff *skb, u8 cmd)
  565. {
  566. void *hdr;
  567. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  568. if (hdr == NULL)
  569. return -1;
  570. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  571. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  572. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  573. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  574. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  575. goto nla_put_failure;
  576. if (!list_empty(&family->ops_list)) {
  577. struct nlattr *nla_ops;
  578. struct genl_ops *ops;
  579. int idx = 1;
  580. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  581. if (nla_ops == NULL)
  582. goto nla_put_failure;
  583. list_for_each_entry(ops, &family->ops_list, ops_list) {
  584. struct nlattr *nest;
  585. nest = nla_nest_start(skb, idx++);
  586. if (nest == NULL)
  587. goto nla_put_failure;
  588. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  589. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
  590. goto nla_put_failure;
  591. nla_nest_end(skb, nest);
  592. }
  593. nla_nest_end(skb, nla_ops);
  594. }
  595. if (!list_empty(&family->mcast_groups)) {
  596. struct genl_multicast_group *grp;
  597. struct nlattr *nla_grps;
  598. int idx = 1;
  599. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  600. if (nla_grps == NULL)
  601. goto nla_put_failure;
  602. list_for_each_entry(grp, &family->mcast_groups, list) {
  603. struct nlattr *nest;
  604. nest = nla_nest_start(skb, idx++);
  605. if (nest == NULL)
  606. goto nla_put_failure;
  607. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  608. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  609. grp->name))
  610. goto nla_put_failure;
  611. nla_nest_end(skb, nest);
  612. }
  613. nla_nest_end(skb, nla_grps);
  614. }
  615. return genlmsg_end(skb, hdr);
  616. nla_put_failure:
  617. genlmsg_cancel(skb, hdr);
  618. return -EMSGSIZE;
  619. }
  620. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
  621. u32 seq, u32 flags, struct sk_buff *skb,
  622. u8 cmd)
  623. {
  624. void *hdr;
  625. struct nlattr *nla_grps;
  626. struct nlattr *nest;
  627. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  628. if (hdr == NULL)
  629. return -1;
  630. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
  631. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
  632. goto nla_put_failure;
  633. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  634. if (nla_grps == NULL)
  635. goto nla_put_failure;
  636. nest = nla_nest_start(skb, 1);
  637. if (nest == NULL)
  638. goto nla_put_failure;
  639. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  640. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  641. grp->name))
  642. goto nla_put_failure;
  643. nla_nest_end(skb, nest);
  644. nla_nest_end(skb, nla_grps);
  645. return genlmsg_end(skb, hdr);
  646. nla_put_failure:
  647. genlmsg_cancel(skb, hdr);
  648. return -EMSGSIZE;
  649. }
  650. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  651. {
  652. int i, n = 0;
  653. struct genl_family *rt;
  654. struct net *net = sock_net(skb->sk);
  655. int chains_to_skip = cb->args[0];
  656. int fams_to_skip = cb->args[1];
  657. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  658. n = 0;
  659. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  660. if (!rt->netnsok && !net_eq(net, &init_net))
  661. continue;
  662. if (++n < fams_to_skip)
  663. continue;
  664. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  665. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  666. skb, CTRL_CMD_NEWFAMILY) < 0)
  667. goto errout;
  668. }
  669. fams_to_skip = 0;
  670. }
  671. errout:
  672. cb->args[0] = i;
  673. cb->args[1] = n;
  674. return skb->len;
  675. }
  676. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  677. u32 portid, int seq, u8 cmd)
  678. {
  679. struct sk_buff *skb;
  680. int err;
  681. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  682. if (skb == NULL)
  683. return ERR_PTR(-ENOBUFS);
  684. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  685. if (err < 0) {
  686. nlmsg_free(skb);
  687. return ERR_PTR(err);
  688. }
  689. return skb;
  690. }
  691. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  692. u32 portid, int seq, u8 cmd)
  693. {
  694. struct sk_buff *skb;
  695. int err;
  696. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  697. if (skb == NULL)
  698. return ERR_PTR(-ENOBUFS);
  699. err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
  700. if (err < 0) {
  701. nlmsg_free(skb);
  702. return ERR_PTR(err);
  703. }
  704. return skb;
  705. }
  706. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  707. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  708. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  709. .len = GENL_NAMSIZ - 1 },
  710. };
  711. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  712. {
  713. struct sk_buff *msg;
  714. struct genl_family *res = NULL;
  715. int err = -EINVAL;
  716. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  717. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  718. res = genl_family_find_byid(id);
  719. err = -ENOENT;
  720. }
  721. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  722. char *name;
  723. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  724. res = genl_family_find_byname(name);
  725. #ifdef CONFIG_MODULES
  726. if (res == NULL) {
  727. genl_unlock();
  728. request_module("net-pf-%d-proto-%d-family-%s",
  729. PF_NETLINK, NETLINK_GENERIC, name);
  730. genl_lock();
  731. res = genl_family_find_byname(name);
  732. }
  733. #endif
  734. err = -ENOENT;
  735. }
  736. if (res == NULL)
  737. return err;
  738. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  739. /* family doesn't exist here */
  740. return -ENOENT;
  741. }
  742. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  743. CTRL_CMD_NEWFAMILY);
  744. if (IS_ERR(msg))
  745. return PTR_ERR(msg);
  746. return genlmsg_reply(msg, info);
  747. }
  748. static int genl_ctrl_event(int event, void *data)
  749. {
  750. struct sk_buff *msg;
  751. struct genl_family *family;
  752. struct genl_multicast_group *grp;
  753. /* genl is still initialising */
  754. if (!init_net.genl_sock)
  755. return 0;
  756. switch (event) {
  757. case CTRL_CMD_NEWFAMILY:
  758. case CTRL_CMD_DELFAMILY:
  759. family = data;
  760. msg = ctrl_build_family_msg(family, 0, 0, event);
  761. break;
  762. case CTRL_CMD_NEWMCAST_GRP:
  763. case CTRL_CMD_DELMCAST_GRP:
  764. grp = data;
  765. family = grp->family;
  766. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  767. break;
  768. default:
  769. return -EINVAL;
  770. }
  771. if (IS_ERR(msg))
  772. return PTR_ERR(msg);
  773. if (!family->netnsok) {
  774. genlmsg_multicast_netns(&init_net, msg, 0,
  775. GENL_ID_CTRL, GFP_KERNEL);
  776. } else {
  777. rcu_read_lock();
  778. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  779. rcu_read_unlock();
  780. }
  781. return 0;
  782. }
  783. static struct genl_ops genl_ctrl_ops = {
  784. .cmd = CTRL_CMD_GETFAMILY,
  785. .doit = ctrl_getfamily,
  786. .dumpit = ctrl_dumpfamily,
  787. .policy = ctrl_policy,
  788. };
  789. static struct genl_multicast_group notify_grp = {
  790. .name = "notify",
  791. };
  792. static int __net_init genl_pernet_init(struct net *net)
  793. {
  794. struct netlink_kernel_cfg cfg = {
  795. .input = genl_rcv,
  796. .flags = NL_CFG_F_NONROOT_RECV,
  797. };
  798. /* we'll bump the group number right afterwards */
  799. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  800. if (!net->genl_sock && net_eq(net, &init_net))
  801. panic("GENL: Cannot initialize generic netlink\n");
  802. if (!net->genl_sock)
  803. return -ENOMEM;
  804. return 0;
  805. }
  806. static void __net_exit genl_pernet_exit(struct net *net)
  807. {
  808. netlink_kernel_release(net->genl_sock);
  809. net->genl_sock = NULL;
  810. }
  811. static struct pernet_operations genl_pernet_ops = {
  812. .init = genl_pernet_init,
  813. .exit = genl_pernet_exit,
  814. };
  815. static int __init genl_init(void)
  816. {
  817. int i, err;
  818. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  819. INIT_LIST_HEAD(&family_ht[i]);
  820. err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
  821. if (err < 0)
  822. goto problem;
  823. err = register_pernet_subsys(&genl_pernet_ops);
  824. if (err)
  825. goto problem;
  826. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  827. if (err < 0)
  828. goto problem;
  829. return 0;
  830. problem:
  831. panic("GENL: Cannot register controller: %d\n", err);
  832. }
  833. subsys_initcall(genl_init);
  834. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  835. gfp_t flags)
  836. {
  837. struct sk_buff *tmp;
  838. struct net *net, *prev = NULL;
  839. int err;
  840. for_each_net_rcu(net) {
  841. if (prev) {
  842. tmp = skb_clone(skb, flags);
  843. if (!tmp) {
  844. err = -ENOMEM;
  845. goto error;
  846. }
  847. err = nlmsg_multicast(prev->genl_sock, tmp,
  848. portid, group, flags);
  849. if (err)
  850. goto error;
  851. }
  852. prev = net;
  853. }
  854. return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  855. error:
  856. kfree_skb(skb);
  857. return err;
  858. }
  859. int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
  860. gfp_t flags)
  861. {
  862. return genlmsg_mcast(skb, portid, group, flags);
  863. }
  864. EXPORT_SYMBOL(genlmsg_multicast_allns);
  865. void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
  866. struct nlmsghdr *nlh, gfp_t flags)
  867. {
  868. struct sock *sk = net->genl_sock;
  869. int report = 0;
  870. if (nlh)
  871. report = nlmsg_report(nlh);
  872. nlmsg_notify(sk, skb, portid, group, report, flags);
  873. }
  874. EXPORT_SYMBOL(genl_notify);