cls_api.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/cls_api.c Packet classifier API.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version
  7.  * 2 of the License, or (at your option) any later version.
  8.  *
  9.  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10.  *
  11.  * Changes:
  12.  *
  13.  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
  14.  */
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/bitops.h>
  18. #include <linux/config.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/in.h>
  27. #include <linux/errno.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/rtnetlink.h>
  32. #include <linux/init.h>
  33. #include <linux/kmod.h>
  34. #include <net/sock.h>
  35. #include <net/pkt_sched.h>
  36. /* The list of all installed classifier types */
  37. static struct tcf_proto_ops *tcf_proto_base;
  38. /* Protects list of registered TC modules. It is pure SMP lock. */
  39. static rwlock_t cls_mod_lock = RW_LOCK_UNLOCKED;
  40. /* Find classifier type by string name */
  41. struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
  42. {
  43. struct tcf_proto_ops *t = NULL;
  44. if (kind) {
  45. read_lock(&cls_mod_lock);
  46. for (t = tcf_proto_base; t; t = t->next) {
  47. if (rtattr_strcmp(kind, t->kind) == 0)
  48. break;
  49. }
  50. read_unlock(&cls_mod_lock);
  51. }
  52. return t;
  53. }
  54. /* Register(unregister) new classifier type */
  55. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  56. {
  57. struct tcf_proto_ops *t, **tp;
  58. write_lock(&cls_mod_lock);
  59. for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next) {
  60. if (strcmp(ops->kind, t->kind) == 0) {
  61. write_unlock(&cls_mod_lock);
  62. return -EEXIST;
  63. }
  64. }
  65. ops->next = NULL;
  66. *tp = ops;
  67. write_unlock(&cls_mod_lock);
  68. return 0;
  69. }
  70. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  71. {
  72. struct tcf_proto_ops *t, **tp;
  73. write_lock(&cls_mod_lock);
  74. for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
  75. if (t == ops)
  76. break;
  77. if (!t) {
  78. write_unlock(&cls_mod_lock);
  79. return -ENOENT;
  80. }
  81. *tp = t->next;
  82. write_unlock(&cls_mod_lock);
  83. return 0;
  84. }
  85. static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
  86.   struct tcf_proto *tp, unsigned long fh, int event);
  87. /* Select new prio value from the range, managed by kernel. */
  88. static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
  89. {
  90. u32 first = TC_H_MAKE(0xC0000000U,0U);
  91. if (tp)
  92. first = tp->prio-1;
  93. return first;
  94. }
  95. /* Add/change/delete/get a filter node */
  96. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  97. {
  98. struct rtattr **tca = arg;
  99. struct tcmsg *t = NLMSG_DATA(n);
  100. u32 protocol = TC_H_MIN(t->tcm_info);
  101. u32 prio = TC_H_MAJ(t->tcm_info);
  102. u32 nprio = prio;
  103. u32 parent = t->tcm_parent;
  104. struct net_device *dev;
  105. struct Qdisc  *q;
  106. struct tcf_proto **back, **chain;
  107. struct tcf_proto *tp = NULL;
  108. struct tcf_proto_ops *tp_ops;
  109. struct Qdisc_class_ops *cops;
  110. unsigned long cl = 0;
  111. unsigned long fh;
  112. int err;
  113. if (prio == 0) {
  114. /* If no priority is given, user wants we allocated it. */
  115. if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
  116. return -ENOENT;
  117. prio = TC_H_MAKE(0x80000000U,0U);
  118. }
  119. /* Find head of filter chain. */
  120. /* Find link */
  121. if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
  122. return -ENODEV;
  123. /* Find qdisc */
  124. if (!parent) {
  125. q = dev->qdisc_sleeping;
  126. parent = q->handle;
  127. } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
  128. return -EINVAL;
  129. /* Is it classful? */
  130. if ((cops = q->ops->cl_ops) == NULL)
  131. return -EINVAL;
  132. /* Do we search for filter, attached to class? */
  133. if (TC_H_MIN(parent)) {
  134. cl = cops->get(q, parent);
  135. if (cl == 0)
  136. return -ENOENT;
  137. }
  138. /* And the last stroke */
  139. chain = cops->tcf_chain(q, cl);
  140. err = -EINVAL;
  141. if (chain == NULL)
  142. goto errout;
  143. /* Check the chain for existence of proto-tcf with this priority */
  144. for (back = chain; (tp=*back) != NULL; back = &tp->next) {
  145. if (tp->prio >= prio) {
  146. if (tp->prio == prio) {
  147. if (!nprio || (tp->protocol != protocol && protocol))
  148. goto errout;
  149. } else
  150. tp = NULL;
  151. break;
  152. }
  153. }
  154. if (tp == NULL) {
  155. /* Proto-tcf does not exist, create new one */
  156. if (tca[TCA_KIND-1] == NULL || !protocol)
  157. goto errout;
  158. err = -ENOENT;
  159. if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
  160. goto errout;
  161. /* Create new proto tcf */
  162. err = -ENOBUFS;
  163. if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
  164. goto errout;
  165. tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
  166. #ifdef CONFIG_KMOD
  167. if (tp_ops==NULL && tca[TCA_KIND-1] != NULL) {
  168. struct rtattr *kind = tca[TCA_KIND-1];
  169. char module_name[4 + IFNAMSIZ + 1];
  170. if (RTA_PAYLOAD(kind) <= IFNAMSIZ) {
  171. sprintf(module_name, "cls_%s", (char*)RTA_DATA(kind));
  172. request_module (module_name);
  173. tp_ops = tcf_proto_lookup_ops(kind);
  174. }
  175. }
  176. #endif
  177. if (tp_ops == NULL) {
  178. err = -EINVAL;
  179. kfree(tp);
  180. goto errout;
  181. }
  182. memset(tp, 0, sizeof(*tp));
  183. tp->ops = tp_ops;
  184. tp->protocol = protocol;
  185. tp->prio = nprio ? : tcf_auto_prio(*back);
  186. tp->q = q;
  187. tp->classify = tp_ops->classify;
  188. tp->classid = parent;
  189. err = tp_ops->init(tp);
  190. if (err) {
  191. kfree(tp);
  192. goto errout;
  193. }
  194. write_lock(&qdisc_tree_lock);
  195. spin_lock_bh(&dev->queue_lock);
  196. tp->next = *back;
  197. *back = tp;
  198. spin_unlock_bh(&dev->queue_lock);
  199. write_unlock(&qdisc_tree_lock);
  200. } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
  201. goto errout;
  202. fh = tp->ops->get(tp, t->tcm_handle);
  203. if (fh == 0) {
  204. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  205. write_lock(&qdisc_tree_lock);
  206. spin_lock_bh(&dev->queue_lock);
  207. *back = tp->next;
  208. spin_unlock_bh(&dev->queue_lock);
  209. write_unlock(&qdisc_tree_lock);
  210. tp->ops->destroy(tp);
  211. kfree(tp);
  212. err = 0;
  213. goto errout;
  214. }
  215. err = -ENOENT;
  216. if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
  217. goto errout;
  218. } else {
  219. switch (n->nlmsg_type) {
  220. case RTM_NEWTFILTER:
  221. err = -EEXIST;
  222. if (n->nlmsg_flags&NLM_F_EXCL)
  223. goto errout;
  224. break;
  225. case RTM_DELTFILTER:
  226. err = tp->ops->delete(tp, fh);
  227. goto errout;
  228. case RTM_GETTFILTER:
  229. err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
  230. goto errout;
  231. default:
  232. err = -EINVAL;
  233. goto errout;
  234. }
  235. }
  236. err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
  237. if (err == 0)
  238. tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
  239. errout:
  240. if (cl)
  241. cops->put(q, cl);
  242. return err;
  243. }
  244. static int
  245. tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
  246.       u32 pid, u32 seq, unsigned flags, int event)
  247. {
  248. struct tcmsg *tcm;
  249. struct nlmsghdr  *nlh;
  250. unsigned char  *b = skb->tail;
  251. nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
  252. nlh->nlmsg_flags = flags;
  253. tcm = NLMSG_DATA(nlh);
  254. tcm->tcm_family = AF_UNSPEC;
  255. tcm->tcm_ifindex = tp->q->dev->ifindex;
  256. tcm->tcm_parent = tp->classid;
  257. tcm->tcm_handle = 0;
  258. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  259. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
  260. if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
  261. goto rtattr_failure;
  262. nlh->nlmsg_len = skb->tail - b;
  263. return skb->len;
  264. nlmsg_failure:
  265. rtattr_failure:
  266. skb_trim(skb, b - skb->data);
  267. return -1;
  268. }
  269. static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
  270.   struct tcf_proto *tp, unsigned long fh, int event)
  271. {
  272. struct sk_buff *skb;
  273. u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
  274. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  275. if (!skb)
  276. return -ENOBUFS;
  277. if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
  278. kfree_skb(skb);
  279. return -EINVAL;
  280. }
  281. return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  282. }
  283. struct tcf_dump_args
  284. {
  285. struct tcf_walker w;
  286. struct sk_buff *skb;
  287. struct netlink_callback *cb;
  288. };
  289. static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
  290. {
  291. struct tcf_dump_args *a = (void*)arg;
  292. return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
  293.      a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
  294. }
  295. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  296. {
  297. int t;
  298. int s_t;
  299. struct net_device *dev;
  300. struct Qdisc *q;
  301. struct tcf_proto *tp, **chain;
  302. struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
  303. unsigned long cl = 0;
  304. struct Qdisc_class_ops *cops;
  305. struct tcf_dump_args arg;
  306. if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
  307. return skb->len;
  308. if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
  309. return skb->len;
  310. read_lock(&qdisc_tree_lock);
  311. if (!tcm->tcm_parent)
  312. q = dev->qdisc_sleeping;
  313. else
  314. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  315. if (q == NULL) {
  316. read_unlock(&qdisc_tree_lock);
  317. dev_put(dev);
  318. return skb->len;
  319. }
  320. if ((cops = q->ops->cl_ops) == NULL)
  321. goto errout;
  322. if (TC_H_MIN(tcm->tcm_parent)) {
  323. cl = cops->get(q, tcm->tcm_parent);
  324. if (cl == 0)
  325. goto errout;
  326. }
  327. chain = cops->tcf_chain(q, cl);
  328. if (chain == NULL)
  329. goto errout;
  330. s_t = cb->args[0];
  331. for (tp=*chain, t=0; tp; tp = tp->next, t++) {
  332. if (t < s_t) continue;
  333. if (TC_H_MAJ(tcm->tcm_info) &&
  334.     TC_H_MAJ(tcm->tcm_info) != tp->prio)
  335. continue;
  336. if (TC_H_MIN(tcm->tcm_info) &&
  337.     TC_H_MIN(tcm->tcm_info) != tp->protocol)
  338. continue;
  339. if (t > s_t)
  340. memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
  341. if (cb->args[1] == 0) {
  342. if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
  343.   cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
  344. break;
  345. }
  346. cb->args[1] = 1;
  347. }
  348. if (tp->ops->walk == NULL)
  349. continue;
  350. arg.w.fn = tcf_node_dump;
  351. arg.skb = skb;
  352. arg.cb = cb;
  353. arg.w.stop = 0;
  354. arg.w.skip = cb->args[1]-1;
  355. arg.w.count = 0;
  356. tp->ops->walk(tp, &arg.w);
  357. cb->args[1] = arg.w.count+1;
  358. if (arg.w.stop)
  359. break;
  360. }
  361. cb->args[0] = t;
  362. errout:
  363. if (cl)
  364. cops->put(q, cl);
  365. read_unlock(&qdisc_tree_lock);
  366. dev_put(dev);
  367. return skb->len;
  368. }
  369. int __init tc_filter_init(void)
  370. {
  371. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  372. /* Setup rtnetlink links. It is made here to avoid
  373.    exporting large number of public symbols.
  374.  */
  375. if (link_p) {
  376. link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
  377. link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
  378. link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
  379. link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
  380. }
  381. #define INIT_TC_FILTER(name) { 
  382.           extern struct tcf_proto_ops cls_##name##_ops; 
  383.           register_tcf_proto_ops(&cls_##name##_ops); 
  384. }
  385. #ifdef CONFIG_NET_CLS_U32
  386. INIT_TC_FILTER(u32);
  387. #endif
  388. #ifdef CONFIG_NET_CLS_ROUTE4
  389. INIT_TC_FILTER(route4);
  390. #endif
  391. #ifdef CONFIG_NET_CLS_FW
  392. INIT_TC_FILTER(fw);
  393. #endif
  394. #ifdef CONFIG_NET_CLS_RSVP
  395. INIT_TC_FILTER(rsvp);
  396. #endif
  397. #ifdef CONFIG_NET_CLS_TCINDEX
  398. INIT_TC_FILTER(tcindex);
  399. #endif
  400. #ifdef CONFIG_NET_CLS_RSVP6
  401. INIT_TC_FILTER(rsvp6);
  402. #endif
  403. return 0;
  404. }