blob: 4dc4574897705d526c3115cd94272d046e3e51e6 [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;
Hangbin Liu52d0d4042018-09-12 10:04:21 +080072 bool ttl_inherit;
John W. Linville2d07dc72015-05-13 12:57:30 -040073};
74
Pravin B Shelar371bd102015-08-26 23:46:54 -070075struct geneve_sock {
76 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070077 struct list_head list;
78 struct socket *sock;
79 struct rcu_head rcu;
80 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070081 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070082};
John W. Linville2d07dc72015-05-13 12:57:30 -040083
84static inline __u32 geneve_net_vni_hash(u8 vni[3])
85{
86 __u32 vnid;
87
88 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
89 return hash_32(vnid, VNI_HASH_BITS);
90}
91
Pravin B Shelare305ac62015-08-26 23:46:52 -070092static __be64 vni_to_tunnel_id(const __u8 *vni)
93{
94#ifdef __BIG_ENDIAN
95 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
96#else
97 return (__force __be64)(((__force u64)vni[0] << 40) |
98 ((__force u64)vni[1] << 48) |
99 ((__force u64)vni[2] << 56));
100#endif
101}
102
pravin shelar9b4437a2016-11-21 11:02:58 -0800103/* Convert 64 bit tunnel ID to 24 bit VNI. */
104static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
105{
106#ifdef __BIG_ENDIAN
107 vni[0] = (__force __u8)(tun_id >> 16);
108 vni[1] = (__force __u8)(tun_id >> 8);
109 vni[2] = (__force __u8)tun_id;
110#else
111 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
112 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
113 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
114#endif
115}
116
pravin shelar2e0b26e2016-11-21 11:03:01 -0800117static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
118{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800119 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800120}
121
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100122static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
123{
124 return gs->sock->sk->sk_family;
125}
126
Pravin B Shelar66d47002015-08-26 23:46:55 -0700127static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700128 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400129{
John W. Linville2d07dc72015-05-13 12:57:30 -0400130 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200131 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400132 __u32 hash;
133
John W. Linville2d07dc72015-05-13 12:57:30 -0400134 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700135 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700136 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200137 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
138 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
139 addr == node->geneve->info.key.u.ipv4.dst)
140 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400141 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700142 return NULL;
143}
144
John W. Linville8ed66f02015-10-26 17:01:44 -0400145#if IS_ENABLED(CONFIG_IPV6)
146static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
147 struct in6_addr addr6, u8 vni[])
148{
149 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200150 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400151 __u32 hash;
152
153 /* Find the device for this VNI */
154 hash = geneve_net_vni_hash(vni);
155 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200156 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
157 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
158 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst))
159 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400160 }
161 return NULL;
162}
163#endif
164
Pravin B Shelar371bd102015-08-26 23:46:54 -0700165static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
166{
167 return (struct genevehdr *)(udp_hdr(skb) + 1);
168}
169
Jiri Benc9fc47542016-02-18 11:22:50 +0100170static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
171 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700172{
John W. Linville8ed66f02015-10-26 17:01:44 -0400173 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800174 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700175
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100176 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100177 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800178 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100179
John W. Linville8ed66f02015-10-26 17:01:44 -0400180 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700181
John W. Linville8ed66f02015-10-26 17:01:44 -0400182 if (gs->collect_md) {
183 vni = zero_vni;
184 addr = 0;
185 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100186 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400187 addr = iph->saddr;
188 }
189
Jiri Benc9fc47542016-02-18 11:22:50 +0100190 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400191#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100192 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800193 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100194 struct ipv6hdr *ip6h;
195 struct in6_addr addr6;
196
John W. Linville8ed66f02015-10-26 17:01:44 -0400197 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
198
199 if (gs->collect_md) {
200 vni = zero_vni;
201 addr6 = zero_addr6;
202 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100203 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400204 addr6 = ip6h->saddr;
205 }
206
Jiri Benc9fc47542016-02-18 11:22:50 +0100207 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400208#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700209 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100210 return NULL;
211}
212
213/* geneve receive/decap routine */
214static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
215 struct sk_buff *skb)
216{
217 struct genevehdr *gnvh = geneve_hdr(skb);
218 struct metadata_dst *tun_dst = NULL;
219 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700220 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100221 int err = 0;
222 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400223
Pravin B Shelar371bd102015-08-26 23:46:54 -0700224 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700225 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700226
227 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
228 (gnvh->oam ? TUNNEL_OAM : 0) |
229 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
230
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100231 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700232 vni_to_tunnel_id(gnvh->vni),
233 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700234 if (!tun_dst) {
235 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700236 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700237 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700238 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700239 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700240 gnvh->options, gnvh->opt_len * 4,
241 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700242 } else {
243 /* Drop packets w/ critical options,
244 * since we don't support any...
245 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700246 if (gnvh->critical) {
247 geneve->dev->stats.rx_frame_errors++;
248 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700249 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700250 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700251 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400252
253 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400254 skb->protocol = eth_type_trans(skb, geneve->dev);
255 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
256
Pravin B Shelare305ac62015-08-26 23:46:52 -0700257 if (tun_dst)
258 skb_dst_set(skb, &tun_dst->dst);
259
John W. Linville2d07dc72015-05-13 12:57:30 -0400260 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700261 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
262 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400263 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700264 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400265
Jiri Benc9fc47542016-02-18 11:22:50 +0100266 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400267 skb_reset_network_header(skb);
268
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 if (geneve_get_sk_family(gs) == AF_INET)
270 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400271#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100272 else
273 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400274#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400275
276 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400277 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100278 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400279 net_info_ratelimited("non-ECT from %pI4 "
280 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100281 &((struct iphdr *)oiph)->saddr,
282 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400283#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100284 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400285 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100286 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400287#endif
288 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400289 if (err > 1) {
290 ++geneve->dev->stats.rx_frame_errors;
291 ++geneve->dev->stats.rx_errors;
292 goto drop;
293 }
294 }
295
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700296 len = skb->len;
297 err = gro_cells_receive(&geneve->gro_cells, skb);
298 if (likely(err == NET_RX_SUCCESS)) {
299 stats = this_cpu_ptr(geneve->dev->tstats);
300 u64_stats_update_begin(&stats->syncp);
301 stats->rx_packets++;
302 stats->rx_bytes += len;
303 u64_stats_update_end(&stats->syncp);
304 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400305 return;
306drop:
307 /* Consume bad packet */
308 kfree_skb(skb);
309}
310
311/* Setup stats when device is created */
312static int geneve_init(struct net_device *dev)
313{
Jesse Gross8e816df2015-08-28 16:54:40 -0700314 struct geneve_dev *geneve = netdev_priv(dev);
315 int err;
316
John W. Linville2d07dc72015-05-13 12:57:30 -0400317 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
318 if (!dev->tstats)
319 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700320
321 err = gro_cells_init(&geneve->gro_cells, dev);
322 if (err) {
323 free_percpu(dev->tstats);
324 return err;
325 }
326
pravin shelar9b4437a2016-11-21 11:02:58 -0800327 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100328 if (err) {
329 free_percpu(dev->tstats);
330 gro_cells_destroy(&geneve->gro_cells);
331 return err;
332 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400333 return 0;
334}
335
336static void geneve_uninit(struct net_device *dev)
337{
Jesse Gross8e816df2015-08-28 16:54:40 -0700338 struct geneve_dev *geneve = netdev_priv(dev);
339
pravin shelar9b4437a2016-11-21 11:02:58 -0800340 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700341 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400342 free_percpu(dev->tstats);
343}
344
Pravin B Shelar371bd102015-08-26 23:46:54 -0700345/* Callback from net/ipv4/udp.c to receive packets */
346static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
347{
348 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100349 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700350 struct geneve_sock *gs;
351 int opts_len;
352
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700353 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700354 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200355 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700356
357 /* Return packets with reserved bits set */
358 geneveh = geneve_hdr(skb);
359 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200360 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700361
362 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200363 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700364
Jiri Benc9fc47542016-02-18 11:22:50 +0100365 gs = rcu_dereference_sk_user_data(sk);
366 if (!gs)
367 goto drop;
368
369 geneve = geneve_lookup_skb(gs, skb);
370 if (!geneve)
371 goto drop;
372
Pravin B Shelar371bd102015-08-26 23:46:54 -0700373 opts_len = geneveh->opt_len * 4;
374 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100375 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700376 !net_eq(geneve->net, dev_net(geneve->dev)))) {
377 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700378 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700379 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700380
Jiri Benc9fc47542016-02-18 11:22:50 +0100381 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700382 return 0;
383
384drop:
385 /* Consume bad packet */
386 kfree_skb(skb);
387 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700388}
389
Stefano Brivioa0796642018-11-08 12:19:18 +0100390/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
391static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
392{
393 struct genevehdr *geneveh;
394 struct geneve_sock *gs;
395 u8 zero_vni[3] = { 0 };
396 u8 *vni = zero_vni;
397
398 if (skb->len < GENEVE_BASE_HLEN)
399 return -EINVAL;
400
401 geneveh = geneve_hdr(skb);
402 if (geneveh->ver != GENEVE_VER)
403 return -EINVAL;
404
405 if (geneveh->proto_type != htons(ETH_P_TEB))
406 return -EINVAL;
407
408 gs = rcu_dereference_sk_user_data(sk);
409 if (!gs)
410 return -ENOENT;
411
412 if (geneve_get_sk_family(gs) == AF_INET) {
413 struct iphdr *iph = ip_hdr(skb);
414 __be32 addr4 = 0;
415
416 if (!gs->collect_md) {
417 vni = geneve_hdr(skb)->vni;
418 addr4 = iph->daddr;
419 }
420
421 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
422 }
423
424#if IS_ENABLED(CONFIG_IPV6)
425 if (geneve_get_sk_family(gs) == AF_INET6) {
426 struct ipv6hdr *ip6h = ipv6_hdr(skb);
427 struct in6_addr addr6 = { 0 };
428
429 if (!gs->collect_md) {
430 vni = geneve_hdr(skb)->vni;
431 addr6 = ip6h->daddr;
432 }
433
434 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
435 }
436#endif
437
438 return -EPFNOSUPPORT;
439}
440
Pravin B Shelar371bd102015-08-26 23:46:54 -0700441static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800442 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700443{
444 struct socket *sock;
445 struct udp_port_cfg udp_conf;
446 int err;
447
448 memset(&udp_conf, 0, sizeof(udp_conf));
449
450 if (ipv6) {
451 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400452 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800453 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700454 } else {
455 udp_conf.family = AF_INET;
456 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
457 }
458
459 udp_conf.local_udp_port = port;
460
461 /* Open UDP socket */
462 err = udp_sock_create(net, &udp_conf, &sock);
463 if (err < 0)
464 return ERR_PTR(err);
465
466 return sock;
467}
468
Pravin B Shelar371bd102015-08-26 23:46:54 -0700469static int geneve_hlen(struct genevehdr *gh)
470{
471 return sizeof(*gh) + gh->opt_len * 4;
472}
473
David Millerd4546c22018-06-24 14:13:49 +0900474static struct sk_buff *geneve_gro_receive(struct sock *sk,
475 struct list_head *head,
476 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700477{
David Millerd4546c22018-06-24 14:13:49 +0900478 struct sk_buff *pp = NULL;
479 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700480 struct genevehdr *gh, *gh2;
481 unsigned int hlen, gh_len, off_gnv;
482 const struct packet_offload *ptype;
483 __be16 type;
484 int flush = 1;
485
486 off_gnv = skb_gro_offset(skb);
487 hlen = off_gnv + sizeof(*gh);
488 gh = skb_gro_header_fast(skb, off_gnv);
489 if (skb_gro_header_hard(skb, hlen)) {
490 gh = skb_gro_header_slow(skb, hlen, off_gnv);
491 if (unlikely(!gh))
492 goto out;
493 }
494
495 if (gh->ver != GENEVE_VER || gh->oam)
496 goto out;
497 gh_len = geneve_hlen(gh);
498
499 hlen = off_gnv + gh_len;
500 if (skb_gro_header_hard(skb, hlen)) {
501 gh = skb_gro_header_slow(skb, hlen, off_gnv);
502 if (unlikely(!gh))
503 goto out;
504 }
505
David Millerd4546c22018-06-24 14:13:49 +0900506 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700507 if (!NAPI_GRO_CB(p)->same_flow)
508 continue;
509
510 gh2 = (struct genevehdr *)(p->data + off_gnv);
511 if (gh->opt_len != gh2->opt_len ||
512 memcmp(gh, gh2, gh_len)) {
513 NAPI_GRO_CB(p)->same_flow = 0;
514 continue;
515 }
516 }
517
518 type = gh->proto_type;
519
520 rcu_read_lock();
521 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800522 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700523 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700524
525 skb_gro_pull(skb, gh_len);
526 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200527 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800528 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700529
530out_unlock:
531 rcu_read_unlock();
532out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200533 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534
535 return pp;
536}
537
Tom Herbert4a0090a2016-04-05 08:22:55 -0700538static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
539 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700540{
541 struct genevehdr *gh;
542 struct packet_offload *ptype;
543 __be16 type;
544 int gh_len;
545 int err = -ENOSYS;
546
Pravin B Shelar371bd102015-08-26 23:46:54 -0700547 gh = (struct genevehdr *)(skb->data + nhoff);
548 gh_len = geneve_hlen(gh);
549 type = gh->proto_type;
550
551 rcu_read_lock();
552 ptype = gro_find_complete_by_type(type);
553 if (ptype)
554 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
555
556 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700557
558 skb_set_inner_mac_header(skb, nhoff + gh_len);
559
Pravin B Shelar371bd102015-08-26 23:46:54 -0700560 return err;
561}
562
563/* Create new listen socket if needed */
564static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800565 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700566{
567 struct geneve_net *gn = net_generic(net, geneve_net_id);
568 struct geneve_sock *gs;
569 struct socket *sock;
570 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700571 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700572
573 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
574 if (!gs)
575 return ERR_PTR(-ENOMEM);
576
pravin shelar9b4437a2016-11-21 11:02:58 -0800577 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700578 if (IS_ERR(sock)) {
579 kfree(gs);
580 return ERR_CAST(sock);
581 }
582
583 gs->sock = sock;
584 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700585 for (h = 0; h < VNI_HASH_SIZE; ++h)
586 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700587
588 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700589 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590
591 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700592 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700593 tunnel_cfg.sk_user_data = gs;
594 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700595 tunnel_cfg.gro_receive = geneve_gro_receive;
596 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700597 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100598 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700599 tunnel_cfg.encap_destroy = NULL;
600 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700601 list_add(&gs->list, &gn->sock_list);
602 return gs;
603}
604
John W. Linville8ed66f02015-10-26 17:01:44 -0400605static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606{
John W. Linville8ed66f02015-10-26 17:01:44 -0400607 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700608 return;
609
610 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700611 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700612 udp_tunnel_sock_release(gs->sock);
613 kfree_rcu(gs, rcu);
614}
615
John W. Linville8ed66f02015-10-26 17:01:44 -0400616static void geneve_sock_release(struct geneve_dev *geneve)
617{
pravin shelarfceb9c32016-10-28 09:59:16 -0700618 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400619#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700620 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
621
622 rcu_assign_pointer(geneve->sock6, NULL);
623#endif
624
625 rcu_assign_pointer(geneve->sock4, NULL);
626 synchronize_net();
627
628 __geneve_sock_release(gs4);
629#if IS_ENABLED(CONFIG_IPV6)
630 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400631#endif
632}
633
Pravin B Shelar371bd102015-08-26 23:46:54 -0700634static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400635 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700636 __be16 dst_port)
637{
638 struct geneve_sock *gs;
639
640 list_for_each_entry(gs, &gn->sock_list, list) {
641 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100642 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700643 return gs;
644 }
645 }
646 return NULL;
647}
648
John W. Linville8ed66f02015-10-26 17:01:44 -0400649static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400650{
John W. Linville2d07dc72015-05-13 12:57:30 -0400651 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700652 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200653 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400654 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800655 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700656 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400657
pravin shelar9b4437a2016-11-21 11:02:58 -0800658 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700659 if (gs) {
660 gs->refcnt++;
661 goto out;
662 }
663
pravin shelar9b4437a2016-11-21 11:02:58 -0800664 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
665 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400666 if (IS_ERR(gs))
667 return PTR_ERR(gs);
668
Pravin B Shelar371bd102015-08-26 23:46:54 -0700669out:
670 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400671#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200672 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700673 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200674 node = &geneve->hlist6;
675 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400676#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200677 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700678 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200679 node = &geneve->hlist4;
680 }
681 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700682
pravin shelar9b4437a2016-11-21 11:02:58 -0800683 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
684 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200685 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400686 return 0;
687}
688
John W. Linville8ed66f02015-10-26 17:01:44 -0400689static int geneve_open(struct net_device *dev)
690{
691 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800692 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400693 bool metadata = geneve->collect_md;
694 int ret = 0;
695
John W. Linville8ed66f02015-10-26 17:01:44 -0400696#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400697 if (ipv6 || metadata)
698 ret = geneve_sock_add(geneve, true);
699#endif
700 if (!ret && (!ipv6 || metadata))
701 ret = geneve_sock_add(geneve, false);
702 if (ret < 0)
703 geneve_sock_release(geneve);
704
705 return ret;
706}
707
John W. Linville2d07dc72015-05-13 12:57:30 -0400708static int geneve_stop(struct net_device *dev)
709{
710 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400711
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200712 hlist_del_init_rcu(&geneve->hlist4.hlist);
713#if IS_ENABLED(CONFIG_IPV6)
714 hlist_del_init_rcu(&geneve->hlist6.hlist);
715#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400716 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400717 return 0;
718}
719
John W. Linville8ed66f02015-10-26 17:01:44 -0400720static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800721 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400722{
723 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800724 geneveh->opt_len = info->options_len / 4;
725 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
726 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400727 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800728 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400729 geneveh->proto_type = htons(ETH_P_TEB);
730 geneveh->rsvd2 = 0;
731
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700732 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
733 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400734}
735
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800736static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
737 const struct ip_tunnel_info *info,
738 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700739{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800740 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700741 struct genevehdr *gnvh;
742 int min_headroom;
743 int err;
744
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800745 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400746 skb_scrub_packet(skb, xnet);
747
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800748 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
749 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400750 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400751 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400752 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400753
Alexander Duyckaed069d2016-04-14 15:33:37 -0400754 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300755 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400756 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400757
Johannes Bergd58ff352017-06-16 14:29:23 +0200758 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800759 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400760 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
761 return 0;
762
763free_dst:
764 dst_release(dst);
765 return err;
766}
John W. Linville8ed66f02015-10-26 17:01:44 -0400767
768static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
769 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700770 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400771 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800772 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700773{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100774 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700775 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100776 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777 struct rtable *rt = NULL;
778 __u8 tos;
779
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700780 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700781 return ERR_PTR(-EIO);
782
Pravin B Shelare305ac62015-08-26 23:46:52 -0700783 memset(fl4, 0, sizeof(*fl4));
784 fl4->flowi4_mark = skb->mark;
785 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800786 fl4->daddr = info->key.u.ipv4.dst;
787 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700788
pravin shelar9b4437a2016-11-21 11:02:58 -0800789 tos = info->key.tos;
790 if ((tos == 1) && !geneve->collect_md) {
791 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
792 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100793 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800794 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100795
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800796 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100797 if (use_cache) {
798 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
799 if (rt)
800 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700801 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700802 rt = ip_route_output_key(geneve->net, fl4);
803 if (IS_ERR(rt)) {
804 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700805 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700806 }
807 if (rt->dst.dev == dev) { /* is this necessary? */
808 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700809 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700810 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700811 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100812 if (use_cache)
813 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700814 return rt;
815}
816
John W. Linville8ed66f02015-10-26 17:01:44 -0400817#if IS_ENABLED(CONFIG_IPV6)
818static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
819 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700820 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400821 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800822 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400823{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100824 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400825 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400826 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100827 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400828 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400829
pravin shelarfceb9c32016-10-28 09:59:16 -0700830 if (!gs6)
831 return ERR_PTR(-EIO);
832
John W. Linville8ed66f02015-10-26 17:01:44 -0400833 memset(fl6, 0, sizeof(*fl6));
834 fl6->flowi6_mark = skb->mark;
835 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800836 fl6->daddr = info->key.u.ipv6.dst;
837 fl6->saddr = info->key.u.ipv6.src;
838 prio = info->key.tos;
839 if ((prio == 1) && !geneve->collect_md) {
840 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
841 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100842 }
843
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
845 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800846 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100847 if (use_cache) {
848 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
849 if (dst)
850 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400851 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400852 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
853 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
854 return ERR_PTR(-ENETUNREACH);
855 }
856 if (dst->dev == dev) { /* is this necessary? */
857 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
858 dst_release(dst);
859 return ERR_PTR(-ELOOP);
860 }
861
Paolo Abeni468dfff2016-02-12 15:43:58 +0100862 if (use_cache)
863 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400864 return dst;
865}
866#endif
867
pravin shelar9b4437a2016-11-21 11:02:58 -0800868static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800869 struct geneve_dev *geneve,
870 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700871{
pravin shelar9b4437a2016-11-21 11:02:58 -0800872 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
873 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
874 const struct ip_tunnel_key *key = &info->key;
875 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400876 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400877 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700878 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700879 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800880 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400881
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700882 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 if (IS_ERR(rt))
884 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700885
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200886 skb_tunnel_check_pmtu(skb, &rt->dst,
887 GENEVE_IPV4_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800888
Pravin B Shelar371bd102015-08-26 23:46:54 -0700889 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800890 if (geneve->collect_md) {
891 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700892 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700893 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800894 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800895 if (geneve->ttl_inherit)
896 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
897 else
898 ttl = key->ttl;
899 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400900 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800901 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
902
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800903 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800904 if (unlikely(err))
905 return err;
906
Pravin B Shelar039f5062015-12-24 14:34:54 -0800907 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800908 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800909 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800910 !(info->key.tun_flags & TUNNEL_CSUM));
911 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400912}
913
John W. Linville8ed66f02015-10-26 17:01:44 -0400914#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800915static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800916 struct geneve_dev *geneve,
917 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400918{
pravin shelar9b4437a2016-11-21 11:02:58 -0800919 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
920 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
921 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400922 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400923 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400924 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400925 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800926 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400927
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700928 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800929 if (IS_ERR(dst))
930 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400931
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200932 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800933
John W. Linville8ed66f02015-10-26 17:01:44 -0400934 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800935 if (geneve->collect_md) {
936 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400937 ttl = key->ttl;
938 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100939 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800940 ip_hdr(skb), skb);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800941 if (geneve->ttl_inherit)
942 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
943 else
944 ttl = key->ttl;
945 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400946 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800947 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800948 if (unlikely(err))
949 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100950
Pravin B Shelar039f5062015-12-24 14:34:54 -0800951 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800952 &fl6.saddr, &fl6.daddr, prio, ttl,
953 info->key.label, sport, geneve->info.key.tp_dst,
954 !(info->key.tun_flags & TUNNEL_CSUM));
955 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400956}
957#endif
958
959static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
960{
961 struct geneve_dev *geneve = netdev_priv(dev);
962 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800963 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400964
pravin shelar9b4437a2016-11-21 11:02:58 -0800965 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400966 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800967 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
968 err = -EINVAL;
969 netdev_dbg(dev, "no tunnel metadata\n");
970 goto tx_error;
971 }
972 } else {
973 info = &geneve->info;
974 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400975
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800976 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400977#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800978 if (info->mode & IP_TUNNEL_INFO_IPV6)
979 err = geneve6_xmit_skb(skb, dev, geneve, info);
980 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400981#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800982 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800983 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800984
985 if (likely(!err))
986 return NETDEV_TX_OK;
987tx_error:
988 dev_kfree_skb(skb);
989
990 if (err == -ELOOP)
991 dev->stats.collisions++;
992 else if (err == -ENETUNREACH)
993 dev->stats.tx_carrier_errors++;
994
995 dev->stats.tx_errors++;
996 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400997}
998
Jarod Wilson91572082016-10-20 13:55:20 -0400999static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001000{
Jarod Wilson91572082016-10-20 13:55:20 -04001001 if (new_mtu > dev->max_mtu)
1002 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001003 else if (new_mtu < dev->min_mtu)
1004 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001005
David Wragg55e5bfb2016-02-10 00:05:57 +00001006 dev->mtu = new_mtu;
1007 return 0;
1008}
1009
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001010static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1011{
1012 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1013 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001014
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001015 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001016 struct rtable *rt;
1017 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001018 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -08001019
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001020 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001021 if (IS_ERR(rt))
1022 return PTR_ERR(rt);
1023
1024 ip_rt_put(rt);
1025 info->key.u.ipv4.src = fl4.saddr;
1026#if IS_ENABLED(CONFIG_IPV6)
1027 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001028 struct dst_entry *dst;
1029 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001030 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -08001031
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001032 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001033 if (IS_ERR(dst))
1034 return PTR_ERR(dst);
1035
1036 dst_release(dst);
1037 info->key.u.ipv6.src = fl6.saddr;
1038#endif
1039 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001040 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001041 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001042
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001043 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1044 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -08001045 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001046 return 0;
1047}
1048
John W. Linville2d07dc72015-05-13 12:57:30 -04001049static const struct net_device_ops geneve_netdev_ops = {
1050 .ndo_init = geneve_init,
1051 .ndo_uninit = geneve_uninit,
1052 .ndo_open = geneve_open,
1053 .ndo_stop = geneve_stop,
1054 .ndo_start_xmit = geneve_xmit,
1055 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001056 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001057 .ndo_validate_addr = eth_validate_addr,
1058 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001059 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001060};
1061
1062static void geneve_get_drvinfo(struct net_device *dev,
1063 struct ethtool_drvinfo *drvinfo)
1064{
1065 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1066 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1067}
1068
1069static const struct ethtool_ops geneve_ethtool_ops = {
1070 .get_drvinfo = geneve_get_drvinfo,
1071 .get_link = ethtool_op_get_link,
1072};
1073
1074/* Info for udev, that this is a virtual tunnel endpoint */
1075static struct device_type geneve_type = {
1076 .name = "geneve",
1077};
1078
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001079/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001080 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001081 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001082 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001083static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001084{
1085 struct net *net = dev_net(dev);
1086 struct geneve_net *gn = net_generic(net, geneve_net_id);
1087 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001088
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001089 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001090 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1091 if (push) {
1092 udp_tunnel_push_rx_port(dev, gs->sock,
1093 UDP_TUNNEL_TYPE_GENEVE);
1094 } else {
1095 udp_tunnel_drop_rx_port(dev, gs->sock,
1096 UDP_TUNNEL_TYPE_GENEVE);
1097 }
1098 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001099 rcu_read_unlock();
1100}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001101
John W. Linville2d07dc72015-05-13 12:57:30 -04001102/* Initialize the device structure. */
1103static void geneve_setup(struct net_device *dev)
1104{
1105 ether_setup(dev);
1106
1107 dev->netdev_ops = &geneve_netdev_ops;
1108 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001109 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001110
1111 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1112
John W. Linville2d07dc72015-05-13 12:57:30 -04001113 dev->features |= NETIF_F_LLTX;
1114 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1115 dev->features |= NETIF_F_RXCSUM;
1116 dev->features |= NETIF_F_GSO_SOFTWARE;
1117
John W. Linville2d07dc72015-05-13 12:57:30 -04001118 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1119 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001120
Jarod Wilson91572082016-10-20 13:55:20 -04001121 /* MTU range: 68 - (something less than 65535) */
1122 dev->min_mtu = ETH_MIN_MTU;
1123 /* The max_mtu calculation does not take account of GENEVE
1124 * options, to avoid excluding potentially valid
1125 * configurations. This will be further reduced by IPvX hdr size.
1126 */
1127 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1128
John W. Linville2d07dc72015-05-13 12:57:30 -04001129 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001130 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001131 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001132 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001133}
1134
1135static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1136 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1137 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001138 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001139 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001140 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001141 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001142 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001143 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001144 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1145 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1146 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001147 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001148};
1149
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001150static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1151 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001152{
1153 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001154 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1155 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1156 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001157 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001158 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001159
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001160 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1161 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1162 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001163 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001164 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001165 }
1166
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001167 if (!data) {
1168 NL_SET_ERR_MSG(extack,
1169 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001170 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001171 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001172
1173 if (data[IFLA_GENEVE_ID]) {
1174 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1175
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001176 if (vni >= GENEVE_N_VID) {
1177 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1178 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001179 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001180 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001181 }
1182
1183 return 0;
1184}
1185
Pravin B Shelar371bd102015-08-26 23:46:54 -07001186static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001187 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001188 bool *tun_on_same_port,
1189 bool *tun_collect_md)
1190{
pravin shelar9b4437a2016-11-21 11:02:58 -08001191 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001192
1193 *tun_on_same_port = false;
1194 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001195 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001196 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001197 *tun_collect_md = geneve->collect_md;
1198 *tun_on_same_port = true;
1199 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001200 if (info->key.tun_id == geneve->info.key.tun_id &&
1201 info->key.tp_dst == geneve->info.key.tp_dst &&
1202 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001203 t = geneve;
1204 }
1205 return t;
1206}
1207
pravin shelar9b4437a2016-11-21 11:02:58 -08001208static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1209{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001210 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1211 info->key.ttl || info->key.label || info->key.tp_src ||
1212 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001213}
1214
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001215static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1216 struct ip_tunnel_info *b)
1217{
1218 if (ip_tunnel_info_af(a) == AF_INET)
1219 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1220 else
1221 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1222}
1223
Pravin B Shelare305ac62015-08-26 23:46:52 -07001224static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001225 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001226 const struct ip_tunnel_info *info,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001227 bool metadata, bool ipv6_rx_csum,
1228 bool ttl_inherit)
John W. Linville2d07dc72015-05-13 12:57:30 -04001229{
1230 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001231 struct geneve_dev *t, *geneve = netdev_priv(dev);
1232 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001233 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001234
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001235 if (metadata && !is_tnl_info_zero(info)) {
1236 NL_SET_ERR_MSG(extack,
1237 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001238 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001239 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001240
1241 geneve->net = net;
1242 geneve->dev = dev;
1243
pravin shelar9b4437a2016-11-21 11:02:58 -08001244 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001245 if (t)
1246 return -EBUSY;
1247
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001248 /* make enough headroom for basic scenario */
1249 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001250 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001251 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001252 dev->max_mtu -= sizeof(struct iphdr);
1253 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001254 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001255 dev->max_mtu -= sizeof(struct ipv6hdr);
1256 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001257 dev->needed_headroom = encap_len + ETH_HLEN;
1258
Pravin B Shelar371bd102015-08-26 23:46:54 -07001259 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001260 if (tun_on_same_port) {
1261 NL_SET_ERR_MSG(extack,
1262 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001263 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001264 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001265 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001266 if (tun_collect_md) {
1267 NL_SET_ERR_MSG(extack,
1268 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001269 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001270 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001271 }
1272
pravin shelar9b4437a2016-11-21 11:02:58 -08001273 dst_cache_reset(&geneve->info.dst_cache);
1274 geneve->info = *info;
1275 geneve->collect_md = metadata;
1276 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001277 geneve->ttl_inherit = ttl_inherit;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001278
John W. Linville2d07dc72015-05-13 12:57:30 -04001279 err = register_netdevice(dev);
1280 if (err)
1281 return err;
1282
1283 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001284 return 0;
1285}
1286
pravin shelar9b4437a2016-11-21 11:02:58 -08001287static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1288{
1289 memset(info, 0, sizeof(*info));
1290 info->key.tp_dst = htons(dst_port);
1291}
1292
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001293static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1294 struct netlink_ext_ack *extack,
1295 struct ip_tunnel_info *info, bool *metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001296 bool *use_udp6_rx_checksums, bool *ttl_inherit,
1297 bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001298{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001299 int attrtype;
1300
1301 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1302 NL_SET_ERR_MSG(extack,
1303 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001304 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001305 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001306
1307 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001308 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1309 attrtype = IFLA_GENEVE_REMOTE;
1310 goto change_notsup;
1311 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001312
1313 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001314 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001315
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001316 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001317 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1318 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001319 return -EINVAL;
1320 }
1321 }
1322
pravin shelar9b4437a2016-11-21 11:02:58 -08001323 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001324#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001325 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1326 attrtype = IFLA_GENEVE_REMOTE6;
1327 goto change_notsup;
1328 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001329
1330 info->mode = IP_TUNNEL_INFO_IPV6;
1331 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001332 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001333
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001334 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001335 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001336 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1337 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001338 return -EINVAL;
1339 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001340 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001341 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1342 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001343 return -EINVAL;
1344 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001345 info->key.tun_flags |= TUNNEL_CSUM;
1346 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001347#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001348 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1349 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001350 return -EPFNOSUPPORT;
1351#endif
1352 }
1353
1354 if (data[IFLA_GENEVE_ID]) {
1355 __u32 vni;
1356 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001357 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001358
1359 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1360 tvni[0] = (vni & 0x00ff0000) >> 16;
1361 tvni[1] = (vni & 0x0000ff00) >> 8;
1362 tvni[2] = vni & 0x000000ff;
1363
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001364 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001365 if (changelink && (tunid != info->key.tun_id)) {
1366 attrtype = IFLA_GENEVE_ID;
1367 goto change_notsup;
1368 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001369 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001370 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001371
Hangbin Liua97d97b2018-09-29 23:06:29 +08001372 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1373 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1374 *ttl_inherit = true;
1375 else
1376 *ttl_inherit = false;
1377 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001378 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Hangbin Liua97d97b2018-09-29 23:06:29 +08001379 *ttl_inherit = false;
1380 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001381
Pravin B Shelare305ac62015-08-26 23:46:52 -07001382 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001383 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001384
pravin shelar9b4437a2016-11-21 11:02:58 -08001385 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001386 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001387 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001388 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1389 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1390 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001391 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001392 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001393 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001394
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001395 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001396 if (changelink) {
1397 attrtype = IFLA_GENEVE_PORT;
1398 goto change_notsup;
1399 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001400 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1401 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001402
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001403 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001404 if (changelink) {
1405 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1406 goto change_notsup;
1407 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001408 *metadata = true;
1409 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001410
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001411 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001412 if (changelink) {
1413 attrtype = IFLA_GENEVE_UDP_CSUM;
1414 goto change_notsup;
1415 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001416 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1417 info->key.tun_flags |= TUNNEL_CSUM;
1418 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001419
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001420 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001421#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001422 if (changelink) {
1423 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1424 goto change_notsup;
1425 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001426 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1427 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001428#else
1429 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1430 "IPv6 support not enabled in the kernel");
1431 return -EPFNOSUPPORT;
1432#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001433 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001434
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001435 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001436#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001437 if (changelink) {
1438 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1439 goto change_notsup;
1440 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001441 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1442 *use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001443#else
1444 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1445 "IPv6 support not enabled in the kernel");
1446 return -EPFNOSUPPORT;
1447#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001448 }
1449
1450 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001451change_notsup:
1452 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1453 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1454 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001455}
1456
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001457static void geneve_link_config(struct net_device *dev,
1458 struct ip_tunnel_info *info, struct nlattr *tb[])
1459{
1460 struct geneve_dev *geneve = netdev_priv(dev);
1461 int ldev_mtu = 0;
1462
1463 if (tb[IFLA_MTU]) {
1464 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1465 return;
1466 }
1467
1468 switch (ip_tunnel_info_af(info)) {
1469 case AF_INET: {
1470 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1471 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1472
1473 if (!IS_ERR(rt) && rt->dst.dev) {
1474 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1475 ip_rt_put(rt);
1476 }
1477 break;
1478 }
1479#if IS_ENABLED(CONFIG_IPV6)
1480 case AF_INET6: {
1481 struct rt6_info *rt = rt6_lookup(geneve->net,
1482 &info->key.u.ipv6.dst, NULL, 0,
1483 NULL, 0);
1484
1485 if (rt && rt->dst.dev)
1486 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1487 ip6_rt_put(rt);
1488 break;
1489 }
1490#endif
1491 }
1492
1493 if (ldev_mtu <= 0)
1494 return;
1495
1496 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1497}
1498
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001499static int geneve_newlink(struct net *net, struct net_device *dev,
1500 struct nlattr *tb[], struct nlattr *data[],
1501 struct netlink_ext_ack *extack)
1502{
1503 bool use_udp6_rx_checksums = false;
1504 struct ip_tunnel_info info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001505 bool ttl_inherit = false;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001506 bool metadata = false;
1507 int err;
1508
1509 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001510 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001511 &use_udp6_rx_checksums, &ttl_inherit, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001512 if (err)
1513 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001514
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001515 err = geneve_configure(net, dev, extack, &info, metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001516 use_udp6_rx_checksums, ttl_inherit);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001517 if (err)
1518 return err;
1519
1520 geneve_link_config(dev, &info, tb);
1521
1522 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001523}
1524
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001525/* Quiesces the geneve device data path for both TX and RX.
1526 *
1527 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1528 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1529 * to complete for the existing set of in-flight packets to be transmitted,
1530 * then we would have quiesced the transmit data path. All the future packets
1531 * will get dropped until we unquiesce the data path.
1532 *
1533 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1534 * if we set that to NULL under RCU and wait for synchronize_net() to
1535 * complete, then we would have quiesced the receive data path.
1536 */
1537static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1538 struct geneve_sock **gs6)
1539{
1540 *gs4 = rtnl_dereference(geneve->sock4);
1541 rcu_assign_pointer(geneve->sock4, NULL);
1542 if (*gs4)
1543 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1544#if IS_ENABLED(CONFIG_IPV6)
1545 *gs6 = rtnl_dereference(geneve->sock6);
1546 rcu_assign_pointer(geneve->sock6, NULL);
1547 if (*gs6)
1548 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1549#else
1550 *gs6 = NULL;
1551#endif
1552 synchronize_net();
1553}
1554
1555/* Resumes the geneve device data path for both TX and RX. */
1556static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1557 struct geneve_sock __maybe_unused *gs6)
1558{
1559 rcu_assign_pointer(geneve->sock4, gs4);
1560 if (gs4)
1561 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1562#if IS_ENABLED(CONFIG_IPV6)
1563 rcu_assign_pointer(geneve->sock6, gs6);
1564 if (gs6)
1565 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1566#endif
1567 synchronize_net();
1568}
1569
1570static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1571 struct nlattr *data[],
1572 struct netlink_ext_ack *extack)
1573{
1574 struct geneve_dev *geneve = netdev_priv(dev);
1575 struct geneve_sock *gs4, *gs6;
1576 struct ip_tunnel_info info;
1577 bool metadata;
1578 bool use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001579 bool ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001580 int err;
1581
1582 /* If the geneve device is configured for metadata (or externally
1583 * controlled, for example, OVS), then nothing can be changed.
1584 */
1585 if (geneve->collect_md)
1586 return -EOPNOTSUPP;
1587
1588 /* Start with the existing info. */
1589 memcpy(&info, &geneve->info, sizeof(info));
1590 metadata = geneve->collect_md;
1591 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001592 ttl_inherit = geneve->ttl_inherit;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001593 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001594 &use_udp6_rx_checksums, &ttl_inherit, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001595 if (err)
1596 return err;
1597
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001598 if (!geneve_dst_addr_equal(&geneve->info, &info)) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001599 dst_cache_reset(&info.dst_cache);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001600 geneve_link_config(dev, &info, tb);
1601 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001602
1603 geneve_quiesce(geneve, &gs4, &gs6);
1604 geneve->info = info;
1605 geneve->collect_md = metadata;
1606 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001607 geneve->ttl_inherit = ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001608 geneve_unquiesce(geneve, gs4, gs6);
1609
1610 return 0;
1611}
1612
John W. Linville2d07dc72015-05-13 12:57:30 -04001613static void geneve_dellink(struct net_device *dev, struct list_head *head)
1614{
1615 struct geneve_dev *geneve = netdev_priv(dev);
1616
John W. Linville2d07dc72015-05-13 12:57:30 -04001617 list_del(&geneve->next);
1618 unregister_netdevice_queue(dev, head);
1619}
1620
1621static size_t geneve_get_size(const struct net_device *dev)
1622{
1623 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001624 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001625 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001626 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001627 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001628 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001629 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001630 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1631 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1632 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001633 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001634 0;
1635}
1636
1637static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1638{
1639 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001640 struct ip_tunnel_info *info = &geneve->info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001641 bool ttl_inherit = geneve->ttl_inherit;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001642 bool metadata = geneve->collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001643 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001644 __u32 vni;
1645
pravin shelar9b4437a2016-11-21 11:02:58 -08001646 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1647 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001648 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1649 goto nla_put_failure;
1650
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001651 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001652 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001653 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001654 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001655 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1656 !!(info->key.tun_flags & TUNNEL_CSUM)))
1657 goto nla_put_failure;
1658
John W. Linville8ed66f02015-10-26 17:01:44 -04001659#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001660 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001661 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001662 &info->key.u.ipv6.dst))
1663 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001664 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1665 !(info->key.tun_flags & TUNNEL_CSUM)))
1666 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001667#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001668 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001669
pravin shelar9b4437a2016-11-21 11:02:58 -08001670 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1671 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1672 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001673 goto nla_put_failure;
1674
pravin shelar9b4437a2016-11-21 11:02:58 -08001675 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001676 goto nla_put_failure;
1677
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001678 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001679 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001680
Hangbin Liuf9094b72017-11-23 11:27:24 +08001681#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001682 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1683 !geneve->use_udp6_rx_checksums))
1684 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001685#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001686
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001687 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1688 goto nla_put_failure;
1689
John W. Linville2d07dc72015-05-13 12:57:30 -04001690 return 0;
1691
1692nla_put_failure:
1693 return -EMSGSIZE;
1694}
1695
1696static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1697 .kind = "geneve",
1698 .maxtype = IFLA_GENEVE_MAX,
1699 .policy = geneve_policy,
1700 .priv_size = sizeof(struct geneve_dev),
1701 .setup = geneve_setup,
1702 .validate = geneve_validate,
1703 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001704 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001705 .dellink = geneve_dellink,
1706 .get_size = geneve_get_size,
1707 .fill_info = geneve_fill_info,
1708};
1709
Pravin B Shelare305ac62015-08-26 23:46:52 -07001710struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1711 u8 name_assign_type, u16 dst_port)
1712{
1713 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001714 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001715 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001716 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001717 int err;
1718
1719 memset(tb, 0, sizeof(tb));
1720 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001721 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001722 if (IS_ERR(dev))
1723 return dev;
1724
pravin shelar9b4437a2016-11-21 11:02:58 -08001725 init_tnl_info(&info, dst_port);
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001726 err = geneve_configure(net, dev, NULL, &info, true, true, false);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001727 if (err) {
1728 free_netdev(dev);
1729 return ERR_PTR(err);
1730 }
David Wragg7e059152016-02-10 00:05:58 +00001731
1732 /* openvswitch users expect packet sizes to be unrestricted,
1733 * so set the largest MTU we can.
1734 */
Jarod Wilson91572082016-10-20 13:55:20 -04001735 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001736 if (err)
1737 goto err;
1738
Nicolas Dichtel41009482016-06-13 10:31:07 +02001739 err = rtnl_configure_link(dev, NULL);
1740 if (err < 0)
1741 goto err;
1742
Pravin B Shelare305ac62015-08-26 23:46:52 -07001743 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001744err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001745 geneve_dellink(dev, &list_kill);
1746 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001747 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001748}
1749EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1750
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001751static int geneve_netdevice_event(struct notifier_block *unused,
1752 unsigned long event, void *ptr)
1753{
1754 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1755
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001756 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001757 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001758 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001759 } else if (event == NETDEV_UNREGISTER) {
1760 geneve_offload_rx_ports(dev, false);
1761 } else if (event == NETDEV_REGISTER) {
1762 geneve_offload_rx_ports(dev, true);
1763 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001764
1765 return NOTIFY_DONE;
1766}
1767
1768static struct notifier_block geneve_notifier_block __read_mostly = {
1769 .notifier_call = geneve_netdevice_event,
1770};
1771
John W. Linville2d07dc72015-05-13 12:57:30 -04001772static __net_init int geneve_init_net(struct net *net)
1773{
1774 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001775
1776 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001777 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001778 return 0;
1779}
1780
Haishuang Yan2843a252017-12-16 17:54:50 +08001781static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001782{
1783 struct geneve_net *gn = net_generic(net, geneve_net_id);
1784 struct geneve_dev *geneve, *next;
1785 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001786
1787 /* gather any geneve devices that were moved into this ns */
1788 for_each_netdev_safe(net, dev, aux)
1789 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001790 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001791
1792 /* now gather any other geneve devices that were created in this ns */
1793 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1794 /* If geneve->dev is in the same netns, it was already added
1795 * to the list by the previous loop.
1796 */
1797 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001798 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001799 }
1800
Haishuang Yan2843a252017-12-16 17:54:50 +08001801 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1802}
1803
1804static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1805{
1806 struct net *net;
1807 LIST_HEAD(list);
1808
1809 rtnl_lock();
1810 list_for_each_entry(net, net_list, exit_list)
1811 geneve_destroy_tunnels(net, &list);
1812
John W. Linville2d07dc72015-05-13 12:57:30 -04001813 /* unregister the devices gathered above */
1814 unregister_netdevice_many(&list);
1815 rtnl_unlock();
1816}
1817
1818static struct pernet_operations geneve_net_ops = {
1819 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001820 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001821 .id = &geneve_net_id,
1822 .size = sizeof(struct geneve_net),
1823};
1824
1825static int __init geneve_init_module(void)
1826{
1827 int rc;
1828
1829 rc = register_pernet_subsys(&geneve_net_ops);
1830 if (rc)
1831 goto out1;
1832
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001833 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001834 if (rc)
1835 goto out2;
1836
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001837 rc = rtnl_link_register(&geneve_link_ops);
1838 if (rc)
1839 goto out3;
1840
John W. Linville2d07dc72015-05-13 12:57:30 -04001841 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001842out3:
1843 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001844out2:
1845 unregister_pernet_subsys(&geneve_net_ops);
1846out1:
1847 return rc;
1848}
1849late_initcall(geneve_init_module);
1850
1851static void __exit geneve_cleanup_module(void)
1852{
1853 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001854 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001855 unregister_pernet_subsys(&geneve_net_ops);
1856}
1857module_exit(geneve_cleanup_module);
1858
1859MODULE_LICENSE("GPL");
1860MODULE_VERSION(GENEVE_NETDEV_VER);
1861MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1862MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1863MODULE_ALIAS_RTNL_LINK("geneve");