sch_ingress.c 6.2 KB

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