xref: /openbmc/linux/net/sched/act_vlan.c (revision fceb6435)
1c7e2b968SJiri Pirko /*
2c7e2b968SJiri Pirko  * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
3c7e2b968SJiri Pirko  *
4c7e2b968SJiri Pirko  * This program is free software; you can redistribute it and/or modify
5c7e2b968SJiri Pirko  * it under the terms of the GNU General Public License as published by
6c7e2b968SJiri Pirko  * the Free Software Foundation; either version 2 of the License, or
7c7e2b968SJiri Pirko  * (at your option) any later version.
8c7e2b968SJiri Pirko  */
9c7e2b968SJiri Pirko 
10c7e2b968SJiri Pirko #include <linux/module.h>
11c7e2b968SJiri Pirko #include <linux/init.h>
12c7e2b968SJiri Pirko #include <linux/kernel.h>
13c7e2b968SJiri Pirko #include <linux/skbuff.h>
14c7e2b968SJiri Pirko #include <linux/rtnetlink.h>
15c7e2b968SJiri Pirko #include <linux/if_vlan.h>
16c7e2b968SJiri Pirko #include <net/netlink.h>
17c7e2b968SJiri Pirko #include <net/pkt_sched.h>
18c7e2b968SJiri Pirko 
19c7e2b968SJiri Pirko #include <linux/tc_act/tc_vlan.h>
20c7e2b968SJiri Pirko #include <net/tc_act/tc_vlan.h>
21c7e2b968SJiri Pirko 
22c7e2b968SJiri Pirko #define VLAN_TAB_MASK     15
23c7e2b968SJiri Pirko 
24c7d03a00SAlexey Dobriyan static unsigned int vlan_net_id;
25a85a970aSWANG Cong static struct tc_action_ops act_vlan_ops;
26ddf97ccdSWANG Cong 
27c7e2b968SJiri Pirko static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
28c7e2b968SJiri Pirko 		    struct tcf_result *res)
29c7e2b968SJiri Pirko {
30a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
31c7e2b968SJiri Pirko 	int action;
32c7e2b968SJiri Pirko 	int err;
3345a497f2SShmulik Ladkani 	u16 tci;
34c7e2b968SJiri Pirko 
35c7e2b968SJiri Pirko 	spin_lock(&v->tcf_lock);
369c4a4e48SJamal Hadi Salim 	tcf_lastuse_update(&v->tcf_tm);
37c7e2b968SJiri Pirko 	bstats_update(&v->tcf_bstats, skb);
38c7e2b968SJiri Pirko 	action = v->tcf_action;
39c7e2b968SJiri Pirko 
40f39acc84SShmulik Ladkani 	/* Ensure 'data' points at mac_header prior calling vlan manipulating
41f39acc84SShmulik Ladkani 	 * functions.
42f39acc84SShmulik Ladkani 	 */
43f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
44f39acc84SShmulik Ladkani 		skb_push_rcsum(skb, skb->mac_len);
45f39acc84SShmulik Ladkani 
46c7e2b968SJiri Pirko 	switch (v->tcfv_action) {
47c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
48c7e2b968SJiri Pirko 		err = skb_vlan_pop(skb);
49c7e2b968SJiri Pirko 		if (err)
50c7e2b968SJiri Pirko 			goto drop;
51c7e2b968SJiri Pirko 		break;
52c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
53956af371SHadar Hen Zion 		err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid |
54956af371SHadar Hen Zion 				    (v->tcfv_push_prio << VLAN_PRIO_SHIFT));
55c7e2b968SJiri Pirko 		if (err)
56c7e2b968SJiri Pirko 			goto drop;
57c7e2b968SJiri Pirko 		break;
5845a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
5945a497f2SShmulik Ladkani 		/* No-op if no vlan tag (either hw-accel or in-payload) */
6045a497f2SShmulik Ladkani 		if (!skb_vlan_tagged(skb))
6145a497f2SShmulik Ladkani 			goto unlock;
6245a497f2SShmulik Ladkani 		/* extract existing tag (and guarantee no hw-accel tag) */
6345a497f2SShmulik Ladkani 		if (skb_vlan_tag_present(skb)) {
6445a497f2SShmulik Ladkani 			tci = skb_vlan_tag_get(skb);
6545a497f2SShmulik Ladkani 			skb->vlan_tci = 0;
6645a497f2SShmulik Ladkani 		} else {
6745a497f2SShmulik Ladkani 			/* in-payload vlan tag, pop it */
6845a497f2SShmulik Ladkani 			err = __skb_vlan_pop(skb, &tci);
6945a497f2SShmulik Ladkani 			if (err)
7045a497f2SShmulik Ladkani 				goto drop;
7145a497f2SShmulik Ladkani 		}
7245a497f2SShmulik Ladkani 		/* replace the vid */
7345a497f2SShmulik Ladkani 		tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid;
7445a497f2SShmulik Ladkani 		/* replace prio bits, if tcfv_push_prio specified */
7545a497f2SShmulik Ladkani 		if (v->tcfv_push_prio) {
7645a497f2SShmulik Ladkani 			tci &= ~VLAN_PRIO_MASK;
7745a497f2SShmulik Ladkani 			tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT;
7845a497f2SShmulik Ladkani 		}
7945a497f2SShmulik Ladkani 		/* put updated tci as hwaccel tag */
8045a497f2SShmulik Ladkani 		__vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci);
8145a497f2SShmulik Ladkani 		break;
82c7e2b968SJiri Pirko 	default:
83c7e2b968SJiri Pirko 		BUG();
84c7e2b968SJiri Pirko 	}
85c7e2b968SJiri Pirko 
86c7e2b968SJiri Pirko 	goto unlock;
87c7e2b968SJiri Pirko 
88c7e2b968SJiri Pirko drop:
89c7e2b968SJiri Pirko 	action = TC_ACT_SHOT;
90c7e2b968SJiri Pirko 	v->tcf_qstats.drops++;
91c7e2b968SJiri Pirko unlock:
92f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
93f39acc84SShmulik Ladkani 		skb_pull_rcsum(skb, skb->mac_len);
94f39acc84SShmulik Ladkani 
95c7e2b968SJiri Pirko 	spin_unlock(&v->tcf_lock);
96c7e2b968SJiri Pirko 	return action;
97c7e2b968SJiri Pirko }
98c7e2b968SJiri Pirko 
99c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
100c7e2b968SJiri Pirko 	[TCA_VLAN_PARMS]		= { .len = sizeof(struct tc_vlan) },
101c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_ID]		= { .type = NLA_U16 },
102c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_PROTOCOL]	= { .type = NLA_U16 },
103956af371SHadar Hen Zion 	[TCA_VLAN_PUSH_VLAN_PRIORITY]	= { .type = NLA_U8 },
104c7e2b968SJiri Pirko };
105c7e2b968SJiri Pirko 
106c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla,
107a85a970aSWANG Cong 			 struct nlattr *est, struct tc_action **a,
108c7e2b968SJiri Pirko 			 int ovr, int bind)
109c7e2b968SJiri Pirko {
110ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
111c7e2b968SJiri Pirko 	struct nlattr *tb[TCA_VLAN_MAX + 1];
112c7e2b968SJiri Pirko 	struct tc_vlan *parm;
113c7e2b968SJiri Pirko 	struct tcf_vlan *v;
114c7e2b968SJiri Pirko 	int action;
115c7e2b968SJiri Pirko 	__be16 push_vid = 0;
116c7e2b968SJiri Pirko 	__be16 push_proto = 0;
117956af371SHadar Hen Zion 	u8 push_prio = 0;
118b2313077SWANG Cong 	bool exists = false;
119b2313077SWANG Cong 	int ret = 0, err;
120c7e2b968SJiri Pirko 
121c7e2b968SJiri Pirko 	if (!nla)
122c7e2b968SJiri Pirko 		return -EINVAL;
123c7e2b968SJiri Pirko 
124fceb6435SJohannes Berg 	err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy, NULL);
125c7e2b968SJiri Pirko 	if (err < 0)
126c7e2b968SJiri Pirko 		return err;
127c7e2b968SJiri Pirko 
128c7e2b968SJiri Pirko 	if (!tb[TCA_VLAN_PARMS])
129c7e2b968SJiri Pirko 		return -EINVAL;
130c7e2b968SJiri Pirko 	parm = nla_data(tb[TCA_VLAN_PARMS]);
1315026c9b1SJamal Hadi Salim 	exists = tcf_hash_check(tn, parm->index, a, bind);
1325026c9b1SJamal Hadi Salim 	if (exists && bind)
1335026c9b1SJamal Hadi Salim 		return 0;
1345026c9b1SJamal Hadi Salim 
135c7e2b968SJiri Pirko 	switch (parm->v_action) {
136c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
137c7e2b968SJiri Pirko 		break;
138c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
13945a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
1405026c9b1SJamal Hadi Salim 		if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
1415026c9b1SJamal Hadi Salim 			if (exists)
142a85a970aSWANG Cong 				tcf_hash_release(*a, bind);
143c7e2b968SJiri Pirko 			return -EINVAL;
1445026c9b1SJamal Hadi Salim 		}
145c7e2b968SJiri Pirko 		push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
1465026c9b1SJamal Hadi Salim 		if (push_vid >= VLAN_VID_MASK) {
1475026c9b1SJamal Hadi Salim 			if (exists)
148a85a970aSWANG Cong 				tcf_hash_release(*a, bind);
149c7e2b968SJiri Pirko 			return -ERANGE;
1505026c9b1SJamal Hadi Salim 		}
151c7e2b968SJiri Pirko 
152c7e2b968SJiri Pirko 		if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
153c7e2b968SJiri Pirko 			push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
154c7e2b968SJiri Pirko 			switch (push_proto) {
155c7e2b968SJiri Pirko 			case htons(ETH_P_8021Q):
156c7e2b968SJiri Pirko 			case htons(ETH_P_8021AD):
157c7e2b968SJiri Pirko 				break;
158c7e2b968SJiri Pirko 			default:
159c7e2b968SJiri Pirko 				return -EPROTONOSUPPORT;
160c7e2b968SJiri Pirko 			}
161c7e2b968SJiri Pirko 		} else {
162c7e2b968SJiri Pirko 			push_proto = htons(ETH_P_8021Q);
163c7e2b968SJiri Pirko 		}
164956af371SHadar Hen Zion 
165956af371SHadar Hen Zion 		if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
166956af371SHadar Hen Zion 			push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
167c7e2b968SJiri Pirko 		break;
168c7e2b968SJiri Pirko 	default:
1695026c9b1SJamal Hadi Salim 		if (exists)
170a85a970aSWANG Cong 			tcf_hash_release(*a, bind);
171c7e2b968SJiri Pirko 		return -EINVAL;
172c7e2b968SJiri Pirko 	}
173c7e2b968SJiri Pirko 	action = parm->v_action;
174c7e2b968SJiri Pirko 
1755026c9b1SJamal Hadi Salim 	if (!exists) {
176ddf97ccdSWANG Cong 		ret = tcf_hash_create(tn, parm->index, est, a,
177a85a970aSWANG Cong 				      &act_vlan_ops, bind, false);
178c7e2b968SJiri Pirko 		if (ret)
179c7e2b968SJiri Pirko 			return ret;
180c7e2b968SJiri Pirko 
181c7e2b968SJiri Pirko 		ret = ACT_P_CREATED;
182c7e2b968SJiri Pirko 	} else {
183a85a970aSWANG Cong 		tcf_hash_release(*a, bind);
184c7e2b968SJiri Pirko 		if (!ovr)
185c7e2b968SJiri Pirko 			return -EEXIST;
186c7e2b968SJiri Pirko 	}
187c7e2b968SJiri Pirko 
188a85a970aSWANG Cong 	v = to_vlan(*a);
189c7e2b968SJiri Pirko 
190c7e2b968SJiri Pirko 	spin_lock_bh(&v->tcf_lock);
191c7e2b968SJiri Pirko 
192c7e2b968SJiri Pirko 	v->tcfv_action = action;
193c7e2b968SJiri Pirko 	v->tcfv_push_vid = push_vid;
194956af371SHadar Hen Zion 	v->tcfv_push_prio = push_prio;
195c7e2b968SJiri Pirko 	v->tcfv_push_proto = push_proto;
196c7e2b968SJiri Pirko 
197c7e2b968SJiri Pirko 	v->tcf_action = parm->action;
198c7e2b968SJiri Pirko 
199c7e2b968SJiri Pirko 	spin_unlock_bh(&v->tcf_lock);
200c7e2b968SJiri Pirko 
201c7e2b968SJiri Pirko 	if (ret == ACT_P_CREATED)
202a85a970aSWANG Cong 		tcf_hash_insert(tn, *a);
203c7e2b968SJiri Pirko 	return ret;
204c7e2b968SJiri Pirko }
205c7e2b968SJiri Pirko 
206c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
207c7e2b968SJiri Pirko 			 int bind, int ref)
208c7e2b968SJiri Pirko {
209c7e2b968SJiri Pirko 	unsigned char *b = skb_tail_pointer(skb);
210a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
211c7e2b968SJiri Pirko 	struct tc_vlan opt = {
212c7e2b968SJiri Pirko 		.index    = v->tcf_index,
213c7e2b968SJiri Pirko 		.refcnt   = v->tcf_refcnt - ref,
214c7e2b968SJiri Pirko 		.bindcnt  = v->tcf_bindcnt - bind,
215c7e2b968SJiri Pirko 		.action   = v->tcf_action,
216c7e2b968SJiri Pirko 		.v_action = v->tcfv_action,
217c7e2b968SJiri Pirko 	};
218c7e2b968SJiri Pirko 	struct tcf_t t;
219c7e2b968SJiri Pirko 
220c7e2b968SJiri Pirko 	if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
221c7e2b968SJiri Pirko 		goto nla_put_failure;
222c7e2b968SJiri Pirko 
22345a497f2SShmulik Ladkani 	if ((v->tcfv_action == TCA_VLAN_ACT_PUSH ||
22445a497f2SShmulik Ladkani 	     v->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
225c7e2b968SJiri Pirko 	    (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
2260b0f43feSJamal Hadi Salim 	     nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
227956af371SHadar Hen Zion 			  v->tcfv_push_proto) ||
228956af371SHadar Hen Zion 	     (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
229956af371SHadar Hen Zion 					      v->tcfv_push_prio))))
230c7e2b968SJiri Pirko 		goto nla_put_failure;
231c7e2b968SJiri Pirko 
23248d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &v->tcf_tm);
2339854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
234c7e2b968SJiri Pirko 		goto nla_put_failure;
235c7e2b968SJiri Pirko 	return skb->len;
236c7e2b968SJiri Pirko 
237c7e2b968SJiri Pirko nla_put_failure:
238c7e2b968SJiri Pirko 	nlmsg_trim(skb, b);
239c7e2b968SJiri Pirko 	return -1;
240c7e2b968SJiri Pirko }
241c7e2b968SJiri Pirko 
242ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
243ddf97ccdSWANG Cong 			   struct netlink_callback *cb, int type,
244a85a970aSWANG Cong 			   const struct tc_action_ops *ops)
245ddf97ccdSWANG Cong {
246ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
247ddf97ccdSWANG Cong 
248a85a970aSWANG Cong 	return tcf_generic_walker(tn, skb, cb, type, ops);
249ddf97ccdSWANG Cong }
250ddf97ccdSWANG Cong 
251a85a970aSWANG Cong static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
252ddf97ccdSWANG Cong {
253ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
254ddf97ccdSWANG Cong 
255ddf97ccdSWANG Cong 	return tcf_hash_search(tn, a, index);
256ddf97ccdSWANG Cong }
257ddf97ccdSWANG Cong 
258c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = {
259c7e2b968SJiri Pirko 	.kind		=	"vlan",
260c7e2b968SJiri Pirko 	.type		=	TCA_ACT_VLAN,
261c7e2b968SJiri Pirko 	.owner		=	THIS_MODULE,
262c7e2b968SJiri Pirko 	.act		=	tcf_vlan,
263c7e2b968SJiri Pirko 	.dump		=	tcf_vlan_dump,
264c7e2b968SJiri Pirko 	.init		=	tcf_vlan_init,
265ddf97ccdSWANG Cong 	.walk		=	tcf_vlan_walker,
266ddf97ccdSWANG Cong 	.lookup		=	tcf_vlan_search,
267a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_vlan),
268ddf97ccdSWANG Cong };
269ddf97ccdSWANG Cong 
270ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net)
271ddf97ccdSWANG Cong {
272ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
273ddf97ccdSWANG Cong 
274ddf97ccdSWANG Cong 	return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
275ddf97ccdSWANG Cong }
276ddf97ccdSWANG Cong 
277ddf97ccdSWANG Cong static void __net_exit vlan_exit_net(struct net *net)
278ddf97ccdSWANG Cong {
279ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
280ddf97ccdSWANG Cong 
281ddf97ccdSWANG Cong 	tc_action_net_exit(tn);
282ddf97ccdSWANG Cong }
283ddf97ccdSWANG Cong 
284ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = {
285ddf97ccdSWANG Cong 	.init = vlan_init_net,
286ddf97ccdSWANG Cong 	.exit = vlan_exit_net,
287ddf97ccdSWANG Cong 	.id   = &vlan_net_id,
288ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
289c7e2b968SJiri Pirko };
290c7e2b968SJiri Pirko 
291c7e2b968SJiri Pirko static int __init vlan_init_module(void)
292c7e2b968SJiri Pirko {
293ddf97ccdSWANG Cong 	return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
294c7e2b968SJiri Pirko }
295c7e2b968SJiri Pirko 
296c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void)
297c7e2b968SJiri Pirko {
298ddf97ccdSWANG Cong 	tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
299c7e2b968SJiri Pirko }
300c7e2b968SJiri Pirko 
301c7e2b968SJiri Pirko module_init(vlan_init_module);
302c7e2b968SJiri Pirko module_exit(vlan_cleanup_module);
303c7e2b968SJiri Pirko 
304c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
305c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions");
306c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2");
307