genetlink.c 23 KB

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