]>
Commit | Line | Data |
---|---|---|
1b7ee51f | 1 | /* |
e23775f2 | 2 | * VXLAN: Virtual eXtensible Local Area Network |
1b7ee51f | 3 | * |
e23775f2 | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
1b7ee51f | 5 | * |
e23775f2 PS |
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 | |
8 | * published by the Free Software Foundation. | |
1b7ee51f PS |
9 | */ |
10 | ||
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/skbuff.h> | |
19 | #include <linux/rculist.h> | |
20 | #include <linux/netdevice.h> | |
f2252c61 | 21 | #include <linux/netdev_features.h> |
1b7ee51f PS |
22 | #include <linux/in.h> |
23 | #include <linux/ip.h> | |
24 | #include <linux/udp.h> | |
25 | #include <linux/igmp.h> | |
26 | #include <linux/etherdevice.h> | |
27 | #include <linux/if_ether.h> | |
28 | #include <linux/if_vlan.h> | |
29 | #include <linux/hash.h> | |
30 | #include <linux/ethtool.h> | |
31 | #include <net/arp.h> | |
f2252c61 | 32 | #include <net/dst_metadata.h> |
1b7ee51f PS |
33 | #include <net/ndisc.h> |
34 | #include <net/ip.h> | |
35 | #include <net/ip_tunnels.h> | |
36 | #include <net/icmp.h> | |
37 | #include <net/udp.h> | |
131971ce | 38 | #include <net/udp_tunnel.h> |
1b7ee51f PS |
39 | #include <net/rtnetlink.h> |
40 | #include <net/route.h> | |
41 | #include <net/dsfield.h> | |
42 | #include <net/inet_ecn.h> | |
43 | #include <net/net_namespace.h> | |
44 | #include <net/netns/generic.h> | |
e23775f2 | 45 | #include <net/protocol.h> |
f2252c61 | 46 | |
e23775f2 PS |
47 | #if IS_ENABLED(CONFIG_IPV6) |
48 | #include <net/ipv6.h> | |
49 | #include <net/addrconf.h> | |
50 | #include <net/ip6_tunnel.h> | |
51 | #include <net/ip6_checksum.h> | |
f2252c61 | 52 | #include <net/ip6_route.h> |
e23775f2 | 53 | #endif |
1b7ee51f | 54 | |
b80837ac | 55 | #include <net/tun_proto.h> |
0643a78b | 56 | #include <net/vxlan.h> |
1b7ee51f | 57 | #include "gso.h" |
e23775f2 | 58 | #include "vport-netdev.h" |
f2252c61 | 59 | #include "compat.h" |
e23775f2 | 60 | |
f2252c61 | 61 | #ifndef USE_UPSTREAM_TUNNEL |
e23775f2 PS |
62 | #define VXLAN_VERSION "0.1" |
63 | ||
64 | #define PORT_HASH_BITS 8 | |
65 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) | |
66 | #define FDB_AGE_DEFAULT 300 /* 5 min */ | |
67 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ | |
68 | ||
e23775f2 PS |
69 | /* UDP port for VXLAN traffic. |
70 | * The IANA assigned port is 4789, but the Linux default is 8472 | |
71 | * for compatibility with early adopters. | |
72 | */ | |
73 | static unsigned short vxlan_port __read_mostly = 8472; | |
74 | module_param_named(udp_port, vxlan_port, ushort, 0444); | |
75 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); | |
76 | ||
77 | static int vxlan_net_id; | |
78 | static struct rtnl_link_ops vxlan_link_ops; | |
1b7ee51f | 79 | |
f2252c61 | 80 | static const u8 all_zeros_mac[ETH_ALEN + 2]; |
b22a8d87 | 81 | |
f2252c61 | 82 | static int vxlan_sock_add(struct vxlan_dev *vxlan); |
e23775f2 PS |
83 | |
84 | /* per-network namespace private data for this module */ | |
85 | struct vxlan_net { | |
86 | struct list_head vxlan_list; | |
87 | struct hlist_head sock_list[PORT_HASH_SIZE]; | |
88 | spinlock_t sock_lock; | |
1b7ee51f PS |
89 | }; |
90 | ||
e23775f2 PS |
91 | /* Forwarding table entry */ |
92 | struct vxlan_fdb { | |
93 | struct hlist_node hlist; /* linked list of entries */ | |
94 | struct rcu_head rcu; | |
95 | unsigned long updated; /* jiffies */ | |
96 | unsigned long used; | |
97 | struct list_head remotes; | |
98 | u8 eth_addr[ETH_ALEN]; | |
99 | u16 state; /* see ndm_state */ | |
100 | u8 flags; /* see ndm_flags */ | |
101 | }; | |
102 | ||
103 | /* salt for hash table */ | |
104 | static u32 vxlan_salt __read_mostly; | |
e23775f2 PS |
105 | |
106 | static inline bool vxlan_collect_metadata(struct vxlan_sock *vs) | |
107 | { | |
108 | return vs->flags & VXLAN_F_COLLECT_METADATA || | |
109 | ip_tunnel_collect_metadata(); | |
110 | } | |
111 | ||
112 | #if IS_ENABLED(CONFIG_IPV6) | |
113 | static inline | |
114 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
115 | { | |
116 | if (a->sa.sa_family != b->sa.sa_family) | |
117 | return false; | |
118 | if (a->sa.sa_family == AF_INET6) | |
119 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); | |
120 | else | |
121 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
122 | } | |
123 | ||
124 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
125 | { | |
126 | if (ipa->sa.sa_family == AF_INET6) | |
127 | return ipv6_addr_any(&ipa->sin6.sin6_addr); | |
128 | else | |
129 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); | |
130 | } | |
131 | ||
132 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
133 | { | |
134 | if (ipa->sa.sa_family == AF_INET6) | |
135 | return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr); | |
136 | else | |
137 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); | |
138 | } | |
139 | ||
140 | #else /* !CONFIG_IPV6 */ | |
141 | ||
142 | static inline | |
143 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
144 | { | |
145 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
146 | } | |
147 | ||
148 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
149 | { | |
150 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); | |
151 | } | |
152 | ||
153 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
154 | { | |
155 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); | |
156 | } | |
e23775f2 PS |
157 | #endif |
158 | ||
159 | /* Virtual Network hash table head */ | |
f2252c61 | 160 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni) |
e23775f2 | 161 | { |
f2252c61 | 162 | return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)]; |
e23775f2 PS |
163 | } |
164 | ||
165 | /* Socket hash table head */ | |
166 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) | |
167 | { | |
168 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
169 | ||
170 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; | |
171 | } | |
172 | ||
e23775f2 PS |
173 | /* Find VXLAN socket based on network namespace, address family and UDP port |
174 | * and enabled unshareable flags. | |
175 | */ | |
176 | static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family, | |
177 | __be16 port, u32 flags) | |
1b7ee51f | 178 | { |
1b7ee51f | 179 | struct vxlan_sock *vs; |
1b7ee51f | 180 | |
e23775f2 | 181 | flags &= VXLAN_F_RCV_FLAGS; |
1b7ee51f | 182 | |
e23775f2 | 183 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
8063e095 | 184 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
e23775f2 PS |
185 | vxlan_get_sk_family(vs) == family && |
186 | vs->flags == flags) | |
187 | return vs; | |
188 | } | |
189 | return NULL; | |
190 | } | |
ababf424 | 191 | |
f2252c61 | 192 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni) |
e23775f2 PS |
193 | { |
194 | struct vxlan_dev *vxlan; | |
195 | ||
f2252c61 PS |
196 | /* For flow based devices, map all packets to VNI 0 */ |
197 | if (vs->flags & VXLAN_F_COLLECT_METADATA) | |
198 | vni = 0; | |
199 | ||
200 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) { | |
201 | if (vxlan->default_dst.remote_vni == vni) | |
e23775f2 | 202 | return vxlan; |
1b7ee51f PS |
203 | } |
204 | ||
e23775f2 PS |
205 | return NULL; |
206 | } | |
1b7ee51f | 207 | |
e23775f2 | 208 | /* Look up VNI in a per net namespace table */ |
f2252c61 | 209 | static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni, |
e23775f2 PS |
210 | sa_family_t family, __be16 port, |
211 | u32 flags) | |
212 | { | |
213 | struct vxlan_sock *vs; | |
214 | ||
215 | vs = vxlan_find_sock(net, family, port, flags); | |
1b7ee51f | 216 | if (!vs) |
e23775f2 | 217 | return NULL; |
1b7ee51f | 218 | |
f2252c61 | 219 | return vxlan_vs_find_vni(vs, vni); |
e23775f2 | 220 | } |
3174a818 | 221 | |
f2252c61 PS |
222 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
223 | const u8 *mac, union vxlan_addr *ip, | |
224 | __u16 state, __u16 flags, | |
0643a78b | 225 | __be16 port, __be32 vni, __u32 ifindex, |
f2252c61 | 226 | __u8 ndm_flags) |
e23775f2 PS |
227 | { |
228 | return -EINVAL; | |
229 | } | |
3174a818 | 230 | |
f2252c61 PS |
231 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
232 | { | |
233 | ||
234 | } | |
235 | ||
e23775f2 PS |
236 | static inline size_t vxlan_nlmsg_size(void) |
237 | { | |
238 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
239 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
240 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ | |
241 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ | |
242 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ | |
243 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ | |
244 | + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */ | |
245 | + nla_total_size(sizeof(struct nda_cacheinfo)); | |
246 | } | |
3174a818 | 247 | |
e23775f2 PS |
248 | #ifdef HAVE_UDP_OFFLOAD |
249 | #ifdef HAVE_NETIF_F_GSO_TUNNEL_REMCSUM | |
f2252c61 | 250 | |
e23775f2 PS |
251 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, |
252 | unsigned int off, | |
253 | struct vxlanhdr *vh, size_t hdrlen, | |
f2252c61 PS |
254 | __be32 vni_field, |
255 | struct gro_remcsum *grc, | |
e23775f2 PS |
256 | bool nopartial) |
257 | { | |
258 | size_t start, offset; | |
259 | ||
260 | if (skb->remcsum_offload) | |
261 | return vh; | |
262 | ||
263 | if (!NAPI_GRO_CB(skb)->csum_valid) | |
264 | return NULL; | |
265 | ||
f2252c61 PS |
266 | start = vxlan_rco_start(vni_field); |
267 | offset = start + vxlan_rco_offset(vni_field); | |
e23775f2 PS |
268 | |
269 | vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen, | |
270 | start, offset, grc, nopartial); | |
271 | ||
272 | skb->remcsum_offload = 1; | |
273 | ||
274 | return vh; | |
275 | } | |
276 | #else | |
277 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, | |
f2252c61 PS |
278 | unsigned int off, |
279 | struct vxlanhdr *vh, size_t hdrlen, | |
280 | u32 data, struct gro_remcsum *grc, | |
281 | bool nopartial) | |
e23775f2 PS |
282 | { |
283 | return NULL; | |
3174a818 | 284 | } |
e23775f2 | 285 | #endif |
3174a818 | 286 | |
e23775f2 PS |
287 | #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF |
288 | static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, | |
f2252c61 | 289 | struct sk_buff *skb) |
e23775f2 PS |
290 | #else |
291 | static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, | |
f2252c61 PS |
292 | struct sk_buff *skb, |
293 | struct udp_offload *uoff) | |
e23775f2 | 294 | #endif |
1b7ee51f | 295 | { |
e23775f2 PS |
296 | #ifdef HAVE_UDP_OFFLOAD_ARG_UOFF |
297 | struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock, | |
f2252c61 | 298 | udp_offloads); |
e23775f2 PS |
299 | #else |
300 | struct vxlan_sock *vs = NULL; | |
301 | #endif | |
302 | struct sk_buff *p, **pp = NULL; | |
303 | struct vxlanhdr *vh, *vh2; | |
304 | unsigned int hlen, off_vx; | |
305 | int flush = 1; | |
f2252c61 | 306 | __be32 flags; |
e23775f2 | 307 | struct gro_remcsum grc; |
1b7ee51f | 308 | |
e23775f2 | 309 | skb_gro_remcsum_init(&grc); |
1b7ee51f | 310 | |
e23775f2 PS |
311 | off_vx = skb_gro_offset(skb); |
312 | hlen = off_vx + sizeof(*vh); | |
313 | vh = skb_gro_header_fast(skb, off_vx); | |
314 | if (skb_gro_header_hard(skb, hlen)) { | |
315 | vh = skb_gro_header_slow(skb, hlen, off_vx); | |
316 | if (unlikely(!vh)) | |
317 | goto out; | |
93258bd7 | 318 | } |
1b7ee51f | 319 | |
e23775f2 | 320 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); |
1b7ee51f | 321 | |
f2252c61 | 322 | flags = vh->vx_flags; |
3174a818 | 323 | |
9989631e | 324 | if ((flags & VXLAN_HF_RCO) && vs && (vs->flags & VXLAN_F_REMCSUM_RX)) { |
e23775f2 | 325 | vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), |
f2252c61 | 326 | vh->vx_vni, &grc, |
e23775f2 PS |
327 | !!(vs->flags & |
328 | VXLAN_F_REMCSUM_NOPARTIAL)); | |
1b7ee51f | 329 | |
e23775f2 PS |
330 | if (!vh) |
331 | goto out; | |
332 | } | |
1b7ee51f | 333 | |
e23775f2 | 334 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ |
e4bafe59 | 335 | |
e23775f2 PS |
336 | for (p = *head; p; p = p->next) { |
337 | if (!NAPI_GRO_CB(p)->same_flow) | |
338 | continue; | |
a109c9fb | 339 | |
e23775f2 PS |
340 | vh2 = (struct vxlanhdr *)(p->data + off_vx); |
341 | if (vh->vx_flags != vh2->vx_flags || | |
342 | vh->vx_vni != vh2->vx_vni) { | |
343 | NAPI_GRO_CB(p)->same_flow = 0; | |
344 | continue; | |
345 | } | |
346 | } | |
347 | ||
348 | pp = eth_gro_receive(head, skb); | |
f2252c61 | 349 | flush = 0; |
e23775f2 PS |
350 | |
351 | out: | |
352 | skb_gro_remcsum_cleanup(skb, &grc); | |
353 | NAPI_GRO_CB(skb)->flush |= flush; | |
354 | ||
355 | return pp; | |
a109c9fb PS |
356 | } |
357 | ||
e23775f2 PS |
358 | #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF |
359 | static int vxlan_gro_complete(struct sk_buff *skb, int nhoff) | |
360 | #else | |
361 | static int vxlan_gro_complete(struct sk_buff *skb, int nhoff, | |
f2252c61 | 362 | struct udp_offload *uoff) |
e23775f2 | 363 | #endif |
a109c9fb | 364 | { |
cc3af88e PS |
365 | /* Sets 'skb->inner_mac_header' since we are always called with |
366 | * 'skb->encapsulation' set. | |
367 | */ | |
e23775f2 | 368 | udp_tunnel_gro_complete(skb, nhoff); |
a109c9fb | 369 | |
e23775f2 | 370 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
a109c9fb | 371 | } |
f2252c61 | 372 | #endif |
a109c9fb | 373 | |
e23775f2 PS |
374 | /* Notify netdevs that UDP port started listening */ |
375 | static void vxlan_notify_add_rx_port(struct vxlan_sock *vs) | |
131971ce | 376 | { |
e23775f2 PS |
377 | struct net_device *dev; |
378 | struct sock *sk = vs->sock->sk; | |
379 | struct net *net = sock_net(sk); | |
380 | sa_family_t sa_family = vxlan_get_sk_family(vs); | |
131971ce | 381 | |
f2252c61 PS |
382 | |
383 | if (sa_family == AF_INET) { | |
384 | int err; | |
385 | ||
edf6cd31 | 386 | err = udp_add_offload(net, &vs->udp_offloads); |
f2252c61 PS |
387 | if (err) |
388 | pr_warn("vxlan: udp_add_offload failed with status %d\n", err); | |
389 | } | |
390 | ||
edf6cd31 PS |
391 | rcu_read_lock(); |
392 | for_each_netdev_rcu(net, dev) { | |
393 | #ifdef HAVE_NDO_ADD_VXLAN_PORT | |
394 | __be16 port = inet_sk(sk)->inet_sport; | |
395 | ||
396 | if (dev->netdev_ops->ndo_add_vxlan_port) | |
397 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, | |
398 | port); | |
e2e11c89 | 399 | #elif defined(HAVE_NDO_UDP_TUNNEL_ADD) |
400 | struct udp_tunnel_info ti; | |
401 | if (vs->flags & VXLAN_F_GPE) | |
402 | ti.type = UDP_TUNNEL_TYPE_VXLAN_GPE; | |
403 | else | |
404 | ti.type = UDP_TUNNEL_TYPE_VXLAN; | |
405 | ti.sa_family = sa_family; | |
406 | ti.port = inet_sk(sk)->inet_sport; | |
407 | ||
408 | if (dev->netdev_ops->ndo_udp_tunnel_add) | |
409 | dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti); | |
f2252c61 | 410 | #endif |
edf6cd31 PS |
411 | } |
412 | rcu_read_unlock(); | |
e23775f2 | 413 | } |
131971ce | 414 | |
e23775f2 PS |
415 | /* Notify netdevs that UDP port is no more listening */ |
416 | static void vxlan_notify_del_rx_port(struct vxlan_sock *vs) | |
417 | { | |
418 | struct net_device *dev; | |
419 | struct sock *sk = vs->sock->sk; | |
420 | struct net *net = sock_net(sk); | |
421 | sa_family_t sa_family = vxlan_get_sk_family(vs); | |
131971ce | 422 | |
e23775f2 PS |
423 | rcu_read_lock(); |
424 | for_each_netdev_rcu(net, dev) { | |
edf6cd31 PS |
425 | #ifdef HAVE_NDO_ADD_VXLAN_PORT |
426 | __be16 port = inet_sk(sk)->inet_sport; | |
427 | ||
e23775f2 PS |
428 | if (dev->netdev_ops->ndo_del_vxlan_port) |
429 | dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family, | |
edf6cd31 | 430 | port); |
e2e11c89 | 431 | #elif defined(HAVE_NDO_UDP_TUNNEL_ADD) |
432 | struct udp_tunnel_info ti; | |
433 | if (vs->flags & VXLAN_F_GPE) | |
434 | ti.type = UDP_TUNNEL_TYPE_VXLAN_GPE; | |
435 | else | |
436 | ti.type = UDP_TUNNEL_TYPE_VXLAN; | |
437 | ti.port = inet_sk(sk)->inet_sport; | |
438 | ti.sa_family = sa_family; | |
439 | ||
440 | if (dev->netdev_ops->ndo_udp_tunnel_del) | |
441 | dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti); | |
edf6cd31 | 442 | #endif |
e23775f2 PS |
443 | } |
444 | rcu_read_unlock(); | |
131971ce | 445 | |
f2252c61 | 446 | if (sa_family == AF_INET) { |
e23775f2 | 447 | udp_del_offload(&vs->udp_offloads); |
edf6cd31 | 448 | } |
1b7ee51f PS |
449 | } |
450 | ||
e23775f2 PS |
451 | /* See if multicast group is already in use by other ID */ |
452 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) | |
1b7ee51f | 453 | { |
e23775f2 | 454 | struct vxlan_dev *vxlan; |
76e671ea PS |
455 | struct vxlan_sock *sock4; |
456 | struct vxlan_sock *sock6 = NULL; | |
f2252c61 | 457 | unsigned short family = dev->default_dst.remote_ip.sa.sa_family; |
e23775f2 | 458 | |
76e671ea PS |
459 | sock4 = rtnl_dereference(dev->vn4_sock); |
460 | ||
e23775f2 PS |
461 | /* The vxlan_sock is only used by dev, leaving group has |
462 | * no effect on other vxlan devices. | |
463 | */ | |
76e671ea | 464 | if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1) |
f2252c61 PS |
465 | return false; |
466 | #if IS_ENABLED(CONFIG_IPV6) | |
76e671ea PS |
467 | sock6 = rtnl_dereference(dev->vn6_sock); |
468 | if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1) | |
e23775f2 | 469 | return false; |
f2252c61 | 470 | #endif |
1b7ee51f | 471 | |
e23775f2 PS |
472 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
473 | if (!netif_running(vxlan->dev) || vxlan == dev) | |
474 | continue; | |
475 | ||
76e671ea PS |
476 | if (family == AF_INET && |
477 | rtnl_dereference(vxlan->vn4_sock) != sock4) | |
e23775f2 | 478 | continue; |
f2252c61 | 479 | #if IS_ENABLED(CONFIG_IPV6) |
76e671ea PS |
480 | if (family == AF_INET6 && |
481 | rtnl_dereference(vxlan->vn6_sock) != sock6) | |
f2252c61 PS |
482 | continue; |
483 | #endif | |
e23775f2 PS |
484 | |
485 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, | |
486 | &dev->default_dst.remote_ip)) | |
487 | continue; | |
488 | ||
489 | if (vxlan->default_dst.remote_ifindex != | |
490 | dev->default_dst.remote_ifindex) | |
491 | continue; | |
492 | ||
493 | return true; | |
494 | } | |
495 | ||
496 | return false; | |
1b7ee51f | 497 | } |
29c71cfa | 498 | |
f2252c61 | 499 | static bool __vxlan_sock_release_prep(struct vxlan_sock *vs) |
e23775f2 | 500 | { |
f2252c61 | 501 | struct vxlan_net *vn; |
e23775f2 | 502 | |
f2252c61 PS |
503 | if (!vs) |
504 | return false; | |
e23775f2 | 505 | if (!atomic_dec_and_test(&vs->refcnt)) |
f2252c61 | 506 | return false; |
e23775f2 | 507 | |
f2252c61 | 508 | vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
e23775f2 PS |
509 | spin_lock(&vn->sock_lock); |
510 | hlist_del_rcu(&vs->hlist); | |
e23775f2 | 511 | vxlan_notify_del_rx_port(vs); |
e23775f2 PS |
512 | spin_unlock(&vn->sock_lock); |
513 | ||
f2252c61 PS |
514 | return true; |
515 | } | |
516 | ||
517 | static void vxlan_sock_release(struct vxlan_dev *vxlan) | |
518 | { | |
76e671ea | 519 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
f2252c61 | 520 | #if IS_ENABLED(CONFIG_IPV6) |
76e671ea PS |
521 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
522 | ||
523 | rcu_assign_pointer(vxlan->vn6_sock, NULL); | |
f2252c61 PS |
524 | #endif |
525 | ||
76e671ea | 526 | rcu_assign_pointer(vxlan->vn4_sock, NULL); |
f2252c61 PS |
527 | synchronize_net(); |
528 | ||
76e671ea PS |
529 | if (__vxlan_sock_release_prep(sock4)) { |
530 | udp_tunnel_sock_release(sock4->sock); | |
531 | kfree(sock4); | |
f2252c61 PS |
532 | } |
533 | ||
534 | #if IS_ENABLED(CONFIG_IPV6) | |
76e671ea PS |
535 | if (__vxlan_sock_release_prep(sock6)) { |
536 | udp_tunnel_sock_release(sock6->sock); | |
537 | kfree(sock6); | |
f2252c61 PS |
538 | } |
539 | #endif | |
e23775f2 PS |
540 | } |
541 | ||
542 | /* Update multicast group membership when first VNI on | |
543 | * multicast address is brought up | |
544 | */ | |
545 | static int vxlan_igmp_join(struct vxlan_dev *vxlan) | |
546 | { | |
547 | return -EINVAL; | |
548 | } | |
549 | ||
550 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ | |
551 | static int vxlan_igmp_leave(struct vxlan_dev *vxlan) | |
552 | { | |
553 | return -EINVAL; | |
554 | } | |
555 | ||
f2252c61 PS |
556 | static bool vxlan_remcsum(struct vxlanhdr *unparsed, |
557 | struct sk_buff *skb, u32 vxflags) | |
e23775f2 | 558 | { |
f2252c61 PS |
559 | #ifndef USE_UPSTREAM_TUNNEL |
560 | return false; | |
561 | #else | |
562 | size_t start, offset; | |
e23775f2 | 563 | |
f2252c61 PS |
564 | if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload) |
565 | goto out; | |
e23775f2 | 566 | |
f2252c61 PS |
567 | start = vxlan_rco_start(unparsed->vx_vni); |
568 | offset = start + vxlan_rco_offset(unparsed->vx_vni); | |
e23775f2 | 569 | |
f2252c61 PS |
570 | if (!pskb_may_pull(skb, offset + sizeof(u16))) |
571 | return false; | |
e23775f2 | 572 | |
f2252c61 PS |
573 | skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset, |
574 | !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL)); | |
575 | out: | |
576 | unparsed->vx_flags &= ~VXLAN_HF_RCO; | |
577 | unparsed->vx_vni &= VXLAN_VNI_MASK; | |
578 | return true; | |
e23775f2 | 579 | #endif |
f2252c61 | 580 | } |
e23775f2 | 581 | |
f2252c61 PS |
582 | static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed, |
583 | struct sk_buff *skb, u32 vxflags, | |
584 | struct vxlan_metadata *md) | |
e23775f2 | 585 | { |
f2252c61 PS |
586 | struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed; |
587 | struct metadata_dst *tun_dst; | |
e23775f2 | 588 | |
f2252c61 PS |
589 | if (!(unparsed->vx_flags & VXLAN_HF_GBP)) |
590 | goto out; | |
e23775f2 | 591 | |
f2252c61 | 592 | md->gbp = ntohs(gbp->policy_id); |
e23775f2 | 593 | |
f2252c61 | 594 | tun_dst = (struct metadata_dst *)skb_dst(skb); |
e23775f2 | 595 | if (tun_dst) { |
f2252c61 PS |
596 | tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT; |
597 | tun_dst->u.tun_info.options_len = sizeof(*md); | |
e23775f2 | 598 | } |
f2252c61 PS |
599 | if (gbp->dont_learn) |
600 | md->gbp |= VXLAN_GBP_DONT_LEARN; | |
e23775f2 | 601 | |
f2252c61 PS |
602 | if (gbp->policy_applied) |
603 | md->gbp |= VXLAN_GBP_POLICY_APPLIED; | |
e23775f2 | 604 | |
e23775f2 | 605 | /* In flow-based mode, GBP is carried in dst_metadata */ |
f2252c61 | 606 | if (!(vxflags & VXLAN_F_COLLECT_METADATA)) |
e23775f2 | 607 | skb->mark = md->gbp; |
f2252c61 PS |
608 | out: |
609 | unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS; | |
610 | } | |
e23775f2 | 611 | |
f2252c61 PS |
612 | static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed, |
613 | __be16 *protocol, | |
614 | struct sk_buff *skb, u32 vxflags) | |
615 | { | |
616 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed; | |
e23775f2 | 617 | |
f2252c61 PS |
618 | /* Need to have Next Protocol set for interfaces in GPE mode. */ |
619 | if (!gpe->np_applied) | |
620 | return false; | |
621 | /* "The initial version is 0. If a receiver does not support the | |
622 | * version indicated it MUST drop the packet. | |
623 | */ | |
624 | if (gpe->version != 0) | |
625 | return false; | |
626 | /* "When the O bit is set to 1, the packet is an OAM packet and OAM | |
627 | * processing MUST occur." However, we don't implement OAM | |
628 | * processing, thus drop the packet. | |
629 | */ | |
630 | if (gpe->oam_flag) | |
631 | return false; | |
632 | ||
b80837ac YY |
633 | *protocol = tun_p_to_eth_p(gpe->next_protocol); |
634 | if (!*protocol) | |
f2252c61 | 635 | return false; |
e23775f2 | 636 | |
f2252c61 PS |
637 | unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS; |
638 | return true; | |
639 | } | |
e23775f2 | 640 | |
f2252c61 PS |
641 | static bool vxlan_set_mac(struct vxlan_dev *vxlan, |
642 | struct vxlan_sock *vs, | |
643 | struct sk_buff *skb) | |
644 | { | |
645 | return true; | |
646 | } | |
647 | ||
648 | static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph, | |
649 | struct sk_buff *skb) | |
650 | { | |
651 | int err = 0; | |
652 | ||
653 | if (vxlan_get_sk_family(vs) == AF_INET) | |
654 | err = IP_ECN_decapsulate(oiph, skb); | |
655 | #if IS_ENABLED(CONFIG_IPV6) | |
656 | else | |
657 | err = IP6_ECN_decapsulate(oiph, skb); | |
658 | #endif | |
659 | return err <= 1; | |
e23775f2 PS |
660 | } |
661 | ||
662 | /* Callback from net/ipv4/udp.c to receive packets */ | |
f2252c61 | 663 | static int vxlan_rcv(struct sock *sk, struct sk_buff *skb) |
e23775f2 | 664 | { |
e23775f2 PS |
665 | union { |
666 | struct metadata_dst dst; | |
f2252c61 | 667 | char buf[sizeof(struct metadata_dst) + sizeof(struct vxlan_metadata)]; |
e23775f2 PS |
668 | } buf; |
669 | ||
f2252c61 PS |
670 | struct pcpu_sw_netstats *stats; |
671 | struct vxlan_dev *vxlan; | |
672 | struct vxlan_sock *vs; | |
673 | struct vxlanhdr unparsed; | |
674 | struct vxlan_metadata _md; | |
675 | struct vxlan_metadata *md = &_md; | |
676 | __be16 protocol = htons(ETH_P_TEB); | |
677 | bool raw_proto = false; | |
678 | void *oiph; | |
e23775f2 | 679 | |
f2252c61 PS |
680 | /* Need UDP and VXLAN header to be present */ |
681 | if (!pskb_may_pull(skb, VXLAN_HLEN)) | |
2b3397eb | 682 | goto drop; |
f2252c61 PS |
683 | |
684 | unparsed = *vxlan_hdr(skb); | |
685 | /* VNI flag always required to be set */ | |
686 | if (!(unparsed.vx_flags & VXLAN_HF_VNI)) { | |
687 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
688 | ntohl(vxlan_hdr(skb)->vx_flags), | |
689 | ntohl(vxlan_hdr(skb)->vx_vni)); | |
690 | /* Return non vxlan pkt */ | |
2b3397eb | 691 | goto drop; |
e23775f2 PS |
692 | } |
693 | ||
f2252c61 PS |
694 | unparsed.vx_flags &= ~VXLAN_HF_VNI; |
695 | unparsed.vx_vni &= ~VXLAN_VNI_MASK; | |
e23775f2 PS |
696 | |
697 | vs = rcu_dereference_sk_user_data(sk); | |
698 | if (!vs) | |
699 | goto drop; | |
700 | ||
f2252c61 PS |
701 | #if IS_ENABLED(CONFIG_IPV6) |
702 | #ifdef OVS_CHECK_UDP_TUNNEL_ZERO_CSUM | |
703 | if (vxlan_get_sk_family(vs) == AF_INET6 && | |
704 | !udp_hdr(skb)->check && | |
705 | !(vs->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) { | |
706 | udp6_csum_zero_error(skb); | |
707 | goto drop; | |
e23775f2 PS |
708 | } |
709 | #endif | |
f2252c61 PS |
710 | #endif |
711 | vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni)); | |
712 | if (!vxlan) | |
713 | goto drop; | |
e23775f2 PS |
714 | |
715 | /* For backwards compatibility, only allow reserved fields to be | |
716 | * used by VXLAN extensions if explicitly requested. | |
717 | */ | |
f2252c61 PS |
718 | if (vs->flags & VXLAN_F_GPE) { |
719 | if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags)) | |
720 | goto drop; | |
721 | raw_proto = true; | |
722 | } | |
e23775f2 | 723 | |
f2252c61 PS |
724 | if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto, |
725 | !net_eq(vxlan->net, dev_net(vxlan->dev)))) | |
726 | goto drop; | |
727 | ||
728 | if (vxlan_collect_metadata(vs)) { | |
729 | __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni); | |
730 | struct metadata_dst *tun_dst; | |
e23775f2 | 731 | |
f2252c61 | 732 | tun_dst = &buf.dst; |
a5d59bf3 | 733 | ovs_udp_tun_rx_dst(tun_dst, skb, |
f2252c61 PS |
734 | vxlan_get_sk_family(vs), TUNNEL_KEY, |
735 | vxlan_vni_to_tun_id(vni), sizeof(*md)); | |
e23775f2 | 736 | |
f2252c61 PS |
737 | if (!tun_dst) |
738 | goto drop; | |
e23775f2 | 739 | |
f2252c61 | 740 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); |
e23775f2 | 741 | |
f2252c61 PS |
742 | ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst); |
743 | } else { | |
744 | memset(md, 0, sizeof(*md)); | |
e23775f2 PS |
745 | } |
746 | ||
f2252c61 PS |
747 | if (vs->flags & VXLAN_F_REMCSUM_RX) |
748 | if (!vxlan_remcsum(&unparsed, skb, vs->flags)) | |
749 | goto drop; | |
750 | ||
751 | if (vs->flags & VXLAN_F_GBP) | |
752 | vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md); | |
753 | /* Note that GBP and GPE can never be active together. This is | |
754 | * ensured in vxlan_dev_configure. | |
755 | */ | |
756 | ||
757 | if (unparsed.vx_flags || unparsed.vx_vni) { | |
e23775f2 PS |
758 | /* If there are any unprocessed flags remaining treat |
759 | * this as a malformed packet. This behavior diverges from | |
760 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved | |
761 | * in reserved fields are to be ignored. The approach here | |
762 | * maintains compatibility with previous stack code, and also | |
763 | * is more robust and provides a little more security in | |
764 | * adding extensions to VXLAN. | |
765 | */ | |
f2252c61 PS |
766 | goto drop; |
767 | } | |
768 | ||
769 | if (!raw_proto) { | |
770 | if (!vxlan_set_mac(vxlan, vs, skb)) | |
771 | goto drop; | |
772 | skb_reset_mac_header(skb); | |
773 | skb->protocol = eth_type_trans(skb, vxlan->dev); | |
774 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
775 | } else { | |
776 | skb_reset_mac_header(skb); | |
777 | skb->dev = vxlan->dev; | |
778 | skb->pkt_type = PACKET_HOST; | |
779 | } | |
780 | ||
781 | oiph = skb_network_header(skb); | |
782 | skb_reset_network_header(skb); | |
e23775f2 | 783 | |
f2252c61 PS |
784 | if (!vxlan_ecn_decapsulate(vs, oiph, skb)) { |
785 | ++vxlan->dev->stats.rx_frame_errors; | |
786 | ++vxlan->dev->stats.rx_errors; | |
787 | goto drop; | |
e23775f2 PS |
788 | } |
789 | ||
f2252c61 PS |
790 | stats = this_cpu_ptr(vxlan->dev->tstats); |
791 | u64_stats_update_begin(&stats->syncp); | |
792 | stats->rx_packets++; | |
793 | stats->rx_bytes += skb->len; | |
794 | u64_stats_update_end(&stats->syncp); | |
795 | ||
796 | netdev_port_receive(skb, skb_tunnel_info(skb)); | |
e23775f2 PS |
797 | return 0; |
798 | ||
799 | drop: | |
800 | /* Consume bad packet */ | |
801 | kfree_skb(skb); | |
802 | return 0; | |
e23775f2 PS |
803 | } |
804 | ||
805 | static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags, | |
806 | struct vxlan_metadata *md) | |
807 | { | |
808 | struct vxlanhdr_gbp *gbp; | |
809 | ||
810 | if (!md->gbp) | |
811 | return; | |
812 | ||
813 | gbp = (struct vxlanhdr_gbp *)vxh; | |
f2252c61 | 814 | vxh->vx_flags |= VXLAN_HF_GBP; |
e23775f2 PS |
815 | |
816 | if (md->gbp & VXLAN_GBP_DONT_LEARN) | |
817 | gbp->dont_learn = 1; | |
818 | ||
819 | if (md->gbp & VXLAN_GBP_POLICY_APPLIED) | |
820 | gbp->policy_applied = 1; | |
821 | ||
822 | gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK); | |
823 | } | |
824 | ||
f2252c61 PS |
825 | static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags, |
826 | __be16 protocol) | |
e23775f2 | 827 | { |
f2252c61 | 828 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh; |
e23775f2 | 829 | |
f2252c61 | 830 | gpe->np_applied = 1; |
b80837ac YY |
831 | gpe->next_protocol = tun_p_from_eth_p(protocol); |
832 | if (!gpe->next_protocol) | |
833 | return -EPFNOSUPPORT; | |
834 | return 0; | |
e23775f2 | 835 | } |
e23775f2 | 836 | |
f2252c61 PS |
837 | static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, |
838 | int iphdr_len, __be32 vni, | |
839 | struct vxlan_metadata *md, u32 vxflags, | |
840 | bool udp_sum) | |
e23775f2 | 841 | { |
cf5789aa | 842 | void (*fix_segment)(struct sk_buff *); |
e23775f2 PS |
843 | struct vxlanhdr *vxh; |
844 | int min_headroom; | |
845 | int err; | |
f2252c61 PS |
846 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
847 | __be16 inner_protocol = htons(ETH_P_TEB); | |
e23775f2 PS |
848 | |
849 | if ((vxflags & VXLAN_F_REMCSUM_TX) && | |
850 | skb->ip_summed == CHECKSUM_PARTIAL) { | |
851 | int csum_start = skb_checksum_start_offset(skb); | |
852 | ||
853 | if (csum_start <= VXLAN_MAX_REMCSUM_START && | |
854 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && | |
855 | (skb->csum_offset == offsetof(struct udphdr, check) || | |
f2252c61 | 856 | skb->csum_offset == offsetof(struct tcphdr, check))) |
e23775f2 | 857 | type |= SKB_GSO_TUNNEL_REMCSUM; |
e23775f2 PS |
858 | } |
859 | ||
f2252c61 PS |
860 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
861 | + VXLAN_HLEN + iphdr_len | |
e23775f2 PS |
862 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
863 | ||
864 | /* Need space for new headers (invalidates iph ptr) */ | |
865 | err = skb_cow_head(skb, min_headroom); | |
f2252c61 PS |
866 | if (unlikely(err)) |
867 | goto out_free; | |
e23775f2 | 868 | |
436d36db GR |
869 | if (skb_vlan_tag_present(skb)) |
870 | skb = __vlan_hwaccel_push_inside(skb); | |
e23775f2 PS |
871 | if (WARN_ON(!skb)) |
872 | return -ENOMEM; | |
873 | ||
cf5789aa | 874 | type |= udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
b416d163 | 875 | #ifndef USE_UPSTREAM_TUNNEL_GSO |
db4b6975 | 876 | fix_segment = !udp_sum ? ovs_udp_gso : ovs_udp_csum_gso; |
b416d163 PS |
877 | #else |
878 | fix_segment = NULL; | |
879 | #endif | |
2a94b571 | 880 | err = ovs_iptunnel_handle_offloads(skb, type, fix_segment); |
f2252c61 PS |
881 | if (err) |
882 | goto out_free; | |
883 | ||
e23775f2 | 884 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
f2252c61 PS |
885 | vxh->vx_flags = VXLAN_HF_VNI; |
886 | vxh->vx_vni = vxlan_vni_field(vni); | |
e23775f2 PS |
887 | |
888 | if (type & SKB_GSO_TUNNEL_REMCSUM) { | |
f2252c61 | 889 | unsigned int start; |
e23775f2 | 890 | |
f2252c61 PS |
891 | start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr); |
892 | vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset); | |
893 | vxh->vx_flags |= VXLAN_HF_RCO; | |
e23775f2 PS |
894 | |
895 | if (!skb_is_gso(skb)) { | |
896 | skb->ip_summed = CHECKSUM_NONE; | |
e23775f2 | 897 | skb->encapsulation = 0; |
e23775f2 PS |
898 | } |
899 | } | |
f2252c61 | 900 | |
e23775f2 PS |
901 | if (vxflags & VXLAN_F_GBP) |
902 | vxlan_build_gbp_hdr(vxh, vxflags, md); | |
f2252c61 PS |
903 | if (vxflags & VXLAN_F_GPE) { |
904 | err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol); | |
905 | if (err < 0) | |
906 | goto out_free; | |
907 | inner_protocol = skb->protocol; | |
908 | } | |
e23775f2 | 909 | |
f2252c61 | 910 | ovs_skb_set_inner_protocol(skb, inner_protocol); |
43dd2fce | 911 | return 0; |
f2252c61 PS |
912 | |
913 | out_free: | |
914 | kfree_skb(skb); | |
915 | return err; | |
916 | } | |
917 | ||
918 | static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, | |
919 | struct sk_buff *skb, int oif, u8 tos, | |
920 | __be32 daddr, __be32 *saddr, | |
d69a9596 | 921 | __be16 dport, __be16 sport, |
e02c0ed7 | 922 | struct dst_cache *dst_cache, |
f2252c61 PS |
923 | const struct ip_tunnel_info *info) |
924 | { | |
ec7d36ff | 925 | bool use_cache = (dst_cache && ip_tunnel_dst_cache_usable(skb, info)); |
f2252c61 PS |
926 | struct rtable *rt = NULL; |
927 | struct flowi4 fl4; | |
928 | ||
e02c0ed7 PS |
929 | if (tos && !info) |
930 | use_cache = false; | |
931 | if (use_cache) { | |
932 | rt = dst_cache_get_ip4(dst_cache, saddr); | |
933 | if (rt) | |
934 | return rt; | |
935 | } | |
936 | ||
f2252c61 PS |
937 | memset(&fl4, 0, sizeof(fl4)); |
938 | fl4.flowi4_oif = oif; | |
939 | fl4.flowi4_tos = RT_TOS(tos); | |
940 | fl4.flowi4_mark = skb->mark; | |
941 | fl4.flowi4_proto = IPPROTO_UDP; | |
942 | fl4.daddr = daddr; | |
885f0013 | 943 | fl4.saddr = *saddr; |
d69a9596 QX |
944 | fl4.fl4_dport = dport; |
945 | fl4.fl4_sport = sport; | |
f2252c61 PS |
946 | |
947 | rt = ip_route_output_key(vxlan->net, &fl4); | |
948 | if (!IS_ERR(rt)) { | |
949 | *saddr = fl4.saddr; | |
e02c0ed7 PS |
950 | if (use_cache) |
951 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); | |
f2252c61 PS |
952 | } |
953 | return rt; | |
954 | } | |
955 | ||
956 | #if IS_ENABLED(CONFIG_IPV6) | |
957 | static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan, | |
958 | struct sk_buff *skb, int oif, u8 tos, | |
959 | __be32 label, | |
960 | const struct in6_addr *daddr, | |
961 | struct in6_addr *saddr, | |
d69a9596 | 962 | __be16 dport, __be16 sport, |
e02c0ed7 | 963 | struct dst_cache *dst_cache, |
f2252c61 PS |
964 | const struct ip_tunnel_info *info) |
965 | { | |
76e671ea | 966 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
ec7d36ff | 967 | bool use_cache = (dst_cache && ip_tunnel_dst_cache_usable(skb, info)); |
f2252c61 PS |
968 | struct dst_entry *ndst; |
969 | struct flowi6 fl6; | |
33f9c873 GR |
970 | #if !defined(HAVE_IPV6_STUB_WITH_DST_ENTRY) || \ |
971 | !defined(HAVE_IPV6_DST_LOOKUP_FLOW) | |
f2252c61 | 972 | int err; |
33f9c873 | 973 | #endif |
f2252c61 | 974 | |
76e671ea PS |
975 | if (!sock6) |
976 | return ERR_PTR(-EIO); | |
977 | ||
e02c0ed7 PS |
978 | if (tos && !info) |
979 | use_cache = false; | |
980 | if (use_cache) { | |
981 | ndst = dst_cache_get_ip6(dst_cache, saddr); | |
982 | if (ndst) | |
983 | return ndst; | |
984 | } | |
985 | ||
f2252c61 PS |
986 | memset(&fl6, 0, sizeof(fl6)); |
987 | fl6.flowi6_oif = oif; | |
988 | fl6.daddr = *daddr; | |
885f0013 | 989 | fl6.saddr = *saddr; |
f2252c61 PS |
990 | fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label); |
991 | fl6.flowi6_mark = skb->mark; | |
992 | fl6.flowi6_proto = IPPROTO_UDP; | |
d69a9596 QX |
993 | fl6.fl6_dport = dport; |
994 | fl6.fl6_sport = sport; | |
f2252c61 | 995 | |
33f9c873 GR |
996 | #if defined(HAVE_IPV6_STUB_WITH_DST_ENTRY) && defined(HAVE_IPV6_DST_LOOKUP_FLOW) |
997 | #ifdef HAVE_IPV6_DST_LOOKUP_FLOW_NET | |
998 | ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk, | |
999 | &fl6, NULL); | |
1000 | #else | |
1001 | ndst = ipv6_stub->ipv6_dst_lookup_flow(sock6->sock->sk, &fl6, NULL); | |
1002 | #endif | |
1003 | if (unlikely(IS_ERR(ndst))) { | |
1004 | #elif defined(HAVE_IPV6_DST_LOOKUP_FLOW_NET) | |
5519e384 YHW |
1005 | err = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk, |
1006 | &ndst, &fl6); | |
1007 | #elif defined(HAVE_IPV6_DST_LOOKUP_FLOW) | |
1008 | err = ipv6_stub->ipv6_dst_lookup_flow(sock6->sock->sk, &ndst, &fl6); | |
1009 | #elif defined(HAVE_IPV6_DST_LOOKUP_NET) | |
1010 | err = ipv6_stub->ipv6_dst_lookup(vxlan->net, sock6->sock->sk, | |
f2252c61 | 1011 | &ndst, &fl6); |
5519e384 | 1012 | #elif defined(HAVE_IPV6_STUB) |
f2252c61 PS |
1013 | err = ipv6_stub->ipv6_dst_lookup(vxlan->vn6_sock->sock->sk, |
1014 | &ndst, &fl6); | |
1015 | #else | |
1016 | err = ip6_dst_lookup(vxlan->vn6_sock->sock->sk, &ndst, &fl6); | |
f2252c61 | 1017 | #endif |
33f9c873 GR |
1018 | #if defined(HAVE_IPV6_STUB_WITH_DST_ENTRY) && defined(HAVE_IPV6_DST_LOOKUP_FLOW) |
1019 | return ERR_PTR(-ENETUNREACH); | |
1020 | } | |
1021 | #else | |
f2252c61 PS |
1022 | if (err < 0) |
1023 | return ERR_PTR(err); | |
33f9c873 | 1024 | #endif |
f2252c61 PS |
1025 | |
1026 | *saddr = fl6.saddr; | |
e02c0ed7 PS |
1027 | if (use_cache) |
1028 | dst_cache_set_ip6(dst_cache, ndst, saddr); | |
f2252c61 PS |
1029 | return ndst; |
1030 | } | |
1031 | #endif | |
1032 | ||
1033 | /* Bypass encapsulation if the destination is local */ | |
1034 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
1035 | struct vxlan_dev *dst_vxlan) | |
1036 | { | |
1037 | skb->dev->stats.rx_dropped++; | |
1038 | kfree_skb(skb); | |
e23775f2 PS |
1039 | } |
1040 | ||
1041 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, | |
1042 | struct vxlan_rdst *rdst, bool did_rsc) | |
1043 | { | |
e02c0ed7 | 1044 | struct dst_cache *dst_cache; |
e23775f2 PS |
1045 | struct ip_tunnel_info *info; |
1046 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f2252c61 | 1047 | struct sock *sk; |
e23775f2 PS |
1048 | struct rtable *rt = NULL; |
1049 | const struct iphdr *old_iph; | |
e23775f2 | 1050 | union vxlan_addr *dst; |
885f0013 PS |
1051 | union vxlan_addr remote_ip, local_ip; |
1052 | union vxlan_addr *src; | |
e23775f2 PS |
1053 | struct vxlan_metadata _md; |
1054 | struct vxlan_metadata *md = &_md; | |
1055 | __be16 src_port = 0, dst_port; | |
f2252c61 | 1056 | __be32 vni, label; |
e23775f2 PS |
1057 | __be16 df = 0; |
1058 | __u8 tos, ttl; | |
1059 | int err; | |
1060 | u32 flags = vxlan->flags; | |
f2252c61 PS |
1061 | bool udp_sum = false; |
1062 | bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); | |
e23775f2 PS |
1063 | |
1064 | info = skb_tunnel_info(skb); | |
1065 | ||
1066 | if (rdst) { | |
1067 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port; | |
1068 | vni = rdst->remote_vni; | |
1069 | dst = &rdst->remote_ip; | |
885f0013 | 1070 | src = &vxlan->cfg.saddr; |
e02c0ed7 | 1071 | dst_cache = &rdst->dst_cache; |
e23775f2 PS |
1072 | } else { |
1073 | if (!info) { | |
1074 | WARN_ONCE(1, "%s: Missing encapsulation instructions\n", | |
1075 | dev->name); | |
1076 | goto drop; | |
1077 | } | |
e23775f2 | 1078 | dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; |
f2252c61 PS |
1079 | vni = vxlan_tun_id_to_vni(info->key.tun_id); |
1080 | remote_ip.sa.sa_family = ip_tunnel_info_af(info); | |
885f0013 | 1081 | if (remote_ip.sa.sa_family == AF_INET) { |
e23775f2 | 1082 | remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst; |
885f0013 PS |
1083 | local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src; |
1084 | } else { | |
e23775f2 | 1085 | remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst; |
885f0013 PS |
1086 | local_ip.sin6.sin6_addr = info->key.u.ipv6.src; |
1087 | } | |
e23775f2 | 1088 | dst = &remote_ip; |
885f0013 | 1089 | src = &local_ip; |
e02c0ed7 | 1090 | dst_cache = &info->dst_cache; |
e23775f2 PS |
1091 | } |
1092 | ||
1093 | if (vxlan_addr_any(dst)) { | |
1094 | if (did_rsc) { | |
1095 | /* short-circuited back to local bridge */ | |
f2252c61 PS |
1096 | vxlan_encap_bypass(skb, vxlan, vxlan); |
1097 | return; | |
e23775f2 PS |
1098 | } |
1099 | goto drop; | |
1100 | } | |
1101 | ||
1102 | old_iph = ip_hdr(skb); | |
1103 | ||
1104 | ttl = vxlan->cfg.ttl; | |
1105 | if (!ttl && vxlan_addr_multicast(dst)) | |
1106 | ttl = 1; | |
1107 | ||
1108 | tos = vxlan->cfg.tos; | |
1109 | if (tos == 1) | |
1110 | tos = ip_tunnel_get_dsfield(old_iph, skb); | |
1111 | ||
3259c4ff | 1112 | label = vxlan->cfg.label; |
e23775f2 PS |
1113 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
1114 | vxlan->cfg.port_max, true); | |
1115 | ||
1116 | if (info) { | |
e23775f2 PS |
1117 | ttl = info->key.ttl; |
1118 | tos = info->key.tos; | |
3259c4ff | 1119 | label = info->key.label; |
f2252c61 | 1120 | udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
e23775f2 | 1121 | |
3378775c PJV |
1122 | if (info->options_len && |
1123 | info->key.tun_flags & TUNNEL_VXLAN_OPT) | |
e23775f2 PS |
1124 | md = ip_tunnel_info_opts(info); |
1125 | } else { | |
1126 | md->gbp = skb->mark; | |
1127 | } | |
1128 | ||
1129 | if (dst->sa.sa_family == AF_INET) { | |
76e671ea PS |
1130 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
1131 | ||
1132 | if (!sock4) | |
f2252c61 | 1133 | goto drop; |
76e671ea | 1134 | sk = sock4->sock->sk; |
e23775f2 | 1135 | |
f2252c61 PS |
1136 | rt = vxlan_get_route(vxlan, skb, |
1137 | rdst ? rdst->remote_ifindex : 0, tos, | |
885f0013 PS |
1138 | dst->sin.sin_addr.s_addr, |
1139 | &src->sin.sin_addr.s_addr, | |
d69a9596 | 1140 | dst_port, src_port, |
e02c0ed7 | 1141 | dst_cache, info); |
e23775f2 PS |
1142 | if (IS_ERR(rt)) { |
1143 | netdev_dbg(dev, "no route to %pI4\n", | |
1144 | &dst->sin.sin_addr.s_addr); | |
1145 | dev->stats.tx_carrier_errors++; | |
1146 | goto tx_error; | |
1147 | } | |
1148 | ||
8063e095 | 1149 | if (rt->dst.dev == dev) { |
e23775f2 PS |
1150 | netdev_dbg(dev, "circular route to %pI4\n", |
1151 | &dst->sin.sin_addr.s_addr); | |
1152 | dev->stats.collisions++; | |
1153 | goto rt_tx_error; | |
1154 | } | |
1155 | ||
1156 | /* Bypass encapsulation if the destination is local */ | |
b1c74f35 | 1157 | if (!info && rt->rt_flags & RTCF_LOCAL && |
e23775f2 PS |
1158 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
1159 | struct vxlan_dev *dst_vxlan; | |
1160 | ||
1161 | ip_rt_put(rt); | |
1162 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, | |
1163 | dst->sa.sa_family, dst_port, | |
1164 | vxlan->flags); | |
1165 | if (!dst_vxlan) | |
1166 | goto tx_error; | |
f2252c61 PS |
1167 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
1168 | return; | |
e23775f2 PS |
1169 | } |
1170 | ||
f2252c61 PS |
1171 | if (!info) |
1172 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); | |
1173 | else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) | |
1174 | df = htons(IP_DF); | |
1175 | ||
e23775f2 | 1176 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
8063e095 | 1177 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
f2252c61 PS |
1178 | err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr), |
1179 | vni, md, flags, udp_sum); | |
1180 | if (err < 0) | |
1181 | goto xmit_tx_error; | |
1182 | ||
885f0013 | 1183 | udp_tunnel_xmit_skb(rt, sk, skb, src->sin.sin_addr.s_addr, |
f2252c61 PS |
1184 | dst->sin.sin_addr.s_addr, tos, ttl, df, |
1185 | src_port, dst_port, xnet, !udp_sum); | |
e23775f2 PS |
1186 | #if IS_ENABLED(CONFIG_IPV6) |
1187 | } else { | |
76e671ea | 1188 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
e23775f2 | 1189 | struct dst_entry *ndst; |
e23775f2 PS |
1190 | u32 rt6i_flags; |
1191 | ||
76e671ea | 1192 | if (!sock6) |
f2252c61 | 1193 | goto drop; |
76e671ea | 1194 | sk = sock6->sock->sk; |
e23775f2 | 1195 | |
f2252c61 PS |
1196 | ndst = vxlan6_get_route(vxlan, skb, |
1197 | rdst ? rdst->remote_ifindex : 0, tos, | |
885f0013 PS |
1198 | label, &dst->sin6.sin6_addr, |
1199 | &src->sin6.sin6_addr, | |
d69a9596 | 1200 | dst_port, src_port, |
e02c0ed7 | 1201 | dst_cache, info); |
f2252c61 | 1202 | if (IS_ERR(ndst)) { |
e23775f2 PS |
1203 | netdev_dbg(dev, "no route to %pI6\n", |
1204 | &dst->sin6.sin6_addr); | |
1205 | dev->stats.tx_carrier_errors++; | |
1206 | goto tx_error; | |
1207 | } | |
1208 | ||
1209 | if (ndst->dev == dev) { | |
1210 | netdev_dbg(dev, "circular route to %pI6\n", | |
1211 | &dst->sin6.sin6_addr); | |
1212 | dst_release(ndst); | |
1213 | dev->stats.collisions++; | |
1214 | goto tx_error; | |
1215 | } | |
1216 | ||
1217 | /* Bypass encapsulation if the destination is local */ | |
1218 | rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags; | |
b1c74f35 | 1219 | if (!info && rt6i_flags & RTF_LOCAL && |
e23775f2 PS |
1220 | !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
1221 | struct vxlan_dev *dst_vxlan; | |
1222 | ||
1223 | dst_release(ndst); | |
1224 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, | |
1225 | dst->sa.sa_family, dst_port, | |
1226 | vxlan->flags); | |
1227 | if (!dst_vxlan) | |
1228 | goto tx_error; | |
f2252c61 PS |
1229 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
1230 | return; | |
e23775f2 PS |
1231 | } |
1232 | ||
f2252c61 PS |
1233 | if (!info) |
1234 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); | |
1235 | ||
1236 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); | |
e23775f2 | 1237 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
f2252c61 PS |
1238 | skb_scrub_packet(skb, xnet); |
1239 | err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr), | |
1240 | vni, md, flags, udp_sum); | |
1241 | if (err < 0) { | |
1242 | dst_release(ndst); | |
1243 | return; | |
1244 | } | |
1245 | udp_tunnel6_xmit_skb(ndst, sk, skb, dev, | |
885f0013 PS |
1246 | &src->sin6.sin6_addr, |
1247 | &dst->sin6.sin6_addr, tos, ttl, | |
f2252c61 | 1248 | label, src_port, dst_port, !udp_sum); |
e23775f2 PS |
1249 | #endif |
1250 | } | |
1251 | ||
1252 | return; | |
1253 | ||
1254 | drop: | |
1255 | dev->stats.tx_dropped++; | |
1256 | goto tx_free; | |
1257 | ||
f2252c61 PS |
1258 | xmit_tx_error: |
1259 | /* skb is already freed. */ | |
1260 | skb = NULL; | |
e23775f2 PS |
1261 | rt_tx_error: |
1262 | ip_rt_put(rt); | |
1263 | tx_error: | |
1264 | dev->stats.tx_errors++; | |
1265 | tx_free: | |
1266 | dev_kfree_skb(skb); | |
1267 | } | |
1268 | ||
1269 | /* Transmit local packets over Vxlan | |
1270 | * | |
1271 | * Outer IP header inherits ECN and DF from inner header. | |
1272 | * Outer UDP destination is the VXLAN assigned port. | |
1273 | * source port is based on hash of flow | |
1274 | */ | |
1275 | netdev_tx_t rpl_vxlan_xmit(struct sk_buff *skb) | |
1276 | { | |
1277 | struct net_device *dev = skb->dev; | |
1278 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1279 | const struct ip_tunnel_info *info; | |
1280 | ||
1281 | info = skb_tunnel_info(skb); | |
e23775f2 | 1282 | skb_reset_mac_header(skb); |
f2252c61 PS |
1283 | if (vxlan->flags & VXLAN_F_COLLECT_METADATA) { |
1284 | if (info && info->mode & IP_TUNNEL_INFO_TX) { | |
1285 | vxlan_xmit_one(skb, dev, NULL, false); | |
1286 | return NETDEV_TX_OK; | |
1287 | } | |
e23775f2 | 1288 | } |
f2252c61 PS |
1289 | |
1290 | dev->stats.tx_dropped++; | |
e23775f2 PS |
1291 | kfree_skb(skb); |
1292 | return NETDEV_TX_OK; | |
1293 | } | |
f2252c61 | 1294 | EXPORT_SYMBOL_GPL(rpl_vxlan_xmit); |
e23775f2 PS |
1295 | |
1296 | /* Walk the forwarding table and purge stale entries */ | |
7f63d830 | 1297 | #ifdef HAVE_INIT_TIMER_DEFERRABLE |
e23775f2 PS |
1298 | static void vxlan_cleanup(unsigned long arg) |
1299 | { | |
1300 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; | |
7f63d830 YS |
1301 | #else |
1302 | static void vxlan_cleanup(struct timer_list *t) | |
1303 | { | |
1304 | struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer); | |
1305 | #endif | |
e23775f2 PS |
1306 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
1307 | unsigned int h; | |
1308 | ||
1309 | if (!netif_running(vxlan->dev)) | |
1310 | return; | |
1311 | ||
1312 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1313 | struct hlist_node *p, *n; | |
1314 | ||
1315 | spin_lock_bh(&vxlan->hash_lock); | |
1316 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1317 | struct vxlan_fdb *f | |
1318 | = container_of(p, struct vxlan_fdb, hlist); | |
1319 | unsigned long timeout; | |
1320 | ||
1321 | if (f->state & NUD_PERMANENT) | |
1322 | continue; | |
1323 | ||
1324 | timeout = f->used + vxlan->cfg.age_interval * HZ; | |
1325 | if (time_before_eq(timeout, jiffies)) { | |
1326 | netdev_dbg(vxlan->dev, | |
1327 | "garbage collect %pM\n", | |
1328 | f->eth_addr); | |
1329 | f->state = NUD_STALE; | |
1330 | vxlan_fdb_destroy(vxlan, f); | |
1331 | } else if (time_before(timeout, next_timer)) | |
1332 | next_timer = timeout; | |
1333 | } | |
1334 | spin_unlock_bh(&vxlan->hash_lock); | |
1335 | } | |
1336 | ||
1337 | mod_timer(&vxlan->age_timer, next_timer); | |
1338 | } | |
1339 | ||
1340 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) | |
1341 | { | |
1342 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); | |
f2252c61 | 1343 | __be32 vni = vxlan->default_dst.remote_vni; |
e23775f2 | 1344 | |
e23775f2 PS |
1345 | spin_lock(&vn->sock_lock); |
1346 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); | |
1347 | spin_unlock(&vn->sock_lock); | |
1348 | } | |
1349 | ||
1350 | /* Setup stats when device is created */ | |
e23775f2 PS |
1351 | static int vxlan_init(struct net_device *dev) |
1352 | { | |
f2252c61 | 1353 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
e23775f2 PS |
1354 | if (!dev->tstats) |
1355 | return -ENOMEM; | |
1356 | ||
1357 | return 0; | |
1358 | } | |
e23775f2 PS |
1359 | |
1360 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) | |
1361 | { | |
e23775f2 PS |
1362 | } |
1363 | ||
e23775f2 PS |
1364 | static void vxlan_uninit(struct net_device *dev) |
1365 | { | |
1366 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1367 | ||
1368 | vxlan_fdb_delete_default(vxlan); | |
1369 | ||
1370 | free_percpu(dev->tstats); | |
1371 | } | |
e23775f2 PS |
1372 | |
1373 | /* Start ageing timer and join group when device is brought up */ | |
1374 | static int vxlan_open(struct net_device *dev) | |
1375 | { | |
1376 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f2252c61 | 1377 | int ret; |
e23775f2 | 1378 | |
f2252c61 PS |
1379 | ret = vxlan_sock_add(vxlan); |
1380 | if (ret < 0) | |
1381 | return ret; | |
e23775f2 PS |
1382 | |
1383 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { | |
1384 | ret = vxlan_igmp_join(vxlan); | |
1385 | if (ret == -EADDRINUSE) | |
1386 | ret = 0; | |
1387 | if (ret) { | |
f2252c61 | 1388 | vxlan_sock_release(vxlan); |
e23775f2 PS |
1389 | return ret; |
1390 | } | |
1391 | } | |
1392 | ||
1393 | if (vxlan->cfg.age_interval) | |
1394 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); | |
1395 | ||
1396 | return ret; | |
1397 | } | |
1398 | ||
1399 | /* Purge the forwarding table */ | |
1400 | static void vxlan_flush(struct vxlan_dev *vxlan) | |
1401 | { | |
1402 | unsigned int h; | |
1403 | ||
1404 | spin_lock_bh(&vxlan->hash_lock); | |
1405 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1406 | struct hlist_node *p, *n; | |
e23775f2 PS |
1407 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
1408 | struct vxlan_fdb *f | |
1409 | = container_of(p, struct vxlan_fdb, hlist); | |
1410 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ | |
1411 | if (!is_zero_ether_addr(f->eth_addr)) | |
1412 | vxlan_fdb_destroy(vxlan, f); | |
1413 | } | |
1414 | } | |
1415 | spin_unlock_bh(&vxlan->hash_lock); | |
1416 | } | |
1417 | ||
1418 | /* Cleanup timer and forwarding table on shutdown */ | |
1419 | static int vxlan_stop(struct net_device *dev) | |
1420 | { | |
1421 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1422 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); | |
e23775f2 PS |
1423 | int ret = 0; |
1424 | ||
1425 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && | |
1426 | !vxlan_group_used(vn, vxlan)) | |
1427 | ret = vxlan_igmp_leave(vxlan); | |
1428 | ||
1429 | del_timer_sync(&vxlan->age_timer); | |
1430 | ||
1431 | vxlan_flush(vxlan); | |
f2252c61 | 1432 | vxlan_sock_release(vxlan); |
e23775f2 PS |
1433 | |
1434 | return ret; | |
1435 | } | |
1436 | ||
1437 | /* Stub, nothing needs to be done. */ | |
1438 | static void vxlan_set_multicast_list(struct net_device *dev) | |
1439 | { | |
1440 | } | |
1441 | ||
27e39e3e DW |
1442 | static int __vxlan_change_mtu(struct net_device *dev, |
1443 | struct net_device *lowerdev, | |
1444 | struct vxlan_rdst *dst, int new_mtu, bool strict) | |
e23775f2 | 1445 | { |
27e39e3e | 1446 | int max_mtu = IP_MAX_MTU; |
e23775f2 | 1447 | |
27e39e3e DW |
1448 | if (lowerdev) |
1449 | max_mtu = lowerdev->mtu; | |
e23775f2 PS |
1450 | |
1451 | if (dst->remote_ip.sa.sa_family == AF_INET6) | |
27e39e3e | 1452 | max_mtu -= VXLAN6_HEADROOM; |
e23775f2 | 1453 | else |
27e39e3e | 1454 | max_mtu -= VXLAN_HEADROOM; |
e23775f2 | 1455 | |
27e39e3e | 1456 | if (new_mtu < 68) |
e23775f2 PS |
1457 | return -EINVAL; |
1458 | ||
27e39e3e DW |
1459 | if (new_mtu > max_mtu) { |
1460 | if (strict) | |
1461 | return -EINVAL; | |
1462 | ||
1463 | new_mtu = max_mtu; | |
1464 | } | |
1465 | ||
e23775f2 PS |
1466 | dev->mtu = new_mtu; |
1467 | return 0; | |
1468 | } | |
1469 | ||
27e39e3e DW |
1470 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
1471 | { | |
1472 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1473 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
1474 | struct net_device *lowerdev = __dev_get_by_index(vxlan->net, | |
1475 | dst->remote_ifindex); | |
1476 | return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true); | |
1477 | } | |
1478 | ||
f2252c61 | 1479 | int ovs_vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
aad7cb91 PS |
1480 | { |
1481 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f2252c61 PS |
1482 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
1483 | __be16 sport, dport; | |
aad7cb91 | 1484 | |
f2252c61 PS |
1485 | sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
1486 | vxlan->cfg.port_max, true); | |
1487 | dport = info->key.tp_dst ? : vxlan->cfg.dst_port; | |
aad7cb91 | 1488 | |
f2252c61 | 1489 | if (ip_tunnel_info_af(info) == AF_INET) { |
76e671ea | 1490 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
f2252c61 PS |
1491 | struct rtable *rt; |
1492 | ||
76e671ea | 1493 | if (!sock4) |
f2252c61 PS |
1494 | return -EINVAL; |
1495 | rt = vxlan_get_route(vxlan, skb, 0, info->key.tos, | |
1496 | info->key.u.ipv4.dst, | |
d69a9596 QX |
1497 | &info->key.u.ipv4.src, |
1498 | dport, sport, NULL, info); | |
f2252c61 PS |
1499 | if (IS_ERR(rt)) |
1500 | return PTR_ERR(rt); | |
1501 | ip_rt_put(rt); | |
1502 | } else { | |
1503 | #if IS_ENABLED(CONFIG_IPV6) | |
1504 | struct dst_entry *ndst; | |
aad7cb91 | 1505 | |
f2252c61 PS |
1506 | ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos, |
1507 | info->key.label, &info->key.u.ipv6.dst, | |
d69a9596 QX |
1508 | &info->key.u.ipv6.src, |
1509 | dport, sport, NULL, info); | |
f2252c61 PS |
1510 | if (IS_ERR(ndst)) |
1511 | return PTR_ERR(ndst); | |
1512 | dst_release(ndst); | |
1513 | #else /* !CONFIG_IPV6 */ | |
1514 | return -EPFNOSUPPORT; | |
1515 | #endif | |
1516 | } | |
aad7cb91 PS |
1517 | info->key.tp_src = sport; |
1518 | info->key.tp_dst = dport; | |
1519 | return 0; | |
1520 | } | |
f2252c61 | 1521 | EXPORT_SYMBOL_GPL(ovs_vxlan_fill_metadata_dst); |
aad7cb91 | 1522 | |
f2252c61 | 1523 | static netdev_tx_t vxlan_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
aad7cb91 | 1524 | { |
f2252c61 PS |
1525 | /* Drop All packets coming from networking stack. OVS-CB is |
1526 | * not initialized for these packets. | |
1527 | */ | |
aad7cb91 | 1528 | |
f2252c61 PS |
1529 | dev_kfree_skb(skb); |
1530 | dev->stats.tx_dropped++; | |
1531 | return NETDEV_TX_OK; | |
aad7cb91 | 1532 | } |
aad7cb91 | 1533 | |
f2252c61 | 1534 | static const struct net_device_ops vxlan_netdev_ether_ops = { |
e23775f2 PS |
1535 | .ndo_init = vxlan_init, |
1536 | .ndo_uninit = vxlan_uninit, | |
e23775f2 PS |
1537 | .ndo_open = vxlan_open, |
1538 | .ndo_stop = vxlan_stop, | |
1539 | .ndo_start_xmit = vxlan_dev_xmit, | |
f2252c61 | 1540 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
e23775f2 | 1541 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
39ca3383 | 1542 | #ifdef HAVE_RHEL7_MAX_MTU |
3ab8a26e | 1543 | .ndo_size = sizeof(struct net_device_ops), |
39ca3383 YHW |
1544 | .extended.ndo_change_mtu = vxlan_change_mtu, |
1545 | #else | |
e23775f2 | 1546 | .ndo_change_mtu = vxlan_change_mtu, |
39ca3383 | 1547 | #endif |
e23775f2 PS |
1548 | .ndo_validate_addr = eth_validate_addr, |
1549 | .ndo_set_mac_address = eth_mac_addr, | |
aad7cb91 | 1550 | #ifdef HAVE_NDO_FILL_METADATA_DST |
f2252c61 PS |
1551 | .ndo_fill_metadata_dst = ovs_vxlan_fill_metadata_dst, |
1552 | #endif | |
1553 | }; | |
1554 | ||
1555 | static const struct net_device_ops vxlan_netdev_raw_ops = { | |
1556 | .ndo_init = vxlan_init, | |
1557 | .ndo_uninit = vxlan_uninit, | |
1558 | .ndo_open = vxlan_open, | |
1559 | .ndo_stop = vxlan_stop, | |
1560 | .ndo_start_xmit = vxlan_dev_xmit, | |
1561 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
39ca3383 | 1562 | #ifdef HAVE_RHEL7_MAX_MTU |
3ab8a26e | 1563 | .ndo_size = sizeof(struct net_device_ops), |
39ca3383 YHW |
1564 | .extended.ndo_change_mtu = vxlan_change_mtu, |
1565 | #else | |
f2252c61 | 1566 | .ndo_change_mtu = vxlan_change_mtu, |
39ca3383 | 1567 | #endif |
f2252c61 PS |
1568 | #ifdef HAVE_NDO_FILL_METADATA_DST |
1569 | .ndo_fill_metadata_dst = ovs_vxlan_fill_metadata_dst, | |
aad7cb91 | 1570 | #endif |
e23775f2 PS |
1571 | }; |
1572 | ||
1573 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
1574 | static struct device_type vxlan_type = { | |
1575 | .name = "vxlan", | |
1576 | }; | |
1577 | ||
e2e11c89 | 1578 | /* Calls the ndo_add_vxlan_port or ndo_udp_tunnel_add of the caller |
1579 | * in order to supply the listening VXLAN udp ports. Callers are | |
1580 | * expected to implement the ndo_add_vxlan_port. | |
f2252c61 PS |
1581 | */ |
1582 | static void vxlan_push_rx_ports(struct net_device *dev) | |
1583 | { | |
1584 | #ifdef HAVE_NDO_ADD_VXLAN_PORT | |
1585 | struct vxlan_sock *vs; | |
1586 | struct net *net = dev_net(dev); | |
1587 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
1588 | sa_family_t sa_family; | |
1589 | __be16 port; | |
1590 | unsigned int i; | |
1591 | ||
1592 | if (!dev->netdev_ops->ndo_add_vxlan_port) | |
1593 | return; | |
1594 | ||
1595 | spin_lock(&vn->sock_lock); | |
1596 | for (i = 0; i < PORT_HASH_SIZE; ++i) { | |
1597 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { | |
1598 | port = inet_sk(vs->sock->sk)->inet_sport; | |
1599 | sa_family = vxlan_get_sk_family(vs); | |
1600 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, | |
1601 | port); | |
1602 | } | |
1603 | } | |
1604 | spin_unlock(&vn->sock_lock); | |
e2e11c89 | 1605 | #elif defined(HAVE_NDO_UDP_TUNNEL_ADD) |
1606 | struct vxlan_sock *vs; | |
1607 | struct net *net = dev_net(dev); | |
1608 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
1609 | unsigned int i; | |
1610 | ||
1611 | if (!dev->netdev_ops->ndo_udp_tunnel_add) | |
1612 | return; | |
1613 | ||
1614 | spin_lock(&vn->sock_lock); | |
1615 | for (i = 0; i < PORT_HASH_SIZE; ++i) { | |
1616 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { | |
1617 | struct udp_tunnel_info ti; | |
1618 | if (vs->flags & VXLAN_F_GPE) | |
1619 | ti.type = UDP_TUNNEL_TYPE_VXLAN_GPE; | |
1620 | else | |
1621 | ti.type = UDP_TUNNEL_TYPE_VXLAN; | |
1622 | ti.port = inet_sk(vs->sock->sk)->inet_sport; | |
1623 | ti.sa_family = vxlan_get_sk_family(vs); | |
1624 | ||
1625 | dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti); | |
1626 | } | |
1627 | } | |
1628 | spin_unlock(&vn->sock_lock); | |
f2252c61 PS |
1629 | #endif |
1630 | } | |
1631 | ||
e23775f2 PS |
1632 | /* Initialize the device structure. */ |
1633 | static void vxlan_setup(struct net_device *dev) | |
1634 | { | |
1635 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1636 | unsigned int h; | |
1637 | ||
1638 | eth_hw_addr_random(dev); | |
1639 | ether_setup(dev); | |
1640 | ||
436d36db | 1641 | #ifndef HAVE_NEEDS_FREE_NETDEV |
e23775f2 | 1642 | dev->destructor = free_netdev; |
436d36db GR |
1643 | #else |
1644 | dev->needs_free_netdev = true; | |
1645 | #endif | |
e23775f2 PS |
1646 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
1647 | ||
1648 | dev->features |= NETIF_F_LLTX; | |
1649 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; | |
1650 | dev->features |= NETIF_F_RXCSUM; | |
1651 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1652 | ||
1653 | dev->vlan_features = dev->features; | |
1654 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; | |
e23775f2 PS |
1655 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
1656 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
1657 | dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; | |
e23775f2 PS |
1658 | #if 0 |
1659 | netif_keep_dst(dev); | |
1660 | #endif | |
f2252c61 | 1661 | dev->priv_flags |= IFF_NO_QUEUE; |
e23775f2 PS |
1662 | |
1663 | INIT_LIST_HEAD(&vxlan->next); | |
1664 | spin_lock_init(&vxlan->hash_lock); | |
1665 | ||
7f63d830 | 1666 | #ifdef HAVE_INIT_TIMER_DEFERRABLE |
e23775f2 PS |
1667 | init_timer_deferrable(&vxlan->age_timer); |
1668 | vxlan->age_timer.function = vxlan_cleanup; | |
1669 | vxlan->age_timer.data = (unsigned long) vxlan; | |
7f63d830 YS |
1670 | #else |
1671 | timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE); | |
1672 | #endif | |
e23775f2 PS |
1673 | |
1674 | vxlan->cfg.dst_port = htons(vxlan_port); | |
1675 | ||
1676 | vxlan->dev = dev; | |
1677 | ||
1678 | for (h = 0; h < FDB_HASH_SIZE; ++h) | |
1679 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
1680 | } | |
1681 | ||
f2252c61 PS |
1682 | static void vxlan_ether_setup(struct net_device *dev) |
1683 | { | |
1684 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; | |
1685 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
1686 | dev->netdev_ops = &vxlan_netdev_ether_ops; | |
1687 | } | |
1688 | ||
1689 | static void vxlan_raw_setup(struct net_device *dev) | |
1690 | { | |
1691 | dev->header_ops = NULL; | |
1692 | dev->type = ARPHRD_NONE; | |
1693 | dev->hard_header_len = 0; | |
1694 | dev->addr_len = 0; | |
1695 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; | |
1696 | dev->netdev_ops = &vxlan_netdev_raw_ops; | |
1697 | } | |
1698 | ||
e23775f2 | 1699 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
f2252c61 | 1700 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
9cfa4718 | 1701 | [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) }, |
f2252c61 PS |
1702 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
1703 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, | |
9cfa4718 | 1704 | [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) }, |
f2252c61 PS |
1705 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
1706 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, | |
1707 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
1708 | [IFLA_VXLAN_LABEL] = { .type = NLA_U32 }, | |
1709 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, | |
1710 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
1711 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
1712 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, | |
1713 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, | |
1714 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
1715 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
1716 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
1717 | [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 }, | |
e23775f2 | 1718 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
f2252c61 PS |
1719 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
1720 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
1721 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
1722 | [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, | |
1723 | [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, | |
1724 | [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, }, | |
1725 | [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, }, | |
1726 | [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG }, | |
e23775f2 PS |
1727 | }; |
1728 | ||
82b7e6d1 | 1729 | #ifdef HAVE_RTNLOP_VALIDATE_WITH_EXTACK |
436d36db GR |
1730 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[], |
1731 | struct netlink_ext_ack *extack) | |
1732 | #else | |
e23775f2 | 1733 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
436d36db | 1734 | #endif |
e23775f2 PS |
1735 | { |
1736 | if (tb[IFLA_ADDRESS]) { | |
1737 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
1738 | pr_debug("invalid link address (not ethernet)\n"); | |
1739 | return -EINVAL; | |
1740 | } | |
1741 | ||
1742 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
1743 | pr_debug("invalid all zero ethernet address\n"); | |
1744 | return -EADDRNOTAVAIL; | |
1745 | } | |
1746 | } | |
1747 | ||
1748 | if (!data) | |
1749 | return -EINVAL; | |
1750 | ||
1751 | if (data[IFLA_VXLAN_ID]) { | |
1752 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); | |
1753 | if (id >= VXLAN_VID_MASK) | |
1754 | return -ERANGE; | |
1755 | } | |
1756 | ||
1757 | if (data[IFLA_VXLAN_PORT_RANGE]) { | |
1758 | const struct ifla_vxlan_port_range *p | |
1759 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
1760 | ||
1761 | if (ntohs(p->high) < ntohs(p->low)) { | |
1762 | pr_debug("port range %u .. %u not valid\n", | |
1763 | ntohs(p->low), ntohs(p->high)); | |
1764 | return -EINVAL; | |
1765 | } | |
1766 | } | |
1767 | ||
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | static void vxlan_get_drvinfo(struct net_device *netdev, | |
1772 | struct ethtool_drvinfo *drvinfo) | |
1773 | { | |
1774 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
1775 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
1776 | } | |
1777 | ||
1778 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
1779 | .get_drvinfo = vxlan_get_drvinfo, | |
1780 | .get_link = ethtool_op_get_link, | |
1781 | }; | |
1782 | ||
e23775f2 PS |
1783 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
1784 | __be16 port, u32 flags) | |
1785 | { | |
1786 | struct socket *sock; | |
1787 | struct udp_port_cfg udp_conf; | |
1788 | int err; | |
1789 | ||
1790 | memset(&udp_conf, 0, sizeof(udp_conf)); | |
1791 | ||
1792 | if (ipv6) { | |
1793 | udp_conf.family = AF_INET6; | |
1794 | udp_conf.use_udp6_rx_checksums = | |
1795 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); | |
1796 | udp_conf.ipv6_v6only = 1; | |
1797 | } else { | |
1798 | udp_conf.family = AF_INET; | |
1799 | } | |
1800 | ||
1801 | udp_conf.local_udp_port = port; | |
1802 | ||
1803 | /* Open UDP socket */ | |
1804 | err = udp_sock_create(net, &udp_conf, &sock); | |
1805 | if (err < 0) | |
1806 | return ERR_PTR(err); | |
1807 | ||
1808 | return sock; | |
1809 | } | |
1810 | ||
1811 | /* Create new listen socket if needed */ | |
f2252c61 PS |
1812 | static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6, |
1813 | __be16 port, u32 flags) | |
e23775f2 PS |
1814 | { |
1815 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
1816 | struct vxlan_sock *vs; | |
1817 | struct socket *sock; | |
1818 | unsigned int h; | |
e23775f2 PS |
1819 | struct udp_tunnel_sock_cfg tunnel_cfg; |
1820 | ||
1821 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); | |
1822 | if (!vs) | |
1823 | return ERR_PTR(-ENOMEM); | |
1824 | ||
1825 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
1826 | INIT_HLIST_HEAD(&vs->vni_list[h]); | |
1827 | ||
e23775f2 PS |
1828 | sock = vxlan_create_sock(net, ipv6, port, flags); |
1829 | if (IS_ERR(sock)) { | |
e23775f2 PS |
1830 | kfree(vs); |
1831 | return ERR_CAST(sock); | |
1832 | } | |
1833 | ||
1834 | vs->sock = sock; | |
1835 | atomic_set(&vs->refcnt, 1); | |
1836 | vs->flags = (flags & VXLAN_F_RCV_FLAGS); | |
1837 | ||
e23775f2 PS |
1838 | #ifdef HAVE_UDP_OFFLOAD |
1839 | vs->udp_offloads.port = port; | |
1840 | vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive; | |
1841 | vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete; | |
e23775f2 PS |
1842 | #endif |
1843 | ||
1844 | spin_lock(&vn->sock_lock); | |
1845 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); | |
f2252c61 | 1846 | vxlan_notify_add_rx_port(vs); |
e23775f2 PS |
1847 | spin_unlock(&vn->sock_lock); |
1848 | ||
1849 | /* Mark socket as an encapsulation socket. */ | |
f2252c61 | 1850 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
e23775f2 PS |
1851 | tunnel_cfg.sk_user_data = vs; |
1852 | tunnel_cfg.encap_type = 1; | |
f2252c61 | 1853 | tunnel_cfg.encap_rcv = vxlan_rcv; |
e23775f2 | 1854 | tunnel_cfg.encap_destroy = NULL; |
f2252c61 PS |
1855 | #ifdef HAVE_UDP_TUNNEL_SOCK_CFG_GRO_RECEIVE |
1856 | tunnel_cfg.gro_receive = vxlan_gro_receive; | |
1857 | tunnel_cfg.gro_complete = vxlan_gro_complete; | |
1858 | #endif | |
e23775f2 PS |
1859 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
1860 | ||
1861 | return vs; | |
1862 | } | |
1863 | ||
f2252c61 | 1864 | static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6) |
e23775f2 | 1865 | { |
f2252c61 PS |
1866 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
1867 | struct vxlan_sock *vs = NULL; | |
e23775f2 | 1868 | |
f2252c61 | 1869 | if (!vxlan->cfg.no_share) { |
e23775f2 | 1870 | spin_lock(&vn->sock_lock); |
f2252c61 PS |
1871 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
1872 | vxlan->cfg.dst_port, vxlan->flags); | |
1873 | if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) { | |
e23775f2 | 1874 | spin_unlock(&vn->sock_lock); |
f2252c61 | 1875 | return -EBUSY; |
e23775f2 PS |
1876 | } |
1877 | spin_unlock(&vn->sock_lock); | |
1878 | } | |
f2252c61 PS |
1879 | if (!vs) |
1880 | vs = vxlan_socket_create(vxlan->net, ipv6, | |
1881 | vxlan->cfg.dst_port, vxlan->flags); | |
1882 | if (IS_ERR(vs)) | |
1883 | return PTR_ERR(vs); | |
1884 | #if IS_ENABLED(CONFIG_IPV6) | |
1885 | if (ipv6) | |
76e671ea | 1886 | rcu_assign_pointer(vxlan->vn6_sock, vs); |
f2252c61 PS |
1887 | else |
1888 | #endif | |
76e671ea | 1889 | rcu_assign_pointer(vxlan->vn4_sock, vs); |
f2252c61 PS |
1890 | vxlan_vs_add_dev(vs, vxlan); |
1891 | return 0; | |
1892 | } | |
e23775f2 | 1893 | |
f2252c61 PS |
1894 | static int vxlan_sock_add(struct vxlan_dev *vxlan) |
1895 | { | |
f2252c61 | 1896 | bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA; |
86e4311b JB |
1897 | bool ipv6 = vxlan->flags & VXLAN_F_IPV6 || metadata; |
1898 | bool ipv4 = !ipv6 || metadata; | |
f2252c61 PS |
1899 | int ret = 0; |
1900 | ||
76e671ea | 1901 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
f2252c61 | 1902 | #if IS_ENABLED(CONFIG_IPV6) |
76e671ea | 1903 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
86e4311b | 1904 | if (ipv6) { |
f2252c61 | 1905 | ret = __vxlan_sock_add(vxlan, true); |
86e4311b JB |
1906 | if (ret < 0 && ret != -EAFNOSUPPORT) |
1907 | ipv4 = false; | |
1908 | } | |
f2252c61 | 1909 | #endif |
86e4311b | 1910 | if (ipv4) |
f2252c61 PS |
1911 | ret = __vxlan_sock_add(vxlan, false); |
1912 | if (ret < 0) | |
1913 | vxlan_sock_release(vxlan); | |
1914 | return ret; | |
e23775f2 PS |
1915 | } |
1916 | ||
1917 | static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, | |
1918 | struct vxlan_config *conf) | |
1919 | { | |
1920 | struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); | |
f2252c61 | 1921 | struct vxlan_dev *vxlan = netdev_priv(dev), *tmp; |
e23775f2 | 1922 | struct vxlan_rdst *dst = &vxlan->default_dst; |
f2252c61 | 1923 | unsigned short needed_headroom = ETH_HLEN; |
e23775f2 PS |
1924 | int err; |
1925 | bool use_ipv6 = false; | |
1926 | __be16 default_port = vxlan->cfg.dst_port; | |
06f1a61a | 1927 | struct net_device *lowerdev = NULL; |
e23775f2 | 1928 | |
f2252c61 PS |
1929 | if (conf->flags & VXLAN_F_GPE) { |
1930 | if (conf->flags & ~VXLAN_F_ALLOWED_GPE) | |
1931 | return -EINVAL; | |
1932 | /* For now, allow GPE only together with COLLECT_METADATA. | |
1933 | * This can be relaxed later; in such case, the other side | |
1934 | * of the PtP link will have to be provided. | |
1935 | */ | |
1936 | if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) | |
1937 | return -EINVAL; | |
1938 | ||
1939 | vxlan_raw_setup(dev); | |
1940 | } else { | |
1941 | vxlan_ether_setup(dev); | |
1942 | } | |
1943 | ||
e23775f2 PS |
1944 | vxlan->net = src_net; |
1945 | ||
1946 | dst->remote_vni = conf->vni; | |
1947 | ||
1948 | memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip)); | |
1949 | ||
1950 | /* Unless IPv6 is explicitly requested, assume IPv4 */ | |
1951 | if (!dst->remote_ip.sa.sa_family) | |
1952 | dst->remote_ip.sa.sa_family = AF_INET; | |
1953 | ||
1954 | if (dst->remote_ip.sa.sa_family == AF_INET6 || | |
1955 | vxlan->cfg.saddr.sa.sa_family == AF_INET6) { | |
1956 | if (!IS_ENABLED(CONFIG_IPV6)) | |
1957 | return -EPFNOSUPPORT; | |
1958 | use_ipv6 = true; | |
f2252c61 PS |
1959 | vxlan->flags |= VXLAN_F_IPV6; |
1960 | } | |
1961 | ||
1962 | if (conf->label && !use_ipv6) { | |
1963 | pr_info("label only supported in use with IPv6\n"); | |
1964 | return -EINVAL; | |
e23775f2 PS |
1965 | } |
1966 | ||
1967 | if (conf->remote_ifindex) { | |
06f1a61a | 1968 | lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex); |
e23775f2 PS |
1969 | dst->remote_ifindex = conf->remote_ifindex; |
1970 | ||
1971 | if (!lowerdev) { | |
1972 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); | |
1973 | return -ENODEV; | |
1974 | } | |
1975 | ||
1976 | #if IS_ENABLED(CONFIG_IPV6) | |
1977 | if (use_ipv6) { | |
1978 | struct inet6_dev *idev = __in6_dev_get(lowerdev); | |
1979 | if (idev && idev->cnf.disable_ipv6) { | |
1980 | pr_info("IPv6 is disabled via sysctl\n"); | |
1981 | return -EPERM; | |
1982 | } | |
e23775f2 PS |
1983 | } |
1984 | #endif | |
1985 | ||
1986 | if (!conf->mtu) | |
1987 | dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); | |
1988 | ||
f2252c61 | 1989 | needed_headroom = lowerdev->hard_header_len; |
e23775f2 PS |
1990 | } |
1991 | ||
06f1a61a DW |
1992 | if (conf->mtu) { |
1993 | err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false); | |
1994 | if (err) | |
1995 | return err; | |
1996 | } | |
1997 | ||
f2252c61 PS |
1998 | if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA) |
1999 | needed_headroom += VXLAN6_HEADROOM; | |
2000 | else | |
2001 | needed_headroom += VXLAN_HEADROOM; | |
2002 | dev->needed_headroom = needed_headroom; | |
2003 | ||
e23775f2 | 2004 | memcpy(&vxlan->cfg, conf, sizeof(*conf)); |
f2252c61 PS |
2005 | if (!vxlan->cfg.dst_port) { |
2006 | if (conf->flags & VXLAN_F_GPE) | |
2007 | vxlan->cfg.dst_port = 4790; /* IANA assigned VXLAN-GPE port */ | |
2008 | else | |
2009 | vxlan->cfg.dst_port = default_port; | |
2010 | } | |
e23775f2 PS |
2011 | vxlan->flags |= conf->flags; |
2012 | ||
2013 | if (!vxlan->cfg.age_interval) | |
2014 | vxlan->cfg.age_interval = FDB_AGE_DEFAULT; | |
2015 | ||
f2252c61 PS |
2016 | list_for_each_entry(tmp, &vn->vxlan_list, next) { |
2017 | if (tmp->cfg.vni == conf->vni && | |
2018 | (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 || | |
2019 | tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 && | |
2020 | tmp->cfg.dst_port == vxlan->cfg.dst_port && | |
2021 | (tmp->flags & VXLAN_F_RCV_FLAGS) == | |
2022 | (vxlan->flags & VXLAN_F_RCV_FLAGS)) | |
e23775f2 | 2023 | return -EEXIST; |
f2252c61 | 2024 | } |
e23775f2 PS |
2025 | |
2026 | dev->ethtool_ops = &vxlan_ethtool_ops; | |
2027 | ||
2028 | /* create an fdb entry for a valid default destination */ | |
2029 | if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { | |
2030 | err = vxlan_fdb_create(vxlan, all_zeros_mac, | |
2031 | &vxlan->default_dst.remote_ip, | |
2032 | NUD_REACHABLE|NUD_PERMANENT, | |
2033 | NLM_F_EXCL|NLM_F_CREATE, | |
2034 | vxlan->cfg.dst_port, | |
2035 | vxlan->default_dst.remote_vni, | |
2036 | vxlan->default_dst.remote_ifindex, | |
2037 | NTF_SELF); | |
2038 | if (err) | |
2039 | return err; | |
2040 | } | |
2041 | ||
2042 | err = register_netdevice(dev); | |
2043 | if (err) { | |
2044 | vxlan_fdb_delete_default(vxlan); | |
2045 | return err; | |
2046 | } | |
2047 | ||
2048 | list_add(&vxlan->next, &vn->vxlan_list); | |
2049 | ||
2050 | return 0; | |
2051 | } | |
2052 | ||
436d36db GR |
2053 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
2054 | static int vxlan_newlink(struct net *src_net, struct net_device *dev, | |
2055 | struct nlattr *tb[], struct nlattr *data[], | |
2056 | struct netlink_ext_ack *extack) | |
2057 | #else | |
e23775f2 PS |
2058 | static int vxlan_newlink(struct net *src_net, struct net_device *dev, |
2059 | struct nlattr *tb[], struct nlattr *data[]) | |
436d36db | 2060 | #endif |
e23775f2 | 2061 | { |
f2252c61 | 2062 | pr_info("unsupported operation\n"); |
e23775f2 PS |
2063 | return -EINVAL; |
2064 | } | |
2065 | ||
e23775f2 | 2066 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
e23775f2 PS |
2067 | { |
2068 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
2069 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); | |
2070 | ||
2071 | spin_lock(&vn->sock_lock); | |
2072 | if (!hlist_unhashed(&vxlan->hlist)) | |
2073 | hlist_del_rcu(&vxlan->hlist); | |
2074 | spin_unlock(&vn->sock_lock); | |
2075 | ||
2076 | list_del(&vxlan->next); | |
2077 | unregister_netdevice_queue(dev, head); | |
2078 | } | |
2079 | ||
2080 | static size_t vxlan_get_size(const struct net_device *dev) | |
2081 | { | |
2082 | ||
2083 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
2084 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ | |
2085 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ | |
2086 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ | |
2087 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ | |
2088 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ | |
f2252c61 | 2089 | nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */ |
e23775f2 PS |
2090 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
2091 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ | |
2092 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
2093 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
2094 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
2095 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */ | |
2096 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ | |
2097 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
2098 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + | |
2099 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ | |
2100 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ | |
2101 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ | |
2102 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ | |
2103 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ | |
2104 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ | |
2105 | 0; | |
2106 | } | |
2107 | ||
2108 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
2109 | { | |
2110 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
f2252c61 PS |
2111 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
2112 | struct ifla_vxlan_port_range ports = { | |
2113 | .low = htons(vxlan->cfg.port_min), | |
2114 | .high = htons(vxlan->cfg.port_max), | |
2115 | }; | |
2116 | ||
2117 | if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni))) | |
2118 | goto nla_put_failure; | |
2119 | ||
2120 | if (!vxlan_addr_any(&dst->remote_ip)) { | |
2121 | if (dst->remote_ip.sa.sa_family == AF_INET) { | |
2122 | if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP, | |
2123 | dst->remote_ip.sin.sin_addr.s_addr)) | |
2124 | goto nla_put_failure; | |
2125 | #if IS_ENABLED(CONFIG_IPV6) | |
2126 | } else { | |
2127 | if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6, | |
2128 | &dst->remote_ip.sin6.sin6_addr)) | |
2129 | goto nla_put_failure; | |
2130 | #endif | |
2131 | } | |
2132 | } | |
2133 | ||
2134 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) | |
2135 | goto nla_put_failure; | |
2136 | ||
2137 | if (!vxlan_addr_any(&vxlan->cfg.saddr)) { | |
2138 | if (vxlan->cfg.saddr.sa.sa_family == AF_INET) { | |
2139 | if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL, | |
2140 | vxlan->cfg.saddr.sin.sin_addr.s_addr)) | |
2141 | goto nla_put_failure; | |
2142 | #if IS_ENABLED(CONFIG_IPV6) | |
2143 | } else { | |
2144 | if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6, | |
2145 | &vxlan->cfg.saddr.sin6.sin6_addr)) | |
2146 | goto nla_put_failure; | |
2147 | #endif | |
2148 | } | |
2149 | } | |
2150 | ||
2151 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) || | |
2152 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) || | |
2153 | nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) || | |
2154 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, | |
2155 | !!(vxlan->flags & VXLAN_F_LEARN)) || | |
2156 | nla_put_u8(skb, IFLA_VXLAN_PROXY, | |
2157 | !!(vxlan->flags & VXLAN_F_PROXY)) || | |
2158 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || | |
2159 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, | |
2160 | !!(vxlan->flags & VXLAN_F_L2MISS)) || | |
2161 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, | |
2162 | !!(vxlan->flags & VXLAN_F_L3MISS)) || | |
2163 | nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA, | |
2164 | !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) || | |
2165 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) || | |
2166 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) || | |
2167 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) || | |
2168 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, | |
2169 | !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) || | |
2170 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, | |
2171 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || | |
2172 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, | |
2173 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || | |
2174 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, | |
2175 | !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) || | |
2176 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, | |
2177 | !!(vxlan->flags & VXLAN_F_REMCSUM_RX))) | |
2178 | goto nla_put_failure; | |
2179 | ||
2180 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) | |
2181 | goto nla_put_failure; | |
2182 | ||
2183 | if (vxlan->flags & VXLAN_F_GBP && | |
2184 | nla_put_flag(skb, IFLA_VXLAN_GBP)) | |
2185 | goto nla_put_failure; | |
2186 | ||
2187 | if (vxlan->flags & VXLAN_F_GPE && | |
2188 | nla_put_flag(skb, IFLA_VXLAN_GPE)) | |
2189 | goto nla_put_failure; | |
e23775f2 | 2190 | |
f2252c61 PS |
2191 | if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL && |
2192 | nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL)) | |
e23775f2 PS |
2193 | goto nla_put_failure; |
2194 | ||
2195 | return 0; | |
2196 | ||
2197 | nla_put_failure: | |
2198 | return -EMSGSIZE; | |
2199 | } | |
2200 | ||
2201 | #ifdef HAVE_GET_LINK_NET | |
2202 | static struct net *vxlan_get_link_net(const struct net_device *dev) | |
2203 | { | |
2204 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
2205 | ||
2206 | return vxlan->net; | |
2207 | } | |
2208 | #endif | |
2209 | ||
2210 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { | |
2211 | .kind = "ovs_vxlan", | |
2212 | .maxtype = IFLA_VXLAN_MAX, | |
2213 | .policy = vxlan_policy, | |
2214 | .priv_size = sizeof(struct vxlan_dev), | |
2215 | .setup = vxlan_setup, | |
2216 | .validate = vxlan_validate, | |
2217 | .newlink = vxlan_newlink, | |
2218 | .dellink = vxlan_dellink, | |
2219 | .get_size = vxlan_get_size, | |
2220 | .fill_info = vxlan_fill_info, | |
2221 | #ifdef HAVE_GET_LINK_NET | |
2222 | .get_link_net = vxlan_get_link_net, | |
2223 | #endif | |
2224 | }; | |
2225 | ||
f6d70869 PS |
2226 | struct net_device *rpl_vxlan_dev_create(struct net *net, const char *name, |
2227 | u8 name_assign_type, | |
2228 | struct vxlan_config *conf) | |
2229 | { | |
2230 | struct nlattr *tb[IFLA_MAX + 1]; | |
2231 | struct net_device *dev; | |
2232 | int err; | |
2233 | ||
2234 | memset(&tb, 0, sizeof(tb)); | |
2235 | ||
2236 | dev = rtnl_create_link(net, name, name_assign_type, | |
2237 | &vxlan_link_ops, tb); | |
2238 | if (IS_ERR(dev)) | |
2239 | return dev; | |
2240 | ||
2241 | err = vxlan_dev_configure(net, dev, conf); | |
2242 | if (err < 0) { | |
2243 | free_netdev(dev); | |
2244 | return ERR_PTR(err); | |
2245 | } | |
2246 | ||
2247 | err = rtnl_configure_link(dev, NULL); | |
2248 | if (err < 0) { | |
2249 | LIST_HEAD(list_kill); | |
2250 | ||
2251 | vxlan_dellink(dev, &list_kill); | |
2252 | unregister_netdevice_many(&list_kill); | |
2253 | return ERR_PTR(err); | |
2254 | } | |
2255 | ||
2256 | return dev; | |
2257 | } | |
2258 | EXPORT_SYMBOL_GPL(rpl_vxlan_dev_create); | |
2259 | ||
e23775f2 PS |
2260 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
2261 | struct net_device *dev) | |
2262 | { | |
2263 | struct vxlan_dev *vxlan, *next; | |
2264 | LIST_HEAD(list_kill); | |
2265 | ||
2266 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
2267 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
2268 | ||
2269 | /* In case we created vxlan device with carrier | |
2270 | * and we loose the carrier due to module unload | |
2271 | * we also need to remove vxlan device. In other | |
2272 | * cases, it's not necessary and remote_ifindex | |
2273 | * is 0 here, so no matches. | |
2274 | */ | |
2275 | if (dst->remote_ifindex == dev->ifindex) | |
e23775f2 | 2276 | vxlan_dellink(vxlan->dev, &list_kill); |
e23775f2 PS |
2277 | } |
2278 | ||
2279 | unregister_netdevice_many(&list_kill); | |
2280 | } | |
2281 | ||
f2252c61 PS |
2282 | static int vxlan_netdevice_event(struct notifier_block *unused, |
2283 | unsigned long event, void *ptr) | |
e23775f2 PS |
2284 | { |
2285 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
2286 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); | |
2287 | ||
2288 | if (event == NETDEV_UNREGISTER) | |
2289 | vxlan_handle_lowerdev_unregister(vn, dev); | |
f2252c61 PS |
2290 | else if (event == NETDEV_OFFLOAD_PUSH_VXLAN) |
2291 | vxlan_push_rx_ports(dev); | |
e23775f2 PS |
2292 | |
2293 | return NOTIFY_DONE; | |
2294 | } | |
2295 | ||
2296 | static struct notifier_block vxlan_notifier_block __read_mostly = { | |
f2252c61 | 2297 | .notifier_call = vxlan_netdevice_event, |
e23775f2 PS |
2298 | }; |
2299 | ||
2300 | static __net_init int vxlan_init_net(struct net *net) | |
2301 | { | |
2302 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2303 | unsigned int h; | |
2304 | ||
2305 | INIT_LIST_HEAD(&vn->vxlan_list); | |
2306 | spin_lock_init(&vn->sock_lock); | |
2307 | ||
2308 | for (h = 0; h < PORT_HASH_SIZE; ++h) | |
2309 | INIT_HLIST_HEAD(&vn->sock_list[h]); | |
2310 | ||
2311 | return 0; | |
2312 | } | |
2313 | ||
2314 | static void __net_exit vxlan_exit_net(struct net *net) | |
2315 | { | |
2316 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2317 | struct vxlan_dev *vxlan, *next; | |
2318 | struct net_device *dev, *aux; | |
2319 | LIST_HEAD(list); | |
2320 | ||
2321 | rtnl_lock(); | |
2322 | for_each_netdev_safe(net, dev, aux) | |
2323 | if (dev->rtnl_link_ops == &vxlan_link_ops) | |
2324 | unregister_netdevice_queue(dev, &list); | |
2325 | ||
2326 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
2327 | /* If vxlan->dev is in the same netns, it has already been added | |
2328 | * to the list by the previous loop. | |
2329 | */ | |
f2252c61 | 2330 | if (!net_eq(dev_net(vxlan->dev), net)) { |
e23775f2 | 2331 | unregister_netdevice_queue(vxlan->dev, &list); |
f2252c61 | 2332 | } |
e23775f2 PS |
2333 | } |
2334 | ||
2335 | unregister_netdevice_many(&list); | |
2336 | rtnl_unlock(); | |
2337 | } | |
2338 | ||
2339 | static struct pernet_operations vxlan_net_ops = { | |
2340 | .init = vxlan_init_net, | |
2341 | .exit = vxlan_exit_net, | |
2342 | .id = &vxlan_net_id, | |
2343 | .size = sizeof(struct vxlan_net), | |
2344 | }; | |
2345 | ||
e23775f2 PS |
2346 | int rpl_vxlan_init_module(void) |
2347 | { | |
2348 | int rc; | |
2349 | ||
e23775f2 PS |
2350 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
2351 | ||
2352 | rc = register_pernet_subsys(&vxlan_net_ops); | |
2353 | if (rc) | |
2354 | goto out1; | |
2355 | ||
2356 | rc = register_netdevice_notifier(&vxlan_notifier_block); | |
2357 | if (rc) | |
2358 | goto out2; | |
2359 | ||
2360 | rc = rtnl_link_register(&vxlan_link_ops); | |
2361 | if (rc) | |
2362 | goto out3; | |
2363 | ||
2364 | pr_info("VxLAN tunneling driver\n"); | |
2365 | return 0; | |
2366 | out3: | |
2367 | unregister_netdevice_notifier(&vxlan_notifier_block); | |
2368 | out2: | |
2369 | unregister_pernet_subsys(&vxlan_net_ops); | |
2370 | out1: | |
258b27d3 | 2371 | pr_err("Error while initializing VxLAN %d\n", rc); |
e23775f2 PS |
2372 | return rc; |
2373 | } | |
2374 | ||
2375 | void rpl_vxlan_cleanup_module(void) | |
2376 | { | |
2377 | rtnl_link_unregister(&vxlan_link_ops); | |
2378 | unregister_netdevice_notifier(&vxlan_notifier_block); | |
e23775f2 PS |
2379 | unregister_pernet_subsys(&vxlan_net_ops); |
2380 | /* rcu_barrier() is called by netns */ | |
2381 | } | |
2382 | #endif |