2 * net/sched/mirred.c packet mirroring and redirect actions
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.
9 * Authors: Jamal Hadi Salim (2002-4)
11 * TODO: Add ingress support (and socket redirect support)
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>
24 #include <linux/socket.h>
25 #include <linux/sockios.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>
36 #include <net/pkt_sched.h>
37 #include <linux/tc_act/tc_mirred.h>
38 #include <net/tc_act/tc_mirred.h>
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
44 /* use generic hash table */
46 #define MY_TAB_MASK (MY_TAB_SIZE - 1)
48 static struct tcf_mirred
*tcf_mirred_ht
[MY_TAB_SIZE
];
49 static DEFINE_RWLOCK(mirred_lock
);
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
57 #define CONFIG_NET_ACT_INIT 1
58 #include <net/pkt_act.h>
61 tcf_mirred_release(struct tcf_mirred
*p
, int bind
)
67 if(!p
->bindcnt
&& p
->refcnt
<= 0) {
77 tcf_mirred_init(struct rtattr
*rta
, struct rtattr
*est
, struct tc_action
*a
,
80 struct rtattr
*tb
[TCA_MIRRED_MAX
];
81 struct tc_mirred
*parm
;
83 struct net_device
*dev
= NULL
;
87 if (rta
== NULL
|| rtattr_parse_nested(tb
, TCA_MIRRED_MAX
, rta
) < 0)
90 if (tb
[TCA_MIRRED_PARMS
-1] == NULL
||
91 RTA_PAYLOAD(tb
[TCA_MIRRED_PARMS
-1]) < sizeof(*parm
))
93 parm
= RTA_DATA(tb
[TCA_MIRRED_PARMS
-1]);
96 dev
= __dev_get_by_index(parm
->ifindex
);
114 p
= tcf_hash_check(parm
->index
, a
, ovr
, bind
);
118 p
= tcf_hash_create(parm
->index
, est
, a
, sizeof(*p
), ovr
, bind
);
124 tcf_mirred_release(p
, bind
);
129 spin_lock_bh(&p
->lock
);
130 p
->action
= parm
->action
;
131 p
->eaction
= parm
->eaction
;
133 p
->ifindex
= parm
->ifindex
;
134 if (ret
!= ACT_P_CREATED
)
138 p
->ok_push
= ok_push
;
140 spin_unlock_bh(&p
->lock
);
141 if (ret
== ACT_P_CREATED
)
144 DPRINTK("tcf_mirred_init index %d action %d eaction %d device %s "
145 "ifindex %d\n", parm
->index
, parm
->action
, parm
->eaction
,
146 dev
->name
, parm
->ifindex
);
151 tcf_mirred_cleanup(struct tc_action
*a
, int bind
)
153 struct tcf_mirred
*p
= PRIV(a
, mirred
);
156 return tcf_mirred_release(p
, bind
);
161 tcf_mirred(struct sk_buff
*skb
, struct tc_action
*a
, struct tcf_result
*res
)
163 struct tcf_mirred
*p
= PRIV(a
, mirred
);
164 struct net_device
*dev
;
165 struct sk_buff
*skb2
= NULL
;
166 u32 at
= G_TC_AT(skb
->tc_verd
);
171 p
->tm
.lastuse
= jiffies
;
173 if (!(dev
->flags
&IFF_UP
) ) {
175 printk("mirred to Houston: device %s is gone!\n",
180 p
->qstats
.overlimits
++;
181 p
->bstats
.bytes
+= skb
->len
;
183 spin_unlock(&p
->lock
);
184 /* should we be asking for packet to be dropped?
185 * may make sense for redirect case only
190 skb2
= skb_clone(skb
, GFP_ATOMIC
);
193 if (p
->eaction
!= TCA_EGRESS_MIRROR
&& p
->eaction
!= TCA_EGRESS_REDIR
) {
195 printk("tcf_mirred unknown action %d\n", p
->eaction
);
199 p
->bstats
.bytes
+= skb2
->len
;
201 if (!(at
& AT_EGRESS
))
203 skb_push(skb2
, skb2
->dev
->hard_header_len
);
205 /* mirror is always swallowed */
206 if (p
->eaction
!= TCA_EGRESS_MIRROR
)
207 skb2
->tc_verd
= SET_TC_FROM(skb2
->tc_verd
, at
);
210 skb2
->input_dev
= skb
->dev
;
211 dev_queue_xmit(skb2
);
212 spin_unlock(&p
->lock
);
217 tcf_mirred_dump(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
219 unsigned char *b
= skb
->tail
;
220 struct tc_mirred opt
;
221 struct tcf_mirred
*p
= PRIV(a
, mirred
);
224 opt
.index
= p
->index
;
225 opt
.action
= p
->action
;
226 opt
.refcnt
= p
->refcnt
- ref
;
227 opt
.bindcnt
= p
->bindcnt
- bind
;
228 opt
.eaction
= p
->eaction
;
229 opt
.ifindex
= p
->ifindex
;
230 DPRINTK("tcf_mirred_dump index %d action %d eaction %d ifindex %d\n",
231 p
->index
, p
->action
, p
->eaction
, p
->ifindex
);
232 RTA_PUT(skb
, TCA_MIRRED_PARMS
, sizeof(opt
), &opt
);
233 t
.install
= jiffies_to_clock_t(jiffies
- p
->tm
.install
);
234 t
.lastuse
= jiffies_to_clock_t(jiffies
- p
->tm
.lastuse
);
235 t
.expires
= jiffies_to_clock_t(p
->tm
.expires
);
236 RTA_PUT(skb
, TCA_MIRRED_TM
, sizeof(t
), &t
);
240 skb_trim(skb
, b
- skb
->data
);
244 static struct tc_action_ops act_mirred_ops
= {
246 .type
= TCA_ACT_MIRRED
,
247 .capab
= TCA_CAP_NONE
,
248 .owner
= THIS_MODULE
,
250 .dump
= tcf_mirred_dump
,
251 .cleanup
= tcf_mirred_cleanup
,
252 .lookup
= tcf_hash_search
,
253 .init
= tcf_mirred_init
,
254 .walk
= tcf_generic_walker
257 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
258 MODULE_DESCRIPTION("Device Mirror/redirect actions");
259 MODULE_LICENSE("GPL");
262 mirred_init_module(void)
264 printk("Mirror/redirect action on\n");
265 return tcf_register_action(&act_mirred_ops
);
269 mirred_cleanup_module(void)
271 tcf_unregister_action(&act_mirred_ops
);
274 module_init(mirred_init_module
);
275 module_exit(mirred_cleanup_module
);