sch_ingress.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
  21. static int nf_registered;
  22. #endif
  23. struct ingress_qdisc_data {
  24. struct tcf_proto *filter_list;
  25. };
  26. /* ------------------------- Class/flow operations ------------------------- */
  27. static int ingress_graft(struct Qdisc *sch, unsigned long arg,
  28. struct Qdisc *new, struct Qdisc **old)
  29. {
  30. return -EOPNOTSUPP;
  31. }
  32. static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
  33. {
  34. return NULL;
  35. }
  36. static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
  37. {
  38. return TC_H_MIN(classid) + 1;
  39. }
  40. static unsigned long ingress_bind_filter(struct Qdisc *sch,
  41. unsigned long parent, u32 classid)
  42. {
  43. return ingress_get(sch, classid);
  44. }
  45. static void ingress_put(struct Qdisc *sch, unsigned long cl)
  46. {
  47. }
  48. static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
  49. struct nlattr **tca, unsigned long *arg)
  50. {
  51. return 0;
  52. }
  53. static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  54. {
  55. return;
  56. }
  57. static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
  58. {
  59. struct ingress_qdisc_data *p = qdisc_priv(sch);
  60. return &p->filter_list;
  61. }
  62. /* --------------------------- Qdisc operations ---------------------------- */
  63. static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  64. {
  65. struct ingress_qdisc_data *p = qdisc_priv(sch);
  66. struct tcf_result res;
  67. int result;
  68. result = tc_classify(skb, p->filter_list, &res);
  69. /*
  70. * Unlike normal "enqueue" functions, ingress_enqueue returns a
  71. * firewall FW_* code.
  72. */
  73. #ifdef CONFIG_NET_CLS_ACT
  74. sch->bstats.packets++;
  75. sch->bstats.bytes += skb->len;
  76. switch (result) {
  77. case TC_ACT_SHOT:
  78. result = TC_ACT_SHOT;
  79. sch->qstats.drops++;
  80. break;
  81. case TC_ACT_STOLEN:
  82. case TC_ACT_QUEUED:
  83. result = TC_ACT_STOLEN;
  84. break;
  85. case TC_ACT_RECLASSIFY:
  86. case TC_ACT_OK:
  87. skb->tc_index = TC_H_MIN(res.classid);
  88. default:
  89. result = TC_ACT_OK;
  90. break;
  91. }
  92. #else
  93. result = NF_ACCEPT;
  94. sch->bstats.packets++;
  95. sch->bstats.bytes += skb->len;
  96. #endif
  97. return result;
  98. }
  99. #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
  100. static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
  101. const struct net_device *indev,
  102. const struct net_device *outdev,
  103. int (*okfn)(struct sk_buff *))
  104. {
  105. struct Qdisc *q;
  106. struct net_device *dev = skb->dev;
  107. int fwres = NF_ACCEPT;
  108. if (dev->qdisc_ingress) {
  109. spin_lock(&dev->ingress_lock);
  110. if ((q = dev->qdisc_ingress) != NULL)
  111. fwres = q->enqueue(skb, q);
  112. spin_unlock(&dev->ingress_lock);
  113. }
  114. return fwres;
  115. }
  116. /* after ipt_filter */
  117. static struct nf_hook_ops ing_ops[] __read_mostly = {
  118. {
  119. .hook = ing_hook,
  120. .owner = THIS_MODULE,
  121. .pf = PF_INET,
  122. .hooknum = NF_INET_PRE_ROUTING,
  123. .priority = NF_IP_PRI_FILTER + 1,
  124. },
  125. {
  126. .hook = ing_hook,
  127. .owner = THIS_MODULE,
  128. .pf = PF_INET6,
  129. .hooknum = NF_INET_PRE_ROUTING,
  130. .priority = NF_IP6_PRI_FILTER + 1,
  131. },
  132. };
  133. #endif
  134. static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
  135. {
  136. #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
  137. printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
  138. if (!nf_registered) {
  139. if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
  140. printk("ingress qdisc registration error \n");
  141. return -EINVAL;
  142. }
  143. nf_registered++;
  144. }
  145. #endif
  146. return 0;
  147. }
  148. /* ------------------------------------------------------------- */
  149. static void ingress_destroy(struct Qdisc *sch)
  150. {
  151. struct ingress_qdisc_data *p = qdisc_priv(sch);
  152. tcf_destroy_chain(p->filter_list);
  153. }
  154. static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
  155. {
  156. struct nlattr *nest;
  157. nest = nla_nest_start(skb, TCA_OPTIONS);
  158. if (nest == NULL)
  159. goto nla_put_failure;
  160. nla_nest_end(skb, nest);
  161. return skb->len;
  162. nla_put_failure:
  163. nla_nest_cancel(skb, nest);
  164. return -1;
  165. }
  166. static const struct Qdisc_class_ops ingress_class_ops = {
  167. .graft = ingress_graft,
  168. .leaf = ingress_leaf,
  169. .get = ingress_get,
  170. .put = ingress_put,
  171. .change = ingress_change,
  172. .walk = ingress_walk,
  173. .tcf_chain = ingress_find_tcf,
  174. .bind_tcf = ingress_bind_filter,
  175. .unbind_tcf = ingress_put,
  176. };
  177. static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
  178. .cl_ops = &ingress_class_ops,
  179. .id = "ingress",
  180. .priv_size = sizeof(struct ingress_qdisc_data),
  181. .enqueue = ingress_enqueue,
  182. .init = ingress_init,
  183. .destroy = ingress_destroy,
  184. .dump = ingress_dump,
  185. .owner = THIS_MODULE,
  186. };
  187. static int __init ingress_module_init(void)
  188. {
  189. return register_qdisc(&ingress_qdisc_ops);
  190. }
  191. static void __exit ingress_module_exit(void)
  192. {
  193. unregister_qdisc(&ingress_qdisc_ops);
  194. #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
  195. if (nf_registered)
  196. nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
  197. #endif
  198. }
  199. module_init(ingress_module_init)
  200. module_exit(ingress_module_exit)
  201. MODULE_LICENSE("GPL");