genetlink.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/socket.h>
  13. #include <linux/string.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/mutex.h>
  16. #include <linux/bitmap.h>
  17. #include <net/sock.h>
  18. #include <net/genetlink.h>
  19. struct sock *genl_sock = NULL;
  20. static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
  21. static inline void genl_lock(void)
  22. {
  23. mutex_lock(&genl_mutex);
  24. }
  25. static inline void genl_unlock(void)
  26. {
  27. mutex_unlock(&genl_mutex);
  28. }
  29. #define GENL_FAM_TAB_SIZE 16
  30. #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
  31. static struct list_head family_ht[GENL_FAM_TAB_SIZE];
  32. /*
  33. * Bitmap of multicast groups that are currently in use.
  34. *
  35. * To avoid an allocation at boot of just one unsigned long,
  36. * declare it global instead.
  37. * Bit 0 is marked as already used since group 0 is invalid.
  38. */
  39. static unsigned long mc_group_start = 0x1;
  40. static unsigned long *mc_groups = &mc_group_start;
  41. static unsigned long mc_groups_longs = 1;
  42. static int genl_ctrl_event(int event, void *data);
  43. static inline unsigned int genl_family_hash(unsigned int id)
  44. {
  45. return id & GENL_FAM_TAB_MASK;
  46. }
  47. static inline struct list_head *genl_family_chain(unsigned int id)
  48. {
  49. return &family_ht[genl_family_hash(id)];
  50. }
  51. static struct genl_family *genl_family_find_byid(unsigned int id)
  52. {
  53. struct genl_family *f;
  54. list_for_each_entry(f, genl_family_chain(id), family_list)
  55. if (f->id == id)
  56. return f;
  57. return NULL;
  58. }
  59. static struct genl_family *genl_family_find_byname(char *name)
  60. {
  61. struct genl_family *f;
  62. int i;
  63. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  64. list_for_each_entry(f, genl_family_chain(i), family_list)
  65. if (strcmp(f->name, name) == 0)
  66. return f;
  67. return NULL;
  68. }
  69. static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
  70. {
  71. struct genl_ops *ops;
  72. list_for_each_entry(ops, &family->ops_list, ops_list)
  73. if (ops->cmd == cmd)
  74. return ops;
  75. return NULL;
  76. }
  77. /* Of course we are going to have problems once we hit
  78. * 2^16 alive types, but that can only happen by year 2K
  79. */
  80. static inline u16 genl_generate_id(void)
  81. {
  82. static u16 id_gen_idx;
  83. int overflowed = 0;
  84. do {
  85. if (id_gen_idx == 0)
  86. id_gen_idx = GENL_MIN_ID;
  87. if (++id_gen_idx > GENL_MAX_ID) {
  88. if (!overflowed) {
  89. overflowed = 1;
  90. id_gen_idx = 0;
  91. continue;
  92. } else
  93. return 0;
  94. }
  95. } while (genl_family_find_byid(id_gen_idx));
  96. return id_gen_idx;
  97. }
  98. static struct genl_multicast_group notify_grp;
  99. /**
  100. * genl_register_mc_group - register a multicast group
  101. *
  102. * Registers the specified multicast group and notifies userspace
  103. * about the new group.
  104. *
  105. * Returns 0 on success or a negative error code.
  106. *
  107. * @family: The generic netlink family the group shall be registered for.
  108. * @grp: The group to register, must have a name.
  109. */
  110. int genl_register_mc_group(struct genl_family *family,
  111. struct genl_multicast_group *grp)
  112. {
  113. int id;
  114. unsigned long *new_groups;
  115. int err;
  116. BUG_ON(grp->name[0] == '\0');
  117. genl_lock();
  118. /* special-case our own group */
  119. if (grp == &notify_grp)
  120. id = GENL_ID_CTRL;
  121. else
  122. id = find_first_zero_bit(mc_groups,
  123. mc_groups_longs * BITS_PER_LONG);
  124. if (id >= mc_groups_longs * BITS_PER_LONG) {
  125. size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
  126. if (mc_groups == &mc_group_start) {
  127. new_groups = kzalloc(nlen, GFP_KERNEL);
  128. if (!new_groups) {
  129. err = -ENOMEM;
  130. goto out;
  131. }
  132. mc_groups = new_groups;
  133. *mc_groups = mc_group_start;
  134. } else {
  135. new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
  136. if (!new_groups) {
  137. err = -ENOMEM;
  138. goto out;
  139. }
  140. mc_groups = new_groups;
  141. mc_groups[mc_groups_longs] = 0;
  142. }
  143. mc_groups_longs++;
  144. }
  145. err = netlink_change_ngroups(genl_sock,
  146. mc_groups_longs * BITS_PER_LONG);
  147. if (err)
  148. goto out;
  149. grp->id = id;
  150. set_bit(id, mc_groups);
  151. list_add_tail(&grp->list, &family->mcast_groups);
  152. grp->family = family;
  153. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
  154. out:
  155. genl_unlock();
  156. return err;
  157. }
  158. EXPORT_SYMBOL(genl_register_mc_group);
  159. static void __genl_unregister_mc_group(struct genl_family *family,
  160. struct genl_multicast_group *grp)
  161. {
  162. BUG_ON(grp->family != family);
  163. netlink_clear_multicast_users(genl_sock, grp->id);
  164. clear_bit(grp->id, mc_groups);
  165. list_del(&grp->list);
  166. genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
  167. grp->id = 0;
  168. grp->family = NULL;
  169. }
  170. /**
  171. * genl_unregister_mc_group - unregister a multicast group
  172. *
  173. * Unregisters the specified multicast group and notifies userspace
  174. * about it. All current listeners on the group are removed.
  175. *
  176. * Note: It is not necessary to unregister all multicast groups before
  177. * unregistering the family, unregistering the family will cause
  178. * all assigned multicast groups to be unregistered automatically.
  179. *
  180. * @family: Generic netlink family the group belongs to.
  181. * @grp: The group to unregister, must have been registered successfully
  182. * previously.
  183. */
  184. void genl_unregister_mc_group(struct genl_family *family,
  185. struct genl_multicast_group *grp)
  186. {
  187. genl_lock();
  188. __genl_unregister_mc_group(family, grp);
  189. genl_unlock();
  190. }
  191. EXPORT_SYMBOL(genl_unregister_mc_group);
  192. static void genl_unregister_mc_groups(struct genl_family *family)
  193. {
  194. struct genl_multicast_group *grp, *tmp;
  195. list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
  196. __genl_unregister_mc_group(family, grp);
  197. }
  198. /**
  199. * genl_register_ops - register generic netlink operations
  200. * @family: generic netlink family
  201. * @ops: operations to be registered
  202. *
  203. * Registers the specified operations and assigns them to the specified
  204. * family. Either a doit or dumpit callback must be specified or the
  205. * operation will fail. Only one operation structure per command
  206. * identifier may be registered.
  207. *
  208. * See include/net/genetlink.h for more documenation on the operations
  209. * structure.
  210. *
  211. * Returns 0 on success or a negative error code.
  212. */
  213. int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
  214. {
  215. int err = -EINVAL;
  216. if (ops->dumpit == NULL && ops->doit == NULL)
  217. goto errout;
  218. if (genl_get_cmd(ops->cmd, family)) {
  219. err = -EEXIST;
  220. goto errout;
  221. }
  222. if (ops->dumpit)
  223. ops->flags |= GENL_CMD_CAP_DUMP;
  224. if (ops->doit)
  225. ops->flags |= GENL_CMD_CAP_DO;
  226. if (ops->policy)
  227. ops->flags |= GENL_CMD_CAP_HASPOL;
  228. genl_lock();
  229. list_add_tail(&ops->ops_list, &family->ops_list);
  230. genl_unlock();
  231. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  232. err = 0;
  233. errout:
  234. return err;
  235. }
  236. /**
  237. * genl_unregister_ops - unregister generic netlink operations
  238. * @family: generic netlink family
  239. * @ops: operations to be unregistered
  240. *
  241. * Unregisters the specified operations and unassigns them from the
  242. * specified family. The operation blocks until the current message
  243. * processing has finished and doesn't start again until the
  244. * unregister process has finished.
  245. *
  246. * Note: It is not necessary to unregister all operations before
  247. * unregistering the family, unregistering the family will cause
  248. * all assigned operations to be unregistered automatically.
  249. *
  250. * Returns 0 on success or a negative error code.
  251. */
  252. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  253. {
  254. struct genl_ops *rc;
  255. genl_lock();
  256. list_for_each_entry(rc, &family->ops_list, ops_list) {
  257. if (rc == ops) {
  258. list_del(&ops->ops_list);
  259. genl_unlock();
  260. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  261. return 0;
  262. }
  263. }
  264. genl_unlock();
  265. return -ENOENT;
  266. }
  267. /**
  268. * genl_register_family - register a generic netlink family
  269. * @family: generic netlink family
  270. *
  271. * Registers the specified family after validating it first. Only one
  272. * family may be registered with the same family name or identifier.
  273. * The family id may equal GENL_ID_GENERATE causing an unique id to
  274. * be automatically generated and assigned.
  275. *
  276. * Return 0 on success or a negative error code.
  277. */
  278. int genl_register_family(struct genl_family *family)
  279. {
  280. int err = -EINVAL;
  281. if (family->id && family->id < GENL_MIN_ID)
  282. goto errout;
  283. if (family->id > GENL_MAX_ID)
  284. goto errout;
  285. INIT_LIST_HEAD(&family->ops_list);
  286. INIT_LIST_HEAD(&family->mcast_groups);
  287. genl_lock();
  288. if (genl_family_find_byname(family->name)) {
  289. err = -EEXIST;
  290. goto errout_locked;
  291. }
  292. if (genl_family_find_byid(family->id)) {
  293. err = -EEXIST;
  294. goto errout_locked;
  295. }
  296. if (family->id == GENL_ID_GENERATE) {
  297. u16 newid = genl_generate_id();
  298. if (!newid) {
  299. err = -ENOMEM;
  300. goto errout_locked;
  301. }
  302. family->id = newid;
  303. }
  304. if (family->maxattr) {
  305. family->attrbuf = kmalloc((family->maxattr+1) *
  306. sizeof(struct nlattr *), GFP_KERNEL);
  307. if (family->attrbuf == NULL) {
  308. err = -ENOMEM;
  309. goto errout_locked;
  310. }
  311. } else
  312. family->attrbuf = NULL;
  313. list_add_tail(&family->family_list, genl_family_chain(family->id));
  314. genl_unlock();
  315. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  316. return 0;
  317. errout_locked:
  318. genl_unlock();
  319. errout:
  320. return err;
  321. }
  322. /**
  323. * genl_register_family_with_ops - register a generic netlink family
  324. * @family: generic netlink family
  325. * @ops: operations to be registered
  326. * @n_ops: number of elements to register
  327. *
  328. * Registers the specified family and operations from the specified table.
  329. * Only one family may be registered with the same family name or identifier.
  330. *
  331. * The family id may equal GENL_ID_GENERATE causing an unique id to
  332. * be automatically generated and assigned.
  333. *
  334. * Either a doit or dumpit callback must be specified for every registered
  335. * operation or the function will fail. Only one operation structure per
  336. * command identifier may be registered.
  337. *
  338. * See include/net/genetlink.h for more documenation on the operations
  339. * structure.
  340. *
  341. * This is equivalent to calling genl_register_family() followed by
  342. * genl_register_ops() for every operation entry in the table taking
  343. * care to unregister the family on error path.
  344. *
  345. * Return 0 on success or a negative error code.
  346. */
  347. int genl_register_family_with_ops(struct genl_family *family,
  348. struct genl_ops *ops, size_t n_ops)
  349. {
  350. int err, i;
  351. err = genl_register_family(family);
  352. if (err)
  353. return err;
  354. for (i = 0; i < n_ops; ++i, ++ops) {
  355. err = genl_register_ops(family, ops);
  356. if (err)
  357. goto err_out;
  358. }
  359. return 0;
  360. err_out:
  361. genl_unregister_family(family);
  362. return err;
  363. }
  364. EXPORT_SYMBOL(genl_register_family_with_ops);
  365. /**
  366. * genl_unregister_family - unregister generic netlink family
  367. * @family: generic netlink family
  368. *
  369. * Unregisters the specified family.
  370. *
  371. * Returns 0 on success or a negative error code.
  372. */
  373. int genl_unregister_family(struct genl_family *family)
  374. {
  375. struct genl_family *rc;
  376. genl_lock();
  377. genl_unregister_mc_groups(family);
  378. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  379. if (family->id != rc->id || strcmp(rc->name, family->name))
  380. continue;
  381. list_del(&rc->family_list);
  382. INIT_LIST_HEAD(&family->ops_list);
  383. genl_unlock();
  384. kfree(family->attrbuf);
  385. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  386. return 0;
  387. }
  388. genl_unlock();
  389. return -ENOENT;
  390. }
  391. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  392. {
  393. struct genl_ops *ops;
  394. struct genl_family *family;
  395. struct genl_info info;
  396. struct genlmsghdr *hdr = nlmsg_data(nlh);
  397. int hdrlen, err;
  398. family = genl_family_find_byid(nlh->nlmsg_type);
  399. if (family == NULL)
  400. return -ENOENT;
  401. hdrlen = GENL_HDRLEN + family->hdrsize;
  402. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  403. return -EINVAL;
  404. ops = genl_get_cmd(hdr->cmd, family);
  405. if (ops == NULL)
  406. return -EOPNOTSUPP;
  407. if ((ops->flags & GENL_ADMIN_PERM) &&
  408. security_netlink_recv(skb, CAP_NET_ADMIN))
  409. return -EPERM;
  410. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  411. if (ops->dumpit == NULL)
  412. return -EOPNOTSUPP;
  413. genl_unlock();
  414. err = netlink_dump_start(genl_sock, skb, nlh,
  415. ops->dumpit, ops->done);
  416. genl_lock();
  417. return err;
  418. }
  419. if (ops->doit == NULL)
  420. return -EOPNOTSUPP;
  421. if (family->attrbuf) {
  422. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  423. ops->policy);
  424. if (err < 0)
  425. return err;
  426. }
  427. info.snd_seq = nlh->nlmsg_seq;
  428. info.snd_pid = NETLINK_CB(skb).pid;
  429. info.nlhdr = nlh;
  430. info.genlhdr = nlmsg_data(nlh);
  431. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  432. info.attrs = family->attrbuf;
  433. return ops->doit(skb, &info);
  434. }
  435. static void genl_rcv(struct sk_buff *skb)
  436. {
  437. genl_lock();
  438. netlink_rcv_skb(skb, &genl_rcv_msg);
  439. genl_unlock();
  440. }
  441. /**************************************************************************
  442. * Controller
  443. **************************************************************************/
  444. static struct genl_family genl_ctrl = {
  445. .id = GENL_ID_CTRL,
  446. .name = "nlctrl",
  447. .version = 0x2,
  448. .maxattr = CTRL_ATTR_MAX,
  449. };
  450. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  451. u32 flags, struct sk_buff *skb, u8 cmd)
  452. {
  453. void *hdr;
  454. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  455. if (hdr == NULL)
  456. return -1;
  457. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  458. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  459. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  460. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  461. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  462. if (!list_empty(&family->ops_list)) {
  463. struct nlattr *nla_ops;
  464. struct genl_ops *ops;
  465. int idx = 1;
  466. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  467. if (nla_ops == NULL)
  468. goto nla_put_failure;
  469. list_for_each_entry(ops, &family->ops_list, ops_list) {
  470. struct nlattr *nest;
  471. nest = nla_nest_start(skb, idx++);
  472. if (nest == NULL)
  473. goto nla_put_failure;
  474. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  475. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  476. nla_nest_end(skb, nest);
  477. }
  478. nla_nest_end(skb, nla_ops);
  479. }
  480. if (!list_empty(&family->mcast_groups)) {
  481. struct genl_multicast_group *grp;
  482. struct nlattr *nla_grps;
  483. int idx = 1;
  484. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  485. if (nla_grps == NULL)
  486. goto nla_put_failure;
  487. list_for_each_entry(grp, &family->mcast_groups, list) {
  488. struct nlattr *nest;
  489. nest = nla_nest_start(skb, idx++);
  490. if (nest == NULL)
  491. goto nla_put_failure;
  492. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  493. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  494. grp->name);
  495. nla_nest_end(skb, nest);
  496. }
  497. nla_nest_end(skb, nla_grps);
  498. }
  499. return genlmsg_end(skb, hdr);
  500. nla_put_failure:
  501. genlmsg_cancel(skb, hdr);
  502. return -EMSGSIZE;
  503. }
  504. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  505. u32 seq, u32 flags, struct sk_buff *skb,
  506. u8 cmd)
  507. {
  508. void *hdr;
  509. struct nlattr *nla_grps;
  510. struct nlattr *nest;
  511. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  512. if (hdr == NULL)
  513. return -1;
  514. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  515. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  516. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  517. if (nla_grps == NULL)
  518. goto nla_put_failure;
  519. nest = nla_nest_start(skb, 1);
  520. if (nest == NULL)
  521. goto nla_put_failure;
  522. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  523. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  524. grp->name);
  525. nla_nest_end(skb, nest);
  526. nla_nest_end(skb, nla_grps);
  527. return genlmsg_end(skb, hdr);
  528. nla_put_failure:
  529. genlmsg_cancel(skb, hdr);
  530. return -EMSGSIZE;
  531. }
  532. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  533. {
  534. int i, n = 0;
  535. struct genl_family *rt;
  536. int chains_to_skip = cb->args[0];
  537. int fams_to_skip = cb->args[1];
  538. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  539. if (i < chains_to_skip)
  540. continue;
  541. n = 0;
  542. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  543. if (++n < fams_to_skip)
  544. continue;
  545. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  546. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  547. skb, CTRL_CMD_NEWFAMILY) < 0)
  548. goto errout;
  549. }
  550. fams_to_skip = 0;
  551. }
  552. errout:
  553. cb->args[0] = i;
  554. cb->args[1] = n;
  555. return skb->len;
  556. }
  557. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  558. u32 pid, int seq, u8 cmd)
  559. {
  560. struct sk_buff *skb;
  561. int err;
  562. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  563. if (skb == NULL)
  564. return ERR_PTR(-ENOBUFS);
  565. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  566. if (err < 0) {
  567. nlmsg_free(skb);
  568. return ERR_PTR(err);
  569. }
  570. return skb;
  571. }
  572. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  573. u32 pid, int seq, u8 cmd)
  574. {
  575. struct sk_buff *skb;
  576. int err;
  577. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  578. if (skb == NULL)
  579. return ERR_PTR(-ENOBUFS);
  580. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  581. if (err < 0) {
  582. nlmsg_free(skb);
  583. return ERR_PTR(err);
  584. }
  585. return skb;
  586. }
  587. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  588. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  589. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  590. .len = GENL_NAMSIZ - 1 },
  591. };
  592. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  593. {
  594. struct sk_buff *msg;
  595. struct genl_family *res = NULL;
  596. int err = -EINVAL;
  597. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  598. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  599. res = genl_family_find_byid(id);
  600. }
  601. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  602. char *name;
  603. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  604. res = genl_family_find_byname(name);
  605. }
  606. if (res == NULL) {
  607. err = -ENOENT;
  608. goto errout;
  609. }
  610. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  611. CTRL_CMD_NEWFAMILY);
  612. if (IS_ERR(msg)) {
  613. err = PTR_ERR(msg);
  614. goto errout;
  615. }
  616. err = genlmsg_reply(msg, info);
  617. errout:
  618. return err;
  619. }
  620. static int genl_ctrl_event(int event, void *data)
  621. {
  622. struct sk_buff *msg;
  623. if (genl_sock == NULL)
  624. return 0;
  625. switch (event) {
  626. case CTRL_CMD_NEWFAMILY:
  627. case CTRL_CMD_DELFAMILY:
  628. msg = ctrl_build_family_msg(data, 0, 0, event);
  629. if (IS_ERR(msg))
  630. return PTR_ERR(msg);
  631. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  632. break;
  633. case CTRL_CMD_NEWMCAST_GRP:
  634. case CTRL_CMD_DELMCAST_GRP:
  635. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  636. if (IS_ERR(msg))
  637. return PTR_ERR(msg);
  638. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  639. break;
  640. }
  641. return 0;
  642. }
  643. static struct genl_ops genl_ctrl_ops = {
  644. .cmd = CTRL_CMD_GETFAMILY,
  645. .doit = ctrl_getfamily,
  646. .dumpit = ctrl_dumpfamily,
  647. .policy = ctrl_policy,
  648. };
  649. static struct genl_multicast_group notify_grp = {
  650. .name = "notify",
  651. };
  652. static int __init genl_init(void)
  653. {
  654. int i, err;
  655. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  656. INIT_LIST_HEAD(&family_ht[i]);
  657. err = genl_register_family(&genl_ctrl);
  658. if (err < 0)
  659. goto errout;
  660. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  661. if (err < 0)
  662. goto errout_register;
  663. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  664. /* we'll bump the group number right afterwards */
  665. genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0,
  666. genl_rcv, &genl_mutex, THIS_MODULE);
  667. if (genl_sock == NULL)
  668. panic("GENL: Cannot initialize generic netlink\n");
  669. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  670. if (err < 0)
  671. goto errout_register;
  672. return 0;
  673. errout_register:
  674. genl_unregister_family(&genl_ctrl);
  675. errout:
  676. panic("GENL: Cannot register controller: %d\n", err);
  677. }
  678. subsys_initcall(genl_init);
  679. EXPORT_SYMBOL(genl_sock);
  680. EXPORT_SYMBOL(genl_register_ops);
  681. EXPORT_SYMBOL(genl_unregister_ops);
  682. EXPORT_SYMBOL(genl_register_family);
  683. EXPORT_SYMBOL(genl_unregister_family);