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