2 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/if_vlan.h>
16 #include <net/netlink.h>
17 #include <net/pkt_sched.h>
19 #include <linux/tc_act/tc_vlan.h>
20 #include <net/tc_act/tc_vlan.h>
22 #define VLAN_TAB_MASK 15
24 static int tcf_vlan(struct sk_buff
*skb
, const struct tc_action
*a
,
25 struct tcf_result
*res
)
27 struct tcf_vlan
*v
= a
->priv
;
31 spin_lock(&v
->tcf_lock
);
32 v
->tcf_tm
.lastuse
= jiffies
;
33 bstats_update(&v
->tcf_bstats
, skb
);
34 action
= v
->tcf_action
;
36 switch (v
->tcfv_action
) {
37 case TCA_VLAN_ACT_POP
:
38 err
= skb_vlan_pop(skb
);
42 case TCA_VLAN_ACT_PUSH
:
43 err
= skb_vlan_push(skb
, v
->tcfv_push_proto
, v
->tcfv_push_vid
);
55 v
->tcf_qstats
.drops
++;
57 spin_unlock(&v
->tcf_lock
);
61 static const struct nla_policy vlan_policy
[TCA_VLAN_MAX
+ 1] = {
62 [TCA_VLAN_PARMS
] = { .len
= sizeof(struct tc_vlan
) },
63 [TCA_VLAN_PUSH_VLAN_ID
] = { .type
= NLA_U16
},
64 [TCA_VLAN_PUSH_VLAN_PROTOCOL
] = { .type
= NLA_U16
},
67 static int tcf_vlan_init(struct net
*net
, struct nlattr
*nla
,
68 struct nlattr
*est
, struct tc_action
*a
,
71 struct nlattr
*tb
[TCA_VLAN_MAX
+ 1];
76 __be16 push_proto
= 0;
83 err
= nla_parse_nested(tb
, TCA_VLAN_MAX
, nla
, vlan_policy
);
87 if (!tb
[TCA_VLAN_PARMS
])
89 parm
= nla_data(tb
[TCA_VLAN_PARMS
]);
90 switch (parm
->v_action
) {
91 case TCA_VLAN_ACT_POP
:
93 case TCA_VLAN_ACT_PUSH
:
94 if (!tb
[TCA_VLAN_PUSH_VLAN_ID
])
96 push_vid
= nla_get_u16(tb
[TCA_VLAN_PUSH_VLAN_ID
]);
97 if (push_vid
>= VLAN_VID_MASK
)
100 if (tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]) {
101 push_proto
= nla_get_be16(tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]);
102 switch (push_proto
) {
103 case htons(ETH_P_8021Q
):
104 case htons(ETH_P_8021AD
):
107 return -EPROTONOSUPPORT
;
110 push_proto
= htons(ETH_P_8021Q
);
116 action
= parm
->v_action
;
118 if (!tcf_hash_check(parm
->index
, a
, bind
)) {
119 ret
= tcf_hash_create(parm
->index
, est
, a
, sizeof(*v
), bind
);
127 tcf_hash_release(a
, bind
);
134 spin_lock_bh(&v
->tcf_lock
);
136 v
->tcfv_action
= action
;
137 v
->tcfv_push_vid
= push_vid
;
138 v
->tcfv_push_proto
= push_proto
;
140 v
->tcf_action
= parm
->action
;
142 spin_unlock_bh(&v
->tcf_lock
);
144 if (ret
== ACT_P_CREATED
)
149 static int tcf_vlan_dump(struct sk_buff
*skb
, struct tc_action
*a
,
152 unsigned char *b
= skb_tail_pointer(skb
);
153 struct tcf_vlan
*v
= a
->priv
;
154 struct tc_vlan opt
= {
155 .index
= v
->tcf_index
,
156 .refcnt
= v
->tcf_refcnt
- ref
,
157 .bindcnt
= v
->tcf_bindcnt
- bind
,
158 .action
= v
->tcf_action
,
159 .v_action
= v
->tcfv_action
,
163 if (nla_put(skb
, TCA_VLAN_PARMS
, sizeof(opt
), &opt
))
164 goto nla_put_failure
;
166 if (v
->tcfv_action
== TCA_VLAN_ACT_PUSH
&&
167 (nla_put_u16(skb
, TCA_VLAN_PUSH_VLAN_ID
, v
->tcfv_push_vid
) ||
168 nla_put_be16(skb
, TCA_VLAN_PUSH_VLAN_PROTOCOL
, v
->tcfv_push_proto
)))
169 goto nla_put_failure
;
171 t
.install
= jiffies_to_clock_t(jiffies
- v
->tcf_tm
.install
);
172 t
.lastuse
= jiffies_to_clock_t(jiffies
- v
->tcf_tm
.lastuse
);
173 t
.expires
= jiffies_to_clock_t(v
->tcf_tm
.expires
);
174 if (nla_put(skb
, TCA_VLAN_TM
, sizeof(t
), &t
))
175 goto nla_put_failure
;
183 static struct tc_action_ops act_vlan_ops
= {
185 .type
= TCA_ACT_VLAN
,
186 .owner
= THIS_MODULE
,
188 .dump
= tcf_vlan_dump
,
189 .init
= tcf_vlan_init
,
192 static int __init
vlan_init_module(void)
194 return tcf_register_action(&act_vlan_ops
, VLAN_TAB_MASK
);
197 static void __exit
vlan_cleanup_module(void)
199 tcf_unregister_action(&act_vlan_ops
);
202 module_init(vlan_init_module
);
203 module_exit(vlan_cleanup_module
);
205 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
206 MODULE_DESCRIPTION("vlan manipulation actions");
207 MODULE_LICENSE("GPL v2");