genetlink.c 18 KB

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