sch_ingress.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* net/sched/sch_ingress.c - Ingress qdisc
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version
  5. * 2 of the License, or (at your option) any later version.
  6. *
  7. * Authors: Jamal Hadi Salim 1999
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/list.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/netfilter_ipv4.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <linux/netfilter.h>
  17. #include <net/netlink.h>
  18. #include <net/pkt_sched.h>
  19. #undef DEBUG_INGRESS
  20. #ifdef DEBUG_INGRESS /* control */
  21. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  22. #else
  23. #define DPRINTK(format,args...)
  24. #endif
  25. #if 0 /* data */
  26. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  27. #else
  28. #define D2PRINTK(format,args...)
  29. #endif
  30. #define PRIV(sch) qdisc_priv(sch)
  31. /* Thanks to Doron Oz for this hack
  32. */
  33. #ifndef CONFIG_NET_CLS_ACT
  34. #ifdef CONFIG_NETFILTER
  35. static int nf_registered;
  36. #endif
  37. #endif
  38. struct ingress_qdisc_data {
  39. struct Qdisc *q;
  40. struct tcf_proto *filter_list;
  41. };
  42. /* ------------------------- Class/flow operations ------------------------- */
  43. static int ingress_graft(struct Qdisc *sch,unsigned long arg,
  44. struct Qdisc *new,struct Qdisc **old)
  45. {
  46. #ifdef DEBUG_INGRESS
  47. struct ingress_qdisc_data *p = PRIV(sch);
  48. #endif
  49. DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
  50. sch, p, new, old);
  51. DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
  52. return 1;
  53. }
  54. static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
  55. {
  56. return NULL;
  57. }
  58. static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
  59. {
  60. #ifdef DEBUG_INGRESS
  61. struct ingress_qdisc_data *p = PRIV(sch);
  62. #endif
  63. DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
  64. return TC_H_MIN(classid) + 1;
  65. }
  66. static unsigned long ingress_bind_filter(struct Qdisc *sch,
  67. unsigned long parent, u32 classid)
  68. {
  69. return ingress_get(sch, classid);
  70. }
  71. static void ingress_put(struct Qdisc *sch, unsigned long cl)
  72. {
  73. }
  74. static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
  75. struct rtattr **tca, unsigned long *arg)
  76. {
  77. #ifdef DEBUG_INGRESS
  78. struct ingress_qdisc_data *p = PRIV(sch);
  79. #endif
  80. DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
  81. "arg 0x%lx\n", sch, p, classid, parent, *arg);
  82. DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
  83. return 0;
  84. }
  85. static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
  86. {
  87. #ifdef DEBUG_INGRESS
  88. struct ingress_qdisc_data *p = PRIV(sch);
  89. #endif
  90. DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
  91. DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
  92. }
  93. static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
  94. {
  95. struct ingress_qdisc_data *p = PRIV(sch);
  96. return &p->filter_list;
  97. }
  98. /* --------------------------- Qdisc operations ---------------------------- */
  99. static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
  100. {
  101. struct ingress_qdisc_data *p = PRIV(sch);
  102. struct tcf_result res;
  103. int result;
  104. D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  105. result = tc_classify(skb, p->filter_list, &res);
  106. D2PRINTK("result %d class 0x%04x\n", result, res.classid);
  107. /*
  108. * Unlike normal "enqueue" functions, ingress_enqueue returns a
  109. * firewall FW_* code.
  110. */
  111. #ifdef CONFIG_NET_CLS_ACT
  112. sch->bstats.packets++;
  113. sch->bstats.bytes += skb->len;
  114. switch (result) {
  115. case TC_ACT_SHOT:
  116. result = TC_ACT_SHOT;
  117. sch->qstats.drops++;
  118. break;
  119. case TC_ACT_STOLEN:
  120. case TC_ACT_QUEUED:
  121. result = TC_ACT_STOLEN;
  122. break;
  123. case TC_ACT_RECLASSIFY:
  124. case TC_ACT_OK:
  125. case TC_ACT_UNSPEC:
  126. default:
  127. skb->tc_index = TC_H_MIN(res.classid);
  128. result = TC_ACT_OK;
  129. break;
  130. }
  131. #else
  132. D2PRINTK("Overriding result to ACCEPT\n");
  133. result = NF_ACCEPT;
  134. sch->bstats.packets++;
  135. sch->bstats.bytes += skb->len;
  136. #endif
  137. return result;
  138. }
  139. static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
  140. {
  141. /*
  142. struct ingress_qdisc_data *p = PRIV(sch);
  143. D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
  144. */
  145. return NULL;
  146. }
  147. static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
  148. {
  149. /*
  150. struct ingress_qdisc_data *p = PRIV(sch);
  151. D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
  152. */
  153. return 0;
  154. }
  155. static unsigned int ingress_drop(struct Qdisc *sch)
  156. {
  157. #ifdef DEBUG_INGRESS
  158. struct ingress_qdisc_data *p = PRIV(sch);
  159. #endif
  160. DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
  161. return 0;
  162. }
  163. #ifndef CONFIG_NET_CLS_ACT
  164. #ifdef CONFIG_NETFILTER
  165. static unsigned int
  166. ing_hook(unsigned int hook, struct sk_buff **pskb,
  167. const struct net_device *indev,
  168. const struct net_device *outdev,
  169. int (*okfn)(struct sk_buff *))
  170. {
  171. struct Qdisc *q;
  172. struct sk_buff *skb = *pskb;
  173. struct net_device *dev = skb->dev;
  174. int fwres=NF_ACCEPT;
  175. DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
  176. skb->sk ? "(owned)" : "(unowned)",
  177. skb->dev ? (*pskb)->dev->name : "(no dev)",
  178. skb->len);
  179. if (dev->qdisc_ingress) {
  180. spin_lock(&dev->ingress_lock);
  181. if ((q = dev->qdisc_ingress) != NULL)
  182. fwres = q->enqueue(skb, q);
  183. spin_unlock(&dev->ingress_lock);
  184. }
  185. return fwres;
  186. }
  187. /* after ipt_filter */
  188. static struct nf_hook_ops ing_ops = {
  189. .hook = ing_hook,
  190. .owner = THIS_MODULE,
  191. .pf = PF_INET,
  192. .hooknum = NF_IP_PRE_ROUTING,
  193. .priority = NF_IP_PRI_FILTER + 1,
  194. };
  195. static struct nf_hook_ops ing6_ops = {
  196. .hook = ing_hook,
  197. .owner = THIS_MODULE,
  198. .pf = PF_INET6,
  199. .hooknum = NF_IP6_PRE_ROUTING,
  200. .priority = NF_IP6_PRI_FILTER + 1,
  201. };
  202. #endif
  203. #endif
  204. static int ingress_init(struct Qdisc *sch,struct rtattr *opt)
  205. {
  206. struct ingress_qdisc_data *p = PRIV(sch);
  207. /* Make sure either netfilter or preferably CLS_ACT is
  208. * compiled in */
  209. #ifndef CONFIG_NET_CLS_ACT
  210. #ifndef CONFIG_NETFILTER
  211. printk("You MUST compile classifier actions into the kernel\n");
  212. return -EINVAL;
  213. #else
  214. printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
  215. #endif
  216. #endif
  217. #ifndef CONFIG_NET_CLS_ACT
  218. #ifdef CONFIG_NETFILTER
  219. if (!nf_registered) {
  220. if (nf_register_hook(&ing_ops) < 0) {
  221. printk("ingress qdisc registration error \n");
  222. return -EINVAL;
  223. }
  224. nf_registered++;
  225. if (nf_register_hook(&ing6_ops) < 0) {
  226. printk("IPv6 ingress qdisc registration error, " \
  227. "disabling IPv6 support.\n");
  228. } else
  229. nf_registered++;
  230. }
  231. #endif
  232. #endif
  233. DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
  234. p->q = &noop_qdisc;
  235. return 0;
  236. }
  237. static void ingress_reset(struct Qdisc *sch)
  238. {
  239. struct ingress_qdisc_data *p = PRIV(sch);
  240. DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
  241. /*
  242. #if 0
  243. */
  244. /* for future use */
  245. qdisc_reset(p->q);
  246. /*
  247. #endif
  248. */
  249. }
  250. /* ------------------------------------------------------------- */
  251. /* ------------------------------------------------------------- */
  252. static void ingress_destroy(struct Qdisc *sch)
  253. {
  254. struct ingress_qdisc_data *p = PRIV(sch);
  255. DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
  256. tcf_destroy_chain(p->filter_list);
  257. #if 0
  258. /* for future use */
  259. qdisc_destroy(p->q);
  260. #endif
  261. }
  262. static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
  263. {
  264. unsigned char *b = skb_tail_pointer(skb);
  265. struct rtattr *rta;
  266. rta = (struct rtattr *) b;
  267. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  268. rta->rta_len = skb_tail_pointer(skb) - b;
  269. return skb->len;
  270. rtattr_failure:
  271. nlmsg_trim(skb, b);
  272. return -1;
  273. }
  274. static struct Qdisc_class_ops ingress_class_ops = {
  275. .graft = ingress_graft,
  276. .leaf = ingress_leaf,
  277. .get = ingress_get,
  278. .put = ingress_put,
  279. .change = ingress_change,
  280. .delete = NULL,
  281. .walk = ingress_walk,
  282. .tcf_chain = ingress_find_tcf,
  283. .bind_tcf = ingress_bind_filter,
  284. .unbind_tcf = ingress_put,
  285. .dump = NULL,
  286. };
  287. static struct Qdisc_ops ingress_qdisc_ops = {
  288. .next = NULL,
  289. .cl_ops = &ingress_class_ops,
  290. .id = "ingress",
  291. .priv_size = sizeof(struct ingress_qdisc_data),
  292. .enqueue = ingress_enqueue,
  293. .dequeue = ingress_dequeue,
  294. .requeue = ingress_requeue,
  295. .drop = ingress_drop,
  296. .init = ingress_init,
  297. .reset = ingress_reset,
  298. .destroy = ingress_destroy,
  299. .change = NULL,
  300. .dump = ingress_dump,
  301. .owner = THIS_MODULE,
  302. };
  303. static int __init ingress_module_init(void)
  304. {
  305. int ret = 0;
  306. if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
  307. printk("Unable to register Ingress qdisc\n");
  308. return ret;
  309. }
  310. return ret;
  311. }
  312. static void __exit ingress_module_exit(void)
  313. {
  314. unregister_qdisc(&ingress_qdisc_ops);
  315. #ifndef CONFIG_NET_CLS_ACT
  316. #ifdef CONFIG_NETFILTER
  317. if (nf_registered) {
  318. nf_unregister_hook(&ing_ops);
  319. if (nf_registered > 1)
  320. nf_unregister_hook(&ing6_ops);
  321. }
  322. #endif
  323. #endif
  324. }
  325. module_init(ingress_module_init)
  326. module_exit(ingress_module_exit)
  327. MODULE_LICENSE("GPL");