sch_ingress.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #define PRIV(sch) qdisc_priv(sch)
  20. /* Thanks to Doron Oz for this hack */
  21. #ifndef CONFIG_NET_CLS_ACT
  22. #ifdef CONFIG_NETFILTER
  23. static int nf_registered;
  24. #endif
  25. #endif
  26. struct ingress_qdisc_data {
  27. struct Qdisc *q;
  28. struct tcf_proto *filter_list;
  29. };
  30. /* ------------------------- Class/flow operations ------------------------- */
  31. static int ingress_graft(struct Qdisc *sch, unsigned long arg,
  32. struct Qdisc *new, struct Qdisc **old)
  33. {
  34. return 1;
  35. }
  36. static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
  37. {
  38. return NULL;
  39. }
  40. static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
  41. {
  42. return TC_H_MIN(classid) + 1;
  43. }
  44. static unsigned long ingress_bind_filter(struct Qdisc *sch,
  45. unsigned long parent, u32 classid)
  46. {
  47. return ingress_get(sch, classid);
  48. }
  49. static void ingress_put(struct Qdisc *sch, unsigned long cl)
  50. {
  51. }
  52. static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
  53. struct rtattr **tca, unsigned long *arg)
  54. {
  55. return 0;
  56. }
  57. static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  58. {
  59. return;
  60. }
  61. static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
  62. {
  63. struct ingress_qdisc_data *p = PRIV(sch);
  64. return &p->filter_list;
  65. }
  66. /* --------------------------- Qdisc operations ---------------------------- */
  67. static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  68. {
  69. struct ingress_qdisc_data *p = PRIV(sch);
  70. struct tcf_result res;
  71. int result;
  72. result = tc_classify(skb, p->filter_list, &res);
  73. /*
  74. * Unlike normal "enqueue" functions, ingress_enqueue returns a
  75. * firewall FW_* code.
  76. */
  77. #ifdef CONFIG_NET_CLS_ACT
  78. sch->bstats.packets++;
  79. sch->bstats.bytes += skb->len;
  80. switch (result) {
  81. case TC_ACT_SHOT:
  82. result = TC_ACT_SHOT;
  83. sch->qstats.drops++;
  84. break;
  85. case TC_ACT_STOLEN:
  86. case TC_ACT_QUEUED:
  87. result = TC_ACT_STOLEN;
  88. break;
  89. case TC_ACT_RECLASSIFY:
  90. case TC_ACT_OK:
  91. skb->tc_index = TC_H_MIN(res.classid);
  92. default:
  93. result = TC_ACT_OK;
  94. break;
  95. }
  96. #else
  97. result = NF_ACCEPT;
  98. sch->bstats.packets++;
  99. sch->bstats.bytes += skb->len;
  100. #endif
  101. return result;
  102. }
  103. static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
  104. {
  105. return NULL;
  106. }
  107. static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
  108. {
  109. return 0;
  110. }
  111. static unsigned int ingress_drop(struct Qdisc *sch)
  112. {
  113. return 0;
  114. }
  115. #ifndef CONFIG_NET_CLS_ACT
  116. #ifdef CONFIG_NETFILTER
  117. static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
  118. const struct net_device *indev,
  119. const struct net_device *outdev,
  120. int (*okfn)(struct sk_buff *))
  121. {
  122. struct Qdisc *q;
  123. struct net_device *dev = skb->dev;
  124. int fwres = NF_ACCEPT;
  125. if (dev->qdisc_ingress) {
  126. spin_lock(&dev->ingress_lock);
  127. if ((q = dev->qdisc_ingress) != NULL)
  128. fwres = q->enqueue(skb, q);
  129. spin_unlock(&dev->ingress_lock);
  130. }
  131. return fwres;
  132. }
  133. /* after ipt_filter */
  134. static struct nf_hook_ops ing_ops[] __read_mostly = {
  135. {
  136. .hook = ing_hook,
  137. .owner = THIS_MODULE,
  138. .pf = PF_INET,
  139. .hooknum = NF_INET_PRE_ROUTING,
  140. .priority = NF_IP_PRI_FILTER + 1,
  141. },
  142. {
  143. .hook = ing_hook,
  144. .owner = THIS_MODULE,
  145. .pf = PF_INET6,
  146. .hooknum = NF_INET_PRE_ROUTING,
  147. .priority = NF_IP6_PRI_FILTER + 1,
  148. },
  149. };
  150. #endif
  151. #endif
  152. static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
  153. {
  154. struct ingress_qdisc_data *p = PRIV(sch);
  155. /* Make sure either netfilter or preferably CLS_ACT is
  156. * compiled in */
  157. #ifndef CONFIG_NET_CLS_ACT
  158. #ifndef CONFIG_NETFILTER
  159. printk("You MUST compile classifier actions into the kernel\n");
  160. return -EINVAL;
  161. #else
  162. printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
  163. #endif
  164. #endif
  165. #ifndef CONFIG_NET_CLS_ACT
  166. #ifdef CONFIG_NETFILTER
  167. if (!nf_registered) {
  168. if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
  169. printk("ingress qdisc registration error \n");
  170. return -EINVAL;
  171. }
  172. nf_registered++;
  173. }
  174. #endif
  175. #endif
  176. p->q = &noop_qdisc;
  177. return 0;
  178. }
  179. static void ingress_reset(struct Qdisc *sch)
  180. {
  181. return;
  182. }
  183. /* ------------------------------------------------------------- */
  184. static void ingress_destroy(struct Qdisc *sch)
  185. {
  186. struct ingress_qdisc_data *p = PRIV(sch);
  187. tcf_destroy_chain(p->filter_list);
  188. }
  189. static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
  190. {
  191. unsigned char *b = skb_tail_pointer(skb);
  192. struct rtattr *rta;
  193. rta = (struct rtattr *)b;
  194. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  195. rta->rta_len = skb_tail_pointer(skb) - b;
  196. return skb->len;
  197. rtattr_failure:
  198. nlmsg_trim(skb, b);
  199. return -1;
  200. }
  201. static const struct Qdisc_class_ops ingress_class_ops = {
  202. .graft = ingress_graft,
  203. .leaf = ingress_leaf,
  204. .get = ingress_get,
  205. .put = ingress_put,
  206. .change = ingress_change,
  207. .walk = ingress_walk,
  208. .tcf_chain = ingress_find_tcf,
  209. .bind_tcf = ingress_bind_filter,
  210. .unbind_tcf = ingress_put,
  211. };
  212. static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
  213. .cl_ops = &ingress_class_ops,
  214. .id = "ingress",
  215. .priv_size = sizeof(struct ingress_qdisc_data),
  216. .enqueue = ingress_enqueue,
  217. .dequeue = ingress_dequeue,
  218. .requeue = ingress_requeue,
  219. .drop = ingress_drop,
  220. .init = ingress_init,
  221. .reset = ingress_reset,
  222. .destroy = ingress_destroy,
  223. .dump = ingress_dump,
  224. .owner = THIS_MODULE,
  225. };
  226. static int __init ingress_module_init(void)
  227. {
  228. int ret = 0;
  229. if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
  230. printk("Unable to register Ingress qdisc\n");
  231. return ret;
  232. }
  233. return ret;
  234. }
  235. static void __exit ingress_module_exit(void)
  236. {
  237. unregister_qdisc(&ingress_qdisc_ops);
  238. #ifndef CONFIG_NET_CLS_ACT
  239. #ifdef CONFIG_NETFILTER
  240. if (nf_registered)
  241. nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
  242. #endif
  243. #endif
  244. }
  245. module_init(ingress_module_init)
  246. module_exit(ingress_module_exit)
  247. MODULE_LICENSE("GPL");