Merge tag 'mips_fixes_5.2_2' of git://git.kernel.org/pub/scm/linux/kernel/git/mips...
[linux/fpc-iii.git] / net / bridge / br_vlan_tunnel.c
blobd13d2080f5277bce914df6b72c023d16e28d160a
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 u64 tunnel_id)
40 return rhashtable_lookup_fast(tbl, &tunnel_id,
41 br_vlan_tunnel_rht_params);
44 void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
45 struct net_bridge_vlan *vlan)
47 if (!vlan->tinfo.tunnel_dst)
48 return;
49 rhashtable_remove_fast(&vg->tunnel_hash, &vlan->tnode,
50 br_vlan_tunnel_rht_params);
51 vlan->tinfo.tunnel_id = 0;
52 dst_release(&vlan->tinfo.tunnel_dst->dst);
53 vlan->tinfo.tunnel_dst = NULL;
56 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
57 struct net_bridge_vlan *vlan, u32 tun_id)
59 struct metadata_dst *metadata = NULL;
60 __be64 key = key32_to_tunnel_id(cpu_to_be32(tun_id));
61 int err;
63 if (vlan->tinfo.tunnel_dst)
64 return -EEXIST;
66 metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
67 key, 0);
68 if (!metadata)
69 return -EINVAL;
71 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_BRIDGE;
72 vlan->tinfo.tunnel_dst = metadata;
73 vlan->tinfo.tunnel_id = key;
75 err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
76 br_vlan_tunnel_rht_params);
77 if (err)
78 goto out;
80 return 0;
81 out:
82 dst_release(&vlan->tinfo.tunnel_dst->dst);
83 vlan->tinfo.tunnel_dst = NULL;
84 vlan->tinfo.tunnel_id = 0;
86 return err;
89 /* Must be protected by RTNL.
90 * Must be called with vid in range from 1 to 4094 inclusive.
92 int nbp_vlan_tunnel_info_add(struct net_bridge_port *port, u16 vid, u32 tun_id)
94 struct net_bridge_vlan_group *vg;
95 struct net_bridge_vlan *vlan;
97 ASSERT_RTNL();
99 vg = nbp_vlan_group(port);
100 vlan = br_vlan_find(vg, vid);
101 if (!vlan)
102 return -EINVAL;
104 return __vlan_tunnel_info_add(vg, vlan, tun_id);
107 /* Must be protected by RTNL.
108 * Must be called with vid in range from 1 to 4094 inclusive.
110 int nbp_vlan_tunnel_info_delete(struct net_bridge_port *port, u16 vid)
112 struct net_bridge_vlan_group *vg;
113 struct net_bridge_vlan *v;
115 ASSERT_RTNL();
117 vg = nbp_vlan_group(port);
118 v = br_vlan_find(vg, vid);
119 if (!v)
120 return -ENOENT;
122 vlan_tunnel_info_del(vg, v);
124 return 0;
127 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
129 struct net_bridge_vlan *vlan, *tmp;
131 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
132 vlan_tunnel_info_del(vg, vlan);
135 void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
137 struct net_bridge_vlan_group *vg;
139 ASSERT_RTNL();
141 vg = nbp_vlan_group(port);
142 __vlan_tunnel_info_flush(vg);
145 int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
147 return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
150 void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
152 rhashtable_destroy(&vg->tunnel_hash);
155 int br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
156 struct net_bridge_port *p,
157 struct net_bridge_vlan_group *vg)
159 struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
160 struct net_bridge_vlan *vlan;
162 if (!vg || !tinfo)
163 return 0;
165 /* if already tagged, ignore */
166 if (skb_vlan_tagged(skb))
167 return 0;
169 /* lookup vid, given tunnel id */
170 vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
171 if (!vlan)
172 return 0;
174 skb_dst_drop(skb);
176 __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
178 return 0;
181 int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
182 struct net_bridge_vlan *vlan)
184 int err;
186 if (!vlan || !vlan->tinfo.tunnel_id)
187 return 0;
189 if (unlikely(!skb_vlan_tag_present(skb)))
190 return 0;
192 skb_dst_drop(skb);
193 err = skb_vlan_pop(skb);
194 if (err)
195 return err;
197 skb_dst_set(skb, dst_clone(&vlan->tinfo.tunnel_dst->dst));
199 return 0;