genetlink.c 12 KB

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