blob: 36444de701cd9f4f17e132d7b25413032374e0a0 [file] [log] [blame]
John W. Linville2d07dc72015-05-13 12:57:30 -04001/*
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>
John W. Linville2d07dc72015-05-13 12:57:30 -040015#include <linux/etherdevice.h>
16#include <linux/hash.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070017#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070018#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040019#include <net/rtnetlink.h>
20#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070021#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040022
23#define GENEVE_NETDEV_VER "0.6"
24
25#define GENEVE_UDP_PORT 6081
26
27#define GENEVE_N_VID (1u << 24)
28#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
29
30#define VNI_HASH_BITS 10
31#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
32
33static bool log_ecn_error = true;
34module_param(log_ecn_error, bool, 0644);
35MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
36
Pravin B Shelar371bd102015-08-26 23:46:54 -070037#define GENEVE_VER 0
38#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
Alexey Kodanev5edbea62018-04-19 15:42:30 +030039#define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
40#define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
Pravin B Shelar371bd102015-08-26 23:46:54 -070041
John W. Linville2d07dc72015-05-13 12:57:30 -040042/* per-network namespace private data for this module */
43struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070044 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070045 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040046};
47
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030048static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070049
Jiri Benc4b4c21f2017-07-02 19:00:58 +020050struct geneve_dev_node {
51 struct hlist_node hlist;
52 struct geneve_dev *geneve;
53};
54
John W. Linville2d07dc72015-05-13 12:57:30 -040055/* Pseudo network device */
56struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020057 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
58#if IS_ENABLED(CONFIG_IPV6)
59 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
60#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040061 struct net *net; /* netns for packet i/o */
62 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelar9b4437a2016-11-21 11:02:58 -080063 struct ip_tunnel_info info;
pravin shelarfceb9c32016-10-28 09:59:16 -070064 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040065#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070066 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040067#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040068 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070069 struct gro_cells gro_cells;
pravin shelar9b4437a2016-11-21 11:02:58 -080070 bool collect_md;
71 bool use_udp6_rx_checksums;
John W. Linville2d07dc72015-05-13 12:57:30 -040072};
73
Pravin B Shelar371bd102015-08-26 23:46:54 -070074struct geneve_sock {
75 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070076 struct list_head list;
77 struct socket *sock;
78 struct rcu_head rcu;
79 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070080 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070081};
John W. Linville2d07dc72015-05-13 12:57:30 -040082
83static inline __u32 geneve_net_vni_hash(u8 vni[3])
84{
85 __u32 vnid;
86
87 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
88 return hash_32(vnid, VNI_HASH_BITS);
89}
90
Pravin B Shelare305ac62015-08-26 23:46:52 -070091static __be64 vni_to_tunnel_id(const __u8 *vni)
92{
93#ifdef __BIG_ENDIAN
94 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
95#else
96 return (__force __be64)(((__force u64)vni[0] << 40) |
97 ((__force u64)vni[1] << 48) |
98 ((__force u64)vni[2] << 56));
99#endif
100}
101
pravin shelar9b4437a2016-11-21 11:02:58 -0800102/* Convert 64 bit tunnel ID to 24 bit VNI. */
103static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
104{
105#ifdef __BIG_ENDIAN
106 vni[0] = (__force __u8)(tun_id >> 16);
107 vni[1] = (__force __u8)(tun_id >> 8);
108 vni[2] = (__force __u8)tun_id;
109#else
110 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
111 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
112 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
113#endif
114}
115
pravin shelar2e0b26e2016-11-21 11:03:01 -0800116static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
117{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800118 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800119}
120
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100121static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
122{
123 return gs->sock->sk->sk_family;
124}
125
Pravin B Shelar66d47002015-08-26 23:46:55 -0700126static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700127 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400128{
John W. Linville2d07dc72015-05-13 12:57:30 -0400129 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200130 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 __u32 hash;
132
John W. Linville2d07dc72015-05-13 12:57:30 -0400133 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700134 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700135 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200136 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
137 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
138 addr == node->geneve->info.key.u.ipv4.dst)
139 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400140 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700141 return NULL;
142}
143
John W. Linville8ed66f02015-10-26 17:01:44 -0400144#if IS_ENABLED(CONFIG_IPV6)
145static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
146 struct in6_addr addr6, u8 vni[])
147{
148 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200149 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400150 __u32 hash;
151
152 /* Find the device for this VNI */
153 hash = geneve_net_vni_hash(vni);
154 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200155 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
156 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
157 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst))
158 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400159 }
160 return NULL;
161}
162#endif
163
Pravin B Shelar371bd102015-08-26 23:46:54 -0700164static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
165{
166 return (struct genevehdr *)(udp_hdr(skb) + 1);
167}
168
Jiri Benc9fc47542016-02-18 11:22:50 +0100169static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
170 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700171{
John W. Linville8ed66f02015-10-26 17:01:44 -0400172 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800173 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700174
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100175 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100176 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800177 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100178
John W. Linville8ed66f02015-10-26 17:01:44 -0400179 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700180
John W. Linville8ed66f02015-10-26 17:01:44 -0400181 if (gs->collect_md) {
182 vni = zero_vni;
183 addr = 0;
184 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100185 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400186 addr = iph->saddr;
187 }
188
Jiri Benc9fc47542016-02-18 11:22:50 +0100189 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400190#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100191 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800192 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100193 struct ipv6hdr *ip6h;
194 struct in6_addr addr6;
195
John W. Linville8ed66f02015-10-26 17:01:44 -0400196 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
197
198 if (gs->collect_md) {
199 vni = zero_vni;
200 addr6 = zero_addr6;
201 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100202 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400203 addr6 = ip6h->saddr;
204 }
205
Jiri Benc9fc47542016-02-18 11:22:50 +0100206 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400207#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700208 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100209 return NULL;
210}
211
212/* geneve receive/decap routine */
213static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
214 struct sk_buff *skb)
215{
216 struct genevehdr *gnvh = geneve_hdr(skb);
217 struct metadata_dst *tun_dst = NULL;
218 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700219 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100220 int err = 0;
221 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400222
Pravin B Shelar371bd102015-08-26 23:46:54 -0700223 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700224 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700225
226 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
227 (gnvh->oam ? TUNNEL_OAM : 0) |
228 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
229
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100230 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700231 vni_to_tunnel_id(gnvh->vni),
232 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700233 if (!tun_dst) {
234 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700235 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700236 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700237 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700238 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700239 gnvh->options, gnvh->opt_len * 4,
240 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700241 } else {
242 /* Drop packets w/ critical options,
243 * since we don't support any...
244 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700245 if (gnvh->critical) {
246 geneve->dev->stats.rx_frame_errors++;
247 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700248 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700249 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700250 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400251
252 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400253 skb->protocol = eth_type_trans(skb, geneve->dev);
254 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
255
Pravin B Shelare305ac62015-08-26 23:46:52 -0700256 if (tun_dst)
257 skb_dst_set(skb, &tun_dst->dst);
258
John W. Linville2d07dc72015-05-13 12:57:30 -0400259 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700260 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
261 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400262 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700263 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400264
Jiri Benc9fc47542016-02-18 11:22:50 +0100265 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400266 skb_reset_network_header(skb);
267
Jiri Benc9fc47542016-02-18 11:22:50 +0100268 if (geneve_get_sk_family(gs) == AF_INET)
269 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400270#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100271 else
272 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400273#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400274
275 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400276 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100277 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400278 net_info_ratelimited("non-ECT from %pI4 "
279 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100280 &((struct iphdr *)oiph)->saddr,
281 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400282#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100283 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400284 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100285 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400286#endif
287 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400288 if (err > 1) {
289 ++geneve->dev->stats.rx_frame_errors;
290 ++geneve->dev->stats.rx_errors;
291 goto drop;
292 }
293 }
294
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700295 len = skb->len;
296 err = gro_cells_receive(&geneve->gro_cells, skb);
297 if (likely(err == NET_RX_SUCCESS)) {
298 stats = this_cpu_ptr(geneve->dev->tstats);
299 u64_stats_update_begin(&stats->syncp);
300 stats->rx_packets++;
301 stats->rx_bytes += len;
302 u64_stats_update_end(&stats->syncp);
303 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400304 return;
305drop:
306 /* Consume bad packet */
307 kfree_skb(skb);
308}
309
310/* Setup stats when device is created */
311static int geneve_init(struct net_device *dev)
312{
Jesse Gross8e816df2015-08-28 16:54:40 -0700313 struct geneve_dev *geneve = netdev_priv(dev);
314 int err;
315
John W. Linville2d07dc72015-05-13 12:57:30 -0400316 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
317 if (!dev->tstats)
318 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700319
320 err = gro_cells_init(&geneve->gro_cells, dev);
321 if (err) {
322 free_percpu(dev->tstats);
323 return err;
324 }
325
pravin shelar9b4437a2016-11-21 11:02:58 -0800326 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100327 if (err) {
328 free_percpu(dev->tstats);
329 gro_cells_destroy(&geneve->gro_cells);
330 return err;
331 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400332 return 0;
333}
334
335static void geneve_uninit(struct net_device *dev)
336{
Jesse Gross8e816df2015-08-28 16:54:40 -0700337 struct geneve_dev *geneve = netdev_priv(dev);
338
pravin shelar9b4437a2016-11-21 11:02:58 -0800339 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700340 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400341 free_percpu(dev->tstats);
342}
343
Pravin B Shelar371bd102015-08-26 23:46:54 -0700344/* Callback from net/ipv4/udp.c to receive packets */
345static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
346{
347 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100348 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700349 struct geneve_sock *gs;
350 int opts_len;
351
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700352 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700353 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200354 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700355
356 /* Return packets with reserved bits set */
357 geneveh = geneve_hdr(skb);
358 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200359 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700360
361 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200362 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700363
Jiri Benc9fc47542016-02-18 11:22:50 +0100364 gs = rcu_dereference_sk_user_data(sk);
365 if (!gs)
366 goto drop;
367
368 geneve = geneve_lookup_skb(gs, skb);
369 if (!geneve)
370 goto drop;
371
Pravin B Shelar371bd102015-08-26 23:46:54 -0700372 opts_len = geneveh->opt_len * 4;
373 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100374 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700375 !net_eq(geneve->net, dev_net(geneve->dev)))) {
376 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700377 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700378 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700379
Jiri Benc9fc47542016-02-18 11:22:50 +0100380 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700381 return 0;
382
383drop:
384 /* Consume bad packet */
385 kfree_skb(skb);
386 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700387}
388
389static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800390 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700391{
392 struct socket *sock;
393 struct udp_port_cfg udp_conf;
394 int err;
395
396 memset(&udp_conf, 0, sizeof(udp_conf));
397
398 if (ipv6) {
399 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400400 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800401 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700402 } else {
403 udp_conf.family = AF_INET;
404 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
405 }
406
407 udp_conf.local_udp_port = port;
408
409 /* Open UDP socket */
410 err = udp_sock_create(net, &udp_conf, &sock);
411 if (err < 0)
412 return ERR_PTR(err);
413
414 return sock;
415}
416
Pravin B Shelar371bd102015-08-26 23:46:54 -0700417static int geneve_hlen(struct genevehdr *gh)
418{
419 return sizeof(*gh) + gh->opt_len * 4;
420}
421
David Millerd4546c22018-06-24 14:13:49 +0900422static struct sk_buff *geneve_gro_receive(struct sock *sk,
423 struct list_head *head,
424 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700425{
David Millerd4546c22018-06-24 14:13:49 +0900426 struct sk_buff *pp = NULL;
427 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700428 struct genevehdr *gh, *gh2;
429 unsigned int hlen, gh_len, off_gnv;
430 const struct packet_offload *ptype;
431 __be16 type;
432 int flush = 1;
433
434 off_gnv = skb_gro_offset(skb);
435 hlen = off_gnv + sizeof(*gh);
436 gh = skb_gro_header_fast(skb, off_gnv);
437 if (skb_gro_header_hard(skb, hlen)) {
438 gh = skb_gro_header_slow(skb, hlen, off_gnv);
439 if (unlikely(!gh))
440 goto out;
441 }
442
443 if (gh->ver != GENEVE_VER || gh->oam)
444 goto out;
445 gh_len = geneve_hlen(gh);
446
447 hlen = off_gnv + gh_len;
448 if (skb_gro_header_hard(skb, hlen)) {
449 gh = skb_gro_header_slow(skb, hlen, off_gnv);
450 if (unlikely(!gh))
451 goto out;
452 }
453
David Millerd4546c22018-06-24 14:13:49 +0900454 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700455 if (!NAPI_GRO_CB(p)->same_flow)
456 continue;
457
458 gh2 = (struct genevehdr *)(p->data + off_gnv);
459 if (gh->opt_len != gh2->opt_len ||
460 memcmp(gh, gh2, gh_len)) {
461 NAPI_GRO_CB(p)->same_flow = 0;
462 continue;
463 }
464 }
465
466 type = gh->proto_type;
467
468 rcu_read_lock();
469 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800470 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700471 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700472
473 skb_gro_pull(skb, gh_len);
474 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200475 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800476 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700477
478out_unlock:
479 rcu_read_unlock();
480out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200481 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700482
483 return pp;
484}
485
Tom Herbert4a0090a2016-04-05 08:22:55 -0700486static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
487 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700488{
489 struct genevehdr *gh;
490 struct packet_offload *ptype;
491 __be16 type;
492 int gh_len;
493 int err = -ENOSYS;
494
Pravin B Shelar371bd102015-08-26 23:46:54 -0700495 gh = (struct genevehdr *)(skb->data + nhoff);
496 gh_len = geneve_hlen(gh);
497 type = gh->proto_type;
498
499 rcu_read_lock();
500 ptype = gro_find_complete_by_type(type);
501 if (ptype)
502 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
503
504 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700505
506 skb_set_inner_mac_header(skb, nhoff + gh_len);
507
Pravin B Shelar371bd102015-08-26 23:46:54 -0700508 return err;
509}
510
511/* Create new listen socket if needed */
512static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800513 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700514{
515 struct geneve_net *gn = net_generic(net, geneve_net_id);
516 struct geneve_sock *gs;
517 struct socket *sock;
518 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700519 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700520
521 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
522 if (!gs)
523 return ERR_PTR(-ENOMEM);
524
pravin shelar9b4437a2016-11-21 11:02:58 -0800525 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700526 if (IS_ERR(sock)) {
527 kfree(gs);
528 return ERR_CAST(sock);
529 }
530
531 gs->sock = sock;
532 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700533 for (h = 0; h < VNI_HASH_SIZE; ++h)
534 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700535
536 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700537 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700538
539 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700540 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700541 tunnel_cfg.sk_user_data = gs;
542 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700543 tunnel_cfg.gro_receive = geneve_gro_receive;
544 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700545 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
546 tunnel_cfg.encap_destroy = NULL;
547 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700548 list_add(&gs->list, &gn->sock_list);
549 return gs;
550}
551
John W. Linville8ed66f02015-10-26 17:01:44 -0400552static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700553{
John W. Linville8ed66f02015-10-26 17:01:44 -0400554 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700555 return;
556
557 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700558 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700559 udp_tunnel_sock_release(gs->sock);
560 kfree_rcu(gs, rcu);
561}
562
John W. Linville8ed66f02015-10-26 17:01:44 -0400563static void geneve_sock_release(struct geneve_dev *geneve)
564{
pravin shelarfceb9c32016-10-28 09:59:16 -0700565 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400566#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700567 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
568
569 rcu_assign_pointer(geneve->sock6, NULL);
570#endif
571
572 rcu_assign_pointer(geneve->sock4, NULL);
573 synchronize_net();
574
575 __geneve_sock_release(gs4);
576#if IS_ENABLED(CONFIG_IPV6)
577 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400578#endif
579}
580
Pravin B Shelar371bd102015-08-26 23:46:54 -0700581static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400582 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700583 __be16 dst_port)
584{
585 struct geneve_sock *gs;
586
587 list_for_each_entry(gs, &gn->sock_list, list) {
588 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100589 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590 return gs;
591 }
592 }
593 return NULL;
594}
595
John W. Linville8ed66f02015-10-26 17:01:44 -0400596static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400597{
John W. Linville2d07dc72015-05-13 12:57:30 -0400598 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700599 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200600 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400601 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800602 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700603 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400604
pravin shelar9b4437a2016-11-21 11:02:58 -0800605 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606 if (gs) {
607 gs->refcnt++;
608 goto out;
609 }
610
pravin shelar9b4437a2016-11-21 11:02:58 -0800611 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
612 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400613 if (IS_ERR(gs))
614 return PTR_ERR(gs);
615
Pravin B Shelar371bd102015-08-26 23:46:54 -0700616out:
617 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400618#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200619 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700620 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200621 node = &geneve->hlist6;
622 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400623#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200624 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700625 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200626 node = &geneve->hlist4;
627 }
628 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700629
pravin shelar9b4437a2016-11-21 11:02:58 -0800630 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
631 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200632 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400633 return 0;
634}
635
John W. Linville8ed66f02015-10-26 17:01:44 -0400636static int geneve_open(struct net_device *dev)
637{
638 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400639 bool metadata = geneve->collect_md;
Jiri Benc36bd44b2019-02-28 14:56:04 +0100640 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400641 int ret = 0;
642
Jiri Benc36bd44b2019-02-28 14:56:04 +0100643 ipv6 = geneve->info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
644 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400645#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc36bd44b2019-02-28 14:56:04 +0100646 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400647 ret = geneve_sock_add(geneve, true);
Jiri Benc36bd44b2019-02-28 14:56:04 +0100648 if (ret < 0 && ret != -EAFNOSUPPORT)
649 ipv4 = false;
650 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400651#endif
Jiri Benc36bd44b2019-02-28 14:56:04 +0100652 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400653 ret = geneve_sock_add(geneve, false);
654 if (ret < 0)
655 geneve_sock_release(geneve);
656
657 return ret;
658}
659
John W. Linville2d07dc72015-05-13 12:57:30 -0400660static int geneve_stop(struct net_device *dev)
661{
662 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400663
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200664 hlist_del_init_rcu(&geneve->hlist4.hlist);
665#if IS_ENABLED(CONFIG_IPV6)
666 hlist_del_init_rcu(&geneve->hlist6.hlist);
667#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400668 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400669 return 0;
670}
671
John W. Linville8ed66f02015-10-26 17:01:44 -0400672static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800673 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400674{
675 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800676 geneveh->opt_len = info->options_len / 4;
677 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
678 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400679 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800680 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400681 geneveh->proto_type = htons(ETH_P_TEB);
682 geneveh->rsvd2 = 0;
683
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700684 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
685 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400686}
687
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800688static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
689 const struct ip_tunnel_info *info,
690 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700691{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800692 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700693 struct genevehdr *gnvh;
694 int min_headroom;
695 int err;
696
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800697 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 skb_scrub_packet(skb, xnet);
699
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800700 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
701 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400702 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400703 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400704 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400705
Alexander Duyckaed069d2016-04-14 15:33:37 -0400706 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300707 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400708 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400709
Johannes Bergd58ff352017-06-16 14:29:23 +0200710 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800711 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400712 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
713 return 0;
714
715free_dst:
716 dst_release(dst);
717 return err;
718}
John W. Linville8ed66f02015-10-26 17:01:44 -0400719
720static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
721 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700722 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400723 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800724 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700725{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100726 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700727 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100728 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700729 struct rtable *rt = NULL;
730 __u8 tos;
731
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700732 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700733 return ERR_PTR(-EIO);
734
Pravin B Shelare305ac62015-08-26 23:46:52 -0700735 memset(fl4, 0, sizeof(*fl4));
736 fl4->flowi4_mark = skb->mark;
737 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800738 fl4->daddr = info->key.u.ipv4.dst;
739 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700740
pravin shelar9b4437a2016-11-21 11:02:58 -0800741 tos = info->key.tos;
742 if ((tos == 1) && !geneve->collect_md) {
743 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
744 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100745 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800746 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100747
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800748 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100749 if (use_cache) {
750 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
751 if (rt)
752 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700753 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754 rt = ip_route_output_key(geneve->net, fl4);
755 if (IS_ERR(rt)) {
756 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700757 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700758 }
759 if (rt->dst.dev == dev) { /* is this necessary? */
760 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700761 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700762 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700763 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100764 if (use_cache)
765 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700766 return rt;
767}
768
John W. Linville8ed66f02015-10-26 17:01:44 -0400769#if IS_ENABLED(CONFIG_IPV6)
770static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
771 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700772 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400773 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800774 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400775{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100776 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400777 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400778 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100779 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400780 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400781
pravin shelarfceb9c32016-10-28 09:59:16 -0700782 if (!gs6)
783 return ERR_PTR(-EIO);
784
John W. Linville8ed66f02015-10-26 17:01:44 -0400785 memset(fl6, 0, sizeof(*fl6));
786 fl6->flowi6_mark = skb->mark;
787 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800788 fl6->daddr = info->key.u.ipv6.dst;
789 fl6->saddr = info->key.u.ipv6.src;
790 prio = info->key.tos;
791 if ((prio == 1) && !geneve->collect_md) {
792 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
793 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100794 }
795
pravin shelar9b4437a2016-11-21 11:02:58 -0800796 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
797 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800798 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100799 if (use_cache) {
800 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
801 if (dst)
802 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400803 }
Sabrina Dubroca5dd68352019-12-04 15:35:53 +0100804 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
805 NULL);
806 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400807 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
808 return ERR_PTR(-ENETUNREACH);
809 }
810 if (dst->dev == dev) { /* is this necessary? */
811 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
812 dst_release(dst);
813 return ERR_PTR(-ELOOP);
814 }
815
Paolo Abeni468dfff2016-02-12 15:43:58 +0100816 if (use_cache)
817 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400818 return dst;
819}
820#endif
821
pravin shelar9b4437a2016-11-21 11:02:58 -0800822static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800823 struct geneve_dev *geneve,
824 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700825{
pravin shelar9b4437a2016-11-21 11:02:58 -0800826 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
827 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
828 const struct ip_tunnel_key *key = &info->key;
829 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400830 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400831 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700832 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700833 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800834 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400835
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700836 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800837 if (IS_ERR(rt))
838 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700839
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200840 skb_tunnel_check_pmtu(skb, &rt->dst,
841 GENEVE_IPV4_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800842
Pravin B Shelar371bd102015-08-26 23:46:54 -0700843 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 if (geneve->collect_md) {
845 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700846 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700847 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800848 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
849 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400850 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800851 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
852
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800853 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800854 if (unlikely(err))
855 return err;
856
Pravin B Shelar039f5062015-12-24 14:34:54 -0800857 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800858 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800859 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800860 !(info->key.tun_flags & TUNNEL_CSUM));
861 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400862}
863
John W. Linville8ed66f02015-10-26 17:01:44 -0400864#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800865static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800866 struct geneve_dev *geneve,
867 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400868{
pravin shelar9b4437a2016-11-21 11:02:58 -0800869 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
870 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
871 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400872 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400873 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400874 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800876 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400877
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700878 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800879 if (IS_ERR(dst))
880 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400881
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200882 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800883
John W. Linville8ed66f02015-10-26 17:01:44 -0400884 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800885 if (geneve->collect_md) {
886 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400887 ttl = key->ttl;
888 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100889 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800890 ip_hdr(skb), skb);
891 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400892 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800893 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800894 if (unlikely(err))
895 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100896
Pravin B Shelar039f5062015-12-24 14:34:54 -0800897 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800898 &fl6.saddr, &fl6.daddr, prio, ttl,
899 info->key.label, sport, geneve->info.key.tp_dst,
900 !(info->key.tun_flags & TUNNEL_CSUM));
901 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400902}
903#endif
904
905static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
906{
907 struct geneve_dev *geneve = netdev_priv(dev);
908 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800909 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400910
pravin shelar9b4437a2016-11-21 11:02:58 -0800911 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400912 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800913 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
914 err = -EINVAL;
915 netdev_dbg(dev, "no tunnel metadata\n");
916 goto tx_error;
917 }
918 } else {
919 info = &geneve->info;
920 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400921
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800922 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400923#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800924 if (info->mode & IP_TUNNEL_INFO_IPV6)
925 err = geneve6_xmit_skb(skb, dev, geneve, info);
926 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400927#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800928 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800929 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800930
931 if (likely(!err))
932 return NETDEV_TX_OK;
933tx_error:
934 dev_kfree_skb(skb);
935
936 if (err == -ELOOP)
937 dev->stats.collisions++;
938 else if (err == -ENETUNREACH)
939 dev->stats.tx_carrier_errors++;
940
941 dev->stats.tx_errors++;
942 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400943}
944
Jarod Wilson91572082016-10-20 13:55:20 -0400945static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000946{
Jarod Wilson91572082016-10-20 13:55:20 -0400947 if (new_mtu > dev->max_mtu)
948 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +0300949 else if (new_mtu < dev->min_mtu)
950 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000951
David Wragg55e5bfb2016-02-10 00:05:57 +0000952 dev->mtu = new_mtu;
953 return 0;
954}
955
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700956static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
957{
958 struct ip_tunnel_info *info = skb_tunnel_info(skb);
959 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700960
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400961 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800962 struct rtable *rt;
963 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700964 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -0800965
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700966 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400967 if (IS_ERR(rt))
968 return PTR_ERR(rt);
969
970 ip_rt_put(rt);
971 info->key.u.ipv4.src = fl4.saddr;
972#if IS_ENABLED(CONFIG_IPV6)
973 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800974 struct dst_entry *dst;
975 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700976 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -0800977
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700978 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400979 if (IS_ERR(dst))
980 return PTR_ERR(dst);
981
982 dst_release(dst);
983 info->key.u.ipv6.src = fl6.saddr;
984#endif
985 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700986 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400987 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700988
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700989 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
990 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800991 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700992 return 0;
993}
994
John W. Linville2d07dc72015-05-13 12:57:30 -0400995static const struct net_device_ops geneve_netdev_ops = {
996 .ndo_init = geneve_init,
997 .ndo_uninit = geneve_uninit,
998 .ndo_open = geneve_open,
999 .ndo_stop = geneve_stop,
1000 .ndo_start_xmit = geneve_xmit,
1001 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001002 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001003 .ndo_validate_addr = eth_validate_addr,
1004 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001005 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001006};
1007
1008static void geneve_get_drvinfo(struct net_device *dev,
1009 struct ethtool_drvinfo *drvinfo)
1010{
1011 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1012 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1013}
1014
1015static const struct ethtool_ops geneve_ethtool_ops = {
1016 .get_drvinfo = geneve_get_drvinfo,
1017 .get_link = ethtool_op_get_link,
1018};
1019
1020/* Info for udev, that this is a virtual tunnel endpoint */
1021static struct device_type geneve_type = {
1022 .name = "geneve",
1023};
1024
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001025/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001026 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001027 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001028 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001029static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001030{
1031 struct net *net = dev_net(dev);
1032 struct geneve_net *gn = net_generic(net, geneve_net_id);
1033 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001034
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001035 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001036 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1037 if (push) {
1038 udp_tunnel_push_rx_port(dev, gs->sock,
1039 UDP_TUNNEL_TYPE_GENEVE);
1040 } else {
1041 udp_tunnel_drop_rx_port(dev, gs->sock,
1042 UDP_TUNNEL_TYPE_GENEVE);
1043 }
1044 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001045 rcu_read_unlock();
1046}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001047
John W. Linville2d07dc72015-05-13 12:57:30 -04001048/* Initialize the device structure. */
1049static void geneve_setup(struct net_device *dev)
1050{
1051 ether_setup(dev);
1052
1053 dev->netdev_ops = &geneve_netdev_ops;
1054 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001055 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001056
1057 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1058
John W. Linville2d07dc72015-05-13 12:57:30 -04001059 dev->features |= NETIF_F_LLTX;
1060 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1061 dev->features |= NETIF_F_RXCSUM;
1062 dev->features |= NETIF_F_GSO_SOFTWARE;
1063
John W. Linville2d07dc72015-05-13 12:57:30 -04001064 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1065 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001066
Jarod Wilson91572082016-10-20 13:55:20 -04001067 /* MTU range: 68 - (something less than 65535) */
1068 dev->min_mtu = ETH_MIN_MTU;
1069 /* The max_mtu calculation does not take account of GENEVE
1070 * options, to avoid excluding potentially valid
1071 * configurations. This will be further reduced by IPvX hdr size.
1072 */
1073 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1074
John W. Linville2d07dc72015-05-13 12:57:30 -04001075 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001076 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001077 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001078 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001079}
1080
1081static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1082 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1083 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001084 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001085 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001086 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001087 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001088 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001089 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001090 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1091 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1092 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001093};
1094
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001095static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1096 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001097{
1098 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001099 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1100 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1101 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001102 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001103 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001104
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001105 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1106 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1107 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001108 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001109 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001110 }
1111
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001112 if (!data) {
1113 NL_SET_ERR_MSG(extack,
1114 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001115 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001116 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001117
1118 if (data[IFLA_GENEVE_ID]) {
1119 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1120
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001121 if (vni >= GENEVE_N_VID) {
1122 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1123 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001124 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001125 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001126 }
1127
1128 return 0;
1129}
1130
Pravin B Shelar371bd102015-08-26 23:46:54 -07001131static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001132 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001133 bool *tun_on_same_port,
1134 bool *tun_collect_md)
1135{
pravin shelar9b4437a2016-11-21 11:02:58 -08001136 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001137
1138 *tun_on_same_port = false;
1139 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001140 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001141 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001142 *tun_collect_md = geneve->collect_md;
1143 *tun_on_same_port = true;
1144 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001145 if (info->key.tun_id == geneve->info.key.tun_id &&
1146 info->key.tp_dst == geneve->info.key.tp_dst &&
1147 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001148 t = geneve;
1149 }
1150 return t;
1151}
1152
pravin shelar9b4437a2016-11-21 11:02:58 -08001153static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1154{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001155 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1156 info->key.ttl || info->key.label || info->key.tp_src ||
1157 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001158}
1159
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001160static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1161 struct ip_tunnel_info *b)
1162{
1163 if (ip_tunnel_info_af(a) == AF_INET)
1164 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1165 else
1166 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1167}
1168
Pravin B Shelare305ac62015-08-26 23:46:52 -07001169static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001170 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001171 const struct ip_tunnel_info *info,
1172 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001173{
1174 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001175 struct geneve_dev *t, *geneve = netdev_priv(dev);
1176 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001177 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001178
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001179 if (metadata && !is_tnl_info_zero(info)) {
1180 NL_SET_ERR_MSG(extack,
1181 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001182 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001183 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001184
1185 geneve->net = net;
1186 geneve->dev = dev;
1187
pravin shelar9b4437a2016-11-21 11:02:58 -08001188 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001189 if (t)
1190 return -EBUSY;
1191
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001192 /* make enough headroom for basic scenario */
1193 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001194 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001195 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001196 dev->max_mtu -= sizeof(struct iphdr);
1197 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001198 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001199 dev->max_mtu -= sizeof(struct ipv6hdr);
1200 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001201 dev->needed_headroom = encap_len + ETH_HLEN;
1202
Pravin B Shelar371bd102015-08-26 23:46:54 -07001203 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001204 if (tun_on_same_port) {
1205 NL_SET_ERR_MSG(extack,
1206 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001207 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001208 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001209 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001210 if (tun_collect_md) {
1211 NL_SET_ERR_MSG(extack,
1212 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001213 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001214 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001215 }
1216
pravin shelar9b4437a2016-11-21 11:02:58 -08001217 dst_cache_reset(&geneve->info.dst_cache);
1218 geneve->info = *info;
1219 geneve->collect_md = metadata;
1220 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001221
John W. Linville2d07dc72015-05-13 12:57:30 -04001222 err = register_netdevice(dev);
1223 if (err)
1224 return err;
1225
1226 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001227 return 0;
1228}
1229
pravin shelar9b4437a2016-11-21 11:02:58 -08001230static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1231{
1232 memset(info, 0, sizeof(*info));
1233 info->key.tp_dst = htons(dst_port);
1234}
1235
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001236static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1237 struct netlink_ext_ack *extack,
1238 struct ip_tunnel_info *info, bool *metadata,
1239 bool *use_udp6_rx_checksums, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001240{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001241 int attrtype;
1242
1243 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1244 NL_SET_ERR_MSG(extack,
1245 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001246 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001247 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001248
1249 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001250 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1251 attrtype = IFLA_GENEVE_REMOTE;
1252 goto change_notsup;
1253 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001254
1255 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001256 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001257
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001258 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001259 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1260 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001261 return -EINVAL;
1262 }
1263 }
1264
pravin shelar9b4437a2016-11-21 11:02:58 -08001265 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001266#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001267 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1268 attrtype = IFLA_GENEVE_REMOTE6;
1269 goto change_notsup;
1270 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001271
1272 info->mode = IP_TUNNEL_INFO_IPV6;
1273 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001274 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001275
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001276 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001277 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001278 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1279 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001280 return -EINVAL;
1281 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001282 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001283 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1284 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001285 return -EINVAL;
1286 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001287 info->key.tun_flags |= TUNNEL_CSUM;
1288 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001289#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001290 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1291 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001292 return -EPFNOSUPPORT;
1293#endif
1294 }
1295
1296 if (data[IFLA_GENEVE_ID]) {
1297 __u32 vni;
1298 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001299 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001300
1301 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1302 tvni[0] = (vni & 0x00ff0000) >> 16;
1303 tvni[1] = (vni & 0x0000ff00) >> 8;
1304 tvni[2] = vni & 0x000000ff;
1305
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001306 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001307 if (changelink && (tunid != info->key.tun_id)) {
1308 attrtype = IFLA_GENEVE_ID;
1309 goto change_notsup;
1310 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001311 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001312 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001313
Pravin B Shelare305ac62015-08-26 23:46:52 -07001314 if (data[IFLA_GENEVE_TTL])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001315 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001316
1317 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001318 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001319
pravin shelar9b4437a2016-11-21 11:02:58 -08001320 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001321 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001322 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001323 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1324 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1325 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001326 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001327 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001328 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001329
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001330 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001331 if (changelink) {
1332 attrtype = IFLA_GENEVE_PORT;
1333 goto change_notsup;
1334 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001335 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1336 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001337
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001338 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001339 if (changelink) {
1340 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1341 goto change_notsup;
1342 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001343 *metadata = true;
1344 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001345
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001346 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001347 if (changelink) {
1348 attrtype = IFLA_GENEVE_UDP_CSUM;
1349 goto change_notsup;
1350 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001351 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1352 info->key.tun_flags |= TUNNEL_CSUM;
1353 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001354
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001355 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001356#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001357 if (changelink) {
1358 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1359 goto change_notsup;
1360 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001361 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1362 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001363#else
1364 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1365 "IPv6 support not enabled in the kernel");
1366 return -EPFNOSUPPORT;
1367#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001368 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001369
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001370 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001371#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001372 if (changelink) {
1373 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1374 goto change_notsup;
1375 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001376 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1377 *use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001378#else
1379 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1380 "IPv6 support not enabled in the kernel");
1381 return -EPFNOSUPPORT;
1382#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001383 }
1384
1385 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001386change_notsup:
1387 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1388 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1389 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001390}
1391
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001392static void geneve_link_config(struct net_device *dev,
1393 struct ip_tunnel_info *info, struct nlattr *tb[])
1394{
1395 struct geneve_dev *geneve = netdev_priv(dev);
1396 int ldev_mtu = 0;
1397
1398 if (tb[IFLA_MTU]) {
1399 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1400 return;
1401 }
1402
1403 switch (ip_tunnel_info_af(info)) {
1404 case AF_INET: {
1405 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1406 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1407
1408 if (!IS_ERR(rt) && rt->dst.dev) {
1409 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1410 ip_rt_put(rt);
1411 }
1412 break;
1413 }
1414#if IS_ENABLED(CONFIG_IPV6)
1415 case AF_INET6: {
Hangbin Liuc6472332019-02-07 18:36:10 +08001416 struct rt6_info *rt;
1417
1418 if (!__in6_dev_get(dev))
1419 break;
1420
1421 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1422 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001423
1424 if (rt && rt->dst.dev)
1425 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1426 ip6_rt_put(rt);
1427 break;
1428 }
1429#endif
1430 }
1431
1432 if (ldev_mtu <= 0)
1433 return;
1434
1435 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1436}
1437
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001438static int geneve_newlink(struct net *net, struct net_device *dev,
1439 struct nlattr *tb[], struct nlattr *data[],
1440 struct netlink_ext_ack *extack)
1441{
1442 bool use_udp6_rx_checksums = false;
1443 struct ip_tunnel_info info;
1444 bool metadata = false;
1445 int err;
1446
1447 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001448 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001449 &use_udp6_rx_checksums, false);
1450 if (err)
1451 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001452
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001453 err = geneve_configure(net, dev, extack, &info, metadata,
1454 use_udp6_rx_checksums);
1455 if (err)
1456 return err;
1457
1458 geneve_link_config(dev, &info, tb);
1459
1460 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001461}
1462
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001463/* Quiesces the geneve device data path for both TX and RX.
1464 *
1465 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1466 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1467 * to complete for the existing set of in-flight packets to be transmitted,
1468 * then we would have quiesced the transmit data path. All the future packets
1469 * will get dropped until we unquiesce the data path.
1470 *
1471 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1472 * if we set that to NULL under RCU and wait for synchronize_net() to
1473 * complete, then we would have quiesced the receive data path.
1474 */
1475static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1476 struct geneve_sock **gs6)
1477{
1478 *gs4 = rtnl_dereference(geneve->sock4);
1479 rcu_assign_pointer(geneve->sock4, NULL);
1480 if (*gs4)
1481 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1482#if IS_ENABLED(CONFIG_IPV6)
1483 *gs6 = rtnl_dereference(geneve->sock6);
1484 rcu_assign_pointer(geneve->sock6, NULL);
1485 if (*gs6)
1486 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1487#else
1488 *gs6 = NULL;
1489#endif
1490 synchronize_net();
1491}
1492
1493/* Resumes the geneve device data path for both TX and RX. */
1494static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1495 struct geneve_sock __maybe_unused *gs6)
1496{
1497 rcu_assign_pointer(geneve->sock4, gs4);
1498 if (gs4)
1499 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1500#if IS_ENABLED(CONFIG_IPV6)
1501 rcu_assign_pointer(geneve->sock6, gs6);
1502 if (gs6)
1503 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1504#endif
1505 synchronize_net();
1506}
1507
1508static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1509 struct nlattr *data[],
1510 struct netlink_ext_ack *extack)
1511{
1512 struct geneve_dev *geneve = netdev_priv(dev);
1513 struct geneve_sock *gs4, *gs6;
1514 struct ip_tunnel_info info;
1515 bool metadata;
1516 bool use_udp6_rx_checksums;
1517 int err;
1518
1519 /* If the geneve device is configured for metadata (or externally
1520 * controlled, for example, OVS), then nothing can be changed.
1521 */
1522 if (geneve->collect_md)
1523 return -EOPNOTSUPP;
1524
1525 /* Start with the existing info. */
1526 memcpy(&info, &geneve->info, sizeof(info));
1527 metadata = geneve->collect_md;
1528 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001529 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001530 &use_udp6_rx_checksums, true);
1531 if (err)
1532 return err;
1533
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001534 if (!geneve_dst_addr_equal(&geneve->info, &info)) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001535 dst_cache_reset(&info.dst_cache);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001536 geneve_link_config(dev, &info, tb);
1537 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001538
1539 geneve_quiesce(geneve, &gs4, &gs6);
1540 geneve->info = info;
1541 geneve->collect_md = metadata;
1542 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
1543 geneve_unquiesce(geneve, gs4, gs6);
1544
1545 return 0;
1546}
1547
John W. Linville2d07dc72015-05-13 12:57:30 -04001548static void geneve_dellink(struct net_device *dev, struct list_head *head)
1549{
1550 struct geneve_dev *geneve = netdev_priv(dev);
1551
John W. Linville2d07dc72015-05-13 12:57:30 -04001552 list_del(&geneve->next);
1553 unregister_netdevice_queue(dev, head);
1554}
1555
1556static size_t geneve_get_size(const struct net_device *dev)
1557{
1558 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001559 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001560 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001561 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001562 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001563 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001564 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001565 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1566 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1567 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001568 0;
1569}
1570
1571static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1572{
1573 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001574 struct ip_tunnel_info *info = &geneve->info;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001575 bool metadata = geneve->collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001576 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001577 __u32 vni;
1578
pravin shelar9b4437a2016-11-21 11:02:58 -08001579 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1580 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001581 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1582 goto nla_put_failure;
1583
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001584 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001585 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001586 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001587 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001588 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1589 !!(info->key.tun_flags & TUNNEL_CSUM)))
1590 goto nla_put_failure;
1591
John W. Linville8ed66f02015-10-26 17:01:44 -04001592#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001593 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001594 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001595 &info->key.u.ipv6.dst))
1596 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001597 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1598 !(info->key.tun_flags & TUNNEL_CSUM)))
1599 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001600#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001601 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001602
pravin shelar9b4437a2016-11-21 11:02:58 -08001603 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1604 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1605 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001606 goto nla_put_failure;
1607
pravin shelar9b4437a2016-11-21 11:02:58 -08001608 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001609 goto nla_put_failure;
1610
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001611 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001612 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001613
Hangbin Liuf9094b72017-11-23 11:27:24 +08001614#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001615 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1616 !geneve->use_udp6_rx_checksums))
1617 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001618#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001619
John W. Linville2d07dc72015-05-13 12:57:30 -04001620 return 0;
1621
1622nla_put_failure:
1623 return -EMSGSIZE;
1624}
1625
1626static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1627 .kind = "geneve",
1628 .maxtype = IFLA_GENEVE_MAX,
1629 .policy = geneve_policy,
1630 .priv_size = sizeof(struct geneve_dev),
1631 .setup = geneve_setup,
1632 .validate = geneve_validate,
1633 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001634 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001635 .dellink = geneve_dellink,
1636 .get_size = geneve_get_size,
1637 .fill_info = geneve_fill_info,
1638};
1639
Pravin B Shelare305ac62015-08-26 23:46:52 -07001640struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1641 u8 name_assign_type, u16 dst_port)
1642{
1643 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001644 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001645 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001646 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001647 int err;
1648
1649 memset(tb, 0, sizeof(tb));
1650 dev = rtnl_create_link(net, name, name_assign_type,
1651 &geneve_link_ops, tb);
1652 if (IS_ERR(dev))
1653 return dev;
1654
pravin shelar9b4437a2016-11-21 11:02:58 -08001655 init_tnl_info(&info, dst_port);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001656 err = geneve_configure(net, dev, NULL, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001657 if (err) {
1658 free_netdev(dev);
1659 return ERR_PTR(err);
1660 }
David Wragg7e059152016-02-10 00:05:58 +00001661
1662 /* openvswitch users expect packet sizes to be unrestricted,
1663 * so set the largest MTU we can.
1664 */
Jarod Wilson91572082016-10-20 13:55:20 -04001665 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001666 if (err)
1667 goto err;
1668
Nicolas Dichtel41009482016-06-13 10:31:07 +02001669 err = rtnl_configure_link(dev, NULL);
1670 if (err < 0)
1671 goto err;
1672
Pravin B Shelare305ac62015-08-26 23:46:52 -07001673 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001674err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001675 geneve_dellink(dev, &list_kill);
1676 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001677 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001678}
1679EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1680
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001681static int geneve_netdevice_event(struct notifier_block *unused,
1682 unsigned long event, void *ptr)
1683{
1684 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1685
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001686 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001687 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001688 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001689 } else if (event == NETDEV_UNREGISTER) {
1690 geneve_offload_rx_ports(dev, false);
1691 } else if (event == NETDEV_REGISTER) {
1692 geneve_offload_rx_ports(dev, true);
1693 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001694
1695 return NOTIFY_DONE;
1696}
1697
1698static struct notifier_block geneve_notifier_block __read_mostly = {
1699 .notifier_call = geneve_netdevice_event,
1700};
1701
John W. Linville2d07dc72015-05-13 12:57:30 -04001702static __net_init int geneve_init_net(struct net *net)
1703{
1704 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001705
1706 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001707 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001708 return 0;
1709}
1710
Haishuang Yan2843a252017-12-16 17:54:50 +08001711static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001712{
1713 struct geneve_net *gn = net_generic(net, geneve_net_id);
1714 struct geneve_dev *geneve, *next;
1715 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001716
1717 /* gather any geneve devices that were moved into this ns */
1718 for_each_netdev_safe(net, dev, aux)
1719 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001720 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001721
1722 /* now gather any other geneve devices that were created in this ns */
1723 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1724 /* If geneve->dev is in the same netns, it was already added
1725 * to the list by the previous loop.
1726 */
1727 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001728 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001729 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001730}
1731
1732static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1733{
1734 struct net *net;
1735 LIST_HEAD(list);
1736
1737 rtnl_lock();
1738 list_for_each_entry(net, net_list, exit_list)
1739 geneve_destroy_tunnels(net, &list);
1740
John W. Linville2d07dc72015-05-13 12:57:30 -04001741 /* unregister the devices gathered above */
1742 unregister_netdevice_many(&list);
1743 rtnl_unlock();
Florian Westphal2c1a05e2020-03-14 08:18:42 +01001744
1745 list_for_each_entry(net, net_list, exit_list) {
1746 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1747
1748 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1749 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001750}
1751
1752static struct pernet_operations geneve_net_ops = {
1753 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001754 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001755 .id = &geneve_net_id,
1756 .size = sizeof(struct geneve_net),
1757};
1758
1759static int __init geneve_init_module(void)
1760{
1761 int rc;
1762
1763 rc = register_pernet_subsys(&geneve_net_ops);
1764 if (rc)
1765 goto out1;
1766
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001767 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001768 if (rc)
1769 goto out2;
1770
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001771 rc = rtnl_link_register(&geneve_link_ops);
1772 if (rc)
1773 goto out3;
1774
John W. Linville2d07dc72015-05-13 12:57:30 -04001775 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001776out3:
1777 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001778out2:
1779 unregister_pernet_subsys(&geneve_net_ops);
1780out1:
1781 return rc;
1782}
1783late_initcall(geneve_init_module);
1784
1785static void __exit geneve_cleanup_module(void)
1786{
1787 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001788 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001789 unregister_pernet_subsys(&geneve_net_ops);
1790}
1791module_exit(geneve_cleanup_module);
1792
1793MODULE_LICENSE("GPL");
1794MODULE_VERSION(GENEVE_NETDEV_VER);
1795MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1796MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1797MODULE_ALIAS_RTNL_LINK("geneve");