genetlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. static void genl_unregister_mc_groups(struct genl_family *family)
  192. {
  193. struct genl_multicast_group *grp, *tmp;
  194. list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
  195. __genl_unregister_mc_group(family, grp);
  196. }
  197. /**
  198. * genl_register_ops - register generic netlink operations
  199. * @family: generic netlink family
  200. * @ops: operations to be registered
  201. *
  202. * Registers the specified operations and assigns them to the specified
  203. * family. Either a doit or dumpit callback must be specified or the
  204. * operation will fail. Only one operation structure per command
  205. * identifier may be registered.
  206. *
  207. * See include/net/genetlink.h for more documenation on the operations
  208. * structure.
  209. *
  210. * Returns 0 on success or a negative error code.
  211. */
  212. int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
  213. {
  214. int err = -EINVAL;
  215. if (ops->dumpit == NULL && ops->doit == NULL)
  216. goto errout;
  217. if (genl_get_cmd(ops->cmd, family)) {
  218. err = -EEXIST;
  219. goto errout;
  220. }
  221. if (ops->dumpit)
  222. ops->flags |= GENL_CMD_CAP_DUMP;
  223. if (ops->doit)
  224. ops->flags |= GENL_CMD_CAP_DO;
  225. if (ops->policy)
  226. ops->flags |= GENL_CMD_CAP_HASPOL;
  227. genl_lock();
  228. list_add_tail(&ops->ops_list, &family->ops_list);
  229. genl_unlock();
  230. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  231. err = 0;
  232. errout:
  233. return err;
  234. }
  235. /**
  236. * genl_unregister_ops - unregister generic netlink operations
  237. * @family: generic netlink family
  238. * @ops: operations to be unregistered
  239. *
  240. * Unregisters the specified operations and unassigns them from the
  241. * specified family. The operation blocks until the current message
  242. * processing has finished and doesn't start again until the
  243. * unregister process has finished.
  244. *
  245. * Note: It is not necessary to unregister all operations before
  246. * unregistering the family, unregistering the family will cause
  247. * all assigned operations to be unregistered automatically.
  248. *
  249. * Returns 0 on success or a negative error code.
  250. */
  251. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  252. {
  253. struct genl_ops *rc;
  254. genl_lock();
  255. list_for_each_entry(rc, &family->ops_list, ops_list) {
  256. if (rc == ops) {
  257. list_del(&ops->ops_list);
  258. genl_unlock();
  259. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  260. return 0;
  261. }
  262. }
  263. genl_unlock();
  264. return -ENOENT;
  265. }
  266. /**
  267. * genl_register_family - register a generic netlink family
  268. * @family: generic netlink family
  269. *
  270. * Registers the specified family after validating it first. Only one
  271. * family may be registered with the same family name or identifier.
  272. * The family id may equal GENL_ID_GENERATE causing an unique id to
  273. * be automatically generated and assigned.
  274. *
  275. * Return 0 on success or a negative error code.
  276. */
  277. int genl_register_family(struct genl_family *family)
  278. {
  279. int err = -EINVAL;
  280. if (family->id && family->id < GENL_MIN_ID)
  281. goto errout;
  282. if (family->id > GENL_MAX_ID)
  283. goto errout;
  284. INIT_LIST_HEAD(&family->ops_list);
  285. INIT_LIST_HEAD(&family->mcast_groups);
  286. genl_lock();
  287. if (genl_family_find_byname(family->name)) {
  288. err = -EEXIST;
  289. goto errout_locked;
  290. }
  291. if (genl_family_find_byid(family->id)) {
  292. err = -EEXIST;
  293. goto errout_locked;
  294. }
  295. if (family->id == GENL_ID_GENERATE) {
  296. u16 newid = genl_generate_id();
  297. if (!newid) {
  298. err = -ENOMEM;
  299. goto errout_locked;
  300. }
  301. family->id = newid;
  302. }
  303. if (family->maxattr) {
  304. family->attrbuf = kmalloc((family->maxattr+1) *
  305. sizeof(struct nlattr *), GFP_KERNEL);
  306. if (family->attrbuf == NULL) {
  307. err = -ENOMEM;
  308. goto errout_locked;
  309. }
  310. } else
  311. family->attrbuf = NULL;
  312. list_add_tail(&family->family_list, genl_family_chain(family->id));
  313. genl_unlock();
  314. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  315. return 0;
  316. errout_locked:
  317. genl_unlock();
  318. errout:
  319. return err;
  320. }
  321. /**
  322. * genl_unregister_family - unregister generic netlink family
  323. * @family: generic netlink family
  324. *
  325. * Unregisters the specified family.
  326. *
  327. * Returns 0 on success or a negative error code.
  328. */
  329. int genl_unregister_family(struct genl_family *family)
  330. {
  331. struct genl_family *rc;
  332. genl_lock();
  333. genl_unregister_mc_groups(family);
  334. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  335. if (family->id != rc->id || strcmp(rc->name, family->name))
  336. continue;
  337. list_del(&rc->family_list);
  338. INIT_LIST_HEAD(&family->ops_list);
  339. genl_unlock();
  340. kfree(family->attrbuf);
  341. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  342. return 0;
  343. }
  344. genl_unlock();
  345. return -ENOENT;
  346. }
  347. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  348. {
  349. struct genl_ops *ops;
  350. struct genl_family *family;
  351. struct genl_info info;
  352. struct genlmsghdr *hdr = nlmsg_data(nlh);
  353. int hdrlen, err;
  354. family = genl_family_find_byid(nlh->nlmsg_type);
  355. if (family == NULL)
  356. return -ENOENT;
  357. hdrlen = GENL_HDRLEN + family->hdrsize;
  358. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  359. return -EINVAL;
  360. ops = genl_get_cmd(hdr->cmd, family);
  361. if (ops == NULL)
  362. return -EOPNOTSUPP;
  363. if ((ops->flags & GENL_ADMIN_PERM) &&
  364. security_netlink_recv(skb, CAP_NET_ADMIN))
  365. return -EPERM;
  366. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  367. if (ops->dumpit == NULL)
  368. return -EOPNOTSUPP;
  369. genl_unlock();
  370. err = netlink_dump_start(genl_sock, skb, nlh,
  371. ops->dumpit, ops->done);
  372. genl_lock();
  373. return err;
  374. }
  375. if (ops->doit == NULL)
  376. return -EOPNOTSUPP;
  377. if (family->attrbuf) {
  378. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  379. ops->policy);
  380. if (err < 0)
  381. return err;
  382. }
  383. info.snd_seq = nlh->nlmsg_seq;
  384. info.snd_pid = NETLINK_CB(skb).pid;
  385. info.nlhdr = nlh;
  386. info.genlhdr = nlmsg_data(nlh);
  387. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  388. info.attrs = family->attrbuf;
  389. return ops->doit(skb, &info);
  390. }
  391. static void genl_rcv(struct sk_buff *skb)
  392. {
  393. genl_lock();
  394. netlink_rcv_skb(skb, &genl_rcv_msg);
  395. genl_unlock();
  396. }
  397. /**************************************************************************
  398. * Controller
  399. **************************************************************************/
  400. static struct genl_family genl_ctrl = {
  401. .id = GENL_ID_CTRL,
  402. .name = "nlctrl",
  403. .version = 0x2,
  404. .maxattr = CTRL_ATTR_MAX,
  405. };
  406. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  407. u32 flags, struct sk_buff *skb, u8 cmd)
  408. {
  409. void *hdr;
  410. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  411. if (hdr == NULL)
  412. return -1;
  413. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  414. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  415. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  416. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  417. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  418. if (!list_empty(&family->ops_list)) {
  419. struct nlattr *nla_ops;
  420. struct genl_ops *ops;
  421. int idx = 1;
  422. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  423. if (nla_ops == NULL)
  424. goto nla_put_failure;
  425. list_for_each_entry(ops, &family->ops_list, ops_list) {
  426. struct nlattr *nest;
  427. nest = nla_nest_start(skb, idx++);
  428. if (nest == NULL)
  429. goto nla_put_failure;
  430. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  431. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  432. nla_nest_end(skb, nest);
  433. }
  434. nla_nest_end(skb, nla_ops);
  435. }
  436. if (!list_empty(&family->mcast_groups)) {
  437. struct genl_multicast_group *grp;
  438. struct nlattr *nla_grps;
  439. int idx = 1;
  440. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  441. if (nla_grps == NULL)
  442. goto nla_put_failure;
  443. list_for_each_entry(grp, &family->mcast_groups, list) {
  444. struct nlattr *nest;
  445. nest = nla_nest_start(skb, idx++);
  446. if (nest == NULL)
  447. goto nla_put_failure;
  448. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  449. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  450. grp->name);
  451. nla_nest_end(skb, nest);
  452. }
  453. nla_nest_end(skb, nla_grps);
  454. }
  455. return genlmsg_end(skb, hdr);
  456. nla_put_failure:
  457. genlmsg_cancel(skb, hdr);
  458. return -EMSGSIZE;
  459. }
  460. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  461. u32 seq, u32 flags, struct sk_buff *skb,
  462. u8 cmd)
  463. {
  464. void *hdr;
  465. struct nlattr *nla_grps;
  466. struct nlattr *nest;
  467. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  468. if (hdr == NULL)
  469. return -1;
  470. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  471. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  472. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  473. if (nla_grps == NULL)
  474. goto nla_put_failure;
  475. nest = nla_nest_start(skb, 1);
  476. if (nest == NULL)
  477. goto nla_put_failure;
  478. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  479. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  480. grp->name);
  481. nla_nest_end(skb, nest);
  482. nla_nest_end(skb, nla_grps);
  483. return genlmsg_end(skb, hdr);
  484. nla_put_failure:
  485. genlmsg_cancel(skb, hdr);
  486. return -EMSGSIZE;
  487. }
  488. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  489. {
  490. int i, n = 0;
  491. struct genl_family *rt;
  492. int chains_to_skip = cb->args[0];
  493. int fams_to_skip = cb->args[1];
  494. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  495. if (i < chains_to_skip)
  496. continue;
  497. n = 0;
  498. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  499. if (++n < fams_to_skip)
  500. continue;
  501. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  502. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  503. skb, CTRL_CMD_NEWFAMILY) < 0)
  504. goto errout;
  505. }
  506. fams_to_skip = 0;
  507. }
  508. errout:
  509. cb->args[0] = i;
  510. cb->args[1] = n;
  511. return skb->len;
  512. }
  513. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  514. u32 pid, int seq, u8 cmd)
  515. {
  516. struct sk_buff *skb;
  517. int err;
  518. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  519. if (skb == NULL)
  520. return ERR_PTR(-ENOBUFS);
  521. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  522. if (err < 0) {
  523. nlmsg_free(skb);
  524. return ERR_PTR(err);
  525. }
  526. return skb;
  527. }
  528. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  529. u32 pid, int seq, u8 cmd)
  530. {
  531. struct sk_buff *skb;
  532. int err;
  533. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  534. if (skb == NULL)
  535. return ERR_PTR(-ENOBUFS);
  536. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  537. if (err < 0) {
  538. nlmsg_free(skb);
  539. return ERR_PTR(err);
  540. }
  541. return skb;
  542. }
  543. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  544. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  545. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  546. .len = GENL_NAMSIZ - 1 },
  547. };
  548. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  549. {
  550. struct sk_buff *msg;
  551. struct genl_family *res = NULL;
  552. int err = -EINVAL;
  553. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  554. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  555. res = genl_family_find_byid(id);
  556. }
  557. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  558. char *name;
  559. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  560. res = genl_family_find_byname(name);
  561. }
  562. if (res == NULL) {
  563. err = -ENOENT;
  564. goto errout;
  565. }
  566. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  567. CTRL_CMD_NEWFAMILY);
  568. if (IS_ERR(msg)) {
  569. err = PTR_ERR(msg);
  570. goto errout;
  571. }
  572. err = genlmsg_reply(msg, info);
  573. errout:
  574. return err;
  575. }
  576. static int genl_ctrl_event(int event, void *data)
  577. {
  578. struct sk_buff *msg;
  579. if (genl_sock == NULL)
  580. return 0;
  581. switch (event) {
  582. case CTRL_CMD_NEWFAMILY:
  583. case CTRL_CMD_DELFAMILY:
  584. msg = ctrl_build_family_msg(data, 0, 0, event);
  585. if (IS_ERR(msg))
  586. return PTR_ERR(msg);
  587. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  588. break;
  589. case CTRL_CMD_NEWMCAST_GRP:
  590. case CTRL_CMD_DELMCAST_GRP:
  591. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  592. if (IS_ERR(msg))
  593. return PTR_ERR(msg);
  594. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  595. break;
  596. }
  597. return 0;
  598. }
  599. static struct genl_ops genl_ctrl_ops = {
  600. .cmd = CTRL_CMD_GETFAMILY,
  601. .doit = ctrl_getfamily,
  602. .dumpit = ctrl_dumpfamily,
  603. .policy = ctrl_policy,
  604. };
  605. static struct genl_multicast_group notify_grp = {
  606. .name = "notify",
  607. };
  608. static int __init genl_init(void)
  609. {
  610. int i, err;
  611. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  612. INIT_LIST_HEAD(&family_ht[i]);
  613. err = genl_register_family(&genl_ctrl);
  614. if (err < 0)
  615. goto errout;
  616. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  617. if (err < 0)
  618. goto errout_register;
  619. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  620. /* we'll bump the group number right afterwards */
  621. genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0,
  622. genl_rcv, &genl_mutex, THIS_MODULE);
  623. if (genl_sock == NULL)
  624. panic("GENL: Cannot initialize generic netlink\n");
  625. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  626. if (err < 0)
  627. goto errout_register;
  628. return 0;
  629. errout_register:
  630. genl_unregister_family(&genl_ctrl);
  631. errout:
  632. panic("GENL: Cannot register controller: %d\n", err);
  633. }
  634. subsys_initcall(genl_init);
  635. EXPORT_SYMBOL(genl_sock);
  636. EXPORT_SYMBOL(genl_register_ops);
  637. EXPORT_SYMBOL(genl_unregister_ops);
  638. EXPORT_SYMBOL(genl_register_family);
  639. EXPORT_SYMBOL(genl_unregister_family);