genetlink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * NETLINK Generic Netlink Family
  3. *
  4. * Authors: Jamal Hadi Salim
  5. * Thomas Graf <tgraf@suug.ch>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/socket.h>
  12. #include <linux/string.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/mutex.h>
  15. #include <net/sock.h>
  16. #include <net/genetlink.h>
  17. struct sock *genl_sock = NULL;
  18. static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
  19. static void genl_lock(void)
  20. {
  21. mutex_lock(&genl_mutex);
  22. }
  23. static int genl_trylock(void)
  24. {
  25. return !mutex_trylock(&genl_mutex);
  26. }
  27. static void genl_unlock(void)
  28. {
  29. mutex_unlock(&genl_mutex);
  30. if (genl_sock && genl_sock->sk_receive_queue.qlen)
  31. genl_sock->sk_data_ready(genl_sock, 0);
  32. }
  33. #define GENL_FAM_TAB_SIZE 16
  34. #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
  35. static struct list_head family_ht[GENL_FAM_TAB_SIZE];
  36. static int genl_ctrl_event(int event, void *data);
  37. static inline unsigned int genl_family_hash(unsigned int id)
  38. {
  39. return id & GENL_FAM_TAB_MASK;
  40. }
  41. static inline struct list_head *genl_family_chain(unsigned int id)
  42. {
  43. return &family_ht[genl_family_hash(id)];
  44. }
  45. static struct genl_family *genl_family_find_byid(unsigned int id)
  46. {
  47. struct genl_family *f;
  48. list_for_each_entry(f, genl_family_chain(id), family_list)
  49. if (f->id == id)
  50. return f;
  51. return NULL;
  52. }
  53. static struct genl_family *genl_family_find_byname(char *name)
  54. {
  55. struct genl_family *f;
  56. int i;
  57. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  58. list_for_each_entry(f, genl_family_chain(i), family_list)
  59. if (strcmp(f->name, name) == 0)
  60. return f;
  61. return NULL;
  62. }
  63. static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
  64. {
  65. struct genl_ops *ops;
  66. list_for_each_entry(ops, &family->ops_list, ops_list)
  67. if (ops->cmd == cmd)
  68. return ops;
  69. return NULL;
  70. }
  71. /* Of course we are going to have problems once we hit
  72. * 2^16 alive types, but that can only happen by year 2K
  73. */
  74. static inline u16 genl_generate_id(void)
  75. {
  76. static u16 id_gen_idx;
  77. int overflowed = 0;
  78. do {
  79. if (id_gen_idx == 0)
  80. id_gen_idx = GENL_MIN_ID;
  81. if (++id_gen_idx > GENL_MAX_ID) {
  82. if (!overflowed) {
  83. overflowed = 1;
  84. id_gen_idx = 0;
  85. continue;
  86. } else
  87. return 0;
  88. }
  89. } while (genl_family_find_byid(id_gen_idx));
  90. return id_gen_idx;
  91. }
  92. /**
  93. * genl_register_ops - register generic netlink operations
  94. * @family: generic netlink family
  95. * @ops: operations to be registered
  96. *
  97. * Registers the specified operations and assigns them to the specified
  98. * family. Either a doit or dumpit callback must be specified or the
  99. * operation will fail. Only one operation structure per command
  100. * identifier may be registered.
  101. *
  102. * See include/net/genetlink.h for more documenation on the operations
  103. * structure.
  104. *
  105. * Returns 0 on success or a negative error code.
  106. */
  107. int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
  108. {
  109. int err = -EINVAL;
  110. if (ops->dumpit == NULL && ops->doit == NULL)
  111. goto errout;
  112. if (genl_get_cmd(ops->cmd, family)) {
  113. err = -EEXIST;
  114. goto errout;
  115. }
  116. if (ops->dumpit)
  117. ops->flags |= GENL_CMD_CAP_DUMP;
  118. if (ops->doit)
  119. ops->flags |= GENL_CMD_CAP_DO;
  120. if (ops->policy)
  121. ops->flags |= GENL_CMD_CAP_HASPOL;
  122. genl_lock();
  123. list_add_tail(&ops->ops_list, &family->ops_list);
  124. genl_unlock();
  125. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  126. err = 0;
  127. errout:
  128. return err;
  129. }
  130. /**
  131. * genl_unregister_ops - unregister generic netlink operations
  132. * @family: generic netlink family
  133. * @ops: operations to be unregistered
  134. *
  135. * Unregisters the specified operations and unassigns them from the
  136. * specified family. The operation blocks until the current message
  137. * processing has finished and doesn't start again until the
  138. * unregister process has finished.
  139. *
  140. * Note: It is not necessary to unregister all operations before
  141. * unregistering the family, unregistering the family will cause
  142. * all assigned operations to be unregistered automatically.
  143. *
  144. * Returns 0 on success or a negative error code.
  145. */
  146. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  147. {
  148. struct genl_ops *rc;
  149. genl_lock();
  150. list_for_each_entry(rc, &family->ops_list, ops_list) {
  151. if (rc == ops) {
  152. list_del(&ops->ops_list);
  153. genl_unlock();
  154. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  155. return 0;
  156. }
  157. }
  158. genl_unlock();
  159. return -ENOENT;
  160. }
  161. /**
  162. * genl_register_family - register a generic netlink family
  163. * @family: generic netlink family
  164. *
  165. * Registers the specified family after validating it first. Only one
  166. * family may be registered with the same family name or identifier.
  167. * The family id may equal GENL_ID_GENERATE causing an unique id to
  168. * be automatically generated and assigned.
  169. *
  170. * Return 0 on success or a negative error code.
  171. */
  172. int genl_register_family(struct genl_family *family)
  173. {
  174. int err = -EINVAL;
  175. if (family->id && family->id < GENL_MIN_ID)
  176. goto errout;
  177. if (family->id > GENL_MAX_ID)
  178. goto errout;
  179. INIT_LIST_HEAD(&family->ops_list);
  180. genl_lock();
  181. if (genl_family_find_byname(family->name)) {
  182. err = -EEXIST;
  183. goto errout_locked;
  184. }
  185. if (genl_family_find_byid(family->id)) {
  186. err = -EEXIST;
  187. goto errout_locked;
  188. }
  189. if (family->id == GENL_ID_GENERATE) {
  190. u16 newid = genl_generate_id();
  191. if (!newid) {
  192. err = -ENOMEM;
  193. goto errout_locked;
  194. }
  195. family->id = newid;
  196. }
  197. if (family->maxattr) {
  198. family->attrbuf = kmalloc((family->maxattr+1) *
  199. sizeof(struct nlattr *), GFP_KERNEL);
  200. if (family->attrbuf == NULL) {
  201. err = -ENOMEM;
  202. goto errout_locked;
  203. }
  204. } else
  205. family->attrbuf = NULL;
  206. list_add_tail(&family->family_list, genl_family_chain(family->id));
  207. genl_unlock();
  208. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  209. return 0;
  210. errout_locked:
  211. genl_unlock();
  212. errout:
  213. return err;
  214. }
  215. /**
  216. * genl_unregister_family - unregister generic netlink family
  217. * @family: generic netlink family
  218. *
  219. * Unregisters the specified family.
  220. *
  221. * Returns 0 on success or a negative error code.
  222. */
  223. int genl_unregister_family(struct genl_family *family)
  224. {
  225. struct genl_family *rc;
  226. genl_lock();
  227. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  228. if (family->id != rc->id || strcmp(rc->name, family->name))
  229. continue;
  230. list_del(&rc->family_list);
  231. INIT_LIST_HEAD(&family->ops_list);
  232. genl_unlock();
  233. kfree(family->attrbuf);
  234. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  235. return 0;
  236. }
  237. genl_unlock();
  238. return -ENOENT;
  239. }
  240. static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  241. {
  242. struct genl_ops *ops;
  243. struct genl_family *family;
  244. struct genl_info info;
  245. struct genlmsghdr *hdr = nlmsg_data(nlh);
  246. int hdrlen, err;
  247. family = genl_family_find_byid(nlh->nlmsg_type);
  248. if (family == NULL)
  249. return -ENOENT;
  250. hdrlen = GENL_HDRLEN + family->hdrsize;
  251. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  252. return -EINVAL;
  253. ops = genl_get_cmd(hdr->cmd, family);
  254. if (ops == NULL)
  255. return -EOPNOTSUPP;
  256. if ((ops->flags & GENL_ADMIN_PERM) &&
  257. security_netlink_recv(skb, CAP_NET_ADMIN))
  258. return -EPERM;
  259. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  260. if (ops->dumpit == NULL)
  261. return -EOPNOTSUPP;
  262. return netlink_dump_start(genl_sock, skb, nlh,
  263. ops->dumpit, ops->done);
  264. }
  265. if (ops->doit == NULL)
  266. return -EOPNOTSUPP;
  267. if (family->attrbuf) {
  268. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  269. ops->policy);
  270. if (err < 0)
  271. return err;
  272. }
  273. info.snd_seq = nlh->nlmsg_seq;
  274. info.snd_pid = NETLINK_CB(skb).pid;
  275. info.nlhdr = nlh;
  276. info.genlhdr = nlmsg_data(nlh);
  277. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  278. info.attrs = family->attrbuf;
  279. return ops->doit(skb, &info);
  280. }
  281. static void genl_rcv(struct sock *sk, int len)
  282. {
  283. unsigned int qlen = 0;
  284. do {
  285. if (genl_trylock())
  286. return;
  287. netlink_run_queue(sk, &qlen, genl_rcv_msg);
  288. genl_unlock();
  289. } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
  290. }
  291. /**************************************************************************
  292. * Controller
  293. **************************************************************************/
  294. static struct genl_family genl_ctrl = {
  295. .id = GENL_ID_CTRL,
  296. .name = "nlctrl",
  297. .version = 0x2,
  298. .maxattr = CTRL_ATTR_MAX,
  299. };
  300. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  301. u32 flags, struct sk_buff *skb, u8 cmd)
  302. {
  303. void *hdr;
  304. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  305. if (hdr == NULL)
  306. return -1;
  307. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  308. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  309. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  310. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  311. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  312. if (!list_empty(&family->ops_list)) {
  313. struct nlattr *nla_ops;
  314. struct genl_ops *ops;
  315. int idx = 1;
  316. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  317. if (nla_ops == NULL)
  318. goto nla_put_failure;
  319. list_for_each_entry(ops, &family->ops_list, ops_list) {
  320. struct nlattr *nest;
  321. nest = nla_nest_start(skb, idx++);
  322. if (nest == NULL)
  323. goto nla_put_failure;
  324. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  325. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  326. nla_nest_end(skb, nest);
  327. }
  328. nla_nest_end(skb, nla_ops);
  329. }
  330. return genlmsg_end(skb, hdr);
  331. nla_put_failure:
  332. return genlmsg_cancel(skb, hdr);
  333. }
  334. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  335. {
  336. int i, n = 0;
  337. struct genl_family *rt;
  338. int chains_to_skip = cb->args[0];
  339. int fams_to_skip = cb->args[1];
  340. if (chains_to_skip != 0)
  341. genl_lock();
  342. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  343. if (i < chains_to_skip)
  344. continue;
  345. n = 0;
  346. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  347. if (++n < fams_to_skip)
  348. continue;
  349. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  350. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  351. skb, CTRL_CMD_NEWFAMILY) < 0)
  352. goto errout;
  353. }
  354. fams_to_skip = 0;
  355. }
  356. errout:
  357. if (chains_to_skip != 0)
  358. genl_unlock();
  359. cb->args[0] = i;
  360. cb->args[1] = n;
  361. return skb->len;
  362. }
  363. static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
  364. int seq, u8 cmd)
  365. {
  366. struct sk_buff *skb;
  367. int err;
  368. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  369. if (skb == NULL)
  370. return ERR_PTR(-ENOBUFS);
  371. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  372. if (err < 0) {
  373. nlmsg_free(skb);
  374. return ERR_PTR(err);
  375. }
  376. return skb;
  377. }
  378. static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  379. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  380. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  381. .len = GENL_NAMSIZ - 1 },
  382. };
  383. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  384. {
  385. struct sk_buff *msg;
  386. struct genl_family *res = NULL;
  387. int err = -EINVAL;
  388. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  389. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  390. res = genl_family_find_byid(id);
  391. }
  392. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  393. char *name;
  394. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  395. res = genl_family_find_byname(name);
  396. }
  397. if (res == NULL) {
  398. err = -ENOENT;
  399. goto errout;
  400. }
  401. msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
  402. CTRL_CMD_NEWFAMILY);
  403. if (IS_ERR(msg)) {
  404. err = PTR_ERR(msg);
  405. goto errout;
  406. }
  407. err = genlmsg_reply(msg, info);
  408. errout:
  409. return err;
  410. }
  411. static int genl_ctrl_event(int event, void *data)
  412. {
  413. struct sk_buff *msg;
  414. if (genl_sock == NULL)
  415. return 0;
  416. switch (event) {
  417. case CTRL_CMD_NEWFAMILY:
  418. case CTRL_CMD_DELFAMILY:
  419. msg = ctrl_build_msg(data, 0, 0, event);
  420. if (IS_ERR(msg))
  421. return PTR_ERR(msg);
  422. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  423. break;
  424. }
  425. return 0;
  426. }
  427. static struct genl_ops genl_ctrl_ops = {
  428. .cmd = CTRL_CMD_GETFAMILY,
  429. .doit = ctrl_getfamily,
  430. .dumpit = ctrl_dumpfamily,
  431. .policy = ctrl_policy,
  432. };
  433. static int __init genl_init(void)
  434. {
  435. int i, err;
  436. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  437. INIT_LIST_HEAD(&family_ht[i]);
  438. err = genl_register_family(&genl_ctrl);
  439. if (err < 0)
  440. goto errout;
  441. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  442. if (err < 0)
  443. goto errout_register;
  444. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  445. genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
  446. genl_rcv, NULL, THIS_MODULE);
  447. if (genl_sock == NULL)
  448. panic("GENL: Cannot initialize generic netlink\n");
  449. return 0;
  450. errout_register:
  451. genl_unregister_family(&genl_ctrl);
  452. errout:
  453. panic("GENL: Cannot register controller: %d\n", err);
  454. }
  455. subsys_initcall(genl_init);
  456. EXPORT_SYMBOL(genl_sock);
  457. EXPORT_SYMBOL(genl_register_ops);
  458. EXPORT_SYMBOL(genl_unregister_ops);
  459. EXPORT_SYMBOL(genl_register_family);
  460. EXPORT_SYMBOL(genl_unregister_family);