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