genetlink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * NETLINK Generic Netlink Family
  3. *
  4. * Authors: Jamal Hadi Salim
  5. * Thomas Graf <tgraf@suug.ch>
  6. */
  7. #include <linux/config.h>
  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 <net/sock.h>
  16. #include <net/genetlink.h>
  17. struct sock *genl_sock = NULL;
  18. static DECLARE_MUTEX(genl_sem); /* serialization of message processing */
  19. static void genl_lock(void)
  20. {
  21. down(&genl_sem);
  22. }
  23. static int genl_trylock(void)
  24. {
  25. return down_trylock(&genl_sem);
  26. }
  27. static void genl_unlock(void)
  28. {
  29. up(&genl_sem);
  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. genl_lock();
  117. list_add_tail(&ops->ops_list, &family->ops_list);
  118. genl_unlock();
  119. genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
  120. err = 0;
  121. errout:
  122. return err;
  123. }
  124. /**
  125. * genl_unregister_ops - unregister generic netlink operations
  126. * @family: generic netlink family
  127. * @ops: operations to be unregistered
  128. *
  129. * Unregisters the specified operations and unassigns them from the
  130. * specified family. The operation blocks until the current message
  131. * processing has finished and doesn't start again until the
  132. * unregister process has finished.
  133. *
  134. * Note: It is not necessary to unregister all operations before
  135. * unregistering the family, unregistering the family will cause
  136. * all assigned operations to be unregistered automatically.
  137. *
  138. * Returns 0 on success or a negative error code.
  139. */
  140. int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
  141. {
  142. struct genl_ops *rc;
  143. genl_lock();
  144. list_for_each_entry(rc, &family->ops_list, ops_list) {
  145. if (rc == ops) {
  146. list_del(&ops->ops_list);
  147. genl_unlock();
  148. genl_ctrl_event(CTRL_CMD_DELOPS, ops);
  149. return 0;
  150. }
  151. }
  152. genl_unlock();
  153. return -ENOENT;
  154. }
  155. /**
  156. * genl_register_family - register a generic netlink family
  157. * @family: generic netlink family
  158. *
  159. * Registers the specified family after validating it first. Only one
  160. * family may be registered with the same family name or identifier.
  161. * The family id may equal GENL_ID_GENERATE causing an unique id to
  162. * be automatically generated and assigned.
  163. *
  164. * Return 0 on success or a negative error code.
  165. */
  166. int genl_register_family(struct genl_family *family)
  167. {
  168. int err = -EINVAL;
  169. if (family->id && family->id < GENL_MIN_ID)
  170. goto errout;
  171. if (family->id > GENL_MAX_ID)
  172. goto errout;
  173. INIT_LIST_HEAD(&family->ops_list);
  174. genl_lock();
  175. if (genl_family_find_byname(family->name)) {
  176. err = -EEXIST;
  177. goto errout_locked;
  178. }
  179. if (genl_family_find_byid(family->id)) {
  180. err = -EEXIST;
  181. goto errout_locked;
  182. }
  183. if (family->id == GENL_ID_GENERATE) {
  184. u16 newid = genl_generate_id();
  185. if (!newid) {
  186. err = -ENOMEM;
  187. goto errout_locked;
  188. }
  189. family->id = newid;
  190. }
  191. if (family->maxattr) {
  192. family->attrbuf = kmalloc((family->maxattr+1) *
  193. sizeof(struct nlattr *), GFP_KERNEL);
  194. if (family->attrbuf == NULL) {
  195. err = -ENOMEM;
  196. goto errout;
  197. }
  198. } else
  199. family->attrbuf = NULL;
  200. list_add_tail(&family->family_list, genl_family_chain(family->id));
  201. genl_unlock();
  202. genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
  203. return 0;
  204. errout_locked:
  205. genl_unlock();
  206. errout:
  207. return err;
  208. }
  209. /**
  210. * genl_unregister_family - unregister generic netlink family
  211. * @family: generic netlink family
  212. *
  213. * Unregisters the specified family.
  214. *
  215. * Returns 0 on success or a negative error code.
  216. */
  217. int genl_unregister_family(struct genl_family *family)
  218. {
  219. struct genl_family *rc;
  220. genl_lock();
  221. list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
  222. if (family->id != rc->id || strcmp(rc->name, family->name))
  223. continue;
  224. list_del(&rc->family_list);
  225. INIT_LIST_HEAD(&family->ops_list);
  226. genl_unlock();
  227. kfree(family->attrbuf);
  228. genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
  229. return 0;
  230. }
  231. genl_unlock();
  232. return -ENOENT;
  233. }
  234. static inline int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  235. int *errp)
  236. {
  237. struct genl_ops *ops;
  238. struct genl_family *family;
  239. struct genl_info info;
  240. struct genlmsghdr *hdr = nlmsg_data(nlh);
  241. int hdrlen, err = -EINVAL;
  242. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  243. goto ignore;
  244. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  245. goto ignore;
  246. family = genl_family_find_byid(nlh->nlmsg_type);
  247. if (family == NULL) {
  248. err = -ENOENT;
  249. goto errout;
  250. }
  251. hdrlen = GENL_HDRLEN + family->hdrsize;
  252. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  253. goto errout;
  254. ops = genl_get_cmd(hdr->cmd, family);
  255. if (ops == NULL) {
  256. err = -EOPNOTSUPP;
  257. goto errout;
  258. }
  259. if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb)) {
  260. err = -EPERM;
  261. goto errout;
  262. }
  263. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  264. if (ops->dumpit == NULL) {
  265. err = -EOPNOTSUPP;
  266. goto errout;
  267. }
  268. *errp = err = netlink_dump_start(genl_sock, skb, nlh,
  269. ops->dumpit, NULL);
  270. if (err == 0)
  271. skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
  272. skb->len));
  273. return -1;
  274. }
  275. if (ops->doit == NULL) {
  276. err = -EOPNOTSUPP;
  277. goto errout;
  278. }
  279. if (family->attrbuf) {
  280. err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
  281. ops->policy);
  282. if (err < 0)
  283. goto errout;
  284. }
  285. info.snd_seq = nlh->nlmsg_seq;
  286. info.snd_pid = NETLINK_CB(skb).pid;
  287. info.nlhdr = nlh;
  288. info.genlhdr = nlmsg_data(nlh);
  289. info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
  290. info.attrs = family->attrbuf;
  291. *errp = err = ops->doit(skb, &info);
  292. return err;
  293. ignore:
  294. return 0;
  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 int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
  313. u32 flags, struct sk_buff *skb, u8 cmd)
  314. {
  315. void *hdr;
  316. hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd,
  317. family->version);
  318. if (hdr == NULL)
  319. return -1;
  320. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  321. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  322. return genlmsg_end(skb, hdr);
  323. nla_put_failure:
  324. return genlmsg_cancel(skb, hdr);
  325. }
  326. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  327. {
  328. int i, n = 0;
  329. struct genl_family *rt;
  330. int chains_to_skip = cb->args[0];
  331. int fams_to_skip = cb->args[1];
  332. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  333. if (i < chains_to_skip)
  334. continue;
  335. n = 0;
  336. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  337. if (++n < fams_to_skip)
  338. continue;
  339. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  340. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  341. skb, CTRL_CMD_NEWFAMILY) < 0)
  342. goto errout;
  343. }
  344. fams_to_skip = 0;
  345. }
  346. errout:
  347. cb->args[0] = i;
  348. cb->args[1] = n;
  349. return skb->len;
  350. }
  351. static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
  352. int seq, u8 cmd)
  353. {
  354. struct sk_buff *skb;
  355. int err;
  356. skb = nlmsg_new(NLMSG_GOODSIZE);
  357. if (skb == NULL)
  358. return ERR_PTR(-ENOBUFS);
  359. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  360. if (err < 0) {
  361. nlmsg_free(skb);
  362. return ERR_PTR(err);
  363. }
  364. return skb;
  365. }
  366. static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
  367. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  368. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING },
  369. };
  370. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  371. {
  372. struct sk_buff *msg;
  373. struct genl_family *res = NULL;
  374. int err = -EINVAL;
  375. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  376. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  377. res = genl_family_find_byid(id);
  378. }
  379. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  380. char name[GENL_NAMSIZ];
  381. if (nla_strlcpy(name, info->attrs[CTRL_ATTR_FAMILY_NAME],
  382. GENL_NAMSIZ) >= GENL_NAMSIZ)
  383. goto errout;
  384. res = genl_family_find_byname(name);
  385. }
  386. if (res == NULL) {
  387. err = -ENOENT;
  388. goto errout;
  389. }
  390. msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
  391. CTRL_CMD_NEWFAMILY);
  392. if (IS_ERR(msg)) {
  393. err = PTR_ERR(msg);
  394. goto errout;
  395. }
  396. err = genlmsg_unicast(msg, info->snd_pid);
  397. errout:
  398. return err;
  399. }
  400. static int genl_ctrl_event(int event, void *data)
  401. {
  402. struct sk_buff *msg;
  403. if (genl_sock == NULL)
  404. return 0;
  405. switch (event) {
  406. case CTRL_CMD_NEWFAMILY:
  407. case CTRL_CMD_DELFAMILY:
  408. msg = ctrl_build_msg(data, 0, 0, event);
  409. if (IS_ERR(msg))
  410. return PTR_ERR(msg);
  411. genlmsg_multicast(msg, 0, GENL_ID_CTRL);
  412. break;
  413. }
  414. return 0;
  415. }
  416. static struct genl_ops genl_ctrl_ops = {
  417. .cmd = CTRL_CMD_GETFAMILY,
  418. .doit = ctrl_getfamily,
  419. .dumpit = ctrl_dumpfamily,
  420. .policy = ctrl_policy,
  421. };
  422. static struct genl_family genl_ctrl = {
  423. .id = GENL_ID_CTRL,
  424. .name = "nlctrl",
  425. .version = 0x1,
  426. .maxattr = CTRL_ATTR_MAX,
  427. };
  428. static int __init genl_init(void)
  429. {
  430. int i, err;
  431. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  432. INIT_LIST_HEAD(&family_ht[i]);
  433. err = genl_register_family(&genl_ctrl);
  434. if (err < 0)
  435. goto errout;
  436. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  437. if (err < 0)
  438. goto errout_register;
  439. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  440. genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
  441. genl_rcv, THIS_MODULE);
  442. if (genl_sock == NULL) {
  443. panic("GENL: Cannot initialize generic netlink\n");
  444. return -ENOMEM;
  445. }
  446. return 0;
  447. errout_register:
  448. genl_unregister_family(&genl_ctrl);
  449. errout:
  450. panic("GENL: Cannot register controller: %d\n", err);
  451. return err;
  452. }
  453. subsys_initcall(genl_init);
  454. EXPORT_SYMBOL(genl_sock);
  455. EXPORT_SYMBOL(genl_register_ops);
  456. EXPORT_SYMBOL(genl_unregister_ops);
  457. EXPORT_SYMBOL(genl_register_family);
  458. EXPORT_SYMBOL(genl_unregister_family);