]>
Commit | Line | Data |
---|---|---|
2d07dc79 JL |
1 | /* |
2 | * GENEVE: Generic Network Virtualization Encapsulation | |
3 | * | |
4 | * Copyright (c) 2015 Red Hat, Inc. | |
5 | * | |
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. | |
9 | */ | |
10 | ||
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/netdevice.h> | |
16 | #include <linux/etherdevice.h> | |
17 | #include <linux/hash.h> | |
e305ac6c | 18 | #include <net/dst_metadata.h> |
8e816df8 | 19 | #include <net/gro_cells.h> |
2d07dc79 JL |
20 | #include <net/rtnetlink.h> |
21 | #include <net/geneve.h> | |
371bd106 | 22 | #include <net/protocol.h> |
2d07dc79 JL |
23 | |
24 | #define GENEVE_NETDEV_VER "0.6" | |
25 | ||
26 | #define GENEVE_UDP_PORT 6081 | |
27 | ||
28 | #define GENEVE_N_VID (1u << 24) | |
29 | #define GENEVE_VID_MASK (GENEVE_N_VID - 1) | |
30 | ||
31 | #define VNI_HASH_BITS 10 | |
32 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) | |
33 | ||
34 | static bool log_ecn_error = true; | |
35 | module_param(log_ecn_error, bool, 0644); | |
36 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
37 | ||
371bd106 PS |
38 | #define GENEVE_VER 0 |
39 | #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) | |
40 | ||
2d07dc79 JL |
41 | /* per-network namespace private data for this module */ |
42 | struct geneve_net { | |
371bd106 | 43 | struct list_head geneve_list; |
371bd106 | 44 | struct list_head sock_list; |
2d07dc79 JL |
45 | }; |
46 | ||
371bd106 PS |
47 | static int geneve_net_id; |
48 | ||
8ed66f0e JL |
49 | union geneve_addr { |
50 | struct sockaddr_in sin; | |
51 | struct sockaddr_in6 sin6; | |
52 | struct sockaddr sa; | |
53 | }; | |
54 | ||
55 | static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, }; | |
56 | ||
2d07dc79 JL |
57 | /* Pseudo network device */ |
58 | struct geneve_dev { | |
59 | struct hlist_node hlist; /* vni hash table */ | |
60 | struct net *net; /* netns for packet i/o */ | |
61 | struct net_device *dev; /* netdev for geneve tunnel */ | |
8ed66f0e JL |
62 | struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */ |
63 | #if IS_ENABLED(CONFIG_IPV6) | |
64 | struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */ | |
65 | #endif | |
8760ce58 JL |
66 | u8 vni[3]; /* virtual network ID for tunnel */ |
67 | u8 ttl; /* TTL override */ | |
d8951125 | 68 | u8 tos; /* TOS override */ |
8ed66f0e | 69 | union geneve_addr remote; /* IP address for link partner */ |
2d07dc79 | 70 | struct list_head next; /* geneve's per namespace list */ |
8eb3b995 | 71 | __be32 label; /* IPv6 flowlabel override */ |
cd7918b3 | 72 | __be16 dst_port; |
e305ac6c | 73 | bool collect_md; |
8e816df8 | 74 | struct gro_cells gro_cells; |
abe492b4 | 75 | u32 flags; |
468dfffc | 76 | struct dst_cache dst_cache; |
2d07dc79 JL |
77 | }; |
78 | ||
abe492b4 | 79 | /* Geneve device flags */ |
14f1f724 | 80 | #define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0) |
abe492b4 TH |
81 | #define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1) |
82 | #define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2) | |
83 | ||
371bd106 PS |
84 | struct geneve_sock { |
85 | bool collect_md; | |
371bd106 PS |
86 | struct list_head list; |
87 | struct socket *sock; | |
88 | struct rcu_head rcu; | |
89 | int refcnt; | |
66d47003 | 90 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
abe492b4 | 91 | u32 flags; |
371bd106 | 92 | }; |
2d07dc79 JL |
93 | |
94 | static inline __u32 geneve_net_vni_hash(u8 vni[3]) | |
95 | { | |
96 | __u32 vnid; | |
97 | ||
98 | vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; | |
99 | return hash_32(vnid, VNI_HASH_BITS); | |
100 | } | |
101 | ||
e305ac6c PS |
102 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
103 | { | |
104 | #ifdef __BIG_ENDIAN | |
105 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; | |
106 | #else | |
107 | return (__force __be64)(((__force u64)vni[0] << 40) | | |
108 | ((__force u64)vni[1] << 48) | | |
109 | ((__force u64)vni[2] << 56)); | |
110 | #endif | |
111 | } | |
112 | ||
1e9f12ec JB |
113 | static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) |
114 | { | |
115 | return gs->sock->sk->sk_family; | |
116 | } | |
117 | ||
66d47003 | 118 | static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, |
371bd106 | 119 | __be32 addr, u8 vni[]) |
2d07dc79 | 120 | { |
2d07dc79 | 121 | struct hlist_head *vni_list_head; |
e305ac6c | 122 | struct geneve_dev *geneve; |
2d07dc79 JL |
123 | __u32 hash; |
124 | ||
2d07dc79 | 125 | /* Find the device for this VNI */ |
371bd106 | 126 | hash = geneve_net_vni_hash(vni); |
66d47003 | 127 | vni_list_head = &gs->vni_list[hash]; |
e305ac6c | 128 | hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { |
371bd106 | 129 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && |
8ed66f0e JL |
130 | addr == geneve->remote.sin.sin_addr.s_addr) |
131 | return geneve; | |
132 | } | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | #if IS_ENABLED(CONFIG_IPV6) | |
137 | static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, | |
138 | struct in6_addr addr6, u8 vni[]) | |
139 | { | |
140 | struct hlist_head *vni_list_head; | |
141 | struct geneve_dev *geneve; | |
142 | __u32 hash; | |
143 | ||
144 | /* Find the device for this VNI */ | |
145 | hash = geneve_net_vni_hash(vni); | |
146 | vni_list_head = &gs->vni_list[hash]; | |
147 | hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { | |
148 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && | |
149 | ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr)) | |
e305ac6c | 150 | return geneve; |
2d07dc79 | 151 | } |
e305ac6c PS |
152 | return NULL; |
153 | } | |
8ed66f0e | 154 | #endif |
e305ac6c | 155 | |
371bd106 PS |
156 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
157 | { | |
158 | return (struct genevehdr *)(udp_hdr(skb) + 1); | |
159 | } | |
160 | ||
9fc47545 JB |
161 | static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, |
162 | struct sk_buff *skb) | |
e305ac6c | 163 | { |
9fc47545 | 164 | u8 *vni; |
371bd106 | 165 | __be32 addr; |
8ed66f0e | 166 | static u8 zero_vni[3]; |
8ed66f0e | 167 | #if IS_ENABLED(CONFIG_IPV6) |
8ed66f0e JL |
168 | static struct in6_addr zero_addr6; |
169 | #endif | |
e305ac6c | 170 | |
1e9f12ec | 171 | if (geneve_get_sk_family(gs) == AF_INET) { |
9fc47545 JB |
172 | struct iphdr *iph; |
173 | ||
8ed66f0e | 174 | iph = ip_hdr(skb); /* outer IP header... */ |
371bd106 | 175 | |
8ed66f0e JL |
176 | if (gs->collect_md) { |
177 | vni = zero_vni; | |
178 | addr = 0; | |
179 | } else { | |
9fc47545 | 180 | vni = geneve_hdr(skb)->vni; |
8ed66f0e JL |
181 | addr = iph->saddr; |
182 | } | |
183 | ||
9fc47545 | 184 | return geneve_lookup(gs, addr, vni); |
8ed66f0e | 185 | #if IS_ENABLED(CONFIG_IPV6) |
1e9f12ec | 186 | } else if (geneve_get_sk_family(gs) == AF_INET6) { |
9fc47545 JB |
187 | struct ipv6hdr *ip6h; |
188 | struct in6_addr addr6; | |
189 | ||
8ed66f0e | 190 | ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ |
371bd106 | 191 | |
8ed66f0e JL |
192 | if (gs->collect_md) { |
193 | vni = zero_vni; | |
194 | addr6 = zero_addr6; | |
195 | } else { | |
9fc47545 | 196 | vni = geneve_hdr(skb)->vni; |
8ed66f0e JL |
197 | addr6 = ip6h->saddr; |
198 | } | |
199 | ||
9fc47545 | 200 | return geneve6_lookup(gs, addr6, vni); |
8ed66f0e JL |
201 | #endif |
202 | } | |
9fc47545 JB |
203 | return NULL; |
204 | } | |
205 | ||
206 | /* geneve receive/decap routine */ | |
207 | static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, | |
208 | struct sk_buff *skb) | |
209 | { | |
210 | struct genevehdr *gnvh = geneve_hdr(skb); | |
211 | struct metadata_dst *tun_dst = NULL; | |
212 | struct pcpu_sw_netstats *stats; | |
213 | int err = 0; | |
214 | void *oiph; | |
2d07dc79 | 215 | |
371bd106 | 216 | if (ip_tunnel_collect_metadata() || gs->collect_md) { |
e305ac6c | 217 | __be16 flags; |
e305ac6c PS |
218 | |
219 | flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | | |
220 | (gnvh->oam ? TUNNEL_OAM : 0) | | |
221 | (gnvh->critical ? TUNNEL_CRIT_OPT : 0); | |
222 | ||
1e9f12ec | 223 | tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, |
e305ac6c PS |
224 | vni_to_tunnel_id(gnvh->vni), |
225 | gnvh->opt_len * 4); | |
226 | if (!tun_dst) | |
227 | goto drop; | |
e305ac6c | 228 | /* Update tunnel dst according to Geneve options. */ |
4c222798 PS |
229 | ip_tunnel_info_opts_set(&tun_dst->u.tun_info, |
230 | gnvh->options, gnvh->opt_len * 4); | |
e305ac6c PS |
231 | } else { |
232 | /* Drop packets w/ critical options, | |
233 | * since we don't support any... | |
234 | */ | |
235 | if (gnvh->critical) | |
236 | goto drop; | |
237 | } | |
2d07dc79 JL |
238 | |
239 | skb_reset_mac_header(skb); | |
2d07dc79 JL |
240 | skb->protocol = eth_type_trans(skb, geneve->dev); |
241 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
242 | ||
e305ac6c PS |
243 | if (tun_dst) |
244 | skb_dst_set(skb, &tun_dst->dst); | |
245 | ||
2d07dc79 JL |
246 | /* Ignore packet loops (and multicast echo) */ |
247 | if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) | |
248 | goto drop; | |
249 | ||
9fc47545 | 250 | oiph = skb_network_header(skb); |
2d07dc79 JL |
251 | skb_reset_network_header(skb); |
252 | ||
9fc47545 JB |
253 | if (geneve_get_sk_family(gs) == AF_INET) |
254 | err = IP_ECN_decapsulate(oiph, skb); | |
8ed66f0e | 255 | #if IS_ENABLED(CONFIG_IPV6) |
9fc47545 JB |
256 | else |
257 | err = IP6_ECN_decapsulate(oiph, skb); | |
8ed66f0e | 258 | #endif |
2d07dc79 JL |
259 | |
260 | if (unlikely(err)) { | |
8ed66f0e | 261 | if (log_ecn_error) { |
9fc47545 | 262 | if (geneve_get_sk_family(gs) == AF_INET) |
8ed66f0e JL |
263 | net_info_ratelimited("non-ECT from %pI4 " |
264 | "with TOS=%#x\n", | |
9fc47545 JB |
265 | &((struct iphdr *)oiph)->saddr, |
266 | ((struct iphdr *)oiph)->tos); | |
8ed66f0e | 267 | #if IS_ENABLED(CONFIG_IPV6) |
9fc47545 | 268 | else |
8ed66f0e | 269 | net_info_ratelimited("non-ECT from %pI6\n", |
9fc47545 | 270 | &((struct ipv6hdr *)oiph)->saddr); |
8ed66f0e JL |
271 | #endif |
272 | } | |
2d07dc79 JL |
273 | if (err > 1) { |
274 | ++geneve->dev->stats.rx_frame_errors; | |
275 | ++geneve->dev->stats.rx_errors; | |
276 | goto drop; | |
277 | } | |
278 | } | |
279 | ||
280 | stats = this_cpu_ptr(geneve->dev->tstats); | |
281 | u64_stats_update_begin(&stats->syncp); | |
282 | stats->rx_packets++; | |
283 | stats->rx_bytes += skb->len; | |
284 | u64_stats_update_end(&stats->syncp); | |
285 | ||
8e816df8 | 286 | gro_cells_receive(&geneve->gro_cells, skb); |
2d07dc79 JL |
287 | return; |
288 | drop: | |
289 | /* Consume bad packet */ | |
290 | kfree_skb(skb); | |
291 | } | |
292 | ||
293 | /* Setup stats when device is created */ | |
294 | static int geneve_init(struct net_device *dev) | |
295 | { | |
8e816df8 JG |
296 | struct geneve_dev *geneve = netdev_priv(dev); |
297 | int err; | |
298 | ||
2d07dc79 JL |
299 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
300 | if (!dev->tstats) | |
301 | return -ENOMEM; | |
8e816df8 JG |
302 | |
303 | err = gro_cells_init(&geneve->gro_cells, dev); | |
304 | if (err) { | |
305 | free_percpu(dev->tstats); | |
306 | return err; | |
307 | } | |
308 | ||
468dfffc PA |
309 | err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL); |
310 | if (err) { | |
311 | free_percpu(dev->tstats); | |
312 | gro_cells_destroy(&geneve->gro_cells); | |
313 | return err; | |
314 | } | |
315 | ||
2d07dc79 JL |
316 | return 0; |
317 | } | |
318 | ||
319 | static void geneve_uninit(struct net_device *dev) | |
320 | { | |
8e816df8 JG |
321 | struct geneve_dev *geneve = netdev_priv(dev); |
322 | ||
468dfffc | 323 | dst_cache_destroy(&geneve->dst_cache); |
8e816df8 | 324 | gro_cells_destroy(&geneve->gro_cells); |
2d07dc79 JL |
325 | free_percpu(dev->tstats); |
326 | } | |
327 | ||
371bd106 PS |
328 | /* Callback from net/ipv4/udp.c to receive packets */ |
329 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
330 | { | |
331 | struct genevehdr *geneveh; | |
9fc47545 | 332 | struct geneve_dev *geneve; |
371bd106 PS |
333 | struct geneve_sock *gs; |
334 | int opts_len; | |
335 | ||
336 | /* Need Geneve and inner Ethernet header to be present */ | |
337 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) | |
338 | goto error; | |
339 | ||
340 | /* Return packets with reserved bits set */ | |
341 | geneveh = geneve_hdr(skb); | |
342 | if (unlikely(geneveh->ver != GENEVE_VER)) | |
343 | goto error; | |
344 | ||
345 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) | |
346 | goto error; | |
347 | ||
9fc47545 JB |
348 | gs = rcu_dereference_sk_user_data(sk); |
349 | if (!gs) | |
350 | goto drop; | |
351 | ||
352 | geneve = geneve_lookup_skb(gs, skb); | |
353 | if (!geneve) | |
354 | goto drop; | |
355 | ||
371bd106 PS |
356 | opts_len = geneveh->opt_len * 4; |
357 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, | |
7f290c94 JB |
358 | htons(ETH_P_TEB), |
359 | !net_eq(geneve->net, dev_net(geneve->dev)))) | |
371bd106 PS |
360 | goto drop; |
361 | ||
9fc47545 | 362 | geneve_rx(geneve, gs, skb); |
371bd106 PS |
363 | return 0; |
364 | ||
365 | drop: | |
366 | /* Consume bad packet */ | |
367 | kfree_skb(skb); | |
368 | return 0; | |
369 | ||
370 | error: | |
371 | /* Let the UDP layer deal with the skb */ | |
372 | return 1; | |
373 | } | |
374 | ||
375 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, | |
abe492b4 | 376 | __be16 port, u32 flags) |
371bd106 PS |
377 | { |
378 | struct socket *sock; | |
379 | struct udp_port_cfg udp_conf; | |
380 | int err; | |
381 | ||
382 | memset(&udp_conf, 0, sizeof(udp_conf)); | |
383 | ||
384 | if (ipv6) { | |
385 | udp_conf.family = AF_INET6; | |
8ed66f0e | 386 | udp_conf.ipv6_v6only = 1; |
abe492b4 TH |
387 | udp_conf.use_udp6_rx_checksums = |
388 | !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX); | |
371bd106 PS |
389 | } else { |
390 | udp_conf.family = AF_INET; | |
391 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); | |
392 | } | |
393 | ||
394 | udp_conf.local_udp_port = port; | |
395 | ||
396 | /* Open UDP socket */ | |
397 | err = udp_sock_create(net, &udp_conf, &sock); | |
398 | if (err < 0) | |
399 | return ERR_PTR(err); | |
400 | ||
401 | return sock; | |
402 | } | |
403 | ||
404 | static void geneve_notify_add_rx_port(struct geneve_sock *gs) | |
405 | { | |
a8170d2b | 406 | struct net_device *dev; |
371bd106 | 407 | struct sock *sk = gs->sock->sk; |
a8170d2b | 408 | struct net *net = sock_net(sk); |
1e9f12ec | 409 | sa_family_t sa_family = geneve_get_sk_family(gs); |
a8170d2b | 410 | __be16 port = inet_sk(sk)->inet_sport; |
a8170d2b SA |
411 | |
412 | rcu_read_lock(); | |
413 | for_each_netdev_rcu(net, dev) { | |
414 | if (dev->netdev_ops->ndo_add_geneve_port) | |
415 | dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, | |
416 | port); | |
417 | } | |
418 | rcu_read_unlock(); | |
371bd106 PS |
419 | } |
420 | ||
421 | static int geneve_hlen(struct genevehdr *gh) | |
422 | { | |
423 | return sizeof(*gh) + gh->opt_len * 4; | |
424 | } | |
425 | ||
4a0090a9 TH |
426 | static struct sk_buff **geneve_gro_receive(struct sock *sk, |
427 | struct sk_buff **head, | |
428 | struct sk_buff *skb) | |
371bd106 PS |
429 | { |
430 | struct sk_buff *p, **pp = NULL; | |
431 | struct genevehdr *gh, *gh2; | |
432 | unsigned int hlen, gh_len, off_gnv; | |
433 | const struct packet_offload *ptype; | |
434 | __be16 type; | |
435 | int flush = 1; | |
436 | ||
437 | off_gnv = skb_gro_offset(skb); | |
438 | hlen = off_gnv + sizeof(*gh); | |
439 | gh = skb_gro_header_fast(skb, off_gnv); | |
440 | if (skb_gro_header_hard(skb, hlen)) { | |
441 | gh = skb_gro_header_slow(skb, hlen, off_gnv); | |
442 | if (unlikely(!gh)) | |
443 | goto out; | |
444 | } | |
445 | ||
446 | if (gh->ver != GENEVE_VER || gh->oam) | |
447 | goto out; | |
448 | gh_len = geneve_hlen(gh); | |
449 | ||
450 | hlen = off_gnv + gh_len; | |
451 | if (skb_gro_header_hard(skb, hlen)) { | |
452 | gh = skb_gro_header_slow(skb, hlen, off_gnv); | |
453 | if (unlikely(!gh)) | |
454 | goto out; | |
455 | } | |
456 | ||
371bd106 PS |
457 | for (p = *head; p; p = p->next) { |
458 | if (!NAPI_GRO_CB(p)->same_flow) | |
459 | continue; | |
460 | ||
461 | gh2 = (struct genevehdr *)(p->data + off_gnv); | |
462 | if (gh->opt_len != gh2->opt_len || | |
463 | memcmp(gh, gh2, gh_len)) { | |
464 | NAPI_GRO_CB(p)->same_flow = 0; | |
465 | continue; | |
466 | } | |
467 | } | |
468 | ||
469 | type = gh->proto_type; | |
470 | ||
471 | rcu_read_lock(); | |
472 | ptype = gro_find_receive_by_type(type); | |
c194cf93 | 473 | if (!ptype) |
371bd106 | 474 | goto out_unlock; |
371bd106 PS |
475 | |
476 | skb_gro_pull(skb, gh_len); | |
477 | skb_gro_postpull_rcsum(skb, gh, gh_len); | |
478 | pp = ptype->callbacks.gro_receive(head, skb); | |
c194cf93 | 479 | flush = 0; |
371bd106 PS |
480 | |
481 | out_unlock: | |
482 | rcu_read_unlock(); | |
483 | out: | |
484 | NAPI_GRO_CB(skb)->flush |= flush; | |
485 | ||
486 | return pp; | |
487 | } | |
488 | ||
4a0090a9 TH |
489 | static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, |
490 | int nhoff) | |
371bd106 PS |
491 | { |
492 | struct genevehdr *gh; | |
493 | struct packet_offload *ptype; | |
494 | __be16 type; | |
495 | int gh_len; | |
496 | int err = -ENOSYS; | |
497 | ||
498 | udp_tunnel_gro_complete(skb, nhoff); | |
499 | ||
500 | gh = (struct genevehdr *)(skb->data + nhoff); | |
501 | gh_len = geneve_hlen(gh); | |
502 | type = gh->proto_type; | |
503 | ||
504 | rcu_read_lock(); | |
505 | ptype = gro_find_complete_by_type(type); | |
506 | if (ptype) | |
507 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); | |
508 | ||
509 | rcu_read_unlock(); | |
510 | return err; | |
511 | } | |
512 | ||
513 | /* Create new listen socket if needed */ | |
514 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, | |
abe492b4 | 515 | bool ipv6, u32 flags) |
371bd106 PS |
516 | { |
517 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
518 | struct geneve_sock *gs; | |
519 | struct socket *sock; | |
520 | struct udp_tunnel_sock_cfg tunnel_cfg; | |
66d47003 | 521 | int h; |
371bd106 PS |
522 | |
523 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); | |
524 | if (!gs) | |
525 | return ERR_PTR(-ENOMEM); | |
526 | ||
abe492b4 | 527 | sock = geneve_create_sock(net, ipv6, port, flags); |
371bd106 PS |
528 | if (IS_ERR(sock)) { |
529 | kfree(gs); | |
530 | return ERR_CAST(sock); | |
531 | } | |
532 | ||
533 | gs->sock = sock; | |
534 | gs->refcnt = 1; | |
66d47003 PS |
535 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
536 | INIT_HLIST_HEAD(&gs->vni_list[h]); | |
371bd106 PS |
537 | |
538 | /* Initialize the geneve udp offloads structure */ | |
371bd106 PS |
539 | geneve_notify_add_rx_port(gs); |
540 | ||
541 | /* Mark socket as an encapsulation socket */ | |
4a0090a9 | 542 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
371bd106 PS |
543 | tunnel_cfg.sk_user_data = gs; |
544 | tunnel_cfg.encap_type = 1; | |
4a0090a9 TH |
545 | tunnel_cfg.gro_receive = geneve_gro_receive; |
546 | tunnel_cfg.gro_complete = geneve_gro_complete; | |
371bd106 PS |
547 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
548 | tunnel_cfg.encap_destroy = NULL; | |
549 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); | |
371bd106 PS |
550 | list_add(&gs->list, &gn->sock_list); |
551 | return gs; | |
552 | } | |
553 | ||
554 | static void geneve_notify_del_rx_port(struct geneve_sock *gs) | |
555 | { | |
a8170d2b | 556 | struct net_device *dev; |
371bd106 | 557 | struct sock *sk = gs->sock->sk; |
a8170d2b | 558 | struct net *net = sock_net(sk); |
1e9f12ec | 559 | sa_family_t sa_family = geneve_get_sk_family(gs); |
a8170d2b SA |
560 | __be16 port = inet_sk(sk)->inet_sport; |
561 | ||
562 | rcu_read_lock(); | |
563 | for_each_netdev_rcu(net, dev) { | |
564 | if (dev->netdev_ops->ndo_del_geneve_port) | |
565 | dev->netdev_ops->ndo_del_geneve_port(dev, sa_family, | |
566 | port); | |
567 | } | |
568 | ||
569 | rcu_read_unlock(); | |
371bd106 PS |
570 | } |
571 | ||
8ed66f0e | 572 | static void __geneve_sock_release(struct geneve_sock *gs) |
371bd106 | 573 | { |
8ed66f0e | 574 | if (!gs || --gs->refcnt) |
371bd106 PS |
575 | return; |
576 | ||
577 | list_del(&gs->list); | |
578 | geneve_notify_del_rx_port(gs); | |
579 | udp_tunnel_sock_release(gs->sock); | |
580 | kfree_rcu(gs, rcu); | |
581 | } | |
582 | ||
8ed66f0e JL |
583 | static void geneve_sock_release(struct geneve_dev *geneve) |
584 | { | |
585 | __geneve_sock_release(geneve->sock4); | |
586 | #if IS_ENABLED(CONFIG_IPV6) | |
587 | __geneve_sock_release(geneve->sock6); | |
588 | #endif | |
589 | } | |
590 | ||
371bd106 | 591 | static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, |
8ed66f0e | 592 | sa_family_t family, |
371bd106 PS |
593 | __be16 dst_port) |
594 | { | |
595 | struct geneve_sock *gs; | |
596 | ||
597 | list_for_each_entry(gs, &gn->sock_list, list) { | |
598 | if (inet_sk(gs->sock->sk)->inet_sport == dst_port && | |
1e9f12ec | 599 | geneve_get_sk_family(gs) == family) { |
371bd106 PS |
600 | return gs; |
601 | } | |
602 | } | |
603 | return NULL; | |
604 | } | |
605 | ||
8ed66f0e | 606 | static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) |
2d07dc79 | 607 | { |
2d07dc79 | 608 | struct net *net = geneve->net; |
371bd106 | 609 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
2d07dc79 | 610 | struct geneve_sock *gs; |
66d47003 | 611 | __u32 hash; |
2d07dc79 | 612 | |
8ed66f0e | 613 | gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port); |
371bd106 PS |
614 | if (gs) { |
615 | gs->refcnt++; | |
616 | goto out; | |
617 | } | |
618 | ||
abe492b4 | 619 | gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags); |
2d07dc79 JL |
620 | if (IS_ERR(gs)) |
621 | return PTR_ERR(gs); | |
622 | ||
371bd106 PS |
623 | out: |
624 | gs->collect_md = geneve->collect_md; | |
abe492b4 | 625 | gs->flags = geneve->flags; |
8ed66f0e JL |
626 | #if IS_ENABLED(CONFIG_IPV6) |
627 | if (ipv6) | |
628 | geneve->sock6 = gs; | |
629 | else | |
630 | #endif | |
631 | geneve->sock4 = gs; | |
66d47003 PS |
632 | |
633 | hash = geneve_net_vni_hash(geneve->vni); | |
634 | hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]); | |
2d07dc79 JL |
635 | return 0; |
636 | } | |
637 | ||
8ed66f0e JL |
638 | static int geneve_open(struct net_device *dev) |
639 | { | |
640 | struct geneve_dev *geneve = netdev_priv(dev); | |
641 | bool ipv6 = geneve->remote.sa.sa_family == AF_INET6; | |
642 | bool metadata = geneve->collect_md; | |
643 | int ret = 0; | |
644 | ||
645 | geneve->sock4 = NULL; | |
646 | #if IS_ENABLED(CONFIG_IPV6) | |
647 | geneve->sock6 = NULL; | |
648 | if (ipv6 || metadata) | |
649 | ret = geneve_sock_add(geneve, true); | |
650 | #endif | |
651 | if (!ret && (!ipv6 || metadata)) | |
652 | ret = geneve_sock_add(geneve, false); | |
653 | if (ret < 0) | |
654 | geneve_sock_release(geneve); | |
655 | ||
656 | return ret; | |
657 | } | |
658 | ||
2d07dc79 JL |
659 | static int geneve_stop(struct net_device *dev) |
660 | { | |
661 | struct geneve_dev *geneve = netdev_priv(dev); | |
2d07dc79 | 662 | |
66d47003 PS |
663 | if (!hlist_unhashed(&geneve->hlist)) |
664 | hlist_del_rcu(&geneve->hlist); | |
8ed66f0e | 665 | geneve_sock_release(geneve); |
371bd106 PS |
666 | return 0; |
667 | } | |
668 | ||
8ed66f0e JL |
669 | static void geneve_build_header(struct genevehdr *geneveh, |
670 | __be16 tun_flags, u8 vni[3], | |
671 | u8 options_len, u8 *options) | |
672 | { | |
673 | geneveh->ver = GENEVE_VER; | |
674 | geneveh->opt_len = options_len / 4; | |
675 | geneveh->oam = !!(tun_flags & TUNNEL_OAM); | |
676 | geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT); | |
677 | geneveh->rsvd1 = 0; | |
678 | memcpy(geneveh->vni, vni, 3); | |
679 | geneveh->proto_type = htons(ETH_P_TEB); | |
680 | geneveh->rsvd2 = 0; | |
681 | ||
682 | memcpy(geneveh->options, options, options_len); | |
683 | } | |
684 | ||
371bd106 PS |
685 | static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb, |
686 | __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, | |
abe492b4 | 687 | u32 flags, bool xnet) |
371bd106 PS |
688 | { |
689 | struct genevehdr *gnvh; | |
690 | int min_headroom; | |
691 | int err; | |
14f1f724 | 692 | bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX); |
2d07dc79 | 693 | |
8ed66f0e JL |
694 | skb_scrub_packet(skb, xnet); |
695 | ||
371bd106 PS |
696 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
697 | + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr); | |
698 | err = skb_cow_head(skb, min_headroom); | |
aed069df | 699 | if (unlikely(err)) |
371bd106 | 700 | goto free_rt; |
371bd106 | 701 | |
aed069df AD |
702 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
703 | if (err) | |
371bd106 | 704 | goto free_rt; |
371bd106 PS |
705 | |
706 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); | |
8ed66f0e | 707 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); |
371bd106 PS |
708 | |
709 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); | |
2d07dc79 | 710 | return 0; |
371bd106 PS |
711 | |
712 | free_rt: | |
713 | ip_rt_put(rt); | |
714 | return err; | |
2d07dc79 JL |
715 | } |
716 | ||
8ed66f0e JL |
717 | #if IS_ENABLED(CONFIG_IPV6) |
718 | static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb, | |
719 | __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, | |
abe492b4 | 720 | u32 flags, bool xnet) |
8ed66f0e JL |
721 | { |
722 | struct genevehdr *gnvh; | |
723 | int min_headroom; | |
724 | int err; | |
abe492b4 | 725 | bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX); |
8ed66f0e JL |
726 | |
727 | skb_scrub_packet(skb, xnet); | |
728 | ||
729 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len | |
730 | + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr); | |
731 | err = skb_cow_head(skb, min_headroom); | |
aed069df | 732 | if (unlikely(err)) |
8ed66f0e | 733 | goto free_dst; |
8ed66f0e | 734 | |
aed069df | 735 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
1ba64fac | 736 | if (err) |
8ed66f0e | 737 | goto free_dst; |
8ed66f0e JL |
738 | |
739 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); | |
740 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); | |
741 | ||
742 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); | |
743 | return 0; | |
744 | ||
745 | free_dst: | |
746 | dst_release(dst); | |
747 | return err; | |
748 | } | |
749 | #endif | |
750 | ||
751 | static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, | |
752 | struct net_device *dev, | |
753 | struct flowi4 *fl4, | |
754 | struct ip_tunnel_info *info) | |
e305ac6c | 755 | { |
db3c6139 | 756 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
e305ac6c | 757 | struct geneve_dev *geneve = netdev_priv(dev); |
468dfffc | 758 | struct dst_cache *dst_cache; |
e305ac6c PS |
759 | struct rtable *rt = NULL; |
760 | __u8 tos; | |
761 | ||
762 | memset(fl4, 0, sizeof(*fl4)); | |
763 | fl4->flowi4_mark = skb->mark; | |
764 | fl4->flowi4_proto = IPPROTO_UDP; | |
765 | ||
766 | if (info) { | |
767 | fl4->daddr = info->key.u.ipv4.dst; | |
768 | fl4->saddr = info->key.u.ipv4.src; | |
769 | fl4->flowi4_tos = RT_TOS(info->key.tos); | |
468dfffc | 770 | dst_cache = &info->dst_cache; |
e305ac6c PS |
771 | } else { |
772 | tos = geneve->tos; | |
773 | if (tos == 1) { | |
774 | const struct iphdr *iip = ip_hdr(skb); | |
775 | ||
776 | tos = ip_tunnel_get_dsfield(iip, skb); | |
468dfffc | 777 | use_cache = false; |
e305ac6c PS |
778 | } |
779 | ||
780 | fl4->flowi4_tos = RT_TOS(tos); | |
8ed66f0e | 781 | fl4->daddr = geneve->remote.sin.sin_addr.s_addr; |
468dfffc PA |
782 | dst_cache = &geneve->dst_cache; |
783 | } | |
784 | ||
468dfffc PA |
785 | if (use_cache) { |
786 | rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); | |
787 | if (rt) | |
788 | return rt; | |
e305ac6c PS |
789 | } |
790 | ||
791 | rt = ip_route_output_key(geneve->net, fl4); | |
792 | if (IS_ERR(rt)) { | |
793 | netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); | |
fc4099f1 | 794 | return ERR_PTR(-ENETUNREACH); |
e305ac6c PS |
795 | } |
796 | if (rt->dst.dev == dev) { /* is this necessary? */ | |
797 | netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); | |
e305ac6c | 798 | ip_rt_put(rt); |
fc4099f1 | 799 | return ERR_PTR(-ELOOP); |
e305ac6c | 800 | } |
468dfffc PA |
801 | if (use_cache) |
802 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); | |
e305ac6c PS |
803 | return rt; |
804 | } | |
805 | ||
8ed66f0e JL |
806 | #if IS_ENABLED(CONFIG_IPV6) |
807 | static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, | |
808 | struct net_device *dev, | |
809 | struct flowi6 *fl6, | |
810 | struct ip_tunnel_info *info) | |
811 | { | |
db3c6139 | 812 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
8ed66f0e JL |
813 | struct geneve_dev *geneve = netdev_priv(dev); |
814 | struct geneve_sock *gs6 = geneve->sock6; | |
815 | struct dst_entry *dst = NULL; | |
468dfffc | 816 | struct dst_cache *dst_cache; |
3a56f86f | 817 | __u8 prio; |
8ed66f0e JL |
818 | |
819 | memset(fl6, 0, sizeof(*fl6)); | |
820 | fl6->flowi6_mark = skb->mark; | |
821 | fl6->flowi6_proto = IPPROTO_UDP; | |
822 | ||
823 | if (info) { | |
824 | fl6->daddr = info->key.u.ipv6.dst; | |
825 | fl6->saddr = info->key.u.ipv6.src; | |
95caf6f7 DB |
826 | fl6->flowlabel = ip6_make_flowinfo(RT_TOS(info->key.tos), |
827 | info->key.label); | |
468dfffc | 828 | dst_cache = &info->dst_cache; |
8ed66f0e | 829 | } else { |
3a56f86f JL |
830 | prio = geneve->tos; |
831 | if (prio == 1) { | |
832 | const struct iphdr *iip = ip_hdr(skb); | |
833 | ||
834 | prio = ip_tunnel_get_dsfield(iip, skb); | |
468dfffc | 835 | use_cache = false; |
3a56f86f JL |
836 | } |
837 | ||
95caf6f7 DB |
838 | fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio), |
839 | geneve->label); | |
8ed66f0e | 840 | fl6->daddr = geneve->remote.sin6.sin6_addr; |
468dfffc PA |
841 | dst_cache = &geneve->dst_cache; |
842 | } | |
843 | ||
468dfffc PA |
844 | if (use_cache) { |
845 | dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); | |
846 | if (dst) | |
847 | return dst; | |
8ed66f0e JL |
848 | } |
849 | ||
850 | if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) { | |
851 | netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); | |
852 | return ERR_PTR(-ENETUNREACH); | |
853 | } | |
854 | if (dst->dev == dev) { /* is this necessary? */ | |
855 | netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); | |
856 | dst_release(dst); | |
857 | return ERR_PTR(-ELOOP); | |
858 | } | |
859 | ||
468dfffc PA |
860 | if (use_cache) |
861 | dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); | |
8ed66f0e JL |
862 | return dst; |
863 | } | |
864 | #endif | |
865 | ||
e305ac6c PS |
866 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
867 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) | |
868 | { | |
869 | #ifdef __BIG_ENDIAN | |
870 | vni[0] = (__force __u8)(tun_id >> 16); | |
871 | vni[1] = (__force __u8)(tun_id >> 8); | |
872 | vni[2] = (__force __u8)tun_id; | |
873 | #else | |
874 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); | |
875 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); | |
876 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); | |
877 | #endif | |
878 | } | |
879 | ||
8ed66f0e JL |
880 | static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
881 | struct ip_tunnel_info *info) | |
2d07dc79 JL |
882 | { |
883 | struct geneve_dev *geneve = netdev_priv(dev); | |
8ed66f0e | 884 | struct geneve_sock *gs4 = geneve->sock4; |
2d07dc79 | 885 | struct rtable *rt = NULL; |
08399efc | 886 | const struct iphdr *iip; /* interior IP header */ |
fc4099f1 | 887 | int err = -EINVAL; |
2d07dc79 | 888 | struct flowi4 fl4; |
8760ce58 | 889 | __u8 tos, ttl; |
e305ac6c | 890 | __be16 sport; |
371bd106 | 891 | __be16 df; |
8ed66f0e | 892 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
abe492b4 | 893 | u32 flags = geneve->flags; |
2d07dc79 | 894 | |
e305ac6c | 895 | if (geneve->collect_md) { |
8ed66f0e | 896 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
e305ac6c PS |
897 | netdev_dbg(dev, "no tunnel metadata\n"); |
898 | goto tx_error; | |
899 | } | |
7f9562a1 JB |
900 | if (info && ip_tunnel_info_af(info) != AF_INET) |
901 | goto tx_error; | |
e305ac6c | 902 | } |
980c394c | 903 | |
8ed66f0e | 904 | rt = geneve_get_v4_rt(skb, dev, &fl4, info); |
2d07dc79 | 905 | if (IS_ERR(rt)) { |
fc4099f1 | 906 | err = PTR_ERR(rt); |
2d07dc79 JL |
907 | goto tx_error; |
908 | } | |
371bd106 PS |
909 | |
910 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); | |
e305ac6c | 911 | skb_reset_mac_header(skb); |
e305ac6c | 912 | |
08399efc JL |
913 | iip = ip_hdr(skb); |
914 | ||
e305ac6c PS |
915 | if (info) { |
916 | const struct ip_tunnel_key *key = &info->key; | |
e305ac6c PS |
917 | u8 *opts = NULL; |
918 | u8 vni[3]; | |
e305ac6c PS |
919 | |
920 | tunnel_id_to_vni(key->tun_id, vni); | |
14ca0751 | 921 | if (info->options_len) |
4c222798 | 922 | opts = ip_tunnel_info_opts(info); |
e305ac6c | 923 | |
abe492b4 | 924 | if (key->tun_flags & TUNNEL_CSUM) |
14f1f724 | 925 | flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX; |
abe492b4 | 926 | else |
14f1f724 | 927 | flags |= GENEVE_F_UDP_ZERO_CSUM_TX; |
abe492b4 | 928 | |
371bd106 | 929 | err = geneve_build_skb(rt, skb, key->tun_flags, vni, |
abe492b4 | 930 | info->options_len, opts, flags, xnet); |
371bd106 | 931 | if (unlikely(err)) |
aed069df | 932 | goto tx_error; |
371bd106 | 933 | |
08399efc | 934 | tos = ip_tunnel_ecn_encap(key->tos, iip, skb); |
371bd106 PS |
935 | ttl = key->ttl; |
936 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
e305ac6c | 937 | } else { |
371bd106 | 938 | err = geneve_build_skb(rt, skb, 0, geneve->vni, |
abe492b4 | 939 | 0, NULL, flags, xnet); |
371bd106 | 940 | if (unlikely(err)) |
aed069df | 941 | goto tx_error; |
371bd106 | 942 | |
e305ac6c | 943 | tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb); |
e305ac6c PS |
944 | ttl = geneve->ttl; |
945 | if (!ttl && IN_MULTICAST(ntohl(fl4.daddr))) | |
946 | ttl = 1; | |
e305ac6c | 947 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
371bd106 | 948 | df = 0; |
2d07dc79 | 949 | } |
039f5062 PS |
950 | udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, |
951 | tos, ttl, df, sport, geneve->dst_port, | |
952 | !net_eq(geneve->net, dev_net(geneve->dev)), | |
14f1f724 | 953 | !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX)); |
2d07dc79 | 954 | |
2d07dc79 JL |
955 | return NETDEV_TX_OK; |
956 | ||
2d07dc79 | 957 | tx_error: |
2d07dc79 | 958 | dev_kfree_skb(skb); |
aed069df | 959 | |
fc4099f1 PS |
960 | if (err == -ELOOP) |
961 | dev->stats.collisions++; | |
962 | else if (err == -ENETUNREACH) | |
963 | dev->stats.tx_carrier_errors++; | |
964 | else | |
965 | dev->stats.tx_errors++; | |
2d07dc79 JL |
966 | return NETDEV_TX_OK; |
967 | } | |
968 | ||
8ed66f0e JL |
969 | #if IS_ENABLED(CONFIG_IPV6) |
970 | static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, | |
971 | struct ip_tunnel_info *info) | |
972 | { | |
973 | struct geneve_dev *geneve = netdev_priv(dev); | |
974 | struct geneve_sock *gs6 = geneve->sock6; | |
975 | struct dst_entry *dst = NULL; | |
3a56f86f | 976 | const struct iphdr *iip; /* interior IP header */ |
8ed66f0e JL |
977 | int err = -EINVAL; |
978 | struct flowi6 fl6; | |
3a56f86f | 979 | __u8 prio, ttl; |
8ed66f0e | 980 | __be16 sport; |
8eb3b995 | 981 | __be32 label; |
8ed66f0e | 982 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
abe492b4 | 983 | u32 flags = geneve->flags; |
8ed66f0e JL |
984 | |
985 | if (geneve->collect_md) { | |
986 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { | |
987 | netdev_dbg(dev, "no tunnel metadata\n"); | |
988 | goto tx_error; | |
989 | } | |
990 | } | |
991 | ||
992 | dst = geneve_get_v6_dst(skb, dev, &fl6, info); | |
993 | if (IS_ERR(dst)) { | |
994 | err = PTR_ERR(dst); | |
995 | goto tx_error; | |
996 | } | |
997 | ||
998 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); | |
999 | skb_reset_mac_header(skb); | |
1000 | ||
3a56f86f JL |
1001 | iip = ip_hdr(skb); |
1002 | ||
8ed66f0e JL |
1003 | if (info) { |
1004 | const struct ip_tunnel_key *key = &info->key; | |
1005 | u8 *opts = NULL; | |
1006 | u8 vni[3]; | |
1007 | ||
1008 | tunnel_id_to_vni(key->tun_id, vni); | |
14ca0751 | 1009 | if (info->options_len) |
8ed66f0e JL |
1010 | opts = ip_tunnel_info_opts(info); |
1011 | ||
abe492b4 | 1012 | if (key->tun_flags & TUNNEL_CSUM) |
35e2d115 | 1013 | flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX; |
abe492b4 | 1014 | else |
35e2d115 | 1015 | flags |= GENEVE_F_UDP_ZERO_CSUM6_TX; |
abe492b4 | 1016 | |
8ed66f0e JL |
1017 | err = geneve6_build_skb(dst, skb, key->tun_flags, vni, |
1018 | info->options_len, opts, | |
abe492b4 | 1019 | flags, xnet); |
8ed66f0e | 1020 | if (unlikely(err)) |
aed069df | 1021 | goto tx_error; |
8ed66f0e | 1022 | |
3a56f86f | 1023 | prio = ip_tunnel_ecn_encap(key->tos, iip, skb); |
8ed66f0e | 1024 | ttl = key->ttl; |
8eb3b995 | 1025 | label = info->key.label; |
8ed66f0e | 1026 | } else { |
8ed66f0e | 1027 | err = geneve6_build_skb(dst, skb, 0, geneve->vni, |
abe492b4 | 1028 | 0, NULL, flags, xnet); |
8ed66f0e | 1029 | if (unlikely(err)) |
aed069df | 1030 | goto tx_error; |
8ed66f0e | 1031 | |
95caf6f7 DB |
1032 | prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel), |
1033 | iip, skb); | |
8ed66f0e JL |
1034 | ttl = geneve->ttl; |
1035 | if (!ttl && ipv6_addr_is_multicast(&fl6.daddr)) | |
1036 | ttl = 1; | |
1037 | ttl = ttl ? : ip6_dst_hoplimit(dst); | |
8eb3b995 | 1038 | label = geneve->label; |
8ed66f0e | 1039 | } |
8eb3b995 | 1040 | |
039f5062 | 1041 | udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, |
8eb3b995 | 1042 | &fl6.saddr, &fl6.daddr, prio, ttl, label, |
039f5062 PS |
1043 | sport, geneve->dst_port, |
1044 | !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX)); | |
8ed66f0e JL |
1045 | return NETDEV_TX_OK; |
1046 | ||
1047 | tx_error: | |
1048 | dev_kfree_skb(skb); | |
aed069df | 1049 | |
8ed66f0e JL |
1050 | if (err == -ELOOP) |
1051 | dev->stats.collisions++; | |
1052 | else if (err == -ENETUNREACH) | |
1053 | dev->stats.tx_carrier_errors++; | |
1054 | else | |
1055 | dev->stats.tx_errors++; | |
1056 | return NETDEV_TX_OK; | |
1057 | } | |
1058 | #endif | |
1059 | ||
1060 | static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) | |
1061 | { | |
1062 | struct geneve_dev *geneve = netdev_priv(dev); | |
1063 | struct ip_tunnel_info *info = NULL; | |
1064 | ||
1065 | if (geneve->collect_md) | |
1066 | info = skb_tunnel_info(skb); | |
1067 | ||
1068 | #if IS_ENABLED(CONFIG_IPV6) | |
1069 | if ((info && ip_tunnel_info_af(info) == AF_INET6) || | |
1070 | (!info && geneve->remote.sa.sa_family == AF_INET6)) | |
1071 | return geneve6_xmit_skb(skb, dev, info); | |
1072 | #endif | |
1073 | return geneve_xmit_skb(skb, dev, info); | |
1074 | } | |
1075 | ||
aeee0e66 | 1076 | static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict) |
55e5bfb5 | 1077 | { |
aeee0e66 DW |
1078 | /* The max_mtu calculation does not take account of GENEVE |
1079 | * options, to avoid excluding potentially valid | |
1080 | * configurations. | |
55e5bfb5 | 1081 | */ |
aeee0e66 DW |
1082 | int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - sizeof(struct iphdr) |
1083 | - dev->hard_header_len; | |
1084 | ||
1085 | if (new_mtu < 68) | |
55e5bfb5 | 1086 | return -EINVAL; |
aeee0e66 DW |
1087 | |
1088 | if (new_mtu > max_mtu) { | |
1089 | if (strict) | |
1090 | return -EINVAL; | |
1091 | ||
1092 | new_mtu = max_mtu; | |
1093 | } | |
1094 | ||
55e5bfb5 DW |
1095 | dev->mtu = new_mtu; |
1096 | return 0; | |
1097 | } | |
1098 | ||
aeee0e66 DW |
1099 | static int geneve_change_mtu(struct net_device *dev, int new_mtu) |
1100 | { | |
1101 | return __geneve_change_mtu(dev, new_mtu, true); | |
1102 | } | |
1103 | ||
fc4099f1 PS |
1104 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
1105 | { | |
1106 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
1107 | struct geneve_dev *geneve = netdev_priv(dev); | |
1108 | struct rtable *rt; | |
1109 | struct flowi4 fl4; | |
b8812fa8 JL |
1110 | #if IS_ENABLED(CONFIG_IPV6) |
1111 | struct dst_entry *dst; | |
1112 | struct flowi6 fl6; | |
1113 | #endif | |
fc4099f1 | 1114 | |
b8812fa8 JL |
1115 | if (ip_tunnel_info_af(info) == AF_INET) { |
1116 | rt = geneve_get_v4_rt(skb, dev, &fl4, info); | |
1117 | if (IS_ERR(rt)) | |
1118 | return PTR_ERR(rt); | |
fc4099f1 | 1119 | |
b8812fa8 JL |
1120 | ip_rt_put(rt); |
1121 | info->key.u.ipv4.src = fl4.saddr; | |
1122 | #if IS_ENABLED(CONFIG_IPV6) | |
1123 | } else if (ip_tunnel_info_af(info) == AF_INET6) { | |
1124 | dst = geneve_get_v6_dst(skb, dev, &fl6, info); | |
1125 | if (IS_ERR(dst)) | |
1126 | return PTR_ERR(dst); | |
1127 | ||
1128 | dst_release(dst); | |
1129 | info->key.u.ipv6.src = fl6.saddr; | |
1130 | #endif | |
1131 | } else { | |
1132 | return -EINVAL; | |
1133 | } | |
fc4099f1 | 1134 | |
fc4099f1 PS |
1135 | info->key.tp_src = udp_flow_src_port(geneve->net, skb, |
1136 | 1, USHRT_MAX, true); | |
1137 | info->key.tp_dst = geneve->dst_port; | |
1138 | return 0; | |
1139 | } | |
1140 | ||
2d07dc79 JL |
1141 | static const struct net_device_ops geneve_netdev_ops = { |
1142 | .ndo_init = geneve_init, | |
1143 | .ndo_uninit = geneve_uninit, | |
1144 | .ndo_open = geneve_open, | |
1145 | .ndo_stop = geneve_stop, | |
1146 | .ndo_start_xmit = geneve_xmit, | |
1147 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
55e5bfb5 | 1148 | .ndo_change_mtu = geneve_change_mtu, |
2d07dc79 JL |
1149 | .ndo_validate_addr = eth_validate_addr, |
1150 | .ndo_set_mac_address = eth_mac_addr, | |
fc4099f1 | 1151 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
2d07dc79 JL |
1152 | }; |
1153 | ||
1154 | static void geneve_get_drvinfo(struct net_device *dev, | |
1155 | struct ethtool_drvinfo *drvinfo) | |
1156 | { | |
1157 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); | |
1158 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); | |
1159 | } | |
1160 | ||
1161 | static const struct ethtool_ops geneve_ethtool_ops = { | |
1162 | .get_drvinfo = geneve_get_drvinfo, | |
1163 | .get_link = ethtool_op_get_link, | |
1164 | }; | |
1165 | ||
1166 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
1167 | static struct device_type geneve_type = { | |
1168 | .name = "geneve", | |
1169 | }; | |
1170 | ||
05ca4029 SA |
1171 | /* Calls the ndo_add_geneve_port of the caller in order to |
1172 | * supply the listening GENEVE udp ports. Callers are expected | |
1173 | * to implement the ndo_add_geneve_port. | |
1174 | */ | |
681e683f | 1175 | static void geneve_push_rx_ports(struct net_device *dev) |
05ca4029 SA |
1176 | { |
1177 | struct net *net = dev_net(dev); | |
1178 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
1179 | struct geneve_sock *gs; | |
1180 | sa_family_t sa_family; | |
1181 | struct sock *sk; | |
1182 | __be16 port; | |
1183 | ||
681e683f HFS |
1184 | if (!dev->netdev_ops->ndo_add_geneve_port) |
1185 | return; | |
1186 | ||
05ca4029 SA |
1187 | rcu_read_lock(); |
1188 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { | |
1189 | sk = gs->sock->sk; | |
1190 | sa_family = sk->sk_family; | |
1191 | port = inet_sk(sk)->inet_sport; | |
1192 | dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port); | |
1193 | } | |
1194 | rcu_read_unlock(); | |
1195 | } | |
05ca4029 | 1196 | |
2d07dc79 JL |
1197 | /* Initialize the device structure. */ |
1198 | static void geneve_setup(struct net_device *dev) | |
1199 | { | |
1200 | ether_setup(dev); | |
1201 | ||
1202 | dev->netdev_ops = &geneve_netdev_ops; | |
1203 | dev->ethtool_ops = &geneve_ethtool_ops; | |
1204 | dev->destructor = free_netdev; | |
1205 | ||
1206 | SET_NETDEV_DEVTYPE(dev, &geneve_type); | |
1207 | ||
2d07dc79 JL |
1208 | dev->features |= NETIF_F_LLTX; |
1209 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; | |
1210 | dev->features |= NETIF_F_RXCSUM; | |
1211 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1212 | ||
2d07dc79 JL |
1213 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
1214 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
2d07dc79 JL |
1215 | |
1216 | netif_keep_dst(dev); | |
fc41cdb3 | 1217 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
ed961ac2 | 1218 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
87cd3dca | 1219 | eth_hw_addr_random(dev); |
2d07dc79 JL |
1220 | } |
1221 | ||
1222 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { | |
1223 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, | |
1224 | [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
8ed66f0e | 1225 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
8760ce58 | 1226 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
d8951125 | 1227 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
8eb3b995 | 1228 | [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, |
cd7918b3 | 1229 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
e305ac6c | 1230 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
abe492b4 TH |
1231 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
1232 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
1233 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
2d07dc79 JL |
1234 | }; |
1235 | ||
1236 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1237 | { | |
1238 | if (tb[IFLA_ADDRESS]) { | |
1239 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1240 | return -EINVAL; | |
1241 | ||
1242 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1243 | return -EADDRNOTAVAIL; | |
1244 | } | |
1245 | ||
1246 | if (!data) | |
1247 | return -EINVAL; | |
1248 | ||
1249 | if (data[IFLA_GENEVE_ID]) { | |
1250 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); | |
1251 | ||
1252 | if (vni >= GENEVE_VID_MASK) | |
1253 | return -ERANGE; | |
1254 | } | |
1255 | ||
1256 | return 0; | |
1257 | } | |
1258 | ||
371bd106 PS |
1259 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
1260 | __be16 dst_port, | |
8ed66f0e | 1261 | union geneve_addr *remote, |
371bd106 PS |
1262 | u8 vni[], |
1263 | bool *tun_on_same_port, | |
1264 | bool *tun_collect_md) | |
1265 | { | |
1266 | struct geneve_dev *geneve, *t; | |
1267 | ||
1268 | *tun_on_same_port = false; | |
1269 | *tun_collect_md = false; | |
1270 | t = NULL; | |
1271 | list_for_each_entry(geneve, &gn->geneve_list, next) { | |
1272 | if (geneve->dst_port == dst_port) { | |
1273 | *tun_collect_md = geneve->collect_md; | |
1274 | *tun_on_same_port = true; | |
1275 | } | |
1276 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && | |
8ed66f0e | 1277 | !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) && |
371bd106 PS |
1278 | dst_port == geneve->dst_port) |
1279 | t = geneve; | |
1280 | } | |
1281 | return t; | |
1282 | } | |
1283 | ||
e305ac6c | 1284 | static int geneve_configure(struct net *net, struct net_device *dev, |
8ed66f0e | 1285 | union geneve_addr *remote, |
8eb3b995 DB |
1286 | __u32 vni, __u8 ttl, __u8 tos, __be32 label, |
1287 | __be16 dst_port, bool metadata, u32 flags) | |
2d07dc79 JL |
1288 | { |
1289 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
371bd106 PS |
1290 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
1291 | bool tun_collect_md, tun_on_same_port; | |
184fc8b5 | 1292 | int err, encap_len; |
2d07dc79 | 1293 | |
8ed66f0e JL |
1294 | if (!remote) |
1295 | return -EINVAL; | |
1296 | if (metadata && | |
8eb3b995 | 1297 | (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl || label)) |
8ed66f0e | 1298 | return -EINVAL; |
2d07dc79 JL |
1299 | |
1300 | geneve->net = net; | |
1301 | geneve->dev = dev; | |
1302 | ||
2d07dc79 JL |
1303 | geneve->vni[0] = (vni & 0x00ff0000) >> 16; |
1304 | geneve->vni[1] = (vni & 0x0000ff00) >> 8; | |
1305 | geneve->vni[2] = vni & 0x000000ff; | |
1306 | ||
8ed66f0e JL |
1307 | if ((remote->sa.sa_family == AF_INET && |
1308 | IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) || | |
1309 | (remote->sa.sa_family == AF_INET6 && | |
1310 | ipv6_addr_is_multicast(&remote->sin6.sin6_addr))) | |
2d07dc79 | 1311 | return -EINVAL; |
8eb3b995 DB |
1312 | if (label && remote->sa.sa_family != AF_INET6) |
1313 | return -EINVAL; | |
1314 | ||
8ed66f0e | 1315 | geneve->remote = *remote; |
2d07dc79 | 1316 | |
e305ac6c PS |
1317 | geneve->ttl = ttl; |
1318 | geneve->tos = tos; | |
8eb3b995 | 1319 | geneve->label = label; |
7bbe33ff | 1320 | geneve->dst_port = dst_port; |
e305ac6c | 1321 | geneve->collect_md = metadata; |
abe492b4 | 1322 | geneve->flags = flags; |
e305ac6c | 1323 | |
8ed66f0e | 1324 | t = geneve_find_dev(gn, dst_port, remote, geneve->vni, |
371bd106 PS |
1325 | &tun_on_same_port, &tun_collect_md); |
1326 | if (t) | |
1327 | return -EBUSY; | |
1328 | ||
184fc8b5 PA |
1329 | /* make enough headroom for basic scenario */ |
1330 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; | |
1331 | if (remote->sa.sa_family == AF_INET) | |
1332 | encap_len += sizeof(struct iphdr); | |
1333 | else | |
1334 | encap_len += sizeof(struct ipv6hdr); | |
1335 | dev->needed_headroom = encap_len + ETH_HLEN; | |
1336 | ||
371bd106 PS |
1337 | if (metadata) { |
1338 | if (tun_on_same_port) | |
1339 | return -EPERM; | |
1340 | } else { | |
1341 | if (tun_collect_md) | |
1342 | return -EPERM; | |
1343 | } | |
1344 | ||
468dfffc PA |
1345 | dst_cache_reset(&geneve->dst_cache); |
1346 | ||
2d07dc79 JL |
1347 | err = register_netdevice(dev); |
1348 | if (err) | |
1349 | return err; | |
1350 | ||
e305ac6c | 1351 | list_add(&geneve->next, &gn->geneve_list); |
e305ac6c PS |
1352 | return 0; |
1353 | } | |
1354 | ||
1355 | static int geneve_newlink(struct net *net, struct net_device *dev, | |
1356 | struct nlattr *tb[], struct nlattr *data[]) | |
1357 | { | |
7bbe33ff | 1358 | __be16 dst_port = htons(GENEVE_UDP_PORT); |
e305ac6c PS |
1359 | __u8 ttl = 0, tos = 0; |
1360 | bool metadata = false; | |
8ed66f0e | 1361 | union geneve_addr remote = geneve_remote_unspec; |
8eb3b995 | 1362 | __be32 label = 0; |
e277de5f | 1363 | __u32 vni = 0; |
abe492b4 | 1364 | u32 flags = 0; |
e305ac6c | 1365 | |
8ed66f0e JL |
1366 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) |
1367 | return -EINVAL; | |
1368 | ||
1369 | if (data[IFLA_GENEVE_REMOTE]) { | |
1370 | remote.sa.sa_family = AF_INET; | |
1371 | remote.sin.sin_addr.s_addr = | |
1372 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); | |
1373 | } | |
1374 | ||
1375 | if (data[IFLA_GENEVE_REMOTE6]) { | |
1376 | if (!IS_ENABLED(CONFIG_IPV6)) | |
1377 | return -EPFNOSUPPORT; | |
1378 | ||
1379 | remote.sa.sa_family = AF_INET6; | |
1380 | remote.sin6.sin6_addr = | |
1381 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); | |
1382 | ||
1383 | if (ipv6_addr_type(&remote.sin6.sin6_addr) & | |
1384 | IPV6_ADDR_LINKLOCAL) { | |
1385 | netdev_dbg(dev, "link-local remote is unsupported\n"); | |
1386 | return -EINVAL; | |
1387 | } | |
1388 | } | |
1389 | ||
e277de5f JG |
1390 | if (data[IFLA_GENEVE_ID]) |
1391 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); | |
e305ac6c | 1392 | |
8760ce58 | 1393 | if (data[IFLA_GENEVE_TTL]) |
e305ac6c | 1394 | ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
8760ce58 | 1395 | |
d8951125 | 1396 | if (data[IFLA_GENEVE_TOS]) |
e305ac6c | 1397 | tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
d8951125 | 1398 | |
8eb3b995 DB |
1399 | if (data[IFLA_GENEVE_LABEL]) |
1400 | label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & | |
1401 | IPV6_FLOWLABEL_MASK; | |
1402 | ||
e305ac6c | 1403 | if (data[IFLA_GENEVE_PORT]) |
7bbe33ff | 1404 | dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]); |
2d07dc79 | 1405 | |
e305ac6c PS |
1406 | if (data[IFLA_GENEVE_COLLECT_METADATA]) |
1407 | metadata = true; | |
2d07dc79 | 1408 | |
abe492b4 | 1409 | if (data[IFLA_GENEVE_UDP_CSUM] && |
14f1f724 AD |
1410 | !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
1411 | flags |= GENEVE_F_UDP_ZERO_CSUM_TX; | |
abe492b4 TH |
1412 | |
1413 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] && | |
1414 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) | |
1415 | flags |= GENEVE_F_UDP_ZERO_CSUM6_TX; | |
1416 | ||
1417 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] && | |
1418 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) | |
1419 | flags |= GENEVE_F_UDP_ZERO_CSUM6_RX; | |
1420 | ||
8eb3b995 DB |
1421 | return geneve_configure(net, dev, &remote, vni, ttl, tos, label, |
1422 | dst_port, metadata, flags); | |
2d07dc79 JL |
1423 | } |
1424 | ||
1425 | static void geneve_dellink(struct net_device *dev, struct list_head *head) | |
1426 | { | |
1427 | struct geneve_dev *geneve = netdev_priv(dev); | |
1428 | ||
2d07dc79 JL |
1429 | list_del(&geneve->next); |
1430 | unregister_netdevice_queue(dev, head); | |
1431 | } | |
1432 | ||
1433 | static size_t geneve_get_size(const struct net_device *dev) | |
1434 | { | |
1435 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ | |
8ed66f0e | 1436 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
8760ce58 | 1437 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
d8951125 | 1438 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
8eb3b995 | 1439 | nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ |
7bbe33ff | 1440 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
e305ac6c | 1441 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
abe492b4 TH |
1442 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
1443 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ | |
1444 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ | |
2d07dc79 JL |
1445 | 0; |
1446 | } | |
1447 | ||
1448 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1449 | { | |
1450 | struct geneve_dev *geneve = netdev_priv(dev); | |
1451 | __u32 vni; | |
1452 | ||
1453 | vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2]; | |
1454 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) | |
1455 | goto nla_put_failure; | |
1456 | ||
8ed66f0e JL |
1457 | if (geneve->remote.sa.sa_family == AF_INET) { |
1458 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, | |
1459 | geneve->remote.sin.sin_addr.s_addr)) | |
1460 | goto nla_put_failure; | |
1461 | #if IS_ENABLED(CONFIG_IPV6) | |
1462 | } else { | |
1463 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, | |
1464 | &geneve->remote.sin6.sin6_addr)) | |
1465 | goto nla_put_failure; | |
1466 | #endif | |
1467 | } | |
2d07dc79 | 1468 | |
d8951125 | 1469 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) || |
8eb3b995 DB |
1470 | nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos) || |
1471 | nla_put_be32(skb, IFLA_GENEVE_LABEL, geneve->label)) | |
8760ce58 JL |
1472 | goto nla_put_failure; |
1473 | ||
7bbe33ff | 1474 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port)) |
cd7918b3 PS |
1475 | goto nla_put_failure; |
1476 | ||
e305ac6c PS |
1477 | if (geneve->collect_md) { |
1478 | if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) | |
1479 | goto nla_put_failure; | |
1480 | } | |
1481 | ||
abe492b4 | 1482 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
14f1f724 | 1483 | !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) || |
abe492b4 TH |
1484 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
1485 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) || | |
1486 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, | |
1487 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX))) | |
1488 | goto nla_put_failure; | |
1489 | ||
2d07dc79 JL |
1490 | return 0; |
1491 | ||
1492 | nla_put_failure: | |
1493 | return -EMSGSIZE; | |
1494 | } | |
1495 | ||
1496 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { | |
1497 | .kind = "geneve", | |
1498 | .maxtype = IFLA_GENEVE_MAX, | |
1499 | .policy = geneve_policy, | |
1500 | .priv_size = sizeof(struct geneve_dev), | |
1501 | .setup = geneve_setup, | |
1502 | .validate = geneve_validate, | |
1503 | .newlink = geneve_newlink, | |
1504 | .dellink = geneve_dellink, | |
1505 | .get_size = geneve_get_size, | |
1506 | .fill_info = geneve_fill_info, | |
1507 | }; | |
1508 | ||
e305ac6c PS |
1509 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
1510 | u8 name_assign_type, u16 dst_port) | |
1511 | { | |
1512 | struct nlattr *tb[IFLA_MAX + 1]; | |
1513 | struct net_device *dev; | |
1514 | int err; | |
1515 | ||
1516 | memset(tb, 0, sizeof(tb)); | |
1517 | dev = rtnl_create_link(net, name, name_assign_type, | |
1518 | &geneve_link_ops, tb); | |
1519 | if (IS_ERR(dev)) | |
1520 | return dev; | |
1521 | ||
8ed66f0e | 1522 | err = geneve_configure(net, dev, &geneve_remote_unspec, |
8eb3b995 | 1523 | 0, 0, 0, 0, htons(dst_port), true, |
c868ee70 | 1524 | GENEVE_F_UDP_ZERO_CSUM6_RX); |
7e059158 DW |
1525 | if (err) |
1526 | goto err; | |
1527 | ||
1528 | /* openvswitch users expect packet sizes to be unrestricted, | |
1529 | * so set the largest MTU we can. | |
1530 | */ | |
aeee0e66 | 1531 | err = __geneve_change_mtu(dev, IP_MAX_MTU, false); |
7e059158 DW |
1532 | if (err) |
1533 | goto err; | |
1534 | ||
e305ac6c | 1535 | return dev; |
7e059158 DW |
1536 | |
1537 | err: | |
1538 | free_netdev(dev); | |
1539 | return ERR_PTR(err); | |
e305ac6c PS |
1540 | } |
1541 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); | |
1542 | ||
681e683f HFS |
1543 | static int geneve_netdevice_event(struct notifier_block *unused, |
1544 | unsigned long event, void *ptr) | |
1545 | { | |
1546 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
1547 | ||
1548 | if (event == NETDEV_OFFLOAD_PUSH_GENEVE) | |
1549 | geneve_push_rx_ports(dev); | |
1550 | ||
1551 | return NOTIFY_DONE; | |
1552 | } | |
1553 | ||
1554 | static struct notifier_block geneve_notifier_block __read_mostly = { | |
1555 | .notifier_call = geneve_netdevice_event, | |
1556 | }; | |
1557 | ||
2d07dc79 JL |
1558 | static __net_init int geneve_init_net(struct net *net) |
1559 | { | |
1560 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
2d07dc79 JL |
1561 | |
1562 | INIT_LIST_HEAD(&gn->geneve_list); | |
371bd106 | 1563 | INIT_LIST_HEAD(&gn->sock_list); |
2d07dc79 JL |
1564 | return 0; |
1565 | } | |
1566 | ||
1567 | static void __net_exit geneve_exit_net(struct net *net) | |
1568 | { | |
1569 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
1570 | struct geneve_dev *geneve, *next; | |
1571 | struct net_device *dev, *aux; | |
1572 | LIST_HEAD(list); | |
1573 | ||
1574 | rtnl_lock(); | |
1575 | ||
1576 | /* gather any geneve devices that were moved into this ns */ | |
1577 | for_each_netdev_safe(net, dev, aux) | |
1578 | if (dev->rtnl_link_ops == &geneve_link_ops) | |
1579 | unregister_netdevice_queue(dev, &list); | |
1580 | ||
1581 | /* now gather any other geneve devices that were created in this ns */ | |
1582 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { | |
1583 | /* If geneve->dev is in the same netns, it was already added | |
1584 | * to the list by the previous loop. | |
1585 | */ | |
1586 | if (!net_eq(dev_net(geneve->dev), net)) | |
1587 | unregister_netdevice_queue(geneve->dev, &list); | |
1588 | } | |
1589 | ||
1590 | /* unregister the devices gathered above */ | |
1591 | unregister_netdevice_many(&list); | |
1592 | rtnl_unlock(); | |
1593 | } | |
1594 | ||
1595 | static struct pernet_operations geneve_net_ops = { | |
1596 | .init = geneve_init_net, | |
1597 | .exit = geneve_exit_net, | |
1598 | .id = &geneve_net_id, | |
1599 | .size = sizeof(struct geneve_net), | |
1600 | }; | |
1601 | ||
1602 | static int __init geneve_init_module(void) | |
1603 | { | |
1604 | int rc; | |
1605 | ||
1606 | rc = register_pernet_subsys(&geneve_net_ops); | |
1607 | if (rc) | |
1608 | goto out1; | |
1609 | ||
681e683f | 1610 | rc = register_netdevice_notifier(&geneve_notifier_block); |
2d07dc79 JL |
1611 | if (rc) |
1612 | goto out2; | |
1613 | ||
681e683f HFS |
1614 | rc = rtnl_link_register(&geneve_link_ops); |
1615 | if (rc) | |
1616 | goto out3; | |
1617 | ||
2d07dc79 | 1618 | return 0; |
681e683f HFS |
1619 | |
1620 | out3: | |
1621 | unregister_netdevice_notifier(&geneve_notifier_block); | |
2d07dc79 JL |
1622 | out2: |
1623 | unregister_pernet_subsys(&geneve_net_ops); | |
1624 | out1: | |
1625 | return rc; | |
1626 | } | |
1627 | late_initcall(geneve_init_module); | |
1628 | ||
1629 | static void __exit geneve_cleanup_module(void) | |
1630 | { | |
1631 | rtnl_link_unregister(&geneve_link_ops); | |
681e683f | 1632 | unregister_netdevice_notifier(&geneve_notifier_block); |
2d07dc79 JL |
1633 | unregister_pernet_subsys(&geneve_net_ops); |
1634 | } | |
1635 | module_exit(geneve_cleanup_module); | |
1636 | ||
1637 | MODULE_LICENSE("GPL"); | |
1638 | MODULE_VERSION(GENEVE_NETDEV_VER); | |
1639 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); | |
1640 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); | |
1641 | MODULE_ALIAS_RTNL_LINK("geneve"); |