genetlink.c 18 KB

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