genetlink.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. 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. * @pid: netlink pid 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 pid, 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, pid, 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_pid = NETLINK_CB(skb).pid;
  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 pid, u32 seq,
  534. u32 flags, struct sk_buff *skb, u8 cmd)
  535. {
  536. void *hdr;
  537. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  538. if (hdr == NULL)
  539. return -1;
  540. 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. 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. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  558. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  559. nla_nest_end(skb, nest);
  560. }
  561. nla_nest_end(skb, nla_ops);
  562. }
  563. if (!list_empty(&family->mcast_groups)) {
  564. struct genl_multicast_group *grp;
  565. struct nlattr *nla_grps;
  566. int idx = 1;
  567. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  568. if (nla_grps == NULL)
  569. goto nla_put_failure;
  570. list_for_each_entry(grp, &family->mcast_groups, list) {
  571. struct nlattr *nest;
  572. nest = nla_nest_start(skb, idx++);
  573. if (nest == NULL)
  574. goto nla_put_failure;
  575. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  576. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  577. grp->name);
  578. nla_nest_end(skb, nest);
  579. }
  580. nla_nest_end(skb, nla_grps);
  581. }
  582. return genlmsg_end(skb, hdr);
  583. nla_put_failure:
  584. genlmsg_cancel(skb, hdr);
  585. return -EMSGSIZE;
  586. }
  587. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  588. u32 seq, u32 flags, struct sk_buff *skb,
  589. u8 cmd)
  590. {
  591. void *hdr;
  592. struct nlattr *nla_grps;
  593. struct nlattr *nest;
  594. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  595. if (hdr == NULL)
  596. return -1;
  597. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  598. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  599. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  600. if (nla_grps == NULL)
  601. goto nla_put_failure;
  602. nest = nla_nest_start(skb, 1);
  603. if (nest == NULL)
  604. goto nla_put_failure;
  605. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  606. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  607. grp->name);
  608. nla_nest_end(skb, nest);
  609. nla_nest_end(skb, nla_grps);
  610. return genlmsg_end(skb, hdr);
  611. nla_put_failure:
  612. genlmsg_cancel(skb, hdr);
  613. return -EMSGSIZE;
  614. }
  615. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  616. {
  617. int i, n = 0;
  618. struct genl_family *rt;
  619. struct net *net = sock_net(skb->sk);
  620. int chains_to_skip = cb->args[0];
  621. int fams_to_skip = cb->args[1];
  622. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  623. n = 0;
  624. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  625. if (!rt->netnsok && !net_eq(net, &init_net))
  626. continue;
  627. if (++n < fams_to_skip)
  628. continue;
  629. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  630. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  631. skb, CTRL_CMD_NEWFAMILY) < 0)
  632. goto errout;
  633. }
  634. fams_to_skip = 0;
  635. }
  636. errout:
  637. cb->args[0] = i;
  638. cb->args[1] = n;
  639. return skb->len;
  640. }
  641. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  642. u32 pid, int seq, u8 cmd)
  643. {
  644. struct sk_buff *skb;
  645. int err;
  646. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  647. if (skb == NULL)
  648. return ERR_PTR(-ENOBUFS);
  649. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  650. if (err < 0) {
  651. nlmsg_free(skb);
  652. return ERR_PTR(err);
  653. }
  654. return skb;
  655. }
  656. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  657. u32 pid, int seq, u8 cmd)
  658. {
  659. struct sk_buff *skb;
  660. int err;
  661. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  662. if (skb == NULL)
  663. return ERR_PTR(-ENOBUFS);
  664. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  665. if (err < 0) {
  666. nlmsg_free(skb);
  667. return ERR_PTR(err);
  668. }
  669. return skb;
  670. }
  671. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  672. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  673. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  674. .len = GENL_NAMSIZ - 1 },
  675. };
  676. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  677. {
  678. struct sk_buff *msg;
  679. struct genl_family *res = NULL;
  680. int err = -EINVAL;
  681. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  682. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  683. res = genl_family_find_byid(id);
  684. err = -ENOENT;
  685. }
  686. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  687. char *name;
  688. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  689. res = genl_family_find_byname(name);
  690. #ifdef CONFIG_MODULES
  691. if (res == NULL) {
  692. genl_unlock();
  693. request_module("net-pf-%d-proto-%d-type-%s",
  694. PF_NETLINK, NETLINK_GENERIC, name);
  695. genl_lock();
  696. res = genl_family_find_byname(name);
  697. }
  698. #endif
  699. err = -ENOENT;
  700. }
  701. if (res == NULL)
  702. return err;
  703. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  704. /* family doesn't exist here */
  705. return -ENOENT;
  706. }
  707. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  708. CTRL_CMD_NEWFAMILY);
  709. if (IS_ERR(msg))
  710. return PTR_ERR(msg);
  711. return genlmsg_reply(msg, info);
  712. }
  713. static int genl_ctrl_event(int event, void *data)
  714. {
  715. struct sk_buff *msg;
  716. struct genl_family *family;
  717. struct genl_multicast_group *grp;
  718. /* genl is still initialising */
  719. if (!init_net.genl_sock)
  720. return 0;
  721. switch (event) {
  722. case CTRL_CMD_NEWFAMILY:
  723. case CTRL_CMD_DELFAMILY:
  724. family = data;
  725. msg = ctrl_build_family_msg(family, 0, 0, event);
  726. break;
  727. case CTRL_CMD_NEWMCAST_GRP:
  728. case CTRL_CMD_DELMCAST_GRP:
  729. grp = data;
  730. family = grp->family;
  731. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  732. break;
  733. default:
  734. return -EINVAL;
  735. }
  736. if (IS_ERR(msg))
  737. return PTR_ERR(msg);
  738. if (!family->netnsok) {
  739. genlmsg_multicast_netns(&init_net, msg, 0,
  740. GENL_ID_CTRL, GFP_KERNEL);
  741. } else {
  742. rcu_read_lock();
  743. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  744. rcu_read_unlock();
  745. }
  746. return 0;
  747. }
  748. static struct genl_ops genl_ctrl_ops = {
  749. .cmd = CTRL_CMD_GETFAMILY,
  750. .doit = ctrl_getfamily,
  751. .dumpit = ctrl_dumpfamily,
  752. .policy = ctrl_policy,
  753. };
  754. static struct genl_multicast_group notify_grp = {
  755. .name = "notify",
  756. };
  757. static int __net_init genl_pernet_init(struct net *net)
  758. {
  759. /* we'll bump the group number right afterwards */
  760. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
  761. genl_rcv, &genl_mutex,
  762. THIS_MODULE);
  763. if (!net->genl_sock && net_eq(net, &init_net))
  764. panic("GENL: Cannot initialize generic netlink\n");
  765. if (!net->genl_sock)
  766. return -ENOMEM;
  767. return 0;
  768. }
  769. static void __net_exit genl_pernet_exit(struct net *net)
  770. {
  771. netlink_kernel_release(net->genl_sock);
  772. net->genl_sock = NULL;
  773. }
  774. static struct pernet_operations genl_pernet_ops = {
  775. .init = genl_pernet_init,
  776. .exit = genl_pernet_exit,
  777. };
  778. static int __init genl_init(void)
  779. {
  780. int i, err;
  781. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  782. INIT_LIST_HEAD(&family_ht[i]);
  783. err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
  784. if (err < 0)
  785. goto problem;
  786. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  787. err = register_pernet_subsys(&genl_pernet_ops);
  788. if (err)
  789. goto problem;
  790. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  791. if (err < 0)
  792. goto problem;
  793. return 0;
  794. problem:
  795. panic("GENL: Cannot register controller: %d\n", err);
  796. }
  797. subsys_initcall(genl_init);
  798. static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
  799. gfp_t flags)
  800. {
  801. struct sk_buff *tmp;
  802. struct net *net, *prev = NULL;
  803. int err;
  804. for_each_net_rcu(net) {
  805. if (prev) {
  806. tmp = skb_clone(skb, flags);
  807. if (!tmp) {
  808. err = -ENOMEM;
  809. goto error;
  810. }
  811. err = nlmsg_multicast(prev->genl_sock, tmp,
  812. pid, group, flags);
  813. if (err)
  814. goto error;
  815. }
  816. prev = net;
  817. }
  818. return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
  819. error:
  820. kfree_skb(skb);
  821. return err;
  822. }
  823. int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
  824. gfp_t flags)
  825. {
  826. return genlmsg_mcast(skb, pid, group, flags);
  827. }
  828. EXPORT_SYMBOL(genlmsg_multicast_allns);
  829. void genl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
  830. struct nlmsghdr *nlh, gfp_t flags)
  831. {
  832. struct sock *sk = net->genl_sock;
  833. int report = 0;
  834. if (nlh)
  835. report = nlmsg_report(nlh);
  836. nlmsg_notify(sk, skb, pid, group, report, flags);
  837. }
  838. EXPORT_SYMBOL(genl_notify);