Commit 95ab09917a8187a02b518e22587c7f035edc7465

Authored by Gao feng
Committed by David S. Miller
1 parent 79d4a94fab

vxlan: leave multicast group when vxlan device down

vxlan_group_used only allows device to leave multicast group
when the remote_ip of this vxlan device is difference from
other vxlan devices' remote_ip. this will cause device not
leave multicast group untile the vn_sock of this vxlan deivce
being released.

The check in vxlan_group_used is not quite precise. since even
the remote_ip is same, but these vxlan devices may use different
lower devices, and they may use different vn_socks.

Only when some vxlan devices use the same vn_sock,same lower
device and same remote_ip, the mc_list of the vn_sock should
not be changed.

Signed-off-by: Gao feng <gaofeng@cn.fujitsu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 1 changed file with 21 additions and 6 deletions Inline Diff

1 /* 1 /*
2 * VXLAN: Virtual eXtensible Local Area Network 2 * VXLAN: Virtual eXtensible Local Area Network
3 * 3 *
4 * Copyright (c) 2012-2013 Vyatta Inc. 4 * Copyright (c) 2012-2013 Vyatta Inc.
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 */ 9 */
10 10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 12
13 #include <linux/kernel.h> 13 #include <linux/kernel.h>
14 #include <linux/types.h> 14 #include <linux/types.h>
15 #include <linux/module.h> 15 #include <linux/module.h>
16 #include <linux/errno.h> 16 #include <linux/errno.h>
17 #include <linux/slab.h> 17 #include <linux/slab.h>
18 #include <linux/skbuff.h> 18 #include <linux/skbuff.h>
19 #include <linux/rculist.h> 19 #include <linux/rculist.h>
20 #include <linux/netdevice.h> 20 #include <linux/netdevice.h>
21 #include <linux/in.h> 21 #include <linux/in.h>
22 #include <linux/ip.h> 22 #include <linux/ip.h>
23 #include <linux/udp.h> 23 #include <linux/udp.h>
24 #include <linux/igmp.h> 24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h> 25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h> 26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h> 27 #include <linux/if_vlan.h>
28 #include <linux/hash.h> 28 #include <linux/hash.h>
29 #include <linux/ethtool.h> 29 #include <linux/ethtool.h>
30 #include <net/arp.h> 30 #include <net/arp.h>
31 #include <net/ndisc.h> 31 #include <net/ndisc.h>
32 #include <net/ip.h> 32 #include <net/ip.h>
33 #include <net/ip_tunnels.h> 33 #include <net/ip_tunnels.h>
34 #include <net/icmp.h> 34 #include <net/icmp.h>
35 #include <net/udp.h> 35 #include <net/udp.h>
36 #include <net/rtnetlink.h> 36 #include <net/rtnetlink.h>
37 #include <net/route.h> 37 #include <net/route.h>
38 #include <net/dsfield.h> 38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h> 39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h> 40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h> 41 #include <net/netns/generic.h>
42 #include <net/vxlan.h> 42 #include <net/vxlan.h>
43 #if IS_ENABLED(CONFIG_IPV6) 43 #if IS_ENABLED(CONFIG_IPV6)
44 #include <net/ipv6.h> 44 #include <net/ipv6.h>
45 #include <net/addrconf.h> 45 #include <net/addrconf.h>
46 #include <net/ip6_tunnel.h> 46 #include <net/ip6_tunnel.h>
47 #include <net/ip6_checksum.h> 47 #include <net/ip6_checksum.h>
48 #endif 48 #endif
49 49
50 #define VXLAN_VERSION "0.1" 50 #define VXLAN_VERSION "0.1"
51 51
52 #define PORT_HASH_BITS 8 52 #define PORT_HASH_BITS 8
53 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) 53 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
54 #define VNI_HASH_BITS 10 54 #define VNI_HASH_BITS 10
55 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) 55 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56 #define FDB_HASH_BITS 8 56 #define FDB_HASH_BITS 8
57 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) 57 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */ 58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ 59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60 60
61 #define VXLAN_N_VID (1u << 24) 61 #define VXLAN_N_VID (1u << 24)
62 #define VXLAN_VID_MASK (VXLAN_N_VID - 1) 62 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
63 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr)) 63 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
64 64
65 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ 65 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
66 66
67 /* VXLAN protocol header */ 67 /* VXLAN protocol header */
68 struct vxlanhdr { 68 struct vxlanhdr {
69 __be32 vx_flags; 69 __be32 vx_flags;
70 __be32 vx_vni; 70 __be32 vx_vni;
71 }; 71 };
72 72
73 /* UDP port for VXLAN traffic. 73 /* UDP port for VXLAN traffic.
74 * The IANA assigned port is 4789, but the Linux default is 8472 74 * The IANA assigned port is 4789, but the Linux default is 8472
75 * for compatibility with early adopters. 75 * for compatibility with early adopters.
76 */ 76 */
77 static unsigned short vxlan_port __read_mostly = 8472; 77 static unsigned short vxlan_port __read_mostly = 8472;
78 module_param_named(udp_port, vxlan_port, ushort, 0444); 78 module_param_named(udp_port, vxlan_port, ushort, 0444);
79 MODULE_PARM_DESC(udp_port, "Destination UDP port"); 79 MODULE_PARM_DESC(udp_port, "Destination UDP port");
80 80
81 static bool log_ecn_error = true; 81 static bool log_ecn_error = true;
82 module_param(log_ecn_error, bool, 0644); 82 module_param(log_ecn_error, bool, 0644);
83 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 83 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
84 84
85 static int vxlan_net_id; 85 static int vxlan_net_id;
86 86
87 static const u8 all_zeros_mac[ETH_ALEN]; 87 static const u8 all_zeros_mac[ETH_ALEN];
88 88
89 /* per-network namespace private data for this module */ 89 /* per-network namespace private data for this module */
90 struct vxlan_net { 90 struct vxlan_net {
91 struct list_head vxlan_list; 91 struct list_head vxlan_list;
92 struct hlist_head sock_list[PORT_HASH_SIZE]; 92 struct hlist_head sock_list[PORT_HASH_SIZE];
93 spinlock_t sock_lock; 93 spinlock_t sock_lock;
94 }; 94 };
95 95
96 union vxlan_addr { 96 union vxlan_addr {
97 struct sockaddr_in sin; 97 struct sockaddr_in sin;
98 struct sockaddr_in6 sin6; 98 struct sockaddr_in6 sin6;
99 struct sockaddr sa; 99 struct sockaddr sa;
100 }; 100 };
101 101
102 struct vxlan_rdst { 102 struct vxlan_rdst {
103 union vxlan_addr remote_ip; 103 union vxlan_addr remote_ip;
104 __be16 remote_port; 104 __be16 remote_port;
105 u32 remote_vni; 105 u32 remote_vni;
106 u32 remote_ifindex; 106 u32 remote_ifindex;
107 struct list_head list; 107 struct list_head list;
108 struct rcu_head rcu; 108 struct rcu_head rcu;
109 }; 109 };
110 110
111 /* Forwarding table entry */ 111 /* Forwarding table entry */
112 struct vxlan_fdb { 112 struct vxlan_fdb {
113 struct hlist_node hlist; /* linked list of entries */ 113 struct hlist_node hlist; /* linked list of entries */
114 struct rcu_head rcu; 114 struct rcu_head rcu;
115 unsigned long updated; /* jiffies */ 115 unsigned long updated; /* jiffies */
116 unsigned long used; 116 unsigned long used;
117 struct list_head remotes; 117 struct list_head remotes;
118 u16 state; /* see ndm_state */ 118 u16 state; /* see ndm_state */
119 u8 flags; /* see ndm_flags */ 119 u8 flags; /* see ndm_flags */
120 u8 eth_addr[ETH_ALEN]; 120 u8 eth_addr[ETH_ALEN];
121 }; 121 };
122 122
123 /* Pseudo network device */ 123 /* Pseudo network device */
124 struct vxlan_dev { 124 struct vxlan_dev {
125 struct hlist_node hlist; /* vni hash table */ 125 struct hlist_node hlist; /* vni hash table */
126 struct list_head next; /* vxlan's per namespace list */ 126 struct list_head next; /* vxlan's per namespace list */
127 struct vxlan_sock *vn_sock; /* listening socket */ 127 struct vxlan_sock *vn_sock; /* listening socket */
128 struct net_device *dev; 128 struct net_device *dev;
129 struct vxlan_rdst default_dst; /* default destination */ 129 struct vxlan_rdst default_dst; /* default destination */
130 union vxlan_addr saddr; /* source address */ 130 union vxlan_addr saddr; /* source address */
131 __be16 dst_port; 131 __be16 dst_port;
132 __u16 port_min; /* source port range */ 132 __u16 port_min; /* source port range */
133 __u16 port_max; 133 __u16 port_max;
134 __u8 tos; /* TOS override */ 134 __u8 tos; /* TOS override */
135 __u8 ttl; 135 __u8 ttl;
136 u32 flags; /* VXLAN_F_* below */ 136 u32 flags; /* VXLAN_F_* below */
137 137
138 struct work_struct sock_work; 138 struct work_struct sock_work;
139 struct work_struct igmp_join; 139 struct work_struct igmp_join;
140 struct work_struct igmp_leave; 140 struct work_struct igmp_leave;
141 141
142 unsigned long age_interval; 142 unsigned long age_interval;
143 struct timer_list age_timer; 143 struct timer_list age_timer;
144 spinlock_t hash_lock; 144 spinlock_t hash_lock;
145 unsigned int addrcnt; 145 unsigned int addrcnt;
146 unsigned int addrmax; 146 unsigned int addrmax;
147 147
148 struct hlist_head fdb_head[FDB_HASH_SIZE]; 148 struct hlist_head fdb_head[FDB_HASH_SIZE];
149 }; 149 };
150 150
151 #define VXLAN_F_LEARN 0x01 151 #define VXLAN_F_LEARN 0x01
152 #define VXLAN_F_PROXY 0x02 152 #define VXLAN_F_PROXY 0x02
153 #define VXLAN_F_RSC 0x04 153 #define VXLAN_F_RSC 0x04
154 #define VXLAN_F_L2MISS 0x08 154 #define VXLAN_F_L2MISS 0x08
155 #define VXLAN_F_L3MISS 0x10 155 #define VXLAN_F_L3MISS 0x10
156 #define VXLAN_F_IPV6 0x20 /* internal flag */ 156 #define VXLAN_F_IPV6 0x20 /* internal flag */
157 157
158 /* salt for hash table */ 158 /* salt for hash table */
159 static u32 vxlan_salt __read_mostly; 159 static u32 vxlan_salt __read_mostly;
160 static struct workqueue_struct *vxlan_wq; 160 static struct workqueue_struct *vxlan_wq;
161 161
162 static void vxlan_sock_work(struct work_struct *work); 162 static void vxlan_sock_work(struct work_struct *work);
163 163
164 #if IS_ENABLED(CONFIG_IPV6) 164 #if IS_ENABLED(CONFIG_IPV6)
165 static inline 165 static inline
166 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) 166 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
167 { 167 {
168 if (a->sa.sa_family != b->sa.sa_family) 168 if (a->sa.sa_family != b->sa.sa_family)
169 return false; 169 return false;
170 if (a->sa.sa_family == AF_INET6) 170 if (a->sa.sa_family == AF_INET6)
171 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); 171 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
172 else 172 else
173 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; 173 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
174 } 174 }
175 175
176 static inline bool vxlan_addr_any(const union vxlan_addr *ipa) 176 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
177 { 177 {
178 if (ipa->sa.sa_family == AF_INET6) 178 if (ipa->sa.sa_family == AF_INET6)
179 return ipv6_addr_any(&ipa->sin6.sin6_addr); 179 return ipv6_addr_any(&ipa->sin6.sin6_addr);
180 else 180 else
181 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); 181 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
182 } 182 }
183 183
184 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) 184 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
185 { 185 {
186 if (ipa->sa.sa_family == AF_INET6) 186 if (ipa->sa.sa_family == AF_INET6)
187 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr); 187 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
188 else 188 else
189 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); 189 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
190 } 190 }
191 191
192 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) 192 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
193 { 193 {
194 if (nla_len(nla) >= sizeof(struct in6_addr)) { 194 if (nla_len(nla) >= sizeof(struct in6_addr)) {
195 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr)); 195 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
196 ip->sa.sa_family = AF_INET6; 196 ip->sa.sa_family = AF_INET6;
197 return 0; 197 return 0;
198 } else if (nla_len(nla) >= sizeof(__be32)) { 198 } else if (nla_len(nla) >= sizeof(__be32)) {
199 ip->sin.sin_addr.s_addr = nla_get_be32(nla); 199 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
200 ip->sa.sa_family = AF_INET; 200 ip->sa.sa_family = AF_INET;
201 return 0; 201 return 0;
202 } else { 202 } else {
203 return -EAFNOSUPPORT; 203 return -EAFNOSUPPORT;
204 } 204 }
205 } 205 }
206 206
207 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, 207 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
208 const union vxlan_addr *ip) 208 const union vxlan_addr *ip)
209 { 209 {
210 if (ip->sa.sa_family == AF_INET6) 210 if (ip->sa.sa_family == AF_INET6)
211 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr); 211 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
212 else 212 else
213 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); 213 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
214 } 214 }
215 215
216 #else /* !CONFIG_IPV6 */ 216 #else /* !CONFIG_IPV6 */
217 217
218 static inline 218 static inline
219 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) 219 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
220 { 220 {
221 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; 221 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
222 } 222 }
223 223
224 static inline bool vxlan_addr_any(const union vxlan_addr *ipa) 224 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
225 { 225 {
226 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); 226 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
227 } 227 }
228 228
229 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) 229 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
230 { 230 {
231 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); 231 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
232 } 232 }
233 233
234 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) 234 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
235 { 235 {
236 if (nla_len(nla) >= sizeof(struct in6_addr)) { 236 if (nla_len(nla) >= sizeof(struct in6_addr)) {
237 return -EAFNOSUPPORT; 237 return -EAFNOSUPPORT;
238 } else if (nla_len(nla) >= sizeof(__be32)) { 238 } else if (nla_len(nla) >= sizeof(__be32)) {
239 ip->sin.sin_addr.s_addr = nla_get_be32(nla); 239 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
240 ip->sa.sa_family = AF_INET; 240 ip->sa.sa_family = AF_INET;
241 return 0; 241 return 0;
242 } else { 242 } else {
243 return -EAFNOSUPPORT; 243 return -EAFNOSUPPORT;
244 } 244 }
245 } 245 }
246 246
247 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, 247 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
248 const union vxlan_addr *ip) 248 const union vxlan_addr *ip)
249 { 249 {
250 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); 250 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
251 } 251 }
252 #endif 252 #endif
253 253
254 /* Virtual Network hash table head */ 254 /* Virtual Network hash table head */
255 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) 255 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
256 { 256 {
257 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; 257 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
258 } 258 }
259 259
260 /* Socket hash table head */ 260 /* Socket hash table head */
261 static inline struct hlist_head *vs_head(struct net *net, __be16 port) 261 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
262 { 262 {
263 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 263 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
264 264
265 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; 265 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
266 } 266 }
267 267
268 /* First remote destination for a forwarding entry. 268 /* First remote destination for a forwarding entry.
269 * Guaranteed to be non-NULL because remotes are never deleted. 269 * Guaranteed to be non-NULL because remotes are never deleted.
270 */ 270 */
271 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) 271 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
272 { 272 {
273 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); 273 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
274 } 274 }
275 275
276 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) 276 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
277 { 277 {
278 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); 278 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
279 } 279 }
280 280
281 /* Find VXLAN socket based on network namespace and UDP port */ 281 /* Find VXLAN socket based on network namespace and UDP port */
282 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port) 282 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
283 { 283 {
284 struct vxlan_sock *vs; 284 struct vxlan_sock *vs;
285 285
286 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { 286 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
287 if (inet_sk(vs->sock->sk)->inet_sport == port) 287 if (inet_sk(vs->sock->sk)->inet_sport == port)
288 return vs; 288 return vs;
289 } 289 }
290 return NULL; 290 return NULL;
291 } 291 }
292 292
293 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id) 293 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
294 { 294 {
295 struct vxlan_dev *vxlan; 295 struct vxlan_dev *vxlan;
296 296
297 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { 297 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
298 if (vxlan->default_dst.remote_vni == id) 298 if (vxlan->default_dst.remote_vni == id)
299 return vxlan; 299 return vxlan;
300 } 300 }
301 301
302 return NULL; 302 return NULL;
303 } 303 }
304 304
305 /* Look up VNI in a per net namespace table */ 305 /* Look up VNI in a per net namespace table */
306 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port) 306 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
307 { 307 {
308 struct vxlan_sock *vs; 308 struct vxlan_sock *vs;
309 309
310 vs = vxlan_find_sock(net, port); 310 vs = vxlan_find_sock(net, port);
311 if (!vs) 311 if (!vs)
312 return NULL; 312 return NULL;
313 313
314 return vxlan_vs_find_vni(vs, id); 314 return vxlan_vs_find_vni(vs, id);
315 } 315 }
316 316
317 /* Fill in neighbour message in skbuff. */ 317 /* Fill in neighbour message in skbuff. */
318 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, 318 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
319 const struct vxlan_fdb *fdb, 319 const struct vxlan_fdb *fdb,
320 u32 portid, u32 seq, int type, unsigned int flags, 320 u32 portid, u32 seq, int type, unsigned int flags,
321 const struct vxlan_rdst *rdst) 321 const struct vxlan_rdst *rdst)
322 { 322 {
323 unsigned long now = jiffies; 323 unsigned long now = jiffies;
324 struct nda_cacheinfo ci; 324 struct nda_cacheinfo ci;
325 struct nlmsghdr *nlh; 325 struct nlmsghdr *nlh;
326 struct ndmsg *ndm; 326 struct ndmsg *ndm;
327 bool send_ip, send_eth; 327 bool send_ip, send_eth;
328 328
329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); 329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
330 if (nlh == NULL) 330 if (nlh == NULL)
331 return -EMSGSIZE; 331 return -EMSGSIZE;
332 332
333 ndm = nlmsg_data(nlh); 333 ndm = nlmsg_data(nlh);
334 memset(ndm, 0, sizeof(*ndm)); 334 memset(ndm, 0, sizeof(*ndm));
335 335
336 send_eth = send_ip = true; 336 send_eth = send_ip = true;
337 337
338 if (type == RTM_GETNEIGH) { 338 if (type == RTM_GETNEIGH) {
339 ndm->ndm_family = AF_INET; 339 ndm->ndm_family = AF_INET;
340 send_ip = !vxlan_addr_any(&rdst->remote_ip); 340 send_ip = !vxlan_addr_any(&rdst->remote_ip);
341 send_eth = !is_zero_ether_addr(fdb->eth_addr); 341 send_eth = !is_zero_ether_addr(fdb->eth_addr);
342 } else 342 } else
343 ndm->ndm_family = AF_BRIDGE; 343 ndm->ndm_family = AF_BRIDGE;
344 ndm->ndm_state = fdb->state; 344 ndm->ndm_state = fdb->state;
345 ndm->ndm_ifindex = vxlan->dev->ifindex; 345 ndm->ndm_ifindex = vxlan->dev->ifindex;
346 ndm->ndm_flags = fdb->flags; 346 ndm->ndm_flags = fdb->flags;
347 ndm->ndm_type = NDA_DST; 347 ndm->ndm_type = NDA_DST;
348 348
349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) 349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
350 goto nla_put_failure; 350 goto nla_put_failure;
351 351
352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip)) 352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
353 goto nla_put_failure; 353 goto nla_put_failure;
354 354
355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && 355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
356 nla_put_be16(skb, NDA_PORT, rdst->remote_port)) 356 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
357 goto nla_put_failure; 357 goto nla_put_failure;
358 if (rdst->remote_vni != vxlan->default_dst.remote_vni && 358 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni)) 359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
360 goto nla_put_failure; 360 goto nla_put_failure;
361 if (rdst->remote_ifindex && 361 if (rdst->remote_ifindex &&
362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) 362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
363 goto nla_put_failure; 363 goto nla_put_failure;
364 364
365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used); 365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
366 ci.ndm_confirmed = 0; 366 ci.ndm_confirmed = 0;
367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); 367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 ci.ndm_refcnt = 0; 368 ci.ndm_refcnt = 0;
369 369
370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
371 goto nla_put_failure; 371 goto nla_put_failure;
372 372
373 return nlmsg_end(skb, nlh); 373 return nlmsg_end(skb, nlh);
374 374
375 nla_put_failure: 375 nla_put_failure:
376 nlmsg_cancel(skb, nlh); 376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE; 377 return -EMSGSIZE;
378 } 378 }
379 379
380 static inline size_t vxlan_nlmsg_size(void) 380 static inline size_t vxlan_nlmsg_size(void)
381 { 381 {
382 return NLMSG_ALIGN(sizeof(struct ndmsg)) 382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ 383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ 384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */ 385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */ 386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ 387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
388 + nla_total_size(sizeof(struct nda_cacheinfo)); 388 + nla_total_size(sizeof(struct nda_cacheinfo));
389 } 389 }
390 390
391 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, 391 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
392 struct vxlan_fdb *fdb, int type) 392 struct vxlan_fdb *fdb, int type)
393 { 393 {
394 struct net *net = dev_net(vxlan->dev); 394 struct net *net = dev_net(vxlan->dev);
395 struct sk_buff *skb; 395 struct sk_buff *skb;
396 int err = -ENOBUFS; 396 int err = -ENOBUFS;
397 397
398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); 398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
399 if (skb == NULL) 399 if (skb == NULL)
400 goto errout; 400 goto errout;
401 401
402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, 402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
403 first_remote_rtnl(fdb)); 403 first_remote_rtnl(fdb));
404 if (err < 0) { 404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ 405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE); 406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb); 407 kfree_skb(skb);
408 goto errout; 408 goto errout;
409 } 409 }
410 410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return; 412 return;
413 errout: 413 errout:
414 if (err < 0) 414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416 } 416 }
417 417
418 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) 418 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
419 { 419 {
420 struct vxlan_dev *vxlan = netdev_priv(dev); 420 struct vxlan_dev *vxlan = netdev_priv(dev);
421 struct vxlan_fdb f = { 421 struct vxlan_fdb f = {
422 .state = NUD_STALE, 422 .state = NUD_STALE,
423 }; 423 };
424 struct vxlan_rdst remote = { 424 struct vxlan_rdst remote = {
425 .remote_ip = *ipa, /* goes to NDA_DST */ 425 .remote_ip = *ipa, /* goes to NDA_DST */
426 .remote_vni = VXLAN_N_VID, 426 .remote_vni = VXLAN_N_VID,
427 }; 427 };
428 428
429 INIT_LIST_HEAD(&f.remotes); 429 INIT_LIST_HEAD(&f.remotes);
430 list_add_rcu(&remote.list, &f.remotes); 430 list_add_rcu(&remote.list, &f.remotes);
431 431
432 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); 432 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
433 } 433 }
434 434
435 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) 435 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
436 { 436 {
437 struct vxlan_fdb f = { 437 struct vxlan_fdb f = {
438 .state = NUD_STALE, 438 .state = NUD_STALE,
439 }; 439 };
440 440
441 INIT_LIST_HEAD(&f.remotes); 441 INIT_LIST_HEAD(&f.remotes);
442 memcpy(f.eth_addr, eth_addr, ETH_ALEN); 442 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
443 443
444 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); 444 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
445 } 445 }
446 446
447 /* Hash Ethernet address */ 447 /* Hash Ethernet address */
448 static u32 eth_hash(const unsigned char *addr) 448 static u32 eth_hash(const unsigned char *addr)
449 { 449 {
450 u64 value = get_unaligned((u64 *)addr); 450 u64 value = get_unaligned((u64 *)addr);
451 451
452 /* only want 6 bytes */ 452 /* only want 6 bytes */
453 #ifdef __BIG_ENDIAN 453 #ifdef __BIG_ENDIAN
454 value >>= 16; 454 value >>= 16;
455 #else 455 #else
456 value <<= 16; 456 value <<= 16;
457 #endif 457 #endif
458 return hash_64(value, FDB_HASH_BITS); 458 return hash_64(value, FDB_HASH_BITS);
459 } 459 }
460 460
461 /* Hash chain to use given mac address */ 461 /* Hash chain to use given mac address */
462 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, 462 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
463 const u8 *mac) 463 const u8 *mac)
464 { 464 {
465 return &vxlan->fdb_head[eth_hash(mac)]; 465 return &vxlan->fdb_head[eth_hash(mac)];
466 } 466 }
467 467
468 /* Look up Ethernet address in forwarding table */ 468 /* Look up Ethernet address in forwarding table */
469 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, 469 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
470 const u8 *mac) 470 const u8 *mac)
471 471
472 { 472 {
473 struct hlist_head *head = vxlan_fdb_head(vxlan, mac); 473 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
474 struct vxlan_fdb *f; 474 struct vxlan_fdb *f;
475 475
476 hlist_for_each_entry_rcu(f, head, hlist) { 476 hlist_for_each_entry_rcu(f, head, hlist) {
477 if (ether_addr_equal(mac, f->eth_addr)) 477 if (ether_addr_equal(mac, f->eth_addr))
478 return f; 478 return f;
479 } 479 }
480 480
481 return NULL; 481 return NULL;
482 } 482 }
483 483
484 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, 484 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
485 const u8 *mac) 485 const u8 *mac)
486 { 486 {
487 struct vxlan_fdb *f; 487 struct vxlan_fdb *f;
488 488
489 f = __vxlan_find_mac(vxlan, mac); 489 f = __vxlan_find_mac(vxlan, mac);
490 if (f) 490 if (f)
491 f->used = jiffies; 491 f->used = jiffies;
492 492
493 return f; 493 return f;
494 } 494 }
495 495
496 /* caller should hold vxlan->hash_lock */ 496 /* caller should hold vxlan->hash_lock */
497 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, 497 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
498 union vxlan_addr *ip, __be16 port, 498 union vxlan_addr *ip, __be16 port,
499 __u32 vni, __u32 ifindex) 499 __u32 vni, __u32 ifindex)
500 { 500 {
501 struct vxlan_rdst *rd; 501 struct vxlan_rdst *rd;
502 502
503 list_for_each_entry(rd, &f->remotes, list) { 503 list_for_each_entry(rd, &f->remotes, list) {
504 if (vxlan_addr_equal(&rd->remote_ip, ip) && 504 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
505 rd->remote_port == port && 505 rd->remote_port == port &&
506 rd->remote_vni == vni && 506 rd->remote_vni == vni &&
507 rd->remote_ifindex == ifindex) 507 rd->remote_ifindex == ifindex)
508 return rd; 508 return rd;
509 } 509 }
510 510
511 return NULL; 511 return NULL;
512 } 512 }
513 513
514 /* Replace destination of unicast mac */ 514 /* Replace destination of unicast mac */
515 static int vxlan_fdb_replace(struct vxlan_fdb *f, 515 static int vxlan_fdb_replace(struct vxlan_fdb *f,
516 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex) 516 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
517 { 517 {
518 struct vxlan_rdst *rd; 518 struct vxlan_rdst *rd;
519 519
520 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); 520 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
521 if (rd) 521 if (rd)
522 return 0; 522 return 0;
523 523
524 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); 524 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
525 if (!rd) 525 if (!rd)
526 return 0; 526 return 0;
527 rd->remote_ip = *ip; 527 rd->remote_ip = *ip;
528 rd->remote_port = port; 528 rd->remote_port = port;
529 rd->remote_vni = vni; 529 rd->remote_vni = vni;
530 rd->remote_ifindex = ifindex; 530 rd->remote_ifindex = ifindex;
531 return 1; 531 return 1;
532 } 532 }
533 533
534 /* Add/update destinations for multicast */ 534 /* Add/update destinations for multicast */
535 static int vxlan_fdb_append(struct vxlan_fdb *f, 535 static int vxlan_fdb_append(struct vxlan_fdb *f,
536 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex) 536 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
537 { 537 {
538 struct vxlan_rdst *rd; 538 struct vxlan_rdst *rd;
539 539
540 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); 540 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
541 if (rd) 541 if (rd)
542 return 0; 542 return 0;
543 543
544 rd = kmalloc(sizeof(*rd), GFP_ATOMIC); 544 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
545 if (rd == NULL) 545 if (rd == NULL)
546 return -ENOBUFS; 546 return -ENOBUFS;
547 rd->remote_ip = *ip; 547 rd->remote_ip = *ip;
548 rd->remote_port = port; 548 rd->remote_port = port;
549 rd->remote_vni = vni; 549 rd->remote_vni = vni;
550 rd->remote_ifindex = ifindex; 550 rd->remote_ifindex = ifindex;
551 551
552 list_add_tail_rcu(&rd->list, &f->remotes); 552 list_add_tail_rcu(&rd->list, &f->remotes);
553 553
554 return 1; 554 return 1;
555 } 555 }
556 556
557 /* Notify netdevs that UDP port started listening */ 557 /* Notify netdevs that UDP port started listening */
558 static void vxlan_notify_add_rx_port(struct sock *sk) 558 static void vxlan_notify_add_rx_port(struct sock *sk)
559 { 559 {
560 struct net_device *dev; 560 struct net_device *dev;
561 struct net *net = sock_net(sk); 561 struct net *net = sock_net(sk);
562 sa_family_t sa_family = sk->sk_family; 562 sa_family_t sa_family = sk->sk_family;
563 __be16 port = inet_sk(sk)->inet_sport; 563 __be16 port = inet_sk(sk)->inet_sport;
564 564
565 rcu_read_lock(); 565 rcu_read_lock();
566 for_each_netdev_rcu(net, dev) { 566 for_each_netdev_rcu(net, dev) {
567 if (dev->netdev_ops->ndo_add_vxlan_port) 567 if (dev->netdev_ops->ndo_add_vxlan_port)
568 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, 568 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
569 port); 569 port);
570 } 570 }
571 rcu_read_unlock(); 571 rcu_read_unlock();
572 } 572 }
573 573
574 /* Notify netdevs that UDP port is no more listening */ 574 /* Notify netdevs that UDP port is no more listening */
575 static void vxlan_notify_del_rx_port(struct sock *sk) 575 static void vxlan_notify_del_rx_port(struct sock *sk)
576 { 576 {
577 struct net_device *dev; 577 struct net_device *dev;
578 struct net *net = sock_net(sk); 578 struct net *net = sock_net(sk);
579 sa_family_t sa_family = sk->sk_family; 579 sa_family_t sa_family = sk->sk_family;
580 __be16 port = inet_sk(sk)->inet_sport; 580 __be16 port = inet_sk(sk)->inet_sport;
581 581
582 rcu_read_lock(); 582 rcu_read_lock();
583 for_each_netdev_rcu(net, dev) { 583 for_each_netdev_rcu(net, dev) {
584 if (dev->netdev_ops->ndo_del_vxlan_port) 584 if (dev->netdev_ops->ndo_del_vxlan_port)
585 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family, 585 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
586 port); 586 port);
587 } 587 }
588 rcu_read_unlock(); 588 rcu_read_unlock();
589 } 589 }
590 590
591 /* Add new entry to forwarding table -- assumes lock held */ 591 /* Add new entry to forwarding table -- assumes lock held */
592 static int vxlan_fdb_create(struct vxlan_dev *vxlan, 592 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
593 const u8 *mac, union vxlan_addr *ip, 593 const u8 *mac, union vxlan_addr *ip,
594 __u16 state, __u16 flags, 594 __u16 state, __u16 flags,
595 __be16 port, __u32 vni, __u32 ifindex, 595 __be16 port, __u32 vni, __u32 ifindex,
596 __u8 ndm_flags) 596 __u8 ndm_flags)
597 { 597 {
598 struct vxlan_fdb *f; 598 struct vxlan_fdb *f;
599 int notify = 0; 599 int notify = 0;
600 600
601 f = __vxlan_find_mac(vxlan, mac); 601 f = __vxlan_find_mac(vxlan, mac);
602 if (f) { 602 if (f) {
603 if (flags & NLM_F_EXCL) { 603 if (flags & NLM_F_EXCL) {
604 netdev_dbg(vxlan->dev, 604 netdev_dbg(vxlan->dev,
605 "lost race to create %pM\n", mac); 605 "lost race to create %pM\n", mac);
606 return -EEXIST; 606 return -EEXIST;
607 } 607 }
608 if (f->state != state) { 608 if (f->state != state) {
609 f->state = state; 609 f->state = state;
610 f->updated = jiffies; 610 f->updated = jiffies;
611 notify = 1; 611 notify = 1;
612 } 612 }
613 if (f->flags != ndm_flags) { 613 if (f->flags != ndm_flags) {
614 f->flags = ndm_flags; 614 f->flags = ndm_flags;
615 f->updated = jiffies; 615 f->updated = jiffies;
616 notify = 1; 616 notify = 1;
617 } 617 }
618 if ((flags & NLM_F_REPLACE)) { 618 if ((flags & NLM_F_REPLACE)) {
619 /* Only change unicasts */ 619 /* Only change unicasts */
620 if (!(is_multicast_ether_addr(f->eth_addr) || 620 if (!(is_multicast_ether_addr(f->eth_addr) ||
621 is_zero_ether_addr(f->eth_addr))) { 621 is_zero_ether_addr(f->eth_addr))) {
622 int rc = vxlan_fdb_replace(f, ip, port, vni, 622 int rc = vxlan_fdb_replace(f, ip, port, vni,
623 ifindex); 623 ifindex);
624 624
625 if (rc < 0) 625 if (rc < 0)
626 return rc; 626 return rc;
627 notify |= rc; 627 notify |= rc;
628 } else 628 } else
629 return -EOPNOTSUPP; 629 return -EOPNOTSUPP;
630 } 630 }
631 if ((flags & NLM_F_APPEND) && 631 if ((flags & NLM_F_APPEND) &&
632 (is_multicast_ether_addr(f->eth_addr) || 632 (is_multicast_ether_addr(f->eth_addr) ||
633 is_zero_ether_addr(f->eth_addr))) { 633 is_zero_ether_addr(f->eth_addr))) {
634 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); 634 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
635 635
636 if (rc < 0) 636 if (rc < 0)
637 return rc; 637 return rc;
638 notify |= rc; 638 notify |= rc;
639 } 639 }
640 } else { 640 } else {
641 if (!(flags & NLM_F_CREATE)) 641 if (!(flags & NLM_F_CREATE))
642 return -ENOENT; 642 return -ENOENT;
643 643
644 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) 644 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
645 return -ENOSPC; 645 return -ENOSPC;
646 646
647 /* Disallow replace to add a multicast entry */ 647 /* Disallow replace to add a multicast entry */
648 if ((flags & NLM_F_REPLACE) && 648 if ((flags & NLM_F_REPLACE) &&
649 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) 649 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
650 return -EOPNOTSUPP; 650 return -EOPNOTSUPP;
651 651
652 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); 652 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
653 f = kmalloc(sizeof(*f), GFP_ATOMIC); 653 f = kmalloc(sizeof(*f), GFP_ATOMIC);
654 if (!f) 654 if (!f)
655 return -ENOMEM; 655 return -ENOMEM;
656 656
657 notify = 1; 657 notify = 1;
658 f->state = state; 658 f->state = state;
659 f->flags = ndm_flags; 659 f->flags = ndm_flags;
660 f->updated = f->used = jiffies; 660 f->updated = f->used = jiffies;
661 INIT_LIST_HEAD(&f->remotes); 661 INIT_LIST_HEAD(&f->remotes);
662 memcpy(f->eth_addr, mac, ETH_ALEN); 662 memcpy(f->eth_addr, mac, ETH_ALEN);
663 663
664 vxlan_fdb_append(f, ip, port, vni, ifindex); 664 vxlan_fdb_append(f, ip, port, vni, ifindex);
665 665
666 ++vxlan->addrcnt; 666 ++vxlan->addrcnt;
667 hlist_add_head_rcu(&f->hlist, 667 hlist_add_head_rcu(&f->hlist,
668 vxlan_fdb_head(vxlan, mac)); 668 vxlan_fdb_head(vxlan, mac));
669 } 669 }
670 670
671 if (notify) 671 if (notify)
672 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); 672 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
673 673
674 return 0; 674 return 0;
675 } 675 }
676 676
677 static void vxlan_fdb_free(struct rcu_head *head) 677 static void vxlan_fdb_free(struct rcu_head *head)
678 { 678 {
679 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); 679 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
680 struct vxlan_rdst *rd, *nd; 680 struct vxlan_rdst *rd, *nd;
681 681
682 list_for_each_entry_safe(rd, nd, &f->remotes, list) 682 list_for_each_entry_safe(rd, nd, &f->remotes, list)
683 kfree(rd); 683 kfree(rd);
684 kfree(f); 684 kfree(f);
685 } 685 }
686 686
687 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) 687 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
688 { 688 {
689 netdev_dbg(vxlan->dev, 689 netdev_dbg(vxlan->dev,
690 "delete %pM\n", f->eth_addr); 690 "delete %pM\n", f->eth_addr);
691 691
692 --vxlan->addrcnt; 692 --vxlan->addrcnt;
693 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); 693 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
694 694
695 hlist_del_rcu(&f->hlist); 695 hlist_del_rcu(&f->hlist);
696 call_rcu(&f->rcu, vxlan_fdb_free); 696 call_rcu(&f->rcu, vxlan_fdb_free);
697 } 697 }
698 698
699 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, 699 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
700 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex) 700 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
701 { 701 {
702 struct net *net = dev_net(vxlan->dev); 702 struct net *net = dev_net(vxlan->dev);
703 int err; 703 int err;
704 704
705 if (tb[NDA_DST]) { 705 if (tb[NDA_DST]) {
706 err = vxlan_nla_get_addr(ip, tb[NDA_DST]); 706 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
707 if (err) 707 if (err)
708 return err; 708 return err;
709 } else { 709 } else {
710 union vxlan_addr *remote = &vxlan->default_dst.remote_ip; 710 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
711 if (remote->sa.sa_family == AF_INET) { 711 if (remote->sa.sa_family == AF_INET) {
712 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); 712 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
713 ip->sa.sa_family = AF_INET; 713 ip->sa.sa_family = AF_INET;
714 #if IS_ENABLED(CONFIG_IPV6) 714 #if IS_ENABLED(CONFIG_IPV6)
715 } else { 715 } else {
716 ip->sin6.sin6_addr = in6addr_any; 716 ip->sin6.sin6_addr = in6addr_any;
717 ip->sa.sa_family = AF_INET6; 717 ip->sa.sa_family = AF_INET6;
718 #endif 718 #endif
719 } 719 }
720 } 720 }
721 721
722 if (tb[NDA_PORT]) { 722 if (tb[NDA_PORT]) {
723 if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) 723 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
724 return -EINVAL; 724 return -EINVAL;
725 *port = nla_get_be16(tb[NDA_PORT]); 725 *port = nla_get_be16(tb[NDA_PORT]);
726 } else { 726 } else {
727 *port = vxlan->dst_port; 727 *port = vxlan->dst_port;
728 } 728 }
729 729
730 if (tb[NDA_VNI]) { 730 if (tb[NDA_VNI]) {
731 if (nla_len(tb[NDA_VNI]) != sizeof(u32)) 731 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
732 return -EINVAL; 732 return -EINVAL;
733 *vni = nla_get_u32(tb[NDA_VNI]); 733 *vni = nla_get_u32(tb[NDA_VNI]);
734 } else { 734 } else {
735 *vni = vxlan->default_dst.remote_vni; 735 *vni = vxlan->default_dst.remote_vni;
736 } 736 }
737 737
738 if (tb[NDA_IFINDEX]) { 738 if (tb[NDA_IFINDEX]) {
739 struct net_device *tdev; 739 struct net_device *tdev;
740 740
741 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) 741 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
742 return -EINVAL; 742 return -EINVAL;
743 *ifindex = nla_get_u32(tb[NDA_IFINDEX]); 743 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
744 tdev = dev_get_by_index(net, *ifindex); 744 tdev = dev_get_by_index(net, *ifindex);
745 if (!tdev) 745 if (!tdev)
746 return -EADDRNOTAVAIL; 746 return -EADDRNOTAVAIL;
747 dev_put(tdev); 747 dev_put(tdev);
748 } else { 748 } else {
749 *ifindex = 0; 749 *ifindex = 0;
750 } 750 }
751 751
752 return 0; 752 return 0;
753 } 753 }
754 754
755 /* Add static entry (via netlink) */ 755 /* Add static entry (via netlink) */
756 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 756 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
757 struct net_device *dev, 757 struct net_device *dev,
758 const unsigned char *addr, u16 flags) 758 const unsigned char *addr, u16 flags)
759 { 759 {
760 struct vxlan_dev *vxlan = netdev_priv(dev); 760 struct vxlan_dev *vxlan = netdev_priv(dev);
761 /* struct net *net = dev_net(vxlan->dev); */ 761 /* struct net *net = dev_net(vxlan->dev); */
762 union vxlan_addr ip; 762 union vxlan_addr ip;
763 __be16 port; 763 __be16 port;
764 u32 vni, ifindex; 764 u32 vni, ifindex;
765 int err; 765 int err;
766 766
767 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { 767 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
768 pr_info("RTM_NEWNEIGH with invalid state %#x\n", 768 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
769 ndm->ndm_state); 769 ndm->ndm_state);
770 return -EINVAL; 770 return -EINVAL;
771 } 771 }
772 772
773 if (tb[NDA_DST] == NULL) 773 if (tb[NDA_DST] == NULL)
774 return -EINVAL; 774 return -EINVAL;
775 775
776 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); 776 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
777 if (err) 777 if (err)
778 return err; 778 return err;
779 779
780 spin_lock_bh(&vxlan->hash_lock); 780 spin_lock_bh(&vxlan->hash_lock);
781 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags, 781 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
782 port, vni, ifindex, ndm->ndm_flags); 782 port, vni, ifindex, ndm->ndm_flags);
783 spin_unlock_bh(&vxlan->hash_lock); 783 spin_unlock_bh(&vxlan->hash_lock);
784 784
785 return err; 785 return err;
786 } 786 }
787 787
788 /* Delete entry (via netlink) */ 788 /* Delete entry (via netlink) */
789 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], 789 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
790 struct net_device *dev, 790 struct net_device *dev,
791 const unsigned char *addr) 791 const unsigned char *addr)
792 { 792 {
793 struct vxlan_dev *vxlan = netdev_priv(dev); 793 struct vxlan_dev *vxlan = netdev_priv(dev);
794 struct vxlan_fdb *f; 794 struct vxlan_fdb *f;
795 struct vxlan_rdst *rd = NULL; 795 struct vxlan_rdst *rd = NULL;
796 union vxlan_addr ip; 796 union vxlan_addr ip;
797 __be16 port; 797 __be16 port;
798 u32 vni, ifindex; 798 u32 vni, ifindex;
799 int err; 799 int err;
800 800
801 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); 801 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
802 if (err) 802 if (err)
803 return err; 803 return err;
804 804
805 err = -ENOENT; 805 err = -ENOENT;
806 806
807 spin_lock_bh(&vxlan->hash_lock); 807 spin_lock_bh(&vxlan->hash_lock);
808 f = vxlan_find_mac(vxlan, addr); 808 f = vxlan_find_mac(vxlan, addr);
809 if (!f) 809 if (!f)
810 goto out; 810 goto out;
811 811
812 if (!vxlan_addr_any(&ip)) { 812 if (!vxlan_addr_any(&ip)) {
813 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); 813 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
814 if (!rd) 814 if (!rd)
815 goto out; 815 goto out;
816 } 816 }
817 817
818 err = 0; 818 err = 0;
819 819
820 /* remove a destination if it's not the only one on the list, 820 /* remove a destination if it's not the only one on the list,
821 * otherwise destroy the fdb entry 821 * otherwise destroy the fdb entry
822 */ 822 */
823 if (rd && !list_is_singular(&f->remotes)) { 823 if (rd && !list_is_singular(&f->remotes)) {
824 list_del_rcu(&rd->list); 824 list_del_rcu(&rd->list);
825 kfree_rcu(rd, rcu); 825 kfree_rcu(rd, rcu);
826 goto out; 826 goto out;
827 } 827 }
828 828
829 vxlan_fdb_destroy(vxlan, f); 829 vxlan_fdb_destroy(vxlan, f);
830 830
831 out: 831 out:
832 spin_unlock_bh(&vxlan->hash_lock); 832 spin_unlock_bh(&vxlan->hash_lock);
833 833
834 return err; 834 return err;
835 } 835 }
836 836
837 /* Dump forwarding table */ 837 /* Dump forwarding table */
838 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 838 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
839 struct net_device *dev, int idx) 839 struct net_device *dev, int idx)
840 { 840 {
841 struct vxlan_dev *vxlan = netdev_priv(dev); 841 struct vxlan_dev *vxlan = netdev_priv(dev);
842 unsigned int h; 842 unsigned int h;
843 843
844 for (h = 0; h < FDB_HASH_SIZE; ++h) { 844 for (h = 0; h < FDB_HASH_SIZE; ++h) {
845 struct vxlan_fdb *f; 845 struct vxlan_fdb *f;
846 int err; 846 int err;
847 847
848 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { 848 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
849 struct vxlan_rdst *rd; 849 struct vxlan_rdst *rd;
850 850
851 if (idx < cb->args[0]) 851 if (idx < cb->args[0])
852 goto skip; 852 goto skip;
853 853
854 list_for_each_entry_rcu(rd, &f->remotes, list) { 854 list_for_each_entry_rcu(rd, &f->remotes, list) {
855 err = vxlan_fdb_info(skb, vxlan, f, 855 err = vxlan_fdb_info(skb, vxlan, f,
856 NETLINK_CB(cb->skb).portid, 856 NETLINK_CB(cb->skb).portid,
857 cb->nlh->nlmsg_seq, 857 cb->nlh->nlmsg_seq,
858 RTM_NEWNEIGH, 858 RTM_NEWNEIGH,
859 NLM_F_MULTI, rd); 859 NLM_F_MULTI, rd);
860 if (err < 0) 860 if (err < 0)
861 goto out; 861 goto out;
862 } 862 }
863 skip: 863 skip:
864 ++idx; 864 ++idx;
865 } 865 }
866 } 866 }
867 out: 867 out:
868 return idx; 868 return idx;
869 } 869 }
870 870
871 /* Watch incoming packets to learn mapping between Ethernet address 871 /* Watch incoming packets to learn mapping between Ethernet address
872 * and Tunnel endpoint. 872 * and Tunnel endpoint.
873 * Return true if packet is bogus and should be droppped. 873 * Return true if packet is bogus and should be droppped.
874 */ 874 */
875 static bool vxlan_snoop(struct net_device *dev, 875 static bool vxlan_snoop(struct net_device *dev,
876 union vxlan_addr *src_ip, const u8 *src_mac) 876 union vxlan_addr *src_ip, const u8 *src_mac)
877 { 877 {
878 struct vxlan_dev *vxlan = netdev_priv(dev); 878 struct vxlan_dev *vxlan = netdev_priv(dev);
879 struct vxlan_fdb *f; 879 struct vxlan_fdb *f;
880 880
881 f = vxlan_find_mac(vxlan, src_mac); 881 f = vxlan_find_mac(vxlan, src_mac);
882 if (likely(f)) { 882 if (likely(f)) {
883 struct vxlan_rdst *rdst = first_remote_rcu(f); 883 struct vxlan_rdst *rdst = first_remote_rcu(f);
884 884
885 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip))) 885 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
886 return false; 886 return false;
887 887
888 /* Don't migrate static entries, drop packets */ 888 /* Don't migrate static entries, drop packets */
889 if (f->state & NUD_NOARP) 889 if (f->state & NUD_NOARP)
890 return true; 890 return true;
891 891
892 if (net_ratelimit()) 892 if (net_ratelimit())
893 netdev_info(dev, 893 netdev_info(dev,
894 "%pM migrated from %pIS to %pIS\n", 894 "%pM migrated from %pIS to %pIS\n",
895 src_mac, &rdst->remote_ip, &src_ip); 895 src_mac, &rdst->remote_ip, &src_ip);
896 896
897 rdst->remote_ip = *src_ip; 897 rdst->remote_ip = *src_ip;
898 f->updated = jiffies; 898 f->updated = jiffies;
899 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); 899 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
900 } else { 900 } else {
901 /* learned new entry */ 901 /* learned new entry */
902 spin_lock(&vxlan->hash_lock); 902 spin_lock(&vxlan->hash_lock);
903 903
904 /* close off race between vxlan_flush and incoming packets */ 904 /* close off race between vxlan_flush and incoming packets */
905 if (netif_running(dev)) 905 if (netif_running(dev))
906 vxlan_fdb_create(vxlan, src_mac, src_ip, 906 vxlan_fdb_create(vxlan, src_mac, src_ip,
907 NUD_REACHABLE, 907 NUD_REACHABLE,
908 NLM_F_EXCL|NLM_F_CREATE, 908 NLM_F_EXCL|NLM_F_CREATE,
909 vxlan->dst_port, 909 vxlan->dst_port,
910 vxlan->default_dst.remote_vni, 910 vxlan->default_dst.remote_vni,
911 0, NTF_SELF); 911 0, NTF_SELF);
912 spin_unlock(&vxlan->hash_lock); 912 spin_unlock(&vxlan->hash_lock);
913 } 913 }
914 914
915 return false; 915 return false;
916 } 916 }
917 917
918 /* See if multicast group is already in use by other ID */ 918 /* See if multicast group is already in use by other ID */
919 static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip) 919 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
920 { 920 {
921 struct vxlan_dev *vxlan; 921 struct vxlan_dev *vxlan;
922 922
923 /* The vxlan_sock is only used by dev, leaving group has
924 * no effect on other vxlan devices.
925 */
926 if (atomic_read(&dev->vn_sock->refcnt) == 1)
927 return false;
928
923 list_for_each_entry(vxlan, &vn->vxlan_list, next) { 929 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
924 if (!netif_running(vxlan->dev)) 930 if (!netif_running(vxlan->dev) || vxlan == dev)
925 continue; 931 continue;
926 932
927 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip, 933 if (vxlan->vn_sock != dev->vn_sock)
928 remote_ip)) 934 continue;
929 return true; 935
936 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
937 &dev->default_dst.remote_ip))
938 continue;
939
940 if (vxlan->default_dst.remote_ifindex !=
941 dev->default_dst.remote_ifindex)
942 continue;
943
944 return true;
930 } 945 }
931 946
932 return false; 947 return false;
933 } 948 }
934 949
935 static void vxlan_sock_hold(struct vxlan_sock *vs) 950 static void vxlan_sock_hold(struct vxlan_sock *vs)
936 { 951 {
937 atomic_inc(&vs->refcnt); 952 atomic_inc(&vs->refcnt);
938 } 953 }
939 954
940 void vxlan_sock_release(struct vxlan_sock *vs) 955 void vxlan_sock_release(struct vxlan_sock *vs)
941 { 956 {
942 struct sock *sk = vs->sock->sk; 957 struct sock *sk = vs->sock->sk;
943 struct net *net = sock_net(sk); 958 struct net *net = sock_net(sk);
944 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 959 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
945 960
946 if (!atomic_dec_and_test(&vs->refcnt)) 961 if (!atomic_dec_and_test(&vs->refcnt))
947 return; 962 return;
948 963
949 spin_lock(&vn->sock_lock); 964 spin_lock(&vn->sock_lock);
950 hlist_del_rcu(&vs->hlist); 965 hlist_del_rcu(&vs->hlist);
951 rcu_assign_sk_user_data(vs->sock->sk, NULL); 966 rcu_assign_sk_user_data(vs->sock->sk, NULL);
952 vxlan_notify_del_rx_port(sk); 967 vxlan_notify_del_rx_port(sk);
953 spin_unlock(&vn->sock_lock); 968 spin_unlock(&vn->sock_lock);
954 969
955 queue_work(vxlan_wq, &vs->del_work); 970 queue_work(vxlan_wq, &vs->del_work);
956 } 971 }
957 EXPORT_SYMBOL_GPL(vxlan_sock_release); 972 EXPORT_SYMBOL_GPL(vxlan_sock_release);
958 973
959 /* Callback to update multicast group membership when first VNI on 974 /* Callback to update multicast group membership when first VNI on
960 * multicast asddress is brought up 975 * multicast asddress is brought up
961 * Done as workqueue because ip_mc_join_group acquires RTNL. 976 * Done as workqueue because ip_mc_join_group acquires RTNL.
962 */ 977 */
963 static void vxlan_igmp_join(struct work_struct *work) 978 static void vxlan_igmp_join(struct work_struct *work)
964 { 979 {
965 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); 980 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
966 struct vxlan_sock *vs = vxlan->vn_sock; 981 struct vxlan_sock *vs = vxlan->vn_sock;
967 struct sock *sk = vs->sock->sk; 982 struct sock *sk = vs->sock->sk;
968 union vxlan_addr *ip = &vxlan->default_dst.remote_ip; 983 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
969 int ifindex = vxlan->default_dst.remote_ifindex; 984 int ifindex = vxlan->default_dst.remote_ifindex;
970 985
971 lock_sock(sk); 986 lock_sock(sk);
972 if (ip->sa.sa_family == AF_INET) { 987 if (ip->sa.sa_family == AF_INET) {
973 struct ip_mreqn mreq = { 988 struct ip_mreqn mreq = {
974 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, 989 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
975 .imr_ifindex = ifindex, 990 .imr_ifindex = ifindex,
976 }; 991 };
977 992
978 ip_mc_join_group(sk, &mreq); 993 ip_mc_join_group(sk, &mreq);
979 #if IS_ENABLED(CONFIG_IPV6) 994 #if IS_ENABLED(CONFIG_IPV6)
980 } else { 995 } else {
981 ipv6_stub->ipv6_sock_mc_join(sk, ifindex, 996 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
982 &ip->sin6.sin6_addr); 997 &ip->sin6.sin6_addr);
983 #endif 998 #endif
984 } 999 }
985 release_sock(sk); 1000 release_sock(sk);
986 1001
987 vxlan_sock_release(vs); 1002 vxlan_sock_release(vs);
988 dev_put(vxlan->dev); 1003 dev_put(vxlan->dev);
989 } 1004 }
990 1005
991 /* Inverse of vxlan_igmp_join when last VNI is brought down */ 1006 /* Inverse of vxlan_igmp_join when last VNI is brought down */
992 static void vxlan_igmp_leave(struct work_struct *work) 1007 static void vxlan_igmp_leave(struct work_struct *work)
993 { 1008 {
994 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); 1009 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
995 struct vxlan_sock *vs = vxlan->vn_sock; 1010 struct vxlan_sock *vs = vxlan->vn_sock;
996 struct sock *sk = vs->sock->sk; 1011 struct sock *sk = vs->sock->sk;
997 union vxlan_addr *ip = &vxlan->default_dst.remote_ip; 1012 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
998 int ifindex = vxlan->default_dst.remote_ifindex; 1013 int ifindex = vxlan->default_dst.remote_ifindex;
999 1014
1000 lock_sock(sk); 1015 lock_sock(sk);
1001 if (ip->sa.sa_family == AF_INET) { 1016 if (ip->sa.sa_family == AF_INET) {
1002 struct ip_mreqn mreq = { 1017 struct ip_mreqn mreq = {
1003 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, 1018 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1004 .imr_ifindex = ifindex, 1019 .imr_ifindex = ifindex,
1005 }; 1020 };
1006 1021
1007 ip_mc_leave_group(sk, &mreq); 1022 ip_mc_leave_group(sk, &mreq);
1008 #if IS_ENABLED(CONFIG_IPV6) 1023 #if IS_ENABLED(CONFIG_IPV6)
1009 } else { 1024 } else {
1010 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, 1025 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1011 &ip->sin6.sin6_addr); 1026 &ip->sin6.sin6_addr);
1012 #endif 1027 #endif
1013 } 1028 }
1014 1029
1015 release_sock(sk); 1030 release_sock(sk);
1016 1031
1017 vxlan_sock_release(vs); 1032 vxlan_sock_release(vs);
1018 dev_put(vxlan->dev); 1033 dev_put(vxlan->dev);
1019 } 1034 }
1020 1035
1021 /* Callback from net/ipv4/udp.c to receive packets */ 1036 /* Callback from net/ipv4/udp.c to receive packets */
1022 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 1037 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1023 { 1038 {
1024 struct vxlan_sock *vs; 1039 struct vxlan_sock *vs;
1025 struct vxlanhdr *vxh; 1040 struct vxlanhdr *vxh;
1026 __be16 port; 1041 __be16 port;
1027 1042
1028 /* Need Vxlan and inner Ethernet header to be present */ 1043 /* Need Vxlan and inner Ethernet header to be present */
1029 if (!pskb_may_pull(skb, VXLAN_HLEN)) 1044 if (!pskb_may_pull(skb, VXLAN_HLEN))
1030 goto error; 1045 goto error;
1031 1046
1032 /* Return packets with reserved bits set */ 1047 /* Return packets with reserved bits set */
1033 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); 1048 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1034 if (vxh->vx_flags != htonl(VXLAN_FLAGS) || 1049 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1035 (vxh->vx_vni & htonl(0xff))) { 1050 (vxh->vx_vni & htonl(0xff))) {
1036 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", 1051 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1037 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); 1052 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1038 goto error; 1053 goto error;
1039 } 1054 }
1040 1055
1041 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) 1056 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1042 goto drop; 1057 goto drop;
1043 1058
1044 port = inet_sk(sk)->inet_sport; 1059 port = inet_sk(sk)->inet_sport;
1045 1060
1046 vs = rcu_dereference_sk_user_data(sk); 1061 vs = rcu_dereference_sk_user_data(sk);
1047 if (!vs) 1062 if (!vs)
1048 goto drop; 1063 goto drop;
1049 1064
1050 vs->rcv(vs, skb, vxh->vx_vni); 1065 vs->rcv(vs, skb, vxh->vx_vni);
1051 return 0; 1066 return 0;
1052 1067
1053 drop: 1068 drop:
1054 /* Consume bad packet */ 1069 /* Consume bad packet */
1055 kfree_skb(skb); 1070 kfree_skb(skb);
1056 return 0; 1071 return 0;
1057 1072
1058 error: 1073 error:
1059 /* Return non vxlan pkt */ 1074 /* Return non vxlan pkt */
1060 return 1; 1075 return 1;
1061 } 1076 }
1062 1077
1063 static void vxlan_rcv(struct vxlan_sock *vs, 1078 static void vxlan_rcv(struct vxlan_sock *vs,
1064 struct sk_buff *skb, __be32 vx_vni) 1079 struct sk_buff *skb, __be32 vx_vni)
1065 { 1080 {
1066 struct iphdr *oip = NULL; 1081 struct iphdr *oip = NULL;
1067 struct ipv6hdr *oip6 = NULL; 1082 struct ipv6hdr *oip6 = NULL;
1068 struct vxlan_dev *vxlan; 1083 struct vxlan_dev *vxlan;
1069 struct pcpu_tstats *stats; 1084 struct pcpu_tstats *stats;
1070 union vxlan_addr saddr; 1085 union vxlan_addr saddr;
1071 __u32 vni; 1086 __u32 vni;
1072 int err = 0; 1087 int err = 0;
1073 union vxlan_addr *remote_ip; 1088 union vxlan_addr *remote_ip;
1074 1089
1075 vni = ntohl(vx_vni) >> 8; 1090 vni = ntohl(vx_vni) >> 8;
1076 /* Is this VNI defined? */ 1091 /* Is this VNI defined? */
1077 vxlan = vxlan_vs_find_vni(vs, vni); 1092 vxlan = vxlan_vs_find_vni(vs, vni);
1078 if (!vxlan) 1093 if (!vxlan)
1079 goto drop; 1094 goto drop;
1080 1095
1081 remote_ip = &vxlan->default_dst.remote_ip; 1096 remote_ip = &vxlan->default_dst.remote_ip;
1082 skb_reset_mac_header(skb); 1097 skb_reset_mac_header(skb);
1083 skb->protocol = eth_type_trans(skb, vxlan->dev); 1098 skb->protocol = eth_type_trans(skb, vxlan->dev);
1084 1099
1085 /* Ignore packet loops (and multicast echo) */ 1100 /* Ignore packet loops (and multicast echo) */
1086 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) 1101 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1087 goto drop; 1102 goto drop;
1088 1103
1089 /* Re-examine inner Ethernet packet */ 1104 /* Re-examine inner Ethernet packet */
1090 if (remote_ip->sa.sa_family == AF_INET) { 1105 if (remote_ip->sa.sa_family == AF_INET) {
1091 oip = ip_hdr(skb); 1106 oip = ip_hdr(skb);
1092 saddr.sin.sin_addr.s_addr = oip->saddr; 1107 saddr.sin.sin_addr.s_addr = oip->saddr;
1093 saddr.sa.sa_family = AF_INET; 1108 saddr.sa.sa_family = AF_INET;
1094 #if IS_ENABLED(CONFIG_IPV6) 1109 #if IS_ENABLED(CONFIG_IPV6)
1095 } else { 1110 } else {
1096 oip6 = ipv6_hdr(skb); 1111 oip6 = ipv6_hdr(skb);
1097 saddr.sin6.sin6_addr = oip6->saddr; 1112 saddr.sin6.sin6_addr = oip6->saddr;
1098 saddr.sa.sa_family = AF_INET6; 1113 saddr.sa.sa_family = AF_INET6;
1099 #endif 1114 #endif
1100 } 1115 }
1101 1116
1102 if ((vxlan->flags & VXLAN_F_LEARN) && 1117 if ((vxlan->flags & VXLAN_F_LEARN) &&
1103 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source)) 1118 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1104 goto drop; 1119 goto drop;
1105 1120
1106 skb_reset_network_header(skb); 1121 skb_reset_network_header(skb);
1107 1122
1108 /* If the NIC driver gave us an encapsulated packet with 1123 /* If the NIC driver gave us an encapsulated packet with
1109 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, 1124 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1110 * leave the CHECKSUM_UNNECESSARY, the device checksummed it 1125 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1111 * for us. Otherwise force the upper layers to verify it. 1126 * for us. Otherwise force the upper layers to verify it.
1112 */ 1127 */
1113 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || 1128 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1114 !(vxlan->dev->features & NETIF_F_RXCSUM)) 1129 !(vxlan->dev->features & NETIF_F_RXCSUM))
1115 skb->ip_summed = CHECKSUM_NONE; 1130 skb->ip_summed = CHECKSUM_NONE;
1116 1131
1117 skb->encapsulation = 0; 1132 skb->encapsulation = 0;
1118 1133
1119 if (oip6) 1134 if (oip6)
1120 err = IP6_ECN_decapsulate(oip6, skb); 1135 err = IP6_ECN_decapsulate(oip6, skb);
1121 if (oip) 1136 if (oip)
1122 err = IP_ECN_decapsulate(oip, skb); 1137 err = IP_ECN_decapsulate(oip, skb);
1123 1138
1124 if (unlikely(err)) { 1139 if (unlikely(err)) {
1125 if (log_ecn_error) { 1140 if (log_ecn_error) {
1126 if (oip6) 1141 if (oip6)
1127 net_info_ratelimited("non-ECT from %pI6\n", 1142 net_info_ratelimited("non-ECT from %pI6\n",
1128 &oip6->saddr); 1143 &oip6->saddr);
1129 if (oip) 1144 if (oip)
1130 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", 1145 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1131 &oip->saddr, oip->tos); 1146 &oip->saddr, oip->tos);
1132 } 1147 }
1133 if (err > 1) { 1148 if (err > 1) {
1134 ++vxlan->dev->stats.rx_frame_errors; 1149 ++vxlan->dev->stats.rx_frame_errors;
1135 ++vxlan->dev->stats.rx_errors; 1150 ++vxlan->dev->stats.rx_errors;
1136 goto drop; 1151 goto drop;
1137 } 1152 }
1138 } 1153 }
1139 1154
1140 stats = this_cpu_ptr(vxlan->dev->tstats); 1155 stats = this_cpu_ptr(vxlan->dev->tstats);
1141 u64_stats_update_begin(&stats->syncp); 1156 u64_stats_update_begin(&stats->syncp);
1142 stats->rx_packets++; 1157 stats->rx_packets++;
1143 stats->rx_bytes += skb->len; 1158 stats->rx_bytes += skb->len;
1144 u64_stats_update_end(&stats->syncp); 1159 u64_stats_update_end(&stats->syncp);
1145 1160
1146 netif_rx(skb); 1161 netif_rx(skb);
1147 1162
1148 return; 1163 return;
1149 drop: 1164 drop:
1150 /* Consume bad packet */ 1165 /* Consume bad packet */
1151 kfree_skb(skb); 1166 kfree_skb(skb);
1152 } 1167 }
1153 1168
1154 static int arp_reduce(struct net_device *dev, struct sk_buff *skb) 1169 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1155 { 1170 {
1156 struct vxlan_dev *vxlan = netdev_priv(dev); 1171 struct vxlan_dev *vxlan = netdev_priv(dev);
1157 struct arphdr *parp; 1172 struct arphdr *parp;
1158 u8 *arpptr, *sha; 1173 u8 *arpptr, *sha;
1159 __be32 sip, tip; 1174 __be32 sip, tip;
1160 struct neighbour *n; 1175 struct neighbour *n;
1161 1176
1162 if (dev->flags & IFF_NOARP) 1177 if (dev->flags & IFF_NOARP)
1163 goto out; 1178 goto out;
1164 1179
1165 if (!pskb_may_pull(skb, arp_hdr_len(dev))) { 1180 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1166 dev->stats.tx_dropped++; 1181 dev->stats.tx_dropped++;
1167 goto out; 1182 goto out;
1168 } 1183 }
1169 parp = arp_hdr(skb); 1184 parp = arp_hdr(skb);
1170 1185
1171 if ((parp->ar_hrd != htons(ARPHRD_ETHER) && 1186 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1172 parp->ar_hrd != htons(ARPHRD_IEEE802)) || 1187 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1173 parp->ar_pro != htons(ETH_P_IP) || 1188 parp->ar_pro != htons(ETH_P_IP) ||
1174 parp->ar_op != htons(ARPOP_REQUEST) || 1189 parp->ar_op != htons(ARPOP_REQUEST) ||
1175 parp->ar_hln != dev->addr_len || 1190 parp->ar_hln != dev->addr_len ||
1176 parp->ar_pln != 4) 1191 parp->ar_pln != 4)
1177 goto out; 1192 goto out;
1178 arpptr = (u8 *)parp + sizeof(struct arphdr); 1193 arpptr = (u8 *)parp + sizeof(struct arphdr);
1179 sha = arpptr; 1194 sha = arpptr;
1180 arpptr += dev->addr_len; /* sha */ 1195 arpptr += dev->addr_len; /* sha */
1181 memcpy(&sip, arpptr, sizeof(sip)); 1196 memcpy(&sip, arpptr, sizeof(sip));
1182 arpptr += sizeof(sip); 1197 arpptr += sizeof(sip);
1183 arpptr += dev->addr_len; /* tha */ 1198 arpptr += dev->addr_len; /* tha */
1184 memcpy(&tip, arpptr, sizeof(tip)); 1199 memcpy(&tip, arpptr, sizeof(tip));
1185 1200
1186 if (ipv4_is_loopback(tip) || 1201 if (ipv4_is_loopback(tip) ||
1187 ipv4_is_multicast(tip)) 1202 ipv4_is_multicast(tip))
1188 goto out; 1203 goto out;
1189 1204
1190 n = neigh_lookup(&arp_tbl, &tip, dev); 1205 n = neigh_lookup(&arp_tbl, &tip, dev);
1191 1206
1192 if (n) { 1207 if (n) {
1193 struct vxlan_fdb *f; 1208 struct vxlan_fdb *f;
1194 struct sk_buff *reply; 1209 struct sk_buff *reply;
1195 1210
1196 if (!(n->nud_state & NUD_CONNECTED)) { 1211 if (!(n->nud_state & NUD_CONNECTED)) {
1197 neigh_release(n); 1212 neigh_release(n);
1198 goto out; 1213 goto out;
1199 } 1214 }
1200 1215
1201 f = vxlan_find_mac(vxlan, n->ha); 1216 f = vxlan_find_mac(vxlan, n->ha);
1202 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { 1217 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1203 /* bridge-local neighbor */ 1218 /* bridge-local neighbor */
1204 neigh_release(n); 1219 neigh_release(n);
1205 goto out; 1220 goto out;
1206 } 1221 }
1207 1222
1208 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, 1223 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1209 n->ha, sha); 1224 n->ha, sha);
1210 1225
1211 neigh_release(n); 1226 neigh_release(n);
1212 1227
1213 skb_reset_mac_header(reply); 1228 skb_reset_mac_header(reply);
1214 __skb_pull(reply, skb_network_offset(reply)); 1229 __skb_pull(reply, skb_network_offset(reply));
1215 reply->ip_summed = CHECKSUM_UNNECESSARY; 1230 reply->ip_summed = CHECKSUM_UNNECESSARY;
1216 reply->pkt_type = PACKET_HOST; 1231 reply->pkt_type = PACKET_HOST;
1217 1232
1218 if (netif_rx_ni(reply) == NET_RX_DROP) 1233 if (netif_rx_ni(reply) == NET_RX_DROP)
1219 dev->stats.rx_dropped++; 1234 dev->stats.rx_dropped++;
1220 } else if (vxlan->flags & VXLAN_F_L3MISS) { 1235 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1221 union vxlan_addr ipa = { 1236 union vxlan_addr ipa = {
1222 .sin.sin_addr.s_addr = tip, 1237 .sin.sin_addr.s_addr = tip,
1223 .sa.sa_family = AF_INET, 1238 .sa.sa_family = AF_INET,
1224 }; 1239 };
1225 1240
1226 vxlan_ip_miss(dev, &ipa); 1241 vxlan_ip_miss(dev, &ipa);
1227 } 1242 }
1228 out: 1243 out:
1229 consume_skb(skb); 1244 consume_skb(skb);
1230 return NETDEV_TX_OK; 1245 return NETDEV_TX_OK;
1231 } 1246 }
1232 1247
1233 #if IS_ENABLED(CONFIG_IPV6) 1248 #if IS_ENABLED(CONFIG_IPV6)
1234 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb) 1249 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1235 { 1250 {
1236 struct vxlan_dev *vxlan = netdev_priv(dev); 1251 struct vxlan_dev *vxlan = netdev_priv(dev);
1237 struct neighbour *n; 1252 struct neighbour *n;
1238 union vxlan_addr ipa; 1253 union vxlan_addr ipa;
1239 const struct ipv6hdr *iphdr; 1254 const struct ipv6hdr *iphdr;
1240 const struct in6_addr *saddr, *daddr; 1255 const struct in6_addr *saddr, *daddr;
1241 struct nd_msg *msg; 1256 struct nd_msg *msg;
1242 struct inet6_dev *in6_dev = NULL; 1257 struct inet6_dev *in6_dev = NULL;
1243 1258
1244 in6_dev = __in6_dev_get(dev); 1259 in6_dev = __in6_dev_get(dev);
1245 if (!in6_dev) 1260 if (!in6_dev)
1246 goto out; 1261 goto out;
1247 1262
1248 if (!pskb_may_pull(skb, skb->len)) 1263 if (!pskb_may_pull(skb, skb->len))
1249 goto out; 1264 goto out;
1250 1265
1251 iphdr = ipv6_hdr(skb); 1266 iphdr = ipv6_hdr(skb);
1252 saddr = &iphdr->saddr; 1267 saddr = &iphdr->saddr;
1253 daddr = &iphdr->daddr; 1268 daddr = &iphdr->daddr;
1254 1269
1255 if (ipv6_addr_loopback(daddr) || 1270 if (ipv6_addr_loopback(daddr) ||
1256 ipv6_addr_is_multicast(daddr)) 1271 ipv6_addr_is_multicast(daddr))
1257 goto out; 1272 goto out;
1258 1273
1259 msg = (struct nd_msg *)skb_transport_header(skb); 1274 msg = (struct nd_msg *)skb_transport_header(skb);
1260 if (msg->icmph.icmp6_code != 0 || 1275 if (msg->icmph.icmp6_code != 0 ||
1261 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) 1276 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1262 goto out; 1277 goto out;
1263 1278
1264 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev); 1279 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
1265 1280
1266 if (n) { 1281 if (n) {
1267 struct vxlan_fdb *f; 1282 struct vxlan_fdb *f;
1268 1283
1269 if (!(n->nud_state & NUD_CONNECTED)) { 1284 if (!(n->nud_state & NUD_CONNECTED)) {
1270 neigh_release(n); 1285 neigh_release(n);
1271 goto out; 1286 goto out;
1272 } 1287 }
1273 1288
1274 f = vxlan_find_mac(vxlan, n->ha); 1289 f = vxlan_find_mac(vxlan, n->ha);
1275 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { 1290 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1276 /* bridge-local neighbor */ 1291 /* bridge-local neighbor */
1277 neigh_release(n); 1292 neigh_release(n);
1278 goto out; 1293 goto out;
1279 } 1294 }
1280 1295
1281 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target, 1296 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
1282 !!in6_dev->cnf.forwarding, 1297 !!in6_dev->cnf.forwarding,
1283 true, false, false); 1298 true, false, false);
1284 neigh_release(n); 1299 neigh_release(n);
1285 } else if (vxlan->flags & VXLAN_F_L3MISS) { 1300 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1286 ipa.sin6.sin6_addr = *daddr; 1301 ipa.sin6.sin6_addr = *daddr;
1287 ipa.sa.sa_family = AF_INET6; 1302 ipa.sa.sa_family = AF_INET6;
1288 vxlan_ip_miss(dev, &ipa); 1303 vxlan_ip_miss(dev, &ipa);
1289 } 1304 }
1290 1305
1291 out: 1306 out:
1292 consume_skb(skb); 1307 consume_skb(skb);
1293 return NETDEV_TX_OK; 1308 return NETDEV_TX_OK;
1294 } 1309 }
1295 #endif 1310 #endif
1296 1311
1297 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) 1312 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1298 { 1313 {
1299 struct vxlan_dev *vxlan = netdev_priv(dev); 1314 struct vxlan_dev *vxlan = netdev_priv(dev);
1300 struct neighbour *n; 1315 struct neighbour *n;
1301 1316
1302 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) 1317 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1303 return false; 1318 return false;
1304 1319
1305 n = NULL; 1320 n = NULL;
1306 switch (ntohs(eth_hdr(skb)->h_proto)) { 1321 switch (ntohs(eth_hdr(skb)->h_proto)) {
1307 case ETH_P_IP: 1322 case ETH_P_IP:
1308 { 1323 {
1309 struct iphdr *pip; 1324 struct iphdr *pip;
1310 1325
1311 if (!pskb_may_pull(skb, sizeof(struct iphdr))) 1326 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1312 return false; 1327 return false;
1313 pip = ip_hdr(skb); 1328 pip = ip_hdr(skb);
1314 n = neigh_lookup(&arp_tbl, &pip->daddr, dev); 1329 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1315 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { 1330 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1316 union vxlan_addr ipa = { 1331 union vxlan_addr ipa = {
1317 .sin.sin_addr.s_addr = pip->daddr, 1332 .sin.sin_addr.s_addr = pip->daddr,
1318 .sa.sa_family = AF_INET, 1333 .sa.sa_family = AF_INET,
1319 }; 1334 };
1320 1335
1321 vxlan_ip_miss(dev, &ipa); 1336 vxlan_ip_miss(dev, &ipa);
1322 return false; 1337 return false;
1323 } 1338 }
1324 1339
1325 break; 1340 break;
1326 } 1341 }
1327 #if IS_ENABLED(CONFIG_IPV6) 1342 #if IS_ENABLED(CONFIG_IPV6)
1328 case ETH_P_IPV6: 1343 case ETH_P_IPV6:
1329 { 1344 {
1330 struct ipv6hdr *pip6; 1345 struct ipv6hdr *pip6;
1331 1346
1332 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 1347 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1333 return false; 1348 return false;
1334 pip6 = ipv6_hdr(skb); 1349 pip6 = ipv6_hdr(skb);
1335 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); 1350 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1336 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { 1351 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1337 union vxlan_addr ipa = { 1352 union vxlan_addr ipa = {
1338 .sin6.sin6_addr = pip6->daddr, 1353 .sin6.sin6_addr = pip6->daddr,
1339 .sa.sa_family = AF_INET6, 1354 .sa.sa_family = AF_INET6,
1340 }; 1355 };
1341 1356
1342 vxlan_ip_miss(dev, &ipa); 1357 vxlan_ip_miss(dev, &ipa);
1343 return false; 1358 return false;
1344 } 1359 }
1345 1360
1346 break; 1361 break;
1347 } 1362 }
1348 #endif 1363 #endif
1349 default: 1364 default:
1350 return false; 1365 return false;
1351 } 1366 }
1352 1367
1353 if (n) { 1368 if (n) {
1354 bool diff; 1369 bool diff;
1355 1370
1356 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); 1371 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1357 if (diff) { 1372 if (diff) {
1358 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, 1373 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1359 dev->addr_len); 1374 dev->addr_len);
1360 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); 1375 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1361 } 1376 }
1362 neigh_release(n); 1377 neigh_release(n);
1363 return diff; 1378 return diff;
1364 } 1379 }
1365 1380
1366 return false; 1381 return false;
1367 } 1382 }
1368 1383
1369 static void vxlan_sock_put(struct sk_buff *skb) 1384 static void vxlan_sock_put(struct sk_buff *skb)
1370 { 1385 {
1371 sock_put(skb->sk); 1386 sock_put(skb->sk);
1372 } 1387 }
1373 1388
1374 /* On transmit, associate with the tunnel socket */ 1389 /* On transmit, associate with the tunnel socket */
1375 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb) 1390 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
1376 { 1391 {
1377 skb_orphan(skb); 1392 skb_orphan(skb);
1378 sock_hold(sk); 1393 sock_hold(sk);
1379 skb->sk = sk; 1394 skb->sk = sk;
1380 skb->destructor = vxlan_sock_put; 1395 skb->destructor = vxlan_sock_put;
1381 } 1396 }
1382 1397
1383 /* Compute source port for outgoing packet 1398 /* Compute source port for outgoing packet
1384 * first choice to use L4 flow hash since it will spread 1399 * first choice to use L4 flow hash since it will spread
1385 * better and maybe available from hardware 1400 * better and maybe available from hardware
1386 * secondary choice is to use jhash on the Ethernet header 1401 * secondary choice is to use jhash on the Ethernet header
1387 */ 1402 */
1388 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb) 1403 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
1389 { 1404 {
1390 unsigned int range = (port_max - port_min) + 1; 1405 unsigned int range = (port_max - port_min) + 1;
1391 u32 hash; 1406 u32 hash;
1392 1407
1393 hash = skb_get_rxhash(skb); 1408 hash = skb_get_rxhash(skb);
1394 if (!hash) 1409 if (!hash)
1395 hash = jhash(skb->data, 2 * ETH_ALEN, 1410 hash = jhash(skb->data, 2 * ETH_ALEN,
1396 (__force u32) skb->protocol); 1411 (__force u32) skb->protocol);
1397 1412
1398 return htons((((u64) hash * range) >> 32) + port_min); 1413 return htons((((u64) hash * range) >> 32) + port_min);
1399 } 1414 }
1400 EXPORT_SYMBOL_GPL(vxlan_src_port); 1415 EXPORT_SYMBOL_GPL(vxlan_src_port);
1401 1416
1402 static int handle_offloads(struct sk_buff *skb) 1417 static int handle_offloads(struct sk_buff *skb)
1403 { 1418 {
1404 if (skb_is_gso(skb)) { 1419 if (skb_is_gso(skb)) {
1405 int err = skb_unclone(skb, GFP_ATOMIC); 1420 int err = skb_unclone(skb, GFP_ATOMIC);
1406 if (unlikely(err)) 1421 if (unlikely(err))
1407 return err; 1422 return err;
1408 1423
1409 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1424 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1410 } else if (skb->ip_summed != CHECKSUM_PARTIAL) 1425 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1411 skb->ip_summed = CHECKSUM_NONE; 1426 skb->ip_summed = CHECKSUM_NONE;
1412 1427
1413 return 0; 1428 return 0;
1414 } 1429 }
1415 1430
1416 #if IS_ENABLED(CONFIG_IPV6) 1431 #if IS_ENABLED(CONFIG_IPV6)
1417 static int vxlan6_xmit_skb(struct vxlan_sock *vs, 1432 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1418 struct dst_entry *dst, struct sk_buff *skb, 1433 struct dst_entry *dst, struct sk_buff *skb,
1419 struct net_device *dev, struct in6_addr *saddr, 1434 struct net_device *dev, struct in6_addr *saddr,
1420 struct in6_addr *daddr, __u8 prio, __u8 ttl, 1435 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1421 __be16 src_port, __be16 dst_port, __be32 vni) 1436 __be16 src_port, __be16 dst_port, __be32 vni)
1422 { 1437 {
1423 struct ipv6hdr *ip6h; 1438 struct ipv6hdr *ip6h;
1424 struct vxlanhdr *vxh; 1439 struct vxlanhdr *vxh;
1425 struct udphdr *uh; 1440 struct udphdr *uh;
1426 int min_headroom; 1441 int min_headroom;
1427 int err; 1442 int err;
1428 1443
1429 if (!skb->encapsulation) { 1444 if (!skb->encapsulation) {
1430 skb_reset_inner_headers(skb); 1445 skb_reset_inner_headers(skb);
1431 skb->encapsulation = 1; 1446 skb->encapsulation = 1;
1432 } 1447 }
1433 1448
1434 skb_scrub_packet(skb, false); 1449 skb_scrub_packet(skb, false);
1435 1450
1436 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len 1451 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1437 + VXLAN_HLEN + sizeof(struct ipv6hdr) 1452 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1438 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); 1453 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1439 1454
1440 /* Need space for new headers (invalidates iph ptr) */ 1455 /* Need space for new headers (invalidates iph ptr) */
1441 err = skb_cow_head(skb, min_headroom); 1456 err = skb_cow_head(skb, min_headroom);
1442 if (unlikely(err)) 1457 if (unlikely(err))
1443 return err; 1458 return err;
1444 1459
1445 if (vlan_tx_tag_present(skb)) { 1460 if (vlan_tx_tag_present(skb)) {
1446 if (WARN_ON(!__vlan_put_tag(skb, 1461 if (WARN_ON(!__vlan_put_tag(skb,
1447 skb->vlan_proto, 1462 skb->vlan_proto,
1448 vlan_tx_tag_get(skb)))) 1463 vlan_tx_tag_get(skb))))
1449 return -ENOMEM; 1464 return -ENOMEM;
1450 1465
1451 skb->vlan_tci = 0; 1466 skb->vlan_tci = 0;
1452 } 1467 }
1453 1468
1454 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); 1469 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1455 vxh->vx_flags = htonl(VXLAN_FLAGS); 1470 vxh->vx_flags = htonl(VXLAN_FLAGS);
1456 vxh->vx_vni = vni; 1471 vxh->vx_vni = vni;
1457 1472
1458 __skb_push(skb, sizeof(*uh)); 1473 __skb_push(skb, sizeof(*uh));
1459 skb_reset_transport_header(skb); 1474 skb_reset_transport_header(skb);
1460 uh = udp_hdr(skb); 1475 uh = udp_hdr(skb);
1461 1476
1462 uh->dest = dst_port; 1477 uh->dest = dst_port;
1463 uh->source = src_port; 1478 uh->source = src_port;
1464 1479
1465 uh->len = htons(skb->len); 1480 uh->len = htons(skb->len);
1466 uh->check = 0; 1481 uh->check = 0;
1467 1482
1468 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1483 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1469 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1484 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1470 IPSKB_REROUTED); 1485 IPSKB_REROUTED);
1471 skb_dst_set(skb, dst); 1486 skb_dst_set(skb, dst);
1472 1487
1473 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) { 1488 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1474 __wsum csum = skb_checksum(skb, 0, skb->len, 0); 1489 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1475 skb->ip_summed = CHECKSUM_UNNECESSARY; 1490 skb->ip_summed = CHECKSUM_UNNECESSARY;
1476 uh->check = csum_ipv6_magic(saddr, daddr, skb->len, 1491 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1477 IPPROTO_UDP, csum); 1492 IPPROTO_UDP, csum);
1478 if (uh->check == 0) 1493 if (uh->check == 0)
1479 uh->check = CSUM_MANGLED_0; 1494 uh->check = CSUM_MANGLED_0;
1480 } else { 1495 } else {
1481 skb->ip_summed = CHECKSUM_PARTIAL; 1496 skb->ip_summed = CHECKSUM_PARTIAL;
1482 skb->csum_start = skb_transport_header(skb) - skb->head; 1497 skb->csum_start = skb_transport_header(skb) - skb->head;
1483 skb->csum_offset = offsetof(struct udphdr, check); 1498 skb->csum_offset = offsetof(struct udphdr, check);
1484 uh->check = ~csum_ipv6_magic(saddr, daddr, 1499 uh->check = ~csum_ipv6_magic(saddr, daddr,
1485 skb->len, IPPROTO_UDP, 0); 1500 skb->len, IPPROTO_UDP, 0);
1486 } 1501 }
1487 1502
1488 __skb_push(skb, sizeof(*ip6h)); 1503 __skb_push(skb, sizeof(*ip6h));
1489 skb_reset_network_header(skb); 1504 skb_reset_network_header(skb);
1490 ip6h = ipv6_hdr(skb); 1505 ip6h = ipv6_hdr(skb);
1491 ip6h->version = 6; 1506 ip6h->version = 6;
1492 ip6h->priority = prio; 1507 ip6h->priority = prio;
1493 ip6h->flow_lbl[0] = 0; 1508 ip6h->flow_lbl[0] = 0;
1494 ip6h->flow_lbl[1] = 0; 1509 ip6h->flow_lbl[1] = 0;
1495 ip6h->flow_lbl[2] = 0; 1510 ip6h->flow_lbl[2] = 0;
1496 ip6h->payload_len = htons(skb->len); 1511 ip6h->payload_len = htons(skb->len);
1497 ip6h->nexthdr = IPPROTO_UDP; 1512 ip6h->nexthdr = IPPROTO_UDP;
1498 ip6h->hop_limit = ttl; 1513 ip6h->hop_limit = ttl;
1499 ip6h->daddr = *daddr; 1514 ip6h->daddr = *daddr;
1500 ip6h->saddr = *saddr; 1515 ip6h->saddr = *saddr;
1501 1516
1502 vxlan_set_owner(vs->sock->sk, skb); 1517 vxlan_set_owner(vs->sock->sk, skb);
1503 1518
1504 err = handle_offloads(skb); 1519 err = handle_offloads(skb);
1505 if (err) 1520 if (err)
1506 return err; 1521 return err;
1507 1522
1508 ip6tunnel_xmit(skb, dev); 1523 ip6tunnel_xmit(skb, dev);
1509 return 0; 1524 return 0;
1510 } 1525 }
1511 #endif 1526 #endif
1512 1527
1513 int vxlan_xmit_skb(struct vxlan_sock *vs, 1528 int vxlan_xmit_skb(struct vxlan_sock *vs,
1514 struct rtable *rt, struct sk_buff *skb, 1529 struct rtable *rt, struct sk_buff *skb,
1515 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, 1530 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1516 __be16 src_port, __be16 dst_port, __be32 vni) 1531 __be16 src_port, __be16 dst_port, __be32 vni)
1517 { 1532 {
1518 struct vxlanhdr *vxh; 1533 struct vxlanhdr *vxh;
1519 struct udphdr *uh; 1534 struct udphdr *uh;
1520 int min_headroom; 1535 int min_headroom;
1521 int err; 1536 int err;
1522 1537
1523 if (!skb->encapsulation) { 1538 if (!skb->encapsulation) {
1524 skb_reset_inner_headers(skb); 1539 skb_reset_inner_headers(skb);
1525 skb->encapsulation = 1; 1540 skb->encapsulation = 1;
1526 } 1541 }
1527 1542
1528 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len 1543 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1529 + VXLAN_HLEN + sizeof(struct iphdr) 1544 + VXLAN_HLEN + sizeof(struct iphdr)
1530 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); 1545 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1531 1546
1532 /* Need space for new headers (invalidates iph ptr) */ 1547 /* Need space for new headers (invalidates iph ptr) */
1533 err = skb_cow_head(skb, min_headroom); 1548 err = skb_cow_head(skb, min_headroom);
1534 if (unlikely(err)) 1549 if (unlikely(err))
1535 return err; 1550 return err;
1536 1551
1537 if (vlan_tx_tag_present(skb)) { 1552 if (vlan_tx_tag_present(skb)) {
1538 if (WARN_ON(!__vlan_put_tag(skb, 1553 if (WARN_ON(!__vlan_put_tag(skb,
1539 skb->vlan_proto, 1554 skb->vlan_proto,
1540 vlan_tx_tag_get(skb)))) 1555 vlan_tx_tag_get(skb))))
1541 return -ENOMEM; 1556 return -ENOMEM;
1542 1557
1543 skb->vlan_tci = 0; 1558 skb->vlan_tci = 0;
1544 } 1559 }
1545 1560
1546 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); 1561 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1547 vxh->vx_flags = htonl(VXLAN_FLAGS); 1562 vxh->vx_flags = htonl(VXLAN_FLAGS);
1548 vxh->vx_vni = vni; 1563 vxh->vx_vni = vni;
1549 1564
1550 __skb_push(skb, sizeof(*uh)); 1565 __skb_push(skb, sizeof(*uh));
1551 skb_reset_transport_header(skb); 1566 skb_reset_transport_header(skb);
1552 uh = udp_hdr(skb); 1567 uh = udp_hdr(skb);
1553 1568
1554 uh->dest = dst_port; 1569 uh->dest = dst_port;
1555 uh->source = src_port; 1570 uh->source = src_port;
1556 1571
1557 uh->len = htons(skb->len); 1572 uh->len = htons(skb->len);
1558 uh->check = 0; 1573 uh->check = 0;
1559 1574
1560 vxlan_set_owner(vs->sock->sk, skb); 1575 vxlan_set_owner(vs->sock->sk, skb);
1561 1576
1562 err = handle_offloads(skb); 1577 err = handle_offloads(skb);
1563 if (err) 1578 if (err)
1564 return err; 1579 return err;
1565 1580
1566 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, 1581 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
1567 false); 1582 false);
1568 } 1583 }
1569 EXPORT_SYMBOL_GPL(vxlan_xmit_skb); 1584 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1570 1585
1571 /* Bypass encapsulation if the destination is local */ 1586 /* Bypass encapsulation if the destination is local */
1572 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, 1587 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1573 struct vxlan_dev *dst_vxlan) 1588 struct vxlan_dev *dst_vxlan)
1574 { 1589 {
1575 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); 1590 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1576 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); 1591 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1577 union vxlan_addr loopback; 1592 union vxlan_addr loopback;
1578 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; 1593 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1579 1594
1580 skb->pkt_type = PACKET_HOST; 1595 skb->pkt_type = PACKET_HOST;
1581 skb->encapsulation = 0; 1596 skb->encapsulation = 0;
1582 skb->dev = dst_vxlan->dev; 1597 skb->dev = dst_vxlan->dev;
1583 __skb_pull(skb, skb_network_offset(skb)); 1598 __skb_pull(skb, skb_network_offset(skb));
1584 1599
1585 if (remote_ip->sa.sa_family == AF_INET) { 1600 if (remote_ip->sa.sa_family == AF_INET) {
1586 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1601 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1587 loopback.sa.sa_family = AF_INET; 1602 loopback.sa.sa_family = AF_INET;
1588 #if IS_ENABLED(CONFIG_IPV6) 1603 #if IS_ENABLED(CONFIG_IPV6)
1589 } else { 1604 } else {
1590 loopback.sin6.sin6_addr = in6addr_loopback; 1605 loopback.sin6.sin6_addr = in6addr_loopback;
1591 loopback.sa.sa_family = AF_INET6; 1606 loopback.sa.sa_family = AF_INET6;
1592 #endif 1607 #endif
1593 } 1608 }
1594 1609
1595 if (dst_vxlan->flags & VXLAN_F_LEARN) 1610 if (dst_vxlan->flags & VXLAN_F_LEARN)
1596 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source); 1611 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1597 1612
1598 u64_stats_update_begin(&tx_stats->syncp); 1613 u64_stats_update_begin(&tx_stats->syncp);
1599 tx_stats->tx_packets++; 1614 tx_stats->tx_packets++;
1600 tx_stats->tx_bytes += skb->len; 1615 tx_stats->tx_bytes += skb->len;
1601 u64_stats_update_end(&tx_stats->syncp); 1616 u64_stats_update_end(&tx_stats->syncp);
1602 1617
1603 if (netif_rx(skb) == NET_RX_SUCCESS) { 1618 if (netif_rx(skb) == NET_RX_SUCCESS) {
1604 u64_stats_update_begin(&rx_stats->syncp); 1619 u64_stats_update_begin(&rx_stats->syncp);
1605 rx_stats->rx_packets++; 1620 rx_stats->rx_packets++;
1606 rx_stats->rx_bytes += skb->len; 1621 rx_stats->rx_bytes += skb->len;
1607 u64_stats_update_end(&rx_stats->syncp); 1622 u64_stats_update_end(&rx_stats->syncp);
1608 } else { 1623 } else {
1609 skb->dev->stats.rx_dropped++; 1624 skb->dev->stats.rx_dropped++;
1610 } 1625 }
1611 } 1626 }
1612 1627
1613 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, 1628 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1614 struct vxlan_rdst *rdst, bool did_rsc) 1629 struct vxlan_rdst *rdst, bool did_rsc)
1615 { 1630 {
1616 struct vxlan_dev *vxlan = netdev_priv(dev); 1631 struct vxlan_dev *vxlan = netdev_priv(dev);
1617 struct rtable *rt = NULL; 1632 struct rtable *rt = NULL;
1618 const struct iphdr *old_iph; 1633 const struct iphdr *old_iph;
1619 struct flowi4 fl4; 1634 struct flowi4 fl4;
1620 union vxlan_addr *dst; 1635 union vxlan_addr *dst;
1621 __be16 src_port = 0, dst_port; 1636 __be16 src_port = 0, dst_port;
1622 u32 vni; 1637 u32 vni;
1623 __be16 df = 0; 1638 __be16 df = 0;
1624 __u8 tos, ttl; 1639 __u8 tos, ttl;
1625 int err; 1640 int err;
1626 1641
1627 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; 1642 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1628 vni = rdst->remote_vni; 1643 vni = rdst->remote_vni;
1629 dst = &rdst->remote_ip; 1644 dst = &rdst->remote_ip;
1630 1645
1631 if (vxlan_addr_any(dst)) { 1646 if (vxlan_addr_any(dst)) {
1632 if (did_rsc) { 1647 if (did_rsc) {
1633 /* short-circuited back to local bridge */ 1648 /* short-circuited back to local bridge */
1634 vxlan_encap_bypass(skb, vxlan, vxlan); 1649 vxlan_encap_bypass(skb, vxlan, vxlan);
1635 return; 1650 return;
1636 } 1651 }
1637 goto drop; 1652 goto drop;
1638 } 1653 }
1639 1654
1640 old_iph = ip_hdr(skb); 1655 old_iph = ip_hdr(skb);
1641 1656
1642 ttl = vxlan->ttl; 1657 ttl = vxlan->ttl;
1643 if (!ttl && vxlan_addr_multicast(dst)) 1658 if (!ttl && vxlan_addr_multicast(dst))
1644 ttl = 1; 1659 ttl = 1;
1645 1660
1646 tos = vxlan->tos; 1661 tos = vxlan->tos;
1647 if (tos == 1) 1662 if (tos == 1)
1648 tos = ip_tunnel_get_dsfield(old_iph, skb); 1663 tos = ip_tunnel_get_dsfield(old_iph, skb);
1649 1664
1650 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb); 1665 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
1651 1666
1652 if (dst->sa.sa_family == AF_INET) { 1667 if (dst->sa.sa_family == AF_INET) {
1653 memset(&fl4, 0, sizeof(fl4)); 1668 memset(&fl4, 0, sizeof(fl4));
1654 fl4.flowi4_oif = rdst->remote_ifindex; 1669 fl4.flowi4_oif = rdst->remote_ifindex;
1655 fl4.flowi4_tos = RT_TOS(tos); 1670 fl4.flowi4_tos = RT_TOS(tos);
1656 fl4.daddr = dst->sin.sin_addr.s_addr; 1671 fl4.daddr = dst->sin.sin_addr.s_addr;
1657 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr; 1672 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1658 1673
1659 rt = ip_route_output_key(dev_net(dev), &fl4); 1674 rt = ip_route_output_key(dev_net(dev), &fl4);
1660 if (IS_ERR(rt)) { 1675 if (IS_ERR(rt)) {
1661 netdev_dbg(dev, "no route to %pI4\n", 1676 netdev_dbg(dev, "no route to %pI4\n",
1662 &dst->sin.sin_addr.s_addr); 1677 &dst->sin.sin_addr.s_addr);
1663 dev->stats.tx_carrier_errors++; 1678 dev->stats.tx_carrier_errors++;
1664 goto tx_error; 1679 goto tx_error;
1665 } 1680 }
1666 1681
1667 if (rt->dst.dev == dev) { 1682 if (rt->dst.dev == dev) {
1668 netdev_dbg(dev, "circular route to %pI4\n", 1683 netdev_dbg(dev, "circular route to %pI4\n",
1669 &dst->sin.sin_addr.s_addr); 1684 &dst->sin.sin_addr.s_addr);
1670 dev->stats.collisions++; 1685 dev->stats.collisions++;
1671 goto tx_error; 1686 goto tx_error;
1672 } 1687 }
1673 1688
1674 /* Bypass encapsulation if the destination is local */ 1689 /* Bypass encapsulation if the destination is local */
1675 if (rt->rt_flags & RTCF_LOCAL && 1690 if (rt->rt_flags & RTCF_LOCAL &&
1676 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { 1691 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1677 struct vxlan_dev *dst_vxlan; 1692 struct vxlan_dev *dst_vxlan;
1678 1693
1679 ip_rt_put(rt); 1694 ip_rt_put(rt);
1680 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); 1695 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1681 if (!dst_vxlan) 1696 if (!dst_vxlan)
1682 goto tx_error; 1697 goto tx_error;
1683 vxlan_encap_bypass(skb, vxlan, dst_vxlan); 1698 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1684 return; 1699 return;
1685 } 1700 }
1686 1701
1687 tos = ip_tunnel_ecn_encap(tos, old_iph, skb); 1702 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1688 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 1703 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1689 1704
1690 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb, 1705 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1691 fl4.saddr, dst->sin.sin_addr.s_addr, 1706 fl4.saddr, dst->sin.sin_addr.s_addr,
1692 tos, ttl, df, src_port, dst_port, 1707 tos, ttl, df, src_port, dst_port,
1693 htonl(vni << 8)); 1708 htonl(vni << 8));
1694 1709
1695 if (err < 0) 1710 if (err < 0)
1696 goto rt_tx_error; 1711 goto rt_tx_error;
1697 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 1712 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1698 #if IS_ENABLED(CONFIG_IPV6) 1713 #if IS_ENABLED(CONFIG_IPV6)
1699 } else { 1714 } else {
1700 struct sock *sk = vxlan->vn_sock->sock->sk; 1715 struct sock *sk = vxlan->vn_sock->sock->sk;
1701 struct dst_entry *ndst; 1716 struct dst_entry *ndst;
1702 struct flowi6 fl6; 1717 struct flowi6 fl6;
1703 u32 flags; 1718 u32 flags;
1704 1719
1705 memset(&fl6, 0, sizeof(fl6)); 1720 memset(&fl6, 0, sizeof(fl6));
1706 fl6.flowi6_oif = rdst->remote_ifindex; 1721 fl6.flowi6_oif = rdst->remote_ifindex;
1707 fl6.daddr = dst->sin6.sin6_addr; 1722 fl6.daddr = dst->sin6.sin6_addr;
1708 fl6.saddr = vxlan->saddr.sin6.sin6_addr; 1723 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1709 fl6.flowi6_proto = IPPROTO_UDP; 1724 fl6.flowi6_proto = IPPROTO_UDP;
1710 1725
1711 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) { 1726 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1712 netdev_dbg(dev, "no route to %pI6\n", 1727 netdev_dbg(dev, "no route to %pI6\n",
1713 &dst->sin6.sin6_addr); 1728 &dst->sin6.sin6_addr);
1714 dev->stats.tx_carrier_errors++; 1729 dev->stats.tx_carrier_errors++;
1715 goto tx_error; 1730 goto tx_error;
1716 } 1731 }
1717 1732
1718 if (ndst->dev == dev) { 1733 if (ndst->dev == dev) {
1719 netdev_dbg(dev, "circular route to %pI6\n", 1734 netdev_dbg(dev, "circular route to %pI6\n",
1720 &dst->sin6.sin6_addr); 1735 &dst->sin6.sin6_addr);
1721 dst_release(ndst); 1736 dst_release(ndst);
1722 dev->stats.collisions++; 1737 dev->stats.collisions++;
1723 goto tx_error; 1738 goto tx_error;
1724 } 1739 }
1725 1740
1726 /* Bypass encapsulation if the destination is local */ 1741 /* Bypass encapsulation if the destination is local */
1727 flags = ((struct rt6_info *)ndst)->rt6i_flags; 1742 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1728 if (flags & RTF_LOCAL && 1743 if (flags & RTF_LOCAL &&
1729 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { 1744 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1730 struct vxlan_dev *dst_vxlan; 1745 struct vxlan_dev *dst_vxlan;
1731 1746
1732 dst_release(ndst); 1747 dst_release(ndst);
1733 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); 1748 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1734 if (!dst_vxlan) 1749 if (!dst_vxlan)
1735 goto tx_error; 1750 goto tx_error;
1736 vxlan_encap_bypass(skb, vxlan, dst_vxlan); 1751 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1737 return; 1752 return;
1738 } 1753 }
1739 1754
1740 ttl = ttl ? : ip6_dst_hoplimit(ndst); 1755 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1741 1756
1742 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb, 1757 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
1743 dev, &fl6.saddr, &fl6.daddr, 0, ttl, 1758 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1744 src_port, dst_port, htonl(vni << 8)); 1759 src_port, dst_port, htonl(vni << 8));
1745 #endif 1760 #endif
1746 } 1761 }
1747 1762
1748 return; 1763 return;
1749 1764
1750 drop: 1765 drop:
1751 dev->stats.tx_dropped++; 1766 dev->stats.tx_dropped++;
1752 goto tx_free; 1767 goto tx_free;
1753 1768
1754 rt_tx_error: 1769 rt_tx_error:
1755 ip_rt_put(rt); 1770 ip_rt_put(rt);
1756 tx_error: 1771 tx_error:
1757 dev->stats.tx_errors++; 1772 dev->stats.tx_errors++;
1758 tx_free: 1773 tx_free:
1759 dev_kfree_skb(skb); 1774 dev_kfree_skb(skb);
1760 } 1775 }
1761 1776
1762 /* Transmit local packets over Vxlan 1777 /* Transmit local packets over Vxlan
1763 * 1778 *
1764 * Outer IP header inherits ECN and DF from inner header. 1779 * Outer IP header inherits ECN and DF from inner header.
1765 * Outer UDP destination is the VXLAN assigned port. 1780 * Outer UDP destination is the VXLAN assigned port.
1766 * source port is based on hash of flow 1781 * source port is based on hash of flow
1767 */ 1782 */
1768 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) 1783 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1769 { 1784 {
1770 struct vxlan_dev *vxlan = netdev_priv(dev); 1785 struct vxlan_dev *vxlan = netdev_priv(dev);
1771 struct ethhdr *eth; 1786 struct ethhdr *eth;
1772 bool did_rsc = false; 1787 bool did_rsc = false;
1773 struct vxlan_rdst *rdst; 1788 struct vxlan_rdst *rdst;
1774 struct vxlan_fdb *f; 1789 struct vxlan_fdb *f;
1775 1790
1776 skb_reset_mac_header(skb); 1791 skb_reset_mac_header(skb);
1777 eth = eth_hdr(skb); 1792 eth = eth_hdr(skb);
1778 1793
1779 if ((vxlan->flags & VXLAN_F_PROXY)) { 1794 if ((vxlan->flags & VXLAN_F_PROXY)) {
1780 if (ntohs(eth->h_proto) == ETH_P_ARP) 1795 if (ntohs(eth->h_proto) == ETH_P_ARP)
1781 return arp_reduce(dev, skb); 1796 return arp_reduce(dev, skb);
1782 #if IS_ENABLED(CONFIG_IPV6) 1797 #if IS_ENABLED(CONFIG_IPV6)
1783 else if (ntohs(eth->h_proto) == ETH_P_IPV6 && 1798 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1784 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) && 1799 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1785 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { 1800 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1786 struct nd_msg *msg; 1801 struct nd_msg *msg;
1787 1802
1788 msg = (struct nd_msg *)skb_transport_header(skb); 1803 msg = (struct nd_msg *)skb_transport_header(skb);
1789 if (msg->icmph.icmp6_code == 0 && 1804 if (msg->icmph.icmp6_code == 0 &&
1790 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) 1805 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1791 return neigh_reduce(dev, skb); 1806 return neigh_reduce(dev, skb);
1792 } 1807 }
1793 #endif 1808 #endif
1794 } 1809 }
1795 1810
1796 f = vxlan_find_mac(vxlan, eth->h_dest); 1811 f = vxlan_find_mac(vxlan, eth->h_dest);
1797 did_rsc = false; 1812 did_rsc = false;
1798 1813
1799 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && 1814 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1800 (ntohs(eth->h_proto) == ETH_P_IP || 1815 (ntohs(eth->h_proto) == ETH_P_IP ||
1801 ntohs(eth->h_proto) == ETH_P_IPV6)) { 1816 ntohs(eth->h_proto) == ETH_P_IPV6)) {
1802 did_rsc = route_shortcircuit(dev, skb); 1817 did_rsc = route_shortcircuit(dev, skb);
1803 if (did_rsc) 1818 if (did_rsc)
1804 f = vxlan_find_mac(vxlan, eth->h_dest); 1819 f = vxlan_find_mac(vxlan, eth->h_dest);
1805 } 1820 }
1806 1821
1807 if (f == NULL) { 1822 if (f == NULL) {
1808 f = vxlan_find_mac(vxlan, all_zeros_mac); 1823 f = vxlan_find_mac(vxlan, all_zeros_mac);
1809 if (f == NULL) { 1824 if (f == NULL) {
1810 if ((vxlan->flags & VXLAN_F_L2MISS) && 1825 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1811 !is_multicast_ether_addr(eth->h_dest)) 1826 !is_multicast_ether_addr(eth->h_dest))
1812 vxlan_fdb_miss(vxlan, eth->h_dest); 1827 vxlan_fdb_miss(vxlan, eth->h_dest);
1813 1828
1814 dev->stats.tx_dropped++; 1829 dev->stats.tx_dropped++;
1815 dev_kfree_skb(skb); 1830 dev_kfree_skb(skb);
1816 return NETDEV_TX_OK; 1831 return NETDEV_TX_OK;
1817 } 1832 }
1818 } 1833 }
1819 1834
1820 list_for_each_entry_rcu(rdst, &f->remotes, list) { 1835 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1821 struct sk_buff *skb1; 1836 struct sk_buff *skb1;
1822 1837
1823 skb1 = skb_clone(skb, GFP_ATOMIC); 1838 skb1 = skb_clone(skb, GFP_ATOMIC);
1824 if (skb1) 1839 if (skb1)
1825 vxlan_xmit_one(skb1, dev, rdst, did_rsc); 1840 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1826 } 1841 }
1827 1842
1828 dev_kfree_skb(skb); 1843 dev_kfree_skb(skb);
1829 return NETDEV_TX_OK; 1844 return NETDEV_TX_OK;
1830 } 1845 }
1831 1846
1832 /* Walk the forwarding table and purge stale entries */ 1847 /* Walk the forwarding table and purge stale entries */
1833 static void vxlan_cleanup(unsigned long arg) 1848 static void vxlan_cleanup(unsigned long arg)
1834 { 1849 {
1835 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; 1850 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1836 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; 1851 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1837 unsigned int h; 1852 unsigned int h;
1838 1853
1839 if (!netif_running(vxlan->dev)) 1854 if (!netif_running(vxlan->dev))
1840 return; 1855 return;
1841 1856
1842 spin_lock_bh(&vxlan->hash_lock); 1857 spin_lock_bh(&vxlan->hash_lock);
1843 for (h = 0; h < FDB_HASH_SIZE; ++h) { 1858 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1844 struct hlist_node *p, *n; 1859 struct hlist_node *p, *n;
1845 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { 1860 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1846 struct vxlan_fdb *f 1861 struct vxlan_fdb *f
1847 = container_of(p, struct vxlan_fdb, hlist); 1862 = container_of(p, struct vxlan_fdb, hlist);
1848 unsigned long timeout; 1863 unsigned long timeout;
1849 1864
1850 if (f->state & NUD_PERMANENT) 1865 if (f->state & NUD_PERMANENT)
1851 continue; 1866 continue;
1852 1867
1853 timeout = f->used + vxlan->age_interval * HZ; 1868 timeout = f->used + vxlan->age_interval * HZ;
1854 if (time_before_eq(timeout, jiffies)) { 1869 if (time_before_eq(timeout, jiffies)) {
1855 netdev_dbg(vxlan->dev, 1870 netdev_dbg(vxlan->dev,
1856 "garbage collect %pM\n", 1871 "garbage collect %pM\n",
1857 f->eth_addr); 1872 f->eth_addr);
1858 f->state = NUD_STALE; 1873 f->state = NUD_STALE;
1859 vxlan_fdb_destroy(vxlan, f); 1874 vxlan_fdb_destroy(vxlan, f);
1860 } else if (time_before(timeout, next_timer)) 1875 } else if (time_before(timeout, next_timer))
1861 next_timer = timeout; 1876 next_timer = timeout;
1862 } 1877 }
1863 } 1878 }
1864 spin_unlock_bh(&vxlan->hash_lock); 1879 spin_unlock_bh(&vxlan->hash_lock);
1865 1880
1866 mod_timer(&vxlan->age_timer, next_timer); 1881 mod_timer(&vxlan->age_timer, next_timer);
1867 } 1882 }
1868 1883
1869 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) 1884 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1870 { 1885 {
1871 __u32 vni = vxlan->default_dst.remote_vni; 1886 __u32 vni = vxlan->default_dst.remote_vni;
1872 1887
1873 vxlan->vn_sock = vs; 1888 vxlan->vn_sock = vs;
1874 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); 1889 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1875 } 1890 }
1876 1891
1877 /* Setup stats when device is created */ 1892 /* Setup stats when device is created */
1878 static int vxlan_init(struct net_device *dev) 1893 static int vxlan_init(struct net_device *dev)
1879 { 1894 {
1880 struct vxlan_dev *vxlan = netdev_priv(dev); 1895 struct vxlan_dev *vxlan = netdev_priv(dev);
1881 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); 1896 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1882 struct vxlan_sock *vs; 1897 struct vxlan_sock *vs;
1883 int i; 1898 int i;
1884 1899
1885 dev->tstats = alloc_percpu(struct pcpu_tstats); 1900 dev->tstats = alloc_percpu(struct pcpu_tstats);
1886 if (!dev->tstats) 1901 if (!dev->tstats)
1887 return -ENOMEM; 1902 return -ENOMEM;
1888 1903
1889 for_each_possible_cpu(i) { 1904 for_each_possible_cpu(i) {
1890 struct pcpu_tstats *vxlan_stats; 1905 struct pcpu_tstats *vxlan_stats;
1891 vxlan_stats = per_cpu_ptr(dev->tstats, i); 1906 vxlan_stats = per_cpu_ptr(dev->tstats, i);
1892 u64_stats_init(&vxlan_stats->syncp); 1907 u64_stats_init(&vxlan_stats->syncp);
1893 } 1908 }
1894 1909
1895 1910
1896 spin_lock(&vn->sock_lock); 1911 spin_lock(&vn->sock_lock);
1897 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port); 1912 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1898 if (vs) { 1913 if (vs) {
1899 /* If we have a socket with same port already, reuse it */ 1914 /* If we have a socket with same port already, reuse it */
1900 atomic_inc(&vs->refcnt); 1915 atomic_inc(&vs->refcnt);
1901 vxlan_vs_add_dev(vs, vxlan); 1916 vxlan_vs_add_dev(vs, vxlan);
1902 } else { 1917 } else {
1903 /* otherwise make new socket outside of RTNL */ 1918 /* otherwise make new socket outside of RTNL */
1904 dev_hold(dev); 1919 dev_hold(dev);
1905 queue_work(vxlan_wq, &vxlan->sock_work); 1920 queue_work(vxlan_wq, &vxlan->sock_work);
1906 } 1921 }
1907 spin_unlock(&vn->sock_lock); 1922 spin_unlock(&vn->sock_lock);
1908 1923
1909 return 0; 1924 return 0;
1910 } 1925 }
1911 1926
1912 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) 1927 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1913 { 1928 {
1914 struct vxlan_fdb *f; 1929 struct vxlan_fdb *f;
1915 1930
1916 spin_lock_bh(&vxlan->hash_lock); 1931 spin_lock_bh(&vxlan->hash_lock);
1917 f = __vxlan_find_mac(vxlan, all_zeros_mac); 1932 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1918 if (f) 1933 if (f)
1919 vxlan_fdb_destroy(vxlan, f); 1934 vxlan_fdb_destroy(vxlan, f);
1920 spin_unlock_bh(&vxlan->hash_lock); 1935 spin_unlock_bh(&vxlan->hash_lock);
1921 } 1936 }
1922 1937
1923 static void vxlan_uninit(struct net_device *dev) 1938 static void vxlan_uninit(struct net_device *dev)
1924 { 1939 {
1925 struct vxlan_dev *vxlan = netdev_priv(dev); 1940 struct vxlan_dev *vxlan = netdev_priv(dev);
1926 struct vxlan_sock *vs = vxlan->vn_sock; 1941 struct vxlan_sock *vs = vxlan->vn_sock;
1927 1942
1928 vxlan_fdb_delete_default(vxlan); 1943 vxlan_fdb_delete_default(vxlan);
1929 1944
1930 if (vs) 1945 if (vs)
1931 vxlan_sock_release(vs); 1946 vxlan_sock_release(vs);
1932 free_percpu(dev->tstats); 1947 free_percpu(dev->tstats);
1933 } 1948 }
1934 1949
1935 /* Start ageing timer and join group when device is brought up */ 1950 /* Start ageing timer and join group when device is brought up */
1936 static int vxlan_open(struct net_device *dev) 1951 static int vxlan_open(struct net_device *dev)
1937 { 1952 {
1938 struct vxlan_dev *vxlan = netdev_priv(dev); 1953 struct vxlan_dev *vxlan = netdev_priv(dev);
1939 struct vxlan_sock *vs = vxlan->vn_sock; 1954 struct vxlan_sock *vs = vxlan->vn_sock;
1940 1955
1941 /* socket hasn't been created */ 1956 /* socket hasn't been created */
1942 if (!vs) 1957 if (!vs)
1943 return -ENOTCONN; 1958 return -ENOTCONN;
1944 1959
1945 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { 1960 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
1946 vxlan_sock_hold(vs); 1961 vxlan_sock_hold(vs);
1947 dev_hold(dev); 1962 dev_hold(dev);
1948 queue_work(vxlan_wq, &vxlan->igmp_join); 1963 queue_work(vxlan_wq, &vxlan->igmp_join);
1949 } 1964 }
1950 1965
1951 if (vxlan->age_interval) 1966 if (vxlan->age_interval)
1952 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); 1967 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1953 1968
1954 return 0; 1969 return 0;
1955 } 1970 }
1956 1971
1957 /* Purge the forwarding table */ 1972 /* Purge the forwarding table */
1958 static void vxlan_flush(struct vxlan_dev *vxlan) 1973 static void vxlan_flush(struct vxlan_dev *vxlan)
1959 { 1974 {
1960 unsigned int h; 1975 unsigned int h;
1961 1976
1962 spin_lock_bh(&vxlan->hash_lock); 1977 spin_lock_bh(&vxlan->hash_lock);
1963 for (h = 0; h < FDB_HASH_SIZE; ++h) { 1978 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1964 struct hlist_node *p, *n; 1979 struct hlist_node *p, *n;
1965 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { 1980 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1966 struct vxlan_fdb *f 1981 struct vxlan_fdb *f
1967 = container_of(p, struct vxlan_fdb, hlist); 1982 = container_of(p, struct vxlan_fdb, hlist);
1968 /* the all_zeros_mac entry is deleted at vxlan_uninit */ 1983 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1969 if (!is_zero_ether_addr(f->eth_addr)) 1984 if (!is_zero_ether_addr(f->eth_addr))
1970 vxlan_fdb_destroy(vxlan, f); 1985 vxlan_fdb_destroy(vxlan, f);
1971 } 1986 }
1972 } 1987 }
1973 spin_unlock_bh(&vxlan->hash_lock); 1988 spin_unlock_bh(&vxlan->hash_lock);
1974 } 1989 }
1975 1990
1976 /* Cleanup timer and forwarding table on shutdown */ 1991 /* Cleanup timer and forwarding table on shutdown */
1977 static int vxlan_stop(struct net_device *dev) 1992 static int vxlan_stop(struct net_device *dev)
1978 { 1993 {
1979 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); 1994 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1980 struct vxlan_dev *vxlan = netdev_priv(dev); 1995 struct vxlan_dev *vxlan = netdev_priv(dev);
1981 struct vxlan_sock *vs = vxlan->vn_sock; 1996 struct vxlan_sock *vs = vxlan->vn_sock;
1982 1997
1983 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && 1998 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1984 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) { 1999 !vxlan_group_used(vn, vxlan)) {
1985 vxlan_sock_hold(vs); 2000 vxlan_sock_hold(vs);
1986 dev_hold(dev); 2001 dev_hold(dev);
1987 queue_work(vxlan_wq, &vxlan->igmp_leave); 2002 queue_work(vxlan_wq, &vxlan->igmp_leave);
1988 } 2003 }
1989 2004
1990 del_timer_sync(&vxlan->age_timer); 2005 del_timer_sync(&vxlan->age_timer);
1991 2006
1992 vxlan_flush(vxlan); 2007 vxlan_flush(vxlan);
1993 2008
1994 return 0; 2009 return 0;
1995 } 2010 }
1996 2011
1997 /* Stub, nothing needs to be done. */ 2012 /* Stub, nothing needs to be done. */
1998 static void vxlan_set_multicast_list(struct net_device *dev) 2013 static void vxlan_set_multicast_list(struct net_device *dev)
1999 { 2014 {
2000 } 2015 }
2001 2016
2002 static const struct net_device_ops vxlan_netdev_ops = { 2017 static const struct net_device_ops vxlan_netdev_ops = {
2003 .ndo_init = vxlan_init, 2018 .ndo_init = vxlan_init,
2004 .ndo_uninit = vxlan_uninit, 2019 .ndo_uninit = vxlan_uninit,
2005 .ndo_open = vxlan_open, 2020 .ndo_open = vxlan_open,
2006 .ndo_stop = vxlan_stop, 2021 .ndo_stop = vxlan_stop,
2007 .ndo_start_xmit = vxlan_xmit, 2022 .ndo_start_xmit = vxlan_xmit,
2008 .ndo_get_stats64 = ip_tunnel_get_stats64, 2023 .ndo_get_stats64 = ip_tunnel_get_stats64,
2009 .ndo_set_rx_mode = vxlan_set_multicast_list, 2024 .ndo_set_rx_mode = vxlan_set_multicast_list,
2010 .ndo_change_mtu = eth_change_mtu, 2025 .ndo_change_mtu = eth_change_mtu,
2011 .ndo_validate_addr = eth_validate_addr, 2026 .ndo_validate_addr = eth_validate_addr,
2012 .ndo_set_mac_address = eth_mac_addr, 2027 .ndo_set_mac_address = eth_mac_addr,
2013 .ndo_fdb_add = vxlan_fdb_add, 2028 .ndo_fdb_add = vxlan_fdb_add,
2014 .ndo_fdb_del = vxlan_fdb_delete, 2029 .ndo_fdb_del = vxlan_fdb_delete,
2015 .ndo_fdb_dump = vxlan_fdb_dump, 2030 .ndo_fdb_dump = vxlan_fdb_dump,
2016 }; 2031 };
2017 2032
2018 /* Info for udev, that this is a virtual tunnel endpoint */ 2033 /* Info for udev, that this is a virtual tunnel endpoint */
2019 static struct device_type vxlan_type = { 2034 static struct device_type vxlan_type = {
2020 .name = "vxlan", 2035 .name = "vxlan",
2021 }; 2036 };
2022 2037
2023 /* Calls the ndo_add_vxlan_port of the caller in order to 2038 /* Calls the ndo_add_vxlan_port of the caller in order to
2024 * supply the listening VXLAN udp ports. Callers are expected 2039 * supply the listening VXLAN udp ports. Callers are expected
2025 * to implement the ndo_add_vxlan_port. 2040 * to implement the ndo_add_vxlan_port.
2026 */ 2041 */
2027 void vxlan_get_rx_port(struct net_device *dev) 2042 void vxlan_get_rx_port(struct net_device *dev)
2028 { 2043 {
2029 struct vxlan_sock *vs; 2044 struct vxlan_sock *vs;
2030 struct net *net = dev_net(dev); 2045 struct net *net = dev_net(dev);
2031 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2046 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2032 sa_family_t sa_family; 2047 sa_family_t sa_family;
2033 __be16 port; 2048 __be16 port;
2034 unsigned int i; 2049 unsigned int i;
2035 2050
2036 spin_lock(&vn->sock_lock); 2051 spin_lock(&vn->sock_lock);
2037 for (i = 0; i < PORT_HASH_SIZE; ++i) { 2052 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2038 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { 2053 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2039 port = inet_sk(vs->sock->sk)->inet_sport; 2054 port = inet_sk(vs->sock->sk)->inet_sport;
2040 sa_family = vs->sock->sk->sk_family; 2055 sa_family = vs->sock->sk->sk_family;
2041 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, 2056 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2042 port); 2057 port);
2043 } 2058 }
2044 } 2059 }
2045 spin_unlock(&vn->sock_lock); 2060 spin_unlock(&vn->sock_lock);
2046 } 2061 }
2047 EXPORT_SYMBOL_GPL(vxlan_get_rx_port); 2062 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2048 2063
2049 /* Initialize the device structure. */ 2064 /* Initialize the device structure. */
2050 static void vxlan_setup(struct net_device *dev) 2065 static void vxlan_setup(struct net_device *dev)
2051 { 2066 {
2052 struct vxlan_dev *vxlan = netdev_priv(dev); 2067 struct vxlan_dev *vxlan = netdev_priv(dev);
2053 unsigned int h; 2068 unsigned int h;
2054 int low, high; 2069 int low, high;
2055 2070
2056 eth_hw_addr_random(dev); 2071 eth_hw_addr_random(dev);
2057 ether_setup(dev); 2072 ether_setup(dev);
2058 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6) 2073 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2059 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM; 2074 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
2060 else 2075 else
2061 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; 2076 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
2062 2077
2063 dev->netdev_ops = &vxlan_netdev_ops; 2078 dev->netdev_ops = &vxlan_netdev_ops;
2064 dev->destructor = free_netdev; 2079 dev->destructor = free_netdev;
2065 SET_NETDEV_DEVTYPE(dev, &vxlan_type); 2080 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2066 2081
2067 dev->tx_queue_len = 0; 2082 dev->tx_queue_len = 0;
2068 dev->features |= NETIF_F_LLTX; 2083 dev->features |= NETIF_F_LLTX;
2069 dev->features |= NETIF_F_NETNS_LOCAL; 2084 dev->features |= NETIF_F_NETNS_LOCAL;
2070 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; 2085 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2071 dev->features |= NETIF_F_RXCSUM; 2086 dev->features |= NETIF_F_RXCSUM;
2072 dev->features |= NETIF_F_GSO_SOFTWARE; 2087 dev->features |= NETIF_F_GSO_SOFTWARE;
2073 2088
2074 dev->vlan_features = dev->features; 2089 dev->vlan_features = dev->features;
2075 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 2090 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2076 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 2091 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2077 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 2092 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2078 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 2093 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2079 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 2094 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
2080 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 2095 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2081 2096
2082 INIT_LIST_HEAD(&vxlan->next); 2097 INIT_LIST_HEAD(&vxlan->next);
2083 spin_lock_init(&vxlan->hash_lock); 2098 spin_lock_init(&vxlan->hash_lock);
2084 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); 2099 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2085 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); 2100 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2086 INIT_WORK(&vxlan->sock_work, vxlan_sock_work); 2101 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2087 2102
2088 init_timer_deferrable(&vxlan->age_timer); 2103 init_timer_deferrable(&vxlan->age_timer);
2089 vxlan->age_timer.function = vxlan_cleanup; 2104 vxlan->age_timer.function = vxlan_cleanup;
2090 vxlan->age_timer.data = (unsigned long) vxlan; 2105 vxlan->age_timer.data = (unsigned long) vxlan;
2091 2106
2092 inet_get_local_port_range(dev_net(dev), &low, &high); 2107 inet_get_local_port_range(dev_net(dev), &low, &high);
2093 vxlan->port_min = low; 2108 vxlan->port_min = low;
2094 vxlan->port_max = high; 2109 vxlan->port_max = high;
2095 vxlan->dst_port = htons(vxlan_port); 2110 vxlan->dst_port = htons(vxlan_port);
2096 2111
2097 vxlan->dev = dev; 2112 vxlan->dev = dev;
2098 2113
2099 for (h = 0; h < FDB_HASH_SIZE; ++h) 2114 for (h = 0; h < FDB_HASH_SIZE; ++h)
2100 INIT_HLIST_HEAD(&vxlan->fdb_head[h]); 2115 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2101 } 2116 }
2102 2117
2103 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { 2118 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2104 [IFLA_VXLAN_ID] = { .type = NLA_U32 }, 2119 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2105 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, 2120 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2106 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, 2121 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2107 [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, 2122 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2108 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, 2123 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2109 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, 2124 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2110 [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, 2125 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2111 [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, 2126 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2112 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, 2127 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2113 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, 2128 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2114 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, 2129 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2115 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, 2130 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2116 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, 2131 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2117 [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, 2132 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2118 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, 2133 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2119 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, 2134 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2120 [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, 2135 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2121 }; 2136 };
2122 2137
2123 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) 2138 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2124 { 2139 {
2125 if (tb[IFLA_ADDRESS]) { 2140 if (tb[IFLA_ADDRESS]) {
2126 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { 2141 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2127 pr_debug("invalid link address (not ethernet)\n"); 2142 pr_debug("invalid link address (not ethernet)\n");
2128 return -EINVAL; 2143 return -EINVAL;
2129 } 2144 }
2130 2145
2131 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { 2146 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2132 pr_debug("invalid all zero ethernet address\n"); 2147 pr_debug("invalid all zero ethernet address\n");
2133 return -EADDRNOTAVAIL; 2148 return -EADDRNOTAVAIL;
2134 } 2149 }
2135 } 2150 }
2136 2151
2137 if (!data) 2152 if (!data)
2138 return -EINVAL; 2153 return -EINVAL;
2139 2154
2140 if (data[IFLA_VXLAN_ID]) { 2155 if (data[IFLA_VXLAN_ID]) {
2141 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); 2156 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2142 if (id >= VXLAN_VID_MASK) 2157 if (id >= VXLAN_VID_MASK)
2143 return -ERANGE; 2158 return -ERANGE;
2144 } 2159 }
2145 2160
2146 if (data[IFLA_VXLAN_PORT_RANGE]) { 2161 if (data[IFLA_VXLAN_PORT_RANGE]) {
2147 const struct ifla_vxlan_port_range *p 2162 const struct ifla_vxlan_port_range *p
2148 = nla_data(data[IFLA_VXLAN_PORT_RANGE]); 2163 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2149 2164
2150 if (ntohs(p->high) < ntohs(p->low)) { 2165 if (ntohs(p->high) < ntohs(p->low)) {
2151 pr_debug("port range %u .. %u not valid\n", 2166 pr_debug("port range %u .. %u not valid\n",
2152 ntohs(p->low), ntohs(p->high)); 2167 ntohs(p->low), ntohs(p->high));
2153 return -EINVAL; 2168 return -EINVAL;
2154 } 2169 }
2155 } 2170 }
2156 2171
2157 return 0; 2172 return 0;
2158 } 2173 }
2159 2174
2160 static void vxlan_get_drvinfo(struct net_device *netdev, 2175 static void vxlan_get_drvinfo(struct net_device *netdev,
2161 struct ethtool_drvinfo *drvinfo) 2176 struct ethtool_drvinfo *drvinfo)
2162 { 2177 {
2163 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); 2178 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2164 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); 2179 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2165 } 2180 }
2166 2181
2167 static const struct ethtool_ops vxlan_ethtool_ops = { 2182 static const struct ethtool_ops vxlan_ethtool_ops = {
2168 .get_drvinfo = vxlan_get_drvinfo, 2183 .get_drvinfo = vxlan_get_drvinfo,
2169 .get_link = ethtool_op_get_link, 2184 .get_link = ethtool_op_get_link,
2170 }; 2185 };
2171 2186
2172 static void vxlan_del_work(struct work_struct *work) 2187 static void vxlan_del_work(struct work_struct *work)
2173 { 2188 {
2174 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); 2189 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2175 2190
2176 sk_release_kernel(vs->sock->sk); 2191 sk_release_kernel(vs->sock->sk);
2177 kfree_rcu(vs, rcu); 2192 kfree_rcu(vs, rcu);
2178 } 2193 }
2179 2194
2180 #if IS_ENABLED(CONFIG_IPV6) 2195 #if IS_ENABLED(CONFIG_IPV6)
2181 /* Create UDP socket for encapsulation receive. AF_INET6 socket 2196 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2182 * could be used for both IPv4 and IPv6 communications, but 2197 * could be used for both IPv4 and IPv6 communications, but
2183 * users may set bindv6only=1. 2198 * users may set bindv6only=1.
2184 */ 2199 */
2185 static struct socket *create_v6_sock(struct net *net, __be16 port) 2200 static struct socket *create_v6_sock(struct net *net, __be16 port)
2186 { 2201 {
2187 struct sock *sk; 2202 struct sock *sk;
2188 struct socket *sock; 2203 struct socket *sock;
2189 struct sockaddr_in6 vxlan_addr = { 2204 struct sockaddr_in6 vxlan_addr = {
2190 .sin6_family = AF_INET6, 2205 .sin6_family = AF_INET6,
2191 .sin6_port = port, 2206 .sin6_port = port,
2192 }; 2207 };
2193 int rc, val = 1; 2208 int rc, val = 1;
2194 2209
2195 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 2210 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2196 if (rc < 0) { 2211 if (rc < 0) {
2197 pr_debug("UDPv6 socket create failed\n"); 2212 pr_debug("UDPv6 socket create failed\n");
2198 return ERR_PTR(rc); 2213 return ERR_PTR(rc);
2199 } 2214 }
2200 2215
2201 /* Put in proper namespace */ 2216 /* Put in proper namespace */
2202 sk = sock->sk; 2217 sk = sock->sk;
2203 sk_change_net(sk, net); 2218 sk_change_net(sk, net);
2204 2219
2205 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, 2220 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2206 (char *)&val, sizeof(val)); 2221 (char *)&val, sizeof(val));
2207 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr, 2222 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2208 sizeof(struct sockaddr_in6)); 2223 sizeof(struct sockaddr_in6));
2209 if (rc < 0) { 2224 if (rc < 0) {
2210 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n", 2225 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2211 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc); 2226 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2212 sk_release_kernel(sk); 2227 sk_release_kernel(sk);
2213 return ERR_PTR(rc); 2228 return ERR_PTR(rc);
2214 } 2229 }
2215 /* At this point, IPv6 module should have been loaded in 2230 /* At this point, IPv6 module should have been loaded in
2216 * sock_create_kern(). 2231 * sock_create_kern().
2217 */ 2232 */
2218 BUG_ON(!ipv6_stub); 2233 BUG_ON(!ipv6_stub);
2219 2234
2220 /* Disable multicast loopback */ 2235 /* Disable multicast loopback */
2221 inet_sk(sk)->mc_loop = 0; 2236 inet_sk(sk)->mc_loop = 0;
2222 return sock; 2237 return sock;
2223 } 2238 }
2224 2239
2225 #else 2240 #else
2226 2241
2227 static struct socket *create_v6_sock(struct net *net, __be16 port) 2242 static struct socket *create_v6_sock(struct net *net, __be16 port)
2228 { 2243 {
2229 return ERR_PTR(-EPFNOSUPPORT); 2244 return ERR_PTR(-EPFNOSUPPORT);
2230 } 2245 }
2231 #endif 2246 #endif
2232 2247
2233 static struct socket *create_v4_sock(struct net *net, __be16 port) 2248 static struct socket *create_v4_sock(struct net *net, __be16 port)
2234 { 2249 {
2235 struct sock *sk; 2250 struct sock *sk;
2236 struct socket *sock; 2251 struct socket *sock;
2237 struct sockaddr_in vxlan_addr = { 2252 struct sockaddr_in vxlan_addr = {
2238 .sin_family = AF_INET, 2253 .sin_family = AF_INET,
2239 .sin_addr.s_addr = htonl(INADDR_ANY), 2254 .sin_addr.s_addr = htonl(INADDR_ANY),
2240 .sin_port = port, 2255 .sin_port = port,
2241 }; 2256 };
2242 int rc; 2257 int rc;
2243 2258
2244 /* Create UDP socket for encapsulation receive. */ 2259 /* Create UDP socket for encapsulation receive. */
2245 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 2260 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2246 if (rc < 0) { 2261 if (rc < 0) {
2247 pr_debug("UDP socket create failed\n"); 2262 pr_debug("UDP socket create failed\n");
2248 return ERR_PTR(rc); 2263 return ERR_PTR(rc);
2249 } 2264 }
2250 2265
2251 /* Put in proper namespace */ 2266 /* Put in proper namespace */
2252 sk = sock->sk; 2267 sk = sock->sk;
2253 sk_change_net(sk, net); 2268 sk_change_net(sk, net);
2254 2269
2255 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr, 2270 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2256 sizeof(vxlan_addr)); 2271 sizeof(vxlan_addr));
2257 if (rc < 0) { 2272 if (rc < 0) {
2258 pr_debug("bind for UDP socket %pI4:%u (%d)\n", 2273 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2259 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); 2274 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2260 sk_release_kernel(sk); 2275 sk_release_kernel(sk);
2261 return ERR_PTR(rc); 2276 return ERR_PTR(rc);
2262 } 2277 }
2263 2278
2264 /* Disable multicast loopback */ 2279 /* Disable multicast loopback */
2265 inet_sk(sk)->mc_loop = 0; 2280 inet_sk(sk)->mc_loop = 0;
2266 return sock; 2281 return sock;
2267 } 2282 }
2268 2283
2269 /* Create new listen socket if needed */ 2284 /* Create new listen socket if needed */
2270 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, 2285 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2271 vxlan_rcv_t *rcv, void *data, bool ipv6) 2286 vxlan_rcv_t *rcv, void *data, bool ipv6)
2272 { 2287 {
2273 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2288 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2274 struct vxlan_sock *vs; 2289 struct vxlan_sock *vs;
2275 struct socket *sock; 2290 struct socket *sock;
2276 struct sock *sk; 2291 struct sock *sk;
2277 unsigned int h; 2292 unsigned int h;
2278 2293
2279 vs = kmalloc(sizeof(*vs), GFP_KERNEL); 2294 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
2280 if (!vs) 2295 if (!vs)
2281 return ERR_PTR(-ENOMEM); 2296 return ERR_PTR(-ENOMEM);
2282 2297
2283 for (h = 0; h < VNI_HASH_SIZE; ++h) 2298 for (h = 0; h < VNI_HASH_SIZE; ++h)
2284 INIT_HLIST_HEAD(&vs->vni_list[h]); 2299 INIT_HLIST_HEAD(&vs->vni_list[h]);
2285 2300
2286 INIT_WORK(&vs->del_work, vxlan_del_work); 2301 INIT_WORK(&vs->del_work, vxlan_del_work);
2287 2302
2288 if (ipv6) 2303 if (ipv6)
2289 sock = create_v6_sock(net, port); 2304 sock = create_v6_sock(net, port);
2290 else 2305 else
2291 sock = create_v4_sock(net, port); 2306 sock = create_v4_sock(net, port);
2292 if (IS_ERR(sock)) { 2307 if (IS_ERR(sock)) {
2293 kfree(vs); 2308 kfree(vs);
2294 return ERR_CAST(sock); 2309 return ERR_CAST(sock);
2295 } 2310 }
2296 2311
2297 vs->sock = sock; 2312 vs->sock = sock;
2298 sk = sock->sk; 2313 sk = sock->sk;
2299 atomic_set(&vs->refcnt, 1); 2314 atomic_set(&vs->refcnt, 1);
2300 vs->rcv = rcv; 2315 vs->rcv = rcv;
2301 vs->data = data; 2316 vs->data = data;
2302 rcu_assign_sk_user_data(vs->sock->sk, vs); 2317 rcu_assign_sk_user_data(vs->sock->sk, vs);
2303 2318
2304 spin_lock(&vn->sock_lock); 2319 spin_lock(&vn->sock_lock);
2305 hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); 2320 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2306 vxlan_notify_add_rx_port(sk); 2321 vxlan_notify_add_rx_port(sk);
2307 spin_unlock(&vn->sock_lock); 2322 spin_unlock(&vn->sock_lock);
2308 2323
2309 /* Mark socket as an encapsulation socket. */ 2324 /* Mark socket as an encapsulation socket. */
2310 udp_sk(sk)->encap_type = 1; 2325 udp_sk(sk)->encap_type = 1;
2311 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; 2326 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
2312 #if IS_ENABLED(CONFIG_IPV6) 2327 #if IS_ENABLED(CONFIG_IPV6)
2313 if (ipv6) 2328 if (ipv6)
2314 ipv6_stub->udpv6_encap_enable(); 2329 ipv6_stub->udpv6_encap_enable();
2315 else 2330 else
2316 #endif 2331 #endif
2317 udp_encap_enable(); 2332 udp_encap_enable();
2318 2333
2319 return vs; 2334 return vs;
2320 } 2335 }
2321 2336
2322 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, 2337 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2323 vxlan_rcv_t *rcv, void *data, 2338 vxlan_rcv_t *rcv, void *data,
2324 bool no_share, bool ipv6) 2339 bool no_share, bool ipv6)
2325 { 2340 {
2326 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2341 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2327 struct vxlan_sock *vs; 2342 struct vxlan_sock *vs;
2328 2343
2329 vs = vxlan_socket_create(net, port, rcv, data, ipv6); 2344 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
2330 if (!IS_ERR(vs)) 2345 if (!IS_ERR(vs))
2331 return vs; 2346 return vs;
2332 2347
2333 if (no_share) /* Return error if sharing is not allowed. */ 2348 if (no_share) /* Return error if sharing is not allowed. */
2334 return vs; 2349 return vs;
2335 2350
2336 spin_lock(&vn->sock_lock); 2351 spin_lock(&vn->sock_lock);
2337 vs = vxlan_find_sock(net, port); 2352 vs = vxlan_find_sock(net, port);
2338 if (vs) { 2353 if (vs) {
2339 if (vs->rcv == rcv) 2354 if (vs->rcv == rcv)
2340 atomic_inc(&vs->refcnt); 2355 atomic_inc(&vs->refcnt);
2341 else 2356 else
2342 vs = ERR_PTR(-EBUSY); 2357 vs = ERR_PTR(-EBUSY);
2343 } 2358 }
2344 spin_unlock(&vn->sock_lock); 2359 spin_unlock(&vn->sock_lock);
2345 2360
2346 if (!vs) 2361 if (!vs)
2347 vs = ERR_PTR(-EINVAL); 2362 vs = ERR_PTR(-EINVAL);
2348 2363
2349 return vs; 2364 return vs;
2350 } 2365 }
2351 EXPORT_SYMBOL_GPL(vxlan_sock_add); 2366 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2352 2367
2353 /* Scheduled at device creation to bind to a socket */ 2368 /* Scheduled at device creation to bind to a socket */
2354 static void vxlan_sock_work(struct work_struct *work) 2369 static void vxlan_sock_work(struct work_struct *work)
2355 { 2370 {
2356 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); 2371 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2357 struct net *net = dev_net(vxlan->dev); 2372 struct net *net = dev_net(vxlan->dev);
2358 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2373 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2359 __be16 port = vxlan->dst_port; 2374 __be16 port = vxlan->dst_port;
2360 struct vxlan_sock *nvs; 2375 struct vxlan_sock *nvs;
2361 2376
2362 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6); 2377 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
2363 spin_lock(&vn->sock_lock); 2378 spin_lock(&vn->sock_lock);
2364 if (!IS_ERR(nvs)) 2379 if (!IS_ERR(nvs))
2365 vxlan_vs_add_dev(nvs, vxlan); 2380 vxlan_vs_add_dev(nvs, vxlan);
2366 spin_unlock(&vn->sock_lock); 2381 spin_unlock(&vn->sock_lock);
2367 2382
2368 dev_put(vxlan->dev); 2383 dev_put(vxlan->dev);
2369 } 2384 }
2370 2385
2371 static int vxlan_newlink(struct net *net, struct net_device *dev, 2386 static int vxlan_newlink(struct net *net, struct net_device *dev,
2372 struct nlattr *tb[], struct nlattr *data[]) 2387 struct nlattr *tb[], struct nlattr *data[])
2373 { 2388 {
2374 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2389 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2375 struct vxlan_dev *vxlan = netdev_priv(dev); 2390 struct vxlan_dev *vxlan = netdev_priv(dev);
2376 struct vxlan_rdst *dst = &vxlan->default_dst; 2391 struct vxlan_rdst *dst = &vxlan->default_dst;
2377 __u32 vni; 2392 __u32 vni;
2378 int err; 2393 int err;
2379 bool use_ipv6 = false; 2394 bool use_ipv6 = false;
2380 2395
2381 if (!data[IFLA_VXLAN_ID]) 2396 if (!data[IFLA_VXLAN_ID])
2382 return -EINVAL; 2397 return -EINVAL;
2383 2398
2384 vni = nla_get_u32(data[IFLA_VXLAN_ID]); 2399 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2385 dst->remote_vni = vni; 2400 dst->remote_vni = vni;
2386 2401
2387 if (data[IFLA_VXLAN_GROUP]) { 2402 if (data[IFLA_VXLAN_GROUP]) {
2388 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); 2403 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2389 dst->remote_ip.sa.sa_family = AF_INET; 2404 dst->remote_ip.sa.sa_family = AF_INET;
2390 } else if (data[IFLA_VXLAN_GROUP6]) { 2405 } else if (data[IFLA_VXLAN_GROUP6]) {
2391 if (!IS_ENABLED(CONFIG_IPV6)) 2406 if (!IS_ENABLED(CONFIG_IPV6))
2392 return -EPFNOSUPPORT; 2407 return -EPFNOSUPPORT;
2393 2408
2394 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6], 2409 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2395 sizeof(struct in6_addr)); 2410 sizeof(struct in6_addr));
2396 dst->remote_ip.sa.sa_family = AF_INET6; 2411 dst->remote_ip.sa.sa_family = AF_INET6;
2397 use_ipv6 = true; 2412 use_ipv6 = true;
2398 } 2413 }
2399 2414
2400 if (data[IFLA_VXLAN_LOCAL]) { 2415 if (data[IFLA_VXLAN_LOCAL]) {
2401 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); 2416 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2402 vxlan->saddr.sa.sa_family = AF_INET; 2417 vxlan->saddr.sa.sa_family = AF_INET;
2403 } else if (data[IFLA_VXLAN_LOCAL6]) { 2418 } else if (data[IFLA_VXLAN_LOCAL6]) {
2404 if (!IS_ENABLED(CONFIG_IPV6)) 2419 if (!IS_ENABLED(CONFIG_IPV6))
2405 return -EPFNOSUPPORT; 2420 return -EPFNOSUPPORT;
2406 2421
2407 /* TODO: respect scope id */ 2422 /* TODO: respect scope id */
2408 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6], 2423 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2409 sizeof(struct in6_addr)); 2424 sizeof(struct in6_addr));
2410 vxlan->saddr.sa.sa_family = AF_INET6; 2425 vxlan->saddr.sa.sa_family = AF_INET6;
2411 use_ipv6 = true; 2426 use_ipv6 = true;
2412 } 2427 }
2413 2428
2414 if (data[IFLA_VXLAN_LINK] && 2429 if (data[IFLA_VXLAN_LINK] &&
2415 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { 2430 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2416 struct net_device *lowerdev 2431 struct net_device *lowerdev
2417 = __dev_get_by_index(net, dst->remote_ifindex); 2432 = __dev_get_by_index(net, dst->remote_ifindex);
2418 2433
2419 if (!lowerdev) { 2434 if (!lowerdev) {
2420 pr_info("ifindex %d does not exist\n", dst->remote_ifindex); 2435 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2421 return -ENODEV; 2436 return -ENODEV;
2422 } 2437 }
2423 2438
2424 #if IS_ENABLED(CONFIG_IPV6) 2439 #if IS_ENABLED(CONFIG_IPV6)
2425 if (use_ipv6) { 2440 if (use_ipv6) {
2426 struct inet6_dev *idev = __in6_dev_get(lowerdev); 2441 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2427 if (idev && idev->cnf.disable_ipv6) { 2442 if (idev && idev->cnf.disable_ipv6) {
2428 pr_info("IPv6 is disabled via sysctl\n"); 2443 pr_info("IPv6 is disabled via sysctl\n");
2429 return -EPERM; 2444 return -EPERM;
2430 } 2445 }
2431 vxlan->flags |= VXLAN_F_IPV6; 2446 vxlan->flags |= VXLAN_F_IPV6;
2432 } 2447 }
2433 #endif 2448 #endif
2434 2449
2435 if (!tb[IFLA_MTU]) 2450 if (!tb[IFLA_MTU])
2436 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); 2451 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2437 2452
2438 /* update header length based on lower device */ 2453 /* update header length based on lower device */
2439 dev->hard_header_len = lowerdev->hard_header_len + 2454 dev->hard_header_len = lowerdev->hard_header_len +
2440 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); 2455 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2441 } 2456 }
2442 2457
2443 if (data[IFLA_VXLAN_TOS]) 2458 if (data[IFLA_VXLAN_TOS])
2444 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); 2459 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2445 2460
2446 if (data[IFLA_VXLAN_TTL]) 2461 if (data[IFLA_VXLAN_TTL])
2447 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); 2462 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2448 2463
2449 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) 2464 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2450 vxlan->flags |= VXLAN_F_LEARN; 2465 vxlan->flags |= VXLAN_F_LEARN;
2451 2466
2452 if (data[IFLA_VXLAN_AGEING]) 2467 if (data[IFLA_VXLAN_AGEING])
2453 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); 2468 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2454 else 2469 else
2455 vxlan->age_interval = FDB_AGE_DEFAULT; 2470 vxlan->age_interval = FDB_AGE_DEFAULT;
2456 2471
2457 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) 2472 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2458 vxlan->flags |= VXLAN_F_PROXY; 2473 vxlan->flags |= VXLAN_F_PROXY;
2459 2474
2460 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) 2475 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2461 vxlan->flags |= VXLAN_F_RSC; 2476 vxlan->flags |= VXLAN_F_RSC;
2462 2477
2463 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) 2478 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2464 vxlan->flags |= VXLAN_F_L2MISS; 2479 vxlan->flags |= VXLAN_F_L2MISS;
2465 2480
2466 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) 2481 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2467 vxlan->flags |= VXLAN_F_L3MISS; 2482 vxlan->flags |= VXLAN_F_L3MISS;
2468 2483
2469 if (data[IFLA_VXLAN_LIMIT]) 2484 if (data[IFLA_VXLAN_LIMIT])
2470 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); 2485 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2471 2486
2472 if (data[IFLA_VXLAN_PORT_RANGE]) { 2487 if (data[IFLA_VXLAN_PORT_RANGE]) {
2473 const struct ifla_vxlan_port_range *p 2488 const struct ifla_vxlan_port_range *p
2474 = nla_data(data[IFLA_VXLAN_PORT_RANGE]); 2489 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2475 vxlan->port_min = ntohs(p->low); 2490 vxlan->port_min = ntohs(p->low);
2476 vxlan->port_max = ntohs(p->high); 2491 vxlan->port_max = ntohs(p->high);
2477 } 2492 }
2478 2493
2479 if (data[IFLA_VXLAN_PORT]) 2494 if (data[IFLA_VXLAN_PORT])
2480 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); 2495 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2481 2496
2482 if (vxlan_find_vni(net, vni, vxlan->dst_port)) { 2497 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2483 pr_info("duplicate VNI %u\n", vni); 2498 pr_info("duplicate VNI %u\n", vni);
2484 return -EEXIST; 2499 return -EEXIST;
2485 } 2500 }
2486 2501
2487 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); 2502 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2488 2503
2489 /* create an fdb entry for a valid default destination */ 2504 /* create an fdb entry for a valid default destination */
2490 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { 2505 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2491 err = vxlan_fdb_create(vxlan, all_zeros_mac, 2506 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2492 &vxlan->default_dst.remote_ip, 2507 &vxlan->default_dst.remote_ip,
2493 NUD_REACHABLE|NUD_PERMANENT, 2508 NUD_REACHABLE|NUD_PERMANENT,
2494 NLM_F_EXCL|NLM_F_CREATE, 2509 NLM_F_EXCL|NLM_F_CREATE,
2495 vxlan->dst_port, 2510 vxlan->dst_port,
2496 vxlan->default_dst.remote_vni, 2511 vxlan->default_dst.remote_vni,
2497 vxlan->default_dst.remote_ifindex, 2512 vxlan->default_dst.remote_ifindex,
2498 NTF_SELF); 2513 NTF_SELF);
2499 if (err) 2514 if (err)
2500 return err; 2515 return err;
2501 } 2516 }
2502 2517
2503 err = register_netdevice(dev); 2518 err = register_netdevice(dev);
2504 if (err) { 2519 if (err) {
2505 vxlan_fdb_delete_default(vxlan); 2520 vxlan_fdb_delete_default(vxlan);
2506 return err; 2521 return err;
2507 } 2522 }
2508 2523
2509 list_add(&vxlan->next, &vn->vxlan_list); 2524 list_add(&vxlan->next, &vn->vxlan_list);
2510 2525
2511 return 0; 2526 return 0;
2512 } 2527 }
2513 2528
2514 static void vxlan_dellink(struct net_device *dev, struct list_head *head) 2529 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2515 { 2530 {
2516 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); 2531 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2517 struct vxlan_dev *vxlan = netdev_priv(dev); 2532 struct vxlan_dev *vxlan = netdev_priv(dev);
2518 2533
2519 spin_lock(&vn->sock_lock); 2534 spin_lock(&vn->sock_lock);
2520 if (!hlist_unhashed(&vxlan->hlist)) 2535 if (!hlist_unhashed(&vxlan->hlist))
2521 hlist_del_rcu(&vxlan->hlist); 2536 hlist_del_rcu(&vxlan->hlist);
2522 spin_unlock(&vn->sock_lock); 2537 spin_unlock(&vn->sock_lock);
2523 2538
2524 list_del(&vxlan->next); 2539 list_del(&vxlan->next);
2525 unregister_netdevice_queue(dev, head); 2540 unregister_netdevice_queue(dev, head);
2526 } 2541 }
2527 2542
2528 static size_t vxlan_get_size(const struct net_device *dev) 2543 static size_t vxlan_get_size(const struct net_device *dev)
2529 { 2544 {
2530 2545
2531 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ 2546 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2532 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ 2547 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2533 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ 2548 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2534 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ 2549 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2535 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ 2550 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2536 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ 2551 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2537 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ 2552 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2538 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ 2553 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2539 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ 2554 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2540 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ 2555 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2541 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ 2556 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2542 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ 2557 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2543 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ 2558 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2544 nla_total_size(sizeof(struct ifla_vxlan_port_range)) + 2559 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2545 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ 2560 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
2546 0; 2561 0;
2547 } 2562 }
2548 2563
2549 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) 2564 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2550 { 2565 {
2551 const struct vxlan_dev *vxlan = netdev_priv(dev); 2566 const struct vxlan_dev *vxlan = netdev_priv(dev);
2552 const struct vxlan_rdst *dst = &vxlan->default_dst; 2567 const struct vxlan_rdst *dst = &vxlan->default_dst;
2553 struct ifla_vxlan_port_range ports = { 2568 struct ifla_vxlan_port_range ports = {
2554 .low = htons(vxlan->port_min), 2569 .low = htons(vxlan->port_min),
2555 .high = htons(vxlan->port_max), 2570 .high = htons(vxlan->port_max),
2556 }; 2571 };
2557 2572
2558 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) 2573 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2559 goto nla_put_failure; 2574 goto nla_put_failure;
2560 2575
2561 if (!vxlan_addr_any(&dst->remote_ip)) { 2576 if (!vxlan_addr_any(&dst->remote_ip)) {
2562 if (dst->remote_ip.sa.sa_family == AF_INET) { 2577 if (dst->remote_ip.sa.sa_family == AF_INET) {
2563 if (nla_put_be32(skb, IFLA_VXLAN_GROUP, 2578 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2564 dst->remote_ip.sin.sin_addr.s_addr)) 2579 dst->remote_ip.sin.sin_addr.s_addr))
2565 goto nla_put_failure; 2580 goto nla_put_failure;
2566 #if IS_ENABLED(CONFIG_IPV6) 2581 #if IS_ENABLED(CONFIG_IPV6)
2567 } else { 2582 } else {
2568 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr), 2583 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2569 &dst->remote_ip.sin6.sin6_addr)) 2584 &dst->remote_ip.sin6.sin6_addr))
2570 goto nla_put_failure; 2585 goto nla_put_failure;
2571 #endif 2586 #endif
2572 } 2587 }
2573 } 2588 }
2574 2589
2575 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) 2590 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2576 goto nla_put_failure; 2591 goto nla_put_failure;
2577 2592
2578 if (!vxlan_addr_any(&vxlan->saddr)) { 2593 if (!vxlan_addr_any(&vxlan->saddr)) {
2579 if (vxlan->saddr.sa.sa_family == AF_INET) { 2594 if (vxlan->saddr.sa.sa_family == AF_INET) {
2580 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL, 2595 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2581 vxlan->saddr.sin.sin_addr.s_addr)) 2596 vxlan->saddr.sin.sin_addr.s_addr))
2582 goto nla_put_failure; 2597 goto nla_put_failure;
2583 #if IS_ENABLED(CONFIG_IPV6) 2598 #if IS_ENABLED(CONFIG_IPV6)
2584 } else { 2599 } else {
2585 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr), 2600 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2586 &vxlan->saddr.sin6.sin6_addr)) 2601 &vxlan->saddr.sin6.sin6_addr))
2587 goto nla_put_failure; 2602 goto nla_put_failure;
2588 #endif 2603 #endif
2589 } 2604 }
2590 } 2605 }
2591 2606
2592 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || 2607 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2593 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || 2608 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2594 nla_put_u8(skb, IFLA_VXLAN_LEARNING, 2609 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2595 !!(vxlan->flags & VXLAN_F_LEARN)) || 2610 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2596 nla_put_u8(skb, IFLA_VXLAN_PROXY, 2611 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2597 !!(vxlan->flags & VXLAN_F_PROXY)) || 2612 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2598 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || 2613 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2599 nla_put_u8(skb, IFLA_VXLAN_L2MISS, 2614 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2600 !!(vxlan->flags & VXLAN_F_L2MISS)) || 2615 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2601 nla_put_u8(skb, IFLA_VXLAN_L3MISS, 2616 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2602 !!(vxlan->flags & VXLAN_F_L3MISS)) || 2617 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2603 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || 2618 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2604 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || 2619 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2605 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) 2620 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
2606 goto nla_put_failure; 2621 goto nla_put_failure;
2607 2622
2608 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) 2623 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2609 goto nla_put_failure; 2624 goto nla_put_failure;
2610 2625
2611 return 0; 2626 return 0;
2612 2627
2613 nla_put_failure: 2628 nla_put_failure:
2614 return -EMSGSIZE; 2629 return -EMSGSIZE;
2615 } 2630 }
2616 2631
2617 static struct rtnl_link_ops vxlan_link_ops __read_mostly = { 2632 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2618 .kind = "vxlan", 2633 .kind = "vxlan",
2619 .maxtype = IFLA_VXLAN_MAX, 2634 .maxtype = IFLA_VXLAN_MAX,
2620 .policy = vxlan_policy, 2635 .policy = vxlan_policy,
2621 .priv_size = sizeof(struct vxlan_dev), 2636 .priv_size = sizeof(struct vxlan_dev),
2622 .setup = vxlan_setup, 2637 .setup = vxlan_setup,
2623 .validate = vxlan_validate, 2638 .validate = vxlan_validate,
2624 .newlink = vxlan_newlink, 2639 .newlink = vxlan_newlink,
2625 .dellink = vxlan_dellink, 2640 .dellink = vxlan_dellink,
2626 .get_size = vxlan_get_size, 2641 .get_size = vxlan_get_size,
2627 .fill_info = vxlan_fill_info, 2642 .fill_info = vxlan_fill_info,
2628 }; 2643 };
2629 2644
2630 static __net_init int vxlan_init_net(struct net *net) 2645 static __net_init int vxlan_init_net(struct net *net)
2631 { 2646 {
2632 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2647 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2633 unsigned int h; 2648 unsigned int h;
2634 2649
2635 INIT_LIST_HEAD(&vn->vxlan_list); 2650 INIT_LIST_HEAD(&vn->vxlan_list);
2636 spin_lock_init(&vn->sock_lock); 2651 spin_lock_init(&vn->sock_lock);
2637 2652
2638 for (h = 0; h < PORT_HASH_SIZE; ++h) 2653 for (h = 0; h < PORT_HASH_SIZE; ++h)
2639 INIT_HLIST_HEAD(&vn->sock_list[h]); 2654 INIT_HLIST_HEAD(&vn->sock_list[h]);
2640 2655
2641 return 0; 2656 return 0;
2642 } 2657 }
2643 2658
2644 static __net_exit void vxlan_exit_net(struct net *net) 2659 static __net_exit void vxlan_exit_net(struct net *net)
2645 { 2660 {
2646 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 2661 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2647 struct vxlan_dev *vxlan; 2662 struct vxlan_dev *vxlan;
2648 LIST_HEAD(list); 2663 LIST_HEAD(list);
2649 2664
2650 rtnl_lock(); 2665 rtnl_lock();
2651 list_for_each_entry(vxlan, &vn->vxlan_list, next) 2666 list_for_each_entry(vxlan, &vn->vxlan_list, next)
2652 unregister_netdevice_queue(vxlan->dev, &list); 2667 unregister_netdevice_queue(vxlan->dev, &list);
2653 unregister_netdevice_many(&list); 2668 unregister_netdevice_many(&list);
2654 rtnl_unlock(); 2669 rtnl_unlock();
2655 } 2670 }
2656 2671
2657 static struct pernet_operations vxlan_net_ops = { 2672 static struct pernet_operations vxlan_net_ops = {
2658 .init = vxlan_init_net, 2673 .init = vxlan_init_net,
2659 .exit = vxlan_exit_net, 2674 .exit = vxlan_exit_net,
2660 .id = &vxlan_net_id, 2675 .id = &vxlan_net_id,
2661 .size = sizeof(struct vxlan_net), 2676 .size = sizeof(struct vxlan_net),
2662 }; 2677 };
2663 2678
2664 static int __init vxlan_init_module(void) 2679 static int __init vxlan_init_module(void)
2665 { 2680 {
2666 int rc; 2681 int rc;
2667 2682
2668 vxlan_wq = alloc_workqueue("vxlan", 0, 0); 2683 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2669 if (!vxlan_wq) 2684 if (!vxlan_wq)
2670 return -ENOMEM; 2685 return -ENOMEM;
2671 2686
2672 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); 2687 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2673 2688
2674 rc = register_pernet_device(&vxlan_net_ops); 2689 rc = register_pernet_device(&vxlan_net_ops);
2675 if (rc) 2690 if (rc)
2676 goto out1; 2691 goto out1;
2677 2692
2678 rc = rtnl_link_register(&vxlan_link_ops); 2693 rc = rtnl_link_register(&vxlan_link_ops);
2679 if (rc) 2694 if (rc)
2680 goto out2; 2695 goto out2;
2681 2696
2682 return 0; 2697 return 0;
2683 2698
2684 out2: 2699 out2:
2685 unregister_pernet_device(&vxlan_net_ops); 2700 unregister_pernet_device(&vxlan_net_ops);
2686 out1: 2701 out1:
2687 destroy_workqueue(vxlan_wq); 2702 destroy_workqueue(vxlan_wq);
2688 return rc; 2703 return rc;
2689 } 2704 }
2690 late_initcall(vxlan_init_module); 2705 late_initcall(vxlan_init_module);
2691 2706
2692 static void __exit vxlan_cleanup_module(void) 2707 static void __exit vxlan_cleanup_module(void)
2693 { 2708 {
2694 rtnl_link_unregister(&vxlan_link_ops); 2709 rtnl_link_unregister(&vxlan_link_ops);
2695 destroy_workqueue(vxlan_wq); 2710 destroy_workqueue(vxlan_wq);
2696 unregister_pernet_device(&vxlan_net_ops); 2711 unregister_pernet_device(&vxlan_net_ops);
2697 rcu_barrier(); 2712 rcu_barrier();
2698 } 2713 }
2699 module_exit(vxlan_cleanup_module); 2714 module_exit(vxlan_cleanup_module);
2700 2715
2701 MODULE_LICENSE("GPL"); 2716 MODULE_LICENSE("GPL");
2702 MODULE_VERSION(VXLAN_VERSION); 2717 MODULE_VERSION(VXLAN_VERSION);
2703 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); 2718 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2704 MODULE_ALIAS_RTNL_LINK("vxlan"); 2719 MODULE_ALIAS_RTNL_LINK("vxlan");
2705 2720