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 vlan_net_id;
25 static struct tc_action_ops act_vlan_ops;
27 static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
28 struct tcf_result *res)
30 struct tcf_vlan *v = to_vlan(a);
35 spin_lock(&v->tcf_lock);
36 tcf_lastuse_update(&v->tcf_tm);
37 bstats_update(&v->tcf_bstats, skb);
38 action = v->tcf_action;
40 switch (v->tcfv_action) {
41 case TCA_VLAN_ACT_POP:
42 err = skb_vlan_pop(skb);
46 case TCA_VLAN_ACT_PUSH:
47 err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid |
48 (v->tcfv_push_prio << VLAN_PRIO_SHIFT));
52 case TCA_VLAN_ACT_MODIFY:
53 /* No-op if no vlan tag (either hw-accel or in-payload) */
54 if (!skb_vlan_tagged(skb))
56 /* extract existing tag (and guarantee no hw-accel tag) */
57 if (skb_vlan_tag_present(skb)) {
58 tci = skb_vlan_tag_get(skb);
61 /* in-payload vlan tag, pop it */
62 err = __skb_vlan_pop(skb, &tci);
67 tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid;
68 /* replace prio bits, if tcfv_push_prio specified */
69 if (v->tcfv_push_prio) {
70 tci &= ~VLAN_PRIO_MASK;
71 tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT;
73 /* put updated tci as hwaccel tag */
74 __vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci);
84 v->tcf_qstats.drops++;
86 spin_unlock(&v->tcf_lock);
90 static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
91 [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
92 [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
93 [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
94 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
97 static int tcf_vlan_init(struct net *net, struct nlattr *nla,
98 struct nlattr *est, struct tc_action **a,
101 struct tc_action_net *tn = net_generic(net, vlan_net_id);
102 struct nlattr *tb[TCA_VLAN_MAX + 1];
103 struct tc_vlan *parm;
107 __be16 push_proto = 0;
115 err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
119 if (!tb[TCA_VLAN_PARMS])
121 parm = nla_data(tb[TCA_VLAN_PARMS]);
122 exists = tcf_hash_check(tn, parm->index, a, bind);
126 switch (parm->v_action) {
127 case TCA_VLAN_ACT_POP:
129 case TCA_VLAN_ACT_PUSH:
130 case TCA_VLAN_ACT_MODIFY:
131 if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
133 tcf_hash_release(*a, bind);
136 push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
137 if (push_vid >= VLAN_VID_MASK) {
139 tcf_hash_release(*a, bind);
143 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
144 push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
145 switch (push_proto) {
146 case htons(ETH_P_8021Q):
147 case htons(ETH_P_8021AD):
150 return -EPROTONOSUPPORT;
153 push_proto = htons(ETH_P_8021Q);
156 if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
157 push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
161 tcf_hash_release(*a, bind);
164 action = parm->v_action;
167 ret = tcf_hash_create(tn, parm->index, est, a,
168 &act_vlan_ops, bind, false);
174 tcf_hash_release(*a, bind);
181 spin_lock_bh(&v->tcf_lock);
183 v->tcfv_action = action;
184 v->tcfv_push_vid = push_vid;
185 v->tcfv_push_prio = push_prio;
186 v->tcfv_push_proto = push_proto;
188 v->tcf_action = parm->action;
190 spin_unlock_bh(&v->tcf_lock);
192 if (ret == ACT_P_CREATED)
193 tcf_hash_insert(tn, *a);
197 static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
200 unsigned char *b = skb_tail_pointer(skb);
201 struct tcf_vlan *v = to_vlan(a);
202 struct tc_vlan opt = {
203 .index = v->tcf_index,
204 .refcnt = v->tcf_refcnt - ref,
205 .bindcnt = v->tcf_bindcnt - bind,
206 .action = v->tcf_action,
207 .v_action = v->tcfv_action,
211 if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
212 goto nla_put_failure;
214 if ((v->tcfv_action == TCA_VLAN_ACT_PUSH ||
215 v->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
216 (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
217 nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
218 v->tcfv_push_proto) ||
219 (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
220 v->tcfv_push_prio))))
221 goto nla_put_failure;
223 tcf_tm_dump(&t, &v->tcf_tm);
224 if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
225 goto nla_put_failure;
233 static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
234 struct netlink_callback *cb, int type,
235 const struct tc_action_ops *ops)
237 struct tc_action_net *tn = net_generic(net, vlan_net_id);
239 return tcf_generic_walker(tn, skb, cb, type, ops);
242 static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
244 struct tc_action_net *tn = net_generic(net, vlan_net_id);
246 return tcf_hash_search(tn, a, index);
249 static struct tc_action_ops act_vlan_ops = {
251 .type = TCA_ACT_VLAN,
252 .owner = THIS_MODULE,
254 .dump = tcf_vlan_dump,
255 .init = tcf_vlan_init,
256 .walk = tcf_vlan_walker,
257 .lookup = tcf_vlan_search,
258 .size = sizeof(struct tcf_vlan),
261 static __net_init int vlan_init_net(struct net *net)
263 struct tc_action_net *tn = net_generic(net, vlan_net_id);
265 return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
268 static void __net_exit vlan_exit_net(struct net *net)
270 struct tc_action_net *tn = net_generic(net, vlan_net_id);
272 tc_action_net_exit(tn);
275 static struct pernet_operations vlan_net_ops = {
276 .init = vlan_init_net,
277 .exit = vlan_exit_net,
279 .size = sizeof(struct tc_action_net),
282 static int __init vlan_init_module(void)
284 return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
287 static void __exit vlan_cleanup_module(void)
289 tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
292 module_init(vlan_init_module);
293 module_exit(vlan_cleanup_module);
295 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
296 MODULE_DESCRIPTION("vlan manipulation actions");
297 MODULE_LICENSE("GPL v2");