sch_ingress.c 6.3 KB

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