This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / sched / mirred.c
1 /*
2  * net/sched/mirred.c   packet mirroring and redirect actions
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Jamal Hadi Salim (2002-4)
10  *
11  * TODO: Add ingress support (and socket redirect support)
12  *
13  */
14
15 #include <asm/uaccess.h>
16 #include <asm/system.h>
17 #include <asm/bitops.h>
18 #include <linux/config.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/in.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/proc_fs.h>
35 #include <net/sock.h>
36 #include <net/pkt_sched.h>
37 #include <linux/tc_act/tc_mirred.h>
38 #include <net/tc_act/tc_mirred.h>
39
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
42
43
44 /* use generic hash table */
45 #define MY_TAB_SIZE     8
46 #define MY_TAB_MASK     (MY_TAB_SIZE - 1)
47 static u32 idx_gen;
48 static struct tcf_mirred *tcf_mirred_ht[MY_TAB_SIZE];
49 static rwlock_t mirred_lock = RW_LOCK_UNLOCKED;
50
51 /* ovewrride the defaults */
52 #define tcf_st  tcf_mirred
53 #define tc_st  tc_mirred
54 #define tcf_t_lock   mirred_lock
55 #define tcf_ht tcf_mirred_ht
56
57 #define CONFIG_NET_ACT_INIT 1
58 #include <net/pkt_act.h>
59
60 static inline int
61 tcf_mirred_release(struct tcf_mirred *p, int bind)
62 {
63         if (p) {
64                 if (bind) {
65                         p->bindcnt--;
66                 }
67
68                 p->refcnt--;
69                 if(!p->bindcnt && p->refcnt <= 0) {
70                         dev_put(p->dev);
71                         tcf_hash_destroy(p);
72                         return 1;
73                 }
74         }
75
76         return 0;
77 }
78
79 static int
80 tcf_mirred_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,int ovr, int bind)
81 {
82         struct rtattr *tb[TCA_MIRRED_MAX];
83         struct tc_mirred *parm;
84         struct tcf_mirred *p;
85         struct net_device *dev = NULL;
86         int size = sizeof (*p), new = 0;
87
88
89         if (rtattr_parse(tb, TCA_MIRRED_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0) {
90                 DPRINTK("tcf_mirred_init BUG in user space couldnt parse properly\n");
91                 return -1;
92         }
93
94         if (NULL == a || NULL == tb[TCA_MIRRED_PARMS - 1]) {
95                 DPRINTK("BUG: tcf_mirred_init called with NULL params\n");
96                 return -1;
97         }
98
99         parm = RTA_DATA(tb[TCA_MIRRED_PARMS - 1]);
100
101         p = tcf_hash_check(parm, a, ovr, bind);
102         if (NULL == p) { /* new */
103                 p = tcf_hash_create(parm,est,a,size,ovr,bind);
104                 new = 1;
105                 if (NULL == p)
106                         return -1;
107         }
108
109         if (parm->ifindex) {
110                 dev = dev_get_by_index(parm->ifindex);
111                 if (NULL == dev) {
112                         printk("BUG: tcf_mirred_init called with bad device\n");
113                         return -1;
114                 }
115                 switch (dev->type) {
116                         case ARPHRD_TUNNEL:
117                         case ARPHRD_TUNNEL6:
118                         case ARPHRD_SIT:
119                         case ARPHRD_IPGRE:
120                         case ARPHRD_VOID:
121                         case ARPHRD_NONE:
122                                 p->ok_push = 0;
123                                 break;
124                         default:
125                                 p->ok_push = 1;
126                                 break;
127                 }
128         } else {
129                 if (new) {
130                         kfree(p);
131                         return -1;
132                 }       
133         }
134
135         if (new || ovr) {
136                 spin_lock(&p->lock);
137                 p->action = parm->action;
138                 p->eaction = parm->eaction;
139                 if (parm->ifindex) {
140                         p->ifindex = parm->ifindex;
141                         if (ovr)
142                                 dev_put(p->dev);
143                         p->dev = dev;
144                 }
145                 spin_unlock(&p->lock);
146         }
147
148
149         DPRINTK(" tcf_mirred_init index %d action %d eaction %d device %s ifndex %d\n",parm->index,parm->action,parm->eaction,dev->name,parm->ifindex);
150         return new;
151
152 }
153
154 static int
155 tcf_mirred_cleanup(struct tc_action *a, int bind)
156 {
157         struct tcf_mirred *p;
158         p = PRIV(a,mirred);
159         if (NULL != p)
160                 return tcf_mirred_release(p, bind);
161         return 0;
162 }
163
164 static int
165 tcf_mirred(struct sk_buff **pskb, struct tc_action *a)
166 {
167         struct tcf_mirred *p;
168         struct net_device *dev;
169         struct sk_buff *skb2 = NULL;
170         struct sk_buff *skb = *pskb;
171         __u32 at = G_TC_AT(skb->tc_verd);
172
173         if (NULL == a) {
174                 if (net_ratelimit())
175                         printk("BUG: tcf_mirred called with NULL action!\n");
176                 return -1;
177         }
178
179         p = PRIV(a,mirred);
180
181         if (NULL == p) {
182                 if (net_ratelimit())
183                         printk("BUG: tcf_mirred called with NULL params\n");
184                 return -1;
185         }
186
187         spin_lock(&p->lock);
188
189         dev = p->dev;
190         p->tm.lastuse = jiffies;
191
192         if (NULL == dev || !(dev->flags&IFF_UP) ) {
193                 if (net_ratelimit())
194                         printk("mirred to Houston: device %s is gone!\n",
195                                         dev?dev->name:"");
196 bad_mirred:
197                 if (NULL != skb2)
198                         kfree_skb(skb2);
199                 p->qstats.overlimits++;
200                 p->bstats.bytes += skb->len;
201                 p->bstats.packets++;
202                 spin_unlock(&p->lock);
203                 /* should we be asking for packet to be dropped?
204                  * may make sense for redirect case only 
205                 */
206                 return TC_ACT_SHOT;
207         } 
208
209         skb2 = skb_clone(skb, GFP_ATOMIC);
210         if (skb2 == NULL) {
211                 goto bad_mirred;
212         }
213         if (TCA_EGRESS_MIRROR != p->eaction &&
214                 TCA_EGRESS_REDIR != p->eaction) {
215                 if (net_ratelimit())
216                         printk("tcf_mirred unknown action %d\n",p->eaction);
217                 goto bad_mirred;
218         }
219
220         p->bstats.bytes += skb2->len;
221         p->bstats.packets++;
222         if ( !(at & AT_EGRESS)) {
223                 if (p->ok_push) {
224                         skb_push(skb2, skb2->dev->hard_header_len);
225                 }
226         }
227
228         /* mirror is always swallowed */
229         if (TCA_EGRESS_MIRROR != p->eaction)
230                 skb2->tc_verd = SET_TC_FROM(skb2->tc_verd,at);
231
232         skb2->dev = dev;
233         skb2->input_dev = skb->dev;
234         dev_queue_xmit(skb2);
235         spin_unlock(&p->lock);
236         return p->action;
237 }
238
239 static int
240 tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
241 {
242         unsigned char *b = skb->tail;
243         struct tc_mirred opt;
244         struct tcf_mirred *p;
245         struct tcf_t t;
246
247         p = PRIV(a,mirred);
248         if (NULL == p) {
249                 printk("BUG: tcf_mirred_dump called with NULL params\n");
250                 goto rtattr_failure;
251         }
252
253         opt.index = p->index;
254         opt.action = p->action;
255         opt.refcnt = p->refcnt - ref;
256         opt.bindcnt = p->bindcnt - bind;
257         opt.eaction = p->eaction;
258         opt.ifindex = p->ifindex;
259         DPRINTK(" tcf_mirred_dump index %d action %d eaction %d ifndex %d\n",p->index,p->action,p->eaction,p->ifindex);
260         RTA_PUT(skb, TCA_MIRRED_PARMS, sizeof (opt), &opt);
261         t.install = jiffies_to_clock_t(jiffies - p->tm.install);
262         t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
263         t.expires = jiffies_to_clock_t(p->tm.expires);
264         RTA_PUT(skb, TCA_MIRRED_TM, sizeof (t), &t);
265         return skb->len;
266
267       rtattr_failure:
268         skb_trim(skb, b - skb->data);
269         return -1;
270 }
271
272 static struct tc_action_ops act_mirred_ops = {
273         .next           =       NULL,
274         .kind           =       "mirred",
275         .type           =       TCA_ACT_MIRRED,
276         .capab          =       TCA_CAP_NONE,
277         .owner          =       THIS_MODULE,
278         .act            =       tcf_mirred,
279         .dump           =       tcf_mirred_dump,
280         .cleanup        =       tcf_mirred_cleanup,
281         .lookup         =       tcf_hash_search,
282         .init           =       tcf_mirred_init,
283         .walk           =       tcf_generic_walker
284 };
285
286 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
287 MODULE_DESCRIPTION("Device Mirror/redirect actions");
288 MODULE_LICENSE("GPL");
289
290
291 static int __init
292 mirred_init_module(void)
293 {
294         printk("Mirror/redirect action on\n");
295         return tcf_register_action(&act_mirred_ops);
296 }
297
298 static void __exit
299 mirred_cleanup_module(void)
300 {
301         tcf_unregister_action(&act_mirred_ops);
302 }
303
304 module_init(mirred_init_module);
305 module_exit(mirred_cleanup_module);
306