Merge tag 'rproc-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux.git] / net / bridge / br_vlan_tunnel.c
bloba966a6ec826343233e7b0000f5f62f7b7b53b8a4
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Bridge per vlan tunnel port dst_metadata handling code
5 * Authors:
6 * Roopa Prabhu <roopa@cumulusnetworks.com>
7 */
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>
16 #include "br_private.h"
17 #include "br_private_tunnel.h"
19 static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg *arg,
20 const void *ptr)
22 const struct net_bridge_vlan *vle = ptr;
23 __be64 tunid = *(__be64 *)arg->key;
25 return vle->tinfo.tunnel_id != tunid;
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,
37 static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
38 __be64 tunnel_id)
40 return rhashtable_lookup_fast(tbl, &tunnel_id,
41 br_vlan_tunnel_rht_params);
44 static void vlan_tunnel_info_release(struct net_bridge_vlan *vlan)
46 struct metadata_dst *tdst = rtnl_dereference(vlan->tinfo.tunnel_dst);
48 WRITE_ONCE(vlan->tinfo.tunnel_id, 0);
49 RCU_INIT_POINTER(vlan->tinfo.tunnel_dst, NULL);
50 dst_release(&tdst->dst);
53 void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
54 struct net_bridge_vlan *vlan)
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);
63 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
64 struct net_bridge_vlan *vlan, u32 tun_id)
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;
71 if (metadata)
72 return -EEXIST;
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;
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);
83 err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
84 br_vlan_tunnel_rht_params);
85 if (err)
86 goto out;
88 return 0;
89 out:
90 vlan_tunnel_info_release(vlan);
92 return err;
95 /* Must be protected by RTNL.
96 * Must be called with vid in range from 1 to 4094 inclusive.
98 int nbp_vlan_tunnel_info_add(const struct net_bridge_port *port, u16 vid,
99 u32 tun_id)
101 struct net_bridge_vlan_group *vg;
102 struct net_bridge_vlan *vlan;
104 ASSERT_RTNL();
106 vg = nbp_vlan_group(port);
107 vlan = br_vlan_find(vg, vid);
108 if (!vlan)
109 return -EINVAL;
111 return __vlan_tunnel_info_add(vg, vlan, tun_id);
114 /* Must be protected by RTNL.
115 * Must be called with vid in range from 1 to 4094 inclusive.
117 int nbp_vlan_tunnel_info_delete(const struct net_bridge_port *port, u16 vid)
119 struct net_bridge_vlan_group *vg;
120 struct net_bridge_vlan *v;
122 ASSERT_RTNL();
124 vg = nbp_vlan_group(port);
125 v = br_vlan_find(vg, vid);
126 if (!v)
127 return -ENOENT;
129 vlan_tunnel_info_del(vg, v);
131 return 0;
134 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
136 struct net_bridge_vlan *vlan, *tmp;
138 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
139 vlan_tunnel_info_del(vg, vlan);
142 void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
144 struct net_bridge_vlan_group *vg;
146 ASSERT_RTNL();
148 vg = nbp_vlan_group(port);
149 __vlan_tunnel_info_flush(vg);
152 int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
154 return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
157 void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
159 rhashtable_destroy(&vg->tunnel_hash);
162 void br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
163 struct net_bridge_port *p,
164 struct net_bridge_vlan_group *vg)
166 struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
167 struct net_bridge_vlan *vlan;
169 if (!vg || !tinfo)
170 return;
172 /* if already tagged, ignore */
173 if (skb_vlan_tagged(skb))
174 return;
176 /* lookup vid, given tunnel id */
177 vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
178 if (!vlan)
179 return;
181 skb_dst_drop(skb);
183 __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
186 int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
187 struct net_bridge_vlan *vlan)
189 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
190 struct metadata_dst *tunnel_dst;
191 __be64 tunnel_id;
192 int err;
194 if (!vlan)
195 return 0;
197 tunnel_id = READ_ONCE(vlan->tinfo.tunnel_id);
198 if (!tunnel_id || unlikely(!skb_vlan_tag_present(skb)))
199 return 0;
201 skb_dst_drop(skb);
202 err = skb_vlan_pop(skb);
203 if (err)
204 return err;
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;
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);
219 return 0;
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);
226 return 0;