xref: /openbmc/linux/net/sched/act_vlan.c (revision 45a497f2d149a4a8061c61518a79d59f1f3034b2)
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 
24ddf97ccdSWANG Cong static 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;
33*45a497f2SShmulik 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 
40c7e2b968SJiri Pirko 	switch (v->tcfv_action) {
41c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
42c7e2b968SJiri Pirko 		err = skb_vlan_pop(skb);
43c7e2b968SJiri Pirko 		if (err)
44c7e2b968SJiri Pirko 			goto drop;
45c7e2b968SJiri Pirko 		break;
46c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
47956af371SHadar Hen Zion 		err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid |
48956af371SHadar Hen Zion 				    (v->tcfv_push_prio << VLAN_PRIO_SHIFT));
49c7e2b968SJiri Pirko 		if (err)
50c7e2b968SJiri Pirko 			goto drop;
51c7e2b968SJiri Pirko 		break;
52*45a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
53*45a497f2SShmulik Ladkani 		/* No-op if no vlan tag (either hw-accel or in-payload) */
54*45a497f2SShmulik Ladkani 		if (!skb_vlan_tagged(skb))
55*45a497f2SShmulik Ladkani 			goto unlock;
56*45a497f2SShmulik Ladkani 		/* extract existing tag (and guarantee no hw-accel tag) */
57*45a497f2SShmulik Ladkani 		if (skb_vlan_tag_present(skb)) {
58*45a497f2SShmulik Ladkani 			tci = skb_vlan_tag_get(skb);
59*45a497f2SShmulik Ladkani 			skb->vlan_tci = 0;
60*45a497f2SShmulik Ladkani 		} else {
61*45a497f2SShmulik Ladkani 			/* in-payload vlan tag, pop it */
62*45a497f2SShmulik Ladkani 			err = __skb_vlan_pop(skb, &tci);
63*45a497f2SShmulik Ladkani 			if (err)
64*45a497f2SShmulik Ladkani 				goto drop;
65*45a497f2SShmulik Ladkani 		}
66*45a497f2SShmulik Ladkani 		/* replace the vid */
67*45a497f2SShmulik Ladkani 		tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid;
68*45a497f2SShmulik Ladkani 		/* replace prio bits, if tcfv_push_prio specified */
69*45a497f2SShmulik Ladkani 		if (v->tcfv_push_prio) {
70*45a497f2SShmulik Ladkani 			tci &= ~VLAN_PRIO_MASK;
71*45a497f2SShmulik Ladkani 			tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT;
72*45a497f2SShmulik Ladkani 		}
73*45a497f2SShmulik Ladkani 		/* put updated tci as hwaccel tag */
74*45a497f2SShmulik Ladkani 		__vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci);
75*45a497f2SShmulik Ladkani 		break;
76c7e2b968SJiri Pirko 	default:
77c7e2b968SJiri Pirko 		BUG();
78c7e2b968SJiri Pirko 	}
79c7e2b968SJiri Pirko 
80c7e2b968SJiri Pirko 	goto unlock;
81c7e2b968SJiri Pirko 
82c7e2b968SJiri Pirko drop:
83c7e2b968SJiri Pirko 	action = TC_ACT_SHOT;
84c7e2b968SJiri Pirko 	v->tcf_qstats.drops++;
85c7e2b968SJiri Pirko unlock:
86c7e2b968SJiri Pirko 	spin_unlock(&v->tcf_lock);
87c7e2b968SJiri Pirko 	return action;
88c7e2b968SJiri Pirko }
89c7e2b968SJiri Pirko 
90c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
91c7e2b968SJiri Pirko 	[TCA_VLAN_PARMS]		= { .len = sizeof(struct tc_vlan) },
92c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_ID]		= { .type = NLA_U16 },
93c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_PROTOCOL]	= { .type = NLA_U16 },
94956af371SHadar Hen Zion 	[TCA_VLAN_PUSH_VLAN_PRIORITY]	= { .type = NLA_U8 },
95c7e2b968SJiri Pirko };
96c7e2b968SJiri Pirko 
97c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla,
98a85a970aSWANG Cong 			 struct nlattr *est, struct tc_action **a,
99c7e2b968SJiri Pirko 			 int ovr, int bind)
100c7e2b968SJiri Pirko {
101ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
102c7e2b968SJiri Pirko 	struct nlattr *tb[TCA_VLAN_MAX + 1];
103c7e2b968SJiri Pirko 	struct tc_vlan *parm;
104c7e2b968SJiri Pirko 	struct tcf_vlan *v;
105c7e2b968SJiri Pirko 	int action;
106c7e2b968SJiri Pirko 	__be16 push_vid = 0;
107c7e2b968SJiri Pirko 	__be16 push_proto = 0;
108956af371SHadar Hen Zion 	u8 push_prio = 0;
109b2313077SWANG Cong 	bool exists = false;
110b2313077SWANG Cong 	int ret = 0, err;
111c7e2b968SJiri Pirko 
112c7e2b968SJiri Pirko 	if (!nla)
113c7e2b968SJiri Pirko 		return -EINVAL;
114c7e2b968SJiri Pirko 
115c7e2b968SJiri Pirko 	err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
116c7e2b968SJiri Pirko 	if (err < 0)
117c7e2b968SJiri Pirko 		return err;
118c7e2b968SJiri Pirko 
119c7e2b968SJiri Pirko 	if (!tb[TCA_VLAN_PARMS])
120c7e2b968SJiri Pirko 		return -EINVAL;
121c7e2b968SJiri Pirko 	parm = nla_data(tb[TCA_VLAN_PARMS]);
1225026c9b1SJamal Hadi Salim 	exists = tcf_hash_check(tn, parm->index, a, bind);
1235026c9b1SJamal Hadi Salim 	if (exists && bind)
1245026c9b1SJamal Hadi Salim 		return 0;
1255026c9b1SJamal Hadi Salim 
126c7e2b968SJiri Pirko 	switch (parm->v_action) {
127c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
128c7e2b968SJiri Pirko 		break;
129c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
130*45a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
1315026c9b1SJamal Hadi Salim 		if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
1325026c9b1SJamal Hadi Salim 			if (exists)
133a85a970aSWANG Cong 				tcf_hash_release(*a, bind);
134c7e2b968SJiri Pirko 			return -EINVAL;
1355026c9b1SJamal Hadi Salim 		}
136c7e2b968SJiri Pirko 		push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
1375026c9b1SJamal Hadi Salim 		if (push_vid >= VLAN_VID_MASK) {
1385026c9b1SJamal Hadi Salim 			if (exists)
139a85a970aSWANG Cong 				tcf_hash_release(*a, bind);
140c7e2b968SJiri Pirko 			return -ERANGE;
1415026c9b1SJamal Hadi Salim 		}
142c7e2b968SJiri Pirko 
143c7e2b968SJiri Pirko 		if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
144c7e2b968SJiri Pirko 			push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
145c7e2b968SJiri Pirko 			switch (push_proto) {
146c7e2b968SJiri Pirko 			case htons(ETH_P_8021Q):
147c7e2b968SJiri Pirko 			case htons(ETH_P_8021AD):
148c7e2b968SJiri Pirko 				break;
149c7e2b968SJiri Pirko 			default:
150c7e2b968SJiri Pirko 				return -EPROTONOSUPPORT;
151c7e2b968SJiri Pirko 			}
152c7e2b968SJiri Pirko 		} else {
153c7e2b968SJiri Pirko 			push_proto = htons(ETH_P_8021Q);
154c7e2b968SJiri Pirko 		}
155956af371SHadar Hen Zion 
156956af371SHadar Hen Zion 		if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
157956af371SHadar Hen Zion 			push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
158c7e2b968SJiri Pirko 		break;
159c7e2b968SJiri Pirko 	default:
1605026c9b1SJamal Hadi Salim 		if (exists)
161a85a970aSWANG Cong 			tcf_hash_release(*a, bind);
162c7e2b968SJiri Pirko 		return -EINVAL;
163c7e2b968SJiri Pirko 	}
164c7e2b968SJiri Pirko 	action = parm->v_action;
165c7e2b968SJiri Pirko 
1665026c9b1SJamal Hadi Salim 	if (!exists) {
167ddf97ccdSWANG Cong 		ret = tcf_hash_create(tn, parm->index, est, a,
168a85a970aSWANG Cong 				      &act_vlan_ops, bind, false);
169c7e2b968SJiri Pirko 		if (ret)
170c7e2b968SJiri Pirko 			return ret;
171c7e2b968SJiri Pirko 
172c7e2b968SJiri Pirko 		ret = ACT_P_CREATED;
173c7e2b968SJiri Pirko 	} else {
174a85a970aSWANG Cong 		tcf_hash_release(*a, bind);
175c7e2b968SJiri Pirko 		if (!ovr)
176c7e2b968SJiri Pirko 			return -EEXIST;
177c7e2b968SJiri Pirko 	}
178c7e2b968SJiri Pirko 
179a85a970aSWANG Cong 	v = to_vlan(*a);
180c7e2b968SJiri Pirko 
181c7e2b968SJiri Pirko 	spin_lock_bh(&v->tcf_lock);
182c7e2b968SJiri Pirko 
183c7e2b968SJiri Pirko 	v->tcfv_action = action;
184c7e2b968SJiri Pirko 	v->tcfv_push_vid = push_vid;
185956af371SHadar Hen Zion 	v->tcfv_push_prio = push_prio;
186c7e2b968SJiri Pirko 	v->tcfv_push_proto = push_proto;
187c7e2b968SJiri Pirko 
188c7e2b968SJiri Pirko 	v->tcf_action = parm->action;
189c7e2b968SJiri Pirko 
190c7e2b968SJiri Pirko 	spin_unlock_bh(&v->tcf_lock);
191c7e2b968SJiri Pirko 
192c7e2b968SJiri Pirko 	if (ret == ACT_P_CREATED)
193a85a970aSWANG Cong 		tcf_hash_insert(tn, *a);
194c7e2b968SJiri Pirko 	return ret;
195c7e2b968SJiri Pirko }
196c7e2b968SJiri Pirko 
197c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
198c7e2b968SJiri Pirko 			 int bind, int ref)
199c7e2b968SJiri Pirko {
200c7e2b968SJiri Pirko 	unsigned char *b = skb_tail_pointer(skb);
201a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
202c7e2b968SJiri Pirko 	struct tc_vlan opt = {
203c7e2b968SJiri Pirko 		.index    = v->tcf_index,
204c7e2b968SJiri Pirko 		.refcnt   = v->tcf_refcnt - ref,
205c7e2b968SJiri Pirko 		.bindcnt  = v->tcf_bindcnt - bind,
206c7e2b968SJiri Pirko 		.action   = v->tcf_action,
207c7e2b968SJiri Pirko 		.v_action = v->tcfv_action,
208c7e2b968SJiri Pirko 	};
209c7e2b968SJiri Pirko 	struct tcf_t t;
210c7e2b968SJiri Pirko 
211c7e2b968SJiri Pirko 	if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
212c7e2b968SJiri Pirko 		goto nla_put_failure;
213c7e2b968SJiri Pirko 
214*45a497f2SShmulik Ladkani 	if ((v->tcfv_action == TCA_VLAN_ACT_PUSH ||
215*45a497f2SShmulik Ladkani 	     v->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
216c7e2b968SJiri Pirko 	    (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
2170b0f43feSJamal Hadi Salim 	     nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
218956af371SHadar Hen Zion 			  v->tcfv_push_proto) ||
219956af371SHadar Hen Zion 	     (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
220956af371SHadar Hen Zion 					      v->tcfv_push_prio))))
221c7e2b968SJiri Pirko 		goto nla_put_failure;
222c7e2b968SJiri Pirko 
22348d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &v->tcf_tm);
2249854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
225c7e2b968SJiri Pirko 		goto nla_put_failure;
226c7e2b968SJiri Pirko 	return skb->len;
227c7e2b968SJiri Pirko 
228c7e2b968SJiri Pirko nla_put_failure:
229c7e2b968SJiri Pirko 	nlmsg_trim(skb, b);
230c7e2b968SJiri Pirko 	return -1;
231c7e2b968SJiri Pirko }
232c7e2b968SJiri Pirko 
233ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
234ddf97ccdSWANG Cong 			   struct netlink_callback *cb, int type,
235a85a970aSWANG Cong 			   const struct tc_action_ops *ops)
236ddf97ccdSWANG Cong {
237ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
238ddf97ccdSWANG Cong 
239a85a970aSWANG Cong 	return tcf_generic_walker(tn, skb, cb, type, ops);
240ddf97ccdSWANG Cong }
241ddf97ccdSWANG Cong 
242a85a970aSWANG Cong static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
243ddf97ccdSWANG Cong {
244ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
245ddf97ccdSWANG Cong 
246ddf97ccdSWANG Cong 	return tcf_hash_search(tn, a, index);
247ddf97ccdSWANG Cong }
248ddf97ccdSWANG Cong 
249c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = {
250c7e2b968SJiri Pirko 	.kind		=	"vlan",
251c7e2b968SJiri Pirko 	.type		=	TCA_ACT_VLAN,
252c7e2b968SJiri Pirko 	.owner		=	THIS_MODULE,
253c7e2b968SJiri Pirko 	.act		=	tcf_vlan,
254c7e2b968SJiri Pirko 	.dump		=	tcf_vlan_dump,
255c7e2b968SJiri Pirko 	.init		=	tcf_vlan_init,
256ddf97ccdSWANG Cong 	.walk		=	tcf_vlan_walker,
257ddf97ccdSWANG Cong 	.lookup		=	tcf_vlan_search,
258a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_vlan),
259ddf97ccdSWANG Cong };
260ddf97ccdSWANG Cong 
261ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net)
262ddf97ccdSWANG Cong {
263ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
264ddf97ccdSWANG Cong 
265ddf97ccdSWANG Cong 	return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
266ddf97ccdSWANG Cong }
267ddf97ccdSWANG Cong 
268ddf97ccdSWANG Cong static void __net_exit vlan_exit_net(struct net *net)
269ddf97ccdSWANG Cong {
270ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
271ddf97ccdSWANG Cong 
272ddf97ccdSWANG Cong 	tc_action_net_exit(tn);
273ddf97ccdSWANG Cong }
274ddf97ccdSWANG Cong 
275ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = {
276ddf97ccdSWANG Cong 	.init = vlan_init_net,
277ddf97ccdSWANG Cong 	.exit = vlan_exit_net,
278ddf97ccdSWANG Cong 	.id   = &vlan_net_id,
279ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
280c7e2b968SJiri Pirko };
281c7e2b968SJiri Pirko 
282c7e2b968SJiri Pirko static int __init vlan_init_module(void)
283c7e2b968SJiri Pirko {
284ddf97ccdSWANG Cong 	return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
285c7e2b968SJiri Pirko }
286c7e2b968SJiri Pirko 
287c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void)
288c7e2b968SJiri Pirko {
289ddf97ccdSWANG Cong 	tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
290c7e2b968SJiri Pirko }
291c7e2b968SJiri Pirko 
292c7e2b968SJiri Pirko module_init(vlan_init_module);
293c7e2b968SJiri Pirko module_exit(vlan_cleanup_module);
294c7e2b968SJiri Pirko 
295c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
296c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions");
297c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2");
298