xref: /openbmc/linux/net/sched/act_vlan.c (revision c54e1d92)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c7e2b968SJiri Pirko /*
3c7e2b968SJiri Pirko  * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
4c7e2b968SJiri Pirko  */
5c7e2b968SJiri Pirko 
6c7e2b968SJiri Pirko #include <linux/module.h>
7c7e2b968SJiri Pirko #include <linux/init.h>
8c7e2b968SJiri Pirko #include <linux/kernel.h>
9c7e2b968SJiri Pirko #include <linux/skbuff.h>
10c7e2b968SJiri Pirko #include <linux/rtnetlink.h>
11c7e2b968SJiri Pirko #include <linux/if_vlan.h>
12c7e2b968SJiri Pirko #include <net/netlink.h>
13c7e2b968SJiri Pirko #include <net/pkt_sched.h>
147e0c8892SDavide Caratti #include <net/pkt_cls.h>
15c7e2b968SJiri Pirko 
16c7e2b968SJiri Pirko #include <linux/tc_act/tc_vlan.h>
17c7e2b968SJiri Pirko #include <net/tc_act/tc_vlan.h>
18c7e2b968SJiri Pirko 
19c7d03a00SAlexey Dobriyan static unsigned int vlan_net_id;
20a85a970aSWANG Cong static struct tc_action_ops act_vlan_ops;
21ddf97ccdSWANG Cong 
228aa7f22eSJamal Hadi Salim static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a,
23c7e2b968SJiri Pirko 			struct tcf_result *res)
24c7e2b968SJiri Pirko {
25a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
264c5b9d96SManish Kurup 	struct tcf_vlan_params *p;
27c7e2b968SJiri Pirko 	int action;
28c7e2b968SJiri Pirko 	int err;
2945a497f2SShmulik Ladkani 	u16 tci;
30c7e2b968SJiri Pirko 
319c4a4e48SJamal Hadi Salim 	tcf_lastuse_update(&v->tcf_tm);
325e1ad95bSVlad Buslov 	tcf_action_update_bstats(&v->common, skb);
33e0496cbbSManish Kurup 
34f39acc84SShmulik Ladkani 	/* Ensure 'data' points at mac_header prior calling vlan manipulating
35f39acc84SShmulik Ladkani 	 * functions.
36f39acc84SShmulik Ladkani 	 */
37f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
38f39acc84SShmulik Ladkani 		skb_push_rcsum(skb, skb->mac_len);
39f39acc84SShmulik Ladkani 
404c5b9d96SManish Kurup 	action = READ_ONCE(v->tcf_action);
414c5b9d96SManish Kurup 
427fd4b288SPaolo Abeni 	p = rcu_dereference_bh(v->vlan_p);
434c5b9d96SManish Kurup 
444c5b9d96SManish Kurup 	switch (p->tcfv_action) {
45c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
46c7e2b968SJiri Pirko 		err = skb_vlan_pop(skb);
47c7e2b968SJiri Pirko 		if (err)
48c7e2b968SJiri Pirko 			goto drop;
49c7e2b968SJiri Pirko 		break;
50c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
514c5b9d96SManish Kurup 		err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid |
524c5b9d96SManish Kurup 				    (p->tcfv_push_prio << VLAN_PRIO_SHIFT));
53c7e2b968SJiri Pirko 		if (err)
54c7e2b968SJiri Pirko 			goto drop;
55c7e2b968SJiri Pirko 		break;
5645a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
5745a497f2SShmulik Ladkani 		/* No-op if no vlan tag (either hw-accel or in-payload) */
5845a497f2SShmulik Ladkani 		if (!skb_vlan_tagged(skb))
597fd4b288SPaolo Abeni 			goto out;
6045a497f2SShmulik Ladkani 		/* extract existing tag (and guarantee no hw-accel tag) */
6145a497f2SShmulik Ladkani 		if (skb_vlan_tag_present(skb)) {
6245a497f2SShmulik Ladkani 			tci = skb_vlan_tag_get(skb);
63b1817524SMichał Mirosław 			__vlan_hwaccel_clear_tag(skb);
6445a497f2SShmulik Ladkani 		} else {
6545a497f2SShmulik Ladkani 			/* in-payload vlan tag, pop it */
6645a497f2SShmulik Ladkani 			err = __skb_vlan_pop(skb, &tci);
6745a497f2SShmulik Ladkani 			if (err)
6845a497f2SShmulik Ladkani 				goto drop;
6945a497f2SShmulik Ladkani 		}
7045a497f2SShmulik Ladkani 		/* replace the vid */
714c5b9d96SManish Kurup 		tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid;
7245a497f2SShmulik Ladkani 		/* replace prio bits, if tcfv_push_prio specified */
739c5eee0aSBoris Sukholitko 		if (p->tcfv_push_prio_exists) {
7445a497f2SShmulik Ladkani 			tci &= ~VLAN_PRIO_MASK;
754c5b9d96SManish Kurup 			tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
7645a497f2SShmulik Ladkani 		}
7745a497f2SShmulik Ladkani 		/* put updated tci as hwaccel tag */
784c5b9d96SManish Kurup 		__vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
7945a497f2SShmulik Ladkani 		break;
8019fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_POP_ETH:
8119fbcb36SGuillaume Nault 		err = skb_eth_pop(skb);
8219fbcb36SGuillaume Nault 		if (err)
8319fbcb36SGuillaume Nault 			goto drop;
8419fbcb36SGuillaume Nault 		break;
8519fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_PUSH_ETH:
8619fbcb36SGuillaume Nault 		err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src);
8719fbcb36SGuillaume Nault 		if (err)
8819fbcb36SGuillaume Nault 			goto drop;
8919fbcb36SGuillaume Nault 		break;
90c7e2b968SJiri Pirko 	default:
91c7e2b968SJiri Pirko 		BUG();
92c7e2b968SJiri Pirko 	}
93c7e2b968SJiri Pirko 
947fd4b288SPaolo Abeni out:
95f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
96f39acc84SShmulik Ladkani 		skb_pull_rcsum(skb, skb->mac_len);
97f39acc84SShmulik Ladkani 
98c7e2b968SJiri Pirko 	return action;
997fd4b288SPaolo Abeni 
1007fd4b288SPaolo Abeni drop:
10126b537a8SVlad Buslov 	tcf_action_inc_drop_qstats(&v->common);
1027fd4b288SPaolo Abeni 	return TC_ACT_SHOT;
103c7e2b968SJiri Pirko }
104c7e2b968SJiri Pirko 
105c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
10619fbcb36SGuillaume Nault 	[TCA_VLAN_UNSPEC]		= { .strict_start_type = TCA_VLAN_PUSH_ETH_DST },
107c7e2b968SJiri Pirko 	[TCA_VLAN_PARMS]		= { .len = sizeof(struct tc_vlan) },
108c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_ID]		= { .type = NLA_U16 },
109c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_PROTOCOL]	= { .type = NLA_U16 },
110956af371SHadar Hen Zion 	[TCA_VLAN_PUSH_VLAN_PRIORITY]	= { .type = NLA_U8 },
11119fbcb36SGuillaume Nault 	[TCA_VLAN_PUSH_ETH_DST]		= NLA_POLICY_ETH_ADDR,
11219fbcb36SGuillaume Nault 	[TCA_VLAN_PUSH_ETH_SRC]		= NLA_POLICY_ETH_ADDR,
113c7e2b968SJiri Pirko };
114c7e2b968SJiri Pirko 
115c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla,
116a85a970aSWANG Cong 			 struct nlattr *est, struct tc_action **a,
117abbb0d33SVlad Buslov 			 struct tcf_proto *tp, u32 flags,
118abbb0d33SVlad Buslov 			 struct netlink_ext_ack *extack)
119c7e2b968SJiri Pirko {
120ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
121695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
122c7e2b968SJiri Pirko 	struct nlattr *tb[TCA_VLAN_MAX + 1];
1237e0c8892SDavide Caratti 	struct tcf_chain *goto_ch = NULL;
1249c5eee0aSBoris Sukholitko 	bool push_prio_exists = false;
125764e9a24SVlad Buslov 	struct tcf_vlan_params *p;
126c7e2b968SJiri Pirko 	struct tc_vlan *parm;
127c7e2b968SJiri Pirko 	struct tcf_vlan *v;
128c7e2b968SJiri Pirko 	int action;
12994cb5492SDavide Caratti 	u16 push_vid = 0;
130c7e2b968SJiri Pirko 	__be16 push_proto = 0;
131956af371SHadar Hen Zion 	u8 push_prio = 0;
132b2313077SWANG Cong 	bool exists = false;
133b2313077SWANG Cong 	int ret = 0, err;
1347be8ef2cSDmytro Linkin 	u32 index;
135c7e2b968SJiri Pirko 
136c7e2b968SJiri Pirko 	if (!nla)
137c7e2b968SJiri Pirko 		return -EINVAL;
138c7e2b968SJiri Pirko 
1398cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy,
1408cb08174SJohannes Berg 					  NULL);
141c7e2b968SJiri Pirko 	if (err < 0)
142c7e2b968SJiri Pirko 		return err;
143c7e2b968SJiri Pirko 
144c7e2b968SJiri Pirko 	if (!tb[TCA_VLAN_PARMS])
145c7e2b968SJiri Pirko 		return -EINVAL;
146c7e2b968SJiri Pirko 	parm = nla_data(tb[TCA_VLAN_PARMS]);
1477be8ef2cSDmytro Linkin 	index = parm->index;
1487be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
1490190c1d4SVlad Buslov 	if (err < 0)
1500190c1d4SVlad Buslov 		return err;
1510190c1d4SVlad Buslov 	exists = err;
1525026c9b1SJamal Hadi Salim 	if (exists && bind)
1535026c9b1SJamal Hadi Salim 		return 0;
1545026c9b1SJamal Hadi Salim 
155c7e2b968SJiri Pirko 	switch (parm->v_action) {
156c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
157c7e2b968SJiri Pirko 		break;
158c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
15945a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
1605026c9b1SJamal Hadi Salim 		if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
1615026c9b1SJamal Hadi Salim 			if (exists)
16265a206c0SChris Mi 				tcf_idr_release(*a, bind);
1630190c1d4SVlad Buslov 			else
1647be8ef2cSDmytro Linkin 				tcf_idr_cleanup(tn, index);
165c7e2b968SJiri Pirko 			return -EINVAL;
1665026c9b1SJamal Hadi Salim 		}
167c7e2b968SJiri Pirko 		push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
1685026c9b1SJamal Hadi Salim 		if (push_vid >= VLAN_VID_MASK) {
1695026c9b1SJamal Hadi Salim 			if (exists)
17065a206c0SChris Mi 				tcf_idr_release(*a, bind);
1710190c1d4SVlad Buslov 			else
1727be8ef2cSDmytro Linkin 				tcf_idr_cleanup(tn, index);
173c7e2b968SJiri Pirko 			return -ERANGE;
1745026c9b1SJamal Hadi Salim 		}
175c7e2b968SJiri Pirko 
176c7e2b968SJiri Pirko 		if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
177c7e2b968SJiri Pirko 			push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
178c7e2b968SJiri Pirko 			switch (push_proto) {
179c7e2b968SJiri Pirko 			case htons(ETH_P_8021Q):
180c7e2b968SJiri Pirko 			case htons(ETH_P_8021AD):
181c7e2b968SJiri Pirko 				break;
182c7e2b968SJiri Pirko 			default:
1835a4931aeSDavide Caratti 				if (exists)
1845a4931aeSDavide Caratti 					tcf_idr_release(*a, bind);
1850190c1d4SVlad Buslov 				else
1867be8ef2cSDmytro Linkin 					tcf_idr_cleanup(tn, index);
187c7e2b968SJiri Pirko 				return -EPROTONOSUPPORT;
188c7e2b968SJiri Pirko 			}
189c7e2b968SJiri Pirko 		} else {
190c7e2b968SJiri Pirko 			push_proto = htons(ETH_P_8021Q);
191c7e2b968SJiri Pirko 		}
192956af371SHadar Hen Zion 
1939c5eee0aSBoris Sukholitko 		push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY];
1949c5eee0aSBoris Sukholitko 		if (push_prio_exists)
195956af371SHadar Hen Zion 			push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
196c7e2b968SJiri Pirko 		break;
19719fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_POP_ETH:
19819fbcb36SGuillaume Nault 		break;
19919fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_PUSH_ETH:
20019fbcb36SGuillaume Nault 		if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) {
20119fbcb36SGuillaume Nault 			if (exists)
20219fbcb36SGuillaume Nault 				tcf_idr_release(*a, bind);
20319fbcb36SGuillaume Nault 			else
20419fbcb36SGuillaume Nault 				tcf_idr_cleanup(tn, index);
20519fbcb36SGuillaume Nault 			return -EINVAL;
20619fbcb36SGuillaume Nault 		}
20719fbcb36SGuillaume Nault 		break;
208c7e2b968SJiri Pirko 	default:
2095026c9b1SJamal Hadi Salim 		if (exists)
21065a206c0SChris Mi 			tcf_idr_release(*a, bind);
2110190c1d4SVlad Buslov 		else
2127be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
213c7e2b968SJiri Pirko 		return -EINVAL;
214c7e2b968SJiri Pirko 	}
215c7e2b968SJiri Pirko 	action = parm->v_action;
216c7e2b968SJiri Pirko 
2175026c9b1SJamal Hadi Salim 	if (!exists) {
218e3822678SVlad Buslov 		ret = tcf_idr_create_from_flags(tn, index, est, a,
219e3822678SVlad Buslov 						&act_vlan_ops, bind, flags);
2200190c1d4SVlad Buslov 		if (ret) {
2217be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
222c7e2b968SJiri Pirko 			return ret;
2230190c1d4SVlad Buslov 		}
224c7e2b968SJiri Pirko 
225c7e2b968SJiri Pirko 		ret = ACT_P_CREATED;
226695176bfSCong Wang 	} else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
22765a206c0SChris Mi 		tcf_idr_release(*a, bind);
228c7e2b968SJiri Pirko 		return -EEXIST;
229c7e2b968SJiri Pirko 	}
230c7e2b968SJiri Pirko 
2317e0c8892SDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
2327e0c8892SDavide Caratti 	if (err < 0)
2337e0c8892SDavide Caratti 		goto release_idr;
2347e0c8892SDavide Caratti 
235a85a970aSWANG Cong 	v = to_vlan(*a);
236c7e2b968SJiri Pirko 
2374c5b9d96SManish Kurup 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2384c5b9d96SManish Kurup 	if (!p) {
2397e0c8892SDavide Caratti 		err = -ENOMEM;
2407e0c8892SDavide Caratti 		goto put_chain;
2414c5b9d96SManish Kurup 	}
242c7e2b968SJiri Pirko 
2434c5b9d96SManish Kurup 	p->tcfv_action = action;
2444c5b9d96SManish Kurup 	p->tcfv_push_vid = push_vid;
2454c5b9d96SManish Kurup 	p->tcfv_push_prio = push_prio;
2469c5eee0aSBoris Sukholitko 	p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH;
2474c5b9d96SManish Kurup 	p->tcfv_push_proto = push_proto;
2484c5b9d96SManish Kurup 
24919fbcb36SGuillaume Nault 	if (action == TCA_VLAN_ACT_PUSH_ETH) {
25019fbcb36SGuillaume Nault 		nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST],
25119fbcb36SGuillaume Nault 			   ETH_ALEN);
25219fbcb36SGuillaume Nault 		nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC],
25319fbcb36SGuillaume Nault 			   ETH_ALEN);
25419fbcb36SGuillaume Nault 	}
25519fbcb36SGuillaume Nault 
256653cd284SVlad Buslov 	spin_lock_bh(&v->tcf_lock);
2577e0c8892SDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
258445d3749SPaul E. McKenney 	p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
259653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
2604c5b9d96SManish Kurup 
2617e0c8892SDavide Caratti 	if (goto_ch)
2627e0c8892SDavide Caratti 		tcf_chain_put_by_act(goto_ch);
263764e9a24SVlad Buslov 	if (p)
264764e9a24SVlad Buslov 		kfree_rcu(p, rcu);
265c7e2b968SJiri Pirko 
266c7e2b968SJiri Pirko 	return ret;
2677e0c8892SDavide Caratti put_chain:
2687e0c8892SDavide Caratti 	if (goto_ch)
2697e0c8892SDavide Caratti 		tcf_chain_put_by_act(goto_ch);
2707e0c8892SDavide Caratti release_idr:
2717e0c8892SDavide Caratti 	tcf_idr_release(*a, bind);
2727e0c8892SDavide Caratti 	return err;
273c7e2b968SJiri Pirko }
274c7e2b968SJiri Pirko 
2759a63b255SCong Wang static void tcf_vlan_cleanup(struct tc_action *a)
2764c5b9d96SManish Kurup {
2774c5b9d96SManish Kurup 	struct tcf_vlan *v = to_vlan(a);
2784c5b9d96SManish Kurup 	struct tcf_vlan_params *p;
2794c5b9d96SManish Kurup 
2804c5b9d96SManish Kurup 	p = rcu_dereference_protected(v->vlan_p, 1);
2811edf8abeSDavide Caratti 	if (p)
2824c5b9d96SManish Kurup 		kfree_rcu(p, rcu);
2834c5b9d96SManish Kurup }
2844c5b9d96SManish Kurup 
285c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
286c7e2b968SJiri Pirko 			 int bind, int ref)
287c7e2b968SJiri Pirko {
288c7e2b968SJiri Pirko 	unsigned char *b = skb_tail_pointer(skb);
289a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
290764e9a24SVlad Buslov 	struct tcf_vlan_params *p;
291c7e2b968SJiri Pirko 	struct tc_vlan opt = {
292c7e2b968SJiri Pirko 		.index    = v->tcf_index,
293036bb443SVlad Buslov 		.refcnt   = refcount_read(&v->tcf_refcnt) - ref,
294036bb443SVlad Buslov 		.bindcnt  = atomic_read(&v->tcf_bindcnt) - bind,
295c7e2b968SJiri Pirko 	};
296c7e2b968SJiri Pirko 	struct tcf_t t;
297c7e2b968SJiri Pirko 
298653cd284SVlad Buslov 	spin_lock_bh(&v->tcf_lock);
299764e9a24SVlad Buslov 	opt.action = v->tcf_action;
300764e9a24SVlad Buslov 	p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
301764e9a24SVlad Buslov 	opt.v_action = p->tcfv_action;
302c7e2b968SJiri Pirko 	if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
303c7e2b968SJiri Pirko 		goto nla_put_failure;
304c7e2b968SJiri Pirko 
3054c5b9d96SManish Kurup 	if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
3064c5b9d96SManish Kurup 	     p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
3074c5b9d96SManish Kurup 	    (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
3080b0f43feSJamal Hadi Salim 	     nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
3094c5b9d96SManish Kurup 			  p->tcfv_push_proto) ||
3108323b20fSBoris Sukholitko 	     (p->tcfv_push_prio_exists &&
3118323b20fSBoris Sukholitko 	      nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
312c7e2b968SJiri Pirko 		goto nla_put_failure;
313c7e2b968SJiri Pirko 
31419fbcb36SGuillaume Nault 	if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) {
31519fbcb36SGuillaume Nault 		if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN,
31619fbcb36SGuillaume Nault 			    p->tcfv_push_dst))
31719fbcb36SGuillaume Nault 			goto nla_put_failure;
31819fbcb36SGuillaume Nault 		if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN,
31919fbcb36SGuillaume Nault 			    p->tcfv_push_src))
32019fbcb36SGuillaume Nault 			goto nla_put_failure;
32119fbcb36SGuillaume Nault 	}
32219fbcb36SGuillaume Nault 
32348d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &v->tcf_tm);
3249854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
325c7e2b968SJiri Pirko 		goto nla_put_failure;
326653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
327764e9a24SVlad Buslov 
328c7e2b968SJiri Pirko 	return skb->len;
329c7e2b968SJiri Pirko 
330c7e2b968SJiri Pirko nla_put_failure:
331653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
332c7e2b968SJiri Pirko 	nlmsg_trim(skb, b);
333c7e2b968SJiri Pirko 	return -1;
334c7e2b968SJiri Pirko }
335c7e2b968SJiri Pirko 
336ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
337ddf97ccdSWANG Cong 			   struct netlink_callback *cb, int type,
33841780105SAlexander Aring 			   const struct tc_action_ops *ops,
33941780105SAlexander Aring 			   struct netlink_ext_ack *extack)
340ddf97ccdSWANG Cong {
341ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
342ddf97ccdSWANG Cong 
343b3620145SAlexander Aring 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
344ddf97ccdSWANG Cong }
345ddf97ccdSWANG Cong 
3464b61d3e8SPo Liu static void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets,
3474b61d3e8SPo Liu 				  u64 drops, u64 lastuse, bool hw)
348fa730a3bSJiri Pirko {
349fa730a3bSJiri Pirko 	struct tcf_vlan *v = to_vlan(a);
350fa730a3bSJiri Pirko 	struct tcf_t *tm = &v->tcf_tm;
351fa730a3bSJiri Pirko 
3524b61d3e8SPo Liu 	tcf_action_update_stats(a, bytes, packets, drops, hw);
353fa730a3bSJiri Pirko 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
354fa730a3bSJiri Pirko }
355fa730a3bSJiri Pirko 
356f061b48cSCong Wang static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
357ddf97ccdSWANG Cong {
358ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
359ddf97ccdSWANG Cong 
36065a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
361ddf97ccdSWANG Cong }
362ddf97ccdSWANG Cong 
363b35475c5SRoman Mashak static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
364b35475c5SRoman Mashak {
365b35475c5SRoman Mashak 	return nla_total_size(sizeof(struct tc_vlan))
366b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
367b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
368b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
369b35475c5SRoman Mashak }
370b35475c5SRoman Mashak 
371*c54e1d92SBaowen Zheng static int tcf_vlan_offload_act_setup(struct tc_action *act, void *entry_data,
372*c54e1d92SBaowen Zheng 				      u32 *index_inc, bool bind)
373*c54e1d92SBaowen Zheng {
374*c54e1d92SBaowen Zheng 	if (bind) {
375*c54e1d92SBaowen Zheng 		struct flow_action_entry *entry = entry_data;
376*c54e1d92SBaowen Zheng 
377*c54e1d92SBaowen Zheng 		switch (tcf_vlan_action(act)) {
378*c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_PUSH:
379*c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_PUSH;
380*c54e1d92SBaowen Zheng 			entry->vlan.vid = tcf_vlan_push_vid(act);
381*c54e1d92SBaowen Zheng 			entry->vlan.proto = tcf_vlan_push_proto(act);
382*c54e1d92SBaowen Zheng 			entry->vlan.prio = tcf_vlan_push_prio(act);
383*c54e1d92SBaowen Zheng 			break;
384*c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_POP:
385*c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_POP;
386*c54e1d92SBaowen Zheng 			break;
387*c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_MODIFY:
388*c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_MANGLE;
389*c54e1d92SBaowen Zheng 			entry->vlan.vid = tcf_vlan_push_vid(act);
390*c54e1d92SBaowen Zheng 			entry->vlan.proto = tcf_vlan_push_proto(act);
391*c54e1d92SBaowen Zheng 			entry->vlan.prio = tcf_vlan_push_prio(act);
392*c54e1d92SBaowen Zheng 			break;
393*c54e1d92SBaowen Zheng 		default:
394*c54e1d92SBaowen Zheng 			return -EOPNOTSUPP;
395*c54e1d92SBaowen Zheng 		}
396*c54e1d92SBaowen Zheng 		*index_inc = 1;
397*c54e1d92SBaowen Zheng 	} else {
398*c54e1d92SBaowen Zheng 		return -EOPNOTSUPP;
399*c54e1d92SBaowen Zheng 	}
400*c54e1d92SBaowen Zheng 
401*c54e1d92SBaowen Zheng 	return 0;
402*c54e1d92SBaowen Zheng }
403*c54e1d92SBaowen Zheng 
404c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = {
405c7e2b968SJiri Pirko 	.kind		=	"vlan",
406eddd2cf1SEli Cohen 	.id		=	TCA_ID_VLAN,
407c7e2b968SJiri Pirko 	.owner		=	THIS_MODULE,
4088aa7f22eSJamal Hadi Salim 	.act		=	tcf_vlan_act,
409c7e2b968SJiri Pirko 	.dump		=	tcf_vlan_dump,
410c7e2b968SJiri Pirko 	.init		=	tcf_vlan_init,
4114c5b9d96SManish Kurup 	.cleanup	=	tcf_vlan_cleanup,
412ddf97ccdSWANG Cong 	.walk		=	tcf_vlan_walker,
413fa730a3bSJiri Pirko 	.stats_update	=	tcf_vlan_stats_update,
414b35475c5SRoman Mashak 	.get_fill_size	=	tcf_vlan_get_fill_size,
415ddf97ccdSWANG Cong 	.lookup		=	tcf_vlan_search,
416*c54e1d92SBaowen Zheng 	.offload_act_setup =	tcf_vlan_offload_act_setup,
417a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_vlan),
418ddf97ccdSWANG Cong };
419ddf97ccdSWANG Cong 
420ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net)
421ddf97ccdSWANG Cong {
422ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, vlan_net_id);
423ddf97ccdSWANG Cong 
424981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_vlan_ops);
425ddf97ccdSWANG Cong }
426ddf97ccdSWANG Cong 
427039af9c6SCong Wang static void __net_exit vlan_exit_net(struct list_head *net_list)
428ddf97ccdSWANG Cong {
429039af9c6SCong Wang 	tc_action_net_exit(net_list, vlan_net_id);
430ddf97ccdSWANG Cong }
431ddf97ccdSWANG Cong 
432ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = {
433ddf97ccdSWANG Cong 	.init = vlan_init_net,
434039af9c6SCong Wang 	.exit_batch = vlan_exit_net,
435ddf97ccdSWANG Cong 	.id   = &vlan_net_id,
436ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
437c7e2b968SJiri Pirko };
438c7e2b968SJiri Pirko 
439c7e2b968SJiri Pirko static int __init vlan_init_module(void)
440c7e2b968SJiri Pirko {
441ddf97ccdSWANG Cong 	return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
442c7e2b968SJiri Pirko }
443c7e2b968SJiri Pirko 
444c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void)
445c7e2b968SJiri Pirko {
446ddf97ccdSWANG Cong 	tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
447c7e2b968SJiri Pirko }
448c7e2b968SJiri Pirko 
449c7e2b968SJiri Pirko module_init(vlan_init_module);
450c7e2b968SJiri Pirko module_exit(vlan_cleanup_module);
451c7e2b968SJiri Pirko 
452c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
453c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions");
454c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2");
455