Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2 | /* |
Rami Rosen | eb5ce43 | 2012-11-13 13:29:15 +0000 | [diff] [blame] | 3 | * VXLAN: Virtual eXtensible Local Area Network |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4 | * |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 5 | * Copyright (c) 2012-2013 Vyatta Inc. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
| 10 | #include <linux/kernel.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/slab.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 14 | #include <linux/udp.h> |
| 15 | #include <linux/igmp.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 16 | #include <linux/if_ether.h> |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 17 | #include <linux/ethtool.h> |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 18 | #include <net/arp.h> |
| 19 | #include <net/ndisc.h> |
David Ahern | 3616d08 | 2019-03-22 06:06:09 -0700 | [diff] [blame] | 20 | #include <net/ipv6_stubs.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 21 | #include <net/ip.h> |
| 22 | #include <net/icmp.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 23 | #include <net/rtnetlink.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 24 | #include <net/inet_ecn.h> |
| 25 | #include <net/net_namespace.h> |
| 26 | #include <net/netns/generic.h> |
Jiri Benc | fa20e0e | 2017-08-28 21:43:22 +0200 | [diff] [blame] | 27 | #include <net/tun_proto.h> |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 28 | #include <net/vxlan.h> |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 29 | #include <net/nexthop.h> |
stephen hemminger | 40d29af | 2016-02-09 22:07:29 -0800 | [diff] [blame] | 30 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 31 | #if IS_ENABLED(CONFIG_IPV6) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 32 | #include <net/ip6_tunnel.h> |
Cong Wang | 660d98c | 2013-09-02 10:06:52 +0800 | [diff] [blame] | 33 | #include <net/ip6_checksum.h> |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 34 | #endif |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 35 | |
| 36 | #define VXLAN_VERSION "0.1" |
| 37 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 38 | #define PORT_HASH_BITS 8 |
| 39 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 40 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
| 41 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
| 42 | |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 43 | /* UDP port for VXLAN traffic. |
| 44 | * The IANA assigned port is 4789, but the Linux default is 8472 |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 45 | * for compatibility with early adopters. |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 46 | */ |
Stephen Hemminger | 9daaa39 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 47 | static unsigned short vxlan_port __read_mostly = 8472; |
| 48 | module_param_named(udp_port, vxlan_port, ushort, 0444); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 49 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
| 50 | |
| 51 | static bool log_ecn_error = true; |
| 52 | module_param(log_ecn_error, bool, 0644); |
| 53 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 54 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 55 | static unsigned int vxlan_net_id; |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 56 | static struct rtnl_link_ops vxlan_link_ops; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 57 | |
Li RongQing | 7256eac | 2016-01-29 09:43:47 +0800 | [diff] [blame] | 58 | static const u8 all_zeros_mac[ETH_ALEN + 2]; |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 59 | |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 60 | static int vxlan_sock_add(struct vxlan_dev *vxlan); |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 61 | |
Mark Bloch | a53cb29 | 2017-06-02 03:24:08 +0300 | [diff] [blame] | 62 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan); |
| 63 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 64 | /* per-network namespace private data for this module */ |
| 65 | struct vxlan_net { |
| 66 | struct list_head vxlan_list; |
| 67 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 68 | spinlock_t sock_lock; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 71 | /* Forwarding table entry */ |
| 72 | struct vxlan_fdb { |
| 73 | struct hlist_node hlist; /* linked list of entries */ |
| 74 | struct rcu_head rcu; |
| 75 | unsigned long updated; /* jiffies */ |
| 76 | unsigned long used; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 77 | struct list_head remotes; |
Sowmini Varadhan | 7177a3b | 2015-07-20 09:54:50 +0200 | [diff] [blame] | 78 | u8 eth_addr[ETH_ALEN]; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 79 | u16 state; /* see ndm_state */ |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 80 | __be32 vni; |
Petr Machata | 45598c1 | 2018-11-21 08:02:36 +0000 | [diff] [blame] | 81 | u16 flags; /* see ndm_flags and below */ |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 82 | struct list_head nh_list; |
| 83 | struct nexthop __rcu *nh; |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 84 | struct vxlan_dev __rcu *vdev; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Petr Machata | 45598c1 | 2018-11-21 08:02:36 +0000 | [diff] [blame] | 87 | #define NTF_VXLAN_ADDED_BY_USER 0x100 |
| 88 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 89 | /* salt for hash table */ |
| 90 | static u32 vxlan_salt __read_mostly; |
| 91 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 92 | static inline bool vxlan_collect_metadata(struct vxlan_sock *vs) |
| 93 | { |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 94 | return vs->flags & VXLAN_F_COLLECT_METADATA || |
| 95 | ip_tunnel_collect_metadata(); |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 98 | #if IS_ENABLED(CONFIG_IPV6) |
| 99 | static inline |
| 100 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) |
| 101 | { |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 102 | if (a->sa.sa_family != b->sa.sa_family) |
| 103 | return false; |
| 104 | if (a->sa.sa_family == AF_INET6) |
| 105 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); |
| 106 | else |
| 107 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 110 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
| 111 | { |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 112 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 113 | ip->sin6.sin6_addr = nla_get_in6_addr(nla); |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 114 | ip->sa.sa_family = AF_INET6; |
| 115 | return 0; |
| 116 | } else if (nla_len(nla) >= sizeof(__be32)) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 117 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 118 | ip->sa.sa_family = AF_INET; |
| 119 | return 0; |
| 120 | } else { |
| 121 | return -EAFNOSUPPORT; |
| 122 | } |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 126 | const union vxlan_addr *ip) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 127 | { |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 128 | if (ip->sa.sa_family == AF_INET6) |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 129 | return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr); |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 130 | else |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 131 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #else /* !CONFIG_IPV6 */ |
| 135 | |
| 136 | static inline |
| 137 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) |
| 138 | { |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 139 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 142 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
| 143 | { |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 144 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
| 145 | return -EAFNOSUPPORT; |
| 146 | } else if (nla_len(nla) >= sizeof(__be32)) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 147 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 148 | ip->sa.sa_family = AF_INET; |
| 149 | return 0; |
| 150 | } else { |
| 151 | return -EAFNOSUPPORT; |
| 152 | } |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, |
Jiri Benc | f0ef312 | 2015-03-29 16:17:37 +0200 | [diff] [blame] | 156 | const union vxlan_addr *ip) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 157 | { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 158 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 159 | } |
| 160 | #endif |
| 161 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 162 | /* Virtual Network hash table head */ |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 163 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 164 | { |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 165 | return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)]; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Socket hash table head */ |
| 169 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 170 | { |
| 171 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 172 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 173 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
| 174 | } |
| 175 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 176 | /* First remote destination for a forwarding entry. |
| 177 | * Guaranteed to be non-NULL because remotes are never deleted. |
| 178 | */ |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 179 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 180 | { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 181 | if (rcu_access_pointer(fdb->nh)) |
| 182 | return NULL; |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 183 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
| 184 | } |
| 185 | |
| 186 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) |
| 187 | { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 188 | if (rcu_access_pointer(fdb->nh)) |
| 189 | return NULL; |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 190 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Thomas Graf | ac5132d | 2015-01-15 03:53:56 +0100 | [diff] [blame] | 193 | /* Find VXLAN socket based on network namespace, address family and UDP port |
| 194 | * and enabled unshareable flags. |
| 195 | */ |
| 196 | static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family, |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 197 | __be16 port, u32 flags, int ifindex) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 198 | { |
| 199 | struct vxlan_sock *vs; |
Tom Herbert | af33c1a | 2015-01-20 11:23:05 -0800 | [diff] [blame] | 200 | |
| 201 | flags &= VXLAN_F_RCV_FLAGS; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 202 | |
| 203 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 204 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
Jiri Benc | 705cc62 | 2015-08-20 13:56:28 +0200 | [diff] [blame] | 205 | vxlan_get_sk_family(vs) == family && |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 206 | vs->flags == flags && |
| 207 | vs->sock->sk->sk_bound_dev_if == ifindex) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 208 | return vs; |
| 209 | } |
| 210 | return NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 213 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex, |
| 214 | __be32 vni) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 215 | { |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 216 | struct vxlan_dev_node *node; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 217 | |
Jiri Benc | b9167b2 | 2016-02-16 21:59:03 +0100 | [diff] [blame] | 218 | /* For flow based devices, map all packets to VNI 0 */ |
| 219 | if (vs->flags & VXLAN_F_COLLECT_METADATA) |
| 220 | vni = 0; |
| 221 | |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 222 | hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) { |
| 223 | if (node->vxlan->default_dst.remote_vni != vni) |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 224 | continue; |
| 225 | |
| 226 | if (IS_ENABLED(CONFIG_IPV6)) { |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 227 | const struct vxlan_config *cfg = &node->vxlan->cfg; |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 228 | |
| 229 | if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) && |
| 230 | cfg->remote_ifindex != ifindex) |
| 231 | continue; |
| 232 | } |
| 233 | |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 234 | return node->vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 240 | /* Look up VNI in a per net namespace table */ |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 241 | static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex, |
| 242 | __be32 vni, sa_family_t family, |
| 243 | __be16 port, u32 flags) |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 244 | { |
| 245 | struct vxlan_sock *vs; |
| 246 | |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 247 | vs = vxlan_find_sock(net, family, port, flags, ifindex); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 248 | if (!vs) |
| 249 | return NULL; |
| 250 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 251 | return vxlan_vs_find_vni(vs, ifindex, vni); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 252 | } |
| 253 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 254 | /* Fill in neighbour message in skbuff. */ |
| 255 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 256 | const struct vxlan_fdb *fdb, |
| 257 | u32 portid, u32 seq, int type, unsigned int flags, |
| 258 | const struct vxlan_rdst *rdst) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 259 | { |
| 260 | unsigned long now = jiffies; |
| 261 | struct nda_cacheinfo ci; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 262 | bool send_ip, send_eth; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 263 | struct nlmsghdr *nlh; |
| 264 | struct nexthop *nh; |
| 265 | struct ndmsg *ndm; |
Ido Schimmel | 06ec313 | 2020-05-25 00:38:56 +0300 | [diff] [blame] | 266 | int nh_family; |
| 267 | u32 nh_id; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 268 | |
| 269 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 270 | if (nlh == NULL) |
| 271 | return -EMSGSIZE; |
| 272 | |
| 273 | ndm = nlmsg_data(nlh); |
| 274 | memset(ndm, 0, sizeof(*ndm)); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 275 | |
| 276 | send_eth = send_ip = true; |
| 277 | |
Ido Schimmel | 06ec313 | 2020-05-25 00:38:56 +0300 | [diff] [blame] | 278 | rcu_read_lock(); |
| 279 | nh = rcu_dereference(fdb->nh); |
| 280 | if (nh) { |
| 281 | nh_family = nexthop_get_family(nh); |
| 282 | nh_id = nh->id; |
| 283 | } |
| 284 | rcu_read_unlock(); |
| 285 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 286 | if (type == RTM_GETNEIGH) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 287 | if (rdst) { |
| 288 | send_ip = !vxlan_addr_any(&rdst->remote_ip); |
| 289 | ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET; |
| 290 | } else if (nh) { |
Ido Schimmel | 06ec313 | 2020-05-25 00:38:56 +0300 | [diff] [blame] | 291 | ndm->ndm_family = nh_family; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 292 | } |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 293 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
| 294 | } else |
| 295 | ndm->ndm_family = AF_BRIDGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 296 | ndm->ndm_state = fdb->state; |
| 297 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 298 | ndm->ndm_flags = fdb->flags; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 299 | if (rdst && rdst->offloaded) |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 300 | ndm->ndm_flags |= NTF_OFFLOADED; |
Jun Zhao | 545469f | 2014-07-26 00:38:59 +0800 | [diff] [blame] | 301 | ndm->ndm_type = RTN_UNICAST; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 302 | |
Nicolas Dichtel | 193523b | 2015-01-20 15:15:47 +0100 | [diff] [blame] | 303 | if (!net_eq(dev_net(vxlan->dev), vxlan->net) && |
Nicolas Dichtel | 4967082 | 2015-01-26 14:10:53 +0100 | [diff] [blame] | 304 | nla_put_s32(skb, NDA_LINK_NETNSID, |
WANG Cong | 38f507f | 2016-09-01 21:53:44 -0700 | [diff] [blame] | 305 | peernet2id(dev_net(vxlan->dev), vxlan->net))) |
Nicolas Dichtel | 193523b | 2015-01-20 15:15:47 +0100 | [diff] [blame] | 306 | goto nla_put_failure; |
| 307 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 308 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 309 | goto nla_put_failure; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 310 | if (nh) { |
Ido Schimmel | 06ec313 | 2020-05-25 00:38:56 +0300 | [diff] [blame] | 311 | if (nla_put_u32(skb, NDA_NH_ID, nh_id)) |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 312 | goto nla_put_failure; |
| 313 | } else if (rdst) { |
| 314 | if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, |
| 315 | &rdst->remote_ip)) |
| 316 | goto nla_put_failure; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 317 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 318 | if (rdst->remote_port && |
| 319 | rdst->remote_port != vxlan->cfg.dst_port && |
| 320 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
| 321 | goto nla_put_failure; |
| 322 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
| 323 | nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni))) |
| 324 | goto nla_put_failure; |
| 325 | if (rdst->remote_ifindex && |
| 326 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) |
| 327 | goto nla_put_failure; |
| 328 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 329 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 330 | if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni && |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 331 | nla_put_u32(skb, NDA_SRC_VNI, |
| 332 | be32_to_cpu(fdb->vni))) |
| 333 | goto nla_put_failure; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 334 | |
| 335 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 336 | ci.ndm_confirmed = 0; |
| 337 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 338 | ci.ndm_refcnt = 0; |
| 339 | |
| 340 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 341 | goto nla_put_failure; |
| 342 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 343 | nlmsg_end(skb, nlh); |
| 344 | return 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 345 | |
| 346 | nla_put_failure: |
| 347 | nlmsg_cancel(skb, nlh); |
| 348 | return -EMSGSIZE; |
| 349 | } |
| 350 | |
| 351 | static inline size_t vxlan_nlmsg_size(void) |
| 352 | { |
| 353 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 354 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 355 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 356 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 357 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
| 358 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ |
Nicolas Dichtel | 4967082 | 2015-01-26 14:10:53 +0100 | [diff] [blame] | 359 | + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 360 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 361 | } |
| 362 | |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 363 | static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
| 364 | struct vxlan_rdst *rd, int type) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 365 | { |
| 366 | struct net *net = dev_net(vxlan->dev); |
| 367 | struct sk_buff *skb; |
| 368 | int err = -ENOBUFS; |
| 369 | |
| 370 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
| 371 | if (skb == NULL) |
| 372 | goto errout; |
| 373 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 374 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 375 | if (err < 0) { |
| 376 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
| 377 | WARN_ON(err == -EMSGSIZE); |
| 378 | kfree_skb(skb); |
| 379 | goto errout; |
| 380 | } |
| 381 | |
| 382 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 383 | return; |
| 384 | errout: |
| 385 | if (err < 0) |
| 386 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 387 | } |
| 388 | |
Petr Machata | ff23b91 | 2018-12-07 19:55:03 +0000 | [diff] [blame] | 389 | static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan, |
| 390 | const struct vxlan_fdb *fdb, |
| 391 | const struct vxlan_rdst *rd, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 392 | struct netlink_ext_ack *extack, |
Petr Machata | ff23b91 | 2018-12-07 19:55:03 +0000 | [diff] [blame] | 393 | struct switchdev_notifier_vxlan_fdb_info *fdb_info) |
| 394 | { |
| 395 | fdb_info->info.dev = vxlan->dev; |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 396 | fdb_info->info.extack = extack; |
Petr Machata | ff23b91 | 2018-12-07 19:55:03 +0000 | [diff] [blame] | 397 | fdb_info->remote_ip = rd->remote_ip; |
| 398 | fdb_info->remote_port = rd->remote_port; |
| 399 | fdb_info->remote_vni = rd->remote_vni; |
| 400 | fdb_info->remote_ifindex = rd->remote_ifindex; |
| 401 | memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN); |
| 402 | fdb_info->vni = fdb->vni; |
| 403 | fdb_info->offloaded = rd->offloaded; |
| 404 | fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER; |
| 405 | } |
| 406 | |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 407 | static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan, |
| 408 | struct vxlan_fdb *fdb, |
| 409 | struct vxlan_rdst *rd, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 410 | bool adding, |
| 411 | struct netlink_ext_ack *extack) |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 412 | { |
| 413 | struct switchdev_notifier_vxlan_fdb_info info; |
| 414 | enum switchdev_notifier_type notifier_type; |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 415 | int ret; |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 416 | |
| 417 | if (WARN_ON(!rd)) |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 418 | return 0; |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 419 | |
| 420 | notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE |
| 421 | : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE; |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 422 | vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 423 | ret = call_switchdev_notifiers(notifier_type, vxlan->dev, |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 424 | &info.info, extack); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 425 | return notifier_to_errno(ret); |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 428 | static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 429 | struct vxlan_rdst *rd, int type, bool swdev_notify, |
| 430 | struct netlink_ext_ack *extack) |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 431 | { |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 432 | int err; |
| 433 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 434 | if (swdev_notify && rd) { |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 435 | switch (type) { |
| 436 | case RTM_NEWNEIGH: |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 437 | err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 438 | true, extack); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 439 | if (err) |
| 440 | return err; |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 441 | break; |
| 442 | case RTM_DELNEIGH: |
| 443 | vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 444 | false, extack); |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | __vxlan_fdb_notify(vxlan, fdb, rd, type); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 450 | return 0; |
Petr Machata | 9a99735 | 2018-10-17 08:53:22 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 453 | static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 454 | { |
| 455 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 456 | struct vxlan_fdb f = { |
| 457 | .state = NUD_STALE, |
| 458 | }; |
| 459 | struct vxlan_rdst remote = { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 460 | .remote_ip = *ipa, /* goes to NDA_DST */ |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 461 | .remote_vni = cpu_to_be32(VXLAN_N_VID), |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 462 | }; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 463 | |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 464 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) |
| 468 | { |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 469 | struct vxlan_fdb f = { |
| 470 | .state = NUD_STALE, |
| 471 | }; |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 472 | struct vxlan_rdst remote = { }; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 473 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 474 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
| 475 | |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 476 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 477 | } |
| 478 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 479 | /* Hash Ethernet address */ |
| 480 | static u32 eth_hash(const unsigned char *addr) |
| 481 | { |
| 482 | u64 value = get_unaligned((u64 *)addr); |
| 483 | |
| 484 | /* only want 6 bytes */ |
| 485 | #ifdef __BIG_ENDIAN |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 486 | value >>= 16; |
stephen hemminger | 321fb99 | 2012-10-09 20:35:47 +0000 | [diff] [blame] | 487 | #else |
| 488 | value <<= 16; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 489 | #endif |
| 490 | return hash_64(value, FDB_HASH_BITS); |
| 491 | } |
| 492 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 493 | static u32 eth_vni_hash(const unsigned char *addr, __be32 vni) |
| 494 | { |
| 495 | /* use 1 byte of OUI and 3 bytes of NIC */ |
| 496 | u32 key = get_unaligned((u32 *)(addr + 2)); |
| 497 | |
| 498 | return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1); |
| 499 | } |
| 500 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 501 | static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni) |
| 502 | { |
| 503 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) |
| 504 | return eth_vni_hash(mac, vni); |
| 505 | else |
| 506 | return eth_hash(mac); |
| 507 | } |
| 508 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 509 | /* Hash chain to use given mac address */ |
| 510 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 511 | const u8 *mac, __be32 vni) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 512 | { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 513 | return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)]; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* Look up Ethernet address in forwarding table */ |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 517 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 518 | const u8 *mac, __be32 vni) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 519 | { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 520 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 521 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 522 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 523 | hlist_for_each_entry_rcu(f, head, hlist) { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 524 | if (ether_addr_equal(mac, f->eth_addr)) { |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 525 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 526 | if (vni == f->vni) |
| 527 | return f; |
| 528 | } else { |
| 529 | return f; |
| 530 | } |
| 531 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 537 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 538 | const u8 *mac, __be32 vni) |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 539 | { |
| 540 | struct vxlan_fdb *f; |
| 541 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 542 | f = __vxlan_find_mac(vxlan, mac, vni); |
Li RongQing | 016f3d1 | 2018-08-29 11:52:10 +0800 | [diff] [blame] | 543 | if (f && f->used != jiffies) |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 544 | f->used = jiffies; |
| 545 | |
| 546 | return f; |
| 547 | } |
| 548 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 549 | /* caller should hold vxlan->hash_lock */ |
| 550 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 551 | union vxlan_addr *ip, __be16 port, |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 552 | __be32 vni, __u32 ifindex) |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 553 | { |
| 554 | struct vxlan_rdst *rd; |
| 555 | |
| 556 | list_for_each_entry(rd, &f->remotes, list) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 557 | if (vxlan_addr_equal(&rd->remote_ip, ip) && |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 558 | rd->remote_port == port && |
| 559 | rd->remote_vni == vni && |
| 560 | rd->remote_ifindex == ifindex) |
| 561 | return rd; |
| 562 | } |
| 563 | |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Petr Machata | 1941f1d | 2018-10-17 08:53:24 +0000 | [diff] [blame] | 567 | int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni, |
| 568 | struct switchdev_notifier_vxlan_fdb_info *fdb_info) |
| 569 | { |
| 570 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 571 | u8 eth_addr[ETH_ALEN + 2] = { 0 }; |
| 572 | struct vxlan_rdst *rdst; |
| 573 | struct vxlan_fdb *f; |
| 574 | int rc = 0; |
| 575 | |
| 576 | if (is_multicast_ether_addr(mac) || |
| 577 | is_zero_ether_addr(mac)) |
| 578 | return -EINVAL; |
| 579 | |
| 580 | ether_addr_copy(eth_addr, mac); |
| 581 | |
| 582 | rcu_read_lock(); |
| 583 | |
| 584 | f = __vxlan_find_mac(vxlan, eth_addr, vni); |
| 585 | if (!f) { |
| 586 | rc = -ENOENT; |
| 587 | goto out; |
| 588 | } |
| 589 | |
| 590 | rdst = first_remote_rcu(f); |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 591 | vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info); |
Petr Machata | 1941f1d | 2018-10-17 08:53:24 +0000 | [diff] [blame] | 592 | |
| 593 | out: |
| 594 | rcu_read_unlock(); |
| 595 | return rc; |
| 596 | } |
| 597 | EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc); |
| 598 | |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 599 | static int vxlan_fdb_notify_one(struct notifier_block *nb, |
| 600 | const struct vxlan_dev *vxlan, |
| 601 | const struct vxlan_fdb *f, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 602 | const struct vxlan_rdst *rdst, |
| 603 | struct netlink_ext_ack *extack) |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 604 | { |
| 605 | struct switchdev_notifier_vxlan_fdb_info fdb_info; |
| 606 | int rc; |
| 607 | |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 608 | vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info); |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 609 | rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, |
| 610 | &fdb_info); |
| 611 | return notifier_to_errno(rc); |
| 612 | } |
| 613 | |
| 614 | int vxlan_fdb_replay(const struct net_device *dev, __be32 vni, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 615 | struct notifier_block *nb, |
| 616 | struct netlink_ext_ack *extack) |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 617 | { |
| 618 | struct vxlan_dev *vxlan; |
| 619 | struct vxlan_rdst *rdst; |
| 620 | struct vxlan_fdb *f; |
| 621 | unsigned int h; |
| 622 | int rc = 0; |
| 623 | |
| 624 | if (!netif_is_vxlan(dev)) |
| 625 | return -EINVAL; |
| 626 | vxlan = netdev_priv(dev); |
| 627 | |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 628 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 629 | spin_lock_bh(&vxlan->hash_lock[h]); |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 630 | hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) { |
| 631 | if (f->vni == vni) { |
| 632 | list_for_each_entry(rdst, &f->remotes, list) { |
| 633 | rc = vxlan_fdb_notify_one(nb, vxlan, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 634 | f, rdst, |
| 635 | extack); |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 636 | if (rc) |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 637 | goto unlock; |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 641 | spin_unlock_bh(&vxlan->hash_lock[h]); |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 642 | } |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 643 | return 0; |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 644 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 645 | unlock: |
| 646 | spin_unlock_bh(&vxlan->hash_lock[h]); |
Petr Machata | 4f89f5b | 2018-12-07 19:55:04 +0000 | [diff] [blame] | 647 | return rc; |
| 648 | } |
| 649 | EXPORT_SYMBOL_GPL(vxlan_fdb_replay); |
| 650 | |
Petr Machata | e5ff4b1 | 2018-12-07 19:55:06 +0000 | [diff] [blame] | 651 | void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni) |
| 652 | { |
| 653 | struct vxlan_dev *vxlan; |
| 654 | struct vxlan_rdst *rdst; |
| 655 | struct vxlan_fdb *f; |
| 656 | unsigned int h; |
| 657 | |
| 658 | if (!netif_is_vxlan(dev)) |
| 659 | return; |
| 660 | vxlan = netdev_priv(dev); |
| 661 | |
Petr Machata | e5ff4b1 | 2018-12-07 19:55:06 +0000 | [diff] [blame] | 662 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 663 | spin_lock_bh(&vxlan->hash_lock[h]); |
Petr Machata | e5ff4b1 | 2018-12-07 19:55:06 +0000 | [diff] [blame] | 664 | hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) |
| 665 | if (f->vni == vni) |
| 666 | list_for_each_entry(rdst, &f->remotes, list) |
| 667 | rdst->offloaded = false; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 668 | spin_unlock_bh(&vxlan->hash_lock[h]); |
Petr Machata | e5ff4b1 | 2018-12-07 19:55:06 +0000 | [diff] [blame] | 669 | } |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 670 | |
Petr Machata | e5ff4b1 | 2018-12-07 19:55:06 +0000 | [diff] [blame] | 671 | } |
| 672 | EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload); |
| 673 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 674 | /* Replace destination of unicast mac */ |
| 675 | static int vxlan_fdb_replace(struct vxlan_fdb *f, |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 676 | union vxlan_addr *ip, __be16 port, __be32 vni, |
Petr Machata | ccdfd4f | 2019-01-16 23:06:34 +0000 | [diff] [blame] | 677 | __u32 ifindex, struct vxlan_rdst *oldrd) |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 678 | { |
| 679 | struct vxlan_rdst *rd; |
| 680 | |
| 681 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 682 | if (rd) |
| 683 | return 0; |
| 684 | |
| 685 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); |
| 686 | if (!rd) |
| 687 | return 0; |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 688 | |
Petr Machata | ccdfd4f | 2019-01-16 23:06:34 +0000 | [diff] [blame] | 689 | *oldrd = *rd; |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 690 | dst_cache_reset(&rd->dst_cache); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 691 | rd->remote_ip = *ip; |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 692 | rd->remote_port = port; |
| 693 | rd->remote_vni = vni; |
| 694 | rd->remote_ifindex = ifindex; |
Petr Machata | 6ad0b5a | 2018-12-18 13:15:59 +0000 | [diff] [blame] | 695 | rd->offloaded = false; |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 696 | return 1; |
| 697 | } |
| 698 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 699 | /* Add/update destinations for multicast */ |
| 700 | static int vxlan_fdb_append(struct vxlan_fdb *f, |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 701 | union vxlan_addr *ip, __be16 port, __be32 vni, |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 702 | __u32 ifindex, struct vxlan_rdst **rdp) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 703 | { |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 704 | struct vxlan_rdst *rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 705 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 706 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 707 | if (rd) |
| 708 | return 0; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 709 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 710 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
| 711 | if (rd == NULL) |
| 712 | return -ENOBUFS; |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 713 | |
| 714 | if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) { |
| 715 | kfree(rd); |
| 716 | return -ENOBUFS; |
| 717 | } |
| 718 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 719 | rd->remote_ip = *ip; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 720 | rd->remote_port = port; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 721 | rd->offloaded = false; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 722 | rd->remote_vni = vni; |
| 723 | rd->remote_ifindex = ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 724 | |
| 725 | list_add_tail_rcu(&rd->list, &f->remotes); |
| 726 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 727 | *rdp = rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 728 | return 1; |
| 729 | } |
| 730 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 731 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, |
| 732 | unsigned int off, |
| 733 | struct vxlanhdr *vh, size_t hdrlen, |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 734 | __be32 vni_field, |
| 735 | struct gro_remcsum *grc, |
Tom Herbert | 0ace2ca | 2015-02-10 16:30:32 -0800 | [diff] [blame] | 736 | bool nopartial) |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 737 | { |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 738 | size_t start, offset; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 739 | |
| 740 | if (skb->remcsum_offload) |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 741 | return vh; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 742 | |
| 743 | if (!NAPI_GRO_CB(skb)->csum_valid) |
| 744 | return NULL; |
| 745 | |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 746 | start = vxlan_rco_start(vni_field); |
| 747 | offset = start + vxlan_rco_offset(vni_field); |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 748 | |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 749 | vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen, |
| 750 | start, offset, grc, nopartial); |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 751 | |
| 752 | skb->remcsum_offload = 1; |
| 753 | |
| 754 | return vh; |
| 755 | } |
| 756 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 757 | static struct sk_buff *vxlan_gro_receive(struct sock *sk, |
| 758 | struct list_head *head, |
| 759 | struct sk_buff *skb) |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 760 | { |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 761 | struct sk_buff *pp = NULL; |
| 762 | struct sk_buff *p; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 763 | struct vxlanhdr *vh, *vh2; |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 764 | unsigned int hlen, off_vx; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 765 | int flush = 1; |
Tom Herbert | 5602c48 | 2016-04-05 08:22:53 -0700 | [diff] [blame] | 766 | struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk); |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 767 | __be32 flags; |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 768 | struct gro_remcsum grc; |
| 769 | |
| 770 | skb_gro_remcsum_init(&grc); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 771 | |
| 772 | off_vx = skb_gro_offset(skb); |
| 773 | hlen = off_vx + sizeof(*vh); |
| 774 | vh = skb_gro_header_fast(skb, off_vx); |
| 775 | if (skb_gro_header_hard(skb, hlen)) { |
| 776 | vh = skb_gro_header_slow(skb, hlen, off_vx); |
| 777 | if (unlikely(!vh)) |
| 778 | goto out; |
| 779 | } |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 780 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 781 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); |
| 782 | |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 783 | flags = vh->vx_flags; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 784 | |
| 785 | if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { |
| 786 | vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 787 | vh->vx_vni, &grc, |
Tom Herbert | 0ace2ca | 2015-02-10 16:30:32 -0800 | [diff] [blame] | 788 | !!(vs->flags & |
| 789 | VXLAN_F_REMCSUM_NOPARTIAL)); |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 790 | |
| 791 | if (!vh) |
| 792 | goto out; |
| 793 | } |
| 794 | |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 795 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ |
| 796 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 797 | list_for_each_entry(p, head, list) { |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 798 | if (!NAPI_GRO_CB(p)->same_flow) |
| 799 | continue; |
| 800 | |
| 801 | vh2 = (struct vxlanhdr *)(p->data + off_vx); |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 802 | if (vh->vx_flags != vh2->vx_flags || |
| 803 | vh->vx_vni != vh2->vx_vni) { |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 804 | NAPI_GRO_CB(p)->same_flow = 0; |
| 805 | continue; |
| 806 | } |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Sabrina Dubroca | fcd91dd | 2016-10-20 15:58:02 +0200 | [diff] [blame] | 809 | pp = call_gro_receive(eth_gro_receive, head, skb); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 810 | flush = 0; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 811 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 812 | out: |
Sabrina Dubroca | 603d4cf | 2018-06-30 17:38:55 +0200 | [diff] [blame] | 813 | skb_gro_flush_final_remcsum(skb, pp, flush, &grc); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 814 | |
| 815 | return pp; |
| 816 | } |
| 817 | |
Tom Herbert | 5602c48 | 2016-04-05 08:22:53 -0700 | [diff] [blame] | 818 | static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 819 | { |
Jarno Rajahalme | 229740c | 2016-05-03 16:10:21 -0700 | [diff] [blame] | 820 | /* Sets 'skb->inner_mac_header' since we are always called with |
| 821 | * 'skb->encapsulation' set. |
| 822 | */ |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 823 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 826 | static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac, |
| 827 | __u16 state, __be32 src_vni, |
| 828 | __u16 ndm_flags) |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 829 | { |
| 830 | struct vxlan_fdb *f; |
| 831 | |
| 832 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 833 | if (!f) |
| 834 | return NULL; |
| 835 | f->state = state; |
| 836 | f->flags = ndm_flags; |
| 837 | f->updated = f->used = jiffies; |
| 838 | f->vni = src_vni; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 839 | f->nh = NULL; |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 840 | RCU_INIT_POINTER(f->vdev, vxlan); |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 841 | INIT_LIST_HEAD(&f->nh_list); |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 842 | INIT_LIST_HEAD(&f->remotes); |
| 843 | memcpy(f->eth_addr, mac, ETH_ALEN); |
| 844 | |
| 845 | return f; |
| 846 | } |
| 847 | |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 848 | static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac, |
| 849 | __be32 src_vni, struct vxlan_fdb *f) |
| 850 | { |
| 851 | ++vxlan->addrcnt; |
| 852 | hlist_add_head_rcu(&f->hlist, |
| 853 | vxlan_fdb_head(vxlan, mac, src_vni)); |
| 854 | } |
| 855 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 856 | static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
| 857 | u32 nhid, struct netlink_ext_ack *extack) |
| 858 | { |
| 859 | struct nexthop *old_nh = rtnl_dereference(fdb->nh); |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 860 | struct nexthop *nh; |
| 861 | int err = -EINVAL; |
| 862 | |
| 863 | if (old_nh && old_nh->id == nhid) |
| 864 | return 0; |
| 865 | |
| 866 | nh = nexthop_find_by_id(vxlan->net, nhid); |
| 867 | if (!nh) { |
| 868 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 869 | goto err_inval; |
| 870 | } |
| 871 | |
| 872 | if (nh) { |
| 873 | if (!nexthop_get(nh)) { |
| 874 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); |
| 875 | nh = NULL; |
| 876 | goto err_inval; |
| 877 | } |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 878 | if (!nexthop_is_fdb(nh)) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 879 | NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop"); |
| 880 | goto err_inval; |
| 881 | } |
| 882 | |
David Ahern | 50cb876 | 2020-06-09 17:27:28 -0600 | [diff] [blame] | 883 | if (!nexthop_is_multipath(nh)) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 884 | NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group"); |
| 885 | goto err_inval; |
| 886 | } |
| 887 | |
| 888 | /* check nexthop group family */ |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 889 | switch (vxlan->default_dst.remote_ip.sa.sa_family) { |
| 890 | case AF_INET: |
David Ahern | 50cb876 | 2020-06-09 17:27:28 -0600 | [diff] [blame] | 891 | if (!nexthop_has_v4(nh)) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 892 | err = -EAFNOSUPPORT; |
| 893 | NL_SET_ERR_MSG(extack, "Nexthop group family not supported"); |
| 894 | goto err_inval; |
| 895 | } |
| 896 | break; |
| 897 | case AF_INET6: |
David Ahern | 50cb876 | 2020-06-09 17:27:28 -0600 | [diff] [blame] | 898 | if (nexthop_has_v4(nh)) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 899 | err = -EAFNOSUPPORT; |
| 900 | NL_SET_ERR_MSG(extack, "Nexthop group family not supported"); |
| 901 | goto err_inval; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (old_nh) { |
| 907 | list_del_rcu(&fdb->nh_list); |
| 908 | nexthop_put(old_nh); |
| 909 | } |
| 910 | rcu_assign_pointer(fdb->nh, nh); |
| 911 | list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list); |
| 912 | return 1; |
| 913 | |
| 914 | err_inval: |
| 915 | if (nh) |
| 916 | nexthop_put(nh); |
| 917 | return err; |
| 918 | } |
| 919 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 920 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 921 | const u8 *mac, union vxlan_addr *ip, |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 922 | __u16 state, __be16 port, __be32 src_vni, |
Petr Machata | 45598c1 | 2018-11-21 08:02:36 +0000 | [diff] [blame] | 923 | __be32 vni, __u32 ifindex, __u16 ndm_flags, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 924 | u32 nhid, struct vxlan_fdb **fdb, |
| 925 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 926 | { |
| 927 | struct vxlan_rdst *rd = NULL; |
| 928 | struct vxlan_fdb *f; |
| 929 | int rc; |
| 930 | |
| 931 | if (vxlan->cfg.addrmax && |
| 932 | vxlan->addrcnt >= vxlan->cfg.addrmax) |
| 933 | return -ENOSPC; |
| 934 | |
| 935 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 936 | f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags); |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 937 | if (!f) |
| 938 | return -ENOMEM; |
| 939 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 940 | if (nhid) |
| 941 | rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack); |
| 942 | else |
| 943 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
| 944 | if (rc < 0) |
| 945 | goto errout; |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 946 | |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 947 | *fdb = f; |
| 948 | |
| 949 | return 0; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 950 | |
| 951 | errout: |
| 952 | kfree(f); |
| 953 | return rc; |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 956 | static void __vxlan_fdb_free(struct vxlan_fdb *f) |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 957 | { |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 958 | struct vxlan_rdst *rd, *nd; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 959 | struct nexthop *nh; |
| 960 | |
| 961 | nh = rcu_dereference_raw(f->nh); |
| 962 | if (nh) { |
| 963 | rcu_assign_pointer(f->nh, NULL); |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 964 | rcu_assign_pointer(f->vdev, NULL); |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 965 | nexthop_put(nh); |
| 966 | } |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 967 | |
| 968 | list_for_each_entry_safe(rd, nd, &f->remotes, list) { |
| 969 | dst_cache_destroy(&rd->dst_cache); |
| 970 | kfree(rd); |
| 971 | } |
| 972 | kfree(f); |
| 973 | } |
| 974 | |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 975 | static void vxlan_fdb_free(struct rcu_head *head) |
| 976 | { |
| 977 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); |
| 978 | |
| 979 | __vxlan_fdb_free(f); |
| 980 | } |
| 981 | |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 982 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, |
| 983 | bool do_notify, bool swdev_notify) |
| 984 | { |
| 985 | struct vxlan_rdst *rd; |
| 986 | |
| 987 | netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr); |
| 988 | |
| 989 | --vxlan->addrcnt; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 990 | if (do_notify) { |
| 991 | if (rcu_access_pointer(f->nh)) |
| 992 | vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 993 | swdev_notify, NULL); |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 994 | else |
| 995 | list_for_each_entry(rd, &f->remotes, list) |
| 996 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, |
| 997 | swdev_notify, NULL); |
| 998 | } |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 999 | |
| 1000 | hlist_del_rcu(&f->hlist); |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 1001 | list_del_rcu(&f->nh_list); |
Petr Machata | c2b200e | 2019-01-16 23:06:30 +0000 | [diff] [blame] | 1002 | call_rcu(&f->rcu, vxlan_fdb_free); |
| 1003 | } |
| 1004 | |
Petr Machata | fc4aa1c | 2019-02-07 12:18:02 +0000 | [diff] [blame] | 1005 | static void vxlan_dst_free(struct rcu_head *head) |
| 1006 | { |
| 1007 | struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); |
| 1008 | |
| 1009 | dst_cache_destroy(&rd->dst_cache); |
| 1010 | kfree(rd); |
| 1011 | } |
| 1012 | |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1013 | static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan, |
| 1014 | union vxlan_addr *ip, |
| 1015 | __u16 state, __u16 flags, |
| 1016 | __be16 port, __be32 vni, |
| 1017 | __u32 ifindex, __u16 ndm_flags, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1018 | struct vxlan_fdb *f, u32 nhid, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1019 | bool swdev_notify, |
| 1020 | struct netlink_ext_ack *extack) |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1021 | { |
| 1022 | __u16 fdb_flags = (ndm_flags & ~NTF_USE); |
| 1023 | struct vxlan_rdst *rd = NULL; |
Petr Machata | ccdfd4f | 2019-01-16 23:06:34 +0000 | [diff] [blame] | 1024 | struct vxlan_rdst oldrd; |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1025 | int notify = 0; |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1026 | int rc = 0; |
| 1027 | int err; |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1028 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1029 | if (nhid && !rcu_access_pointer(f->nh)) { |
| 1030 | NL_SET_ERR_MSG(extack, |
| 1031 | "Cannot replace an existing non nexthop fdb with a nexthop"); |
| 1032 | return -EOPNOTSUPP; |
| 1033 | } |
| 1034 | |
| 1035 | if (nhid && (flags & NLM_F_APPEND)) { |
| 1036 | NL_SET_ERR_MSG(extack, |
| 1037 | "Cannot append to a nexthop fdb"); |
| 1038 | return -EOPNOTSUPP; |
| 1039 | } |
| 1040 | |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1041 | /* Do not allow an externally learned entry to take over an entry added |
| 1042 | * by the user. |
| 1043 | */ |
| 1044 | if (!(fdb_flags & NTF_EXT_LEARNED) || |
| 1045 | !(f->flags & NTF_VXLAN_ADDED_BY_USER)) { |
| 1046 | if (f->state != state) { |
| 1047 | f->state = state; |
| 1048 | f->updated = jiffies; |
| 1049 | notify = 1; |
| 1050 | } |
| 1051 | if (f->flags != fdb_flags) { |
| 1052 | f->flags = fdb_flags; |
| 1053 | f->updated = jiffies; |
| 1054 | notify = 1; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if ((flags & NLM_F_REPLACE)) { |
| 1059 | /* Only change unicasts */ |
| 1060 | if (!(is_multicast_ether_addr(f->eth_addr) || |
| 1061 | is_zero_ether_addr(f->eth_addr))) { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1062 | if (nhid) { |
| 1063 | rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack); |
| 1064 | if (rc < 0) |
| 1065 | return rc; |
| 1066 | } else { |
| 1067 | rc = vxlan_fdb_replace(f, ip, port, vni, |
| 1068 | ifindex, &oldrd); |
| 1069 | } |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1070 | notify |= rc; |
| 1071 | } else { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1072 | NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries"); |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1073 | return -EOPNOTSUPP; |
| 1074 | } |
| 1075 | } |
| 1076 | if ((flags & NLM_F_APPEND) && |
| 1077 | (is_multicast_ether_addr(f->eth_addr) || |
| 1078 | is_zero_ether_addr(f->eth_addr))) { |
| 1079 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
| 1080 | |
| 1081 | if (rc < 0) |
| 1082 | return rc; |
| 1083 | notify |= rc; |
| 1084 | } |
| 1085 | |
| 1086 | if (ndm_flags & NTF_USE) |
| 1087 | f->used = jiffies; |
| 1088 | |
| 1089 | if (notify) { |
| 1090 | if (rd == NULL) |
| 1091 | rd = first_remote_rtnl(f); |
| 1092 | |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1093 | err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1094 | swdev_notify, extack); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1095 | if (err) |
| 1096 | goto err_notify; |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | return 0; |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1100 | |
| 1101 | err_notify: |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1102 | if (nhid) |
| 1103 | return err; |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1104 | if ((flags & NLM_F_REPLACE) && rc) |
| 1105 | *rd = oldrd; |
Petr Machata | fc4aa1c | 2019-02-07 12:18:02 +0000 | [diff] [blame] | 1106 | else if ((flags & NLM_F_APPEND) && rc) { |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1107 | list_del_rcu(&rd->list); |
Petr Machata | fc4aa1c | 2019-02-07 12:18:02 +0000 | [diff] [blame] | 1108 | call_rcu(&rd->rcu, vxlan_dst_free); |
| 1109 | } |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1110 | return err; |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static int vxlan_fdb_update_create(struct vxlan_dev *vxlan, |
| 1114 | const u8 *mac, union vxlan_addr *ip, |
| 1115 | __u16 state, __u16 flags, |
| 1116 | __be16 port, __be32 src_vni, __be32 vni, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1117 | __u32 ifindex, __u16 ndm_flags, u32 nhid, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1118 | bool swdev_notify, |
| 1119 | struct netlink_ext_ack *extack) |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1120 | { |
| 1121 | __u16 fdb_flags = (ndm_flags & ~NTF_USE); |
| 1122 | struct vxlan_fdb *f; |
| 1123 | int rc; |
| 1124 | |
| 1125 | /* Disallow replace to add a multicast entry */ |
| 1126 | if ((flags & NLM_F_REPLACE) && |
| 1127 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) |
| 1128 | return -EOPNOTSUPP; |
| 1129 | |
| 1130 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
| 1131 | rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1132 | vni, ifindex, fdb_flags, nhid, &f, extack); |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1133 | if (rc < 0) |
| 1134 | return rc; |
| 1135 | |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 1136 | vxlan_fdb_insert(vxlan, mac, src_vni, f); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1137 | rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1138 | swdev_notify, extack); |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1139 | if (rc) |
| 1140 | goto err_notify; |
| 1141 | |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1142 | return 0; |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 1143 | |
| 1144 | err_notify: |
| 1145 | vxlan_fdb_destroy(vxlan, f, false, false); |
| 1146 | return rc; |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 1149 | /* Add new entry to forwarding table -- assumes lock held */ |
| 1150 | static int vxlan_fdb_update(struct vxlan_dev *vxlan, |
| 1151 | const u8 *mac, union vxlan_addr *ip, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1152 | __u16 state, __u16 flags, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1153 | __be16 port, __be32 src_vni, __be32 vni, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1154 | __u32 ifindex, __u16 ndm_flags, u32 nhid, |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1155 | bool swdev_notify, |
| 1156 | struct netlink_ext_ack *extack) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1157 | { |
| 1158 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1159 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1160 | f = __vxlan_find_mac(vxlan, mac, src_vni); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1161 | if (f) { |
| 1162 | if (flags & NLM_F_EXCL) { |
| 1163 | netdev_dbg(vxlan->dev, |
| 1164 | "lost race to create %pM\n", mac); |
| 1165 | return -EEXIST; |
| 1166 | } |
Petr Machata | 0ec566a | 2018-11-21 08:02:37 +0000 | [diff] [blame] | 1167 | |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1168 | return vxlan_fdb_update_existing(vxlan, ip, state, flags, port, |
| 1169 | vni, ifindex, ndm_flags, f, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1170 | nhid, swdev_notify, extack); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1171 | } else { |
| 1172 | if (!(flags & NLM_F_CREATE)) |
| 1173 | return -ENOENT; |
| 1174 | |
Petr Machata | a76d1ca | 2019-01-16 23:06:32 +0000 | [diff] [blame] | 1175 | return vxlan_fdb_update_create(vxlan, mac, ip, state, flags, |
| 1176 | port, src_vni, vni, ifindex, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1177 | ndm_flags, nhid, swdev_notify, |
| 1178 | extack); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1179 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Lance Richardson | 35cf284 | 2017-05-29 13:25:57 -0400 | [diff] [blame] | 1182 | static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 1183 | struct vxlan_rdst *rd, bool swdev_notify) |
Lance Richardson | 35cf284 | 2017-05-29 13:25:57 -0400 | [diff] [blame] | 1184 | { |
| 1185 | list_del_rcu(&rd->list); |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1186 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL); |
Lance Richardson | 35cf284 | 2017-05-29 13:25:57 -0400 | [diff] [blame] | 1187 | call_rcu(&rd->rcu, vxlan_dst_free); |
| 1188 | } |
| 1189 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1190 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1191 | union vxlan_addr *ip, __be16 *port, __be32 *src_vni, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1192 | __be32 *vni, u32 *ifindex, u32 *nhid) |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1193 | { |
| 1194 | struct net *net = dev_net(vxlan->dev); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1195 | int err; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1196 | |
Roopa Prabhu | 72b4868 | 2020-05-28 22:12:35 -0700 | [diff] [blame] | 1197 | if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] || |
| 1198 | tb[NDA_PORT])) |
| 1199 | return -EINVAL; |
| 1200 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1201 | if (tb[NDA_DST]) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1202 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
| 1203 | if (err) |
| 1204 | return err; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1205 | } else { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1206 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 1207 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1208 | if (remote->sa.sa_family == AF_INET) { |
| 1209 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1210 | ip->sa.sa_family = AF_INET; |
| 1211 | #if IS_ENABLED(CONFIG_IPV6) |
| 1212 | } else { |
| 1213 | ip->sin6.sin6_addr = in6addr_any; |
| 1214 | ip->sa.sa_family = AF_INET6; |
| 1215 | #endif |
| 1216 | } |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | if (tb[NDA_PORT]) { |
| 1220 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
| 1221 | return -EINVAL; |
| 1222 | *port = nla_get_be16(tb[NDA_PORT]); |
| 1223 | } else { |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 1224 | *port = vxlan->cfg.dst_port; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | if (tb[NDA_VNI]) { |
| 1228 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) |
| 1229 | return -EINVAL; |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 1230 | *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1231 | } else { |
| 1232 | *vni = vxlan->default_dst.remote_vni; |
| 1233 | } |
| 1234 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1235 | if (tb[NDA_SRC_VNI]) { |
| 1236 | if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32)) |
| 1237 | return -EINVAL; |
| 1238 | *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI])); |
| 1239 | } else { |
| 1240 | *src_vni = vxlan->default_dst.remote_vni; |
| 1241 | } |
| 1242 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1243 | if (tb[NDA_IFINDEX]) { |
| 1244 | struct net_device *tdev; |
| 1245 | |
| 1246 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) |
| 1247 | return -EINVAL; |
| 1248 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
Ying Xue | 7376394 | 2014-01-15 10:23:41 +0800 | [diff] [blame] | 1249 | tdev = __dev_get_by_index(net, *ifindex); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1250 | if (!tdev) |
| 1251 | return -EADDRNOTAVAIL; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1252 | } else { |
| 1253 | *ifindex = 0; |
| 1254 | } |
| 1255 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1256 | if (tb[NDA_NH_ID]) |
| 1257 | *nhid = nla_get_u32(tb[NDA_NH_ID]); |
| 1258 | else |
| 1259 | *nhid = 0; |
| 1260 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1264 | /* Add static entry (via netlink) */ |
| 1265 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 1266 | struct net_device *dev, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 1267 | const unsigned char *addr, u16 vid, u16 flags, |
| 1268 | struct netlink_ext_ack *extack) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1269 | { |
| 1270 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1271 | /* struct net *net = dev_net(vxlan->dev); */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1272 | union vxlan_addr ip; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 1273 | __be16 port; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1274 | __be32 src_vni, vni; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1275 | u32 ifindex, nhid; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1276 | u32 hash_index; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1277 | int err; |
| 1278 | |
| 1279 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
| 1280 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
| 1281 | ndm->ndm_state); |
| 1282 | return -EINVAL; |
| 1283 | } |
| 1284 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1285 | if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID])) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1286 | return -EINVAL; |
| 1287 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1288 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex, |
| 1289 | &nhid); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 1290 | if (err) |
| 1291 | return err; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1292 | |
Mike Rapoport | 5933a7b | 2014-04-01 09:23:01 +0300 | [diff] [blame] | 1293 | if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) |
| 1294 | return -EAFNOSUPPORT; |
| 1295 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1296 | hash_index = fdb_head_index(vxlan, addr, src_vni); |
| 1297 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 1298 | err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags, |
Petr Machata | 45598c1 | 2018-11-21 08:02:36 +0000 | [diff] [blame] | 1299 | port, src_vni, vni, ifindex, |
| 1300 | ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1301 | nhid, true, extack); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1302 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1303 | |
| 1304 | return err; |
| 1305 | } |
| 1306 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1307 | static int __vxlan_fdb_delete(struct vxlan_dev *vxlan, |
| 1308 | const unsigned char *addr, union vxlan_addr ip, |
Xin Long | fc39c38 | 2017-11-26 21:19:05 +0800 | [diff] [blame] | 1309 | __be16 port, __be32 src_vni, __be32 vni, |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 1310 | u32 ifindex, bool swdev_notify) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1311 | { |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1312 | struct vxlan_rdst *rd = NULL; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1313 | struct vxlan_fdb *f; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1314 | int err = -ENOENT; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1315 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1316 | f = vxlan_find_mac(vxlan, addr, src_vni); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1317 | if (!f) |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1318 | return err; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1319 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1320 | if (!vxlan_addr_any(&ip)) { |
| 1321 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1322 | if (!rd) |
| 1323 | goto out; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1324 | } |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1325 | |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1326 | /* remove a destination if it's not the only one on the list, |
| 1327 | * otherwise destroy the fdb entry |
| 1328 | */ |
| 1329 | if (rd && !list_is_singular(&f->remotes)) { |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 1330 | vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1331 | goto out; |
| 1332 | } |
| 1333 | |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 1334 | vxlan_fdb_destroy(vxlan, f, true, swdev_notify); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 1335 | |
| 1336 | out: |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | /* Delete entry (via netlink) */ |
| 1341 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 1342 | struct net_device *dev, |
| 1343 | const unsigned char *addr, u16 vid) |
| 1344 | { |
| 1345 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1346 | union vxlan_addr ip; |
| 1347 | __be32 src_vni, vni; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1348 | u32 ifindex, nhid; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1349 | u32 hash_index; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1350 | __be16 port; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1351 | int err; |
| 1352 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1353 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex, |
| 1354 | &nhid); |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1355 | if (err) |
| 1356 | return err; |
| 1357 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1358 | hash_index = fdb_head_index(vxlan, addr, src_vni); |
| 1359 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 1360 | err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex, |
| 1361 | true); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1362 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1363 | |
| 1364 | return err; |
| 1365 | } |
| 1366 | |
| 1367 | /* Dump forwarding table */ |
| 1368 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 1369 | struct net_device *dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 1370 | struct net_device *filter_dev, int *idx) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1371 | { |
| 1372 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1373 | unsigned int h; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 1374 | int err = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1375 | |
| 1376 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1377 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1378 | |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1379 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1380 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1381 | struct vxlan_rdst *rd; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1382 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1383 | if (rcu_access_pointer(f->nh)) { |
Roopa Prabhu | b18e983 | 2020-06-24 14:02:36 -0700 | [diff] [blame] | 1384 | if (*idx < cb->args[2]) |
| 1385 | goto skip_nh; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1386 | err = vxlan_fdb_info(skb, vxlan, f, |
| 1387 | NETLINK_CB(cb->skb).portid, |
| 1388 | cb->nlh->nlmsg_seq, |
| 1389 | RTM_NEWNEIGH, |
| 1390 | NLM_F_MULTI, NULL); |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1391 | if (err < 0) { |
| 1392 | rcu_read_unlock(); |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1393 | goto out; |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1394 | } |
Roopa Prabhu | b18e983 | 2020-06-24 14:02:36 -0700 | [diff] [blame] | 1395 | skip_nh: |
| 1396 | *idx += 1; |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1397 | continue; |
| 1398 | } |
| 1399 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1400 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 1401 | if (*idx < cb->args[2]) |
Atzm Watanabe | 07a51cd | 2015-08-10 23:39:09 +0900 | [diff] [blame] | 1402 | goto skip; |
| 1403 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1404 | err = vxlan_fdb_info(skb, vxlan, f, |
| 1405 | NETLINK_CB(cb->skb).portid, |
| 1406 | cb->nlh->nlmsg_seq, |
| 1407 | RTM_NEWNEIGH, |
| 1408 | NLM_F_MULTI, rd); |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1409 | if (err < 0) { |
| 1410 | rcu_read_unlock(); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1411 | goto out; |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1412 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1413 | skip: |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 1414 | *idx += 1; |
Atzm Watanabe | 07a51cd | 2015-08-10 23:39:09 +0900 | [diff] [blame] | 1415 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1416 | } |
Ido Schimmel | b514191 | 2020-07-29 11:34:36 +0300 | [diff] [blame] | 1417 | rcu_read_unlock(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1418 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1419 | out: |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 1420 | return err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Roopa Prabhu | 474c3c8 | 2018-12-15 22:35:10 -0800 | [diff] [blame] | 1423 | static int vxlan_fdb_get(struct sk_buff *skb, |
| 1424 | struct nlattr *tb[], |
| 1425 | struct net_device *dev, |
| 1426 | const unsigned char *addr, |
| 1427 | u16 vid, u32 portid, u32 seq, |
| 1428 | struct netlink_ext_ack *extack) |
| 1429 | { |
| 1430 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1431 | struct vxlan_fdb *f; |
| 1432 | __be32 vni; |
| 1433 | int err; |
| 1434 | |
| 1435 | if (tb[NDA_VNI]) |
| 1436 | vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); |
| 1437 | else |
| 1438 | vni = vxlan->default_dst.remote_vni; |
| 1439 | |
| 1440 | rcu_read_lock(); |
| 1441 | |
| 1442 | f = __vxlan_find_mac(vxlan, addr, vni); |
| 1443 | if (!f) { |
| 1444 | NL_SET_ERR_MSG(extack, "Fdb entry not found"); |
| 1445 | err = -ENOENT; |
| 1446 | goto errout; |
| 1447 | } |
| 1448 | |
| 1449 | err = vxlan_fdb_info(skb, vxlan, f, portid, seq, |
| 1450 | RTM_NEWNEIGH, 0, first_remote_rcu(f)); |
| 1451 | errout: |
| 1452 | rcu_read_unlock(); |
| 1453 | return err; |
| 1454 | } |
| 1455 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1456 | /* Watch incoming packets to learn mapping between Ethernet address |
| 1457 | * and Tunnel endpoint. |
Simon Horman | c4b4951 | 2015-04-02 11:17:58 +0900 | [diff] [blame] | 1458 | * Return true if packet is bogus and should be dropped. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1459 | */ |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1460 | static bool vxlan_snoop(struct net_device *dev, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1461 | union vxlan_addr *src_ip, const u8 *src_mac, |
Matthias Schiffer | 87613de | 2017-06-19 10:03:59 +0200 | [diff] [blame] | 1462 | u32 src_ifindex, __be32 vni) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1463 | { |
| 1464 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1465 | struct vxlan_fdb *f; |
Matthias Schiffer | 87613de | 2017-06-19 10:03:59 +0200 | [diff] [blame] | 1466 | u32 ifindex = 0; |
| 1467 | |
| 1468 | #if IS_ENABLED(CONFIG_IPV6) |
| 1469 | if (src_ip->sa.sa_family == AF_INET6 && |
| 1470 | (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)) |
| 1471 | ifindex = src_ifindex; |
| 1472 | #endif |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1473 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1474 | f = vxlan_find_mac(vxlan, src_mac, vni); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1475 | if (likely(f)) { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 1476 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1477 | |
Matthias Schiffer | 87613de | 2017-06-19 10:03:59 +0200 | [diff] [blame] | 1478 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) && |
| 1479 | rdst->remote_ifindex == ifindex)) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1480 | return false; |
| 1481 | |
| 1482 | /* Don't migrate static entries, drop packets */ |
Roopa Prabhu | e0090a9 | 2017-06-11 16:32:50 -0700 | [diff] [blame] | 1483 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1484 | return true; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1485 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1486 | /* Don't override an fdb with nexthop with a learnt entry */ |
| 1487 | if (rcu_access_pointer(f->nh)) |
| 1488 | return true; |
| 1489 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1490 | if (net_ratelimit()) |
| 1491 | netdev_info(dev, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1492 | "%pM migrated from %pIS to %pIS\n", |
Rasmus Villemoes | a4870f7 | 2015-02-07 03:17:31 +0100 | [diff] [blame] | 1493 | src_mac, &rdst->remote_ip.sa, &src_ip->sa); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1494 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1495 | rdst->remote_ip = *src_ip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1496 | f->updated = jiffies; |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 1497 | vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1498 | } else { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1499 | u32 hash_index = fdb_head_index(vxlan, src_mac, vni); |
| 1500 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1501 | /* learned new entry */ |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1502 | spin_lock(&vxlan->hash_lock[hash_index]); |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 1503 | |
| 1504 | /* close off race between vxlan_flush and incoming packets */ |
| 1505 | if (netif_running(dev)) |
Roopa Prabhu | 25e20e7 | 2018-07-04 16:46:30 -0700 | [diff] [blame] | 1506 | vxlan_fdb_update(vxlan, src_mac, src_ip, |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 1507 | NUD_REACHABLE, |
| 1508 | NLM_F_EXCL|NLM_F_CREATE, |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 1509 | vxlan->cfg.dst_port, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1510 | vni, |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 1511 | vxlan->default_dst.remote_vni, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1512 | ifindex, NTF_SELF, 0, true, NULL); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 1513 | spin_unlock(&vxlan->hash_lock[hash_index]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1514 | } |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1515 | |
| 1516 | return false; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1519 | /* See if multicast group is already in use by other ID */ |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1520 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1521 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1522 | struct vxlan_dev *vxlan; |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1523 | struct vxlan_sock *sock4; |
Arnd Bergmann | 4053ab1 | 2016-11-07 22:09:07 +0100 | [diff] [blame] | 1524 | #if IS_ENABLED(CONFIG_IPV6) |
| 1525 | struct vxlan_sock *sock6; |
| 1526 | #endif |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1527 | unsigned short family = dev->default_dst.remote_ip.sa.sa_family; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1528 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1529 | sock4 = rtnl_dereference(dev->vn4_sock); |
| 1530 | |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1531 | /* The vxlan_sock is only used by dev, leaving group has |
| 1532 | * no effect on other vxlan devices. |
| 1533 | */ |
Reshetova, Elena | 66af846 | 2017-07-04 15:52:59 +0300 | [diff] [blame] | 1534 | if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1) |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1535 | return false; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1536 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1537 | sock6 = rtnl_dereference(dev->vn6_sock); |
Reshetova, Elena | 66af846 | 2017-07-04 15:52:59 +0300 | [diff] [blame] | 1538 | if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1) |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1539 | return false; |
| 1540 | #endif |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1541 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1542 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1543 | if (!netif_running(vxlan->dev) || vxlan == dev) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1544 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1545 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1546 | if (family == AF_INET && |
| 1547 | rtnl_dereference(vxlan->vn4_sock) != sock4) |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1548 | continue; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1549 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1550 | if (family == AF_INET6 && |
| 1551 | rtnl_dereference(vxlan->vn6_sock) != sock6) |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1552 | continue; |
| 1553 | #endif |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1554 | |
| 1555 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, |
| 1556 | &dev->default_dst.remote_ip)) |
| 1557 | continue; |
| 1558 | |
| 1559 | if (vxlan->default_dst.remote_ifindex != |
| 1560 | dev->default_dst.remote_ifindex) |
| 1561 | continue; |
| 1562 | |
| 1563 | return true; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1564 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1565 | |
| 1566 | return false; |
| 1567 | } |
| 1568 | |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1569 | static bool __vxlan_sock_release_prep(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1570 | { |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1571 | struct vxlan_net *vn; |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1572 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1573 | if (!vs) |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1574 | return false; |
Reshetova, Elena | 66af846 | 2017-07-04 15:52:59 +0300 | [diff] [blame] | 1575 | if (!refcount_dec_and_test(&vs->refcnt)) |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1576 | return false; |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1577 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1578 | vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1579 | spin_lock(&vn->sock_lock); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1580 | hlist_del_rcu(&vs->hlist); |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 1581 | udp_tunnel_notify_del_rx_port(vs->sock, |
Alexander Duyck | b9adcd69 | 2016-06-16 12:23:19 -0700 | [diff] [blame] | 1582 | (vs->flags & VXLAN_F_GPE) ? |
| 1583 | UDP_TUNNEL_TYPE_VXLAN_GPE : |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 1584 | UDP_TUNNEL_TYPE_VXLAN); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1585 | spin_unlock(&vn->sock_lock); |
| 1586 | |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1587 | return true; |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1590 | static void vxlan_sock_release(struct vxlan_dev *vxlan) |
| 1591 | { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1592 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1593 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1594 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
| 1595 | |
Mark Bloch | 57d8818 | 2017-06-07 14:36:58 +0300 | [diff] [blame] | 1596 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1597 | #endif |
| 1598 | |
Mark Bloch | 57d8818 | 2017-06-07 14:36:58 +0300 | [diff] [blame] | 1599 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1600 | synchronize_net(); |
| 1601 | |
Mark Bloch | a53cb29 | 2017-06-02 03:24:08 +0300 | [diff] [blame] | 1602 | vxlan_vs_del_dev(vxlan); |
| 1603 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1604 | if (__vxlan_sock_release_prep(sock4)) { |
| 1605 | udp_tunnel_sock_release(sock4->sock); |
| 1606 | kfree(sock4); |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1610 | if (__vxlan_sock_release_prep(sock6)) { |
| 1611 | udp_tunnel_sock_release(sock6->sock); |
| 1612 | kfree(sock6); |
Hannes Frederic Sowa | 544a773 | 2016-04-09 12:46:23 +0200 | [diff] [blame] | 1613 | } |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1614 | #endif |
| 1615 | } |
| 1616 | |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1617 | /* Update multicast group membership when first VNI on |
Simon Horman | c4b4951 | 2015-04-02 11:17:58 +0900 | [diff] [blame] | 1618 | * multicast address is brought up |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1619 | */ |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1620 | static int vxlan_igmp_join(struct vxlan_dev *vxlan) |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1621 | { |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1622 | struct sock *sk; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1623 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
| 1624 | int ifindex = vxlan->default_dst.remote_ifindex; |
Marcelo Ricardo Leitner | 149d754 | 2015-03-20 10:26:21 -0300 | [diff] [blame] | 1625 | int ret = -EINVAL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1626 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1627 | if (ip->sa.sa_family == AF_INET) { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1628 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1629 | struct ip_mreqn mreq = { |
| 1630 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, |
| 1631 | .imr_ifindex = ifindex, |
| 1632 | }; |
| 1633 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1634 | sk = sock4->sock->sk; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1635 | lock_sock(sk); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1636 | ret = ip_mc_join_group(sk, &mreq); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1637 | release_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1638 | #if IS_ENABLED(CONFIG_IPV6) |
| 1639 | } else { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1640 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
| 1641 | |
| 1642 | sk = sock6->sock->sk; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1643 | lock_sock(sk); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1644 | ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex, |
| 1645 | &ip->sin6.sin6_addr); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1646 | release_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1647 | #endif |
| 1648 | } |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1649 | |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1650 | return ret; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1654 | static int vxlan_igmp_leave(struct vxlan_dev *vxlan) |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1655 | { |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1656 | struct sock *sk; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1657 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
| 1658 | int ifindex = vxlan->default_dst.remote_ifindex; |
Marcelo Ricardo Leitner | 149d754 | 2015-03-20 10:26:21 -0300 | [diff] [blame] | 1659 | int ret = -EINVAL; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1660 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1661 | if (ip->sa.sa_family == AF_INET) { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1662 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1663 | struct ip_mreqn mreq = { |
| 1664 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, |
| 1665 | .imr_ifindex = ifindex, |
| 1666 | }; |
| 1667 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1668 | sk = sock4->sock->sk; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1669 | lock_sock(sk); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1670 | ret = ip_mc_leave_group(sk, &mreq); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1671 | release_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1672 | #if IS_ENABLED(CONFIG_IPV6) |
| 1673 | } else { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 1674 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
| 1675 | |
| 1676 | sk = sock6->sock->sk; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1677 | lock_sock(sk); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1678 | ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, |
| 1679 | &ip->sin6.sin6_addr); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 1680 | release_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1681 | #endif |
| 1682 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1683 | |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 1684 | return ret; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1687 | static bool vxlan_remcsum(struct vxlanhdr *unparsed, |
| 1688 | struct sk_buff *skb, u32 vxflags) |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 1689 | { |
Jiri Benc | 7d34fa7 | 2016-03-21 17:50:05 +0100 | [diff] [blame] | 1690 | size_t start, offset; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 1691 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1692 | if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload) |
| 1693 | goto out; |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 1694 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1695 | start = vxlan_rco_start(unparsed->vx_vni); |
| 1696 | offset = start + vxlan_rco_offset(unparsed->vx_vni); |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 1697 | |
Jiri Benc | 7d34fa7 | 2016-03-21 17:50:05 +0100 | [diff] [blame] | 1698 | if (!pskb_may_pull(skb, offset + sizeof(u16))) |
Jiri Benc | be5cfea | 2016-02-16 21:58:59 +0100 | [diff] [blame] | 1699 | return false; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 1700 | |
Jiri Benc | be5cfea | 2016-02-16 21:58:59 +0100 | [diff] [blame] | 1701 | skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset, |
| 1702 | !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL)); |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1703 | out: |
| 1704 | unparsed->vx_flags &= ~VXLAN_HF_RCO; |
| 1705 | unparsed->vx_vni &= VXLAN_VNI_MASK; |
Jiri Benc | be5cfea | 2016-02-16 21:58:59 +0100 | [diff] [blame] | 1706 | return true; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1709 | static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed, |
Jiri Benc | 64f87d3 | 2016-02-23 18:02:55 +0100 | [diff] [blame] | 1710 | struct sk_buff *skb, u32 vxflags, |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1711 | struct vxlan_metadata *md) |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1712 | { |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1713 | struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed; |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1714 | struct metadata_dst *tun_dst; |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1715 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1716 | if (!(unparsed->vx_flags & VXLAN_HF_GBP)) |
| 1717 | goto out; |
| 1718 | |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1719 | md->gbp = ntohs(gbp->policy_id); |
| 1720 | |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1721 | tun_dst = (struct metadata_dst *)skb_dst(skb); |
David S. Miller | 810813c | 2016-03-08 12:34:12 -0500 | [diff] [blame] | 1722 | if (tun_dst) { |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1723 | tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT; |
David S. Miller | 810813c | 2016-03-08 12:34:12 -0500 | [diff] [blame] | 1724 | tun_dst->u.tun_info.options_len = sizeof(*md); |
| 1725 | } |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1726 | if (gbp->dont_learn) |
| 1727 | md->gbp |= VXLAN_GBP_DONT_LEARN; |
| 1728 | |
| 1729 | if (gbp->policy_applied) |
| 1730 | md->gbp |= VXLAN_GBP_POLICY_APPLIED; |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1731 | |
Jiri Benc | 64f87d3 | 2016-02-23 18:02:55 +0100 | [diff] [blame] | 1732 | /* In flow-based mode, GBP is carried in dst_metadata */ |
| 1733 | if (!(vxflags & VXLAN_F_COLLECT_METADATA)) |
| 1734 | skb->mark = md->gbp; |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1735 | out: |
| 1736 | unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS; |
Jiri Benc | 3288af0 | 2016-02-16 21:59:00 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1739 | static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed, |
Jiri Benc | 61618ee | 2016-04-11 17:06:08 +0200 | [diff] [blame] | 1740 | __be16 *protocol, |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1741 | struct sk_buff *skb, u32 vxflags) |
| 1742 | { |
| 1743 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed; |
| 1744 | |
| 1745 | /* Need to have Next Protocol set for interfaces in GPE mode. */ |
| 1746 | if (!gpe->np_applied) |
| 1747 | return false; |
| 1748 | /* "The initial version is 0. If a receiver does not support the |
| 1749 | * version indicated it MUST drop the packet. |
| 1750 | */ |
| 1751 | if (gpe->version != 0) |
| 1752 | return false; |
| 1753 | /* "When the O bit is set to 1, the packet is an OAM packet and OAM |
| 1754 | * processing MUST occur." However, we don't implement OAM |
| 1755 | * processing, thus drop the packet. |
| 1756 | */ |
| 1757 | if (gpe->oam_flag) |
| 1758 | return false; |
| 1759 | |
Jiri Benc | fa20e0e | 2017-08-28 21:43:22 +0200 | [diff] [blame] | 1760 | *protocol = tun_p_to_eth_p(gpe->next_protocol); |
| 1761 | if (!*protocol) |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1762 | return false; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1763 | |
| 1764 | unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS; |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
Jiri Benc | 1ab016e | 2016-02-23 18:02:56 +0100 | [diff] [blame] | 1768 | static bool vxlan_set_mac(struct vxlan_dev *vxlan, |
| 1769 | struct vxlan_sock *vs, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1770 | struct sk_buff *skb, __be32 vni) |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1771 | { |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1772 | union vxlan_addr saddr; |
Matthias Schiffer | 87613de | 2017-06-19 10:03:59 +0200 | [diff] [blame] | 1773 | u32 ifindex = skb->dev->ifindex; |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1774 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1775 | skb_reset_mac_header(skb); |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1776 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
| 1777 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 1778 | |
| 1779 | /* Ignore packet loops (and multicast echo) */ |
| 1780 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) |
Jiri Benc | 1ab016e | 2016-02-23 18:02:56 +0100 | [diff] [blame] | 1781 | return false; |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1782 | |
Jiri Benc | 760c680 | 2016-02-23 18:02:57 +0100 | [diff] [blame] | 1783 | /* Get address from the outer IP header */ |
Jiri Benc | ce212d0 | 2015-12-07 16:29:08 +0100 | [diff] [blame] | 1784 | if (vxlan_get_sk_family(vs) == AF_INET) { |
Jiri Benc | 1ab016e | 2016-02-23 18:02:56 +0100 | [diff] [blame] | 1785 | saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1786 | saddr.sa.sa_family = AF_INET; |
| 1787 | #if IS_ENABLED(CONFIG_IPV6) |
| 1788 | } else { |
Jiri Benc | 1ab016e | 2016-02-23 18:02:56 +0100 | [diff] [blame] | 1789 | saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr; |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 1790 | saddr.sa.sa_family = AF_INET6; |
| 1791 | #endif |
| 1792 | } |
| 1793 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 1794 | if ((vxlan->cfg.flags & VXLAN_F_LEARN) && |
Matthias Schiffer | 87613de | 2017-06-19 10:03:59 +0200 | [diff] [blame] | 1795 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni)) |
Jiri Benc | 1ab016e | 2016-02-23 18:02:56 +0100 | [diff] [blame] | 1796 | return false; |
| 1797 | |
| 1798 | return true; |
| 1799 | } |
| 1800 | |
Jiri Benc | 760c680 | 2016-02-23 18:02:57 +0100 | [diff] [blame] | 1801 | static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph, |
| 1802 | struct sk_buff *skb) |
| 1803 | { |
| 1804 | int err = 0; |
| 1805 | |
| 1806 | if (vxlan_get_sk_family(vs) == AF_INET) |
| 1807 | err = IP_ECN_decapsulate(oiph, skb); |
| 1808 | #if IS_ENABLED(CONFIG_IPV6) |
| 1809 | else |
| 1810 | err = IP6_ECN_decapsulate(oiph, skb); |
| 1811 | #endif |
| 1812 | |
| 1813 | if (unlikely(err) && log_ecn_error) { |
| 1814 | if (vxlan_get_sk_family(vs) == AF_INET) |
| 1815 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 1816 | &((struct iphdr *)oiph)->saddr, |
| 1817 | ((struct iphdr *)oiph)->tos); |
| 1818 | else |
| 1819 | net_info_ratelimited("non-ECT from %pI6\n", |
| 1820 | &((struct ipv6hdr *)oiph)->saddr); |
| 1821 | } |
| 1822 | return err <= 1; |
| 1823 | } |
| 1824 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1825 | /* Callback from net/ipv4/udp.c to receive packets */ |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1826 | static int vxlan_rcv(struct sock *sk, struct sk_buff *skb) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1827 | { |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1828 | struct pcpu_sw_netstats *stats; |
Jiri Benc | c9e78ef | 2016-02-18 11:22:51 +0100 | [diff] [blame] | 1829 | struct vxlan_dev *vxlan; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1830 | struct vxlan_sock *vs; |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1831 | struct vxlanhdr unparsed; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 1832 | struct vxlan_metadata _md; |
| 1833 | struct vxlan_metadata *md = &_md; |
Jiri Benc | 61618ee | 2016-04-11 17:06:08 +0200 | [diff] [blame] | 1834 | __be16 protocol = htons(ETH_P_TEB); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1835 | bool raw_proto = false; |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1836 | void *oiph; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1837 | __be32 vni = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1838 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1839 | /* Need UDP and VXLAN header to be present */ |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 1840 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 1841 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1842 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1843 | unparsed = *vxlan_hdr(skb); |
Jiri Benc | 288b01c | 2016-02-16 21:59:02 +0100 | [diff] [blame] | 1844 | /* VNI flag always required to be set */ |
| 1845 | if (!(unparsed.vx_flags & VXLAN_HF_VNI)) { |
| 1846 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
| 1847 | ntohl(vxlan_hdr(skb)->vx_flags), |
| 1848 | ntohl(vxlan_hdr(skb)->vx_vni)); |
| 1849 | /* Return non vxlan pkt */ |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 1850 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1851 | } |
Jiri Benc | 288b01c | 2016-02-16 21:59:02 +0100 | [diff] [blame] | 1852 | unparsed.vx_flags &= ~VXLAN_HF_VNI; |
| 1853 | unparsed.vx_vni &= ~VXLAN_VNI_MASK; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1854 | |
Pravin B Shelar | 559835ea | 2013-09-24 10:25:40 -0700 | [diff] [blame] | 1855 | vs = rcu_dereference_sk_user_data(sk); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1856 | if (!vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1857 | goto drop; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1858 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1859 | vni = vxlan_vni(vxlan_hdr(skb)->vx_vni); |
| 1860 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 1861 | vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni); |
Jiri Benc | c9e78ef | 2016-02-18 11:22:51 +0100 | [diff] [blame] | 1862 | if (!vxlan) |
| 1863 | goto drop; |
| 1864 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1865 | /* For backwards compatibility, only allow reserved fields to be |
| 1866 | * used by VXLAN extensions if explicitly requested. |
| 1867 | */ |
| 1868 | if (vs->flags & VXLAN_F_GPE) { |
| 1869 | if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags)) |
| 1870 | goto drop; |
| 1871 | raw_proto = true; |
| 1872 | } |
| 1873 | |
| 1874 | if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto, |
| 1875 | !net_eq(vxlan->net, dev_net(vxlan->dev)))) |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 1876 | goto drop; |
Jiri Benc | c9e78ef | 2016-02-18 11:22:51 +0100 | [diff] [blame] | 1877 | |
Fabian Frederick | 2ae2904 | 2020-09-25 15:16:02 +0200 | [diff] [blame^] | 1878 | if (vs->flags & VXLAN_F_REMCSUM_RX) |
| 1879 | if (!vxlan_remcsum(&unparsed, skb, vs->flags)) |
| 1880 | goto drop; |
| 1881 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 1882 | if (vxlan_collect_metadata(vs)) { |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1883 | struct metadata_dst *tun_dst; |
Jiri Benc | 07dabf2 | 2016-02-18 19:19:29 +0100 | [diff] [blame] | 1884 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 1885 | tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY, |
Amir Vadai | d817f43 | 2016-09-08 16:23:45 +0300 | [diff] [blame] | 1886 | key32_to_tunnel_id(vni), sizeof(*md)); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 1887 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 1888 | if (!tun_dst) |
| 1889 | goto drop; |
| 1890 | |
Geert Uytterhoeven | 0f1b735 | 2015-09-04 12:49:32 +0200 | [diff] [blame] | 1891 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1892 | |
| 1893 | skb_dst_set(skb, (struct dst_entry *)tun_dst); |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 1894 | } else { |
| 1895 | memset(md, 0, sizeof(*md)); |
| 1896 | } |
| 1897 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1898 | if (vs->flags & VXLAN_F_GBP) |
Jiri Benc | 10a5af2 | 2016-02-23 18:02:59 +0100 | [diff] [blame] | 1899 | vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1900 | /* Note that GBP and GPE can never be active together. This is |
| 1901 | * ensured in vxlan_dev_configure. |
| 1902 | */ |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 1903 | |
Jiri Benc | f14eceb | 2016-02-16 21:59:01 +0100 | [diff] [blame] | 1904 | if (unparsed.vx_flags || unparsed.vx_vni) { |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1905 | /* If there are any unprocessed flags remaining treat |
| 1906 | * this as a malformed packet. This behavior diverges from |
| 1907 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved |
| 1908 | * in reserved fields are to be ignored. The approach here |
Simon Horman | c4b4951 | 2015-04-02 11:17:58 +0900 | [diff] [blame] | 1909 | * maintains compatibility with previous stack code, and also |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1910 | * is more robust and provides a little more security in |
| 1911 | * adding extensions to VXLAN. |
| 1912 | */ |
Jiri Benc | 288b01c | 2016-02-16 21:59:02 +0100 | [diff] [blame] | 1913 | goto drop; |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1916 | if (!raw_proto) { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1917 | if (!vxlan_set_mac(vxlan, vs, skb, vni)) |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1918 | goto drop; |
| 1919 | } else { |
Jiri Benc | 8be0cfa | 2016-05-13 10:48:42 +0200 | [diff] [blame] | 1920 | skb_reset_mac_header(skb); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 1921 | skb->dev = vxlan->dev; |
| 1922 | skb->pkt_type = PACKET_HOST; |
| 1923 | } |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1924 | |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1925 | oiph = skb_network_header(skb); |
| 1926 | skb_reset_network_header(skb); |
| 1927 | |
| 1928 | if (!vxlan_ecn_decapsulate(vs, oiph, skb)) { |
| 1929 | ++vxlan->dev->stats.rx_frame_errors; |
| 1930 | ++vxlan->dev->stats.rx_errors; |
| 1931 | goto drop; |
| 1932 | } |
| 1933 | |
Eric Dumazet | 59cbf56 | 2019-03-10 10:36:40 -0700 | [diff] [blame] | 1934 | rcu_read_lock(); |
| 1935 | |
| 1936 | if (unlikely(!(vxlan->dev->flags & IFF_UP))) { |
| 1937 | rcu_read_unlock(); |
| 1938 | atomic_long_inc(&vxlan->dev->rx_dropped); |
| 1939 | goto drop; |
| 1940 | } |
| 1941 | |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 1942 | stats = this_cpu_ptr(vxlan->dev->tstats); |
| 1943 | u64_stats_update_begin(&stats->syncp); |
| 1944 | stats->rx_packets++; |
| 1945 | stats->rx_bytes += skb->len; |
| 1946 | u64_stats_update_end(&stats->syncp); |
| 1947 | |
| 1948 | gro_cells_receive(&vxlan->gro_cells, skb); |
Eric Dumazet | 59cbf56 | 2019-03-10 10:36:40 -0700 | [diff] [blame] | 1949 | |
| 1950 | rcu_read_unlock(); |
| 1951 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1952 | return 0; |
| 1953 | |
| 1954 | drop: |
Jiri Benc | 288b01c | 2016-02-16 21:59:02 +0100 | [diff] [blame] | 1955 | /* Consume bad packet */ |
| 1956 | kfree_skb(skb); |
| 1957 | return 0; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Stefano Brivio | c3a43b9 | 2018-11-08 12:19:15 +0100 | [diff] [blame] | 1960 | /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */ |
| 1961 | static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb) |
| 1962 | { |
| 1963 | struct vxlan_dev *vxlan; |
| 1964 | struct vxlan_sock *vs; |
| 1965 | struct vxlanhdr *hdr; |
| 1966 | __be32 vni; |
| 1967 | |
Stefano Brivio | 8399a69 | 2019-06-11 00:27:05 +0200 | [diff] [blame] | 1968 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN)) |
Stefano Brivio | c3a43b9 | 2018-11-08 12:19:15 +0100 | [diff] [blame] | 1969 | return -EINVAL; |
| 1970 | |
| 1971 | hdr = vxlan_hdr(skb); |
| 1972 | |
| 1973 | if (!(hdr->vx_flags & VXLAN_HF_VNI)) |
| 1974 | return -EINVAL; |
| 1975 | |
| 1976 | vs = rcu_dereference_sk_user_data(sk); |
| 1977 | if (!vs) |
| 1978 | return -ENOENT; |
| 1979 | |
| 1980 | vni = vxlan_vni(hdr->vx_vni); |
| 1981 | vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni); |
| 1982 | if (!vxlan) |
| 1983 | return -ENOENT; |
| 1984 | |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 1988 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1989 | { |
| 1990 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1991 | struct arphdr *parp; |
| 1992 | u8 *arpptr, *sha; |
| 1993 | __be32 sip, tip; |
| 1994 | struct neighbour *n; |
| 1995 | |
| 1996 | if (dev->flags & IFF_NOARP) |
| 1997 | goto out; |
| 1998 | |
| 1999 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { |
| 2000 | dev->stats.tx_dropped++; |
| 2001 | goto out; |
| 2002 | } |
| 2003 | parp = arp_hdr(skb); |
| 2004 | |
| 2005 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && |
| 2006 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 2007 | parp->ar_pro != htons(ETH_P_IP) || |
| 2008 | parp->ar_op != htons(ARPOP_REQUEST) || |
| 2009 | parp->ar_hln != dev->addr_len || |
| 2010 | parp->ar_pln != 4) |
| 2011 | goto out; |
| 2012 | arpptr = (u8 *)parp + sizeof(struct arphdr); |
| 2013 | sha = arpptr; |
| 2014 | arpptr += dev->addr_len; /* sha */ |
| 2015 | memcpy(&sip, arpptr, sizeof(sip)); |
| 2016 | arpptr += sizeof(sip); |
| 2017 | arpptr += dev->addr_len; /* tha */ |
| 2018 | memcpy(&tip, arpptr, sizeof(tip)); |
| 2019 | |
| 2020 | if (ipv4_is_loopback(tip) || |
| 2021 | ipv4_is_multicast(tip)) |
| 2022 | goto out; |
| 2023 | |
| 2024 | n = neigh_lookup(&arp_tbl, &tip, dev); |
| 2025 | |
| 2026 | if (n) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2027 | struct vxlan_fdb *f; |
| 2028 | struct sk_buff *reply; |
| 2029 | |
| 2030 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 2031 | neigh_release(n); |
| 2032 | goto out; |
| 2033 | } |
| 2034 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2035 | f = vxlan_find_mac(vxlan, n->ha, vni); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2036 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2037 | /* bridge-local neighbor */ |
| 2038 | neigh_release(n); |
| 2039 | goto out; |
| 2040 | } |
| 2041 | |
| 2042 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, |
| 2043 | n->ha, sha); |
| 2044 | |
| 2045 | neigh_release(n); |
| 2046 | |
David Stevens | 7346135 | 2014-03-18 12:32:29 -0400 | [diff] [blame] | 2047 | if (reply == NULL) |
| 2048 | goto out; |
| 2049 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2050 | skb_reset_mac_header(reply); |
| 2051 | __skb_pull(reply, skb_network_offset(reply)); |
| 2052 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 2053 | reply->pkt_type = PACKET_HOST; |
| 2054 | |
| 2055 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 2056 | dev->stats.rx_dropped++; |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2057 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2058 | union vxlan_addr ipa = { |
| 2059 | .sin.sin_addr.s_addr = tip, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 2060 | .sin.sin_family = AF_INET, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2061 | }; |
| 2062 | |
| 2063 | vxlan_ip_miss(dev, &ipa); |
| 2064 | } |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2065 | out: |
| 2066 | consume_skb(skb); |
| 2067 | return NETDEV_TX_OK; |
| 2068 | } |
| 2069 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2070 | #if IS_ENABLED(CONFIG_IPV6) |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2071 | static struct sk_buff *vxlan_na_create(struct sk_buff *request, |
| 2072 | struct neighbour *n, bool isrouter) |
| 2073 | { |
| 2074 | struct net_device *dev = request->dev; |
| 2075 | struct sk_buff *reply; |
| 2076 | struct nd_msg *ns, *na; |
| 2077 | struct ipv6hdr *pip6; |
| 2078 | u8 *daddr; |
| 2079 | int na_olen = 8; /* opt hdr + ETH_ALEN for target */ |
| 2080 | int ns_olen; |
| 2081 | int i, len; |
| 2082 | |
Vincent Bernat | f1fb08f | 2017-04-02 11:00:06 +0200 | [diff] [blame] | 2083 | if (dev == NULL || !pskb_may_pull(request, request->len)) |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2084 | return NULL; |
| 2085 | |
| 2086 | len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + |
| 2087 | sizeof(*na) + na_olen + dev->needed_tailroom; |
| 2088 | reply = alloc_skb(len, GFP_ATOMIC); |
| 2089 | if (reply == NULL) |
| 2090 | return NULL; |
| 2091 | |
| 2092 | reply->protocol = htons(ETH_P_IPV6); |
| 2093 | reply->dev = dev; |
| 2094 | skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); |
| 2095 | skb_push(reply, sizeof(struct ethhdr)); |
Zhang Shengju | 6297b91 | 2016-03-03 01:16:54 +0000 | [diff] [blame] | 2096 | skb_reset_mac_header(reply); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2097 | |
Vincent Bernat | f1fb08f | 2017-04-02 11:00:06 +0200 | [diff] [blame] | 2098 | ns = (struct nd_msg *)(ipv6_hdr(request) + 1); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2099 | |
| 2100 | daddr = eth_hdr(request)->h_source; |
Vincent Bernat | f1fb08f | 2017-04-02 11:00:06 +0200 | [diff] [blame] | 2101 | ns_olen = request->len - skb_network_offset(request) - |
| 2102 | sizeof(struct ipv6hdr) - sizeof(*ns); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2103 | for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { |
Ido Schimmel | 8066e6b | 2020-06-01 15:58:55 +0300 | [diff] [blame] | 2104 | if (!ns->opt[i + 1]) { |
| 2105 | kfree_skb(reply); |
| 2106 | return NULL; |
| 2107 | } |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2108 | if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { |
| 2109 | daddr = ns->opt + i + sizeof(struct nd_opt_hdr); |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | /* Ethernet header */ |
| 2115 | ether_addr_copy(eth_hdr(reply)->h_dest, daddr); |
| 2116 | ether_addr_copy(eth_hdr(reply)->h_source, n->ha); |
| 2117 | eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); |
| 2118 | reply->protocol = htons(ETH_P_IPV6); |
| 2119 | |
| 2120 | skb_pull(reply, sizeof(struct ethhdr)); |
Zhang Shengju | 6297b91 | 2016-03-03 01:16:54 +0000 | [diff] [blame] | 2121 | skb_reset_network_header(reply); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2122 | skb_put(reply, sizeof(struct ipv6hdr)); |
| 2123 | |
| 2124 | /* IPv6 header */ |
| 2125 | |
| 2126 | pip6 = ipv6_hdr(reply); |
| 2127 | memset(pip6, 0, sizeof(struct ipv6hdr)); |
| 2128 | pip6->version = 6; |
| 2129 | pip6->priority = ipv6_hdr(request)->priority; |
| 2130 | pip6->nexthdr = IPPROTO_ICMPV6; |
| 2131 | pip6->hop_limit = 255; |
| 2132 | pip6->daddr = ipv6_hdr(request)->saddr; |
| 2133 | pip6->saddr = *(struct in6_addr *)n->primary_key; |
| 2134 | |
| 2135 | skb_pull(reply, sizeof(struct ipv6hdr)); |
Zhang Shengju | 6297b91 | 2016-03-03 01:16:54 +0000 | [diff] [blame] | 2136 | skb_reset_transport_header(reply); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2137 | |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2138 | /* Neighbor Advertisement */ |
Johannes Berg | b080db5 | 2017-06-16 14:29:19 +0200 | [diff] [blame] | 2139 | na = skb_put_zero(reply, sizeof(*na) + na_olen); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2140 | na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; |
| 2141 | na->icmph.icmp6_router = isrouter; |
| 2142 | na->icmph.icmp6_override = 1; |
| 2143 | na->icmph.icmp6_solicited = 1; |
| 2144 | na->target = ns->target; |
| 2145 | ether_addr_copy(&na->opt[2], n->ha); |
| 2146 | na->opt[0] = ND_OPT_TARGET_LL_ADDR; |
| 2147 | na->opt[1] = na_olen >> 3; |
| 2148 | |
| 2149 | na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, |
| 2150 | &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, |
| 2151 | csum_partial(na, sizeof(*na)+na_olen, 0)); |
| 2152 | |
| 2153 | pip6->payload_len = htons(sizeof(*na)+na_olen); |
| 2154 | |
| 2155 | skb_push(reply, sizeof(struct ipv6hdr)); |
| 2156 | |
| 2157 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 2158 | |
| 2159 | return reply; |
| 2160 | } |
| 2161 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2162 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2163 | { |
| 2164 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Roopa Prabhu | 8dcd81a | 2017-02-20 08:41:16 -0800 | [diff] [blame] | 2165 | const struct in6_addr *daddr; |
Xin Long | 8bff3685 | 2017-11-11 19:58:50 +0800 | [diff] [blame] | 2166 | const struct ipv6hdr *iphdr; |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2167 | struct inet6_dev *in6_dev; |
Xin Long | 8bff3685 | 2017-11-11 19:58:50 +0800 | [diff] [blame] | 2168 | struct neighbour *n; |
| 2169 | struct nd_msg *msg; |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2170 | |
| 2171 | in6_dev = __in6_dev_get(dev); |
| 2172 | if (!in6_dev) |
| 2173 | goto out; |
| 2174 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2175 | iphdr = ipv6_hdr(skb); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2176 | daddr = &iphdr->daddr; |
Vincent Bernat | f1fb08f | 2017-04-02 11:00:06 +0200 | [diff] [blame] | 2177 | msg = (struct nd_msg *)(iphdr + 1); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2178 | |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2179 | if (ipv6_addr_loopback(daddr) || |
| 2180 | ipv6_addr_is_multicast(&msg->target)) |
| 2181 | goto out; |
| 2182 | |
| 2183 | n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2184 | |
| 2185 | if (n) { |
| 2186 | struct vxlan_fdb *f; |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2187 | struct sk_buff *reply; |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2188 | |
| 2189 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 2190 | neigh_release(n); |
| 2191 | goto out; |
| 2192 | } |
| 2193 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2194 | f = vxlan_find_mac(vxlan, n->ha, vni); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2195 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
| 2196 | /* bridge-local neighbor */ |
| 2197 | neigh_release(n); |
| 2198 | goto out; |
| 2199 | } |
| 2200 | |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2201 | reply = vxlan_na_create(skb, n, |
| 2202 | !!(f ? f->flags & NTF_ROUTER : 0)); |
| 2203 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2204 | neigh_release(n); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2205 | |
| 2206 | if (reply == NULL) |
| 2207 | goto out; |
| 2208 | |
| 2209 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 2210 | dev->stats.rx_dropped++; |
| 2211 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2212 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2213 | union vxlan_addr ipa = { |
| 2214 | .sin6.sin6_addr = msg->target, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 2215 | .sin6.sin6_family = AF_INET6, |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 2216 | }; |
| 2217 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2218 | vxlan_ip_miss(dev, &ipa); |
| 2219 | } |
| 2220 | |
| 2221 | out: |
| 2222 | consume_skb(skb); |
| 2223 | return NETDEV_TX_OK; |
| 2224 | } |
| 2225 | #endif |
| 2226 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2227 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
| 2228 | { |
| 2229 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 2230 | struct neighbour *n; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2231 | |
| 2232 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 2233 | return false; |
| 2234 | |
| 2235 | n = NULL; |
| 2236 | switch (ntohs(eth_hdr(skb)->h_proto)) { |
| 2237 | case ETH_P_IP: |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2238 | { |
| 2239 | struct iphdr *pip; |
| 2240 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2241 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 2242 | return false; |
| 2243 | pip = ip_hdr(skb); |
| 2244 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2245 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2246 | union vxlan_addr ipa = { |
| 2247 | .sin.sin_addr.s_addr = pip->daddr, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 2248 | .sin.sin_family = AF_INET, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2249 | }; |
| 2250 | |
| 2251 | vxlan_ip_miss(dev, &ipa); |
| 2252 | return false; |
| 2253 | } |
| 2254 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2255 | break; |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2256 | } |
| 2257 | #if IS_ENABLED(CONFIG_IPV6) |
| 2258 | case ETH_P_IPV6: |
| 2259 | { |
| 2260 | struct ipv6hdr *pip6; |
| 2261 | |
| 2262 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 2263 | return false; |
| 2264 | pip6 = ipv6_hdr(skb); |
| 2265 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2266 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2267 | union vxlan_addr ipa = { |
| 2268 | .sin6.sin6_addr = pip6->daddr, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 2269 | .sin6.sin6_family = AF_INET6, |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2270 | }; |
| 2271 | |
| 2272 | vxlan_ip_miss(dev, &ipa); |
| 2273 | return false; |
| 2274 | } |
| 2275 | |
| 2276 | break; |
| 2277 | } |
| 2278 | #endif |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2279 | default: |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
| 2283 | if (n) { |
| 2284 | bool diff; |
| 2285 | |
Joe Perches | 7367d0b | 2013-09-01 11:51:23 -0700 | [diff] [blame] | 2286 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2287 | if (diff) { |
| 2288 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, |
| 2289 | dev->addr_len); |
| 2290 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); |
| 2291 | } |
| 2292 | neigh_release(n); |
| 2293 | return diff; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2294 | } |
| 2295 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2296 | return false; |
| 2297 | } |
| 2298 | |
Tom Herbert | af33c1a | 2015-01-20 11:23:05 -0800 | [diff] [blame] | 2299 | static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags, |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 2300 | struct vxlan_metadata *md) |
| 2301 | { |
| 2302 | struct vxlanhdr_gbp *gbp; |
| 2303 | |
Thomas Graf | db79a62 | 2015-02-04 17:00:04 +0100 | [diff] [blame] | 2304 | if (!md->gbp) |
| 2305 | return; |
| 2306 | |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 2307 | gbp = (struct vxlanhdr_gbp *)vxh; |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2308 | vxh->vx_flags |= VXLAN_HF_GBP; |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 2309 | |
| 2310 | if (md->gbp & VXLAN_GBP_DONT_LEARN) |
| 2311 | gbp->dont_learn = 1; |
| 2312 | |
| 2313 | if (md->gbp & VXLAN_GBP_POLICY_APPLIED) |
| 2314 | gbp->policy_applied = 1; |
| 2315 | |
| 2316 | gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK); |
| 2317 | } |
| 2318 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2319 | static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags, |
| 2320 | __be16 protocol) |
| 2321 | { |
| 2322 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh; |
| 2323 | |
| 2324 | gpe->np_applied = 1; |
Jiri Benc | fa20e0e | 2017-08-28 21:43:22 +0200 | [diff] [blame] | 2325 | gpe->next_protocol = tun_p_from_eth_p(protocol); |
| 2326 | if (!gpe->next_protocol) |
| 2327 | return -EPFNOSUPPORT; |
| 2328 | return 0; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2329 | } |
| 2330 | |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2331 | static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, |
| 2332 | int iphdr_len, __be32 vni, |
| 2333 | struct vxlan_metadata *md, u32 vxflags, |
Jiri Benc | b4ed5ca | 2016-02-02 18:09:15 +0100 | [diff] [blame] | 2334 | bool udp_sum) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2335 | { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2336 | struct vxlanhdr *vxh; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2337 | int min_headroom; |
| 2338 | int err; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2339 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2340 | __be16 inner_protocol = htons(ETH_P_TEB); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2341 | |
Tom Herbert | af33c1a | 2015-01-20 11:23:05 -0800 | [diff] [blame] | 2342 | if ((vxflags & VXLAN_F_REMCSUM_TX) && |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2343 | skb->ip_summed == CHECKSUM_PARTIAL) { |
| 2344 | int csum_start = skb_checksum_start_offset(skb); |
| 2345 | |
| 2346 | if (csum_start <= VXLAN_MAX_REMCSUM_START && |
| 2347 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && |
| 2348 | (skb->csum_offset == offsetof(struct udphdr, check) || |
Edward Cree | b570850 | 2016-02-11 20:57:17 +0000 | [diff] [blame] | 2349 | skb->csum_offset == offsetof(struct tcphdr, check))) |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2350 | type |= SKB_GSO_TUNNEL_REMCSUM; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2353 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
pravin shelar | 4a4f86c | 2016-11-13 20:43:52 -0800 | [diff] [blame] | 2354 | + VXLAN_HLEN + iphdr_len; |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 2355 | |
| 2356 | /* Need space for new headers (invalidates iph ptr) */ |
| 2357 | err = skb_cow_head(skb, min_headroom); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2358 | if (unlikely(err)) |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2359 | return err; |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 2360 | |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 2361 | err = iptunnel_handle_offloads(skb, type); |
| 2362 | if (err) |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2363 | return err; |
Jesse Gross | b736a62 | 2015-04-09 11:19:14 -0700 | [diff] [blame] | 2364 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 2365 | vxh = __skb_push(skb, sizeof(*vxh)); |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2366 | vxh->vx_flags = VXLAN_HF_VNI; |
| 2367 | vxh->vx_vni = vxlan_vni_field(vni); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 2368 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2369 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2370 | unsigned int start; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2371 | |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2372 | start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr); |
| 2373 | vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset); |
| 2374 | vxh->vx_flags |= VXLAN_HF_RCO; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 2375 | |
| 2376 | if (!skb_is_gso(skb)) { |
| 2377 | skb->ip_summed = CHECKSUM_NONE; |
| 2378 | skb->encapsulation = 0; |
| 2379 | } |
| 2380 | } |
| 2381 | |
Tom Herbert | af33c1a | 2015-01-20 11:23:05 -0800 | [diff] [blame] | 2382 | if (vxflags & VXLAN_F_GBP) |
| 2383 | vxlan_build_gbp_hdr(vxh, vxflags, md); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2384 | if (vxflags & VXLAN_F_GPE) { |
| 2385 | err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol); |
| 2386 | if (err < 0) |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2387 | return err; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2388 | inner_protocol = skb->protocol; |
| 2389 | } |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 2390 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 2391 | skb_set_inner_protocol(skb, inner_protocol); |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 2392 | return 0; |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 2393 | } |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 2394 | |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2395 | static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev, |
| 2396 | struct vxlan_sock *sock4, |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2397 | struct sk_buff *skb, int oif, u8 tos, |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2398 | __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport, |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2399 | struct dst_cache *dst_cache, |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2400 | const struct ip_tunnel_info *info) |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2401 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2402 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2403 | struct rtable *rt = NULL; |
| 2404 | struct flowi4 fl4; |
| 2405 | |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2406 | if (!sock4) |
| 2407 | return ERR_PTR(-EIO); |
| 2408 | |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2409 | if (tos && !info) |
| 2410 | use_cache = false; |
| 2411 | if (use_cache) { |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2412 | rt = dst_cache_get_ip4(dst_cache, saddr); |
| 2413 | if (rt) |
| 2414 | return rt; |
| 2415 | } |
| 2416 | |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2417 | memset(&fl4, 0, sizeof(fl4)); |
| 2418 | fl4.flowi4_oif = oif; |
| 2419 | fl4.flowi4_tos = RT_TOS(tos); |
| 2420 | fl4.flowi4_mark = skb->mark; |
| 2421 | fl4.flowi4_proto = IPPROTO_UDP; |
| 2422 | fl4.daddr = daddr; |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2423 | fl4.saddr = *saddr; |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2424 | fl4.fl4_dport = dport; |
| 2425 | fl4.fl4_sport = sport; |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2426 | |
| 2427 | rt = ip_route_output_key(vxlan->net, &fl4); |
Enrico Weigelt | 478db1f | 2019-06-05 23:19:13 +0200 | [diff] [blame] | 2428 | if (!IS_ERR(rt)) { |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2429 | if (rt->dst.dev == dev) { |
| 2430 | netdev_dbg(dev, "circular route to %pI4\n", &daddr); |
| 2431 | ip_rt_put(rt); |
| 2432 | return ERR_PTR(-ELOOP); |
| 2433 | } |
| 2434 | |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2435 | *saddr = fl4.saddr; |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2436 | if (use_cache) |
| 2437 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2438 | } else { |
| 2439 | netdev_dbg(dev, "no route to %pI4\n", &daddr); |
| 2440 | return ERR_PTR(-ENETUNREACH); |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2441 | } |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 2442 | return rt; |
| 2443 | } |
| 2444 | |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2445 | #if IS_ENABLED(CONFIG_IPV6) |
| 2446 | static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan, |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2447 | struct net_device *dev, |
pravin shelar | 03dc52a | 2016-11-13 20:43:53 -0800 | [diff] [blame] | 2448 | struct vxlan_sock *sock6, |
Daniel Borkmann | 1400615 | 2016-03-04 15:15:08 +0100 | [diff] [blame] | 2449 | struct sk_buff *skb, int oif, u8 tos, |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 2450 | __be32 label, |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2451 | const struct in6_addr *daddr, |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2452 | struct in6_addr *saddr, |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2453 | __be16 dport, __be16 sport, |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2454 | struct dst_cache *dst_cache, |
| 2455 | const struct ip_tunnel_info *info) |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2456 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2457 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2458 | struct dst_entry *ndst; |
| 2459 | struct flowi6 fl6; |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2460 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 2461 | if (!sock6) |
| 2462 | return ERR_PTR(-EIO); |
| 2463 | |
Daniel Borkmann | 1400615 | 2016-03-04 15:15:08 +0100 | [diff] [blame] | 2464 | if (tos && !info) |
| 2465 | use_cache = false; |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2466 | if (use_cache) { |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2467 | ndst = dst_cache_get_ip6(dst_cache, saddr); |
| 2468 | if (ndst) |
| 2469 | return ndst; |
| 2470 | } |
| 2471 | |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2472 | memset(&fl6, 0, sizeof(fl6)); |
| 2473 | fl6.flowi6_oif = oif; |
| 2474 | fl6.daddr = *daddr; |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2475 | fl6.saddr = *saddr; |
Daniel Borkmann | eaa93bf | 2016-03-18 18:37:57 +0100 | [diff] [blame] | 2476 | fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label); |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2477 | fl6.flowi6_mark = skb->mark; |
| 2478 | fl6.flowi6_proto = IPPROTO_UDP; |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2479 | fl6.fl6_dport = dport; |
| 2480 | fl6.fl6_sport = sport; |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2481 | |
Sabrina Dubroca | 6c8991f | 2019-12-04 15:35:53 +0100 | [diff] [blame] | 2482 | ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk, |
| 2483 | &fl6, NULL); |
| 2484 | if (unlikely(IS_ERR(ndst))) { |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2485 | netdev_dbg(dev, "no route to %pI6\n", daddr); |
| 2486 | return ERR_PTR(-ENETUNREACH); |
| 2487 | } |
| 2488 | |
| 2489 | if (unlikely(ndst->dev == dev)) { |
| 2490 | netdev_dbg(dev, "circular route to %pI6\n", daddr); |
| 2491 | dst_release(ndst); |
| 2492 | return ERR_PTR(-ELOOP); |
| 2493 | } |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2494 | |
| 2495 | *saddr = fl6.saddr; |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2496 | if (use_cache) |
Paolo Abeni | 0c1d70a | 2016-02-12 15:43:56 +0100 | [diff] [blame] | 2497 | dst_cache_set_ip6(dst_cache, ndst, saddr); |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2498 | return ndst; |
| 2499 | } |
| 2500 | #endif |
| 2501 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2502 | /* Bypass encapsulation if the destination is local */ |
| 2503 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2504 | struct vxlan_dev *dst_vxlan, __be32 vni, |
| 2505 | bool snoop) |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2506 | { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 2507 | struct pcpu_sw_netstats *tx_stats, *rx_stats; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2508 | union vxlan_addr loopback; |
| 2509 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; |
Eric Dumazet | 4179cb5 | 2019-02-07 12:27:38 -0800 | [diff] [blame] | 2510 | struct net_device *dev; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 2511 | int len = skb->len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2512 | |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 2513 | tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
| 2514 | rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2515 | skb->pkt_type = PACKET_HOST; |
| 2516 | skb->encapsulation = 0; |
| 2517 | skb->dev = dst_vxlan->dev; |
| 2518 | __skb_pull(skb, skb_network_offset(skb)); |
| 2519 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2520 | if (remote_ip->sa.sa_family == AF_INET) { |
| 2521 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 2522 | loopback.sa.sa_family = AF_INET; |
| 2523 | #if IS_ENABLED(CONFIG_IPV6) |
| 2524 | } else { |
| 2525 | loopback.sin6.sin6_addr = in6addr_loopback; |
| 2526 | loopback.sa.sa_family = AF_INET6; |
| 2527 | #endif |
| 2528 | } |
| 2529 | |
Eric Dumazet | 4179cb5 | 2019-02-07 12:27:38 -0800 | [diff] [blame] | 2530 | rcu_read_lock(); |
| 2531 | dev = skb->dev; |
| 2532 | if (unlikely(!(dev->flags & IFF_UP))) { |
| 2533 | kfree_skb(skb); |
| 2534 | goto drop; |
| 2535 | } |
| 2536 | |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2537 | if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop) |
Eric Dumazet | 4179cb5 | 2019-02-07 12:27:38 -0800 | [diff] [blame] | 2538 | vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2539 | |
| 2540 | u64_stats_update_begin(&tx_stats->syncp); |
| 2541 | tx_stats->tx_packets++; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 2542 | tx_stats->tx_bytes += len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2543 | u64_stats_update_end(&tx_stats->syncp); |
| 2544 | |
| 2545 | if (netif_rx(skb) == NET_RX_SUCCESS) { |
| 2546 | u64_stats_update_begin(&rx_stats->syncp); |
| 2547 | rx_stats->rx_packets++; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 2548 | rx_stats->rx_bytes += len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2549 | u64_stats_update_end(&rx_stats->syncp); |
| 2550 | } else { |
Eric Dumazet | 4179cb5 | 2019-02-07 12:27:38 -0800 | [diff] [blame] | 2551 | drop: |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 2552 | dev->stats.rx_dropped++; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2553 | } |
Eric Dumazet | 4179cb5 | 2019-02-07 12:27:38 -0800 | [diff] [blame] | 2554 | rcu_read_unlock(); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2557 | static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev, |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2558 | struct vxlan_dev *vxlan, |
| 2559 | union vxlan_addr *daddr, |
| 2560 | __be16 dst_port, int dst_ifindex, __be32 vni, |
| 2561 | struct dst_entry *dst, |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2562 | u32 rt_flags) |
| 2563 | { |
| 2564 | #if IS_ENABLED(CONFIG_IPV6) |
| 2565 | /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of |
| 2566 | * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple |
| 2567 | * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry. |
| 2568 | */ |
| 2569 | BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL); |
| 2570 | #endif |
| 2571 | /* Bypass encapsulation if the destination is local */ |
| 2572 | if (rt_flags & RTCF_LOCAL && |
| 2573 | !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
| 2574 | struct vxlan_dev *dst_vxlan; |
| 2575 | |
| 2576 | dst_release(dst); |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2577 | dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni, |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2578 | daddr->sa.sa_family, dst_port, |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2579 | vxlan->cfg.flags); |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2580 | if (!dst_vxlan) { |
| 2581 | dev->stats.tx_errors++; |
| 2582 | kfree_skb(skb); |
| 2583 | |
| 2584 | return -ENOENT; |
| 2585 | } |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2586 | vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true); |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2587 | return 1; |
| 2588 | } |
| 2589 | |
| 2590 | return 0; |
| 2591 | } |
| 2592 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2593 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2594 | __be32 default_vni, struct vxlan_rdst *rdst, |
| 2595 | bool did_rsc) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2596 | { |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 2597 | struct dst_cache *dst_cache; |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 2598 | struct ip_tunnel_info *info; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2599 | struct vxlan_dev *vxlan = netdev_priv(dev); |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2600 | const struct iphdr *old_iph = ip_hdr(skb); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2601 | union vxlan_addr *dst; |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2602 | union vxlan_addr remote_ip, local_ip; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 2603 | struct vxlan_metadata _md; |
| 2604 | struct vxlan_metadata *md = &_md; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2605 | __be16 src_port = 0, dst_port; |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2606 | struct dst_entry *ndst = NULL; |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 2607 | __be32 vni, label; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2608 | __u8 tos, ttl; |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2609 | int ifindex; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 2610 | int err; |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2611 | u32 flags = vxlan->cfg.flags; |
Jiri Benc | b4ed5ca | 2016-02-02 18:09:15 +0100 | [diff] [blame] | 2612 | bool udp_sum = false; |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2613 | bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2614 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 2615 | info = skb_tunnel_info(skb); |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 2616 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 2617 | if (rdst) { |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2618 | dst = &rdst->remote_ip; |
| 2619 | if (vxlan_addr_any(dst)) { |
| 2620 | if (did_rsc) { |
| 2621 | /* short-circuited back to local bridge */ |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2622 | vxlan_encap_bypass(skb, vxlan, vxlan, |
| 2623 | default_vni, true); |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2624 | return; |
| 2625 | } |
| 2626 | goto drop; |
| 2627 | } |
| 2628 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 2629 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2630 | vni = (rdst->remote_vni) ? : default_vni; |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2631 | ifindex = rdst->remote_ifindex; |
Brian Russell | 1158632 | 2017-02-24 17:47:11 +0000 | [diff] [blame] | 2632 | local_ip = vxlan->cfg.saddr; |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 2633 | dst_cache = &rdst->dst_cache; |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2634 | md->gbp = skb->mark; |
Hangbin Liu | 72f6d71 | 2018-04-17 14:11:28 +0800 | [diff] [blame] | 2635 | if (flags & VXLAN_F_TTL_INHERIT) { |
| 2636 | ttl = ip_tunnel_get_ttl(old_iph, skb); |
| 2637 | } else { |
| 2638 | ttl = vxlan->cfg.ttl; |
| 2639 | if (!ttl && vxlan_addr_multicast(dst)) |
| 2640 | ttl = 1; |
| 2641 | } |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2642 | |
| 2643 | tos = vxlan->cfg.tos; |
| 2644 | if (tos == 1) |
| 2645 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
| 2646 | |
| 2647 | if (dst->sa.sa_family == AF_INET) |
| 2648 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); |
| 2649 | else |
| 2650 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); |
| 2651 | label = vxlan->cfg.label; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 2652 | } else { |
| 2653 | if (!info) { |
| 2654 | WARN_ONCE(1, "%s: Missing encapsulation instructions\n", |
| 2655 | dev->name); |
| 2656 | goto drop; |
| 2657 | } |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 2658 | remote_ip.sa.sa_family = ip_tunnel_info_af(info); |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2659 | if (remote_ip.sa.sa_family == AF_INET) { |
Jiri Benc | a725e51 | 2015-08-20 13:56:30 +0200 | [diff] [blame] | 2660 | remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst; |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2661 | local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src; |
| 2662 | } else { |
Jiri Benc | a725e51 | 2015-08-20 13:56:30 +0200 | [diff] [blame] | 2663 | remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst; |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2664 | local_ip.sin6.sin6_addr = info->key.u.ipv6.src; |
| 2665 | } |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 2666 | dst = &remote_ip; |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2667 | dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; |
| 2668 | vni = tunnel_id_to_key32(info->key.tun_id); |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2669 | ifindex = 0; |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 2670 | dst_cache = &info->dst_cache; |
Xin Long | eadf52c | 2019-10-29 01:24:32 +0800 | [diff] [blame] | 2671 | if (info->key.tun_flags & TUNNEL_VXLAN_OPT) { |
| 2672 | if (info->options_len < sizeof(*md)) |
| 2673 | goto drop; |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2674 | md = ip_tunnel_info_opts(info); |
Xin Long | eadf52c | 2019-10-29 01:24:32 +0800 | [diff] [blame] | 2675 | } |
Jiri Benc | a725e51 | 2015-08-20 13:56:30 +0200 | [diff] [blame] | 2676 | ttl = info->key.ttl; |
| 2677 | tos = info->key.tos; |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 2678 | label = info->key.label; |
Jiri Benc | b4ed5ca | 2016-02-02 18:09:15 +0100 | [diff] [blame] | 2679 | udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
Jiri Benc | a725e51 | 2015-08-20 13:56:30 +0200 | [diff] [blame] | 2680 | } |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2681 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
| 2682 | vxlan->cfg.port_max, true); |
Jiri Benc | a725e51 | 2015-08-20 13:56:30 +0200 | [diff] [blame] | 2683 | |
Jakub Kicinski | 56de859 | 2017-02-24 11:43:36 -0800 | [diff] [blame] | 2684 | rcu_read_lock(); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2685 | if (dst->sa.sa_family == AF_INET) { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 2686 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2687 | struct rtable *rt; |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2688 | __be16 df = 0; |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 2689 | |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 2690 | if (!ifindex) |
| 2691 | ifindex = sock4->sock->sk->sk_bound_dev_if; |
| 2692 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2693 | rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos, |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2694 | dst->sin.sin_addr.s_addr, |
Brian Russell | 1158632 | 2017-02-24 17:47:11 +0000 | [diff] [blame] | 2695 | &local_ip.sin.sin_addr.s_addr, |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2696 | dst_port, src_port, |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 2697 | dst_cache, info); |
David S. Miller | 8ebd115 | 2016-11-15 16:32:11 -0500 | [diff] [blame] | 2698 | if (IS_ERR(rt)) { |
| 2699 | err = PTR_ERR(rt); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2700 | goto tx_error; |
David S. Miller | 8ebd115 | 2016-11-15 16:32:11 -0500 | [diff] [blame] | 2701 | } |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2702 | |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2703 | if (!info) { |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 2704 | /* Bypass encapsulation if the destination is local */ |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2705 | err = encap_bypass_if_local(skb, dev, vxlan, dst, |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2706 | dst_port, ifindex, vni, |
| 2707 | &rt->dst, rt->rt_flags); |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2708 | if (err) |
Jakub Kicinski | 56de859 | 2017-02-24 11:43:36 -0800 | [diff] [blame] | 2709 | goto out_unlock; |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 2710 | |
| 2711 | if (vxlan->cfg.df == VXLAN_DF_SET) { |
| 2712 | df = htons(IP_DF); |
| 2713 | } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) { |
| 2714 | struct ethhdr *eth = eth_hdr(skb); |
| 2715 | |
| 2716 | if (ntohs(eth->h_proto) == ETH_P_IPV6 || |
| 2717 | (ntohs(eth->h_proto) == ETH_P_IP && |
| 2718 | old_iph->frag_off & htons(IP_DF))) |
| 2719 | df = htons(IP_DF); |
| 2720 | } |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2721 | } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) { |
Alexander Duyck | 6ceb31c | 2016-02-19 11:26:31 -0800 | [diff] [blame] | 2722 | df = htons(IP_DF); |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2723 | } |
Alexander Duyck | 6ceb31c | 2016-02-19 11:26:31 -0800 | [diff] [blame] | 2724 | |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2725 | ndst = &rt->dst; |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2726 | err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM, |
| 2727 | netif_is_any_bridge_port(dev)); |
| 2728 | if (err < 0) { |
| 2729 | goto tx_error; |
| 2730 | } else if (err) { |
| 2731 | if (info) { |
| 2732 | struct in_addr src, dst; |
| 2733 | |
| 2734 | src = remote_ip.sin.sin_addr; |
| 2735 | dst = local_ip.sin.sin_addr; |
| 2736 | info->key.u.ipv4.src = src.s_addr; |
| 2737 | info->key.u.ipv4.dst = dst.s_addr; |
| 2738 | } |
| 2739 | vxlan_encap_bypass(skb, vxlan, vxlan, vni, false); |
| 2740 | dst_release(ndst); |
| 2741 | goto out_unlock; |
| 2742 | } |
Xin Long | a93bf0f | 2017-12-18 14:20:56 +0800 | [diff] [blame] | 2743 | |
Hangbin Liu | a0dced1 | 2020-08-05 10:41:31 +0800 | [diff] [blame] | 2744 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2745 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2746 | err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr), |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2747 | vni, md, flags, udp_sum); |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2748 | if (err < 0) |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2749 | goto tx_error; |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2750 | |
Brian Russell | 1158632 | 2017-02-24 17:47:11 +0000 | [diff] [blame] | 2751 | udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr, |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2752 | dst->sin.sin_addr.s_addr, tos, ttl, df, |
| 2753 | src_port, dst_port, xnet, !udp_sum); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2754 | #if IS_ENABLED(CONFIG_IPV6) |
| 2755 | } else { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 2756 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2757 | |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 2758 | if (!ifindex) |
| 2759 | ifindex = sock6->sock->sk->sk_bound_dev_if; |
| 2760 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2761 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos, |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2762 | label, &dst->sin6.sin6_addr, |
Brian Russell | 1158632 | 2017-02-24 17:47:11 +0000 | [diff] [blame] | 2763 | &local_ip.sin6.sin6_addr, |
Martynas Pumputis | 4ecb1d8 | 2017-01-11 15:18:53 +0000 | [diff] [blame] | 2764 | dst_port, src_port, |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2765 | dst_cache, info); |
Jiri Benc | e5d4b29 | 2015-12-07 13:04:30 +0100 | [diff] [blame] | 2766 | if (IS_ERR(ndst)) { |
David S. Miller | 8ebd115 | 2016-11-15 16:32:11 -0500 | [diff] [blame] | 2767 | err = PTR_ERR(ndst); |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2768 | ndst = NULL; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2769 | goto tx_error; |
| 2770 | } |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2771 | |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2772 | if (!info) { |
| 2773 | u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2774 | |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2775 | err = encap_bypass_if_local(skb, dev, vxlan, dst, |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 2776 | dst_port, ifindex, vni, |
| 2777 | ndst, rt6i_flags); |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2778 | if (err) |
Jakub Kicinski | 56de859 | 2017-02-24 11:43:36 -0800 | [diff] [blame] | 2779 | goto out_unlock; |
pravin shelar | fee1fad | 2016-11-13 20:43:56 -0800 | [diff] [blame] | 2780 | } |
Jesse Gross | 35e2d11 | 2016-01-20 16:22:47 -0800 | [diff] [blame] | 2781 | |
Stefano Brivio | fc68c99 | 2020-08-04 07:53:44 +0200 | [diff] [blame] | 2782 | err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM, |
| 2783 | netif_is_any_bridge_port(dev)); |
| 2784 | if (err < 0) { |
| 2785 | goto tx_error; |
| 2786 | } else if (err) { |
| 2787 | if (info) { |
| 2788 | struct in6_addr src, dst; |
| 2789 | |
| 2790 | src = remote_ip.sin6.sin6_addr; |
| 2791 | dst = local_ip.sin6.sin6_addr; |
| 2792 | info->key.u.ipv6.src = src; |
| 2793 | info->key.u.ipv6.dst = dst; |
| 2794 | } |
| 2795 | |
| 2796 | vxlan_encap_bypass(skb, vxlan, vxlan, vni, false); |
| 2797 | dst_release(ndst); |
| 2798 | goto out_unlock; |
| 2799 | } |
Xin Long | a93bf0f | 2017-12-18 14:20:56 +0800 | [diff] [blame] | 2800 | |
Hangbin Liu | a0dced1 | 2020-08-05 10:41:31 +0800 | [diff] [blame] | 2801 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2802 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
Jiri Benc | f491e56 | 2016-02-02 18:09:16 +0100 | [diff] [blame] | 2803 | skb_scrub_packet(skb, xnet); |
| 2804 | err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr), |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 2805 | vni, md, flags, udp_sum); |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2806 | if (err < 0) |
| 2807 | goto tx_error; |
| 2808 | |
pravin shelar | 0770b53 | 2016-11-13 20:43:57 -0800 | [diff] [blame] | 2809 | udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev, |
Brian Russell | 1158632 | 2017-02-24 17:47:11 +0000 | [diff] [blame] | 2810 | &local_ip.sin6.sin6_addr, |
pravin shelar | 272d96a | 2016-08-05 17:45:36 -0700 | [diff] [blame] | 2811 | &dst->sin6.sin6_addr, tos, ttl, |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 2812 | label, src_port, dst_port, !udp_sum); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2813 | #endif |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 2814 | } |
Jakub Kicinski | 56de859 | 2017-02-24 11:43:36 -0800 | [diff] [blame] | 2815 | out_unlock: |
| 2816 | rcu_read_unlock(); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2817 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2818 | |
| 2819 | drop: |
| 2820 | dev->stats.tx_dropped++; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2821 | dev_kfree_skb(skb); |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2822 | return; |
| 2823 | |
| 2824 | tx_error: |
Jakub Kicinski | 56de859 | 2017-02-24 11:43:36 -0800 | [diff] [blame] | 2825 | rcu_read_unlock(); |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 2826 | if (err == -ELOOP) |
| 2827 | dev->stats.collisions++; |
| 2828 | else if (err == -ENETUNREACH) |
| 2829 | dev->stats.tx_carrier_errors++; |
pravin shelar | c46b789 | 2016-11-13 20:43:54 -0800 | [diff] [blame] | 2830 | dst_release(ndst); |
| 2831 | dev->stats.tx_errors++; |
| 2832 | kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 2835 | static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev, |
| 2836 | struct vxlan_fdb *f, __be32 vni, bool did_rsc) |
| 2837 | { |
| 2838 | struct vxlan_rdst nh_rdst; |
| 2839 | struct nexthop *nh; |
| 2840 | bool do_xmit; |
| 2841 | u32 hash; |
| 2842 | |
| 2843 | memset(&nh_rdst, 0, sizeof(struct vxlan_rdst)); |
| 2844 | hash = skb_get_hash(skb); |
| 2845 | |
| 2846 | rcu_read_lock(); |
| 2847 | nh = rcu_dereference(f->nh); |
| 2848 | if (!nh) { |
| 2849 | rcu_read_unlock(); |
| 2850 | goto drop; |
| 2851 | } |
| 2852 | do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst); |
| 2853 | rcu_read_unlock(); |
| 2854 | |
| 2855 | if (likely(do_xmit)) |
| 2856 | vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc); |
| 2857 | else |
| 2858 | goto drop; |
| 2859 | |
| 2860 | return; |
| 2861 | |
| 2862 | drop: |
| 2863 | dev->stats.tx_dropped++; |
| 2864 | dev_kfree_skb(skb); |
| 2865 | } |
| 2866 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2867 | /* Transmit local packets over Vxlan |
| 2868 | * |
| 2869 | * Outer IP header inherits ECN and DF from inner header. |
| 2870 | * Outer UDP destination is the VXLAN assigned port. |
| 2871 | * source port is based on hash of flow |
| 2872 | */ |
| 2873 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 2874 | { |
| 2875 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2876 | struct vxlan_rdst *rdst, *fdst = NULL; |
Xin Long | 8bff3685 | 2017-11-11 19:58:50 +0800 | [diff] [blame] | 2877 | const struct ip_tunnel_info *info; |
| 2878 | bool did_rsc = false; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2879 | struct vxlan_fdb *f; |
Xin Long | 8bff3685 | 2017-11-11 19:58:50 +0800 | [diff] [blame] | 2880 | struct ethhdr *eth; |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2881 | __be32 vni = 0; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2882 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 2883 | info = skb_tunnel_info(skb); |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 2884 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2885 | skb_reset_mac_header(skb); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2886 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2887 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2888 | if (info && info->mode & IP_TUNNEL_INFO_BRIDGE && |
| 2889 | info->mode & IP_TUNNEL_INFO_TX) { |
| 2890 | vni = tunnel_id_to_key32(info->key.tun_id); |
| 2891 | } else { |
| 2892 | if (info && info->mode & IP_TUNNEL_INFO_TX) |
| 2893 | vxlan_xmit_one(skb, dev, vni, NULL, false); |
| 2894 | else |
| 2895 | kfree_skb(skb); |
| 2896 | return NETDEV_TX_OK; |
| 2897 | } |
Jiri Benc | 47e5d1b | 2016-04-05 14:47:11 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2900 | if (vxlan->cfg.flags & VXLAN_F_PROXY) { |
Jiri Benc | 47e5d1b | 2016-04-05 14:47:11 +0200 | [diff] [blame] | 2901 | eth = eth_hdr(skb); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2902 | if (ntohs(eth->h_proto) == ETH_P_ARP) |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2903 | return arp_reduce(dev, skb, vni); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2904 | #if IS_ENABLED(CONFIG_IPV6) |
Xin Long | 8bff3685 | 2017-11-11 19:58:50 +0800 | [diff] [blame] | 2905 | else if (ntohs(eth->h_proto) == ETH_P_IPV6 && |
| 2906 | pskb_may_pull(skb, sizeof(struct ipv6hdr) + |
| 2907 | sizeof(struct nd_msg)) && |
| 2908 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { |
| 2909 | struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1); |
| 2910 | |
| 2911 | if (m->icmph.icmp6_code == 0 && |
| 2912 | m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) |
Vincent Bernat | f1fb08f | 2017-04-02 11:00:06 +0200 | [diff] [blame] | 2913 | return neigh_reduce(dev, skb, vni); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2914 | } |
| 2915 | #endif |
| 2916 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2917 | |
Jiri Benc | 47e5d1b | 2016-04-05 14:47:11 +0200 | [diff] [blame] | 2918 | eth = eth_hdr(skb); |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2919 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 2920 | did_rsc = false; |
| 2921 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2922 | if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) && |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2923 | (ntohs(eth->h_proto) == ETH_P_IP || |
| 2924 | ntohs(eth->h_proto) == ETH_P_IPV6)) { |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 2925 | did_rsc = route_shortcircuit(dev, skb); |
| 2926 | if (did_rsc) |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2927 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2930 | if (f == NULL) { |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 2931 | f = vxlan_find_mac(vxlan, all_zeros_mac, vni); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2932 | if (f == NULL) { |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 2933 | if ((vxlan->cfg.flags & VXLAN_F_L2MISS) && |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2934 | !is_multicast_ether_addr(eth->h_dest)) |
| 2935 | vxlan_fdb_miss(vxlan, eth->h_dest); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2936 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2937 | dev->stats.tx_dropped++; |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2938 | kfree_skb(skb); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2939 | return NETDEV_TX_OK; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 2940 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 2943 | if (rcu_access_pointer(f->nh)) { |
| 2944 | vxlan_xmit_nh(skb, dev, f, |
| 2945 | (vni ? : vxlan->default_dst.remote_vni), did_rsc); |
| 2946 | } else { |
| 2947 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
| 2948 | struct sk_buff *skb1; |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2949 | |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 2950 | if (!fdst) { |
| 2951 | fdst = rdst; |
| 2952 | continue; |
| 2953 | } |
| 2954 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 2955 | if (skb1) |
| 2956 | vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc); |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2957 | } |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 2958 | if (fdst) |
| 2959 | vxlan_xmit_one(skb, dev, vni, fdst, did_rsc); |
| 2960 | else |
| 2961 | kfree_skb(skb); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2962 | } |
| 2963 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2964 | return NETDEV_TX_OK; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2967 | /* Walk the forwarding table and purge stale entries */ |
Kees Cook | df7e828 | 2017-10-04 16:26:59 -0700 | [diff] [blame] | 2968 | static void vxlan_cleanup(struct timer_list *t) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2969 | { |
Kees Cook | df7e828 | 2017-10-04 16:26:59 -0700 | [diff] [blame] | 2970 | struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2971 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
| 2972 | unsigned int h; |
| 2973 | |
| 2974 | if (!netif_running(vxlan->dev)) |
| 2975 | return; |
| 2976 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2977 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 2978 | struct hlist_node *p, *n; |
Sorin Dumitru | 14e1d0f | 2015-05-26 10:42:04 +0300 | [diff] [blame] | 2979 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 2980 | spin_lock(&vxlan->hash_lock[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2981 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 2982 | struct vxlan_fdb *f |
| 2983 | = container_of(p, struct vxlan_fdb, hlist); |
| 2984 | unsigned long timeout; |
| 2985 | |
Balakrishnan Raman | efb5f68 | 2017-01-23 20:44:33 -0800 | [diff] [blame] | 2986 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2987 | continue; |
| 2988 | |
Roopa Prabhu | def499c | 2017-03-27 15:46:41 -0700 | [diff] [blame] | 2989 | if (f->flags & NTF_EXT_LEARNED) |
| 2990 | continue; |
| 2991 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 2992 | timeout = f->used + vxlan->cfg.age_interval * HZ; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2993 | if (time_before_eq(timeout, jiffies)) { |
| 2994 | netdev_dbg(vxlan->dev, |
| 2995 | "garbage collect %pM\n", |
| 2996 | f->eth_addr); |
| 2997 | f->state = NUD_STALE; |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 2998 | vxlan_fdb_destroy(vxlan, f, true, true); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2999 | } else if (time_before(timeout, next_timer)) |
| 3000 | next_timer = timeout; |
| 3001 | } |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3002 | spin_unlock(&vxlan->hash_lock[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3003 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3004 | |
| 3005 | mod_timer(&vxlan->age_timer, next_timer); |
| 3006 | } |
| 3007 | |
Mark Bloch | a53cb29 | 2017-06-02 03:24:08 +0300 | [diff] [blame] | 3008 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan) |
| 3009 | { |
| 3010 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
| 3011 | |
| 3012 | spin_lock(&vn->sock_lock); |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3013 | hlist_del_init_rcu(&vxlan->hlist4.hlist); |
| 3014 | #if IS_ENABLED(CONFIG_IPV6) |
| 3015 | hlist_del_init_rcu(&vxlan->hlist6.hlist); |
| 3016 | #endif |
Mark Bloch | a53cb29 | 2017-06-02 03:24:08 +0300 | [diff] [blame] | 3017 | spin_unlock(&vn->sock_lock); |
| 3018 | } |
| 3019 | |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3020 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan, |
| 3021 | struct vxlan_dev_node *node) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3022 | { |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3023 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 3024 | __be32 vni = vxlan->default_dst.remote_vni; |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3025 | |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3026 | node->vxlan = vxlan; |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3027 | spin_lock(&vn->sock_lock); |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3028 | hlist_add_head_rcu(&node->hlist, vni_head(vs, vni)); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3029 | spin_unlock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3032 | /* Setup stats when device is created */ |
| 3033 | static int vxlan_init(struct net_device *dev) |
| 3034 | { |
Taehee Yoo | 384d91c | 2020-03-18 13:28:09 +0000 | [diff] [blame] | 3035 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 3036 | int err; |
| 3037 | |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 3038 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 3039 | if (!dev->tstats) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3040 | return -ENOMEM; |
| 3041 | |
Taehee Yoo | 384d91c | 2020-03-18 13:28:09 +0000 | [diff] [blame] | 3042 | err = gro_cells_init(&vxlan->gro_cells, dev); |
| 3043 | if (err) { |
| 3044 | free_percpu(dev->tstats); |
| 3045 | return err; |
| 3046 | } |
| 3047 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3048 | return 0; |
| 3049 | } |
| 3050 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 3051 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni) |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3052 | { |
| 3053 | struct vxlan_fdb *f; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3054 | u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3055 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3056 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 3057 | f = __vxlan_find_mac(vxlan, all_zeros_mac, vni); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3058 | if (f) |
Petr Machata | 0e6160f | 2018-11-21 08:02:35 +0000 | [diff] [blame] | 3059 | vxlan_fdb_destroy(vxlan, f, true, true); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3060 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3061 | } |
| 3062 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 3063 | static void vxlan_uninit(struct net_device *dev) |
| 3064 | { |
| 3065 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 3066 | |
Stefano Brivio | ad6c998 | 2019-03-08 16:40:57 +0100 | [diff] [blame] | 3067 | gro_cells_destroy(&vxlan->gro_cells); |
| 3068 | |
Roopa Prabhu | 3ad7a4b | 2017-01-31 22:59:52 -0800 | [diff] [blame] | 3069 | vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3070 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 3071 | free_percpu(dev->tstats); |
| 3072 | } |
| 3073 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3074 | /* Start ageing timer and join group when device is brought up */ |
| 3075 | static int vxlan_open(struct net_device *dev) |
| 3076 | { |
| 3077 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3078 | int ret; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 3079 | |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3080 | ret = vxlan_sock_add(vxlan); |
| 3081 | if (ret < 0) |
| 3082 | return ret; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3083 | |
Gao feng | 79d4a94 | 2013-12-10 16:37:32 +0800 | [diff] [blame] | 3084 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3085 | ret = vxlan_igmp_join(vxlan); |
Marcelo Ricardo Leitner | bef0057 | 2015-08-25 20:22:35 -0300 | [diff] [blame] | 3086 | if (ret == -EADDRINUSE) |
| 3087 | ret = 0; |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3088 | if (ret) { |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3089 | vxlan_sock_release(vxlan); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3090 | return ret; |
| 3091 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 3094 | if (vxlan->cfg.age_interval) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3095 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
| 3096 | |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3097 | return ret; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /* Purge the forwarding table */ |
Roopa Prabhu | 8b3f933 | 2017-01-23 20:44:32 -0800 | [diff] [blame] | 3101 | static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3102 | { |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 3103 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3104 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3105 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 3106 | struct hlist_node *p, *n; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3107 | |
| 3108 | spin_lock_bh(&vxlan->hash_lock[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3109 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 3110 | struct vxlan_fdb *f |
| 3111 | = container_of(p, struct vxlan_fdb, hlist); |
Roopa Prabhu | 8b3f933 | 2017-01-23 20:44:32 -0800 | [diff] [blame] | 3112 | if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP))) |
| 3113 | continue; |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 3114 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
Taehee Yoo | fda2ec6 | 2020-08-01 07:07:50 +0000 | [diff] [blame] | 3115 | if (is_zero_ether_addr(f->eth_addr) && |
| 3116 | f->vni == vxlan->cfg.vni) |
| 3117 | continue; |
| 3118 | vxlan_fdb_destroy(vxlan, f, true, true); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3119 | } |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3120 | spin_unlock_bh(&vxlan->hash_lock[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3121 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | /* Cleanup timer and forwarding table on shutdown */ |
| 3125 | static int vxlan_stop(struct net_device *dev) |
| 3126 | { |
| 3127 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 3128 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3129 | int ret = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3130 | |
Marcelo Ricardo Leitner | 24c0e68 | 2015-03-23 16:23:12 -0300 | [diff] [blame] | 3131 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
WANG Cong | f13b168 | 2015-04-08 14:48:30 -0700 | [diff] [blame] | 3132 | !vxlan_group_used(vn, vxlan)) |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3133 | ret = vxlan_igmp_leave(vxlan); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3134 | |
| 3135 | del_timer_sync(&vxlan->age_timer); |
| 3136 | |
Roopa Prabhu | 8b3f933 | 2017-01-23 20:44:32 -0800 | [diff] [blame] | 3137 | vxlan_flush(vxlan, false); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3138 | vxlan_sock_release(vxlan); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3139 | |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3140 | return ret; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3143 | /* Stub, nothing needs to be done. */ |
| 3144 | static void vxlan_set_multicast_list(struct net_device *dev) |
| 3145 | { |
| 3146 | } |
| 3147 | |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 3148 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
| 3149 | { |
| 3150 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 3151 | struct vxlan_rdst *dst = &vxlan->default_dst; |
David Wragg | 72564b5 | 2016-02-10 00:05:55 +0000 | [diff] [blame] | 3152 | struct net_device *lowerdev = __dev_get_by_index(vxlan->net, |
| 3153 | dst->remote_ifindex); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3154 | bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6); |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 3155 | |
| 3156 | /* This check is different than dev->max_mtu, because it looks at |
| 3157 | * the lowerdev->mtu, rather than the static dev->max_mtu |
| 3158 | */ |
| 3159 | if (lowerdev) { |
| 3160 | int max_mtu = lowerdev->mtu - |
| 3161 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
| 3162 | if (new_mtu > max_mtu) |
| 3163 | return -EINVAL; |
| 3164 | } |
| 3165 | |
| 3166 | dev->mtu = new_mtu; |
| 3167 | return 0; |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 3168 | } |
| 3169 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 3170 | static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 3171 | { |
| 3172 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 3173 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 3174 | __be16 sport, dport; |
| 3175 | |
| 3176 | sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
| 3177 | vxlan->cfg.port_max, true); |
| 3178 | dport = info->key.tp_dst ? : vxlan->cfg.dst_port; |
| 3179 | |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3180 | if (ip_tunnel_info_af(info) == AF_INET) { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 3181 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 3182 | struct rtable *rt; |
| 3183 | |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 3184 | rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos, |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 3185 | info->key.u.ipv4.dst, |
Paolo Abeni | 22f0708 | 2017-02-17 19:14:27 +0100 | [diff] [blame] | 3186 | &info->key.u.ipv4.src, dport, sport, |
| 3187 | &info->dst_cache, info); |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 3188 | if (IS_ERR(rt)) |
| 3189 | return PTR_ERR(rt); |
| 3190 | ip_rt_put(rt); |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3191 | } else { |
| 3192 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | 03dc52a | 2016-11-13 20:43:53 -0800 | [diff] [blame] | 3193 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3194 | struct dst_entry *ndst; |
| 3195 | |
pravin shelar | 655c3de | 2016-11-13 20:43:55 -0800 | [diff] [blame] | 3196 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos, |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 3197 | info->key.label, &info->key.u.ipv6.dst, |
Paolo Abeni | 22f0708 | 2017-02-17 19:14:27 +0100 | [diff] [blame] | 3198 | &info->key.u.ipv6.src, dport, sport, |
| 3199 | &info->dst_cache, info); |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3200 | if (IS_ERR(ndst)) |
| 3201 | return PTR_ERR(ndst); |
| 3202 | dst_release(ndst); |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3203 | #else /* !CONFIG_IPV6 */ |
| 3204 | return -EPFNOSUPPORT; |
| 3205 | #endif |
| 3206 | } |
Jiri Benc | 1a8496b | 2016-02-02 18:09:14 +0100 | [diff] [blame] | 3207 | info->key.tp_src = sport; |
| 3208 | info->key.tp_dst = dport; |
Jiri Benc | 239e944 | 2015-12-07 13:04:31 +0100 | [diff] [blame] | 3209 | return 0; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
Jiri Benc | 0c867c9 | 2016-04-05 14:47:10 +0200 | [diff] [blame] | 3212 | static const struct net_device_ops vxlan_netdev_ether_ops = { |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3213 | .ndo_init = vxlan_init, |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 3214 | .ndo_uninit = vxlan_uninit, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3215 | .ndo_open = vxlan_open, |
| 3216 | .ndo_stop = vxlan_stop, |
| 3217 | .ndo_start_xmit = vxlan_xmit, |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 3218 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3219 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 3220 | .ndo_change_mtu = vxlan_change_mtu, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3221 | .ndo_validate_addr = eth_validate_addr, |
| 3222 | .ndo_set_mac_address = eth_mac_addr, |
| 3223 | .ndo_fdb_add = vxlan_fdb_add, |
| 3224 | .ndo_fdb_del = vxlan_fdb_delete, |
| 3225 | .ndo_fdb_dump = vxlan_fdb_dump, |
Roopa Prabhu | 474c3c8 | 2018-12-15 22:35:10 -0800 | [diff] [blame] | 3226 | .ndo_fdb_get = vxlan_fdb_get, |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 3227 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, |
Andy Roulin | 8f1af75 | 2019-02-22 18:06:38 +0000 | [diff] [blame] | 3228 | .ndo_change_proto_down = dev_change_proto_down_generic, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3229 | }; |
| 3230 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3231 | static const struct net_device_ops vxlan_netdev_raw_ops = { |
| 3232 | .ndo_init = vxlan_init, |
| 3233 | .ndo_uninit = vxlan_uninit, |
| 3234 | .ndo_open = vxlan_open, |
| 3235 | .ndo_stop = vxlan_stop, |
| 3236 | .ndo_start_xmit = vxlan_xmit, |
| 3237 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
| 3238 | .ndo_change_mtu = vxlan_change_mtu, |
| 3239 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, |
| 3240 | }; |
| 3241 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3242 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 3243 | static struct device_type vxlan_type = { |
| 3244 | .name = "vxlan", |
| 3245 | }; |
| 3246 | |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 3247 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 3248 | * supply the listening VXLAN udp ports. Callers are expected |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 3249 | * to implement the ndo_udp_tunnel_add. |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 3250 | */ |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 3251 | static void vxlan_offload_rx_ports(struct net_device *dev, bool push) |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 3252 | { |
| 3253 | struct vxlan_sock *vs; |
| 3254 | struct net *net = dev_net(dev); |
| 3255 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 3256 | unsigned int i; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 3257 | |
| 3258 | spin_lock(&vn->sock_lock); |
| 3259 | for (i = 0; i < PORT_HASH_SIZE; ++i) { |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 3260 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { |
| 3261 | unsigned short type; |
| 3262 | |
| 3263 | if (vs->flags & VXLAN_F_GPE) |
| 3264 | type = UDP_TUNNEL_TYPE_VXLAN_GPE; |
| 3265 | else |
| 3266 | type = UDP_TUNNEL_TYPE_VXLAN; |
| 3267 | |
| 3268 | if (push) |
| 3269 | udp_tunnel_push_rx_port(dev, vs->sock, type); |
| 3270 | else |
| 3271 | udp_tunnel_drop_rx_port(dev, vs->sock, type); |
| 3272 | } |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 3273 | } |
| 3274 | spin_unlock(&vn->sock_lock); |
| 3275 | } |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 3276 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3277 | /* Initialize the device structure. */ |
| 3278 | static void vxlan_setup(struct net_device *dev) |
| 3279 | { |
| 3280 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 3281 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3282 | |
Jiri Benc | 65226ef | 2016-04-28 16:36:30 +0200 | [diff] [blame] | 3283 | eth_hw_addr_random(dev); |
| 3284 | ether_setup(dev); |
| 3285 | |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 3286 | dev->needs_free_netdev = true; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3287 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
| 3288 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3289 | dev->features |= NETIF_F_LLTX; |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 3290 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 3291 | dev->features |= NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 3292 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 3293 | |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 3294 | dev->vlan_features = dev->features; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 3295 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 3296 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 3297 | netif_keep_dst(dev); |
Jiri Benc | 0c867c9 | 2016-04-05 14:47:10 +0200 | [diff] [blame] | 3298 | dev->priv_flags |= IFF_NO_QUEUE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3299 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3300 | /* MTU range: 68 - 65535 */ |
| 3301 | dev->min_mtu = ETH_MIN_MTU; |
| 3302 | dev->max_mtu = ETH_MAX_MTU; |
| 3303 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3304 | INIT_LIST_HEAD(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3305 | |
Kees Cook | df7e828 | 2017-10-04 16:26:59 -0700 | [diff] [blame] | 3306 | timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3307 | |
| 3308 | vxlan->dev = dev; |
| 3309 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3310 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 3311 | spin_lock_init(&vxlan->hash_lock[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3312 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 3313 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Jiri Benc | 0c867c9 | 2016-04-05 14:47:10 +0200 | [diff] [blame] | 3316 | static void vxlan_ether_setup(struct net_device *dev) |
| 3317 | { |
Jiri Benc | 0c867c9 | 2016-04-05 14:47:10 +0200 | [diff] [blame] | 3318 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 3319 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 3320 | dev->netdev_ops = &vxlan_netdev_ether_ops; |
| 3321 | } |
| 3322 | |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3323 | static void vxlan_raw_setup(struct net_device *dev) |
| 3324 | { |
Jiri Benc | 65226ef | 2016-04-28 16:36:30 +0200 | [diff] [blame] | 3325 | dev->header_ops = NULL; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3326 | dev->type = ARPHRD_NONE; |
| 3327 | dev->hard_header_len = 0; |
| 3328 | dev->addr_len = 0; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3329 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 3330 | dev->netdev_ops = &vxlan_netdev_raw_ops; |
| 3331 | } |
| 3332 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3333 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
| 3334 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 3335 | [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) }, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3336 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3337 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 3338 | [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) }, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3339 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3340 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
| 3341 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 3342 | [IFLA_VXLAN_LABEL] = { .type = NLA_U32 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3343 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
| 3344 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
| 3345 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 3346 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 3347 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
| 3348 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, |
| 3349 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, |
| 3350 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, |
Alexei Starovoitov | f8a9b1b | 2015-07-30 20:10:22 -0700 | [diff] [blame] | 3351 | [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 }, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 3352 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
Tom Herbert | 5c91ae0 | 2014-11-06 18:06:01 -0800 | [diff] [blame] | 3353 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
| 3354 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 3355 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 3356 | [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, |
| 3357 | [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 3358 | [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, }, |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3359 | [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, }, |
Tom Herbert | 0ace2ca | 2015-02-10 16:30:32 -0800 | [diff] [blame] | 3360 | [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG }, |
Hangbin Liu | 72f6d71 | 2018-04-17 14:11:28 +0800 | [diff] [blame] | 3361 | [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG }, |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 3362 | [IFLA_VXLAN_DF] = { .type = NLA_U8 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3363 | }; |
| 3364 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 3365 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[], |
| 3366 | struct netlink_ext_ack *extack) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3367 | { |
| 3368 | if (tb[IFLA_ADDRESS]) { |
| 3369 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3370 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 3371 | "Provided link layer address is not Ethernet"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3372 | return -EINVAL; |
| 3373 | } |
| 3374 | |
| 3375 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3376 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 3377 | "Provided Ethernet address is not unicast"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3378 | return -EADDRNOTAVAIL; |
| 3379 | } |
| 3380 | } |
| 3381 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3382 | if (tb[IFLA_MTU]) { |
Matthias Schiffer | 019b13a | 2017-06-27 14:42:43 +0200 | [diff] [blame] | 3383 | u32 mtu = nla_get_u32(tb[IFLA_MTU]); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3384 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3385 | if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) { |
| 3386 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU], |
| 3387 | "MTU must be between 68 and 65535"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3388 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3389 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3390 | } |
| 3391 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3392 | if (!data) { |
| 3393 | NL_SET_ERR_MSG(extack, |
| 3394 | "Required attributes not provided to perform the operation"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3395 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3396 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3397 | |
| 3398 | if (data[IFLA_VXLAN_ID]) { |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3399 | u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
| 3400 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3401 | if (id >= VXLAN_N_VID) { |
Sabrina Dubroca | cc8e7c6 | 2020-04-22 17:29:50 +0200 | [diff] [blame] | 3402 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID], |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3403 | "VXLAN ID must be lower than 16777216"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3404 | return -ERANGE; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3405 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 3408 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 3409 | const struct ifla_vxlan_port_range *p |
| 3410 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 3411 | |
| 3412 | if (ntohs(p->high) < ntohs(p->low)) { |
Sabrina Dubroca | cc8e7c6 | 2020-04-22 17:29:50 +0200 | [diff] [blame] | 3413 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE], |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3414 | "Invalid source port range"); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 3415 | return -EINVAL; |
| 3416 | } |
| 3417 | } |
| 3418 | |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 3419 | if (data[IFLA_VXLAN_DF]) { |
| 3420 | enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]); |
| 3421 | |
| 3422 | if (df < 0 || df > VXLAN_DF_MAX) { |
Sabrina Dubroca | cc8e7c6 | 2020-04-22 17:29:50 +0200 | [diff] [blame] | 3423 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF], |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 3424 | "Invalid DF attribute"); |
| 3425 | return -EINVAL; |
| 3426 | } |
| 3427 | } |
| 3428 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3429 | return 0; |
| 3430 | } |
| 3431 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 3432 | static void vxlan_get_drvinfo(struct net_device *netdev, |
| 3433 | struct ethtool_drvinfo *drvinfo) |
| 3434 | { |
| 3435 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); |
| 3436 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); |
| 3437 | } |
| 3438 | |
Matthias Schiffer | 36fe3a6 | 2019-11-12 22:12:24 +0100 | [diff] [blame] | 3439 | static int vxlan_get_link_ksettings(struct net_device *dev, |
| 3440 | struct ethtool_link_ksettings *cmd) |
| 3441 | { |
| 3442 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 3443 | struct vxlan_rdst *dst = &vxlan->default_dst; |
| 3444 | struct net_device *lowerdev = __dev_get_by_index(vxlan->net, |
| 3445 | dst->remote_ifindex); |
| 3446 | |
| 3447 | if (!lowerdev) { |
| 3448 | cmd->base.duplex = DUPLEX_UNKNOWN; |
| 3449 | cmd->base.port = PORT_OTHER; |
| 3450 | cmd->base.speed = SPEED_UNKNOWN; |
| 3451 | |
| 3452 | return 0; |
| 3453 | } |
| 3454 | |
| 3455 | return __ethtool_get_link_ksettings(lowerdev, cmd); |
| 3456 | } |
| 3457 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 3458 | static const struct ethtool_ops vxlan_ethtool_ops = { |
Matthias Schiffer | 36fe3a6 | 2019-11-12 22:12:24 +0100 | [diff] [blame] | 3459 | .get_drvinfo = vxlan_get_drvinfo, |
| 3460 | .get_link = ethtool_op_get_link, |
| 3461 | .get_link_ksettings = vxlan_get_link_ksettings, |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 3462 | }; |
| 3463 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3464 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3465 | __be16 port, u32 flags, int ifindex) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3466 | { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3467 | struct socket *sock; |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3468 | struct udp_port_cfg udp_conf; |
| 3469 | int err; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3470 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3471 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 3472 | |
| 3473 | if (ipv6) { |
| 3474 | udp_conf.family = AF_INET6; |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3475 | udp_conf.use_udp6_rx_checksums = |
Alexander Duyck | 3dc2b6a | 2014-11-24 20:08:38 -0800 | [diff] [blame] | 3476 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); |
Jiri Benc | a43a9ef | 2015-08-28 20:48:22 +0200 | [diff] [blame] | 3477 | udp_conf.ipv6_v6only = 1; |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3478 | } else { |
| 3479 | udp_conf.family = AF_INET; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3480 | } |
| 3481 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3482 | udp_conf.local_udp_port = port; |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3483 | udp_conf.bind_ifindex = ifindex; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3484 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 3485 | /* Open UDP socket */ |
| 3486 | err = udp_sock_create(net, &udp_conf, &sock); |
| 3487 | if (err < 0) |
| 3488 | return ERR_PTR(err); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3489 | |
Zhi Yong Wu | 39deb2c | 2013-10-28 14:01:48 +0800 | [diff] [blame] | 3490 | return sock; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* Create new listen socket if needed */ |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3494 | static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6, |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3495 | __be16 port, u32 flags, |
| 3496 | int ifindex) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3497 | { |
| 3498 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 3499 | struct vxlan_sock *vs; |
| 3500 | struct socket *sock; |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 3501 | unsigned int h; |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 3502 | struct udp_tunnel_sock_cfg tunnel_cfg; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3503 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 3504 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3505 | if (!vs) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3506 | return ERR_PTR(-ENOMEM); |
| 3507 | |
| 3508 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 3509 | INIT_HLIST_HEAD(&vs->vni_list[h]); |
| 3510 | |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3511 | sock = vxlan_create_sock(net, ipv6, port, flags, ifindex); |
Zhi Yong Wu | 39deb2c | 2013-10-28 14:01:48 +0800 | [diff] [blame] | 3512 | if (IS_ERR(sock)) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3513 | kfree(vs); |
Duan Jiong | e50fddc | 2013-11-01 13:09:43 +0800 | [diff] [blame] | 3514 | return ERR_CAST(sock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3517 | vs->sock = sock; |
Reshetova, Elena | 66af846 | 2017-07-04 15:52:59 +0300 | [diff] [blame] | 3518 | refcount_set(&vs->refcnt, 1); |
Tom Herbert | af33c1a | 2015-01-20 11:23:05 -0800 | [diff] [blame] | 3519 | vs->flags = (flags & VXLAN_F_RCV_FLAGS); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3520 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3521 | spin_lock(&vn->sock_lock); |
| 3522 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 3523 | udp_tunnel_notify_add_rx_port(sock, |
Alexander Duyck | b9adcd69 | 2016-06-16 12:23:19 -0700 | [diff] [blame] | 3524 | (vs->flags & VXLAN_F_GPE) ? |
| 3525 | UDP_TUNNEL_TYPE_VXLAN_GPE : |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 3526 | UDP_TUNNEL_TYPE_VXLAN); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3527 | spin_unlock(&vn->sock_lock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3528 | |
| 3529 | /* Mark socket as an encapsulation socket. */ |
Tom Herbert | 5602c48 | 2016-04-05 08:22:53 -0700 | [diff] [blame] | 3530 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 3531 | tunnel_cfg.sk_user_data = vs; |
| 3532 | tunnel_cfg.encap_type = 1; |
Jiri Benc | f2d1968ec | 2016-02-23 18:02:58 +0100 | [diff] [blame] | 3533 | tunnel_cfg.encap_rcv = vxlan_rcv; |
Stefano Brivio | c3a43b9 | 2018-11-08 12:19:15 +0100 | [diff] [blame] | 3534 | tunnel_cfg.encap_err_lookup = vxlan_err_lookup; |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 3535 | tunnel_cfg.encap_destroy = NULL; |
Tom Herbert | 5602c48 | 2016-04-05 08:22:53 -0700 | [diff] [blame] | 3536 | tunnel_cfg.gro_receive = vxlan_gro_receive; |
| 3537 | tunnel_cfg.gro_complete = vxlan_gro_complete; |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 3538 | |
| 3539 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 3540 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3541 | return vs; |
| 3542 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3543 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3544 | static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3545 | { |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3546 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
| 3547 | struct vxlan_sock *vs = NULL; |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3548 | struct vxlan_dev_node *node; |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3549 | int l3mdev_index = 0; |
| 3550 | |
| 3551 | if (vxlan->cfg.remote_ifindex) |
| 3552 | l3mdev_index = l3mdev_master_upper_ifindex_by_index( |
| 3553 | vxlan->net, vxlan->cfg.remote_ifindex); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 3554 | |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3555 | if (!vxlan->cfg.no_share) { |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3556 | spin_lock(&vn->sock_lock); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3557 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3558 | vxlan->cfg.dst_port, vxlan->cfg.flags, |
| 3559 | l3mdev_index); |
Reshetova, Elena | 66af846 | 2017-07-04 15:52:59 +0300 | [diff] [blame] | 3560 | if (vs && !refcount_inc_not_zero(&vs->refcnt)) { |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3561 | spin_unlock(&vn->sock_lock); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3562 | return -EBUSY; |
Marcelo Ricardo Leitner | 56ef9c9 | 2015-03-18 14:50:44 -0300 | [diff] [blame] | 3563 | } |
| 3564 | spin_unlock(&vn->sock_lock); |
| 3565 | } |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3566 | if (!vs) |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3567 | vs = vxlan_socket_create(vxlan->net, ipv6, |
Alexis Bauvin | aab8cc3 | 2018-12-03 10:54:40 +0100 | [diff] [blame] | 3568 | vxlan->cfg.dst_port, vxlan->cfg.flags, |
| 3569 | l3mdev_index); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3570 | if (IS_ERR(vs)) |
| 3571 | return PTR_ERR(vs); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3572 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3573 | if (ipv6) { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 3574 | rcu_assign_pointer(vxlan->vn6_sock, vs); |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3575 | node = &vxlan->hlist6; |
| 3576 | } else |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3577 | #endif |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3578 | { |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 3579 | rcu_assign_pointer(vxlan->vn4_sock, vs); |
Jiri Benc | 69e7666 | 2017-07-02 19:00:57 +0200 | [diff] [blame] | 3580 | node = &vxlan->hlist4; |
| 3581 | } |
| 3582 | vxlan_vs_add_dev(vs, vxlan, node); |
Jiri Benc | 205f356 | 2015-09-24 13:50:01 +0200 | [diff] [blame] | 3583 | return 0; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3586 | static int vxlan_sock_add(struct vxlan_dev *vxlan) |
| 3587 | { |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 3588 | bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA; |
| 3589 | bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata; |
Jiri Benc | d074bf9 | 2017-04-27 21:24:35 +0200 | [diff] [blame] | 3590 | bool ipv4 = !ipv6 || metadata; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3591 | int ret = 0; |
| 3592 | |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 3593 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3594 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | c6fcc4f | 2016-10-28 09:59:15 -0700 | [diff] [blame] | 3595 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
Jiri Benc | d074bf9 | 2017-04-27 21:24:35 +0200 | [diff] [blame] | 3596 | if (ipv6) { |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3597 | ret = __vxlan_sock_add(vxlan, true); |
Jiri Benc | d074bf9 | 2017-04-27 21:24:35 +0200 | [diff] [blame] | 3598 | if (ret < 0 && ret != -EAFNOSUPPORT) |
| 3599 | ipv4 = false; |
| 3600 | } |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3601 | #endif |
Jiri Benc | d074bf9 | 2017-04-27 21:24:35 +0200 | [diff] [blame] | 3602 | if (ipv4) |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3603 | ret = __vxlan_sock_add(vxlan, false); |
| 3604 | if (ret < 0) |
| 3605 | vxlan_sock_release(vxlan); |
| 3606 | return ret; |
| 3607 | } |
| 3608 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3609 | static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf, |
| 3610 | struct net_device **lower, |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3611 | struct vxlan_dev *old, |
| 3612 | struct netlink_ext_ack *extack) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3613 | { |
Nicolas Dichtel | 33564bb | 2015-01-26 22:28:14 +0100 | [diff] [blame] | 3614 | struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3615 | struct vxlan_dev *tmp; |
| 3616 | bool use_ipv6 = false; |
| 3617 | |
| 3618 | if (conf->flags & VXLAN_F_GPE) { |
| 3619 | /* For now, allow GPE only together with |
| 3620 | * COLLECT_METADATA. This can be relaxed later; in such |
| 3621 | * case, the other side of the PtP link will have to be |
| 3622 | * provided. |
| 3623 | */ |
| 3624 | if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) || |
| 3625 | !(conf->flags & VXLAN_F_COLLECT_METADATA)) { |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3626 | NL_SET_ERR_MSG(extack, |
| 3627 | "VXLAN GPE does not support this combination of attributes"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3628 | return -EINVAL; |
| 3629 | } |
| 3630 | } |
| 3631 | |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3632 | if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) { |
| 3633 | /* Unless IPv6 is explicitly requested, assume IPv4 */ |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3634 | conf->remote_ip.sa.sa_family = AF_INET; |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3635 | conf->saddr.sa.sa_family = AF_INET; |
| 3636 | } else if (!conf->remote_ip.sa.sa_family) { |
| 3637 | conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family; |
| 3638 | } else if (!conf->saddr.sa.sa_family) { |
| 3639 | conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family; |
| 3640 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3641 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3642 | if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) { |
| 3643 | NL_SET_ERR_MSG(extack, |
| 3644 | "Local and remote address must be from the same family"); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3645 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3646 | } |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3647 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3648 | if (vxlan_addr_multicast(&conf->saddr)) { |
| 3649 | NL_SET_ERR_MSG(extack, "Local address cannot be multicast"); |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3650 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3651 | } |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3652 | |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3653 | if (conf->saddr.sa.sa_family == AF_INET6) { |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3654 | if (!IS_ENABLED(CONFIG_IPV6)) { |
| 3655 | NL_SET_ERR_MSG(extack, |
| 3656 | "IPv6 support not enabled in the kernel"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3657 | return -EPFNOSUPPORT; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3658 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3659 | use_ipv6 = true; |
| 3660 | conf->flags |= VXLAN_F_IPV6; |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3661 | |
| 3662 | if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) { |
| 3663 | int local_type = |
| 3664 | ipv6_addr_type(&conf->saddr.sin6.sin6_addr); |
| 3665 | int remote_type = |
| 3666 | ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr); |
| 3667 | |
| 3668 | if (local_type & IPV6_ADDR_LINKLOCAL) { |
| 3669 | if (!(remote_type & IPV6_ADDR_LINKLOCAL) && |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3670 | (remote_type != IPV6_ADDR_ANY)) { |
| 3671 | NL_SET_ERR_MSG(extack, |
| 3672 | "Invalid combination of local and remote address scopes"); |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3673 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3674 | } |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3675 | |
| 3676 | conf->flags |= VXLAN_F_IPV6_LINKLOCAL; |
| 3677 | } else { |
| 3678 | if (remote_type == |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3679 | (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) { |
| 3680 | NL_SET_ERR_MSG(extack, |
| 3681 | "Invalid combination of local and remote address scopes"); |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3682 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3683 | } |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3684 | |
| 3685 | conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL; |
| 3686 | } |
| 3687 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3688 | } |
| 3689 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3690 | if (conf->label && !use_ipv6) { |
| 3691 | NL_SET_ERR_MSG(extack, |
| 3692 | "Label attribute only applies to IPv6 VXLAN devices"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3693 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3694 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (conf->remote_ifindex) { |
| 3697 | struct net_device *lowerdev; |
| 3698 | |
| 3699 | lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex); |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3700 | if (!lowerdev) { |
| 3701 | NL_SET_ERR_MSG(extack, |
| 3702 | "Invalid local interface, device not found"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3703 | return -ENODEV; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3704 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3705 | |
| 3706 | #if IS_ENABLED(CONFIG_IPV6) |
| 3707 | if (use_ipv6) { |
| 3708 | struct inet6_dev *idev = __in6_dev_get(lowerdev); |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3709 | if (idev && idev->cnf.disable_ipv6) { |
| 3710 | NL_SET_ERR_MSG(extack, |
| 3711 | "IPv6 support disabled by administrator"); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3712 | return -EPERM; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3713 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3714 | } |
| 3715 | #endif |
| 3716 | |
| 3717 | *lower = lowerdev; |
| 3718 | } else { |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3719 | if (vxlan_addr_multicast(&conf->remote_ip)) { |
| 3720 | NL_SET_ERR_MSG(extack, |
| 3721 | "Local interface required for multicast remote destination"); |
| 3722 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3723 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3724 | } |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3725 | |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3726 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3727 | if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) { |
| 3728 | NL_SET_ERR_MSG(extack, |
| 3729 | "Local interface required for link-local local/remote addresses"); |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3730 | return -EINVAL; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3731 | } |
Matthias Schiffer | 0f22a3c | 2017-06-19 10:03:58 +0200 | [diff] [blame] | 3732 | #endif |
| 3733 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3734 | *lower = NULL; |
| 3735 | } |
| 3736 | |
| 3737 | if (!conf->dst_port) { |
| 3738 | if (conf->flags & VXLAN_F_GPE) |
| 3739 | conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */ |
| 3740 | else |
| 3741 | conf->dst_port = htons(vxlan_port); |
| 3742 | } |
| 3743 | |
| 3744 | if (!conf->age_interval) |
| 3745 | conf->age_interval = FDB_AGE_DEFAULT; |
| 3746 | |
| 3747 | list_for_each_entry(tmp, &vn->vxlan_list, next) { |
| 3748 | if (tmp == old) |
| 3749 | continue; |
| 3750 | |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 3751 | if (tmp->cfg.vni != conf->vni) |
| 3752 | continue; |
| 3753 | if (tmp->cfg.dst_port != conf->dst_port) |
| 3754 | continue; |
| 3755 | if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) != |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3756 | (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6))) |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 3757 | continue; |
| 3758 | |
| 3759 | if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) && |
| 3760 | tmp->cfg.remote_ifindex != conf->remote_ifindex) |
| 3761 | continue; |
| 3762 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3763 | NL_SET_ERR_MSG(extack, |
| 3764 | "A VXLAN device with the specified VNI already exists"); |
Matthias Schiffer | 49f810f | 2017-06-19 10:04:00 +0200 | [diff] [blame] | 3765 | return -EEXIST; |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | return 0; |
| 3769 | } |
| 3770 | |
| 3771 | static void vxlan_config_apply(struct net_device *dev, |
| 3772 | struct vxlan_config *conf, |
Sabrina Dubroca | 889ce93 | 2017-06-30 15:50:00 +0200 | [diff] [blame] | 3773 | struct net_device *lowerdev, |
| 3774 | struct net *src_net, |
| 3775 | bool changelink) |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3776 | { |
| 3777 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 3778 | struct vxlan_rdst *dst = &vxlan->default_dst; |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3779 | unsigned short needed_headroom = ETH_HLEN; |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3780 | bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6); |
| 3781 | int max_mtu = ETH_MAX_MTU; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3782 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3783 | if (!changelink) { |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3784 | if (conf->flags & VXLAN_F_GPE) |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3785 | vxlan_raw_setup(dev); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3786 | else |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3787 | vxlan_ether_setup(dev); |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3788 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3789 | if (conf->mtu) |
| 3790 | dev->mtu = conf->mtu; |
Sabrina Dubroca | 889ce93 | 2017-06-30 15:50:00 +0200 | [diff] [blame] | 3791 | |
| 3792 | vxlan->net = src_net; |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 3793 | } |
Jiri Benc | 0c867c9 | 2016-04-05 14:47:10 +0200 | [diff] [blame] | 3794 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 3795 | dst->remote_vni = conf->vni; |
| 3796 | |
| 3797 | memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3798 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3799 | if (lowerdev) { |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 3800 | dst->remote_ifindex = conf->remote_ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3801 | |
Felix Manlunas | d6acfeb | 2017-03-29 17:56:43 -0700 | [diff] [blame] | 3802 | dev->gso_max_size = lowerdev->gso_max_size; |
| 3803 | dev->gso_max_segs = lowerdev->gso_max_segs; |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3804 | |
| 3805 | needed_headroom = lowerdev->hard_header_len; |
| 3806 | |
| 3807 | max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : |
| 3808 | VXLAN_HEADROOM); |
Alexey Kodanev | f870c1f | 2017-12-14 20:20:00 +0300 | [diff] [blame] | 3809 | if (max_mtu < ETH_MIN_MTU) |
| 3810 | max_mtu = ETH_MIN_MTU; |
| 3811 | |
| 3812 | if (!changelink && !conf->mtu) |
| 3813 | dev->mtu = max_mtu; |
Felix Manlunas | d6acfeb | 2017-03-29 17:56:43 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3816 | if (dev->mtu > max_mtu) |
| 3817 | dev->mtu = max_mtu; |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 3818 | |
Jiri Benc | b1be00a | 2015-09-24 13:50:02 +0200 | [diff] [blame] | 3819 | if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA) |
| 3820 | needed_headroom += VXLAN6_HEADROOM; |
| 3821 | else |
| 3822 | needed_headroom += VXLAN_HEADROOM; |
| 3823 | dev->needed_headroom = needed_headroom; |
| 3824 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 3825 | memcpy(&vxlan->cfg, conf, sizeof(*conf)); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3826 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3827 | |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3828 | static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3829 | struct vxlan_config *conf, bool changelink, |
| 3830 | struct netlink_ext_ack *extack) |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3831 | { |
| 3832 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 3833 | struct net_device *lowerdev; |
| 3834 | int ret; |
Vincent Bernat | afb9718 | 2012-10-30 10:27:16 +0000 | [diff] [blame] | 3835 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3836 | ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack); |
Matthias Schiffer | a985343 | 2017-06-19 10:03:55 +0200 | [diff] [blame] | 3837 | if (ret) |
| 3838 | return ret; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3839 | |
Sabrina Dubroca | 889ce93 | 2017-06-30 15:50:00 +0200 | [diff] [blame] | 3840 | vxlan_config_apply(dev, conf, lowerdev, src_net, changelink); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 3841 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3842 | return 0; |
| 3843 | } |
| 3844 | |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3845 | static int __vxlan_dev_create(struct net *net, struct net_device *dev, |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3846 | struct vxlan_config *conf, |
| 3847 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3848 | { |
| 3849 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 3850 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3851 | struct net_device *remote_dev = NULL; |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3852 | struct vxlan_fdb *f = NULL; |
Petr Machata | 6db9246 | 2018-12-18 13:16:00 +0000 | [diff] [blame] | 3853 | bool unregister = false; |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3854 | struct vxlan_rdst *dst; |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3855 | int err; |
| 3856 | |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3857 | dst = &vxlan->default_dst; |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 3858 | err = vxlan_dev_configure(net, dev, conf, false, extack); |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3859 | if (err) |
| 3860 | return err; |
| 3861 | |
| 3862 | dev->ethtool_ops = &vxlan_ethtool_ops; |
| 3863 | |
| 3864 | /* create an fdb entry for a valid default destination */ |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3865 | if (!vxlan_addr_any(&dst->remote_ip)) { |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3866 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3867 | &dst->remote_ip, |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3868 | NUD_REACHABLE | NUD_PERMANENT, |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3869 | vxlan->cfg.dst_port, |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3870 | dst->remote_vni, |
| 3871 | dst->remote_vni, |
| 3872 | dst->remote_ifindex, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 3873 | NTF_SELF, 0, &f, extack); |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3874 | if (err) |
| 3875 | return err; |
| 3876 | } |
| 3877 | |
| 3878 | err = register_netdevice(dev); |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3879 | if (err) |
| 3880 | goto errout; |
Petr Machata | 6db9246 | 2018-12-18 13:16:00 +0000 | [diff] [blame] | 3881 | unregister = true; |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3882 | |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3883 | if (dst->remote_ifindex) { |
| 3884 | remote_dev = __dev_get_by_index(net, dst->remote_ifindex); |
| 3885 | if (!remote_dev) |
| 3886 | goto errout; |
| 3887 | |
| 3888 | err = netdev_upper_dev_link(remote_dev, dev, extack); |
| 3889 | if (err) |
| 3890 | goto errout; |
| 3891 | } |
| 3892 | |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3893 | err = rtnl_configure_link(dev, NULL); |
Petr Machata | 6db9246 | 2018-12-18 13:16:00 +0000 | [diff] [blame] | 3894 | if (err) |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3895 | goto unlink; |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3896 | |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 3897 | if (f) { |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3898 | vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f); |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 3899 | |
| 3900 | /* notify default fdb entry */ |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 3901 | err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 3902 | RTM_NEWNEIGH, true, extack); |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 3903 | if (err) { |
| 3904 | vxlan_fdb_destroy(vxlan, f, false, false); |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3905 | if (remote_dev) |
| 3906 | netdev_upper_dev_unlink(remote_dev, dev); |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 3907 | goto unregister; |
| 3908 | } |
Petr Machata | 61f46fe | 2019-01-16 23:06:38 +0000 | [diff] [blame] | 3909 | } |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3910 | |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3911 | list_add(&vxlan->next, &vn->vxlan_list); |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3912 | if (remote_dev) |
| 3913 | dst->remote_dev = remote_dev; |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3914 | return 0; |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 3915 | unlink: |
| 3916 | if (remote_dev) |
| 3917 | netdev_upper_dev_unlink(remote_dev, dev); |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3918 | errout: |
Petr Machata | 6db9246 | 2018-12-18 13:16:00 +0000 | [diff] [blame] | 3919 | /* unregister_netdevice() destroys the default FDB entry with deletion |
| 3920 | * notification. But the addition notification was not sent yet, so |
| 3921 | * destroy the entry by hand here. |
| 3922 | */ |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3923 | if (f) |
Taehee Yoo | 7c31e54 | 2019-06-28 14:07:25 +0900 | [diff] [blame] | 3924 | __vxlan_fdb_free(f); |
| 3925 | unregister: |
Petr Machata | 6db9246 | 2018-12-18 13:16:00 +0000 | [diff] [blame] | 3926 | if (unregister) |
| 3927 | unregister_netdevice(dev); |
Roopa Prabhu | 0241b83 | 2018-07-04 16:46:32 -0700 | [diff] [blame] | 3928 | return err; |
Nicolas Dichtel | c80498e | 2017-03-13 16:24:03 +0100 | [diff] [blame] | 3929 | } |
| 3930 | |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3931 | /* Set/clear flags based on attribute */ |
| 3932 | static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[], |
| 3933 | int attrtype, unsigned long mask, bool changelink, |
| 3934 | bool changelink_supported, |
| 3935 | struct netlink_ext_ack *extack) |
| 3936 | { |
| 3937 | unsigned long flags; |
| 3938 | |
| 3939 | if (!tb[attrtype]) |
| 3940 | return 0; |
| 3941 | |
| 3942 | if (changelink && !changelink_supported) { |
| 3943 | vxlan_flag_attr_error(attrtype, extack); |
| 3944 | return -EOPNOTSUPP; |
| 3945 | } |
| 3946 | |
| 3947 | if (vxlan_policy[attrtype].type == NLA_FLAG) |
| 3948 | flags = conf->flags | mask; |
| 3949 | else if (nla_get_u8(tb[attrtype])) |
| 3950 | flags = conf->flags | mask; |
| 3951 | else |
| 3952 | flags = conf->flags & ~mask; |
| 3953 | |
| 3954 | conf->flags = flags; |
| 3955 | |
| 3956 | return 0; |
| 3957 | } |
| 3958 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3959 | static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], |
| 3960 | struct net_device *dev, struct vxlan_config *conf, |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3961 | bool changelink, struct netlink_ext_ack *extack) |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3962 | { |
| 3963 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3964 | int err = 0; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3965 | |
| 3966 | memset(conf, 0, sizeof(*conf)); |
| 3967 | |
| 3968 | /* if changelink operation, start with old existing cfg */ |
| 3969 | if (changelink) |
| 3970 | memcpy(conf, &vxlan->cfg, sizeof(*conf)); |
| 3971 | |
| 3972 | if (data[IFLA_VXLAN_ID]) { |
| 3973 | __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); |
| 3974 | |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3975 | if (changelink && (vni != conf->vni)) { |
| 3976 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3977 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3978 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3979 | conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); |
| 3980 | } |
| 3981 | |
| 3982 | if (data[IFLA_VXLAN_GROUP]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3983 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) { |
| 3984 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group"); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3985 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3986 | } |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3987 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3988 | conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3989 | conf->remote_ip.sa.sa_family = AF_INET; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3990 | } else if (data[IFLA_VXLAN_GROUP6]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3991 | if (!IS_ENABLED(CONFIG_IPV6)) { |
| 3992 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3993 | return -EPFNOSUPPORT; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3994 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 3995 | |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3996 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) { |
| 3997 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group"); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 3998 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 3999 | } |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 4000 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4001 | conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]); |
| 4002 | conf->remote_ip.sa.sa_family = AF_INET6; |
| 4003 | } |
| 4004 | |
| 4005 | if (data[IFLA_VXLAN_LOCAL]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4006 | if (changelink && (conf->saddr.sa.sa_family != AF_INET)) { |
| 4007 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old"); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 4008 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4009 | } |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 4010 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4011 | conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]); |
| 4012 | conf->saddr.sa.sa_family = AF_INET; |
| 4013 | } else if (data[IFLA_VXLAN_LOCAL6]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4014 | if (!IS_ENABLED(CONFIG_IPV6)) { |
| 4015 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4016 | return -EPFNOSUPPORT; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4017 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4018 | |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4019 | if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) { |
| 4020 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old"); |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 4021 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4022 | } |
Matthias Schiffer | ce44a4a | 2017-06-19 10:03:57 +0200 | [diff] [blame] | 4023 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4024 | /* TODO: respect scope id */ |
| 4025 | conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]); |
| 4026 | conf->saddr.sa.sa_family = AF_INET6; |
| 4027 | } |
| 4028 | |
| 4029 | if (data[IFLA_VXLAN_LINK]) |
| 4030 | conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]); |
| 4031 | |
| 4032 | if (data[IFLA_VXLAN_TOS]) |
| 4033 | conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
| 4034 | |
| 4035 | if (data[IFLA_VXLAN_TTL]) |
| 4036 | conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
| 4037 | |
Hangbin Liu | 72f6d71 | 2018-04-17 14:11:28 +0800 | [diff] [blame] | 4038 | if (data[IFLA_VXLAN_TTL_INHERIT]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4039 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT, |
| 4040 | VXLAN_F_TTL_INHERIT, changelink, false, |
| 4041 | extack); |
| 4042 | if (err) |
| 4043 | return err; |
| 4044 | |
Hangbin Liu | 72f6d71 | 2018-04-17 14:11:28 +0800 | [diff] [blame] | 4045 | } |
| 4046 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4047 | if (data[IFLA_VXLAN_LABEL]) |
| 4048 | conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) & |
| 4049 | IPV6_FLOWLABEL_MASK; |
| 4050 | |
| 4051 | if (data[IFLA_VXLAN_LEARNING]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4052 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING, |
| 4053 | VXLAN_F_LEARN, changelink, true, |
| 4054 | extack); |
| 4055 | if (err) |
| 4056 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4057 | } else if (!changelink) { |
| 4058 | /* default to learn on a new device */ |
| 4059 | conf->flags |= VXLAN_F_LEARN; |
| 4060 | } |
| 4061 | |
Ido Schimmel | 40051c4 | 2018-11-21 08:02:40 +0000 | [diff] [blame] | 4062 | if (data[IFLA_VXLAN_AGEING]) |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4063 | conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4064 | |
| 4065 | if (data[IFLA_VXLAN_PROXY]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4066 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY, |
| 4067 | VXLAN_F_PROXY, changelink, false, |
| 4068 | extack); |
| 4069 | if (err) |
| 4070 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | if (data[IFLA_VXLAN_RSC]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4074 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC, |
| 4075 | VXLAN_F_RSC, changelink, false, |
| 4076 | extack); |
| 4077 | if (err) |
| 4078 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | if (data[IFLA_VXLAN_L2MISS]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4082 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS, |
| 4083 | VXLAN_F_L2MISS, changelink, false, |
| 4084 | extack); |
| 4085 | if (err) |
| 4086 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | if (data[IFLA_VXLAN_L3MISS]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4090 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS, |
| 4091 | VXLAN_F_L3MISS, changelink, false, |
| 4092 | extack); |
| 4093 | if (err) |
| 4094 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | if (data[IFLA_VXLAN_LIMIT]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4098 | if (changelink) { |
| 4099 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT], |
| 4100 | "Cannot change limit"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4101 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4102 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4103 | conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
| 4104 | } |
| 4105 | |
| 4106 | if (data[IFLA_VXLAN_COLLECT_METADATA]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4107 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA, |
| 4108 | VXLAN_F_COLLECT_METADATA, changelink, false, |
| 4109 | extack); |
| 4110 | if (err) |
| 4111 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 4115 | if (!changelink) { |
| 4116 | const struct ifla_vxlan_port_range *p |
| 4117 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 4118 | conf->port_min = ntohs(p->low); |
| 4119 | conf->port_max = ntohs(p->high); |
| 4120 | } else { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4121 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE], |
| 4122 | "Cannot change port range"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4123 | return -EOPNOTSUPP; |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | if (data[IFLA_VXLAN_PORT]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4128 | if (changelink) { |
| 4129 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT], |
| 4130 | "Cannot change port"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4131 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4132 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4133 | conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); |
| 4134 | } |
| 4135 | |
| 4136 | if (data[IFLA_VXLAN_UDP_CSUM]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4137 | if (changelink) { |
| 4138 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM], |
| 4139 | "Cannot change UDP_CSUM flag"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4140 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4141 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4142 | if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) |
| 4143 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX; |
| 4144 | } |
| 4145 | |
| 4146 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4147 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
| 4148 | VXLAN_F_UDP_ZERO_CSUM6_TX, changelink, |
| 4149 | false, extack); |
| 4150 | if (err) |
| 4151 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4155 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
| 4156 | VXLAN_F_UDP_ZERO_CSUM6_RX, changelink, |
| 4157 | false, extack); |
| 4158 | if (err) |
| 4159 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | if (data[IFLA_VXLAN_REMCSUM_TX]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4163 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX, |
| 4164 | VXLAN_F_REMCSUM_TX, changelink, false, |
| 4165 | extack); |
| 4166 | if (err) |
| 4167 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | if (data[IFLA_VXLAN_REMCSUM_RX]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4171 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX, |
| 4172 | VXLAN_F_REMCSUM_RX, changelink, false, |
| 4173 | extack); |
| 4174 | if (err) |
| 4175 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | if (data[IFLA_VXLAN_GBP]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4179 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP, |
| 4180 | VXLAN_F_GBP, changelink, false, extack); |
| 4181 | if (err) |
| 4182 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | if (data[IFLA_VXLAN_GPE]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4186 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE, |
| 4187 | VXLAN_F_GPE, changelink, false, |
| 4188 | extack); |
| 4189 | if (err) |
| 4190 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4194 | err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL, |
| 4195 | VXLAN_F_REMCSUM_NOPARTIAL, changelink, |
| 4196 | false, extack); |
| 4197 | if (err) |
| 4198 | return err; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | if (tb[IFLA_MTU]) { |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4202 | if (changelink) { |
| 4203 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU], |
| 4204 | "Cannot change mtu"); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4205 | return -EOPNOTSUPP; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4206 | } |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4207 | conf->mtu = nla_get_u32(tb[IFLA_MTU]); |
| 4208 | } |
| 4209 | |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 4210 | if (data[IFLA_VXLAN_DF]) |
| 4211 | conf->df = nla_get_u8(data[IFLA_VXLAN_DF]); |
| 4212 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4213 | return 0; |
| 4214 | } |
| 4215 | |
| 4216 | static int vxlan_newlink(struct net *src_net, struct net_device *dev, |
Matthias Schiffer | 7a3f4a1 | 2017-06-25 23:55:59 +0200 | [diff] [blame] | 4217 | struct nlattr *tb[], struct nlattr *data[], |
| 4218 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4219 | { |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4220 | struct vxlan_config conf; |
| 4221 | int err; |
| 4222 | |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4223 | err = vxlan_nl2conf(tb, data, dev, &conf, false, extack); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4224 | if (err) |
| 4225 | return err; |
| 4226 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 4227 | return __vxlan_dev_create(src_net, dev, &conf, extack); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4230 | static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], |
Matthias Schiffer | ad744b2 | 2017-06-25 23:56:00 +0200 | [diff] [blame] | 4231 | struct nlattr *data[], |
| 4232 | struct netlink_ext_ack *extack) |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4233 | { |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4234 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Petr Machata | 8db9427d5 | 2019-01-16 23:06:39 +0000 | [diff] [blame] | 4235 | struct net_device *lowerdev; |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4236 | struct vxlan_config conf; |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4237 | struct vxlan_rdst *dst; |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4238 | int err; |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4239 | |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4240 | dst = &vxlan->default_dst; |
Roopa Prabhu | 70fb082 | 2019-02-25 22:03:01 -0800 | [diff] [blame] | 4241 | err = vxlan_nl2conf(tb, data, dev, &conf, true, extack); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4242 | if (err) |
| 4243 | return err; |
Jesse Gross | e277de5 | 2015-10-16 16:36:00 -0700 | [diff] [blame] | 4244 | |
Petr Machata | 8db9427d5 | 2019-01-16 23:06:39 +0000 | [diff] [blame] | 4245 | err = vxlan_config_validate(vxlan->net, &conf, &lowerdev, |
| 4246 | vxlan, extack); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4247 | if (err) |
| 4248 | return err; |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4249 | |
Taehee Yoo | c6761cf | 2019-10-30 08:15:12 +0000 | [diff] [blame] | 4250 | if (dst->remote_dev == lowerdev) |
| 4251 | lowerdev = NULL; |
| 4252 | |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4253 | err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev, |
| 4254 | extack); |
| 4255 | if (err) |
| 4256 | return err; |
| 4257 | |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4258 | /* handle default dst entry */ |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4259 | if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4260 | u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni); |
| 4261 | |
| 4262 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4263 | if (!vxlan_addr_any(&conf.remote_ip)) { |
Petr Machata | ce5e098f | 2018-12-18 13:16:02 +0000 | [diff] [blame] | 4264 | err = vxlan_fdb_update(vxlan, all_zeros_mac, |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4265 | &conf.remote_ip, |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4266 | NUD_REACHABLE | NUD_PERMANENT, |
Petr Machata | ce5e098f | 2018-12-18 13:16:02 +0000 | [diff] [blame] | 4267 | NLM_F_APPEND | NLM_F_CREATE, |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4268 | vxlan->cfg.dst_port, |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4269 | conf.vni, conf.vni, |
| 4270 | conf.remote_ifindex, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 4271 | NTF_SELF, 0, true, extack); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4272 | if (err) { |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4273 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4274 | netdev_adjacent_change_abort(dst->remote_dev, |
| 4275 | lowerdev, dev); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4276 | return err; |
| 4277 | } |
| 4278 | } |
Petr Machata | 1cdc98c | 2019-01-16 23:06:43 +0000 | [diff] [blame] | 4279 | if (!vxlan_addr_any(&dst->remote_ip)) |
| 4280 | __vxlan_fdb_delete(vxlan, all_zeros_mac, |
| 4281 | dst->remote_ip, |
| 4282 | vxlan->cfg.dst_port, |
| 4283 | dst->remote_vni, |
| 4284 | dst->remote_vni, |
| 4285 | dst->remote_ifindex, |
| 4286 | true); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4287 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4288 | } |
| 4289 | |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4290 | if (conf.age_interval != vxlan->cfg.age_interval) |
| 4291 | mod_timer(&vxlan->age_timer, jiffies); |
| 4292 | |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4293 | netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev); |
Cong Wang | 845e0eb | 2020-06-08 14:53:01 -0700 | [diff] [blame] | 4294 | if (lowerdev && lowerdev != dst->remote_dev) |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4295 | dst->remote_dev = lowerdev; |
Petr Machata | 038a5a9 | 2019-01-16 23:06:41 +0000 | [diff] [blame] | 4296 | vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true); |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4297 | return 0; |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4300 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
| 4301 | { |
| 4302 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 4303 | |
Roopa Prabhu | 8b3f933 | 2017-01-23 20:44:32 -0800 | [diff] [blame] | 4304 | vxlan_flush(vxlan, true); |
| 4305 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 4306 | list_del(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4307 | unregister_netdevice_queue(dev, head); |
Taehee Yoo | 0ce1822 | 2019-10-21 18:47:57 +0000 | [diff] [blame] | 4308 | if (vxlan->default_dst.remote_dev) |
| 4309 | netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | static size_t vxlan_get_size(const struct net_device *dev) |
| 4313 | { |
| 4314 | |
| 4315 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4316 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4317 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4318 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4319 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
Hangbin Liu | 8fd7806 | 2018-09-26 10:35:42 +0800 | [diff] [blame] | 4320 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4321 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 4322 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */ |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 4323 | nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4324 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 4325 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
| 4326 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ |
| 4327 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ |
| 4328 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ |
Alexei Starovoitov | da8b43c | 2015-08-04 22:51:07 -0700 | [diff] [blame] | 4329 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4330 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
| 4331 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 4332 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 4333 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ |
| 4334 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ |
| 4335 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ |
| 4336 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 4337 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ |
| 4338 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4339 | 0; |
| 4340 | } |
| 4341 | |
| 4342 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 4343 | { |
| 4344 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 4345 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 4346 | struct ifla_vxlan_port_range ports = { |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4347 | .low = htons(vxlan->cfg.port_min), |
| 4348 | .high = htons(vxlan->cfg.port_max), |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 4349 | }; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4350 | |
Jiri Benc | 54bfd87 | 2016-02-16 21:58:58 +0100 | [diff] [blame] | 4351 | if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4352 | goto nla_put_failure; |
| 4353 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4354 | if (!vxlan_addr_any(&dst->remote_ip)) { |
| 4355 | if (dst->remote_ip.sa.sa_family == AF_INET) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4356 | if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP, |
| 4357 | dst->remote_ip.sin.sin_addr.s_addr)) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4358 | goto nla_put_failure; |
| 4359 | #if IS_ENABLED(CONFIG_IPV6) |
| 4360 | } else { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4361 | if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6, |
| 4362 | &dst->remote_ip.sin6.sin6_addr)) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4363 | goto nla_put_failure; |
| 4364 | #endif |
| 4365 | } |
| 4366 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4367 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 4368 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4369 | goto nla_put_failure; |
| 4370 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4371 | if (!vxlan_addr_any(&vxlan->cfg.saddr)) { |
| 4372 | if (vxlan->cfg.saddr.sa.sa_family == AF_INET) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4373 | if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL, |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4374 | vxlan->cfg.saddr.sin.sin_addr.s_addr)) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4375 | goto nla_put_failure; |
| 4376 | #if IS_ENABLED(CONFIG_IPV6) |
| 4377 | } else { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4378 | if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6, |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4379 | &vxlan->cfg.saddr.sin6.sin6_addr)) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 4380 | goto nla_put_failure; |
| 4381 | #endif |
| 4382 | } |
| 4383 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4384 | |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4385 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) || |
Hangbin Liu | 8fd7806 | 2018-09-26 10:35:42 +0800 | [diff] [blame] | 4386 | nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT, |
| 4387 | !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) || |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4388 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) || |
Stefano Brivio | b4d30697 | 2018-11-08 12:19:16 +0100 | [diff] [blame] | 4389 | nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) || |
Daniel Borkmann | e7f70af | 2016-03-09 03:00:03 +0100 | [diff] [blame] | 4390 | nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 4391 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4392 | !!(vxlan->cfg.flags & VXLAN_F_LEARN)) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 4393 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4394 | !!(vxlan->cfg.flags & VXLAN_F_PROXY)) || |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 4395 | nla_put_u8(skb, IFLA_VXLAN_RSC, |
| 4396 | !!(vxlan->cfg.flags & VXLAN_F_RSC)) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 4397 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4398 | !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 4399 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4400 | !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) || |
Alexei Starovoitov | da8b43c | 2015-08-04 22:51:07 -0700 | [diff] [blame] | 4401 | nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA, |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 4402 | !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) || |
Thomas Graf | 0dfbdf4 | 2015-07-21 10:44:02 +0200 | [diff] [blame] | 4403 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) || |
| 4404 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) || |
| 4405 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) || |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 4406 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4407 | !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) || |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 4408 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4409 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 4410 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4411 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 4412 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4413 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) || |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 4414 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, |
Niu Xilei | 98c8147 | 2019-12-30 17:52:22 +0800 | [diff] [blame] | 4415 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4416 | goto nla_put_failure; |
| 4417 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 4418 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
| 4419 | goto nla_put_failure; |
| 4420 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 4421 | if (vxlan->cfg.flags & VXLAN_F_GBP && |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 4422 | nla_put_flag(skb, IFLA_VXLAN_GBP)) |
| 4423 | goto nla_put_failure; |
| 4424 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 4425 | if (vxlan->cfg.flags & VXLAN_F_GPE && |
Jiri Benc | e1e5314 | 2016-04-05 14:47:13 +0200 | [diff] [blame] | 4426 | nla_put_flag(skb, IFLA_VXLAN_GPE)) |
| 4427 | goto nla_put_failure; |
| 4428 | |
Matthias Schiffer | dc5321d | 2017-06-19 10:03:56 +0200 | [diff] [blame] | 4429 | if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL && |
Tom Herbert | 0ace2ca | 2015-02-10 16:30:32 -0800 | [diff] [blame] | 4430 | nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL)) |
| 4431 | goto nla_put_failure; |
| 4432 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4433 | return 0; |
| 4434 | |
| 4435 | nla_put_failure: |
| 4436 | return -EMSGSIZE; |
| 4437 | } |
| 4438 | |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 4439 | static struct net *vxlan_get_link_net(const struct net_device *dev) |
| 4440 | { |
| 4441 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 4442 | |
| 4443 | return vxlan->net; |
| 4444 | } |
| 4445 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4446 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
| 4447 | .kind = "vxlan", |
| 4448 | .maxtype = IFLA_VXLAN_MAX, |
| 4449 | .policy = vxlan_policy, |
| 4450 | .priv_size = sizeof(struct vxlan_dev), |
| 4451 | .setup = vxlan_setup, |
| 4452 | .validate = vxlan_validate, |
| 4453 | .newlink = vxlan_newlink, |
Roopa Prabhu | 8bcdc4f | 2017-02-20 08:29:19 -0800 | [diff] [blame] | 4454 | .changelink = vxlan_changelink, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4455 | .dellink = vxlan_dellink, |
| 4456 | .get_size = vxlan_get_size, |
| 4457 | .fill_info = vxlan_fill_info, |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 4458 | .get_link_net = vxlan_get_link_net, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4459 | }; |
| 4460 | |
Nicolas Dichtel | cf5da33 | 2016-06-13 10:31:05 +0200 | [diff] [blame] | 4461 | struct net_device *vxlan_dev_create(struct net *net, const char *name, |
| 4462 | u8 name_assign_type, |
| 4463 | struct vxlan_config *conf) |
| 4464 | { |
| 4465 | struct nlattr *tb[IFLA_MAX + 1]; |
| 4466 | struct net_device *dev; |
| 4467 | int err; |
| 4468 | |
| 4469 | memset(&tb, 0, sizeof(tb)); |
| 4470 | |
| 4471 | dev = rtnl_create_link(net, name, name_assign_type, |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 4472 | &vxlan_link_ops, tb, NULL); |
Nicolas Dichtel | cf5da33 | 2016-06-13 10:31:05 +0200 | [diff] [blame] | 4473 | if (IS_ERR(dev)) |
| 4474 | return dev; |
| 4475 | |
Girish Moodalbail | 653ef6a3 | 2017-08-11 15:20:59 -0700 | [diff] [blame] | 4476 | err = __vxlan_dev_create(net, dev, conf, NULL); |
Nicolas Dichtel | cf5da33 | 2016-06-13 10:31:05 +0200 | [diff] [blame] | 4477 | if (err < 0) { |
| 4478 | free_netdev(dev); |
| 4479 | return ERR_PTR(err); |
| 4480 | } |
| 4481 | |
| 4482 | err = rtnl_configure_link(dev, NULL); |
| 4483 | if (err < 0) { |
| 4484 | LIST_HEAD(list_kill); |
| 4485 | |
| 4486 | vxlan_dellink(dev, &list_kill); |
| 4487 | unregister_netdevice_many(&list_kill); |
| 4488 | return ERR_PTR(err); |
| 4489 | } |
| 4490 | |
| 4491 | return dev; |
| 4492 | } |
| 4493 | EXPORT_SYMBOL_GPL(vxlan_dev_create); |
| 4494 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4495 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
| 4496 | struct net_device *dev) |
| 4497 | { |
| 4498 | struct vxlan_dev *vxlan, *next; |
| 4499 | LIST_HEAD(list_kill); |
| 4500 | |
| 4501 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { |
| 4502 | struct vxlan_rdst *dst = &vxlan->default_dst; |
| 4503 | |
| 4504 | /* In case we created vxlan device with carrier |
| 4505 | * and we loose the carrier due to module unload |
| 4506 | * we also need to remove vxlan device. In other |
| 4507 | * cases, it's not necessary and remote_ifindex |
| 4508 | * is 0 here, so no matches. |
| 4509 | */ |
| 4510 | if (dst->remote_ifindex == dev->ifindex) |
| 4511 | vxlan_dellink(vxlan->dev, &list_kill); |
| 4512 | } |
| 4513 | |
| 4514 | unregister_netdevice_many(&list_kill); |
| 4515 | } |
| 4516 | |
Hannes Frederic Sowa | b7aade1 | 2016-04-18 21:19:47 +0200 | [diff] [blame] | 4517 | static int vxlan_netdevice_event(struct notifier_block *unused, |
| 4518 | unsigned long event, void *ptr) |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4519 | { |
| 4520 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 4521 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4522 | |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 4523 | if (event == NETDEV_UNREGISTER) { |
Jakub Kicinski | cc4e383 | 2020-07-09 17:42:46 -0700 | [diff] [blame] | 4524 | if (!dev->udp_tunnel_nic_info) |
| 4525 | vxlan_offload_rx_ports(dev, false); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4526 | vxlan_handle_lowerdev_unregister(vn, dev); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 4527 | } else if (event == NETDEV_REGISTER) { |
Jakub Kicinski | cc4e383 | 2020-07-09 17:42:46 -0700 | [diff] [blame] | 4528 | if (!dev->udp_tunnel_nic_info) |
| 4529 | vxlan_offload_rx_ports(dev, true); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 4530 | } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO || |
| 4531 | event == NETDEV_UDP_TUNNEL_DROP_INFO) { |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 4532 | vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 4533 | } |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4534 | |
| 4535 | return NOTIFY_DONE; |
| 4536 | } |
| 4537 | |
| 4538 | static struct notifier_block vxlan_notifier_block __read_mostly = { |
Hannes Frederic Sowa | b7aade1 | 2016-04-18 21:19:47 +0200 | [diff] [blame] | 4539 | .notifier_call = vxlan_netdevice_event, |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4540 | }; |
| 4541 | |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4542 | static void |
| 4543 | vxlan_fdb_offloaded_set(struct net_device *dev, |
| 4544 | struct switchdev_notifier_vxlan_fdb_info *fdb_info) |
| 4545 | { |
| 4546 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 4547 | struct vxlan_rdst *rdst; |
| 4548 | struct vxlan_fdb *f; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4549 | u32 hash_index; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4550 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4551 | hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni); |
| 4552 | |
| 4553 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4554 | |
| 4555 | f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni); |
| 4556 | if (!f) |
| 4557 | goto out; |
| 4558 | |
| 4559 | rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip, |
| 4560 | fdb_info->remote_port, |
| 4561 | fdb_info->remote_vni, |
| 4562 | fdb_info->remote_ifindex); |
| 4563 | if (!rdst) |
| 4564 | goto out; |
| 4565 | |
| 4566 | rdst->offloaded = fdb_info->offloaded; |
| 4567 | |
| 4568 | out: |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4569 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4572 | static int |
| 4573 | vxlan_fdb_external_learn_add(struct net_device *dev, |
| 4574 | struct switchdev_notifier_vxlan_fdb_info *fdb_info) |
| 4575 | { |
| 4576 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 4577 | struct netlink_ext_ack *extack; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4578 | u32 hash_index; |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4579 | int err; |
| 4580 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4581 | hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni); |
Petr Machata | 4c59b7d | 2019-01-16 23:06:54 +0000 | [diff] [blame] | 4582 | extack = switchdev_notifier_info_to_extack(&fdb_info->info); |
| 4583 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4584 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4585 | err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip, |
| 4586 | NUD_REACHABLE, |
| 4587 | NLM_F_CREATE | NLM_F_REPLACE, |
| 4588 | fdb_info->remote_port, |
| 4589 | fdb_info->vni, |
| 4590 | fdb_info->remote_vni, |
| 4591 | fdb_info->remote_ifindex, |
| 4592 | NTF_USE | NTF_SELF | NTF_EXT_LEARNED, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 4593 | 0, false, extack); |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4594 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4595 | |
| 4596 | return err; |
| 4597 | } |
| 4598 | |
| 4599 | static int |
| 4600 | vxlan_fdb_external_learn_del(struct net_device *dev, |
| 4601 | struct switchdev_notifier_vxlan_fdb_info *fdb_info) |
| 4602 | { |
| 4603 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 4604 | struct vxlan_fdb *f; |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4605 | u32 hash_index; |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4606 | int err = 0; |
| 4607 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4608 | hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni); |
| 4609 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4610 | |
| 4611 | f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni); |
| 4612 | if (!f) |
| 4613 | err = -ENOENT; |
| 4614 | else if (f->flags & NTF_EXT_LEARNED) |
| 4615 | err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr, |
| 4616 | fdb_info->remote_ip, |
| 4617 | fdb_info->remote_port, |
| 4618 | fdb_info->vni, |
| 4619 | fdb_info->remote_vni, |
| 4620 | fdb_info->remote_ifindex, |
| 4621 | false); |
| 4622 | |
Litao jiao | fe1e071 | 2019-06-06 17:57:58 +0800 | [diff] [blame] | 4623 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4624 | |
| 4625 | return err; |
| 4626 | } |
| 4627 | |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4628 | static int vxlan_switchdev_event(struct notifier_block *unused, |
| 4629 | unsigned long event, void *ptr) |
| 4630 | { |
| 4631 | struct net_device *dev = switchdev_notifier_info_to_dev(ptr); |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4632 | struct switchdev_notifier_vxlan_fdb_info *fdb_info; |
| 4633 | int err = 0; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4634 | |
| 4635 | switch (event) { |
| 4636 | case SWITCHDEV_VXLAN_FDB_OFFLOADED: |
| 4637 | vxlan_fdb_offloaded_set(dev, ptr); |
| 4638 | break; |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4639 | case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE: |
| 4640 | fdb_info = ptr; |
| 4641 | err = vxlan_fdb_external_learn_add(dev, fdb_info); |
| 4642 | if (err) { |
| 4643 | err = notifier_from_errno(err); |
| 4644 | break; |
| 4645 | } |
| 4646 | fdb_info->offloaded = true; |
| 4647 | vxlan_fdb_offloaded_set(dev, fdb_info); |
| 4648 | break; |
| 4649 | case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE: |
| 4650 | fdb_info = ptr; |
| 4651 | err = vxlan_fdb_external_learn_del(dev, fdb_info); |
| 4652 | if (err) { |
| 4653 | err = notifier_from_errno(err); |
| 4654 | break; |
| 4655 | } |
| 4656 | fdb_info->offloaded = false; |
| 4657 | vxlan_fdb_offloaded_set(dev, fdb_info); |
| 4658 | break; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Petr Machata | 5728ae0 | 2018-11-21 08:02:39 +0000 | [diff] [blame] | 4661 | return err; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = { |
| 4665 | .notifier_call = vxlan_switchdev_event, |
| 4666 | }; |
| 4667 | |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 4668 | static void vxlan_fdb_nh_flush(struct nexthop *nh) |
| 4669 | { |
| 4670 | struct vxlan_fdb *fdb; |
| 4671 | struct vxlan_dev *vxlan; |
| 4672 | u32 hash_index; |
| 4673 | |
| 4674 | rcu_read_lock(); |
| 4675 | list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) { |
| 4676 | vxlan = rcu_dereference(fdb->vdev); |
| 4677 | WARN_ON(!vxlan); |
| 4678 | hash_index = fdb_head_index(vxlan, fdb->eth_addr, |
| 4679 | vxlan->default_dst.remote_vni); |
| 4680 | spin_lock_bh(&vxlan->hash_lock[hash_index]); |
| 4681 | if (!hlist_unhashed(&fdb->hlist)) |
| 4682 | vxlan_fdb_destroy(vxlan, fdb, false, false); |
| 4683 | spin_unlock_bh(&vxlan->hash_lock[hash_index]); |
| 4684 | } |
| 4685 | rcu_read_unlock(); |
| 4686 | } |
| 4687 | |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 4688 | static int vxlan_nexthop_event(struct notifier_block *nb, |
| 4689 | unsigned long event, void *ptr) |
| 4690 | { |
| 4691 | struct nexthop *nh = ptr; |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 4692 | |
| 4693 | if (!nh || event != NEXTHOP_EVENT_DEL) |
| 4694 | return NOTIFY_DONE; |
| 4695 | |
Roopa Prabhu | 79472fe | 2020-05-28 22:12:36 -0700 | [diff] [blame] | 4696 | vxlan_fdb_nh_flush(nh); |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 4697 | |
| 4698 | return NOTIFY_DONE; |
| 4699 | } |
| 4700 | |
| 4701 | static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = { |
| 4702 | .notifier_call = vxlan_nexthop_event, |
| 4703 | }; |
| 4704 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4705 | static __net_init int vxlan_init_net(struct net *net) |
| 4706 | { |
| 4707 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 4708 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4709 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 4710 | INIT_LIST_HEAD(&vn->vxlan_list); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 4711 | spin_lock_init(&vn->sock_lock); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4712 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 4713 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 4714 | INIT_HLIST_HEAD(&vn->sock_list[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4715 | |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 4716 | return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4719 | static void vxlan_destroy_tunnels(struct net *net, struct list_head *head) |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4720 | { |
| 4721 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 4722 | struct vxlan_dev *vxlan, *next; |
| 4723 | struct net_device *dev, *aux; |
Vasily Averin | 0e4ec5a | 2017-11-12 22:28:10 +0300 | [diff] [blame] | 4724 | unsigned int h; |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4725 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4726 | for_each_netdev_safe(net, dev, aux) |
| 4727 | if (dev->rtnl_link_ops == &vxlan_link_ops) |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4728 | unregister_netdevice_queue(dev, head); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4729 | |
| 4730 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { |
| 4731 | /* If vxlan->dev is in the same netns, it has already been added |
| 4732 | * to the list by the previous loop. |
| 4733 | */ |
Zhiqiang Liu | cc4807b | 2019-03-16 17:02:54 +0800 | [diff] [blame] | 4734 | if (!net_eq(dev_net(vxlan->dev), net)) |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4735 | unregister_netdevice_queue(vxlan->dev, head); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4736 | } |
| 4737 | |
Vasily Averin | 0e4ec5a | 2017-11-12 22:28:10 +0300 | [diff] [blame] | 4738 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 4739 | WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h])); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 4740 | } |
| 4741 | |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4742 | static void __net_exit vxlan_exit_batch_net(struct list_head *net_list) |
| 4743 | { |
| 4744 | struct net *net; |
| 4745 | LIST_HEAD(list); |
| 4746 | |
| 4747 | rtnl_lock(); |
| 4748 | list_for_each_entry(net, net_list, exit_list) |
Roopa Prabhu | c7cdbe2 | 2020-05-21 22:26:16 -0700 | [diff] [blame] | 4749 | unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block); |
| 4750 | list_for_each_entry(net, net_list, exit_list) |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4751 | vxlan_destroy_tunnels(net, &list); |
| 4752 | |
| 4753 | unregister_netdevice_many(&list); |
| 4754 | rtnl_unlock(); |
| 4755 | } |
| 4756 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4757 | static struct pernet_operations vxlan_net_ops = { |
| 4758 | .init = vxlan_init_net, |
Haishuang Yan | 57b6112 | 2017-12-16 17:54:49 +0800 | [diff] [blame] | 4759 | .exit_batch = vxlan_exit_batch_net, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4760 | .id = &vxlan_net_id, |
| 4761 | .size = sizeof(struct vxlan_net), |
| 4762 | }; |
| 4763 | |
| 4764 | static int __init vxlan_init_module(void) |
| 4765 | { |
| 4766 | int rc; |
| 4767 | |
| 4768 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
| 4769 | |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 4770 | rc = register_pernet_subsys(&vxlan_net_ops); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4771 | if (rc) |
| 4772 | goto out1; |
| 4773 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4774 | rc = register_netdevice_notifier(&vxlan_notifier_block); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4775 | if (rc) |
| 4776 | goto out2; |
| 4777 | |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4778 | rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4779 | if (rc) |
| 4780 | goto out3; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4781 | |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4782 | rc = rtnl_link_register(&vxlan_link_ops); |
| 4783 | if (rc) |
| 4784 | goto out4; |
| 4785 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4786 | return 0; |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4787 | out4: |
| 4788 | unregister_switchdev_notifier(&vxlan_switchdev_notifier_block); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4789 | out3: |
| 4790 | unregister_netdevice_notifier(&vxlan_notifier_block); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4791 | out2: |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 4792 | unregister_pernet_subsys(&vxlan_net_ops); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4793 | out1: |
| 4794 | return rc; |
| 4795 | } |
Cong Wang | 7332a13 | 2013-05-27 22:35:53 +0000 | [diff] [blame] | 4796 | late_initcall(vxlan_init_module); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4797 | |
| 4798 | static void __exit vxlan_cleanup_module(void) |
| 4799 | { |
Stephen Hemminger | b715398 | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 4800 | rtnl_link_unregister(&vxlan_link_ops); |
Petr Machata | 0efe117 | 2018-10-17 08:53:26 +0000 | [diff] [blame] | 4801 | unregister_switchdev_notifier(&vxlan_switchdev_notifier_block); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 4802 | unregister_netdevice_notifier(&vxlan_notifier_block); |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 4803 | unregister_pernet_subsys(&vxlan_net_ops); |
| 4804 | /* rcu_barrier() is called by netns */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4805 | } |
| 4806 | module_exit(vxlan_cleanup_module); |
| 4807 | |
| 4808 | MODULE_LICENSE("GPL"); |
| 4809 | MODULE_VERSION(VXLAN_VERSION); |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 4810 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
Jesse Brandeburg | ead5139 | 2014-01-17 11:00:33 -0800 | [diff] [blame] | 4811 | MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 4812 | MODULE_ALIAS_RTNL_LINK("vxlan"); |