blob: 750eaa53bf0ce59429d524ba0658ad6f488a4ba0 [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,
239 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700240 } else {
241 /* Drop packets w/ critical options,
242 * since we don't support any...
243 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700244 if (gnvh->critical) {
245 geneve->dev->stats.rx_frame_errors++;
246 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700247 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700248 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700249 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400250
251 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400252 skb->protocol = eth_type_trans(skb, geneve->dev);
253 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
254
Pravin B Shelare305ac62015-08-26 23:46:52 -0700255 if (tun_dst)
256 skb_dst_set(skb, &tun_dst->dst);
257
John W. Linville2d07dc72015-05-13 12:57:30 -0400258 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700259 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
260 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400261 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700262 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400263
Jiri Benc9fc47542016-02-18 11:22:50 +0100264 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400265 skb_reset_network_header(skb);
266
Jiri Benc9fc47542016-02-18 11:22:50 +0100267 if (geneve_get_sk_family(gs) == AF_INET)
268 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400269#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100270 else
271 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400272#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400273
274 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400275 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100276 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400277 net_info_ratelimited("non-ECT from %pI4 "
278 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100279 &((struct iphdr *)oiph)->saddr,
280 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400281#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100282 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400283 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100284 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400285#endif
286 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400287 if (err > 1) {
288 ++geneve->dev->stats.rx_frame_errors;
289 ++geneve->dev->stats.rx_errors;
290 goto drop;
291 }
292 }
293
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700294 len = skb->len;
295 err = gro_cells_receive(&geneve->gro_cells, skb);
296 if (likely(err == NET_RX_SUCCESS)) {
297 stats = this_cpu_ptr(geneve->dev->tstats);
298 u64_stats_update_begin(&stats->syncp);
299 stats->rx_packets++;
300 stats->rx_bytes += len;
301 u64_stats_update_end(&stats->syncp);
302 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400303 return;
304drop:
305 /* Consume bad packet */
306 kfree_skb(skb);
307}
308
309/* Setup stats when device is created */
310static int geneve_init(struct net_device *dev)
311{
Jesse Gross8e816df2015-08-28 16:54:40 -0700312 struct geneve_dev *geneve = netdev_priv(dev);
313 int err;
314
John W. Linville2d07dc72015-05-13 12:57:30 -0400315 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
316 if (!dev->tstats)
317 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700318
319 err = gro_cells_init(&geneve->gro_cells, dev);
320 if (err) {
321 free_percpu(dev->tstats);
322 return err;
323 }
324
pravin shelar9b4437a2016-11-21 11:02:58 -0800325 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100326 if (err) {
327 free_percpu(dev->tstats);
328 gro_cells_destroy(&geneve->gro_cells);
329 return err;
330 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400331 return 0;
332}
333
334static void geneve_uninit(struct net_device *dev)
335{
Jesse Gross8e816df2015-08-28 16:54:40 -0700336 struct geneve_dev *geneve = netdev_priv(dev);
337
pravin shelar9b4437a2016-11-21 11:02:58 -0800338 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700339 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400340 free_percpu(dev->tstats);
341}
342
Pravin B Shelar371bd102015-08-26 23:46:54 -0700343/* Callback from net/ipv4/udp.c to receive packets */
344static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
345{
346 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100347 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700348 struct geneve_sock *gs;
349 int opts_len;
350
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700351 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700352 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200353 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700354
355 /* Return packets with reserved bits set */
356 geneveh = geneve_hdr(skb);
357 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200358 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700359
360 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200361 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700362
Jiri Benc9fc47542016-02-18 11:22:50 +0100363 gs = rcu_dereference_sk_user_data(sk);
364 if (!gs)
365 goto drop;
366
367 geneve = geneve_lookup_skb(gs, skb);
368 if (!geneve)
369 goto drop;
370
Pravin B Shelar371bd102015-08-26 23:46:54 -0700371 opts_len = geneveh->opt_len * 4;
372 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100373 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700374 !net_eq(geneve->net, dev_net(geneve->dev)))) {
375 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700376 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700377 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700378
Jiri Benc9fc47542016-02-18 11:22:50 +0100379 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700380 return 0;
381
382drop:
383 /* Consume bad packet */
384 kfree_skb(skb);
385 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700386}
387
388static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800389 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700390{
391 struct socket *sock;
392 struct udp_port_cfg udp_conf;
393 int err;
394
395 memset(&udp_conf, 0, sizeof(udp_conf));
396
397 if (ipv6) {
398 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400399 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800400 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700401 } else {
402 udp_conf.family = AF_INET;
403 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
404 }
405
406 udp_conf.local_udp_port = port;
407
408 /* Open UDP socket */
409 err = udp_sock_create(net, &udp_conf, &sock);
410 if (err < 0)
411 return ERR_PTR(err);
412
413 return sock;
414}
415
Pravin B Shelar371bd102015-08-26 23:46:54 -0700416static int geneve_hlen(struct genevehdr *gh)
417{
418 return sizeof(*gh) + gh->opt_len * 4;
419}
420
Tom Herbert4a0090a2016-04-05 08:22:55 -0700421static struct sk_buff **geneve_gro_receive(struct sock *sk,
422 struct sk_buff **head,
423 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700424{
425 struct sk_buff *p, **pp = NULL;
426 struct genevehdr *gh, *gh2;
427 unsigned int hlen, gh_len, off_gnv;
428 const struct packet_offload *ptype;
429 __be16 type;
430 int flush = 1;
431
432 off_gnv = skb_gro_offset(skb);
433 hlen = off_gnv + sizeof(*gh);
434 gh = skb_gro_header_fast(skb, off_gnv);
435 if (skb_gro_header_hard(skb, hlen)) {
436 gh = skb_gro_header_slow(skb, hlen, off_gnv);
437 if (unlikely(!gh))
438 goto out;
439 }
440
441 if (gh->ver != GENEVE_VER || gh->oam)
442 goto out;
443 gh_len = geneve_hlen(gh);
444
445 hlen = off_gnv + gh_len;
446 if (skb_gro_header_hard(skb, hlen)) {
447 gh = skb_gro_header_slow(skb, hlen, off_gnv);
448 if (unlikely(!gh))
449 goto out;
450 }
451
Pravin B Shelar371bd102015-08-26 23:46:54 -0700452 for (p = *head; p; p = p->next) {
453 if (!NAPI_GRO_CB(p)->same_flow)
454 continue;
455
456 gh2 = (struct genevehdr *)(p->data + off_gnv);
457 if (gh->opt_len != gh2->opt_len ||
458 memcmp(gh, gh2, gh_len)) {
459 NAPI_GRO_CB(p)->same_flow = 0;
460 continue;
461 }
462 }
463
464 type = gh->proto_type;
465
466 rcu_read_lock();
467 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800468 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700469 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700470
471 skb_gro_pull(skb, gh_len);
472 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200473 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800474 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700475
476out_unlock:
477 rcu_read_unlock();
478out:
479 NAPI_GRO_CB(skb)->flush |= flush;
480
481 return pp;
482}
483
Tom Herbert4a0090a2016-04-05 08:22:55 -0700484static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
485 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700486{
487 struct genevehdr *gh;
488 struct packet_offload *ptype;
489 __be16 type;
490 int gh_len;
491 int err = -ENOSYS;
492
Pravin B Shelar371bd102015-08-26 23:46:54 -0700493 gh = (struct genevehdr *)(skb->data + nhoff);
494 gh_len = geneve_hlen(gh);
495 type = gh->proto_type;
496
497 rcu_read_lock();
498 ptype = gro_find_complete_by_type(type);
499 if (ptype)
500 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
501
502 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700503
504 skb_set_inner_mac_header(skb, nhoff + gh_len);
505
Pravin B Shelar371bd102015-08-26 23:46:54 -0700506 return err;
507}
508
509/* Create new listen socket if needed */
510static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800511 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700512{
513 struct geneve_net *gn = net_generic(net, geneve_net_id);
514 struct geneve_sock *gs;
515 struct socket *sock;
516 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700517 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700518
519 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
520 if (!gs)
521 return ERR_PTR(-ENOMEM);
522
pravin shelar9b4437a2016-11-21 11:02:58 -0800523 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700524 if (IS_ERR(sock)) {
525 kfree(gs);
526 return ERR_CAST(sock);
527 }
528
529 gs->sock = sock;
530 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700531 for (h = 0; h < VNI_HASH_SIZE; ++h)
532 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700533
534 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700535 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700536
537 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700538 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700539 tunnel_cfg.sk_user_data = gs;
540 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700541 tunnel_cfg.gro_receive = geneve_gro_receive;
542 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700543 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
544 tunnel_cfg.encap_destroy = NULL;
545 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700546 list_add(&gs->list, &gn->sock_list);
547 return gs;
548}
549
John W. Linville8ed66f02015-10-26 17:01:44 -0400550static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700551{
John W. Linville8ed66f02015-10-26 17:01:44 -0400552 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700553 return;
554
555 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700556 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700557 udp_tunnel_sock_release(gs->sock);
558 kfree_rcu(gs, rcu);
559}
560
John W. Linville8ed66f02015-10-26 17:01:44 -0400561static void geneve_sock_release(struct geneve_dev *geneve)
562{
pravin shelarfceb9c32016-10-28 09:59:16 -0700563 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400564#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700565 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
566
567 rcu_assign_pointer(geneve->sock6, NULL);
568#endif
569
570 rcu_assign_pointer(geneve->sock4, NULL);
571 synchronize_net();
572
573 __geneve_sock_release(gs4);
574#if IS_ENABLED(CONFIG_IPV6)
575 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400576#endif
577}
578
Pravin B Shelar371bd102015-08-26 23:46:54 -0700579static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400580 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700581 __be16 dst_port)
582{
583 struct geneve_sock *gs;
584
585 list_for_each_entry(gs, &gn->sock_list, list) {
586 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100587 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700588 return gs;
589 }
590 }
591 return NULL;
592}
593
John W. Linville8ed66f02015-10-26 17:01:44 -0400594static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400595{
John W. Linville2d07dc72015-05-13 12:57:30 -0400596 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700597 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200598 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400599 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800600 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700601 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400602
pravin shelar9b4437a2016-11-21 11:02:58 -0800603 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700604 if (gs) {
605 gs->refcnt++;
606 goto out;
607 }
608
pravin shelar9b4437a2016-11-21 11:02:58 -0800609 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
610 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400611 if (IS_ERR(gs))
612 return PTR_ERR(gs);
613
Pravin B Shelar371bd102015-08-26 23:46:54 -0700614out:
615 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400616#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200617 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700618 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200619 node = &geneve->hlist6;
620 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400621#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200622 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700623 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200624 node = &geneve->hlist4;
625 }
626 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700627
pravin shelar9b4437a2016-11-21 11:02:58 -0800628 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
629 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200630 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400631 return 0;
632}
633
John W. Linville8ed66f02015-10-26 17:01:44 -0400634static int geneve_open(struct net_device *dev)
635{
636 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800637 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400638 bool metadata = geneve->collect_md;
639 int ret = 0;
640
John W. Linville8ed66f02015-10-26 17:01:44 -0400641#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400642 if (ipv6 || metadata)
643 ret = geneve_sock_add(geneve, true);
644#endif
645 if (!ret && (!ipv6 || metadata))
646 ret = geneve_sock_add(geneve, false);
647 if (ret < 0)
648 geneve_sock_release(geneve);
649
650 return ret;
651}
652
John W. Linville2d07dc72015-05-13 12:57:30 -0400653static int geneve_stop(struct net_device *dev)
654{
655 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400656
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200657 hlist_del_init_rcu(&geneve->hlist4.hlist);
658#if IS_ENABLED(CONFIG_IPV6)
659 hlist_del_init_rcu(&geneve->hlist6.hlist);
660#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400661 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400662 return 0;
663}
664
John W. Linville8ed66f02015-10-26 17:01:44 -0400665static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800666 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400667{
668 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800669 geneveh->opt_len = info->options_len / 4;
670 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
671 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400672 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800673 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400674 geneveh->proto_type = htons(ETH_P_TEB);
675 geneveh->rsvd2 = 0;
676
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800677 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400678}
679
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800680static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
681 const struct ip_tunnel_info *info,
682 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700683{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800684 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700685 struct genevehdr *gnvh;
686 int min_headroom;
687 int err;
688
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800689 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400690 skb_scrub_packet(skb, xnet);
691
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800692 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
693 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400695 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400696 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400697
Alexander Duyckaed069d2016-04-14 15:33:37 -0400698 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300699 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400700 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400701
Johannes Bergd58ff352017-06-16 14:29:23 +0200702 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800703 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400704 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
705 return 0;
706
707free_dst:
708 dst_release(dst);
709 return err;
710}
John W. Linville8ed66f02015-10-26 17:01:44 -0400711
712static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
713 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700714 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400715 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800716 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700717{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100718 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700719 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100720 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700721 struct rtable *rt = NULL;
722 __u8 tos;
723
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700724 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700725 return ERR_PTR(-EIO);
726
Pravin B Shelare305ac62015-08-26 23:46:52 -0700727 memset(fl4, 0, sizeof(*fl4));
728 fl4->flowi4_mark = skb->mark;
729 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800730 fl4->daddr = info->key.u.ipv4.dst;
731 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700732
pravin shelar9b4437a2016-11-21 11:02:58 -0800733 tos = info->key.tos;
734 if ((tos == 1) && !geneve->collect_md) {
735 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
736 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100737 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800738 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100739
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800740 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100741 if (use_cache) {
742 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
743 if (rt)
744 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700745 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700746 rt = ip_route_output_key(geneve->net, fl4);
747 if (IS_ERR(rt)) {
748 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700749 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700750 }
751 if (rt->dst.dev == dev) { /* is this necessary? */
752 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700753 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700754 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700755 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100756 if (use_cache)
757 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700758 return rt;
759}
760
John W. Linville8ed66f02015-10-26 17:01:44 -0400761#if IS_ENABLED(CONFIG_IPV6)
762static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
763 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700764 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400765 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800766 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400767{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100768 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400769 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400770 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100771 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400772 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400773
pravin shelarfceb9c32016-10-28 09:59:16 -0700774 if (!gs6)
775 return ERR_PTR(-EIO);
776
John W. Linville8ed66f02015-10-26 17:01:44 -0400777 memset(fl6, 0, sizeof(*fl6));
778 fl6->flowi6_mark = skb->mark;
779 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800780 fl6->daddr = info->key.u.ipv6.dst;
781 fl6->saddr = info->key.u.ipv6.src;
782 prio = info->key.tos;
783 if ((prio == 1) && !geneve->collect_md) {
784 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
785 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100786 }
787
pravin shelar9b4437a2016-11-21 11:02:58 -0800788 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
789 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800790 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100791 if (use_cache) {
792 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
793 if (dst)
794 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400795 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400796 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
797 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
798 return ERR_PTR(-ENETUNREACH);
799 }
800 if (dst->dev == dev) { /* is this necessary? */
801 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
802 dst_release(dst);
803 return ERR_PTR(-ELOOP);
804 }
805
Paolo Abeni468dfff2016-02-12 15:43:58 +0100806 if (use_cache)
807 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400808 return dst;
809}
810#endif
811
pravin shelar9b4437a2016-11-21 11:02:58 -0800812static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800813 struct geneve_dev *geneve,
814 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700815{
pravin shelar9b4437a2016-11-21 11:02:58 -0800816 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
817 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
818 const struct ip_tunnel_key *key = &info->key;
819 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400820 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400821 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700822 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700823 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800824 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400825
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700826 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800827 if (IS_ERR(rt))
828 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700829
Xin Long52a589d2017-12-25 14:43:58 +0800830 if (skb_dst(skb)) {
Alexey Kodanev5edbea62018-04-19 15:42:30 +0300831 int mtu = dst_mtu(&rt->dst) - GENEVE_IPV4_HLEN -
832 info->options_len;
Xin Long52a589d2017-12-25 14:43:58 +0800833
Nicolas Dichtelf15ca722018-01-25 19:03:03 +0100834 skb_dst_update_pmtu(skb, mtu);
Xin Long52a589d2017-12-25 14:43:58 +0800835 }
836
Pravin B Shelar371bd102015-08-26 23:46:54 -0700837 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800838 if (geneve->collect_md) {
839 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700840 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700841 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800842 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
843 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400844 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800845 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
846
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800847 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800848 if (unlikely(err))
849 return err;
850
Pravin B Shelar039f5062015-12-24 14:34:54 -0800851 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800852 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800853 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800854 !(info->key.tun_flags & TUNNEL_CSUM));
855 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400856}
857
John W. Linville8ed66f02015-10-26 17:01:44 -0400858#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800859static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800860 struct geneve_dev *geneve,
861 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400862{
pravin shelar9b4437a2016-11-21 11:02:58 -0800863 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
864 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
865 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400866 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400867 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400868 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400869 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800870 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400871
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700872 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800873 if (IS_ERR(dst))
874 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400875
Xin Long52a589d2017-12-25 14:43:58 +0800876 if (skb_dst(skb)) {
Alexey Kodanev5edbea62018-04-19 15:42:30 +0300877 int mtu = dst_mtu(dst) - GENEVE_IPV6_HLEN - info->options_len;
Xin Long52a589d2017-12-25 14:43:58 +0800878
Nicolas Dichtelf15ca722018-01-25 19:03:03 +0100879 skb_dst_update_pmtu(skb, mtu);
Xin Long52a589d2017-12-25 14:43:58 +0800880 }
881
John W. Linville8ed66f02015-10-26 17:01:44 -0400882 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 if (geneve->collect_md) {
884 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400885 ttl = key->ttl;
886 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100887 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800888 ip_hdr(skb), skb);
889 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400890 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800891 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800892 if (unlikely(err))
893 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100894
Pravin B Shelar039f5062015-12-24 14:34:54 -0800895 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 &fl6.saddr, &fl6.daddr, prio, ttl,
897 info->key.label, sport, geneve->info.key.tp_dst,
898 !(info->key.tun_flags & TUNNEL_CSUM));
899 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400900}
901#endif
902
903static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
904{
905 struct geneve_dev *geneve = netdev_priv(dev);
906 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800907 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400908
pravin shelar9b4437a2016-11-21 11:02:58 -0800909 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400910 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800911 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
912 err = -EINVAL;
913 netdev_dbg(dev, "no tunnel metadata\n");
914 goto tx_error;
915 }
916 } else {
917 info = &geneve->info;
918 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400919
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800920 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400921#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800922 if (info->mode & IP_TUNNEL_INFO_IPV6)
923 err = geneve6_xmit_skb(skb, dev, geneve, info);
924 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400925#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800926 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800927 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800928
929 if (likely(!err))
930 return NETDEV_TX_OK;
931tx_error:
932 dev_kfree_skb(skb);
933
934 if (err == -ELOOP)
935 dev->stats.collisions++;
936 else if (err == -ENETUNREACH)
937 dev->stats.tx_carrier_errors++;
938
939 dev->stats.tx_errors++;
940 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400941}
942
Jarod Wilson91572082016-10-20 13:55:20 -0400943static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000944{
Jarod Wilson91572082016-10-20 13:55:20 -0400945 if (new_mtu > dev->max_mtu)
946 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +0300947 else if (new_mtu < dev->min_mtu)
948 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000949
David Wragg55e5bfb2016-02-10 00:05:57 +0000950 dev->mtu = new_mtu;
951 return 0;
952}
953
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700954static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
955{
956 struct ip_tunnel_info *info = skb_tunnel_info(skb);
957 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700958
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400959 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800960 struct rtable *rt;
961 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700962 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -0800963
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700964 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400965 if (IS_ERR(rt))
966 return PTR_ERR(rt);
967
968 ip_rt_put(rt);
969 info->key.u.ipv4.src = fl4.saddr;
970#if IS_ENABLED(CONFIG_IPV6)
971 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800972 struct dst_entry *dst;
973 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700974 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -0800975
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700976 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400977 if (IS_ERR(dst))
978 return PTR_ERR(dst);
979
980 dst_release(dst);
981 info->key.u.ipv6.src = fl6.saddr;
982#endif
983 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700984 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400985 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700986
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700987 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
988 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800989 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700990 return 0;
991}
992
John W. Linville2d07dc72015-05-13 12:57:30 -0400993static const struct net_device_ops geneve_netdev_ops = {
994 .ndo_init = geneve_init,
995 .ndo_uninit = geneve_uninit,
996 .ndo_open = geneve_open,
997 .ndo_stop = geneve_stop,
998 .ndo_start_xmit = geneve_xmit,
999 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001000 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001001 .ndo_validate_addr = eth_validate_addr,
1002 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001003 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001004};
1005
1006static void geneve_get_drvinfo(struct net_device *dev,
1007 struct ethtool_drvinfo *drvinfo)
1008{
1009 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1010 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1011}
1012
1013static const struct ethtool_ops geneve_ethtool_ops = {
1014 .get_drvinfo = geneve_get_drvinfo,
1015 .get_link = ethtool_op_get_link,
1016};
1017
1018/* Info for udev, that this is a virtual tunnel endpoint */
1019static struct device_type geneve_type = {
1020 .name = "geneve",
1021};
1022
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001023/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001024 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001025 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001026 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001027static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001028{
1029 struct net *net = dev_net(dev);
1030 struct geneve_net *gn = net_generic(net, geneve_net_id);
1031 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001032
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001033 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001034 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1035 if (push) {
1036 udp_tunnel_push_rx_port(dev, gs->sock,
1037 UDP_TUNNEL_TYPE_GENEVE);
1038 } else {
1039 udp_tunnel_drop_rx_port(dev, gs->sock,
1040 UDP_TUNNEL_TYPE_GENEVE);
1041 }
1042 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001043 rcu_read_unlock();
1044}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001045
John W. Linville2d07dc72015-05-13 12:57:30 -04001046/* Initialize the device structure. */
1047static void geneve_setup(struct net_device *dev)
1048{
1049 ether_setup(dev);
1050
1051 dev->netdev_ops = &geneve_netdev_ops;
1052 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001053 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001054
1055 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1056
John W. Linville2d07dc72015-05-13 12:57:30 -04001057 dev->features |= NETIF_F_LLTX;
1058 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1059 dev->features |= NETIF_F_RXCSUM;
1060 dev->features |= NETIF_F_GSO_SOFTWARE;
1061
John W. Linville2d07dc72015-05-13 12:57:30 -04001062 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1063 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001064
Jarod Wilson91572082016-10-20 13:55:20 -04001065 /* MTU range: 68 - (something less than 65535) */
1066 dev->min_mtu = ETH_MIN_MTU;
1067 /* The max_mtu calculation does not take account of GENEVE
1068 * options, to avoid excluding potentially valid
1069 * configurations. This will be further reduced by IPvX hdr size.
1070 */
1071 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1072
John W. Linville2d07dc72015-05-13 12:57:30 -04001073 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001074 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001075 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001076 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001077}
1078
1079static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1080 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1081 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001082 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001083 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001084 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001085 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001086 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001087 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001088 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1089 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1090 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001091};
1092
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001093static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1094 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001095{
1096 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001097 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1098 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1099 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001100 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001101 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001102
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001103 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1104 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1105 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001106 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001107 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001108 }
1109
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001110 if (!data) {
1111 NL_SET_ERR_MSG(extack,
1112 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001113 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001114 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001115
1116 if (data[IFLA_GENEVE_ID]) {
1117 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1118
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001119 if (vni >= GENEVE_N_VID) {
1120 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1121 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001122 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001123 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001124 }
1125
1126 return 0;
1127}
1128
Pravin B Shelar371bd102015-08-26 23:46:54 -07001129static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001130 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001131 bool *tun_on_same_port,
1132 bool *tun_collect_md)
1133{
pravin shelar9b4437a2016-11-21 11:02:58 -08001134 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001135
1136 *tun_on_same_port = false;
1137 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001138 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001139 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001140 *tun_collect_md = geneve->collect_md;
1141 *tun_on_same_port = true;
1142 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001143 if (info->key.tun_id == geneve->info.key.tun_id &&
1144 info->key.tp_dst == geneve->info.key.tp_dst &&
1145 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001146 t = geneve;
1147 }
1148 return t;
1149}
1150
pravin shelar9b4437a2016-11-21 11:02:58 -08001151static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1152{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001153 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1154 info->key.ttl || info->key.label || info->key.tp_src ||
1155 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001156}
1157
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001158static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1159 struct ip_tunnel_info *b)
1160{
1161 if (ip_tunnel_info_af(a) == AF_INET)
1162 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1163 else
1164 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1165}
1166
Pravin B Shelare305ac62015-08-26 23:46:52 -07001167static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001168 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001169 const struct ip_tunnel_info *info,
1170 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001171{
1172 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001173 struct geneve_dev *t, *geneve = netdev_priv(dev);
1174 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001175 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001176
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001177 if (metadata && !is_tnl_info_zero(info)) {
1178 NL_SET_ERR_MSG(extack,
1179 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001180 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001181 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001182
1183 geneve->net = net;
1184 geneve->dev = dev;
1185
pravin shelar9b4437a2016-11-21 11:02:58 -08001186 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001187 if (t)
1188 return -EBUSY;
1189
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001190 /* make enough headroom for basic scenario */
1191 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001192 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001193 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001194 dev->max_mtu -= sizeof(struct iphdr);
1195 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001196 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001197 dev->max_mtu -= sizeof(struct ipv6hdr);
1198 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001199 dev->needed_headroom = encap_len + ETH_HLEN;
1200
Pravin B Shelar371bd102015-08-26 23:46:54 -07001201 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001202 if (tun_on_same_port) {
1203 NL_SET_ERR_MSG(extack,
1204 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001205 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001206 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001207 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001208 if (tun_collect_md) {
1209 NL_SET_ERR_MSG(extack,
1210 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001211 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001212 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001213 }
1214
pravin shelar9b4437a2016-11-21 11:02:58 -08001215 dst_cache_reset(&geneve->info.dst_cache);
1216 geneve->info = *info;
1217 geneve->collect_md = metadata;
1218 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001219
John W. Linville2d07dc72015-05-13 12:57:30 -04001220 err = register_netdevice(dev);
1221 if (err)
1222 return err;
1223
1224 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001225 return 0;
1226}
1227
pravin shelar9b4437a2016-11-21 11:02:58 -08001228static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1229{
1230 memset(info, 0, sizeof(*info));
1231 info->key.tp_dst = htons(dst_port);
1232}
1233
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001234static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1235 struct netlink_ext_ack *extack,
1236 struct ip_tunnel_info *info, bool *metadata,
1237 bool *use_udp6_rx_checksums, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001238{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001239 int attrtype;
1240
1241 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1242 NL_SET_ERR_MSG(extack,
1243 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001244 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001245 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001246
1247 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001248 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1249 attrtype = IFLA_GENEVE_REMOTE;
1250 goto change_notsup;
1251 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001252
1253 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001254 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001255
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001256 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001257 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1258 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001259 return -EINVAL;
1260 }
1261 }
1262
pravin shelar9b4437a2016-11-21 11:02:58 -08001263 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001264#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001265 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1266 attrtype = IFLA_GENEVE_REMOTE6;
1267 goto change_notsup;
1268 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001269
1270 info->mode = IP_TUNNEL_INFO_IPV6;
1271 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001272 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001273
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001274 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001275 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001276 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1277 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001278 return -EINVAL;
1279 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001280 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001281 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1282 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001283 return -EINVAL;
1284 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001285 info->key.tun_flags |= TUNNEL_CSUM;
1286 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001287#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001288 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1289 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001290 return -EPFNOSUPPORT;
1291#endif
1292 }
1293
1294 if (data[IFLA_GENEVE_ID]) {
1295 __u32 vni;
1296 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001297 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001298
1299 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1300 tvni[0] = (vni & 0x00ff0000) >> 16;
1301 tvni[1] = (vni & 0x0000ff00) >> 8;
1302 tvni[2] = vni & 0x000000ff;
1303
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001304 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001305 if (changelink && (tunid != info->key.tun_id)) {
1306 attrtype = IFLA_GENEVE_ID;
1307 goto change_notsup;
1308 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001309 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001310 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001311
Pravin B Shelare305ac62015-08-26 23:46:52 -07001312 if (data[IFLA_GENEVE_TTL])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001313 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001314
1315 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001316 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001317
pravin shelar9b4437a2016-11-21 11:02:58 -08001318 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001319 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001320 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001321 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1322 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1323 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001324 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001325 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001326 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001327
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001328 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001329 if (changelink) {
1330 attrtype = IFLA_GENEVE_PORT;
1331 goto change_notsup;
1332 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001333 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1334 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001335
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001336 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001337 if (changelink) {
1338 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1339 goto change_notsup;
1340 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001341 *metadata = true;
1342 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001343
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001344 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001345 if (changelink) {
1346 attrtype = IFLA_GENEVE_UDP_CSUM;
1347 goto change_notsup;
1348 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001349 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1350 info->key.tun_flags |= TUNNEL_CSUM;
1351 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001352
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001353 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001354#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001355 if (changelink) {
1356 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1357 goto change_notsup;
1358 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001359 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1360 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001361#else
1362 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1363 "IPv6 support not enabled in the kernel");
1364 return -EPFNOSUPPORT;
1365#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001366 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001367
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001368 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001369#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001370 if (changelink) {
1371 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1372 goto change_notsup;
1373 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001374 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1375 *use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001376#else
1377 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1378 "IPv6 support not enabled in the kernel");
1379 return -EPFNOSUPPORT;
1380#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001381 }
1382
1383 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001384change_notsup:
1385 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1386 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1387 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001388}
1389
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001390static void geneve_link_config(struct net_device *dev,
1391 struct ip_tunnel_info *info, struct nlattr *tb[])
1392{
1393 struct geneve_dev *geneve = netdev_priv(dev);
1394 int ldev_mtu = 0;
1395
1396 if (tb[IFLA_MTU]) {
1397 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1398 return;
1399 }
1400
1401 switch (ip_tunnel_info_af(info)) {
1402 case AF_INET: {
1403 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1404 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1405
1406 if (!IS_ERR(rt) && rt->dst.dev) {
1407 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1408 ip_rt_put(rt);
1409 }
1410 break;
1411 }
1412#if IS_ENABLED(CONFIG_IPV6)
1413 case AF_INET6: {
1414 struct rt6_info *rt = rt6_lookup(geneve->net,
1415 &info->key.u.ipv6.dst, NULL, 0,
1416 NULL, 0);
1417
1418 if (rt && rt->dst.dev)
1419 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1420 ip6_rt_put(rt);
1421 break;
1422 }
1423#endif
1424 }
1425
1426 if (ldev_mtu <= 0)
1427 return;
1428
1429 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1430}
1431
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001432static int geneve_newlink(struct net *net, struct net_device *dev,
1433 struct nlattr *tb[], struct nlattr *data[],
1434 struct netlink_ext_ack *extack)
1435{
1436 bool use_udp6_rx_checksums = false;
1437 struct ip_tunnel_info info;
1438 bool metadata = false;
1439 int err;
1440
1441 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001442 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001443 &use_udp6_rx_checksums, false);
1444 if (err)
1445 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001446
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001447 err = geneve_configure(net, dev, extack, &info, metadata,
1448 use_udp6_rx_checksums);
1449 if (err)
1450 return err;
1451
1452 geneve_link_config(dev, &info, tb);
1453
1454 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001455}
1456
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001457/* Quiesces the geneve device data path for both TX and RX.
1458 *
1459 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1460 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1461 * to complete for the existing set of in-flight packets to be transmitted,
1462 * then we would have quiesced the transmit data path. All the future packets
1463 * will get dropped until we unquiesce the data path.
1464 *
1465 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1466 * if we set that to NULL under RCU and wait for synchronize_net() to
1467 * complete, then we would have quiesced the receive data path.
1468 */
1469static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1470 struct geneve_sock **gs6)
1471{
1472 *gs4 = rtnl_dereference(geneve->sock4);
1473 rcu_assign_pointer(geneve->sock4, NULL);
1474 if (*gs4)
1475 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1476#if IS_ENABLED(CONFIG_IPV6)
1477 *gs6 = rtnl_dereference(geneve->sock6);
1478 rcu_assign_pointer(geneve->sock6, NULL);
1479 if (*gs6)
1480 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1481#else
1482 *gs6 = NULL;
1483#endif
1484 synchronize_net();
1485}
1486
1487/* Resumes the geneve device data path for both TX and RX. */
1488static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1489 struct geneve_sock __maybe_unused *gs6)
1490{
1491 rcu_assign_pointer(geneve->sock4, gs4);
1492 if (gs4)
1493 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1494#if IS_ENABLED(CONFIG_IPV6)
1495 rcu_assign_pointer(geneve->sock6, gs6);
1496 if (gs6)
1497 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1498#endif
1499 synchronize_net();
1500}
1501
1502static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1503 struct nlattr *data[],
1504 struct netlink_ext_ack *extack)
1505{
1506 struct geneve_dev *geneve = netdev_priv(dev);
1507 struct geneve_sock *gs4, *gs6;
1508 struct ip_tunnel_info info;
1509 bool metadata;
1510 bool use_udp6_rx_checksums;
1511 int err;
1512
1513 /* If the geneve device is configured for metadata (or externally
1514 * controlled, for example, OVS), then nothing can be changed.
1515 */
1516 if (geneve->collect_md)
1517 return -EOPNOTSUPP;
1518
1519 /* Start with the existing info. */
1520 memcpy(&info, &geneve->info, sizeof(info));
1521 metadata = geneve->collect_md;
1522 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001523 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001524 &use_udp6_rx_checksums, true);
1525 if (err)
1526 return err;
1527
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001528 if (!geneve_dst_addr_equal(&geneve->info, &info)) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001529 dst_cache_reset(&info.dst_cache);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001530 geneve_link_config(dev, &info, tb);
1531 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001532
1533 geneve_quiesce(geneve, &gs4, &gs6);
1534 geneve->info = info;
1535 geneve->collect_md = metadata;
1536 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
1537 geneve_unquiesce(geneve, gs4, gs6);
1538
1539 return 0;
1540}
1541
John W. Linville2d07dc72015-05-13 12:57:30 -04001542static void geneve_dellink(struct net_device *dev, struct list_head *head)
1543{
1544 struct geneve_dev *geneve = netdev_priv(dev);
1545
John W. Linville2d07dc72015-05-13 12:57:30 -04001546 list_del(&geneve->next);
1547 unregister_netdevice_queue(dev, head);
1548}
1549
1550static size_t geneve_get_size(const struct net_device *dev)
1551{
1552 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001553 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001554 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001555 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001556 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001557 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001558 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001559 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1560 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1561 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001562 0;
1563}
1564
1565static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1566{
1567 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001568 struct ip_tunnel_info *info = &geneve->info;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001569 bool metadata = geneve->collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001570 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001571 __u32 vni;
1572
pravin shelar9b4437a2016-11-21 11:02:58 -08001573 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1574 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001575 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1576 goto nla_put_failure;
1577
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001578 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001579 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001580 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001581 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001582 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1583 !!(info->key.tun_flags & TUNNEL_CSUM)))
1584 goto nla_put_failure;
1585
John W. Linville8ed66f02015-10-26 17:01:44 -04001586#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001587 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001588 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001589 &info->key.u.ipv6.dst))
1590 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001591 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1592 !(info->key.tun_flags & TUNNEL_CSUM)))
1593 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001594#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001595 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001596
pravin shelar9b4437a2016-11-21 11:02:58 -08001597 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1598 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1599 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001600 goto nla_put_failure;
1601
pravin shelar9b4437a2016-11-21 11:02:58 -08001602 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001603 goto nla_put_failure;
1604
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001605 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001606 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001607
Hangbin Liuf9094b72017-11-23 11:27:24 +08001608#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001609 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1610 !geneve->use_udp6_rx_checksums))
1611 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001612#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001613
John W. Linville2d07dc72015-05-13 12:57:30 -04001614 return 0;
1615
1616nla_put_failure:
1617 return -EMSGSIZE;
1618}
1619
1620static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1621 .kind = "geneve",
1622 .maxtype = IFLA_GENEVE_MAX,
1623 .policy = geneve_policy,
1624 .priv_size = sizeof(struct geneve_dev),
1625 .setup = geneve_setup,
1626 .validate = geneve_validate,
1627 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001628 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001629 .dellink = geneve_dellink,
1630 .get_size = geneve_get_size,
1631 .fill_info = geneve_fill_info,
1632};
1633
Pravin B Shelare305ac62015-08-26 23:46:52 -07001634struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1635 u8 name_assign_type, u16 dst_port)
1636{
1637 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001638 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001639 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001640 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001641 int err;
1642
1643 memset(tb, 0, sizeof(tb));
1644 dev = rtnl_create_link(net, name, name_assign_type,
1645 &geneve_link_ops, tb);
1646 if (IS_ERR(dev))
1647 return dev;
1648
pravin shelar9b4437a2016-11-21 11:02:58 -08001649 init_tnl_info(&info, dst_port);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001650 err = geneve_configure(net, dev, NULL, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001651 if (err) {
1652 free_netdev(dev);
1653 return ERR_PTR(err);
1654 }
David Wragg7e059152016-02-10 00:05:58 +00001655
1656 /* openvswitch users expect packet sizes to be unrestricted,
1657 * so set the largest MTU we can.
1658 */
Jarod Wilson91572082016-10-20 13:55:20 -04001659 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001660 if (err)
1661 goto err;
1662
Nicolas Dichtel41009482016-06-13 10:31:07 +02001663 err = rtnl_configure_link(dev, NULL);
1664 if (err < 0)
1665 goto err;
1666
Pravin B Shelare305ac62015-08-26 23:46:52 -07001667 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001668err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001669 geneve_dellink(dev, &list_kill);
1670 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001671 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001672}
1673EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1674
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001675static int geneve_netdevice_event(struct notifier_block *unused,
1676 unsigned long event, void *ptr)
1677{
1678 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1679
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001680 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001681 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001682 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001683 } else if (event == NETDEV_UNREGISTER) {
1684 geneve_offload_rx_ports(dev, false);
1685 } else if (event == NETDEV_REGISTER) {
1686 geneve_offload_rx_ports(dev, true);
1687 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001688
1689 return NOTIFY_DONE;
1690}
1691
1692static struct notifier_block geneve_notifier_block __read_mostly = {
1693 .notifier_call = geneve_netdevice_event,
1694};
1695
John W. Linville2d07dc72015-05-13 12:57:30 -04001696static __net_init int geneve_init_net(struct net *net)
1697{
1698 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001699
1700 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001701 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001702 return 0;
1703}
1704
Haishuang Yan2843a252017-12-16 17:54:50 +08001705static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001706{
1707 struct geneve_net *gn = net_generic(net, geneve_net_id);
1708 struct geneve_dev *geneve, *next;
1709 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001710
1711 /* gather any geneve devices that were moved into this ns */
1712 for_each_netdev_safe(net, dev, aux)
1713 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001714 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001715
1716 /* now gather any other geneve devices that were created in this ns */
1717 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1718 /* If geneve->dev is in the same netns, it was already added
1719 * to the list by the previous loop.
1720 */
1721 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001722 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001723 }
1724
Haishuang Yan2843a252017-12-16 17:54:50 +08001725 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1726}
1727
1728static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1729{
1730 struct net *net;
1731 LIST_HEAD(list);
1732
1733 rtnl_lock();
1734 list_for_each_entry(net, net_list, exit_list)
1735 geneve_destroy_tunnels(net, &list);
1736
John W. Linville2d07dc72015-05-13 12:57:30 -04001737 /* unregister the devices gathered above */
1738 unregister_netdevice_many(&list);
1739 rtnl_unlock();
1740}
1741
1742static struct pernet_operations geneve_net_ops = {
1743 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001744 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001745 .id = &geneve_net_id,
1746 .size = sizeof(struct geneve_net),
1747};
1748
1749static int __init geneve_init_module(void)
1750{
1751 int rc;
1752
1753 rc = register_pernet_subsys(&geneve_net_ops);
1754 if (rc)
1755 goto out1;
1756
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001757 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001758 if (rc)
1759 goto out2;
1760
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001761 rc = rtnl_link_register(&geneve_link_ops);
1762 if (rc)
1763 goto out3;
1764
John W. Linville2d07dc72015-05-13 12:57:30 -04001765 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001766out3:
1767 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001768out2:
1769 unregister_pernet_subsys(&geneve_net_ops);
1770out1:
1771 return rc;
1772}
1773late_initcall(geneve_init_module);
1774
1775static void __exit geneve_cleanup_module(void)
1776{
1777 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001778 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001779 unregister_pernet_subsys(&geneve_net_ops);
1780}
1781module_exit(geneve_cleanup_module);
1782
1783MODULE_LICENSE("GPL");
1784MODULE_VERSION(GENEVE_NETDEV_VER);
1785MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1786MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1787MODULE_ALIAS_RTNL_LINK("geneve");