sch_ingress.c 9.6 KB

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