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