genetlink.c 23 KB

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