sch_ingress.c 9.6 KB

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