xref: /linux/net/bridge/br_vlan_tunnel.c (revision bfb921b2a9d5d1123d1d10b196a39db629ddef87)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Bridge per vlan tunnel port dst_metadata handling code
4  *
5  *	Authors:
6  *	Roopa Prabhu		<roopa@cumulusnetworks.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <net/switchdev.h>
14 #include <net/dst_metadata.h>
15 
16 #include "br_private.h"
17 #include "br_private_tunnel.h"
18 
19 static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg *arg,
20 				    const void *ptr)
21 {
22 	const struct net_bridge_vlan *vle = ptr;
23 	__be64 tunid = *(__be64 *)arg->key;
24 
25 	return vle->tinfo.tunnel_id != tunid;
26 }
27 
28 static const struct rhashtable_params br_vlan_tunnel_rht_params = {
29 	.head_offset = offsetof(struct net_bridge_vlan, tnode),
30 	.key_offset = offsetof(struct net_bridge_vlan, tinfo.tunnel_id),
31 	.key_len = sizeof(__be64),
32 	.nelem_hint = 3,
33 	.obj_cmpfn = br_vlan_tunid_cmp,
34 	.automatic_shrinking = true,
35 };
36 
37 static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
38 						     __be64 tunnel_id)
39 {
40 	return rhashtable_lookup_fast(tbl, &tunnel_id,
41 				      br_vlan_tunnel_rht_params);
42 }
43 
44 static void vlan_tunnel_info_release(struct net_bridge_vlan *vlan)
45 {
46 	struct metadata_dst *tdst = rtnl_dereference(vlan->tinfo.tunnel_dst);
47 
48 	WRITE_ONCE(vlan->tinfo.tunnel_id, 0);
49 	RCU_INIT_POINTER(vlan->tinfo.tunnel_dst, NULL);
50 	dst_release(&tdst->dst);
51 }
52 
53 void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
54 			  struct net_bridge_vlan *vlan)
55 {
56 	if (!rcu_access_pointer(vlan->tinfo.tunnel_dst))
57 		return;
58 	rhashtable_remove_fast(&vg->tunnel_hash, &vlan->tnode,
59 			       br_vlan_tunnel_rht_params);
60 	vlan_tunnel_info_release(vlan);
61 }
62 
63 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
64 				  struct net_bridge_vlan *vlan, u32 tun_id)
65 {
66 	struct metadata_dst *metadata = rtnl_dereference(vlan->tinfo.tunnel_dst);
67 	__be64 key = key32_to_tunnel_id(cpu_to_be32(tun_id));
68 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
69 	int err;
70 
71 	if (metadata)
72 		return -EEXIST;
73 
74 	__set_bit(IP_TUNNEL_KEY_BIT, flags);
75 	metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, flags, key, 0);
76 	if (!metadata)
77 		return -EINVAL;
78 
79 	metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_BRIDGE;
80 	rcu_assign_pointer(vlan->tinfo.tunnel_dst, metadata);
81 	WRITE_ONCE(vlan->tinfo.tunnel_id, key);
82 
83 	err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
84 					    br_vlan_tunnel_rht_params);
85 	if (err)
86 		goto out;
87 
88 	return 0;
89 out:
90 	vlan_tunnel_info_release(vlan);
91 
92 	return err;
93 }
94 
95 /* Must be protected by RTNL.
96  * Must be called with vid in range from 1 to 4094 inclusive.
97  */
98 int nbp_vlan_tunnel_info_add(const struct net_bridge_port *port, u16 vid,
99 			     u32 tun_id)
100 {
101 	struct net_bridge_vlan_group *vg;
102 	struct net_bridge_vlan *vlan;
103 
104 	ASSERT_RTNL();
105 
106 	vg = nbp_vlan_group(port);
107 	vlan = br_vlan_find(vg, vid);
108 	if (!vlan)
109 		return -EINVAL;
110 
111 	return __vlan_tunnel_info_add(vg, vlan, tun_id);
112 }
113 
114 /* Must be protected by RTNL.
115  * Must be called with vid in range from 1 to 4094 inclusive.
116  */
117 int nbp_vlan_tunnel_info_delete(const struct net_bridge_port *port, u16 vid)
118 {
119 	struct net_bridge_vlan_group *vg;
120 	struct net_bridge_vlan *v;
121 
122 	ASSERT_RTNL();
123 
124 	vg = nbp_vlan_group(port);
125 	v = br_vlan_find(vg, vid);
126 	if (!v)
127 		return -ENOENT;
128 
129 	vlan_tunnel_info_del(vg, v);
130 
131 	return 0;
132 }
133 
134 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
135 {
136 	struct net_bridge_vlan *vlan, *tmp;
137 
138 	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
139 		vlan_tunnel_info_del(vg, vlan);
140 }
141 
142 void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
143 {
144 	struct net_bridge_vlan_group *vg;
145 
146 	ASSERT_RTNL();
147 
148 	vg = nbp_vlan_group(port);
149 	__vlan_tunnel_info_flush(vg);
150 }
151 
152 int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
153 {
154 	return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
155 }
156 
157 void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
158 {
159 	rhashtable_destroy(&vg->tunnel_hash);
160 }
161 
162 void br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
163 				   struct net_bridge_port *p,
164 				   struct net_bridge_vlan_group *vg)
165 {
166 	struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
167 	struct net_bridge_vlan *vlan;
168 
169 	if (!vg || !tinfo)
170 		return;
171 
172 	/* if already tagged, ignore */
173 	if (skb_vlan_tagged(skb))
174 		return;
175 
176 	/* lookup vid, given tunnel id */
177 	vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
178 	if (!vlan)
179 		return;
180 
181 	skb_dst_drop(skb);
182 
183 	__vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
184 }
185 
186 int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
187 				 struct net_bridge_vlan *vlan)
188 {
189 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
190 	struct metadata_dst *tunnel_dst;
191 	__be64 tunnel_id;
192 	int err;
193 
194 	if (!vlan)
195 		return 0;
196 
197 	tunnel_id = READ_ONCE(vlan->tinfo.tunnel_id);
198 	if (!tunnel_id || unlikely(!skb_vlan_tag_present(skb)))
199 		return 0;
200 
201 	skb_dst_drop(skb);
202 	err = skb_vlan_pop(skb);
203 	if (err)
204 		return err;
205 
206 	if (BR_INPUT_SKB_CB(skb)->backup_nhid) {
207 		__set_bit(IP_TUNNEL_KEY_BIT, flags);
208 		tunnel_dst = __ip_tun_set_dst(0, 0, 0, 0, 0, flags,
209 					      tunnel_id, 0);
210 		if (!tunnel_dst)
211 			return -ENOMEM;
212 
213 		tunnel_dst->u.tun_info.mode |= IP_TUNNEL_INFO_TX |
214 					       IP_TUNNEL_INFO_BRIDGE;
215 		tunnel_dst->u.tun_info.key.nhid =
216 			BR_INPUT_SKB_CB(skb)->backup_nhid;
217 		skb_dst_set(skb, &tunnel_dst->dst);
218 
219 		return 0;
220 	}
221 
222 	tunnel_dst = rcu_dereference(vlan->tinfo.tunnel_dst);
223 	if (tunnel_dst && dst_hold_safe(&tunnel_dst->dst))
224 		skb_dst_set(skb, &tunnel_dst->dst);
225 
226 	return 0;
227 }
228