xref: /openbmc/linux/net/sched/act_vlan.c (revision acd0a7ab)
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 
19a85a970aSWANG Cong static struct tc_action_ops act_vlan_ops;
20ddf97ccdSWANG Cong 
218aa7f22eSJamal Hadi Salim static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a,
22c7e2b968SJiri Pirko 			struct tcf_result *res)
23c7e2b968SJiri Pirko {
24a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
254c5b9d96SManish Kurup 	struct tcf_vlan_params *p;
26c7e2b968SJiri Pirko 	int action;
27c7e2b968SJiri Pirko 	int err;
2845a497f2SShmulik Ladkani 	u16 tci;
29c7e2b968SJiri Pirko 
309c4a4e48SJamal Hadi Salim 	tcf_lastuse_update(&v->tcf_tm);
315e1ad95bSVlad Buslov 	tcf_action_update_bstats(&v->common, skb);
32e0496cbbSManish Kurup 
33f39acc84SShmulik Ladkani 	/* Ensure 'data' points at mac_header prior calling vlan manipulating
34f39acc84SShmulik Ladkani 	 * functions.
35f39acc84SShmulik Ladkani 	 */
36f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
37f39acc84SShmulik Ladkani 		skb_push_rcsum(skb, skb->mac_len);
38f39acc84SShmulik Ladkani 
394c5b9d96SManish Kurup 	action = READ_ONCE(v->tcf_action);
404c5b9d96SManish Kurup 
417fd4b288SPaolo Abeni 	p = rcu_dereference_bh(v->vlan_p);
424c5b9d96SManish Kurup 
434c5b9d96SManish Kurup 	switch (p->tcfv_action) {
44c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
45c7e2b968SJiri Pirko 		err = skb_vlan_pop(skb);
46c7e2b968SJiri Pirko 		if (err)
47c7e2b968SJiri Pirko 			goto drop;
48c7e2b968SJiri Pirko 		break;
49c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
504c5b9d96SManish Kurup 		err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid |
514c5b9d96SManish Kurup 				    (p->tcfv_push_prio << VLAN_PRIO_SHIFT));
52c7e2b968SJiri Pirko 		if (err)
53c7e2b968SJiri Pirko 			goto drop;
54c7e2b968SJiri Pirko 		break;
5545a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
5645a497f2SShmulik Ladkani 		/* No-op if no vlan tag (either hw-accel or in-payload) */
5745a497f2SShmulik Ladkani 		if (!skb_vlan_tagged(skb))
587fd4b288SPaolo Abeni 			goto out;
5945a497f2SShmulik Ladkani 		/* extract existing tag (and guarantee no hw-accel tag) */
6045a497f2SShmulik Ladkani 		if (skb_vlan_tag_present(skb)) {
6145a497f2SShmulik Ladkani 			tci = skb_vlan_tag_get(skb);
62b1817524SMichał Mirosław 			__vlan_hwaccel_clear_tag(skb);
6345a497f2SShmulik Ladkani 		} else {
6445a497f2SShmulik Ladkani 			/* in-payload vlan tag, pop it */
6545a497f2SShmulik Ladkani 			err = __skb_vlan_pop(skb, &tci);
6645a497f2SShmulik Ladkani 			if (err)
6745a497f2SShmulik Ladkani 				goto drop;
6845a497f2SShmulik Ladkani 		}
6945a497f2SShmulik Ladkani 		/* replace the vid */
704c5b9d96SManish Kurup 		tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid;
7145a497f2SShmulik Ladkani 		/* replace prio bits, if tcfv_push_prio specified */
729c5eee0aSBoris Sukholitko 		if (p->tcfv_push_prio_exists) {
7345a497f2SShmulik Ladkani 			tci &= ~VLAN_PRIO_MASK;
744c5b9d96SManish Kurup 			tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
7545a497f2SShmulik Ladkani 		}
7645a497f2SShmulik Ladkani 		/* put updated tci as hwaccel tag */
774c5b9d96SManish Kurup 		__vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
7845a497f2SShmulik Ladkani 		break;
7919fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_POP_ETH:
8019fbcb36SGuillaume Nault 		err = skb_eth_pop(skb);
8119fbcb36SGuillaume Nault 		if (err)
8219fbcb36SGuillaume Nault 			goto drop;
8319fbcb36SGuillaume Nault 		break;
8419fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_PUSH_ETH:
8519fbcb36SGuillaume Nault 		err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src);
8619fbcb36SGuillaume Nault 		if (err)
8719fbcb36SGuillaume Nault 			goto drop;
8819fbcb36SGuillaume Nault 		break;
89c7e2b968SJiri Pirko 	default:
90c7e2b968SJiri Pirko 		BUG();
91c7e2b968SJiri Pirko 	}
92c7e2b968SJiri Pirko 
937fd4b288SPaolo Abeni out:
94f39acc84SShmulik Ladkani 	if (skb_at_tc_ingress(skb))
95f39acc84SShmulik Ladkani 		skb_pull_rcsum(skb, skb->mac_len);
96f39acc84SShmulik Ladkani 
97c7e2b968SJiri Pirko 	return action;
987fd4b288SPaolo Abeni 
997fd4b288SPaolo Abeni drop:
10026b537a8SVlad Buslov 	tcf_action_inc_drop_qstats(&v->common);
1017fd4b288SPaolo Abeni 	return TC_ACT_SHOT;
102c7e2b968SJiri Pirko }
103c7e2b968SJiri Pirko 
104c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
10519fbcb36SGuillaume Nault 	[TCA_VLAN_UNSPEC]		= { .strict_start_type = TCA_VLAN_PUSH_ETH_DST },
106c7e2b968SJiri Pirko 	[TCA_VLAN_PARMS]		= { .len = sizeof(struct tc_vlan) },
107c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_ID]		= { .type = NLA_U16 },
108c7e2b968SJiri Pirko 	[TCA_VLAN_PUSH_VLAN_PROTOCOL]	= { .type = NLA_U16 },
109956af371SHadar Hen Zion 	[TCA_VLAN_PUSH_VLAN_PRIORITY]	= { .type = NLA_U8 },
11019fbcb36SGuillaume Nault 	[TCA_VLAN_PUSH_ETH_DST]		= NLA_POLICY_ETH_ADDR,
11119fbcb36SGuillaume Nault 	[TCA_VLAN_PUSH_ETH_SRC]		= NLA_POLICY_ETH_ADDR,
112c7e2b968SJiri Pirko };
113c7e2b968SJiri Pirko 
114c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla,
115a85a970aSWANG Cong 			 struct nlattr *est, struct tc_action **a,
116abbb0d33SVlad Buslov 			 struct tcf_proto *tp, u32 flags,
117abbb0d33SVlad Buslov 			 struct netlink_ext_ack *extack)
118c7e2b968SJiri Pirko {
119*acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
120695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
121c7e2b968SJiri Pirko 	struct nlattr *tb[TCA_VLAN_MAX + 1];
1227e0c8892SDavide Caratti 	struct tcf_chain *goto_ch = NULL;
1239c5eee0aSBoris Sukholitko 	bool push_prio_exists = false;
124764e9a24SVlad Buslov 	struct tcf_vlan_params *p;
125c7e2b968SJiri Pirko 	struct tc_vlan *parm;
126c7e2b968SJiri Pirko 	struct tcf_vlan *v;
127c7e2b968SJiri Pirko 	int action;
12894cb5492SDavide Caratti 	u16 push_vid = 0;
129c7e2b968SJiri Pirko 	__be16 push_proto = 0;
130956af371SHadar Hen Zion 	u8 push_prio = 0;
131b2313077SWANG Cong 	bool exists = false;
132b2313077SWANG Cong 	int ret = 0, err;
1337be8ef2cSDmytro Linkin 	u32 index;
134c7e2b968SJiri Pirko 
135c7e2b968SJiri Pirko 	if (!nla)
136c7e2b968SJiri Pirko 		return -EINVAL;
137c7e2b968SJiri Pirko 
1388cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy,
1398cb08174SJohannes Berg 					  NULL);
140c7e2b968SJiri Pirko 	if (err < 0)
141c7e2b968SJiri Pirko 		return err;
142c7e2b968SJiri Pirko 
143c7e2b968SJiri Pirko 	if (!tb[TCA_VLAN_PARMS])
144c7e2b968SJiri Pirko 		return -EINVAL;
145c7e2b968SJiri Pirko 	parm = nla_data(tb[TCA_VLAN_PARMS]);
1467be8ef2cSDmytro Linkin 	index = parm->index;
1477be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
1480190c1d4SVlad Buslov 	if (err < 0)
1490190c1d4SVlad Buslov 		return err;
1500190c1d4SVlad Buslov 	exists = err;
1515026c9b1SJamal Hadi Salim 	if (exists && bind)
1525026c9b1SJamal Hadi Salim 		return 0;
1535026c9b1SJamal Hadi Salim 
154c7e2b968SJiri Pirko 	switch (parm->v_action) {
155c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_POP:
156c7e2b968SJiri Pirko 		break;
157c7e2b968SJiri Pirko 	case TCA_VLAN_ACT_PUSH:
15845a497f2SShmulik Ladkani 	case TCA_VLAN_ACT_MODIFY:
1595026c9b1SJamal Hadi Salim 		if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
1605026c9b1SJamal Hadi Salim 			if (exists)
16165a206c0SChris Mi 				tcf_idr_release(*a, bind);
1620190c1d4SVlad Buslov 			else
1637be8ef2cSDmytro Linkin 				tcf_idr_cleanup(tn, index);
164c7e2b968SJiri Pirko 			return -EINVAL;
1655026c9b1SJamal Hadi Salim 		}
166c7e2b968SJiri Pirko 		push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
1675026c9b1SJamal Hadi Salim 		if (push_vid >= VLAN_VID_MASK) {
1685026c9b1SJamal Hadi Salim 			if (exists)
16965a206c0SChris Mi 				tcf_idr_release(*a, bind);
1700190c1d4SVlad Buslov 			else
1717be8ef2cSDmytro Linkin 				tcf_idr_cleanup(tn, index);
172c7e2b968SJiri Pirko 			return -ERANGE;
1735026c9b1SJamal Hadi Salim 		}
174c7e2b968SJiri Pirko 
175c7e2b968SJiri Pirko 		if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
176c7e2b968SJiri Pirko 			push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
177c7e2b968SJiri Pirko 			switch (push_proto) {
178c7e2b968SJiri Pirko 			case htons(ETH_P_8021Q):
179c7e2b968SJiri Pirko 			case htons(ETH_P_8021AD):
180c7e2b968SJiri Pirko 				break;
181c7e2b968SJiri Pirko 			default:
1825a4931aeSDavide Caratti 				if (exists)
1835a4931aeSDavide Caratti 					tcf_idr_release(*a, bind);
1840190c1d4SVlad Buslov 				else
1857be8ef2cSDmytro Linkin 					tcf_idr_cleanup(tn, index);
186c7e2b968SJiri Pirko 				return -EPROTONOSUPPORT;
187c7e2b968SJiri Pirko 			}
188c7e2b968SJiri Pirko 		} else {
189c7e2b968SJiri Pirko 			push_proto = htons(ETH_P_8021Q);
190c7e2b968SJiri Pirko 		}
191956af371SHadar Hen Zion 
1929c5eee0aSBoris Sukholitko 		push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY];
1939c5eee0aSBoris Sukholitko 		if (push_prio_exists)
194956af371SHadar Hen Zion 			push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
195c7e2b968SJiri Pirko 		break;
19619fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_POP_ETH:
19719fbcb36SGuillaume Nault 		break;
19819fbcb36SGuillaume Nault 	case TCA_VLAN_ACT_PUSH_ETH:
19919fbcb36SGuillaume Nault 		if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) {
20019fbcb36SGuillaume Nault 			if (exists)
20119fbcb36SGuillaume Nault 				tcf_idr_release(*a, bind);
20219fbcb36SGuillaume Nault 			else
20319fbcb36SGuillaume Nault 				tcf_idr_cleanup(tn, index);
20419fbcb36SGuillaume Nault 			return -EINVAL;
20519fbcb36SGuillaume Nault 		}
20619fbcb36SGuillaume Nault 		break;
207c7e2b968SJiri Pirko 	default:
2085026c9b1SJamal Hadi Salim 		if (exists)
20965a206c0SChris Mi 			tcf_idr_release(*a, bind);
2100190c1d4SVlad Buslov 		else
2117be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
212c7e2b968SJiri Pirko 		return -EINVAL;
213c7e2b968SJiri Pirko 	}
214c7e2b968SJiri Pirko 	action = parm->v_action;
215c7e2b968SJiri Pirko 
2165026c9b1SJamal Hadi Salim 	if (!exists) {
217e3822678SVlad Buslov 		ret = tcf_idr_create_from_flags(tn, index, est, a,
218e3822678SVlad Buslov 						&act_vlan_ops, bind, flags);
2190190c1d4SVlad Buslov 		if (ret) {
2207be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
221c7e2b968SJiri Pirko 			return ret;
2220190c1d4SVlad Buslov 		}
223c7e2b968SJiri Pirko 
224c7e2b968SJiri Pirko 		ret = ACT_P_CREATED;
225695176bfSCong Wang 	} else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
22665a206c0SChris Mi 		tcf_idr_release(*a, bind);
227c7e2b968SJiri Pirko 		return -EEXIST;
228c7e2b968SJiri Pirko 	}
229c7e2b968SJiri Pirko 
2307e0c8892SDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
2317e0c8892SDavide Caratti 	if (err < 0)
2327e0c8892SDavide Caratti 		goto release_idr;
2337e0c8892SDavide Caratti 
234a85a970aSWANG Cong 	v = to_vlan(*a);
235c7e2b968SJiri Pirko 
2364c5b9d96SManish Kurup 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2374c5b9d96SManish Kurup 	if (!p) {
2387e0c8892SDavide Caratti 		err = -ENOMEM;
2397e0c8892SDavide Caratti 		goto put_chain;
2404c5b9d96SManish Kurup 	}
241c7e2b968SJiri Pirko 
2424c5b9d96SManish Kurup 	p->tcfv_action = action;
2434c5b9d96SManish Kurup 	p->tcfv_push_vid = push_vid;
2444c5b9d96SManish Kurup 	p->tcfv_push_prio = push_prio;
2459c5eee0aSBoris Sukholitko 	p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH;
2464c5b9d96SManish Kurup 	p->tcfv_push_proto = push_proto;
2474c5b9d96SManish Kurup 
24819fbcb36SGuillaume Nault 	if (action == TCA_VLAN_ACT_PUSH_ETH) {
24919fbcb36SGuillaume Nault 		nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST],
25019fbcb36SGuillaume Nault 			   ETH_ALEN);
25119fbcb36SGuillaume Nault 		nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC],
25219fbcb36SGuillaume Nault 			   ETH_ALEN);
25319fbcb36SGuillaume Nault 	}
25419fbcb36SGuillaume Nault 
255653cd284SVlad Buslov 	spin_lock_bh(&v->tcf_lock);
2567e0c8892SDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
257445d3749SPaul E. McKenney 	p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
258653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
2594c5b9d96SManish Kurup 
2607e0c8892SDavide Caratti 	if (goto_ch)
2617e0c8892SDavide Caratti 		tcf_chain_put_by_act(goto_ch);
262764e9a24SVlad Buslov 	if (p)
263764e9a24SVlad Buslov 		kfree_rcu(p, rcu);
264c7e2b968SJiri Pirko 
265c7e2b968SJiri Pirko 	return ret;
2667e0c8892SDavide Caratti put_chain:
2677e0c8892SDavide Caratti 	if (goto_ch)
2687e0c8892SDavide Caratti 		tcf_chain_put_by_act(goto_ch);
2697e0c8892SDavide Caratti release_idr:
2707e0c8892SDavide Caratti 	tcf_idr_release(*a, bind);
2717e0c8892SDavide Caratti 	return err;
272c7e2b968SJiri Pirko }
273c7e2b968SJiri Pirko 
2749a63b255SCong Wang static void tcf_vlan_cleanup(struct tc_action *a)
2754c5b9d96SManish Kurup {
2764c5b9d96SManish Kurup 	struct tcf_vlan *v = to_vlan(a);
2774c5b9d96SManish Kurup 	struct tcf_vlan_params *p;
2784c5b9d96SManish Kurup 
2794c5b9d96SManish Kurup 	p = rcu_dereference_protected(v->vlan_p, 1);
2801edf8abeSDavide Caratti 	if (p)
2814c5b9d96SManish Kurup 		kfree_rcu(p, rcu);
2824c5b9d96SManish Kurup }
2834c5b9d96SManish Kurup 
284c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
285c7e2b968SJiri Pirko 			 int bind, int ref)
286c7e2b968SJiri Pirko {
287c7e2b968SJiri Pirko 	unsigned char *b = skb_tail_pointer(skb);
288a85a970aSWANG Cong 	struct tcf_vlan *v = to_vlan(a);
289764e9a24SVlad Buslov 	struct tcf_vlan_params *p;
290c7e2b968SJiri Pirko 	struct tc_vlan opt = {
291c7e2b968SJiri Pirko 		.index    = v->tcf_index,
292036bb443SVlad Buslov 		.refcnt   = refcount_read(&v->tcf_refcnt) - ref,
293036bb443SVlad Buslov 		.bindcnt  = atomic_read(&v->tcf_bindcnt) - bind,
294c7e2b968SJiri Pirko 	};
295c7e2b968SJiri Pirko 	struct tcf_t t;
296c7e2b968SJiri Pirko 
297653cd284SVlad Buslov 	spin_lock_bh(&v->tcf_lock);
298764e9a24SVlad Buslov 	opt.action = v->tcf_action;
299764e9a24SVlad Buslov 	p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
300764e9a24SVlad Buslov 	opt.v_action = p->tcfv_action;
301c7e2b968SJiri Pirko 	if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
302c7e2b968SJiri Pirko 		goto nla_put_failure;
303c7e2b968SJiri Pirko 
3044c5b9d96SManish Kurup 	if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
3054c5b9d96SManish Kurup 	     p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
3064c5b9d96SManish Kurup 	    (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
3070b0f43feSJamal Hadi Salim 	     nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
3084c5b9d96SManish Kurup 			  p->tcfv_push_proto) ||
3098323b20fSBoris Sukholitko 	     (p->tcfv_push_prio_exists &&
3108323b20fSBoris Sukholitko 	      nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
311c7e2b968SJiri Pirko 		goto nla_put_failure;
312c7e2b968SJiri Pirko 
31319fbcb36SGuillaume Nault 	if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) {
31419fbcb36SGuillaume Nault 		if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN,
31519fbcb36SGuillaume Nault 			    p->tcfv_push_dst))
31619fbcb36SGuillaume Nault 			goto nla_put_failure;
31719fbcb36SGuillaume Nault 		if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN,
31819fbcb36SGuillaume Nault 			    p->tcfv_push_src))
31919fbcb36SGuillaume Nault 			goto nla_put_failure;
32019fbcb36SGuillaume Nault 	}
32119fbcb36SGuillaume Nault 
32248d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &v->tcf_tm);
3239854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
324c7e2b968SJiri Pirko 		goto nla_put_failure;
325653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
326764e9a24SVlad Buslov 
327c7e2b968SJiri Pirko 	return skb->len;
328c7e2b968SJiri Pirko 
329c7e2b968SJiri Pirko nla_put_failure:
330653cd284SVlad Buslov 	spin_unlock_bh(&v->tcf_lock);
331c7e2b968SJiri Pirko 	nlmsg_trim(skb, b);
332c7e2b968SJiri Pirko 	return -1;
333c7e2b968SJiri Pirko }
334c7e2b968SJiri Pirko 
335ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
336ddf97ccdSWANG Cong 			   struct netlink_callback *cb, int type,
33741780105SAlexander Aring 			   const struct tc_action_ops *ops,
33841780105SAlexander Aring 			   struct netlink_ext_ack *extack)
339ddf97ccdSWANG Cong {
340*acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
341ddf97ccdSWANG Cong 
342b3620145SAlexander Aring 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
343ddf97ccdSWANG Cong }
344ddf97ccdSWANG Cong 
3454b61d3e8SPo Liu static void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets,
3464b61d3e8SPo Liu 				  u64 drops, u64 lastuse, bool hw)
347fa730a3bSJiri Pirko {
348fa730a3bSJiri Pirko 	struct tcf_vlan *v = to_vlan(a);
349fa730a3bSJiri Pirko 	struct tcf_t *tm = &v->tcf_tm;
350fa730a3bSJiri Pirko 
3514b61d3e8SPo Liu 	tcf_action_update_stats(a, bytes, packets, drops, hw);
352fa730a3bSJiri Pirko 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
353fa730a3bSJiri Pirko }
354fa730a3bSJiri Pirko 
355f061b48cSCong Wang static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
356ddf97ccdSWANG Cong {
357*acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
358ddf97ccdSWANG Cong 
35965a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
360ddf97ccdSWANG Cong }
361ddf97ccdSWANG Cong 
362b35475c5SRoman Mashak static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
363b35475c5SRoman Mashak {
364b35475c5SRoman Mashak 	return nla_total_size(sizeof(struct tc_vlan))
365b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
366b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
367b35475c5SRoman Mashak 		+ nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
368b35475c5SRoman Mashak }
369b35475c5SRoman Mashak 
370c54e1d92SBaowen Zheng static int tcf_vlan_offload_act_setup(struct tc_action *act, void *entry_data,
371c2ccf84eSIdo Schimmel 				      u32 *index_inc, bool bind,
372c2ccf84eSIdo Schimmel 				      struct netlink_ext_ack *extack)
373c54e1d92SBaowen Zheng {
374c54e1d92SBaowen Zheng 	if (bind) {
375c54e1d92SBaowen Zheng 		struct flow_action_entry *entry = entry_data;
376c54e1d92SBaowen Zheng 
377c54e1d92SBaowen Zheng 		switch (tcf_vlan_action(act)) {
378c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_PUSH:
379c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_PUSH;
380c54e1d92SBaowen Zheng 			entry->vlan.vid = tcf_vlan_push_vid(act);
381c54e1d92SBaowen Zheng 			entry->vlan.proto = tcf_vlan_push_proto(act);
382c54e1d92SBaowen Zheng 			entry->vlan.prio = tcf_vlan_push_prio(act);
383c54e1d92SBaowen Zheng 			break;
384c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_POP:
385c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_POP;
386c54e1d92SBaowen Zheng 			break;
387c54e1d92SBaowen Zheng 		case TCA_VLAN_ACT_MODIFY:
388c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_VLAN_MANGLE;
389c54e1d92SBaowen Zheng 			entry->vlan.vid = tcf_vlan_push_vid(act);
390c54e1d92SBaowen Zheng 			entry->vlan.proto = tcf_vlan_push_proto(act);
391c54e1d92SBaowen Zheng 			entry->vlan.prio = tcf_vlan_push_prio(act);
392c54e1d92SBaowen Zheng 			break;
393ab95465cSMaor Dickman 		case TCA_VLAN_ACT_POP_ETH:
394ab95465cSMaor Dickman 			entry->id = FLOW_ACTION_VLAN_POP_ETH;
395ab95465cSMaor Dickman 			break;
396ab95465cSMaor Dickman 		case TCA_VLAN_ACT_PUSH_ETH:
397ab95465cSMaor Dickman 			entry->id = FLOW_ACTION_VLAN_PUSH_ETH;
398ab95465cSMaor Dickman 			tcf_vlan_push_eth(entry->vlan_push_eth.src, entry->vlan_push_eth.dst, act);
399ab95465cSMaor Dickman 			break;
400c54e1d92SBaowen Zheng 		default:
401f8fab316SIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Unsupported vlan action mode offload");
402c54e1d92SBaowen Zheng 			return -EOPNOTSUPP;
403c54e1d92SBaowen Zheng 		}
404c54e1d92SBaowen Zheng 		*index_inc = 1;
405c54e1d92SBaowen Zheng 	} else {
4068cbfe939SBaowen Zheng 		struct flow_offload_action *fl_action = entry_data;
4078cbfe939SBaowen Zheng 
4088cbfe939SBaowen Zheng 		switch (tcf_vlan_action(act)) {
4098cbfe939SBaowen Zheng 		case TCA_VLAN_ACT_PUSH:
4108cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_VLAN_PUSH;
4118cbfe939SBaowen Zheng 			break;
4128cbfe939SBaowen Zheng 		case TCA_VLAN_ACT_POP:
4138cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_VLAN_POP;
4148cbfe939SBaowen Zheng 			break;
4158cbfe939SBaowen Zheng 		case TCA_VLAN_ACT_MODIFY:
4168cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_VLAN_MANGLE;
4178cbfe939SBaowen Zheng 			break;
418ab95465cSMaor Dickman 		case TCA_VLAN_ACT_POP_ETH:
419ab95465cSMaor Dickman 			fl_action->id = FLOW_ACTION_VLAN_POP_ETH;
420ab95465cSMaor Dickman 			break;
421ab95465cSMaor Dickman 		case TCA_VLAN_ACT_PUSH_ETH:
422ab95465cSMaor Dickman 			fl_action->id = FLOW_ACTION_VLAN_PUSH_ETH;
423ab95465cSMaor Dickman 			break;
4248cbfe939SBaowen Zheng 		default:
425c54e1d92SBaowen Zheng 			return -EOPNOTSUPP;
426c54e1d92SBaowen Zheng 		}
4278cbfe939SBaowen Zheng 	}
428c54e1d92SBaowen Zheng 
429c54e1d92SBaowen Zheng 	return 0;
430c54e1d92SBaowen Zheng }
431c54e1d92SBaowen Zheng 
432c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = {
433c7e2b968SJiri Pirko 	.kind		=	"vlan",
434eddd2cf1SEli Cohen 	.id		=	TCA_ID_VLAN,
435c7e2b968SJiri Pirko 	.owner		=	THIS_MODULE,
4368aa7f22eSJamal Hadi Salim 	.act		=	tcf_vlan_act,
437c7e2b968SJiri Pirko 	.dump		=	tcf_vlan_dump,
438c7e2b968SJiri Pirko 	.init		=	tcf_vlan_init,
4394c5b9d96SManish Kurup 	.cleanup	=	tcf_vlan_cleanup,
440ddf97ccdSWANG Cong 	.walk		=	tcf_vlan_walker,
441fa730a3bSJiri Pirko 	.stats_update	=	tcf_vlan_stats_update,
442b35475c5SRoman Mashak 	.get_fill_size	=	tcf_vlan_get_fill_size,
443ddf97ccdSWANG Cong 	.lookup		=	tcf_vlan_search,
444c54e1d92SBaowen Zheng 	.offload_act_setup =	tcf_vlan_offload_act_setup,
445a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_vlan),
446ddf97ccdSWANG Cong };
447ddf97ccdSWANG Cong 
448ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net)
449ddf97ccdSWANG Cong {
450*acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
451ddf97ccdSWANG Cong 
452981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_vlan_ops);
453ddf97ccdSWANG Cong }
454ddf97ccdSWANG Cong 
455039af9c6SCong Wang static void __net_exit vlan_exit_net(struct list_head *net_list)
456ddf97ccdSWANG Cong {
457*acd0a7abSZhengchao Shao 	tc_action_net_exit(net_list, act_vlan_ops.net_id);
458ddf97ccdSWANG Cong }
459ddf97ccdSWANG Cong 
460ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = {
461ddf97ccdSWANG Cong 	.init = vlan_init_net,
462039af9c6SCong Wang 	.exit_batch = vlan_exit_net,
463*acd0a7abSZhengchao Shao 	.id   = &act_vlan_ops.net_id,
464ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
465c7e2b968SJiri Pirko };
466c7e2b968SJiri Pirko 
467c7e2b968SJiri Pirko static int __init vlan_init_module(void)
468c7e2b968SJiri Pirko {
469ddf97ccdSWANG Cong 	return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
470c7e2b968SJiri Pirko }
471c7e2b968SJiri Pirko 
472c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void)
473c7e2b968SJiri Pirko {
474ddf97ccdSWANG Cong 	tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
475c7e2b968SJiri Pirko }
476c7e2b968SJiri Pirko 
477c7e2b968SJiri Pirko module_init(vlan_init_module);
478c7e2b968SJiri Pirko module_exit(vlan_cleanup_module);
479c7e2b968SJiri Pirko 
480c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
481c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions");
482c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2");
483