Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
2d07dc79 JL |
2 | /* |
3 | * GENEVE: Generic Network Virtualization Encapsulation | |
4 | * | |
5 | * Copyright (c) 2015 Red Hat, Inc. | |
2d07dc79 JL |
6 | */ |
7 | ||
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
9 | ||
cc69837f | 10 | #include <linux/ethtool.h> |
2d07dc79 JL |
11 | #include <linux/kernel.h> |
12 | #include <linux/module.h> | |
2d07dc79 JL |
13 | #include <linux/etherdevice.h> |
14 | #include <linux/hash.h> | |
3616d08b | 15 | #include <net/ipv6_stubs.h> |
e305ac6c | 16 | #include <net/dst_metadata.h> |
8e816df8 | 17 | #include <net/gro_cells.h> |
2d07dc79 JL |
18 | #include <net/rtnetlink.h> |
19 | #include <net/geneve.h> | |
4721031c | 20 | #include <net/gro.h> |
371bd106 | 21 | #include <net/protocol.h> |
2d07dc79 JL |
22 | |
23 | #define GENEVE_NETDEV_VER "0.6" | |
24 | ||
2d07dc79 JL |
25 | #define GENEVE_N_VID (1u << 24) |
26 | #define GENEVE_VID_MASK (GENEVE_N_VID - 1) | |
27 | ||
28 | #define VNI_HASH_BITS 10 | |
29 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) | |
30 | ||
31 | static bool log_ecn_error = true; | |
32 | module_param(log_ecn_error, bool, 0644); | |
33 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
34 | ||
371bd106 PS |
35 | #define GENEVE_VER 0 |
36 | #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) | |
5edbea69 AK |
37 | #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN) |
38 | #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN) | |
371bd106 | 39 | |
2d07dc79 JL |
40 | /* per-network namespace private data for this module */ |
41 | struct geneve_net { | |
371bd106 | 42 | struct list_head geneve_list; |
371bd106 | 43 | struct list_head sock_list; |
2d07dc79 JL |
44 | }; |
45 | ||
c7d03a00 | 46 | static unsigned int geneve_net_id; |
371bd106 | 47 | |
4b4c21fa JB |
48 | struct geneve_dev_node { |
49 | struct hlist_node hlist; | |
50 | struct geneve_dev *geneve; | |
51 | }; | |
52 | ||
9e06e859 SD |
53 | struct geneve_config { |
54 | struct ip_tunnel_info info; | |
55 | bool collect_md; | |
56 | bool use_udp6_rx_checksums; | |
57 | bool ttl_inherit; | |
58 | enum ifla_geneve_df df; | |
435fe1c0 | 59 | bool inner_proto_inherit; |
9e06e859 SD |
60 | }; |
61 | ||
2d07dc79 JL |
62 | /* Pseudo network device */ |
63 | struct geneve_dev { | |
4b4c21fa JB |
64 | struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */ |
65 | #if IS_ENABLED(CONFIG_IPV6) | |
66 | struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */ | |
67 | #endif | |
2d07dc79 JL |
68 | struct net *net; /* netns for packet i/o */ |
69 | struct net_device *dev; /* netdev for geneve tunnel */ | |
fceb9c3e | 70 | struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */ |
8ed66f0e | 71 | #if IS_ENABLED(CONFIG_IPV6) |
fceb9c3e | 72 | struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */ |
8ed66f0e | 73 | #endif |
2d07dc79 | 74 | struct list_head next; /* geneve's per namespace list */ |
8e816df8 | 75 | struct gro_cells gro_cells; |
9e06e859 | 76 | struct geneve_config cfg; |
2d07dc79 JL |
77 | }; |
78 | ||
371bd106 PS |
79 | struct geneve_sock { |
80 | bool collect_md; | |
371bd106 PS |
81 | struct list_head list; |
82 | struct socket *sock; | |
83 | struct rcu_head rcu; | |
84 | int refcnt; | |
66d47003 | 85 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
371bd106 | 86 | }; |
2d07dc79 JL |
87 | |
88 | static inline __u32 geneve_net_vni_hash(u8 vni[3]) | |
89 | { | |
90 | __u32 vnid; | |
91 | ||
92 | vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; | |
93 | return hash_32(vnid, VNI_HASH_BITS); | |
94 | } | |
95 | ||
e305ac6c PS |
96 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
97 | { | |
98 | #ifdef __BIG_ENDIAN | |
99 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; | |
100 | #else | |
101 | return (__force __be64)(((__force u64)vni[0] << 40) | | |
102 | ((__force u64)vni[1] << 48) | | |
103 | ((__force u64)vni[2] << 56)); | |
104 | #endif | |
105 | } | |
106 | ||
9b4437a5 | 107 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
108 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) | |
109 | { | |
110 | #ifdef __BIG_ENDIAN | |
111 | vni[0] = (__force __u8)(tun_id >> 16); | |
112 | vni[1] = (__force __u8)(tun_id >> 8); | |
113 | vni[2] = (__force __u8)tun_id; | |
114 | #else | |
115 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); | |
116 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); | |
117 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); | |
118 | #endif | |
119 | } | |
120 | ||
2e0b26e1 | 121 | static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni) |
122 | { | |
2e0b26e1 | 123 | return !memcmp(vni, &tun_id[5], 3); |
2e0b26e1 | 124 | } |
125 | ||
1e9f12ec JB |
126 | static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) |
127 | { | |
128 | return gs->sock->sk->sk_family; | |
129 | } | |
130 | ||
66d47003 | 131 | static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, |
371bd106 | 132 | __be32 addr, u8 vni[]) |
2d07dc79 | 133 | { |
2d07dc79 | 134 | struct hlist_head *vni_list_head; |
4b4c21fa | 135 | struct geneve_dev_node *node; |
2d07dc79 JL |
136 | __u32 hash; |
137 | ||
2d07dc79 | 138 | /* Find the device for this VNI */ |
371bd106 | 139 | hash = geneve_net_vni_hash(vni); |
66d47003 | 140 | vni_list_head = &gs->vni_list[hash]; |
4b4c21fa | 141 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
9e06e859 SD |
142 | if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) && |
143 | addr == node->geneve->cfg.info.key.u.ipv4.dst) | |
4b4c21fa | 144 | return node->geneve; |
8ed66f0e JL |
145 | } |
146 | return NULL; | |
147 | } | |
148 | ||
149 | #if IS_ENABLED(CONFIG_IPV6) | |
150 | static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, | |
151 | struct in6_addr addr6, u8 vni[]) | |
152 | { | |
153 | struct hlist_head *vni_list_head; | |
4b4c21fa | 154 | struct geneve_dev_node *node; |
8ed66f0e JL |
155 | __u32 hash; |
156 | ||
157 | /* Find the device for this VNI */ | |
158 | hash = geneve_net_vni_hash(vni); | |
159 | vni_list_head = &gs->vni_list[hash]; | |
4b4c21fa | 160 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
9e06e859 SD |
161 | if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) && |
162 | ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst)) | |
4b4c21fa | 163 | return node->geneve; |
2d07dc79 | 164 | } |
e305ac6c PS |
165 | return NULL; |
166 | } | |
8ed66f0e | 167 | #endif |
e305ac6c | 168 | |
371bd106 PS |
169 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
170 | { | |
171 | return (struct genevehdr *)(udp_hdr(skb) + 1); | |
172 | } | |
173 | ||
9fc47545 JB |
174 | static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, |
175 | struct sk_buff *skb) | |
e305ac6c | 176 | { |
8ed66f0e | 177 | static u8 zero_vni[3]; |
9b4437a5 | 178 | u8 *vni; |
e305ac6c | 179 | |
1e9f12ec | 180 | if (geneve_get_sk_family(gs) == AF_INET) { |
9fc47545 | 181 | struct iphdr *iph; |
9b4437a5 | 182 | __be32 addr; |
9fc47545 | 183 | |
8ed66f0e | 184 | iph = ip_hdr(skb); /* outer IP header... */ |
371bd106 | 185 | |
8ed66f0e JL |
186 | if (gs->collect_md) { |
187 | vni = zero_vni; | |
188 | addr = 0; | |
189 | } else { | |
9fc47545 | 190 | vni = geneve_hdr(skb)->vni; |
8ed66f0e JL |
191 | addr = iph->saddr; |
192 | } | |
193 | ||
9fc47545 | 194 | return geneve_lookup(gs, addr, vni); |
8ed66f0e | 195 | #if IS_ENABLED(CONFIG_IPV6) |
1e9f12ec | 196 | } else if (geneve_get_sk_family(gs) == AF_INET6) { |
9b4437a5 | 197 | static struct in6_addr zero_addr6; |
9fc47545 JB |
198 | struct ipv6hdr *ip6h; |
199 | struct in6_addr addr6; | |
200 | ||
8ed66f0e | 201 | ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ |
371bd106 | 202 | |
8ed66f0e JL |
203 | if (gs->collect_md) { |
204 | vni = zero_vni; | |
205 | addr6 = zero_addr6; | |
206 | } else { | |
9fc47545 | 207 | vni = geneve_hdr(skb)->vni; |
8ed66f0e JL |
208 | addr6 = ip6h->saddr; |
209 | } | |
210 | ||
9fc47545 | 211 | return geneve6_lookup(gs, addr6, vni); |
8ed66f0e JL |
212 | #endif |
213 | } | |
9fc47545 JB |
214 | return NULL; |
215 | } | |
216 | ||
217 | /* geneve receive/decap routine */ | |
218 | static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, | |
219 | struct sk_buff *skb) | |
220 | { | |
221 | struct genevehdr *gnvh = geneve_hdr(skb); | |
222 | struct metadata_dst *tun_dst = NULL; | |
fe741e23 | 223 | unsigned int len; |
9fc47545 JB |
224 | int err = 0; |
225 | void *oiph; | |
2d07dc79 | 226 | |
371bd106 | 227 | if (ip_tunnel_collect_metadata() || gs->collect_md) { |
e305ac6c | 228 | __be16 flags; |
e305ac6c | 229 | |
9c2e14b4 | 230 | flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) | |
e305ac6c PS |
231 | (gnvh->critical ? TUNNEL_CRIT_OPT : 0); |
232 | ||
1e9f12ec | 233 | tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, |
e305ac6c PS |
234 | vni_to_tunnel_id(gnvh->vni), |
235 | gnvh->opt_len * 4); | |
fe741e23 GM |
236 | if (!tun_dst) { |
237 | geneve->dev->stats.rx_dropped++; | |
e305ac6c | 238 | goto drop; |
fe741e23 | 239 | } |
e305ac6c | 240 | /* Update tunnel dst according to Geneve options. */ |
4c222798 | 241 | ip_tunnel_info_opts_set(&tun_dst->u.tun_info, |
256c87c1 PJV |
242 | gnvh->options, gnvh->opt_len * 4, |
243 | TUNNEL_GENEVE_OPT); | |
e305ac6c PS |
244 | } else { |
245 | /* Drop packets w/ critical options, | |
246 | * since we don't support any... | |
247 | */ | |
fe741e23 GM |
248 | if (gnvh->critical) { |
249 | geneve->dev->stats.rx_frame_errors++; | |
250 | geneve->dev->stats.rx_errors++; | |
e305ac6c | 251 | goto drop; |
fe741e23 | 252 | } |
e305ac6c | 253 | } |
2d07dc79 | 254 | |
e305ac6c PS |
255 | if (tun_dst) |
256 | skb_dst_set(skb, &tun_dst->dst); | |
257 | ||
435fe1c0 EB |
258 | if (gnvh->proto_type == htons(ETH_P_TEB)) { |
259 | skb_reset_mac_header(skb); | |
260 | skb->protocol = eth_type_trans(skb, geneve->dev); | |
261 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
262 | ||
263 | /* Ignore packet loops (and multicast echo) */ | |
264 | if (ether_addr_equal(eth_hdr(skb)->h_source, | |
265 | geneve->dev->dev_addr)) { | |
266 | geneve->dev->stats.rx_errors++; | |
267 | goto drop; | |
268 | } | |
269 | } else { | |
270 | skb_reset_mac_header(skb); | |
271 | skb->dev = geneve->dev; | |
272 | skb->pkt_type = PACKET_HOST; | |
fe741e23 | 273 | } |
c02bd115 | 274 | |
9fc47545 | 275 | oiph = skb_network_header(skb); |
2d07dc79 JL |
276 | skb_reset_network_header(skb); |
277 | ||
9fc47545 JB |
278 | if (geneve_get_sk_family(gs) == AF_INET) |
279 | err = IP_ECN_decapsulate(oiph, skb); | |
8ed66f0e | 280 | #if IS_ENABLED(CONFIG_IPV6) |
9fc47545 JB |
281 | else |
282 | err = IP6_ECN_decapsulate(oiph, skb); | |
8ed66f0e | 283 | #endif |
2d07dc79 JL |
284 | |
285 | if (unlikely(err)) { | |
8ed66f0e | 286 | if (log_ecn_error) { |
9fc47545 | 287 | if (geneve_get_sk_family(gs) == AF_INET) |
8ed66f0e JL |
288 | net_info_ratelimited("non-ECT from %pI4 " |
289 | "with TOS=%#x\n", | |
9fc47545 JB |
290 | &((struct iphdr *)oiph)->saddr, |
291 | ((struct iphdr *)oiph)->tos); | |
8ed66f0e | 292 | #if IS_ENABLED(CONFIG_IPV6) |
9fc47545 | 293 | else |
8ed66f0e | 294 | net_info_ratelimited("non-ECT from %pI6\n", |
9fc47545 | 295 | &((struct ipv6hdr *)oiph)->saddr); |
8ed66f0e JL |
296 | #endif |
297 | } | |
2d07dc79 JL |
298 | if (err > 1) { |
299 | ++geneve->dev->stats.rx_frame_errors; | |
300 | ++geneve->dev->stats.rx_errors; | |
301 | goto drop; | |
302 | } | |
303 | } | |
304 | ||
fe741e23 GM |
305 | len = skb->len; |
306 | err = gro_cells_receive(&geneve->gro_cells, skb); | |
1e84527b FF |
307 | if (likely(err == NET_RX_SUCCESS)) |
308 | dev_sw_netstats_rx_add(geneve->dev, len); | |
309 | ||
2d07dc79 JL |
310 | return; |
311 | drop: | |
312 | /* Consume bad packet */ | |
313 | kfree_skb(skb); | |
314 | } | |
315 | ||
316 | /* Setup stats when device is created */ | |
317 | static int geneve_init(struct net_device *dev) | |
318 | { | |
8e816df8 JG |
319 | struct geneve_dev *geneve = netdev_priv(dev); |
320 | int err; | |
321 | ||
2d07dc79 JL |
322 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
323 | if (!dev->tstats) | |
324 | return -ENOMEM; | |
8e816df8 JG |
325 | |
326 | err = gro_cells_init(&geneve->gro_cells, dev); | |
327 | if (err) { | |
328 | free_percpu(dev->tstats); | |
329 | return err; | |
330 | } | |
331 | ||
9e06e859 | 332 | err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL); |
468dfffc PA |
333 | if (err) { |
334 | free_percpu(dev->tstats); | |
335 | gro_cells_destroy(&geneve->gro_cells); | |
336 | return err; | |
337 | } | |
2d07dc79 JL |
338 | return 0; |
339 | } | |
340 | ||
341 | static void geneve_uninit(struct net_device *dev) | |
342 | { | |
8e816df8 JG |
343 | struct geneve_dev *geneve = netdev_priv(dev); |
344 | ||
9e06e859 | 345 | dst_cache_destroy(&geneve->cfg.info.dst_cache); |
8e816df8 | 346 | gro_cells_destroy(&geneve->gro_cells); |
2d07dc79 JL |
347 | free_percpu(dev->tstats); |
348 | } | |
349 | ||
371bd106 PS |
350 | /* Callback from net/ipv4/udp.c to receive packets */ |
351 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
352 | { | |
353 | struct genevehdr *geneveh; | |
9fc47545 | 354 | struct geneve_dev *geneve; |
371bd106 | 355 | struct geneve_sock *gs; |
435fe1c0 | 356 | __be16 inner_proto; |
371bd106 PS |
357 | int opts_len; |
358 | ||
fe741e23 | 359 | /* Need UDP and Geneve header to be present */ |
371bd106 | 360 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
e5aed006 | 361 | goto drop; |
371bd106 PS |
362 | |
363 | /* Return packets with reserved bits set */ | |
364 | geneveh = geneve_hdr(skb); | |
365 | if (unlikely(geneveh->ver != GENEVE_VER)) | |
e5aed006 | 366 | goto drop; |
371bd106 | 367 | |
435fe1c0 EB |
368 | inner_proto = geneveh->proto_type; |
369 | ||
370 | if (unlikely((inner_proto != htons(ETH_P_TEB) && | |
371 | inner_proto != htons(ETH_P_IP) && | |
372 | inner_proto != htons(ETH_P_IPV6)))) | |
e5aed006 | 373 | goto drop; |
371bd106 | 374 | |
9fc47545 JB |
375 | gs = rcu_dereference_sk_user_data(sk); |
376 | if (!gs) | |
377 | goto drop; | |
378 | ||
379 | geneve = geneve_lookup_skb(gs, skb); | |
380 | if (!geneve) | |
381 | goto drop; | |
382 | ||
435fe1c0 EB |
383 | if (unlikely((!geneve->cfg.inner_proto_inherit && |
384 | inner_proto != htons(ETH_P_TEB)))) { | |
385 | geneve->dev->stats.rx_dropped++; | |
386 | goto drop; | |
387 | } | |
388 | ||
371bd106 | 389 | opts_len = geneveh->opt_len * 4; |
435fe1c0 | 390 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, inner_proto, |
fe741e23 GM |
391 | !net_eq(geneve->net, dev_net(geneve->dev)))) { |
392 | geneve->dev->stats.rx_dropped++; | |
371bd106 | 393 | goto drop; |
fe741e23 | 394 | } |
371bd106 | 395 | |
9fc47545 | 396 | geneve_rx(geneve, gs, skb); |
371bd106 PS |
397 | return 0; |
398 | ||
399 | drop: | |
400 | /* Consume bad packet */ | |
401 | kfree_skb(skb); | |
402 | return 0; | |
371bd106 PS |
403 | } |
404 | ||
a0796644 SB |
405 | /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */ |
406 | static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb) | |
407 | { | |
408 | struct genevehdr *geneveh; | |
409 | struct geneve_sock *gs; | |
410 | u8 zero_vni[3] = { 0 }; | |
411 | u8 *vni = zero_vni; | |
412 | ||
eccc73a6 | 413 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN)) |
a0796644 SB |
414 | return -EINVAL; |
415 | ||
416 | geneveh = geneve_hdr(skb); | |
417 | if (geneveh->ver != GENEVE_VER) | |
418 | return -EINVAL; | |
419 | ||
420 | if (geneveh->proto_type != htons(ETH_P_TEB)) | |
421 | return -EINVAL; | |
422 | ||
423 | gs = rcu_dereference_sk_user_data(sk); | |
424 | if (!gs) | |
425 | return -ENOENT; | |
426 | ||
427 | if (geneve_get_sk_family(gs) == AF_INET) { | |
428 | struct iphdr *iph = ip_hdr(skb); | |
429 | __be32 addr4 = 0; | |
430 | ||
431 | if (!gs->collect_md) { | |
432 | vni = geneve_hdr(skb)->vni; | |
433 | addr4 = iph->daddr; | |
434 | } | |
435 | ||
436 | return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT; | |
437 | } | |
438 | ||
439 | #if IS_ENABLED(CONFIG_IPV6) | |
440 | if (geneve_get_sk_family(gs) == AF_INET6) { | |
441 | struct ipv6hdr *ip6h = ipv6_hdr(skb); | |
8a962c4a NC |
442 | struct in6_addr addr6; |
443 | ||
444 | memset(&addr6, 0, sizeof(struct in6_addr)); | |
a0796644 SB |
445 | |
446 | if (!gs->collect_md) { | |
447 | vni = geneve_hdr(skb)->vni; | |
448 | addr6 = ip6h->daddr; | |
449 | } | |
450 | ||
451 | return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT; | |
452 | } | |
453 | #endif | |
454 | ||
455 | return -EPFNOSUPPORT; | |
456 | } | |
457 | ||
371bd106 | 458 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
9b4437a5 | 459 | __be16 port, bool ipv6_rx_csum) |
371bd106 PS |
460 | { |
461 | struct socket *sock; | |
462 | struct udp_port_cfg udp_conf; | |
463 | int err; | |
464 | ||
465 | memset(&udp_conf, 0, sizeof(udp_conf)); | |
466 | ||
467 | if (ipv6) { | |
468 | udp_conf.family = AF_INET6; | |
8ed66f0e | 469 | udp_conf.ipv6_v6only = 1; |
9b4437a5 | 470 | udp_conf.use_udp6_rx_checksums = ipv6_rx_csum; |
371bd106 PS |
471 | } else { |
472 | udp_conf.family = AF_INET; | |
473 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); | |
474 | } | |
475 | ||
476 | udp_conf.local_udp_port = port; | |
477 | ||
478 | /* Open UDP socket */ | |
479 | err = udp_sock_create(net, &udp_conf, &sock); | |
480 | if (err < 0) | |
481 | return ERR_PTR(err); | |
482 | ||
61630c4f | 483 | udp_allow_gso(sock->sk); |
371bd106 PS |
484 | return sock; |
485 | } | |
486 | ||
371bd106 PS |
487 | static int geneve_hlen(struct genevehdr *gh) |
488 | { | |
489 | return sizeof(*gh) + gh->opt_len * 4; | |
490 | } | |
491 | ||
d4546c25 DM |
492 | static struct sk_buff *geneve_gro_receive(struct sock *sk, |
493 | struct list_head *head, | |
494 | struct sk_buff *skb) | |
371bd106 | 495 | { |
d4546c25 DM |
496 | struct sk_buff *pp = NULL; |
497 | struct sk_buff *p; | |
371bd106 PS |
498 | struct genevehdr *gh, *gh2; |
499 | unsigned int hlen, gh_len, off_gnv; | |
500 | const struct packet_offload *ptype; | |
501 | __be16 type; | |
502 | int flush = 1; | |
503 | ||
504 | off_gnv = skb_gro_offset(skb); | |
505 | hlen = off_gnv + sizeof(*gh); | |
506 | gh = skb_gro_header_fast(skb, off_gnv); | |
507 | if (skb_gro_header_hard(skb, hlen)) { | |
508 | gh = skb_gro_header_slow(skb, hlen, off_gnv); | |
509 | if (unlikely(!gh)) | |
510 | goto out; | |
511 | } | |
512 | ||
513 | if (gh->ver != GENEVE_VER || gh->oam) | |
514 | goto out; | |
515 | gh_len = geneve_hlen(gh); | |
516 | ||
517 | hlen = off_gnv + gh_len; | |
518 | if (skb_gro_header_hard(skb, hlen)) { | |
519 | gh = skb_gro_header_slow(skb, hlen, off_gnv); | |
520 | if (unlikely(!gh)) | |
521 | goto out; | |
522 | } | |
523 | ||
d4546c25 | 524 | list_for_each_entry(p, head, list) { |
371bd106 PS |
525 | if (!NAPI_GRO_CB(p)->same_flow) |
526 | continue; | |
527 | ||
528 | gh2 = (struct genevehdr *)(p->data + off_gnv); | |
529 | if (gh->opt_len != gh2->opt_len || | |
530 | memcmp(gh, gh2, gh_len)) { | |
531 | NAPI_GRO_CB(p)->same_flow = 0; | |
532 | continue; | |
533 | } | |
534 | } | |
535 | ||
f623f83a PA |
536 | skb_gro_pull(skb, gh_len); |
537 | skb_gro_postpull_rcsum(skb, gh, gh_len); | |
371bd106 | 538 | type = gh->proto_type; |
f623f83a PA |
539 | if (likely(type == htons(ETH_P_TEB))) |
540 | return call_gro_receive(eth_gro_receive, head, skb); | |
371bd106 | 541 | |
371bd106 | 542 | ptype = gro_find_receive_by_type(type); |
c194cf93 | 543 | if (!ptype) |
fc1ca334 | 544 | goto out; |
371bd106 | 545 | |
fcd91dd4 | 546 | pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); |
c194cf93 | 547 | flush = 0; |
371bd106 | 548 | |
371bd106 | 549 | out: |
603d4cf8 | 550 | skb_gro_flush_final(skb, pp, flush); |
371bd106 PS |
551 | |
552 | return pp; | |
553 | } | |
554 | ||
4a0090a9 TH |
555 | static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, |
556 | int nhoff) | |
371bd106 PS |
557 | { |
558 | struct genevehdr *gh; | |
559 | struct packet_offload *ptype; | |
560 | __be16 type; | |
561 | int gh_len; | |
562 | int err = -ENOSYS; | |
563 | ||
371bd106 PS |
564 | gh = (struct genevehdr *)(skb->data + nhoff); |
565 | gh_len = geneve_hlen(gh); | |
566 | type = gh->proto_type; | |
567 | ||
f623f83a PA |
568 | /* since skb->encapsulation is set, eth_gro_complete() sets the inner mac header */ |
569 | if (likely(type == htons(ETH_P_TEB))) | |
570 | return eth_gro_complete(skb, nhoff + gh_len); | |
571 | ||
371bd106 PS |
572 | ptype = gro_find_complete_by_type(type); |
573 | if (ptype) | |
574 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); | |
575 | ||
229740c6 JR |
576 | skb_set_inner_mac_header(skb, nhoff + gh_len); |
577 | ||
371bd106 PS |
578 | return err; |
579 | } | |
580 | ||
581 | /* Create new listen socket if needed */ | |
582 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, | |
9b4437a5 | 583 | bool ipv6, bool ipv6_rx_csum) |
371bd106 PS |
584 | { |
585 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
586 | struct geneve_sock *gs; | |
587 | struct socket *sock; | |
588 | struct udp_tunnel_sock_cfg tunnel_cfg; | |
66d47003 | 589 | int h; |
371bd106 PS |
590 | |
591 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); | |
592 | if (!gs) | |
593 | return ERR_PTR(-ENOMEM); | |
594 | ||
9b4437a5 | 595 | sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum); |
371bd106 PS |
596 | if (IS_ERR(sock)) { |
597 | kfree(gs); | |
598 | return ERR_CAST(sock); | |
599 | } | |
600 | ||
601 | gs->sock = sock; | |
602 | gs->refcnt = 1; | |
66d47003 PS |
603 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
604 | INIT_HLIST_HEAD(&gs->vni_list[h]); | |
371bd106 PS |
605 | |
606 | /* Initialize the geneve udp offloads structure */ | |
e7b3db5e | 607 | udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
371bd106 PS |
608 | |
609 | /* Mark socket as an encapsulation socket */ | |
4a0090a9 | 610 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
371bd106 PS |
611 | tunnel_cfg.sk_user_data = gs; |
612 | tunnel_cfg.encap_type = 1; | |
4a0090a9 TH |
613 | tunnel_cfg.gro_receive = geneve_gro_receive; |
614 | tunnel_cfg.gro_complete = geneve_gro_complete; | |
371bd106 | 615 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
a0796644 | 616 | tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup; |
371bd106 PS |
617 | tunnel_cfg.encap_destroy = NULL; |
618 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); | |
371bd106 PS |
619 | list_add(&gs->list, &gn->sock_list); |
620 | return gs; | |
621 | } | |
622 | ||
8ed66f0e | 623 | static void __geneve_sock_release(struct geneve_sock *gs) |
371bd106 | 624 | { |
8ed66f0e | 625 | if (!gs || --gs->refcnt) |
371bd106 PS |
626 | return; |
627 | ||
628 | list_del(&gs->list); | |
e7b3db5e | 629 | udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
371bd106 PS |
630 | udp_tunnel_sock_release(gs->sock); |
631 | kfree_rcu(gs, rcu); | |
632 | } | |
633 | ||
8ed66f0e JL |
634 | static void geneve_sock_release(struct geneve_dev *geneve) |
635 | { | |
fceb9c3e | 636 | struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4); |
8ed66f0e | 637 | #if IS_ENABLED(CONFIG_IPV6) |
fceb9c3e | 638 | struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6); |
639 | ||
640 | rcu_assign_pointer(geneve->sock6, NULL); | |
641 | #endif | |
642 | ||
643 | rcu_assign_pointer(geneve->sock4, NULL); | |
644 | synchronize_net(); | |
645 | ||
646 | __geneve_sock_release(gs4); | |
647 | #if IS_ENABLED(CONFIG_IPV6) | |
648 | __geneve_sock_release(gs6); | |
8ed66f0e JL |
649 | #endif |
650 | } | |
651 | ||
371bd106 | 652 | static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, |
8ed66f0e | 653 | sa_family_t family, |
371bd106 PS |
654 | __be16 dst_port) |
655 | { | |
656 | struct geneve_sock *gs; | |
657 | ||
658 | list_for_each_entry(gs, &gn->sock_list, list) { | |
659 | if (inet_sk(gs->sock->sk)->inet_sport == dst_port && | |
1e9f12ec | 660 | geneve_get_sk_family(gs) == family) { |
371bd106 PS |
661 | return gs; |
662 | } | |
663 | } | |
664 | return NULL; | |
665 | } | |
666 | ||
8ed66f0e | 667 | static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) |
2d07dc79 | 668 | { |
2d07dc79 | 669 | struct net *net = geneve->net; |
371bd106 | 670 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
4b4c21fa | 671 | struct geneve_dev_node *node; |
2d07dc79 | 672 | struct geneve_sock *gs; |
9b4437a5 | 673 | __u8 vni[3]; |
66d47003 | 674 | __u32 hash; |
2d07dc79 | 675 | |
9e06e859 | 676 | gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst); |
371bd106 PS |
677 | if (gs) { |
678 | gs->refcnt++; | |
679 | goto out; | |
680 | } | |
681 | ||
9e06e859 SD |
682 | gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6, |
683 | geneve->cfg.use_udp6_rx_checksums); | |
2d07dc79 JL |
684 | if (IS_ERR(gs)) |
685 | return PTR_ERR(gs); | |
686 | ||
371bd106 | 687 | out: |
9e06e859 | 688 | gs->collect_md = geneve->cfg.collect_md; |
8ed66f0e | 689 | #if IS_ENABLED(CONFIG_IPV6) |
4b4c21fa | 690 | if (ipv6) { |
fceb9c3e | 691 | rcu_assign_pointer(geneve->sock6, gs); |
4b4c21fa JB |
692 | node = &geneve->hlist6; |
693 | } else | |
8ed66f0e | 694 | #endif |
4b4c21fa | 695 | { |
fceb9c3e | 696 | rcu_assign_pointer(geneve->sock4, gs); |
4b4c21fa JB |
697 | node = &geneve->hlist4; |
698 | } | |
699 | node->geneve = geneve; | |
66d47003 | 700 | |
9e06e859 | 701 | tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni); |
9b4437a5 | 702 | hash = geneve_net_vni_hash(vni); |
4b4c21fa | 703 | hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]); |
2d07dc79 JL |
704 | return 0; |
705 | } | |
706 | ||
8ed66f0e JL |
707 | static int geneve_open(struct net_device *dev) |
708 | { | |
709 | struct geneve_dev *geneve = netdev_priv(dev); | |
9e06e859 | 710 | bool metadata = geneve->cfg.collect_md; |
cf1c9ccb | 711 | bool ipv4, ipv6; |
8ed66f0e JL |
712 | int ret = 0; |
713 | ||
9e06e859 | 714 | ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata; |
cf1c9ccb | 715 | ipv4 = !ipv6 || metadata; |
8ed66f0e | 716 | #if IS_ENABLED(CONFIG_IPV6) |
cf1c9ccb | 717 | if (ipv6) { |
8ed66f0e | 718 | ret = geneve_sock_add(geneve, true); |
cf1c9ccb JB |
719 | if (ret < 0 && ret != -EAFNOSUPPORT) |
720 | ipv4 = false; | |
721 | } | |
8ed66f0e | 722 | #endif |
cf1c9ccb | 723 | if (ipv4) |
8ed66f0e JL |
724 | ret = geneve_sock_add(geneve, false); |
725 | if (ret < 0) | |
726 | geneve_sock_release(geneve); | |
727 | ||
728 | return ret; | |
729 | } | |
730 | ||
2d07dc79 JL |
731 | static int geneve_stop(struct net_device *dev) |
732 | { | |
733 | struct geneve_dev *geneve = netdev_priv(dev); | |
2d07dc79 | 734 | |
4b4c21fa JB |
735 | hlist_del_init_rcu(&geneve->hlist4.hlist); |
736 | #if IS_ENABLED(CONFIG_IPV6) | |
737 | hlist_del_init_rcu(&geneve->hlist6.hlist); | |
738 | #endif | |
8ed66f0e | 739 | geneve_sock_release(geneve); |
371bd106 PS |
740 | return 0; |
741 | } | |
742 | ||
8ed66f0e | 743 | static void geneve_build_header(struct genevehdr *geneveh, |
435fe1c0 EB |
744 | const struct ip_tunnel_info *info, |
745 | __be16 inner_proto) | |
8ed66f0e JL |
746 | { |
747 | geneveh->ver = GENEVE_VER; | |
c3ef5aa5 | 748 | geneveh->opt_len = info->options_len / 4; |
749 | geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM); | |
750 | geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT); | |
8ed66f0e | 751 | geneveh->rsvd1 = 0; |
c3ef5aa5 | 752 | tunnel_id_to_vni(info->key.tun_id, geneveh->vni); |
435fe1c0 | 753 | geneveh->proto_type = inner_proto; |
8ed66f0e JL |
754 | geneveh->rsvd2 = 0; |
755 | ||
256c87c1 PJV |
756 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) |
757 | ip_tunnel_info_opts_get(geneveh->options, info); | |
8ed66f0e JL |
758 | } |
759 | ||
c3ef5aa5 | 760 | static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, |
761 | const struct ip_tunnel_info *info, | |
435fe1c0 EB |
762 | bool xnet, int ip_hdr_len, |
763 | bool inner_proto_inherit) | |
371bd106 | 764 | { |
c3ef5aa5 | 765 | bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
8ed66f0e | 766 | struct genevehdr *gnvh; |
435fe1c0 | 767 | __be16 inner_proto; |
8ed66f0e JL |
768 | int min_headroom; |
769 | int err; | |
770 | ||
c3ef5aa5 | 771 | skb_reset_mac_header(skb); |
8ed66f0e JL |
772 | skb_scrub_packet(skb, xnet); |
773 | ||
c3ef5aa5 | 774 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + |
775 | GENEVE_BASE_HLEN + info->options_len + ip_hdr_len; | |
8ed66f0e | 776 | err = skb_cow_head(skb, min_headroom); |
aed069df | 777 | if (unlikely(err)) |
8ed66f0e | 778 | goto free_dst; |
8ed66f0e | 779 | |
aed069df | 780 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
1ba64fac | 781 | if (err) |
8ed66f0e | 782 | goto free_dst; |
8ed66f0e | 783 | |
d58ff351 | 784 | gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len); |
435fe1c0 EB |
785 | inner_proto = inner_proto_inherit ? skb->protocol : htons(ETH_P_TEB); |
786 | geneve_build_header(gnvh, info, inner_proto); | |
787 | skb_set_inner_protocol(skb, inner_proto); | |
8ed66f0e JL |
788 | return 0; |
789 | ||
790 | free_dst: | |
791 | dst_release(dst); | |
792 | return err; | |
793 | } | |
8ed66f0e JL |
794 | |
795 | static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, | |
796 | struct net_device *dev, | |
5b861f6b | 797 | struct geneve_sock *gs4, |
8ed66f0e | 798 | struct flowi4 *fl4, |
34beb215 MG |
799 | const struct ip_tunnel_info *info, |
800 | __be16 dport, __be16 sport) | |
e305ac6c | 801 | { |
db3c6139 | 802 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
e305ac6c | 803 | struct geneve_dev *geneve = netdev_priv(dev); |
468dfffc | 804 | struct dst_cache *dst_cache; |
e305ac6c PS |
805 | struct rtable *rt = NULL; |
806 | __u8 tos; | |
807 | ||
5b861f6b | 808 | if (!gs4) |
fceb9c3e | 809 | return ERR_PTR(-EIO); |
810 | ||
e305ac6c PS |
811 | memset(fl4, 0, sizeof(*fl4)); |
812 | fl4->flowi4_mark = skb->mark; | |
813 | fl4->flowi4_proto = IPPROTO_UDP; | |
9b4437a5 | 814 | fl4->daddr = info->key.u.ipv4.dst; |
815 | fl4->saddr = info->key.u.ipv4.src; | |
34beb215 MG |
816 | fl4->fl4_dport = dport; |
817 | fl4->fl4_sport = sport; | |
e305ac6c | 818 | |
9b4437a5 | 819 | tos = info->key.tos; |
9e06e859 | 820 | if ((tos == 1) && !geneve->cfg.collect_md) { |
9b4437a5 | 821 | tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
822 | use_cache = false; | |
468dfffc | 823 | } |
9b4437a5 | 824 | fl4->flowi4_tos = RT_TOS(tos); |
468dfffc | 825 | |
c3ef5aa5 | 826 | dst_cache = (struct dst_cache *)&info->dst_cache; |
468dfffc PA |
827 | if (use_cache) { |
828 | rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); | |
829 | if (rt) | |
830 | return rt; | |
e305ac6c | 831 | } |
e305ac6c PS |
832 | rt = ip_route_output_key(geneve->net, fl4); |
833 | if (IS_ERR(rt)) { | |
834 | netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); | |
fc4099f1 | 835 | return ERR_PTR(-ENETUNREACH); |
e305ac6c PS |
836 | } |
837 | if (rt->dst.dev == dev) { /* is this necessary? */ | |
838 | netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); | |
e305ac6c | 839 | ip_rt_put(rt); |
fc4099f1 | 840 | return ERR_PTR(-ELOOP); |
e305ac6c | 841 | } |
468dfffc PA |
842 | if (use_cache) |
843 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); | |
e305ac6c PS |
844 | return rt; |
845 | } | |
846 | ||
8ed66f0e JL |
847 | #if IS_ENABLED(CONFIG_IPV6) |
848 | static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, | |
849 | struct net_device *dev, | |
5b861f6b | 850 | struct geneve_sock *gs6, |
8ed66f0e | 851 | struct flowi6 *fl6, |
34beb215 MG |
852 | const struct ip_tunnel_info *info, |
853 | __be16 dport, __be16 sport) | |
8ed66f0e | 854 | { |
db3c6139 | 855 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
8ed66f0e | 856 | struct geneve_dev *geneve = netdev_priv(dev); |
8ed66f0e | 857 | struct dst_entry *dst = NULL; |
468dfffc | 858 | struct dst_cache *dst_cache; |
3a56f86f | 859 | __u8 prio; |
8ed66f0e | 860 | |
fceb9c3e | 861 | if (!gs6) |
862 | return ERR_PTR(-EIO); | |
863 | ||
8ed66f0e JL |
864 | memset(fl6, 0, sizeof(*fl6)); |
865 | fl6->flowi6_mark = skb->mark; | |
866 | fl6->flowi6_proto = IPPROTO_UDP; | |
9b4437a5 | 867 | fl6->daddr = info->key.u.ipv6.dst; |
868 | fl6->saddr = info->key.u.ipv6.src; | |
34beb215 MG |
869 | fl6->fl6_dport = dport; |
870 | fl6->fl6_sport = sport; | |
871 | ||
9b4437a5 | 872 | prio = info->key.tos; |
9e06e859 | 873 | if ((prio == 1) && !geneve->cfg.collect_md) { |
9b4437a5 | 874 | prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
875 | use_cache = false; | |
468dfffc PA |
876 | } |
877 | ||
9b4437a5 | 878 | fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio), |
879 | info->key.label); | |
c3ef5aa5 | 880 | dst_cache = (struct dst_cache *)&info->dst_cache; |
468dfffc PA |
881 | if (use_cache) { |
882 | dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); | |
883 | if (dst) | |
884 | return dst; | |
8ed66f0e | 885 | } |
6c8991f4 SD |
886 | dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6, |
887 | NULL); | |
888 | if (IS_ERR(dst)) { | |
8ed66f0e JL |
889 | netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); |
890 | return ERR_PTR(-ENETUNREACH); | |
891 | } | |
892 | if (dst->dev == dev) { /* is this necessary? */ | |
893 | netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); | |
894 | dst_release(dst); | |
895 | return ERR_PTR(-ELOOP); | |
896 | } | |
897 | ||
468dfffc PA |
898 | if (use_cache) |
899 | dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); | |
8ed66f0e JL |
900 | return dst; |
901 | } | |
902 | #endif | |
903 | ||
9b4437a5 | 904 | static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
c3ef5aa5 | 905 | struct geneve_dev *geneve, |
906 | const struct ip_tunnel_info *info) | |
2d07dc79 | 907 | { |
9b4437a5 | 908 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
909 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); | |
910 | const struct ip_tunnel_key *key = &info->key; | |
911 | struct rtable *rt; | |
2d07dc79 | 912 | struct flowi4 fl4; |
8760ce58 | 913 | __u8 tos, ttl; |
a025fb5f | 914 | __be16 df = 0; |
e305ac6c | 915 | __be16 sport; |
bcceeec3 | 916 | int err; |
980c394c | 917 | |
d13f048d | 918 | if (!pskb_inet_may_pull(skb)) |
6628ddfe PP |
919 | return -EINVAL; |
920 | ||
34beb215 MG |
921 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
922 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info, | |
923 | geneve->cfg.info.key.tp_dst, sport); | |
9b4437a5 | 924 | if (IS_ERR(rt)) |
925 | return PTR_ERR(rt); | |
371bd106 | 926 | |
c1a800e8 SB |
927 | err = skb_tunnel_check_pmtu(skb, &rt->dst, |
928 | GENEVE_IPV4_HLEN + info->options_len, | |
929 | netif_is_any_bridge_port(dev)); | |
930 | if (err < 0) { | |
931 | dst_release(&rt->dst); | |
932 | return err; | |
933 | } else if (err) { | |
934 | struct ip_tunnel_info *info; | |
935 | ||
936 | info = skb_tunnel_info(skb); | |
937 | if (info) { | |
68c1a943 AT |
938 | struct ip_tunnel_info *unclone; |
939 | ||
940 | unclone = skb_tunnel_info_unclone(skb); | |
941 | if (unlikely(!unclone)) { | |
942 | dst_release(&rt->dst); | |
943 | return -ENOMEM; | |
944 | } | |
945 | ||
946 | unclone->key.u.ipv4.dst = fl4.saddr; | |
947 | unclone->key.u.ipv4.src = fl4.daddr; | |
c1a800e8 SB |
948 | } |
949 | ||
950 | if (!pskb_may_pull(skb, ETH_HLEN)) { | |
951 | dst_release(&rt->dst); | |
952 | return -EINVAL; | |
953 | } | |
954 | ||
955 | skb->protocol = eth_type_trans(skb, geneve->dev); | |
baebdf48 | 956 | __netif_rx(skb); |
c1a800e8 SB |
957 | dst_release(&rt->dst); |
958 | return -EMSGSIZE; | |
959 | } | |
52a589d5 | 960 | |
9e06e859 | 961 | if (geneve->cfg.collect_md) { |
9b4437a5 | 962 | tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
371bd106 | 963 | ttl = key->ttl; |
a025fb5f SB |
964 | |
965 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
e305ac6c | 966 | } else { |
9b4437a5 | 967 | tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb); |
9e06e859 | 968 | if (geneve->cfg.ttl_inherit) |
52d0d404 HL |
969 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
970 | else | |
971 | ttl = key->ttl; | |
972 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
a025fb5f | 973 | |
9e06e859 | 974 | if (geneve->cfg.df == GENEVE_DF_SET) { |
a025fb5f | 975 | df = htons(IP_DF); |
9e06e859 | 976 | } else if (geneve->cfg.df == GENEVE_DF_INHERIT) { |
a025fb5f SB |
977 | struct ethhdr *eth = eth_hdr(skb); |
978 | ||
979 | if (ntohs(eth->h_proto) == ETH_P_IPV6) { | |
980 | df = htons(IP_DF); | |
981 | } else if (ntohs(eth->h_proto) == ETH_P_IP) { | |
982 | struct iphdr *iph = ip_hdr(skb); | |
983 | ||
984 | if (iph->frag_off & htons(IP_DF)) | |
985 | df = htons(IP_DF); | |
986 | } | |
987 | } | |
2d07dc79 | 988 | } |
2d07dc79 | 989 | |
435fe1c0 EB |
990 | err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr), |
991 | geneve->cfg.inner_proto_inherit); | |
9b4437a5 | 992 | if (unlikely(err)) |
993 | return err; | |
efeb2267 | 994 | |
9b4437a5 | 995 | udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, |
9e06e859 | 996 | tos, ttl, df, sport, geneve->cfg.info.key.tp_dst, |
9b4437a5 | 997 | !net_eq(geneve->net, dev_net(geneve->dev)), |
998 | !(info->key.tun_flags & TUNNEL_CSUM)); | |
999 | return 0; | |
2d07dc79 JL |
1000 | } |
1001 | ||
8ed66f0e | 1002 | #if IS_ENABLED(CONFIG_IPV6) |
9b4437a5 | 1003 | static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
c3ef5aa5 | 1004 | struct geneve_dev *geneve, |
1005 | const struct ip_tunnel_info *info) | |
8ed66f0e | 1006 | { |
9b4437a5 | 1007 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
1008 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); | |
1009 | const struct ip_tunnel_key *key = &info->key; | |
8ed66f0e | 1010 | struct dst_entry *dst = NULL; |
8ed66f0e | 1011 | struct flowi6 fl6; |
3a56f86f | 1012 | __u8 prio, ttl; |
8ed66f0e | 1013 | __be16 sport; |
bcceeec3 | 1014 | int err; |
8ed66f0e | 1015 | |
d13f048d | 1016 | if (!pskb_inet_may_pull(skb)) |
6628ddfe PP |
1017 | return -EINVAL; |
1018 | ||
34beb215 MG |
1019 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
1020 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info, | |
1021 | geneve->cfg.info.key.tp_dst, sport); | |
9b4437a5 | 1022 | if (IS_ERR(dst)) |
1023 | return PTR_ERR(dst); | |
8ed66f0e | 1024 | |
c1a800e8 SB |
1025 | err = skb_tunnel_check_pmtu(skb, dst, |
1026 | GENEVE_IPV6_HLEN + info->options_len, | |
1027 | netif_is_any_bridge_port(dev)); | |
1028 | if (err < 0) { | |
1029 | dst_release(dst); | |
1030 | return err; | |
1031 | } else if (err) { | |
1032 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
1033 | ||
1034 | if (info) { | |
68c1a943 AT |
1035 | struct ip_tunnel_info *unclone; |
1036 | ||
1037 | unclone = skb_tunnel_info_unclone(skb); | |
1038 | if (unlikely(!unclone)) { | |
1039 | dst_release(dst); | |
1040 | return -ENOMEM; | |
1041 | } | |
1042 | ||
1043 | unclone->key.u.ipv6.dst = fl6.saddr; | |
1044 | unclone->key.u.ipv6.src = fl6.daddr; | |
c1a800e8 SB |
1045 | } |
1046 | ||
1047 | if (!pskb_may_pull(skb, ETH_HLEN)) { | |
1048 | dst_release(dst); | |
1049 | return -EINVAL; | |
1050 | } | |
1051 | ||
1052 | skb->protocol = eth_type_trans(skb, geneve->dev); | |
baebdf48 | 1053 | __netif_rx(skb); |
c1a800e8 SB |
1054 | dst_release(dst); |
1055 | return -EMSGSIZE; | |
1056 | } | |
52a589d5 | 1057 | |
9e06e859 | 1058 | if (geneve->cfg.collect_md) { |
9b4437a5 | 1059 | prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
1060 | ttl = key->ttl; | |
1061 | } else { | |
1062 | prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel), | |
1063 | ip_hdr(skb), skb); | |
9e06e859 | 1064 | if (geneve->cfg.ttl_inherit) |
52d0d404 HL |
1065 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
1066 | else | |
1067 | ttl = key->ttl; | |
1068 | ttl = ttl ? : ip6_dst_hoplimit(dst); | |
9b4437a5 | 1069 | } |
435fe1c0 EB |
1070 | err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr), |
1071 | geneve->cfg.inner_proto_inherit); | |
9b4437a5 | 1072 | if (unlikely(err)) |
1073 | return err; | |
8ed66f0e | 1074 | |
9b4437a5 | 1075 | udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, |
1076 | &fl6.saddr, &fl6.daddr, prio, ttl, | |
9e06e859 | 1077 | info->key.label, sport, geneve->cfg.info.key.tp_dst, |
9b4437a5 | 1078 | !(info->key.tun_flags & TUNNEL_CSUM)); |
1079 | return 0; | |
1080 | } | |
1081 | #endif | |
8ed66f0e | 1082 | |
9b4437a5 | 1083 | static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) |
1084 | { | |
1085 | struct geneve_dev *geneve = netdev_priv(dev); | |
1086 | struct ip_tunnel_info *info = NULL; | |
1087 | int err; | |
abe492b4 | 1088 | |
9e06e859 | 1089 | if (geneve->cfg.collect_md) { |
9b4437a5 | 1090 | info = skb_tunnel_info(skb); |
1091 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { | |
9b4437a5 | 1092 | netdev_dbg(dev, "no tunnel metadata\n"); |
9d149045 JB |
1093 | dev_kfree_skb(skb); |
1094 | dev->stats.tx_dropped++; | |
1095 | return NETDEV_TX_OK; | |
9b4437a5 | 1096 | } |
8ed66f0e | 1097 | } else { |
9e06e859 | 1098 | info = &geneve->cfg.info; |
8ed66f0e | 1099 | } |
8eb3b995 | 1100 | |
a717e3f7 | 1101 | rcu_read_lock(); |
9b4437a5 | 1102 | #if IS_ENABLED(CONFIG_IPV6) |
1103 | if (info->mode & IP_TUNNEL_INFO_IPV6) | |
1104 | err = geneve6_xmit_skb(skb, dev, geneve, info); | |
1105 | else | |
1106 | #endif | |
1107 | err = geneve_xmit_skb(skb, dev, geneve, info); | |
a717e3f7 | 1108 | rcu_read_unlock(); |
8ed66f0e | 1109 | |
9b4437a5 | 1110 | if (likely(!err)) |
1111 | return NETDEV_TX_OK; | |
9d149045 | 1112 | |
c1a800e8 SB |
1113 | if (err != -EMSGSIZE) |
1114 | dev_kfree_skb(skb); | |
aed069df | 1115 | |
8ed66f0e JL |
1116 | if (err == -ELOOP) |
1117 | dev->stats.collisions++; | |
1118 | else if (err == -ENETUNREACH) | |
1119 | dev->stats.tx_carrier_errors++; | |
efeb2267 HY |
1120 | |
1121 | dev->stats.tx_errors++; | |
8ed66f0e JL |
1122 | return NETDEV_TX_OK; |
1123 | } | |
8ed66f0e | 1124 | |
91572088 | 1125 | static int geneve_change_mtu(struct net_device *dev, int new_mtu) |
55e5bfb5 | 1126 | { |
91572088 JW |
1127 | if (new_mtu > dev->max_mtu) |
1128 | new_mtu = dev->max_mtu; | |
321acc1c AK |
1129 | else if (new_mtu < dev->min_mtu) |
1130 | new_mtu = dev->min_mtu; | |
aeee0e66 | 1131 | |
55e5bfb5 DW |
1132 | dev->mtu = new_mtu; |
1133 | return 0; | |
1134 | } | |
1135 | ||
fc4099f1 PS |
1136 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
1137 | { | |
1138 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
1139 | struct geneve_dev *geneve = netdev_priv(dev); | |
34beb215 | 1140 | __be16 sport; |
fc4099f1 | 1141 | |
b8812fa8 | 1142 | if (ip_tunnel_info_af(info) == AF_INET) { |
9b4437a5 | 1143 | struct rtable *rt; |
1144 | struct flowi4 fl4; | |
34beb215 | 1145 | |
5b861f6b | 1146 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); |
34beb215 MG |
1147 | sport = udp_flow_src_port(geneve->net, skb, |
1148 | 1, USHRT_MAX, true); | |
9b4437a5 | 1149 | |
34beb215 MG |
1150 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info, |
1151 | geneve->cfg.info.key.tp_dst, sport); | |
b8812fa8 JL |
1152 | if (IS_ERR(rt)) |
1153 | return PTR_ERR(rt); | |
fc4099f1 | 1154 | |
b8812fa8 JL |
1155 | ip_rt_put(rt); |
1156 | info->key.u.ipv4.src = fl4.saddr; | |
1157 | #if IS_ENABLED(CONFIG_IPV6) | |
1158 | } else if (ip_tunnel_info_af(info) == AF_INET6) { | |
9b4437a5 | 1159 | struct dst_entry *dst; |
1160 | struct flowi6 fl6; | |
34beb215 | 1161 | |
5b861f6b | 1162 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); |
34beb215 MG |
1163 | sport = udp_flow_src_port(geneve->net, skb, |
1164 | 1, USHRT_MAX, true); | |
9b4437a5 | 1165 | |
34beb215 MG |
1166 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info, |
1167 | geneve->cfg.info.key.tp_dst, sport); | |
b8812fa8 JL |
1168 | if (IS_ERR(dst)) |
1169 | return PTR_ERR(dst); | |
1170 | ||
1171 | dst_release(dst); | |
1172 | info->key.u.ipv6.src = fl6.saddr; | |
1173 | #endif | |
1174 | } else { | |
1175 | return -EINVAL; | |
1176 | } | |
fc4099f1 | 1177 | |
34beb215 | 1178 | info->key.tp_src = sport; |
9e06e859 | 1179 | info->key.tp_dst = geneve->cfg.info.key.tp_dst; |
fc4099f1 PS |
1180 | return 0; |
1181 | } | |
1182 | ||
2d07dc79 JL |
1183 | static const struct net_device_ops geneve_netdev_ops = { |
1184 | .ndo_init = geneve_init, | |
1185 | .ndo_uninit = geneve_uninit, | |
1186 | .ndo_open = geneve_open, | |
1187 | .ndo_stop = geneve_stop, | |
1188 | .ndo_start_xmit = geneve_xmit, | |
b220a4a7 | 1189 | .ndo_get_stats64 = dev_get_tstats64, |
55e5bfb5 | 1190 | .ndo_change_mtu = geneve_change_mtu, |
2d07dc79 JL |
1191 | .ndo_validate_addr = eth_validate_addr, |
1192 | .ndo_set_mac_address = eth_mac_addr, | |
fc4099f1 | 1193 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
2d07dc79 JL |
1194 | }; |
1195 | ||
1196 | static void geneve_get_drvinfo(struct net_device *dev, | |
1197 | struct ethtool_drvinfo *drvinfo) | |
1198 | { | |
1199 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); | |
1200 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); | |
1201 | } | |
1202 | ||
1203 | static const struct ethtool_ops geneve_ethtool_ops = { | |
1204 | .get_drvinfo = geneve_get_drvinfo, | |
1205 | .get_link = ethtool_op_get_link, | |
1206 | }; | |
1207 | ||
1208 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
1209 | static struct device_type geneve_type = { | |
1210 | .name = "geneve", | |
1211 | }; | |
1212 | ||
e5de25dc | 1213 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
05ca4029 | 1214 | * supply the listening GENEVE udp ports. Callers are expected |
e5de25dc | 1215 | * to implement the ndo_udp_tunnel_add. |
05ca4029 | 1216 | */ |
2d2b13fc | 1217 | static void geneve_offload_rx_ports(struct net_device *dev, bool push) |
05ca4029 SA |
1218 | { |
1219 | struct net *net = dev_net(dev); | |
1220 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
1221 | struct geneve_sock *gs; | |
681e683f | 1222 | |
05ca4029 | 1223 | rcu_read_lock(); |
2d2b13fc SD |
1224 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { |
1225 | if (push) { | |
1226 | udp_tunnel_push_rx_port(dev, gs->sock, | |
1227 | UDP_TUNNEL_TYPE_GENEVE); | |
1228 | } else { | |
1229 | udp_tunnel_drop_rx_port(dev, gs->sock, | |
1230 | UDP_TUNNEL_TYPE_GENEVE); | |
1231 | } | |
1232 | } | |
05ca4029 SA |
1233 | rcu_read_unlock(); |
1234 | } | |
05ca4029 | 1235 | |
2d07dc79 JL |
1236 | /* Initialize the device structure. */ |
1237 | static void geneve_setup(struct net_device *dev) | |
1238 | { | |
1239 | ether_setup(dev); | |
1240 | ||
1241 | dev->netdev_ops = &geneve_netdev_ops; | |
1242 | dev->ethtool_ops = &geneve_ethtool_ops; | |
cf124db5 | 1243 | dev->needs_free_netdev = true; |
2d07dc79 JL |
1244 | |
1245 | SET_NETDEV_DEVTYPE(dev, &geneve_type); | |
1246 | ||
2d07dc79 | 1247 | dev->features |= NETIF_F_LLTX; |
18423e1a | 1248 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
2d07dc79 JL |
1249 | dev->features |= NETIF_F_RXCSUM; |
1250 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1251 | ||
18423e1a XL |
1252 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
1253 | dev->hw_features |= NETIF_F_RXCSUM; | |
2d07dc79 | 1254 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
2d07dc79 | 1255 | |
91572088 JW |
1256 | /* MTU range: 68 - (something less than 65535) */ |
1257 | dev->min_mtu = ETH_MIN_MTU; | |
1258 | /* The max_mtu calculation does not take account of GENEVE | |
1259 | * options, to avoid excluding potentially valid | |
1260 | * configurations. This will be further reduced by IPvX hdr size. | |
1261 | */ | |
1262 | dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len; | |
1263 | ||
2d07dc79 | 1264 | netif_keep_dst(dev); |
fc41cdb3 | 1265 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
ed961ac2 | 1266 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
87cd3dca | 1267 | eth_hw_addr_random(dev); |
2d07dc79 JL |
1268 | } |
1269 | ||
1270 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { | |
36c2e31a | 1271 | [IFLA_GENEVE_UNSPEC] = { .strict_start_type = IFLA_GENEVE_INNER_PROTO_INHERIT }, |
2d07dc79 | 1272 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, |
c593642c | 1273 | [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) }, |
8ed66f0e | 1274 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
8760ce58 | 1275 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
d8951125 | 1276 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
8eb3b995 | 1277 | [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, |
cd7918b3 | 1278 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
e305ac6c | 1279 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
abe492b4 TH |
1280 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
1281 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
1282 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
52d0d404 | 1283 | [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 }, |
a025fb5f | 1284 | [IFLA_GENEVE_DF] = { .type = NLA_U8 }, |
36c2e31a | 1285 | [IFLA_GENEVE_INNER_PROTO_INHERIT] = { .type = NLA_FLAG }, |
2d07dc79 JL |
1286 | }; |
1287 | ||
a8b8a889 MS |
1288 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[], |
1289 | struct netlink_ext_ack *extack) | |
2d07dc79 JL |
1290 | { |
1291 | if (tb[IFLA_ADDRESS]) { | |
c5ebc440 GM |
1292 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
1293 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], | |
1294 | "Provided link layer address is not Ethernet"); | |
2d07dc79 | 1295 | return -EINVAL; |
c5ebc440 | 1296 | } |
2d07dc79 | 1297 | |
c5ebc440 GM |
1298 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
1299 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], | |
1300 | "Provided Ethernet address is not unicast"); | |
2d07dc79 | 1301 | return -EADDRNOTAVAIL; |
c5ebc440 | 1302 | } |
2d07dc79 JL |
1303 | } |
1304 | ||
c5ebc440 GM |
1305 | if (!data) { |
1306 | NL_SET_ERR_MSG(extack, | |
1307 | "Not enough attributes provided to perform the operation"); | |
2d07dc79 | 1308 | return -EINVAL; |
c5ebc440 | 1309 | } |
2d07dc79 JL |
1310 | |
1311 | if (data[IFLA_GENEVE_ID]) { | |
1312 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); | |
1313 | ||
c5ebc440 GM |
1314 | if (vni >= GENEVE_N_VID) { |
1315 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID], | |
1316 | "Geneve ID must be lower than 16777216"); | |
2d07dc79 | 1317 | return -ERANGE; |
c5ebc440 | 1318 | } |
2d07dc79 JL |
1319 | } |
1320 | ||
a025fb5f SB |
1321 | if (data[IFLA_GENEVE_DF]) { |
1322 | enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]); | |
1323 | ||
1324 | if (df < 0 || df > GENEVE_DF_MAX) { | |
9a7b5b50 | 1325 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF], |
a025fb5f SB |
1326 | "Invalid DF attribute"); |
1327 | return -EINVAL; | |
1328 | } | |
1329 | } | |
1330 | ||
2d07dc79 JL |
1331 | return 0; |
1332 | } | |
1333 | ||
371bd106 | 1334 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
9b4437a5 | 1335 | const struct ip_tunnel_info *info, |
371bd106 PS |
1336 | bool *tun_on_same_port, |
1337 | bool *tun_collect_md) | |
1338 | { | |
9b4437a5 | 1339 | struct geneve_dev *geneve, *t = NULL; |
371bd106 PS |
1340 | |
1341 | *tun_on_same_port = false; | |
1342 | *tun_collect_md = false; | |
371bd106 | 1343 | list_for_each_entry(geneve, &gn->geneve_list, next) { |
9e06e859 SD |
1344 | if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) { |
1345 | *tun_collect_md = geneve->cfg.collect_md; | |
371bd106 PS |
1346 | *tun_on_same_port = true; |
1347 | } | |
9e06e859 SD |
1348 | if (info->key.tun_id == geneve->cfg.info.key.tun_id && |
1349 | info->key.tp_dst == geneve->cfg.info.key.tp_dst && | |
1350 | !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u))) | |
371bd106 PS |
1351 | t = geneve; |
1352 | } | |
1353 | return t; | |
1354 | } | |
1355 | ||
9b4437a5 | 1356 | static bool is_tnl_info_zero(const struct ip_tunnel_info *info) |
1357 | { | |
3fa5f11d SB |
1358 | return !(info->key.tun_id || info->key.tun_flags || info->key.tos || |
1359 | info->key.ttl || info->key.label || info->key.tp_src || | |
1360 | memchr_inv(&info->key.u, 0, sizeof(info->key.u))); | |
9b4437a5 | 1361 | } |
1362 | ||
5b861f6b GM |
1363 | static bool geneve_dst_addr_equal(struct ip_tunnel_info *a, |
1364 | struct ip_tunnel_info *b) | |
1365 | { | |
1366 | if (ip_tunnel_info_af(a) == AF_INET) | |
1367 | return a->key.u.ipv4.dst == b->key.u.ipv4.dst; | |
1368 | else | |
1369 | return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst); | |
1370 | } | |
1371 | ||
e305ac6c | 1372 | static int geneve_configure(struct net *net, struct net_device *dev, |
c5ebc440 | 1373 | struct netlink_ext_ack *extack, |
9e06e859 | 1374 | const struct geneve_config *cfg) |
2d07dc79 JL |
1375 | { |
1376 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
371bd106 | 1377 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
9e06e859 | 1378 | const struct ip_tunnel_info *info = &cfg->info; |
371bd106 | 1379 | bool tun_collect_md, tun_on_same_port; |
184fc8b5 | 1380 | int err, encap_len; |
2d07dc79 | 1381 | |
9e06e859 | 1382 | if (cfg->collect_md && !is_tnl_info_zero(info)) { |
c5ebc440 GM |
1383 | NL_SET_ERR_MSG(extack, |
1384 | "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified"); | |
8ed66f0e | 1385 | return -EINVAL; |
c5ebc440 | 1386 | } |
2d07dc79 JL |
1387 | |
1388 | geneve->net = net; | |
1389 | geneve->dev = dev; | |
1390 | ||
9b4437a5 | 1391 | t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md); |
371bd106 PS |
1392 | if (t) |
1393 | return -EBUSY; | |
1394 | ||
184fc8b5 PA |
1395 | /* make enough headroom for basic scenario */ |
1396 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; | |
9e06e859 | 1397 | if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) { |
184fc8b5 | 1398 | encap_len += sizeof(struct iphdr); |
91572088 JW |
1399 | dev->max_mtu -= sizeof(struct iphdr); |
1400 | } else { | |
184fc8b5 | 1401 | encap_len += sizeof(struct ipv6hdr); |
91572088 JW |
1402 | dev->max_mtu -= sizeof(struct ipv6hdr); |
1403 | } | |
184fc8b5 PA |
1404 | dev->needed_headroom = encap_len + ETH_HLEN; |
1405 | ||
9e06e859 | 1406 | if (cfg->collect_md) { |
c5ebc440 GM |
1407 | if (tun_on_same_port) { |
1408 | NL_SET_ERR_MSG(extack, | |
1409 | "There can be only one externally controlled device on a destination port"); | |
371bd106 | 1410 | return -EPERM; |
c5ebc440 | 1411 | } |
371bd106 | 1412 | } else { |
c5ebc440 GM |
1413 | if (tun_collect_md) { |
1414 | NL_SET_ERR_MSG(extack, | |
1415 | "There already exists an externally controlled device on this destination port"); | |
371bd106 | 1416 | return -EPERM; |
c5ebc440 | 1417 | } |
371bd106 PS |
1418 | } |
1419 | ||
9e06e859 SD |
1420 | dst_cache_reset(&geneve->cfg.info.dst_cache); |
1421 | memcpy(&geneve->cfg, cfg, sizeof(*cfg)); | |
468dfffc | 1422 | |
435fe1c0 EB |
1423 | if (geneve->cfg.inner_proto_inherit) { |
1424 | dev->header_ops = NULL; | |
1425 | dev->type = ARPHRD_NONE; | |
1426 | dev->hard_header_len = 0; | |
1427 | dev->addr_len = 0; | |
1428 | dev->flags = IFF_NOARP; | |
1429 | } | |
1430 | ||
2d07dc79 JL |
1431 | err = register_netdevice(dev); |
1432 | if (err) | |
1433 | return err; | |
1434 | ||
e305ac6c | 1435 | list_add(&geneve->next, &gn->geneve_list); |
e305ac6c PS |
1436 | return 0; |
1437 | } | |
1438 | ||
9b4437a5 | 1439 | static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port) |
1440 | { | |
1441 | memset(info, 0, sizeof(*info)); | |
1442 | info->key.tp_dst = htons(dst_port); | |
1443 | } | |
1444 | ||
c5ebc440 GM |
1445 | static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[], |
1446 | struct netlink_ext_ack *extack, | |
9e06e859 | 1447 | struct geneve_config *cfg, bool changelink) |
e305ac6c | 1448 | { |
9e06e859 | 1449 | struct ip_tunnel_info *info = &cfg->info; |
c5ebc440 GM |
1450 | int attrtype; |
1451 | ||
1452 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) { | |
1453 | NL_SET_ERR_MSG(extack, | |
1454 | "Cannot specify both IPv4 and IPv6 Remote addresses"); | |
8ed66f0e | 1455 | return -EINVAL; |
c5ebc440 | 1456 | } |
8ed66f0e JL |
1457 | |
1458 | if (data[IFLA_GENEVE_REMOTE]) { | |
c5ebc440 GM |
1459 | if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) { |
1460 | attrtype = IFLA_GENEVE_REMOTE; | |
1461 | goto change_notsup; | |
1462 | } | |
5b861f6b GM |
1463 | |
1464 | info->key.u.ipv4.dst = | |
8ed66f0e | 1465 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); |
9b4437a5 | 1466 | |
842841ec | 1467 | if (ipv4_is_multicast(info->key.u.ipv4.dst)) { |
c5ebc440 GM |
1468 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE], |
1469 | "Remote IPv4 address cannot be Multicast"); | |
9b4437a5 | 1470 | return -EINVAL; |
1471 | } | |
8ed66f0e JL |
1472 | } |
1473 | ||
1474 | if (data[IFLA_GENEVE_REMOTE6]) { | |
4c52a889 | 1475 | #if IS_ENABLED(CONFIG_IPV6) |
c5ebc440 GM |
1476 | if (changelink && (ip_tunnel_info_af(info) == AF_INET)) { |
1477 | attrtype = IFLA_GENEVE_REMOTE6; | |
1478 | goto change_notsup; | |
1479 | } | |
5b861f6b GM |
1480 | |
1481 | info->mode = IP_TUNNEL_INFO_IPV6; | |
1482 | info->key.u.ipv6.dst = | |
8ed66f0e JL |
1483 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); |
1484 | ||
5b861f6b | 1485 | if (ipv6_addr_type(&info->key.u.ipv6.dst) & |
8ed66f0e | 1486 | IPV6_ADDR_LINKLOCAL) { |
c5ebc440 GM |
1487 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
1488 | "Remote IPv6 address cannot be link-local"); | |
8ed66f0e JL |
1489 | return -EINVAL; |
1490 | } | |
5b861f6b | 1491 | if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) { |
c5ebc440 GM |
1492 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
1493 | "Remote IPv6 address cannot be Multicast"); | |
9b4437a5 | 1494 | return -EINVAL; |
1495 | } | |
5b861f6b | 1496 | info->key.tun_flags |= TUNNEL_CSUM; |
9e06e859 | 1497 | cfg->use_udp6_rx_checksums = true; |
9b4437a5 | 1498 | #else |
c5ebc440 GM |
1499 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
1500 | "IPv6 support not enabled in the kernel"); | |
9b4437a5 | 1501 | return -EPFNOSUPPORT; |
1502 | #endif | |
8ed66f0e JL |
1503 | } |
1504 | ||
9b4437a5 | 1505 | if (data[IFLA_GENEVE_ID]) { |
1506 | __u32 vni; | |
1507 | __u8 tvni[3]; | |
5b861f6b | 1508 | __be64 tunid; |
9b4437a5 | 1509 | |
e277de5f | 1510 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
9b4437a5 | 1511 | tvni[0] = (vni & 0x00ff0000) >> 16; |
1512 | tvni[1] = (vni & 0x0000ff00) >> 8; | |
1513 | tvni[2] = vni & 0x000000ff; | |
e305ac6c | 1514 | |
5b861f6b | 1515 | tunid = vni_to_tunnel_id(tvni); |
c5ebc440 GM |
1516 | if (changelink && (tunid != info->key.tun_id)) { |
1517 | attrtype = IFLA_GENEVE_ID; | |
1518 | goto change_notsup; | |
1519 | } | |
5b861f6b | 1520 | info->key.tun_id = tunid; |
9b4437a5 | 1521 | } |
5b861f6b | 1522 | |
a97d97ba HL |
1523 | if (data[IFLA_GENEVE_TTL_INHERIT]) { |
1524 | if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT])) | |
9e06e859 | 1525 | cfg->ttl_inherit = true; |
a97d97ba | 1526 | else |
9e06e859 | 1527 | cfg->ttl_inherit = false; |
a97d97ba | 1528 | } else if (data[IFLA_GENEVE_TTL]) { |
5b861f6b | 1529 | info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
9e06e859 | 1530 | cfg->ttl_inherit = false; |
a97d97ba | 1531 | } |
52d0d404 | 1532 | |
d8951125 | 1533 | if (data[IFLA_GENEVE_TOS]) |
5b861f6b | 1534 | info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
d8951125 | 1535 | |
a025fb5f | 1536 | if (data[IFLA_GENEVE_DF]) |
9e06e859 | 1537 | cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]); |
a025fb5f | 1538 | |
9b4437a5 | 1539 | if (data[IFLA_GENEVE_LABEL]) { |
5b861f6b | 1540 | info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & |
9b4437a5 | 1541 | IPV6_FLOWLABEL_MASK; |
c5ebc440 GM |
1542 | if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) { |
1543 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL], | |
1544 | "Label attribute only applies for IPv6 Geneve devices"); | |
9b4437a5 | 1545 | return -EINVAL; |
c5ebc440 | 1546 | } |
9b4437a5 | 1547 | } |
8eb3b995 | 1548 | |
5b861f6b | 1549 | if (data[IFLA_GENEVE_PORT]) { |
c5ebc440 GM |
1550 | if (changelink) { |
1551 | attrtype = IFLA_GENEVE_PORT; | |
1552 | goto change_notsup; | |
1553 | } | |
5b861f6b GM |
1554 | info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]); |
1555 | } | |
2d07dc79 | 1556 | |
5b861f6b | 1557 | if (data[IFLA_GENEVE_COLLECT_METADATA]) { |
c5ebc440 GM |
1558 | if (changelink) { |
1559 | attrtype = IFLA_GENEVE_COLLECT_METADATA; | |
1560 | goto change_notsup; | |
1561 | } | |
9e06e859 | 1562 | cfg->collect_md = true; |
5b861f6b | 1563 | } |
2d07dc79 | 1564 | |
5b861f6b | 1565 | if (data[IFLA_GENEVE_UDP_CSUM]) { |
c5ebc440 GM |
1566 | if (changelink) { |
1567 | attrtype = IFLA_GENEVE_UDP_CSUM; | |
1568 | goto change_notsup; | |
1569 | } | |
5b861f6b GM |
1570 | if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
1571 | info->key.tun_flags |= TUNNEL_CSUM; | |
1572 | } | |
abe492b4 | 1573 | |
5b861f6b | 1574 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) { |
f9094b76 | 1575 | #if IS_ENABLED(CONFIG_IPV6) |
c5ebc440 GM |
1576 | if (changelink) { |
1577 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX; | |
1578 | goto change_notsup; | |
1579 | } | |
5b861f6b GM |
1580 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) |
1581 | info->key.tun_flags &= ~TUNNEL_CSUM; | |
f9094b76 HL |
1582 | #else |
1583 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX], | |
1584 | "IPv6 support not enabled in the kernel"); | |
1585 | return -EPFNOSUPPORT; | |
1586 | #endif | |
5b861f6b | 1587 | } |
abe492b4 | 1588 | |
5b861f6b | 1589 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) { |
f9094b76 | 1590 | #if IS_ENABLED(CONFIG_IPV6) |
c5ebc440 GM |
1591 | if (changelink) { |
1592 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX; | |
1593 | goto change_notsup; | |
1594 | } | |
5b861f6b | 1595 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) |
9e06e859 | 1596 | cfg->use_udp6_rx_checksums = false; |
f9094b76 HL |
1597 | #else |
1598 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX], | |
1599 | "IPv6 support not enabled in the kernel"); | |
1600 | return -EPFNOSUPPORT; | |
1601 | #endif | |
5b861f6b GM |
1602 | } |
1603 | ||
435fe1c0 EB |
1604 | if (data[IFLA_GENEVE_INNER_PROTO_INHERIT]) { |
1605 | if (changelink) { | |
1606 | attrtype = IFLA_GENEVE_INNER_PROTO_INHERIT; | |
1607 | goto change_notsup; | |
1608 | } | |
1609 | cfg->inner_proto_inherit = true; | |
1610 | } | |
1611 | ||
5b861f6b | 1612 | return 0; |
c5ebc440 GM |
1613 | change_notsup: |
1614 | NL_SET_ERR_MSG_ATTR(extack, data[attrtype], | |
435fe1c0 | 1615 | "Changing VNI, Port, endpoint IP address family, external, inner_proto_inherit, and UDP checksum attributes are not supported"); |
c5ebc440 | 1616 | return -EOPNOTSUPP; |
5b861f6b GM |
1617 | } |
1618 | ||
c40e89fd AK |
1619 | static void geneve_link_config(struct net_device *dev, |
1620 | struct ip_tunnel_info *info, struct nlattr *tb[]) | |
1621 | { | |
1622 | struct geneve_dev *geneve = netdev_priv(dev); | |
1623 | int ldev_mtu = 0; | |
1624 | ||
1625 | if (tb[IFLA_MTU]) { | |
1626 | geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
1627 | return; | |
1628 | } | |
1629 | ||
1630 | switch (ip_tunnel_info_af(info)) { | |
1631 | case AF_INET: { | |
1632 | struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst }; | |
1633 | struct rtable *rt = ip_route_output_key(geneve->net, &fl4); | |
1634 | ||
1635 | if (!IS_ERR(rt) && rt->dst.dev) { | |
1636 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN; | |
1637 | ip_rt_put(rt); | |
1638 | } | |
1639 | break; | |
1640 | } | |
1641 | #if IS_ENABLED(CONFIG_IPV6) | |
1642 | case AF_INET6: { | |
c0a47e44 HL |
1643 | struct rt6_info *rt; |
1644 | ||
1645 | if (!__in6_dev_get(dev)) | |
1646 | break; | |
1647 | ||
1648 | rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0, | |
1649 | NULL, 0); | |
c40e89fd AK |
1650 | |
1651 | if (rt && rt->dst.dev) | |
1652 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN; | |
1653 | ip6_rt_put(rt); | |
1654 | break; | |
1655 | } | |
1656 | #endif | |
1657 | } | |
1658 | ||
1659 | if (ldev_mtu <= 0) | |
1660 | return; | |
1661 | ||
1662 | geneve_change_mtu(dev, ldev_mtu - info->options_len); | |
1663 | } | |
1664 | ||
5b861f6b GM |
1665 | static int geneve_newlink(struct net *net, struct net_device *dev, |
1666 | struct nlattr *tb[], struct nlattr *data[], | |
1667 | struct netlink_ext_ack *extack) | |
1668 | { | |
9e06e859 SD |
1669 | struct geneve_config cfg = { |
1670 | .df = GENEVE_DF_UNSET, | |
1671 | .use_udp6_rx_checksums = false, | |
1672 | .ttl_inherit = false, | |
1673 | .collect_md = false, | |
1674 | }; | |
5b861f6b GM |
1675 | int err; |
1676 | ||
9e06e859 SD |
1677 | init_tnl_info(&cfg.info, GENEVE_UDP_PORT); |
1678 | err = geneve_nl2info(tb, data, extack, &cfg, false); | |
5b861f6b GM |
1679 | if (err) |
1680 | return err; | |
abe492b4 | 1681 | |
9e06e859 | 1682 | err = geneve_configure(net, dev, extack, &cfg); |
c40e89fd AK |
1683 | if (err) |
1684 | return err; | |
1685 | ||
9e06e859 | 1686 | geneve_link_config(dev, &cfg.info, tb); |
c40e89fd AK |
1687 | |
1688 | return 0; | |
2d07dc79 JL |
1689 | } |
1690 | ||
5b861f6b GM |
1691 | /* Quiesces the geneve device data path for both TX and RX. |
1692 | * | |
1693 | * On transmit geneve checks for non-NULL geneve_sock before it proceeds. | |
1694 | * So, if we set that socket to NULL under RCU and wait for synchronize_net() | |
1695 | * to complete for the existing set of in-flight packets to be transmitted, | |
1696 | * then we would have quiesced the transmit data path. All the future packets | |
1697 | * will get dropped until we unquiesce the data path. | |
1698 | * | |
1699 | * On receive geneve dereference the geneve_sock stashed in the socket. So, | |
1700 | * if we set that to NULL under RCU and wait for synchronize_net() to | |
1701 | * complete, then we would have quiesced the receive data path. | |
1702 | */ | |
1703 | static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4, | |
1704 | struct geneve_sock **gs6) | |
1705 | { | |
1706 | *gs4 = rtnl_dereference(geneve->sock4); | |
1707 | rcu_assign_pointer(geneve->sock4, NULL); | |
1708 | if (*gs4) | |
1709 | rcu_assign_sk_user_data((*gs4)->sock->sk, NULL); | |
1710 | #if IS_ENABLED(CONFIG_IPV6) | |
1711 | *gs6 = rtnl_dereference(geneve->sock6); | |
1712 | rcu_assign_pointer(geneve->sock6, NULL); | |
1713 | if (*gs6) | |
1714 | rcu_assign_sk_user_data((*gs6)->sock->sk, NULL); | |
1715 | #else | |
1716 | *gs6 = NULL; | |
1717 | #endif | |
1718 | synchronize_net(); | |
1719 | } | |
1720 | ||
1721 | /* Resumes the geneve device data path for both TX and RX. */ | |
1722 | static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4, | |
1723 | struct geneve_sock __maybe_unused *gs6) | |
1724 | { | |
1725 | rcu_assign_pointer(geneve->sock4, gs4); | |
1726 | if (gs4) | |
1727 | rcu_assign_sk_user_data(gs4->sock->sk, gs4); | |
1728 | #if IS_ENABLED(CONFIG_IPV6) | |
1729 | rcu_assign_pointer(geneve->sock6, gs6); | |
1730 | if (gs6) | |
1731 | rcu_assign_sk_user_data(gs6->sock->sk, gs6); | |
1732 | #endif | |
1733 | synchronize_net(); | |
1734 | } | |
1735 | ||
1736 | static int geneve_changelink(struct net_device *dev, struct nlattr *tb[], | |
1737 | struct nlattr *data[], | |
1738 | struct netlink_ext_ack *extack) | |
1739 | { | |
1740 | struct geneve_dev *geneve = netdev_priv(dev); | |
1741 | struct geneve_sock *gs4, *gs6; | |
9e06e859 | 1742 | struct geneve_config cfg; |
5b861f6b GM |
1743 | int err; |
1744 | ||
1745 | /* If the geneve device is configured for metadata (or externally | |
1746 | * controlled, for example, OVS), then nothing can be changed. | |
1747 | */ | |
9e06e859 | 1748 | if (geneve->cfg.collect_md) |
5b861f6b GM |
1749 | return -EOPNOTSUPP; |
1750 | ||
1751 | /* Start with the existing info. */ | |
9e06e859 SD |
1752 | memcpy(&cfg, &geneve->cfg, sizeof(cfg)); |
1753 | err = geneve_nl2info(tb, data, extack, &cfg, true); | |
5b861f6b GM |
1754 | if (err) |
1755 | return err; | |
1756 | ||
9e06e859 SD |
1757 | if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) { |
1758 | dst_cache_reset(&cfg.info.dst_cache); | |
1759 | geneve_link_config(dev, &cfg.info, tb); | |
c40e89fd | 1760 | } |
5b861f6b GM |
1761 | |
1762 | geneve_quiesce(geneve, &gs4, &gs6); | |
9e06e859 | 1763 | memcpy(&geneve->cfg, &cfg, sizeof(cfg)); |
5b861f6b GM |
1764 | geneve_unquiesce(geneve, gs4, gs6); |
1765 | ||
1766 | return 0; | |
1767 | } | |
1768 | ||
2d07dc79 JL |
1769 | static void geneve_dellink(struct net_device *dev, struct list_head *head) |
1770 | { | |
1771 | struct geneve_dev *geneve = netdev_priv(dev); | |
1772 | ||
2d07dc79 JL |
1773 | list_del(&geneve->next); |
1774 | unregister_netdevice_queue(dev, head); | |
1775 | } | |
1776 | ||
1777 | static size_t geneve_get_size(const struct net_device *dev) | |
1778 | { | |
1779 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ | |
8ed66f0e | 1780 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
8760ce58 | 1781 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
d8951125 | 1782 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
a025fb5f | 1783 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */ |
8eb3b995 | 1784 | nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ |
7bbe33ff | 1785 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
e305ac6c | 1786 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
abe492b4 TH |
1787 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
1788 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ | |
1789 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ | |
52d0d404 | 1790 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */ |
36c2e31a | 1791 | nla_total_size(0) + /* IFLA_GENEVE_INNER_PROTO_INHERIT */ |
2d07dc79 JL |
1792 | 0; |
1793 | } | |
1794 | ||
1795 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1796 | { | |
1797 | struct geneve_dev *geneve = netdev_priv(dev); | |
9e06e859 SD |
1798 | struct ip_tunnel_info *info = &geneve->cfg.info; |
1799 | bool ttl_inherit = geneve->cfg.ttl_inherit; | |
1800 | bool metadata = geneve->cfg.collect_md; | |
9b4437a5 | 1801 | __u8 tmp_vni[3]; |
2d07dc79 JL |
1802 | __u32 vni; |
1803 | ||
9b4437a5 | 1804 | tunnel_id_to_vni(info->key.tun_id, tmp_vni); |
1805 | vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2]; | |
2d07dc79 JL |
1806 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) |
1807 | goto nla_put_failure; | |
1808 | ||
fd7eafd0 | 1809 | if (!metadata && ip_tunnel_info_af(info) == AF_INET) { |
8ed66f0e | 1810 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, |
9b4437a5 | 1811 | info->key.u.ipv4.dst)) |
1812 | goto nla_put_failure; | |
9b4437a5 | 1813 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
1814 | !!(info->key.tun_flags & TUNNEL_CSUM))) | |
8ed66f0e | 1815 | goto nla_put_failure; |
9b4437a5 | 1816 | |
8ed66f0e | 1817 | #if IS_ENABLED(CONFIG_IPV6) |
fd7eafd0 | 1818 | } else if (!metadata) { |
8ed66f0e | 1819 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, |
9b4437a5 | 1820 | &info->key.u.ipv6.dst)) |
1821 | goto nla_put_failure; | |
9b4437a5 | 1822 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
1823 | !(info->key.tun_flags & TUNNEL_CSUM))) | |
1824 | goto nla_put_failure; | |
11387fe4 | 1825 | #endif |
fd7eafd0 | 1826 | } |
2d07dc79 | 1827 | |
9b4437a5 | 1828 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) || |
1829 | nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) || | |
1830 | nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label)) | |
8760ce58 JL |
1831 | goto nla_put_failure; |
1832 | ||
9e06e859 | 1833 | if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df)) |
a025fb5f SB |
1834 | goto nla_put_failure; |
1835 | ||
9b4437a5 | 1836 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst)) |
cd7918b3 PS |
1837 | goto nla_put_failure; |
1838 | ||
fd7eafd0 | 1839 | if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) |
f9094b76 | 1840 | goto nla_put_failure; |
fd7eafd0 | 1841 | |
f9094b76 | 1842 | #if IS_ENABLED(CONFIG_IPV6) |
fd7eafd0 | 1843 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, |
9e06e859 | 1844 | !geneve->cfg.use_udp6_rx_checksums)) |
fd7eafd0 | 1845 | goto nla_put_failure; |
f9094b76 | 1846 | #endif |
fd7eafd0 | 1847 | |
52d0d404 HL |
1848 | if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit)) |
1849 | goto nla_put_failure; | |
1850 | ||
435fe1c0 EB |
1851 | if (geneve->cfg.inner_proto_inherit && |
1852 | nla_put_flag(skb, IFLA_GENEVE_INNER_PROTO_INHERIT)) | |
1853 | goto nla_put_failure; | |
1854 | ||
2d07dc79 JL |
1855 | return 0; |
1856 | ||
1857 | nla_put_failure: | |
1858 | return -EMSGSIZE; | |
1859 | } | |
1860 | ||
1861 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { | |
1862 | .kind = "geneve", | |
1863 | .maxtype = IFLA_GENEVE_MAX, | |
1864 | .policy = geneve_policy, | |
1865 | .priv_size = sizeof(struct geneve_dev), | |
1866 | .setup = geneve_setup, | |
1867 | .validate = geneve_validate, | |
1868 | .newlink = geneve_newlink, | |
5b861f6b | 1869 | .changelink = geneve_changelink, |
2d07dc79 JL |
1870 | .dellink = geneve_dellink, |
1871 | .get_size = geneve_get_size, | |
1872 | .fill_info = geneve_fill_info, | |
1873 | }; | |
1874 | ||
e305ac6c PS |
1875 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
1876 | u8 name_assign_type, u16 dst_port) | |
1877 | { | |
1878 | struct nlattr *tb[IFLA_MAX + 1]; | |
1879 | struct net_device *dev; | |
106da663 | 1880 | LIST_HEAD(list_kill); |
e305ac6c | 1881 | int err; |
9e06e859 SD |
1882 | struct geneve_config cfg = { |
1883 | .df = GENEVE_DF_UNSET, | |
1884 | .use_udp6_rx_checksums = true, | |
1885 | .ttl_inherit = false, | |
1886 | .collect_md = true, | |
1887 | }; | |
e305ac6c PS |
1888 | |
1889 | memset(tb, 0, sizeof(tb)); | |
1890 | dev = rtnl_create_link(net, name, name_assign_type, | |
d0522f1c | 1891 | &geneve_link_ops, tb, NULL); |
e305ac6c PS |
1892 | if (IS_ERR(dev)) |
1893 | return dev; | |
1894 | ||
9e06e859 SD |
1895 | init_tnl_info(&cfg.info, dst_port); |
1896 | err = geneve_configure(net, dev, NULL, &cfg); | |
106da663 ND |
1897 | if (err) { |
1898 | free_netdev(dev); | |
1899 | return ERR_PTR(err); | |
1900 | } | |
7e059158 DW |
1901 | |
1902 | /* openvswitch users expect packet sizes to be unrestricted, | |
1903 | * so set the largest MTU we can. | |
1904 | */ | |
91572088 | 1905 | err = geneve_change_mtu(dev, IP_MAX_MTU); |
7e059158 DW |
1906 | if (err) |
1907 | goto err; | |
1908 | ||
41009481 ND |
1909 | err = rtnl_configure_link(dev, NULL); |
1910 | if (err < 0) | |
1911 | goto err; | |
1912 | ||
e305ac6c | 1913 | return dev; |
9b4437a5 | 1914 | err: |
106da663 ND |
1915 | geneve_dellink(dev, &list_kill); |
1916 | unregister_netdevice_many(&list_kill); | |
7e059158 | 1917 | return ERR_PTR(err); |
e305ac6c PS |
1918 | } |
1919 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); | |
1920 | ||
681e683f HFS |
1921 | static int geneve_netdevice_event(struct notifier_block *unused, |
1922 | unsigned long event, void *ptr) | |
1923 | { | |
1924 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
1925 | ||
dedc33e7 JK |
1926 | if (event == NETDEV_UDP_TUNNEL_PUSH_INFO) |
1927 | geneve_offload_rx_ports(dev, true); | |
1928 | else if (event == NETDEV_UDP_TUNNEL_DROP_INFO) | |
1929 | geneve_offload_rx_ports(dev, false); | |
681e683f HFS |
1930 | |
1931 | return NOTIFY_DONE; | |
1932 | } | |
1933 | ||
1934 | static struct notifier_block geneve_notifier_block __read_mostly = { | |
1935 | .notifier_call = geneve_netdevice_event, | |
1936 | }; | |
1937 | ||
2d07dc79 JL |
1938 | static __net_init int geneve_init_net(struct net *net) |
1939 | { | |
1940 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
2d07dc79 JL |
1941 | |
1942 | INIT_LIST_HEAD(&gn->geneve_list); | |
371bd106 | 1943 | INIT_LIST_HEAD(&gn->sock_list); |
2d07dc79 JL |
1944 | return 0; |
1945 | } | |
1946 | ||
2843a253 | 1947 | static void geneve_destroy_tunnels(struct net *net, struct list_head *head) |
2d07dc79 JL |
1948 | { |
1949 | struct geneve_net *gn = net_generic(net, geneve_net_id); | |
1950 | struct geneve_dev *geneve, *next; | |
1951 | struct net_device *dev, *aux; | |
2d07dc79 JL |
1952 | |
1953 | /* gather any geneve devices that were moved into this ns */ | |
1954 | for_each_netdev_safe(net, dev, aux) | |
1955 | if (dev->rtnl_link_ops == &geneve_link_ops) | |
2843a253 | 1956 | unregister_netdevice_queue(dev, head); |
2d07dc79 JL |
1957 | |
1958 | /* now gather any other geneve devices that were created in this ns */ | |
1959 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { | |
1960 | /* If geneve->dev is in the same netns, it was already added | |
1961 | * to the list by the previous loop. | |
1962 | */ | |
1963 | if (!net_eq(dev_net(geneve->dev), net)) | |
2843a253 | 1964 | unregister_netdevice_queue(geneve->dev, head); |
2d07dc79 | 1965 | } |
2843a253 HY |
1966 | } |
1967 | ||
1968 | static void __net_exit geneve_exit_batch_net(struct list_head *net_list) | |
1969 | { | |
1970 | struct net *net; | |
1971 | LIST_HEAD(list); | |
1972 | ||
1973 | rtnl_lock(); | |
1974 | list_for_each_entry(net, net_list, exit_list) | |
1975 | geneve_destroy_tunnels(net, &list); | |
1976 | ||
2d07dc79 JL |
1977 | /* unregister the devices gathered above */ |
1978 | unregister_netdevice_many(&list); | |
1979 | rtnl_unlock(); | |
0fda7600 FW |
1980 | |
1981 | list_for_each_entry(net, net_list, exit_list) { | |
1982 | const struct geneve_net *gn = net_generic(net, geneve_net_id); | |
1983 | ||
1984 | WARN_ON_ONCE(!list_empty(&gn->sock_list)); | |
1985 | } | |
2d07dc79 JL |
1986 | } |
1987 | ||
1988 | static struct pernet_operations geneve_net_ops = { | |
1989 | .init = geneve_init_net, | |
2843a253 | 1990 | .exit_batch = geneve_exit_batch_net, |
2d07dc79 JL |
1991 | .id = &geneve_net_id, |
1992 | .size = sizeof(struct geneve_net), | |
1993 | }; | |
1994 | ||
1995 | static int __init geneve_init_module(void) | |
1996 | { | |
1997 | int rc; | |
1998 | ||
1999 | rc = register_pernet_subsys(&geneve_net_ops); | |
2000 | if (rc) | |
2001 | goto out1; | |
2002 | ||
681e683f | 2003 | rc = register_netdevice_notifier(&geneve_notifier_block); |
2d07dc79 JL |
2004 | if (rc) |
2005 | goto out2; | |
2006 | ||
681e683f HFS |
2007 | rc = rtnl_link_register(&geneve_link_ops); |
2008 | if (rc) | |
2009 | goto out3; | |
2010 | ||
2d07dc79 | 2011 | return 0; |
681e683f HFS |
2012 | out3: |
2013 | unregister_netdevice_notifier(&geneve_notifier_block); | |
2d07dc79 JL |
2014 | out2: |
2015 | unregister_pernet_subsys(&geneve_net_ops); | |
2016 | out1: | |
2017 | return rc; | |
2018 | } | |
2019 | late_initcall(geneve_init_module); | |
2020 | ||
2021 | static void __exit geneve_cleanup_module(void) | |
2022 | { | |
2023 | rtnl_link_unregister(&geneve_link_ops); | |
681e683f | 2024 | unregister_netdevice_notifier(&geneve_notifier_block); |
2d07dc79 JL |
2025 | unregister_pernet_subsys(&geneve_net_ops); |
2026 | } | |
2027 | module_exit(geneve_cleanup_module); | |
2028 | ||
2029 | MODULE_LICENSE("GPL"); | |
2030 | MODULE_VERSION(GENEVE_NETDEV_VER); | |
2031 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); | |
2032 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); | |
2033 | MODULE_ALIAS_RTNL_LINK("geneve"); |