genetlink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. 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_locked;
  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 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, CAP_NET_ADMIN)) {
  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. struct nlattr *nla_ops;
  316. struct genl_ops *ops;
  317. void *hdr;
  318. int idx = 1;
  319. hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd,
  320. family->version);
  321. if (hdr == NULL)
  322. return -1;
  323. NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
  324. NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
  325. NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
  326. NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
  327. NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
  328. nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
  329. if (nla_ops == NULL)
  330. goto nla_put_failure;
  331. list_for_each_entry(ops, &family->ops_list, ops_list) {
  332. struct nlattr *nest;
  333. nest = nla_nest_start(skb, idx++);
  334. if (nest == NULL)
  335. goto nla_put_failure;
  336. NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
  337. NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
  338. if (ops->policy)
  339. NLA_PUT_FLAG(skb, CTRL_ATTR_OP_POLICY);
  340. if (ops->doit)
  341. NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DOIT);
  342. if (ops->dumpit)
  343. NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DUMPIT);
  344. nla_nest_end(skb, nest);
  345. }
  346. nla_nest_end(skb, nla_ops);
  347. return genlmsg_end(skb, hdr);
  348. nla_put_failure:
  349. return genlmsg_cancel(skb, hdr);
  350. }
  351. static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
  352. {
  353. int i, n = 0;
  354. struct genl_family *rt;
  355. int chains_to_skip = cb->args[0];
  356. int fams_to_skip = cb->args[1];
  357. if (chains_to_skip != 0)
  358. genl_lock();
  359. for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
  360. if (i < chains_to_skip)
  361. continue;
  362. n = 0;
  363. list_for_each_entry(rt, genl_family_chain(i), family_list) {
  364. if (++n < fams_to_skip)
  365. continue;
  366. if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
  367. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  368. skb, CTRL_CMD_NEWFAMILY) < 0)
  369. goto errout;
  370. }
  371. fams_to_skip = 0;
  372. }
  373. errout:
  374. if (chains_to_skip != 0)
  375. genl_unlock();
  376. cb->args[0] = i;
  377. cb->args[1] = n;
  378. return skb->len;
  379. }
  380. static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
  381. int seq, u8 cmd)
  382. {
  383. struct sk_buff *skb;
  384. int err;
  385. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  386. if (skb == NULL)
  387. return ERR_PTR(-ENOBUFS);
  388. err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
  389. if (err < 0) {
  390. nlmsg_free(skb);
  391. return ERR_PTR(err);
  392. }
  393. return skb;
  394. }
  395. static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
  396. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  397. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
  398. .len = GENL_NAMSIZ - 1 },
  399. };
  400. static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
  401. {
  402. struct sk_buff *msg;
  403. struct genl_family *res = NULL;
  404. int err = -EINVAL;
  405. if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
  406. u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
  407. res = genl_family_find_byid(id);
  408. }
  409. if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
  410. char *name;
  411. name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
  412. res = genl_family_find_byname(name);
  413. }
  414. if (res == NULL) {
  415. err = -ENOENT;
  416. goto errout;
  417. }
  418. msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
  419. CTRL_CMD_NEWFAMILY);
  420. if (IS_ERR(msg)) {
  421. err = PTR_ERR(msg);
  422. goto errout;
  423. }
  424. err = genlmsg_unicast(msg, info->snd_pid);
  425. errout:
  426. return err;
  427. }
  428. static int genl_ctrl_event(int event, void *data)
  429. {
  430. struct sk_buff *msg;
  431. if (genl_sock == NULL)
  432. return 0;
  433. switch (event) {
  434. case CTRL_CMD_NEWFAMILY:
  435. case CTRL_CMD_DELFAMILY:
  436. msg = ctrl_build_msg(data, 0, 0, event);
  437. if (IS_ERR(msg))
  438. return PTR_ERR(msg);
  439. genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
  440. break;
  441. }
  442. return 0;
  443. }
  444. static struct genl_ops genl_ctrl_ops = {
  445. .cmd = CTRL_CMD_GETFAMILY,
  446. .doit = ctrl_getfamily,
  447. .dumpit = ctrl_dumpfamily,
  448. .policy = ctrl_policy,
  449. };
  450. static struct genl_family genl_ctrl = {
  451. .id = GENL_ID_CTRL,
  452. .name = "nlctrl",
  453. .version = 0x1,
  454. .maxattr = CTRL_ATTR_MAX,
  455. };
  456. static int __init genl_init(void)
  457. {
  458. int i, err;
  459. for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
  460. INIT_LIST_HEAD(&family_ht[i]);
  461. err = genl_register_family(&genl_ctrl);
  462. if (err < 0)
  463. goto errout;
  464. err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
  465. if (err < 0)
  466. goto errout_register;
  467. netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
  468. genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
  469. genl_rcv, THIS_MODULE);
  470. if (genl_sock == NULL)
  471. panic("GENL: Cannot initialize generic netlink\n");
  472. return 0;
  473. errout_register:
  474. genl_unregister_family(&genl_ctrl);
  475. errout:
  476. panic("GENL: Cannot register controller: %d\n", err);
  477. }
  478. subsys_initcall(genl_init);
  479. EXPORT_SYMBOL(genl_sock);
  480. EXPORT_SYMBOL(genl_register_ops);
  481. EXPORT_SYMBOL(genl_unregister_ops);
  482. EXPORT_SYMBOL(genl_register_family);
  483. EXPORT_SYMBOL(genl_unregister_family);