genetlink.c 25 KB

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