genetlink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. int *errp)
  242. {
  243. struct genl_ops *ops;
  244. struct genl_family *family;
  245. struct genl_info info;
  246. struct genlmsghdr *hdr = nlmsg_data(nlh);
  247. int hdrlen, err = -EINVAL;
  248. family = genl_family_find_byid(nlh->nlmsg_type);
  249. if (family == NULL) {
  250. err = -ENOENT;
  251. goto errout;
  252. }
  253. hdrlen = GENL_HDRLEN + family->hdrsize;
  254. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  255. goto errout;
  256. ops = genl_get_cmd(hdr->cmd, family);
  257. if (ops == NULL) {
  258. err = -EOPNOTSUPP;
  259. goto errout;
  260. }
  261. if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb, CAP_NET_ADMIN)) {
  262. err = -EPERM;
  263. goto errout;
  264. }
  265. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  266. if (ops->dumpit == NULL) {
  267. err = -EOPNOTSUPP;
  268. goto errout;
  269. }
  270. *errp = err = netlink_dump_start(genl_sock, skb, nlh,
  271. ops->dumpit, ops->done);
  272. if (err == 0)
  273. skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
  274. skb->len));
  275. return -1;
  276. }
  277. if (ops->doit == NULL) {
  278. err = -EOPNOTSUPP;
  279. goto errout;
  280. }
  281. if (family->attrbuf) {
  282. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  283. ops->policy);
  284. if (err < 0)
  285. goto errout;
  286. }
  287. info.snd_seq = nlh->nlmsg_seq;
  288. info.snd_pid = NETLINK_CB(skb).pid;
  289. info.nlhdr = nlh;
  290. info.genlhdr = nlmsg_data(nlh);
  291. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  292. info.attrs = family->attrbuf;
  293. *errp = err = ops->doit(skb, &info);
  294. return err;
  295. errout:
  296. *errp = err;
  297. return -1;
  298. }
  299. static void genl_rcv(struct sock *sk, int len)
  300. {
  301. unsigned int qlen = 0;
  302. do {
  303. if (genl_trylock())
  304. return;
  305. netlink_run_queue(sk, &qlen, genl_rcv_msg);
  306. genl_unlock();
  307. } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
  308. }
  309. /**************************************************************************
  310. * Controller
  311. **************************************************************************/
  312. static struct genl_family genl_ctrl = {
  313. .id = GENL_ID_CTRL,
  314. .name = "nlctrl",
  315. .version = 0x2,
  316. .maxattr = CTRL_ATTR_MAX,
  317. };
  318. static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  319. u32 flags, struct sk_buff *skb, u8 cmd)
  320. {
  321. void *hdr;
  322. hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
  323. if (hdr == NULL)
  324. return -1;
  325. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  326. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  327. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  328. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  329. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  330. if (!list_empty(&family->ops_list)) {
  331. struct nlattr *nla_ops;
  332. struct genl_ops *ops;
  333. int idx = 1;
  334. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  335. if (nla_ops == NULL)
  336. goto nla_put_failure;
  337. list_for_each_entry(ops, &family->ops_list, ops_list) {
  338. struct nlattr *nest;
  339. nest = nla_nest_start(skb, idx++);
  340. if (nest == NULL)
  341. goto nla_put_failure;
  342. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  343. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  344. nla_nest_end(skb, nest);
  345. }
  346. nla_nest_end(skb, nla_ops);
  347. }
  348. return genlmsg_end(skb, hdr);
  349. nla_put_failure:
  350. return genlmsg_cancel(skb, hdr);
  351. }
  352. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  353. {
  354. int i, n = 0;
  355. struct genl_family *rt;
  356. int chains_to_skip = cb->args[0];
  357. int fams_to_skip = cb->args[1];
  358. if (chains_to_skip != 0)
  359. genl_lock();
  360. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  361. if (i < chains_to_skip)
  362. continue;
  363. n = 0;
  364. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  365. if (++n < fams_to_skip)
  366. continue;
  367. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  368. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  369. skb, CTRL_CMD_NEWFAMILY) < 0)
  370. goto errout;
  371. }
  372. fams_to_skip = 0;
  373. }
  374. errout:
  375. if (chains_to_skip != 0)
  376. genl_unlock();
  377. cb->args[0] = i;
  378. cb->args[1] = n;
  379. return skb->len;
  380. }
  381. static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
  382. int seq, u8 cmd)
  383. {
  384. struct sk_buff *skb;
  385. int err;
  386. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  387. if (skb == NULL)
  388. return ERR_PTR(-ENOBUFS);
  389. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  390. if (err < 0) {
  391. nlmsg_free(skb);
  392. return ERR_PTR(err);
  393. }
  394. return skb;
  395. }
  396. static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
  397. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  398. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  399. .len = GENL_NAMSIZ - 1 },
  400. };
  401. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  402. {
  403. struct sk_buff *msg;
  404. struct genl_family *res = NULL;
  405. int err = -EINVAL;
  406. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  407. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  408. res = genl_family_find_byid(id);
  409. }
  410. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  411. char *name;
  412. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  413. res = genl_family_find_byname(name);
  414. }
  415. if (res == NULL) {
  416. err = -ENOENT;
  417. goto errout;
  418. }
  419. msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
  420. CTRL_CMD_NEWFAMILY);
  421. if (IS_ERR(msg)) {
  422. err = PTR_ERR(msg);
  423. goto errout;
  424. }
  425. err = genlmsg_reply(msg, info);
  426. errout:
  427. return err;
  428. }
  429. static int genl_ctrl_event(int event, void *data)
  430. {
  431. struct sk_buff *msg;
  432. if (genl_sock == NULL)
  433. return 0;
  434. switch (event) {
  435. case CTRL_CMD_NEWFAMILY:
  436. case CTRL_CMD_DELFAMILY:
  437. msg = ctrl_build_msg(data, 0, 0, event);
  438. if (IS_ERR(msg))
  439. return PTR_ERR(msg);
  440. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  441. break;
  442. }
  443. return 0;
  444. }
  445. static struct genl_ops genl_ctrl_ops = {
  446. .cmd = CTRL_CMD_GETFAMILY,
  447. .doit = ctrl_getfamily,
  448. .dumpit = ctrl_dumpfamily,
  449. .policy = ctrl_policy,
  450. };
  451. static int __init genl_init(void)
  452. {
  453. int i, err;
  454. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  455. INIT_LIST_HEAD(&family_ht[i]);
  456. err = genl_register_family(&genl_ctrl);
  457. if (err < 0)
  458. goto errout;
  459. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  460. if (err < 0)
  461. goto errout_register;
  462. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  463. genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
  464. genl_rcv, THIS_MODULE);
  465. if (genl_sock == NULL)
  466. panic("GENL: Cannot initialize generic netlink\n");
  467. return 0;
  468. errout_register:
  469. genl_unregister_family(&genl_ctrl);
  470. errout:
  471. panic("GENL: Cannot register controller: %d\n", err);
  472. }
  473. subsys_initcall(genl_init);
  474. EXPORT_SYMBOL(genl_sock);
  475. EXPORT_SYMBOL(genl_register_ops);
  476. EXPORT_SYMBOL(genl_unregister_ops);
  477. EXPORT_SYMBOL(genl_register_family);
  478. EXPORT_SYMBOL(genl_unregister_family);