genetlink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. .data = ops,
  515. .dump = genl_lock_dumpit,
  516. .done = genl_lock_done,
  517. };
  518. genl_unlock();
  519. rc = netlink_dump_start(net->genl_sock, skb, nlh, &c);
  520. genl_lock();
  521. } else {
  522. struct netlink_dump_control c = {
  523. .dump = ops->dumpit,
  524. .done = ops->done,
  525. };
  526. rc = netlink_dump_start(net->genl_sock, skb, nlh, &c);
  527. }
  528. return rc;
  529. }
  530. if (ops->doit == NULL)
  531. return -EOPNOTSUPP;
  532. if (family->maxattr && family->parallel_ops) {
  533. attrbuf = kmalloc((family->maxattr+1) *
  534. sizeof(struct nlattr *), GFP_KERNEL);
  535. if (attrbuf == NULL)
  536. return -ENOMEM;
  537. } else
  538. attrbuf = family->attrbuf;
  539. if (attrbuf) {
  540. err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
  541. ops->policy);
  542. if (err < 0)
  543. goto out;
  544. }
  545. info.snd_seq = nlh->nlmsg_seq;
  546. info.snd_portid = NETLINK_CB(skb).portid;
  547. info.nlhdr = nlh;
  548. info.genlhdr = nlmsg_data(nlh);
  549. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  550. info.attrs = attrbuf;
  551. genl_info_net_set(&info, net);
  552. memset(&info.user_ptr, 0, sizeof(info.user_ptr));
  553. if (family->pre_doit) {
  554. err = family->pre_doit(ops, skb, &info);
  555. if (err)
  556. goto out;
  557. }
  558. err = ops->doit(skb, &info);
  559. if (family->post_doit)
  560. family->post_doit(ops, skb, &info);
  561. out:
  562. if (family->parallel_ops)
  563. kfree(attrbuf);
  564. return err;
  565. }
  566. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  567. {
  568. struct genl_family *family;
  569. int err;
  570. family = genl_family_find_byid(nlh->nlmsg_type);
  571. if (family == NULL)
  572. return -ENOENT;
  573. if (!family->parallel_ops)
  574. genl_lock();
  575. err = genl_family_rcv_msg(family, skb, nlh);
  576. if (!family->parallel_ops)
  577. genl_unlock();
  578. return err;
  579. }
  580. static void genl_rcv(struct sk_buff *skb)
  581. {
  582. down_read(&cb_lock);
  583. netlink_rcv_skb(skb, &genl_rcv_msg);
  584. up_read(&cb_lock);
  585. }
  586. /**************************************************************************
  587. * Controller
  588. **************************************************************************/
  589. static struct genl_family genl_ctrl = {
  590. .id = GENL_ID_CTRL,
  591. .name = "nlctrl",
  592. .version = 0x2,
  593. .maxattr = CTRL_ATTR_MAX,
  594. .netnsok = true,
  595. };
  596. static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
  597. u32 flags, struct sk_buff *skb, u8 cmd)
  598. {
  599. void *hdr;
  600. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  601. if (hdr == NULL)
  602. return -1;
  603. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
  604. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
  605. nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
  606. nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
  607. nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
  608. goto nla_put_failure;
  609. if (!list_empty(&family->ops_list)) {
  610. struct nlattr *nla_ops;
  611. struct genl_ops *ops;
  612. int idx = 1;
  613. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  614. if (nla_ops == NULL)
  615. goto nla_put_failure;
  616. list_for_each_entry(ops, &family->ops_list, ops_list) {
  617. struct nlattr *nest;
  618. nest = nla_nest_start(skb, idx++);
  619. if (nest == NULL)
  620. goto nla_put_failure;
  621. if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
  622. nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
  623. goto nla_put_failure;
  624. nla_nest_end(skb, nest);
  625. }
  626. nla_nest_end(skb, nla_ops);
  627. }
  628. if (!list_empty(&family->mcast_groups)) {
  629. struct genl_multicast_group *grp;
  630. struct nlattr *nla_grps;
  631. int idx = 1;
  632. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  633. if (nla_grps == NULL)
  634. goto nla_put_failure;
  635. list_for_each_entry(grp, &family->mcast_groups, list) {
  636. struct nlattr *nest;
  637. nest = nla_nest_start(skb, idx++);
  638. if (nest == NULL)
  639. goto nla_put_failure;
  640. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  641. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  642. grp->name))
  643. goto nla_put_failure;
  644. nla_nest_end(skb, nest);
  645. }
  646. nla_nest_end(skb, nla_grps);
  647. }
  648. return genlmsg_end(skb, hdr);
  649. nla_put_failure:
  650. genlmsg_cancel(skb, hdr);
  651. return -EMSGSIZE;
  652. }
  653. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
  654. u32 seq, u32 flags, struct sk_buff *skb,
  655. u8 cmd)
  656. {
  657. void *hdr;
  658. struct nlattr *nla_grps;
  659. struct nlattr *nest;
  660. hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
  661. if (hdr == NULL)
  662. return -1;
  663. if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
  664. nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
  665. goto nla_put_failure;
  666. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  667. if (nla_grps == NULL)
  668. goto nla_put_failure;
  669. nest = nla_nest_start(skb, 1);
  670. if (nest == NULL)
  671. goto nla_put_failure;
  672. if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
  673. nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
  674. grp->name))
  675. goto nla_put_failure;
  676. nla_nest_end(skb, nest);
  677. nla_nest_end(skb, nla_grps);
  678. return genlmsg_end(skb, hdr);
  679. nla_put_failure:
  680. genlmsg_cancel(skb, hdr);
  681. return -EMSGSIZE;
  682. }
  683. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  684. {
  685. int i, n = 0;
  686. struct genl_family *rt;
  687. struct net *net = sock_net(skb->sk);
  688. int chains_to_skip = cb->args[0];
  689. int fams_to_skip = cb->args[1];
  690. for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
  691. n = 0;
  692. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  693. if (!rt->netnsok && !net_eq(net, &init_net))
  694. continue;
  695. if (++n < fams_to_skip)
  696. continue;
  697. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
  698. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  699. skb, CTRL_CMD_NEWFAMILY) < 0)
  700. goto errout;
  701. }
  702. fams_to_skip = 0;
  703. }
  704. errout:
  705. cb->args[0] = i;
  706. cb->args[1] = n;
  707. return skb->len;
  708. }
  709. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  710. u32 portid, int seq, u8 cmd)
  711. {
  712. struct sk_buff *skb;
  713. int err;
  714. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  715. if (skb == NULL)
  716. return ERR_PTR(-ENOBUFS);
  717. err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
  718. if (err < 0) {
  719. nlmsg_free(skb);
  720. return ERR_PTR(err);
  721. }
  722. return skb;
  723. }
  724. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  725. u32 portid, int seq, u8 cmd)
  726. {
  727. struct sk_buff *skb;
  728. int err;
  729. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  730. if (skb == NULL)
  731. return ERR_PTR(-ENOBUFS);
  732. err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
  733. if (err < 0) {
  734. nlmsg_free(skb);
  735. return ERR_PTR(err);
  736. }
  737. return skb;
  738. }
  739. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  740. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  741. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  742. .len = GENL_NAMSIZ - 1 },
  743. };
  744. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  745. {
  746. struct sk_buff *msg;
  747. struct genl_family *res = NULL;
  748. int err = -EINVAL;
  749. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  750. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  751. res = genl_family_find_byid(id);
  752. err = -ENOENT;
  753. }
  754. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  755. char *name;
  756. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  757. res = genl_family_find_byname(name);
  758. #ifdef CONFIG_MODULES
  759. if (res == NULL) {
  760. genl_unlock();
  761. up_read(&cb_lock);
  762. request_module("net-pf-%d-proto-%d-family-%s",
  763. PF_NETLINK, NETLINK_GENERIC, name);
  764. down_read(&cb_lock);
  765. genl_lock();
  766. res = genl_family_find_byname(name);
  767. }
  768. #endif
  769. err = -ENOENT;
  770. }
  771. if (res == NULL)
  772. return err;
  773. if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
  774. /* family doesn't exist here */
  775. return -ENOENT;
  776. }
  777. msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
  778. CTRL_CMD_NEWFAMILY);
  779. if (IS_ERR(msg))
  780. return PTR_ERR(msg);
  781. return genlmsg_reply(msg, info);
  782. }
  783. static int genl_ctrl_event(int event, void *data)
  784. {
  785. struct sk_buff *msg;
  786. struct genl_family *family;
  787. struct genl_multicast_group *grp;
  788. /* genl is still initialising */
  789. if (!init_net.genl_sock)
  790. return 0;
  791. switch (event) {
  792. case CTRL_CMD_NEWFAMILY:
  793. case CTRL_CMD_DELFAMILY:
  794. family = data;
  795. msg = ctrl_build_family_msg(family, 0, 0, event);
  796. break;
  797. case CTRL_CMD_NEWMCAST_GRP:
  798. case CTRL_CMD_DELMCAST_GRP:
  799. grp = data;
  800. family = grp->family;
  801. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  802. break;
  803. default:
  804. return -EINVAL;
  805. }
  806. if (IS_ERR(msg))
  807. return PTR_ERR(msg);
  808. if (!family->netnsok) {
  809. genlmsg_multicast_netns(&init_net, msg, 0,
  810. GENL_ID_CTRL, GFP_KERNEL);
  811. } else {
  812. rcu_read_lock();
  813. genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
  814. rcu_read_unlock();
  815. }
  816. return 0;
  817. }
  818. static struct genl_ops genl_ctrl_ops = {
  819. .cmd = CTRL_CMD_GETFAMILY,
  820. .doit = ctrl_getfamily,
  821. .dumpit = ctrl_dumpfamily,
  822. .policy = ctrl_policy,
  823. };
  824. static struct genl_multicast_group notify_grp = {
  825. .name = "notify",
  826. };
  827. static int __net_init genl_pernet_init(struct net *net)
  828. {
  829. struct netlink_kernel_cfg cfg = {
  830. .input = genl_rcv,
  831. .flags = NL_CFG_F_NONROOT_RECV,
  832. };
  833. /* we'll bump the group number right afterwards */
  834. net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
  835. if (!net->genl_sock && net_eq(net, &init_net))
  836. panic("GENL: Cannot initialize generic netlink\n");
  837. if (!net->genl_sock)
  838. return -ENOMEM;
  839. return 0;
  840. }
  841. static void __net_exit genl_pernet_exit(struct net *net)
  842. {
  843. netlink_kernel_release(net->genl_sock);
  844. net->genl_sock = NULL;
  845. }
  846. static struct pernet_operations genl_pernet_ops = {
  847. .init = genl_pernet_init,
  848. .exit = genl_pernet_exit,
  849. };
  850. static int __init genl_init(void)
  851. {
  852. int i, err;
  853. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  854. INIT_LIST_HEAD(&family_ht[i]);
  855. err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
  856. if (err < 0)
  857. goto problem;
  858. err = register_pernet_subsys(&genl_pernet_ops);
  859. if (err)
  860. goto problem;
  861. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  862. if (err < 0)
  863. goto problem;
  864. return 0;
  865. problem:
  866. panic("GENL: Cannot register controller: %d\n", err);
  867. }
  868. subsys_initcall(genl_init);
  869. static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
  870. gfp_t flags)
  871. {
  872. struct sk_buff *tmp;
  873. struct net *net, *prev = NULL;
  874. int err;
  875. for_each_net_rcu(net) {
  876. if (prev) {
  877. tmp = skb_clone(skb, flags);
  878. if (!tmp) {
  879. err = -ENOMEM;
  880. goto error;
  881. }
  882. err = nlmsg_multicast(prev->genl_sock, tmp,
  883. portid, group, flags);
  884. if (err)
  885. goto error;
  886. }
  887. prev = net;
  888. }
  889. return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
  890. error:
  891. kfree_skb(skb);
  892. return err;
  893. }
  894. int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
  895. gfp_t flags)
  896. {
  897. return genlmsg_mcast(skb, portid, group, flags);
  898. }
  899. EXPORT_SYMBOL(genlmsg_multicast_allns);
  900. void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
  901. struct nlmsghdr *nlh, gfp_t flags)
  902. {
  903. struct sock *sk = net->genl_sock;
  904. int report = 0;
  905. if (nlh)
  906. report = nlmsg_report(nlh);
  907. nlmsg_notify(sk, skb, portid, group, report, flags);
  908. }
  909. EXPORT_SYMBOL(genl_notify);