genetlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 void genl_lock(void)
  22. {
  23. mutex_lock(&genl_mutex);
  24. }
  25. static int genl_trylock(void)
  26. {
  27. return !mutex_trylock(&genl_mutex);
  28. }
  29. static void genl_unlock(void)
  30. {
  31. mutex_unlock(&genl_mutex);
  32. if (genl_sock && genl_sock->sk_receive_queue.qlen)
  33. genl_sock->sk_data_ready(genl_sock, 0);
  34. }
  35. #define GENL_FAM_TAB_SIZE 16
  36. #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
  37. static struct list_head family_ht[GENL_FAM_TAB_SIZE];
  38. /*
  39. * Bitmap of multicast groups that are currently in use.
  40. *
  41. * To avoid an allocation at boot of just one unsigned long,
  42. * declare it global instead.
  43. * Bit 0 is marked as already used since group 0 is invalid.
  44. */
  45. static unsigned long mc_group_start = 0x1;
  46. static unsigned long *mc_groups = &mc_group_start;
  47. static unsigned long mc_groups_longs = 1;
  48. static int genl_ctrl_event(int event, void *data);
  49. static inline unsigned int genl_family_hash(unsigned int id)
  50. {
  51. return id & GENL_FAM_TAB_MASK;
  52. }
  53. static inline struct list_head *genl_family_chain(unsigned int id)
  54. {
  55. return &family_ht[genl_family_hash(id)];
  56. }
  57. static struct genl_family *genl_family_find_byid(unsigned int id)
  58. {
  59. struct genl_family *f;
  60. list_for_each_entry(f, genl_family_chain(id), family_list)
  61. if (f->id == id)
  62. return f;
  63. return NULL;
  64. }
  65. static struct genl_family *genl_family_find_byname(char *name)
  66. {
  67. struct genl_family *f;
  68. int i;
  69. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  70. list_for_each_entry(f, genl_family_chain(i), family_list)
  71. if (strcmp(f->name, name) == 0)
  72. return f;
  73. return NULL;
  74. }
  75. static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
  76. {
  77. struct genl_ops *ops;
  78. list_for_each_entry(ops, &family->ops_list, ops_list)
  79. if (ops->cmd == cmd)
  80. return ops;
  81. return NULL;
  82. }
  83. /* Of course we are going to have problems once we hit
  84. * 2^16 alive types, but that can only happen by year 2K
  85. */
  86. static inline u16 genl_generate_id(void)
  87. {
  88. static u16 id_gen_idx;
  89. int overflowed = 0;
  90. do {
  91. if (id_gen_idx == 0)
  92. id_gen_idx = GENL_MIN_ID;
  93. if (++id_gen_idx > GENL_MAX_ID) {
  94. if (!overflowed) {
  95. overflowed = 1;
  96. id_gen_idx = 0;
  97. continue;
  98. } else
  99. return 0;
  100. }
  101. } while (genl_family_find_byid(id_gen_idx));
  102. return id_gen_idx;
  103. }
  104. static struct genl_multicast_group notify_grp;
  105. /**
  106. * genl_register_mc_group - register a multicast group
  107. *
  108. * Registers the specified multicast group and notifies userspace
  109. * about the new group.
  110. *
  111. * Returns 0 on success or a negative error code.
  112. *
  113. * @family: The generic netlink family the group shall be registered for.
  114. * @grp: The group to register, must have a name.
  115. */
  116. int genl_register_mc_group(struct genl_family *family,
  117. struct genl_multicast_group *grp)
  118. {
  119. int id;
  120. unsigned long *new_groups;
  121. int err;
  122. BUG_ON(grp->name[0] == '\0');
  123. genl_lock();
  124. /* special-case our own group */
  125. if (grp == &notify_grp)
  126. id = GENL_ID_CTRL;
  127. else
  128. id = find_first_zero_bit(mc_groups,
  129. mc_groups_longs * BITS_PER_LONG);
  130. if (id >= mc_groups_longs * BITS_PER_LONG) {
  131. size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
  132. if (mc_groups == &mc_group_start) {
  133. new_groups = kzalloc(nlen, GFP_KERNEL);
  134. if (!new_groups) {
  135. err = -ENOMEM;
  136. goto out;
  137. }
  138. mc_groups = new_groups;
  139. *mc_groups = mc_group_start;
  140. } else {
  141. new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
  142. if (!new_groups) {
  143. err = -ENOMEM;
  144. goto out;
  145. }
  146. mc_groups = new_groups;
  147. mc_groups[mc_groups_longs] = 0;
  148. }
  149. mc_groups_longs++;
  150. }
  151. err = netlink_change_ngroups(genl_sock,
  152. sizeof(unsigned long) * NETLINK_GENERIC);
  153. if (err)
  154. goto out;
  155. grp->id = id;
  156. set_bit(id, mc_groups);
  157. list_add_tail(&grp->list, &family->mcast_groups);
  158. grp->family = family;
  159. genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
  160. out:
  161. genl_unlock();
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(genl_register_mc_group);
  165. /**
  166. * genl_unregister_mc_group - unregister a multicast group
  167. *
  168. * Unregisters the specified multicast group and notifies userspace
  169. * about it. All current listeners on the group are removed.
  170. *
  171. * Note: It is not necessary to unregister all multicast groups before
  172. * unregistering the family, unregistering the family will cause
  173. * all assigned multicast groups to be unregistered automatically.
  174. *
  175. * @family: Generic netlink family the group belongs to.
  176. * @grp: The group to unregister, must have been registered successfully
  177. * previously.
  178. */
  179. void genl_unregister_mc_group(struct genl_family *family,
  180. struct genl_multicast_group *grp)
  181. {
  182. BUG_ON(grp->family != family);
  183. genl_lock();
  184. netlink_clear_multicast_users(genl_sock, grp->id);
  185. clear_bit(grp->id, mc_groups);
  186. list_del(&grp->list);
  187. genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
  188. grp->id = 0;
  189. grp->family = NULL;
  190. genl_unlock();
  191. }
  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_unregister_family - unregister generic netlink family
  324. * @family: generic netlink family
  325. *
  326. * Unregisters the specified family.
  327. *
  328. * Returns 0 on success or a negative error code.
  329. */
  330. int genl_unregister_family(struct genl_family *family)
  331. {
  332. struct genl_family *rc;
  333. genl_unregister_mc_groups(family);
  334. genl_lock();
  335. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  336. if (family->id != rc->id || strcmp(rc->name, family->name))
  337. continue;
  338. list_del(&rc->family_list);
  339. INIT_LIST_HEAD(&family->ops_list);
  340. genl_unlock();
  341. kfree(family->attrbuf);
  342. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  343. return 0;
  344. }
  345. genl_unlock();
  346. return -ENOENT;
  347. }
  348. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  349. {
  350. struct genl_ops *ops;
  351. struct genl_family *family;
  352. struct genl_info info;
  353. struct genlmsghdr *hdr = nlmsg_data(nlh);
  354. int hdrlen, err;
  355. family = genl_family_find_byid(nlh->nlmsg_type);
  356. if (family == NULL)
  357. return -ENOENT;
  358. hdrlen = GENL_HDRLEN + family->hdrsize;
  359. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  360. return -EINVAL;
  361. ops = genl_get_cmd(hdr->cmd, family);
  362. if (ops == NULL)
  363. return -EOPNOTSUPP;
  364. if ((ops->flags & GENL_ADMIN_PERM) &&
  365. security_netlink_recv(skb, CAP_NET_ADMIN))
  366. return -EPERM;
  367. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  368. if (ops->dumpit == NULL)
  369. return -EOPNOTSUPP;
  370. return netlink_dump_start(genl_sock, skb, nlh,
  371. ops->dumpit, ops->done);
  372. }
  373. if (ops->doit == NULL)
  374. return -EOPNOTSUPP;
  375. if (family->attrbuf) {
  376. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  377. ops->policy);
  378. if (err < 0)
  379. return err;
  380. }
  381. info.snd_seq = nlh->nlmsg_seq;
  382. info.snd_pid = NETLINK_CB(skb).pid;
  383. info.nlhdr = nlh;
  384. info.genlhdr = nlmsg_data(nlh);
  385. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  386. info.attrs = family->attrbuf;
  387. return ops->doit(skb, &info);
  388. }
  389. static void genl_rcv(struct sock *sk, int len)
  390. {
  391. unsigned int qlen = 0;
  392. do {
  393. if (genl_trylock())
  394. return;
  395. netlink_run_queue(sk, &qlen, genl_rcv_msg);
  396. genl_unlock();
  397. } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
  398. }
  399. /**************************************************************************
  400. * Controller
  401. **************************************************************************/
  402. static struct genl_family genl_ctrl = {
  403. .id = GENL_ID_CTRL,
  404. .name = "nlctrl",
  405. .version = 0x2,
  406. .maxattr = CTRL_ATTR_MAX,
  407. };
  408. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  409. u32 flags, struct sk_buff *skb, u8 cmd)
  410. {
  411. void *hdr;
  412. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  413. if (hdr == NULL)
  414. return -1;
  415. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  416. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  417. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  418. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  419. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  420. if (!list_empty(&family->ops_list)) {
  421. struct nlattr *nla_ops;
  422. struct genl_ops *ops;
  423. int idx = 1;
  424. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  425. if (nla_ops == NULL)
  426. goto nla_put_failure;
  427. list_for_each_entry(ops, &family->ops_list, ops_list) {
  428. struct nlattr *nest;
  429. nest = nla_nest_start(skb, idx++);
  430. if (nest == NULL)
  431. goto nla_put_failure;
  432. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  433. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  434. nla_nest_end(skb, nest);
  435. }
  436. nla_nest_end(skb, nla_ops);
  437. }
  438. if (!list_empty(&family->mcast_groups)) {
  439. struct genl_multicast_group *grp;
  440. struct nlattr *nla_grps;
  441. int idx = 1;
  442. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  443. if (nla_grps == NULL)
  444. goto nla_put_failure;
  445. list_for_each_entry(grp, &family->mcast_groups, list) {
  446. struct nlattr *nest;
  447. nest = nla_nest_start(skb, idx++);
  448. if (nest == NULL)
  449. goto nla_put_failure;
  450. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  451. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  452. grp->name);
  453. nla_nest_end(skb, nest);
  454. }
  455. nla_nest_end(skb, nla_grps);
  456. }
  457. return genlmsg_end(skb, hdr);
  458. nla_put_failure:
  459. return genlmsg_cancel(skb, hdr);
  460. }
  461. static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
  462. u32 seq, u32 flags, struct sk_buff *skb,
  463. u8 cmd)
  464. {
  465. void *hdr;
  466. struct nlattr *nla_grps;
  467. struct nlattr *nest;
  468. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  469. if (hdr == NULL)
  470. return -1;
  471. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
  472. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
  473. nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
  474. if (nla_grps == NULL)
  475. goto nla_put_failure;
  476. nest = nla_nest_start(skb, 1);
  477. if (nest == NULL)
  478. goto nla_put_failure;
  479. NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
  480. NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
  481. grp->name);
  482. nla_nest_end(skb, nest);
  483. nla_nest_end(skb, nla_grps);
  484. return genlmsg_end(skb, hdr);
  485. nla_put_failure:
  486. return genlmsg_cancel(skb, hdr);
  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. if (chains_to_skip != 0)
  495. genl_lock();
  496. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  497. if (i < chains_to_skip)
  498. continue;
  499. n = 0;
  500. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  501. if (++n < fams_to_skip)
  502. continue;
  503. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  504. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  505. skb, CTRL_CMD_NEWFAMILY) < 0)
  506. goto errout;
  507. }
  508. fams_to_skip = 0;
  509. }
  510. errout:
  511. if (chains_to_skip != 0)
  512. genl_unlock();
  513. cb->args[0] = i;
  514. cb->args[1] = n;
  515. return skb->len;
  516. }
  517. static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
  518. u32 pid, int seq, u8 cmd)
  519. {
  520. struct sk_buff *skb;
  521. int err;
  522. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  523. if (skb == NULL)
  524. return ERR_PTR(-ENOBUFS);
  525. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  526. if (err < 0) {
  527. nlmsg_free(skb);
  528. return ERR_PTR(err);
  529. }
  530. return skb;
  531. }
  532. static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
  533. u32 pid, int seq, u8 cmd)
  534. {
  535. struct sk_buff *skb;
  536. int err;
  537. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  538. if (skb == NULL)
  539. return ERR_PTR(-ENOBUFS);
  540. err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
  541. if (err < 0) {
  542. nlmsg_free(skb);
  543. return ERR_PTR(err);
  544. }
  545. return skb;
  546. }
  547. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  548. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  549. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  550. .len = GENL_NAMSIZ - 1 },
  551. };
  552. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  553. {
  554. struct sk_buff *msg;
  555. struct genl_family *res = NULL;
  556. int err = -EINVAL;
  557. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  558. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  559. res = genl_family_find_byid(id);
  560. }
  561. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  562. char *name;
  563. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  564. res = genl_family_find_byname(name);
  565. }
  566. if (res == NULL) {
  567. err = -ENOENT;
  568. goto errout;
  569. }
  570. msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
  571. CTRL_CMD_NEWFAMILY);
  572. if (IS_ERR(msg)) {
  573. err = PTR_ERR(msg);
  574. goto errout;
  575. }
  576. err = genlmsg_reply(msg, info);
  577. errout:
  578. return err;
  579. }
  580. static int genl_ctrl_event(int event, void *data)
  581. {
  582. struct sk_buff *msg;
  583. if (genl_sock == NULL)
  584. return 0;
  585. switch (event) {
  586. case CTRL_CMD_NEWFAMILY:
  587. case CTRL_CMD_DELFAMILY:
  588. msg = ctrl_build_family_msg(data, 0, 0, event);
  589. if (IS_ERR(msg))
  590. return PTR_ERR(msg);
  591. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  592. break;
  593. case CTRL_CMD_NEWMCAST_GRP:
  594. case CTRL_CMD_DELMCAST_GRP:
  595. msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
  596. if (IS_ERR(msg))
  597. return PTR_ERR(msg);
  598. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  599. break;
  600. }
  601. return 0;
  602. }
  603. static struct genl_ops genl_ctrl_ops = {
  604. .cmd = CTRL_CMD_GETFAMILY,
  605. .doit = ctrl_getfamily,
  606. .dumpit = ctrl_dumpfamily,
  607. .policy = ctrl_policy,
  608. };
  609. static struct genl_multicast_group notify_grp = {
  610. .name = "notify",
  611. };
  612. static int __init genl_init(void)
  613. {
  614. int i, err;
  615. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  616. INIT_LIST_HEAD(&family_ht[i]);
  617. err = genl_register_family(&genl_ctrl);
  618. if (err < 0)
  619. goto errout;
  620. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  621. if (err < 0)
  622. goto errout_register;
  623. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  624. /* we'll bump the group number right afterwards */
  625. genl_sock = netlink_kernel_create(NETLINK_GENERIC, 0, genl_rcv,
  626. NULL, THIS_MODULE);
  627. if (genl_sock == NULL)
  628. panic("GENL: Cannot initialize generic netlink\n");
  629. err = genl_register_mc_group(&genl_ctrl, &notify_grp);
  630. if (err < 0)
  631. goto errout_register;
  632. return 0;
  633. errout_register:
  634. genl_unregister_family(&genl_ctrl);
  635. errout:
  636. panic("GENL: Cannot register controller: %d\n", err);
  637. }
  638. subsys_initcall(genl_init);
  639. EXPORT_SYMBOL(genl_sock);
  640. EXPORT_SYMBOL(genl_register_ops);
  641. EXPORT_SYMBOL(genl_unregister_ops);
  642. EXPORT_SYMBOL(genl_register_family);
  643. EXPORT_SYMBOL(genl_unregister_family);