blob: a0cd1c41cf5f07255283be656f1ecf074417532a [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
390static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800391 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700392{
393 struct socket *sock;
394 struct udp_port_cfg udp_conf;
395 int err;
396
397 memset(&udp_conf, 0, sizeof(udp_conf));
398
399 if (ipv6) {
400 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400401 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800402 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700403 } else {
404 udp_conf.family = AF_INET;
405 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
406 }
407
408 udp_conf.local_udp_port = port;
409
410 /* Open UDP socket */
411 err = udp_sock_create(net, &udp_conf, &sock);
412 if (err < 0)
413 return ERR_PTR(err);
414
415 return sock;
416}
417
Pravin B Shelar371bd102015-08-26 23:46:54 -0700418static int geneve_hlen(struct genevehdr *gh)
419{
420 return sizeof(*gh) + gh->opt_len * 4;
421}
422
David Millerd4546c22018-06-24 14:13:49 +0900423static struct sk_buff *geneve_gro_receive(struct sock *sk,
424 struct list_head *head,
425 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700426{
David Millerd4546c22018-06-24 14:13:49 +0900427 struct sk_buff *pp = NULL;
428 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700429 struct genevehdr *gh, *gh2;
430 unsigned int hlen, gh_len, off_gnv;
431 const struct packet_offload *ptype;
432 __be16 type;
433 int flush = 1;
434
435 off_gnv = skb_gro_offset(skb);
436 hlen = off_gnv + sizeof(*gh);
437 gh = skb_gro_header_fast(skb, off_gnv);
438 if (skb_gro_header_hard(skb, hlen)) {
439 gh = skb_gro_header_slow(skb, hlen, off_gnv);
440 if (unlikely(!gh))
441 goto out;
442 }
443
444 if (gh->ver != GENEVE_VER || gh->oam)
445 goto out;
446 gh_len = geneve_hlen(gh);
447
448 hlen = off_gnv + gh_len;
449 if (skb_gro_header_hard(skb, hlen)) {
450 gh = skb_gro_header_slow(skb, hlen, off_gnv);
451 if (unlikely(!gh))
452 goto out;
453 }
454
David Millerd4546c22018-06-24 14:13:49 +0900455 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700456 if (!NAPI_GRO_CB(p)->same_flow)
457 continue;
458
459 gh2 = (struct genevehdr *)(p->data + off_gnv);
460 if (gh->opt_len != gh2->opt_len ||
461 memcmp(gh, gh2, gh_len)) {
462 NAPI_GRO_CB(p)->same_flow = 0;
463 continue;
464 }
465 }
466
467 type = gh->proto_type;
468
469 rcu_read_lock();
470 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800471 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700472 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700473
474 skb_gro_pull(skb, gh_len);
475 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200476 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800477 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700478
479out_unlock:
480 rcu_read_unlock();
481out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200482 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700483
484 return pp;
485}
486
Tom Herbert4a0090a2016-04-05 08:22:55 -0700487static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
488 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700489{
490 struct genevehdr *gh;
491 struct packet_offload *ptype;
492 __be16 type;
493 int gh_len;
494 int err = -ENOSYS;
495
Pravin B Shelar371bd102015-08-26 23:46:54 -0700496 gh = (struct genevehdr *)(skb->data + nhoff);
497 gh_len = geneve_hlen(gh);
498 type = gh->proto_type;
499
500 rcu_read_lock();
501 ptype = gro_find_complete_by_type(type);
502 if (ptype)
503 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
504
505 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700506
507 skb_set_inner_mac_header(skb, nhoff + gh_len);
508
Pravin B Shelar371bd102015-08-26 23:46:54 -0700509 return err;
510}
511
512/* Create new listen socket if needed */
513static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800514 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700515{
516 struct geneve_net *gn = net_generic(net, geneve_net_id);
517 struct geneve_sock *gs;
518 struct socket *sock;
519 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700520 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700521
522 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
523 if (!gs)
524 return ERR_PTR(-ENOMEM);
525
pravin shelar9b4437a2016-11-21 11:02:58 -0800526 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700527 if (IS_ERR(sock)) {
528 kfree(gs);
529 return ERR_CAST(sock);
530 }
531
532 gs->sock = sock;
533 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700534 for (h = 0; h < VNI_HASH_SIZE; ++h)
535 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700536
537 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700538 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700539
540 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700541 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700542 tunnel_cfg.sk_user_data = gs;
543 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700544 tunnel_cfg.gro_receive = geneve_gro_receive;
545 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700546 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
547 tunnel_cfg.encap_destroy = NULL;
548 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700549 list_add(&gs->list, &gn->sock_list);
550 return gs;
551}
552
John W. Linville8ed66f02015-10-26 17:01:44 -0400553static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700554{
John W. Linville8ed66f02015-10-26 17:01:44 -0400555 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700556 return;
557
558 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700559 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700560 udp_tunnel_sock_release(gs->sock);
561 kfree_rcu(gs, rcu);
562}
563
John W. Linville8ed66f02015-10-26 17:01:44 -0400564static void geneve_sock_release(struct geneve_dev *geneve)
565{
pravin shelarfceb9c32016-10-28 09:59:16 -0700566 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400567#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700568 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
569
570 rcu_assign_pointer(geneve->sock6, NULL);
571#endif
572
573 rcu_assign_pointer(geneve->sock4, NULL);
574 synchronize_net();
575
576 __geneve_sock_release(gs4);
577#if IS_ENABLED(CONFIG_IPV6)
578 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400579#endif
580}
581
Pravin B Shelar371bd102015-08-26 23:46:54 -0700582static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400583 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584 __be16 dst_port)
585{
586 struct geneve_sock *gs;
587
588 list_for_each_entry(gs, &gn->sock_list, list) {
589 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100590 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700591 return gs;
592 }
593 }
594 return NULL;
595}
596
John W. Linville8ed66f02015-10-26 17:01:44 -0400597static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400598{
John W. Linville2d07dc72015-05-13 12:57:30 -0400599 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700600 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200601 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400602 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800603 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700604 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400605
pravin shelar9b4437a2016-11-21 11:02:58 -0800606 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700607 if (gs) {
608 gs->refcnt++;
609 goto out;
610 }
611
pravin shelar9b4437a2016-11-21 11:02:58 -0800612 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
613 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400614 if (IS_ERR(gs))
615 return PTR_ERR(gs);
616
Pravin B Shelar371bd102015-08-26 23:46:54 -0700617out:
618 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400619#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200620 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700621 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200622 node = &geneve->hlist6;
623 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400624#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200625 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700626 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200627 node = &geneve->hlist4;
628 }
629 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700630
pravin shelar9b4437a2016-11-21 11:02:58 -0800631 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
632 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200633 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400634 return 0;
635}
636
John W. Linville8ed66f02015-10-26 17:01:44 -0400637static int geneve_open(struct net_device *dev)
638{
639 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800640 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400641 bool metadata = geneve->collect_md;
642 int ret = 0;
643
John W. Linville8ed66f02015-10-26 17:01:44 -0400644#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400645 if (ipv6 || metadata)
646 ret = geneve_sock_add(geneve, true);
647#endif
648 if (!ret && (!ipv6 || metadata))
649 ret = geneve_sock_add(geneve, false);
650 if (ret < 0)
651 geneve_sock_release(geneve);
652
653 return ret;
654}
655
John W. Linville2d07dc72015-05-13 12:57:30 -0400656static int geneve_stop(struct net_device *dev)
657{
658 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400659
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200660 hlist_del_init_rcu(&geneve->hlist4.hlist);
661#if IS_ENABLED(CONFIG_IPV6)
662 hlist_del_init_rcu(&geneve->hlist6.hlist);
663#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400664 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400665 return 0;
666}
667
John W. Linville8ed66f02015-10-26 17:01:44 -0400668static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800669 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400670{
671 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800672 geneveh->opt_len = info->options_len / 4;
673 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
674 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400675 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800676 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400677 geneveh->proto_type = htons(ETH_P_TEB);
678 geneveh->rsvd2 = 0;
679
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700680 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
681 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400682}
683
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800684static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
685 const struct ip_tunnel_info *info,
686 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700687{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800688 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700689 struct genevehdr *gnvh;
690 int min_headroom;
691 int err;
692
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800693 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 skb_scrub_packet(skb, xnet);
695
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800696 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
697 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400699 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400700 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400701
Alexander Duyckaed069d2016-04-14 15:33:37 -0400702 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300703 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400704 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400705
Johannes Bergd58ff352017-06-16 14:29:23 +0200706 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800707 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400708 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
709 return 0;
710
711free_dst:
712 dst_release(dst);
713 return err;
714}
John W. Linville8ed66f02015-10-26 17:01:44 -0400715
716static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
717 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700718 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400719 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800720 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700721{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100722 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700723 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100724 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700725 struct rtable *rt = NULL;
726 __u8 tos;
727
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700728 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700729 return ERR_PTR(-EIO);
730
Pravin B Shelare305ac62015-08-26 23:46:52 -0700731 memset(fl4, 0, sizeof(*fl4));
732 fl4->flowi4_mark = skb->mark;
733 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800734 fl4->daddr = info->key.u.ipv4.dst;
735 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700736
pravin shelar9b4437a2016-11-21 11:02:58 -0800737 tos = info->key.tos;
738 if ((tos == 1) && !geneve->collect_md) {
739 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
740 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100741 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800742 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100743
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800744 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100745 if (use_cache) {
746 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
747 if (rt)
748 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700749 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700750 rt = ip_route_output_key(geneve->net, fl4);
751 if (IS_ERR(rt)) {
752 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700753 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754 }
755 if (rt->dst.dev == dev) { /* is this necessary? */
756 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700757 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700758 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700759 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100760 if (use_cache)
761 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700762 return rt;
763}
764
John W. Linville8ed66f02015-10-26 17:01:44 -0400765#if IS_ENABLED(CONFIG_IPV6)
766static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
767 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700768 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400769 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800770 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400771{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100772 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400773 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400774 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100775 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400776 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400777
pravin shelarfceb9c32016-10-28 09:59:16 -0700778 if (!gs6)
779 return ERR_PTR(-EIO);
780
John W. Linville8ed66f02015-10-26 17:01:44 -0400781 memset(fl6, 0, sizeof(*fl6));
782 fl6->flowi6_mark = skb->mark;
783 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800784 fl6->daddr = info->key.u.ipv6.dst;
785 fl6->saddr = info->key.u.ipv6.src;
786 prio = info->key.tos;
787 if ((prio == 1) && !geneve->collect_md) {
788 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
789 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100790 }
791
pravin shelar9b4437a2016-11-21 11:02:58 -0800792 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
793 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800794 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100795 if (use_cache) {
796 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
797 if (dst)
798 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400799 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400800 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
801 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
802 return ERR_PTR(-ENETUNREACH);
803 }
804 if (dst->dev == dev) { /* is this necessary? */
805 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
806 dst_release(dst);
807 return ERR_PTR(-ELOOP);
808 }
809
Paolo Abeni468dfff2016-02-12 15:43:58 +0100810 if (use_cache)
811 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400812 return dst;
813}
814#endif
815
pravin shelar9b4437a2016-11-21 11:02:58 -0800816static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800817 struct geneve_dev *geneve,
818 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700819{
pravin shelar9b4437a2016-11-21 11:02:58 -0800820 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
821 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
822 const struct ip_tunnel_key *key = &info->key;
823 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400824 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400825 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700826 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700827 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800828 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400829
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700830 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800831 if (IS_ERR(rt))
832 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700833
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200834 skb_tunnel_check_pmtu(skb, &rt->dst,
835 GENEVE_IPV4_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800836
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);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800843 if (geneve->ttl_inherit)
844 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
845 else
846 ttl = key->ttl;
847 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400848 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800849 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
850
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800851 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800852 if (unlikely(err))
853 return err;
854
Pravin B Shelar039f5062015-12-24 14:34:54 -0800855 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800856 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800857 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800858 !(info->key.tun_flags & TUNNEL_CSUM));
859 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400860}
861
John W. Linville8ed66f02015-10-26 17:01:44 -0400862#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800863static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800864 struct geneve_dev *geneve,
865 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400866{
pravin shelar9b4437a2016-11-21 11:02:58 -0800867 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
868 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
869 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400870 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400871 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400872 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400873 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800874 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400875
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700876 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800877 if (IS_ERR(dst))
878 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400879
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200880 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800881
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);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800889 if (geneve->ttl_inherit)
890 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
891 else
892 ttl = key->ttl;
893 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400894 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800895 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 if (unlikely(err))
897 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100898
Pravin B Shelar039f5062015-12-24 14:34:54 -0800899 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800900 &fl6.saddr, &fl6.daddr, prio, ttl,
901 info->key.label, sport, geneve->info.key.tp_dst,
902 !(info->key.tun_flags & TUNNEL_CSUM));
903 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400904}
905#endif
906
907static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
908{
909 struct geneve_dev *geneve = netdev_priv(dev);
910 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800911 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400912
pravin shelar9b4437a2016-11-21 11:02:58 -0800913 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400914 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800915 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
916 err = -EINVAL;
917 netdev_dbg(dev, "no tunnel metadata\n");
918 goto tx_error;
919 }
920 } else {
921 info = &geneve->info;
922 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400923
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800924 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400925#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800926 if (info->mode & IP_TUNNEL_INFO_IPV6)
927 err = geneve6_xmit_skb(skb, dev, geneve, info);
928 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400929#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800930 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800931 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800932
933 if (likely(!err))
934 return NETDEV_TX_OK;
935tx_error:
936 dev_kfree_skb(skb);
937
938 if (err == -ELOOP)
939 dev->stats.collisions++;
940 else if (err == -ENETUNREACH)
941 dev->stats.tx_carrier_errors++;
942
943 dev->stats.tx_errors++;
944 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400945}
946
Jarod Wilson91572082016-10-20 13:55:20 -0400947static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000948{
Jarod Wilson91572082016-10-20 13:55:20 -0400949 if (new_mtu > dev->max_mtu)
950 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +0300951 else if (new_mtu < dev->min_mtu)
952 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000953
David Wragg55e5bfb2016-02-10 00:05:57 +0000954 dev->mtu = new_mtu;
955 return 0;
956}
957
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700958static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
959{
960 struct ip_tunnel_info *info = skb_tunnel_info(skb);
961 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700962
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400963 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800964 struct rtable *rt;
965 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700966 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -0800967
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700968 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400969 if (IS_ERR(rt))
970 return PTR_ERR(rt);
971
972 ip_rt_put(rt);
973 info->key.u.ipv4.src = fl4.saddr;
974#if IS_ENABLED(CONFIG_IPV6)
975 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800976 struct dst_entry *dst;
977 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700978 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -0800979
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700980 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400981 if (IS_ERR(dst))
982 return PTR_ERR(dst);
983
984 dst_release(dst);
985 info->key.u.ipv6.src = fl6.saddr;
986#endif
987 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700988 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400989 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700990
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700991 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
992 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800993 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700994 return 0;
995}
996
John W. Linville2d07dc72015-05-13 12:57:30 -0400997static const struct net_device_ops geneve_netdev_ops = {
998 .ndo_init = geneve_init,
999 .ndo_uninit = geneve_uninit,
1000 .ndo_open = geneve_open,
1001 .ndo_stop = geneve_stop,
1002 .ndo_start_xmit = geneve_xmit,
1003 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001004 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001005 .ndo_validate_addr = eth_validate_addr,
1006 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001007 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001008};
1009
1010static void geneve_get_drvinfo(struct net_device *dev,
1011 struct ethtool_drvinfo *drvinfo)
1012{
1013 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1014 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1015}
1016
1017static const struct ethtool_ops geneve_ethtool_ops = {
1018 .get_drvinfo = geneve_get_drvinfo,
1019 .get_link = ethtool_op_get_link,
1020};
1021
1022/* Info for udev, that this is a virtual tunnel endpoint */
1023static struct device_type geneve_type = {
1024 .name = "geneve",
1025};
1026
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001027/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001028 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001029 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001030 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001031static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001032{
1033 struct net *net = dev_net(dev);
1034 struct geneve_net *gn = net_generic(net, geneve_net_id);
1035 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001036
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001037 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001038 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1039 if (push) {
1040 udp_tunnel_push_rx_port(dev, gs->sock,
1041 UDP_TUNNEL_TYPE_GENEVE);
1042 } else {
1043 udp_tunnel_drop_rx_port(dev, gs->sock,
1044 UDP_TUNNEL_TYPE_GENEVE);
1045 }
1046 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001047 rcu_read_unlock();
1048}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001049
John W. Linville2d07dc72015-05-13 12:57:30 -04001050/* Initialize the device structure. */
1051static void geneve_setup(struct net_device *dev)
1052{
1053 ether_setup(dev);
1054
1055 dev->netdev_ops = &geneve_netdev_ops;
1056 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001057 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001058
1059 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1060
John W. Linville2d07dc72015-05-13 12:57:30 -04001061 dev->features |= NETIF_F_LLTX;
1062 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1063 dev->features |= NETIF_F_RXCSUM;
1064 dev->features |= NETIF_F_GSO_SOFTWARE;
1065
John W. Linville2d07dc72015-05-13 12:57:30 -04001066 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1067 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001068
Jarod Wilson91572082016-10-20 13:55:20 -04001069 /* MTU range: 68 - (something less than 65535) */
1070 dev->min_mtu = ETH_MIN_MTU;
1071 /* The max_mtu calculation does not take account of GENEVE
1072 * options, to avoid excluding potentially valid
1073 * configurations. This will be further reduced by IPvX hdr size.
1074 */
1075 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1076
John W. Linville2d07dc72015-05-13 12:57:30 -04001077 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001078 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001079 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001080 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001081}
1082
1083static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1084 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1085 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001086 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001087 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001088 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001089 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001090 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001091 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001092 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1093 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1094 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001095 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001096};
1097
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001098static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1099 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001100{
1101 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001102 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1103 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1104 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001105 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001106 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001107
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001108 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1109 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1110 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001111 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001112 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001113 }
1114
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001115 if (!data) {
1116 NL_SET_ERR_MSG(extack,
1117 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001118 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001119 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001120
1121 if (data[IFLA_GENEVE_ID]) {
1122 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1123
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001124 if (vni >= GENEVE_N_VID) {
1125 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1126 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001127 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001128 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001129 }
1130
1131 return 0;
1132}
1133
Pravin B Shelar371bd102015-08-26 23:46:54 -07001134static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001135 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001136 bool *tun_on_same_port,
1137 bool *tun_collect_md)
1138{
pravin shelar9b4437a2016-11-21 11:02:58 -08001139 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001140
1141 *tun_on_same_port = false;
1142 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001143 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001144 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001145 *tun_collect_md = geneve->collect_md;
1146 *tun_on_same_port = true;
1147 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001148 if (info->key.tun_id == geneve->info.key.tun_id &&
1149 info->key.tp_dst == geneve->info.key.tp_dst &&
1150 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001151 t = geneve;
1152 }
1153 return t;
1154}
1155
pravin shelar9b4437a2016-11-21 11:02:58 -08001156static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1157{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001158 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1159 info->key.ttl || info->key.label || info->key.tp_src ||
1160 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001161}
1162
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001163static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1164 struct ip_tunnel_info *b)
1165{
1166 if (ip_tunnel_info_af(a) == AF_INET)
1167 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1168 else
1169 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1170}
1171
Pravin B Shelare305ac62015-08-26 23:46:52 -07001172static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001173 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001174 const struct ip_tunnel_info *info,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001175 bool metadata, bool ipv6_rx_csum,
1176 bool ttl_inherit)
John W. Linville2d07dc72015-05-13 12:57:30 -04001177{
1178 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001179 struct geneve_dev *t, *geneve = netdev_priv(dev);
1180 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001181 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001182
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001183 if (metadata && !is_tnl_info_zero(info)) {
1184 NL_SET_ERR_MSG(extack,
1185 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001186 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001187 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001188
1189 geneve->net = net;
1190 geneve->dev = dev;
1191
pravin shelar9b4437a2016-11-21 11:02:58 -08001192 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001193 if (t)
1194 return -EBUSY;
1195
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001196 /* make enough headroom for basic scenario */
1197 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001198 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001199 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001200 dev->max_mtu -= sizeof(struct iphdr);
1201 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001202 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001203 dev->max_mtu -= sizeof(struct ipv6hdr);
1204 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001205 dev->needed_headroom = encap_len + ETH_HLEN;
1206
Pravin B Shelar371bd102015-08-26 23:46:54 -07001207 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001208 if (tun_on_same_port) {
1209 NL_SET_ERR_MSG(extack,
1210 "There can be only one externally controlled device on a 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 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001214 if (tun_collect_md) {
1215 NL_SET_ERR_MSG(extack,
1216 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001217 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001218 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001219 }
1220
pravin shelar9b4437a2016-11-21 11:02:58 -08001221 dst_cache_reset(&geneve->info.dst_cache);
1222 geneve->info = *info;
1223 geneve->collect_md = metadata;
1224 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001225 geneve->ttl_inherit = ttl_inherit;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001226
John W. Linville2d07dc72015-05-13 12:57:30 -04001227 err = register_netdevice(dev);
1228 if (err)
1229 return err;
1230
1231 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001232 return 0;
1233}
1234
pravin shelar9b4437a2016-11-21 11:02:58 -08001235static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1236{
1237 memset(info, 0, sizeof(*info));
1238 info->key.tp_dst = htons(dst_port);
1239}
1240
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001241static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1242 struct netlink_ext_ack *extack,
1243 struct ip_tunnel_info *info, bool *metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001244 bool *use_udp6_rx_checksums, bool *ttl_inherit,
1245 bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001246{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001247 int attrtype;
1248
1249 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1250 NL_SET_ERR_MSG(extack,
1251 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001252 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001253 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001254
1255 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001256 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1257 attrtype = IFLA_GENEVE_REMOTE;
1258 goto change_notsup;
1259 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001260
1261 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001262 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001263
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001264 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001265 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1266 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001267 return -EINVAL;
1268 }
1269 }
1270
pravin shelar9b4437a2016-11-21 11:02:58 -08001271 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001272#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001273 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1274 attrtype = IFLA_GENEVE_REMOTE6;
1275 goto change_notsup;
1276 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001277
1278 info->mode = IP_TUNNEL_INFO_IPV6;
1279 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001280 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001281
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001282 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001283 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001284 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1285 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001286 return -EINVAL;
1287 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001288 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001289 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1290 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001291 return -EINVAL;
1292 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001293 info->key.tun_flags |= TUNNEL_CSUM;
1294 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001295#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001296 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1297 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001298 return -EPFNOSUPPORT;
1299#endif
1300 }
1301
1302 if (data[IFLA_GENEVE_ID]) {
1303 __u32 vni;
1304 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001305 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001306
1307 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1308 tvni[0] = (vni & 0x00ff0000) >> 16;
1309 tvni[1] = (vni & 0x0000ff00) >> 8;
1310 tvni[2] = vni & 0x000000ff;
1311
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001312 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001313 if (changelink && (tunid != info->key.tun_id)) {
1314 attrtype = IFLA_GENEVE_ID;
1315 goto change_notsup;
1316 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001317 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001318 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001319
Hangbin Liua97d97b2018-09-29 23:06:29 +08001320 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1321 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1322 *ttl_inherit = true;
1323 else
1324 *ttl_inherit = false;
1325 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001326 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Hangbin Liua97d97b2018-09-29 23:06:29 +08001327 *ttl_inherit = false;
1328 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001329
Pravin B Shelare305ac62015-08-26 23:46:52 -07001330 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001331 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001332
pravin shelar9b4437a2016-11-21 11:02:58 -08001333 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001334 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001335 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001336 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1337 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1338 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001339 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001340 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001341 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001342
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001343 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001344 if (changelink) {
1345 attrtype = IFLA_GENEVE_PORT;
1346 goto change_notsup;
1347 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001348 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1349 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001350
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001351 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001352 if (changelink) {
1353 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1354 goto change_notsup;
1355 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001356 *metadata = true;
1357 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001358
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001359 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001360 if (changelink) {
1361 attrtype = IFLA_GENEVE_UDP_CSUM;
1362 goto change_notsup;
1363 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001364 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1365 info->key.tun_flags |= TUNNEL_CSUM;
1366 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001367
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001368 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
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_TX;
1372 goto change_notsup;
1373 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001374 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1375 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001376#else
1377 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1378 "IPv6 support not enabled in the kernel");
1379 return -EPFNOSUPPORT;
1380#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001381 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001382
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001383 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001384#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001385 if (changelink) {
1386 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1387 goto change_notsup;
1388 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001389 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1390 *use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001391#else
1392 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1393 "IPv6 support not enabled in the kernel");
1394 return -EPFNOSUPPORT;
1395#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001396 }
1397
1398 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001399change_notsup:
1400 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1401 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1402 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001403}
1404
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001405static void geneve_link_config(struct net_device *dev,
1406 struct ip_tunnel_info *info, struct nlattr *tb[])
1407{
1408 struct geneve_dev *geneve = netdev_priv(dev);
1409 int ldev_mtu = 0;
1410
1411 if (tb[IFLA_MTU]) {
1412 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1413 return;
1414 }
1415
1416 switch (ip_tunnel_info_af(info)) {
1417 case AF_INET: {
1418 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1419 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1420
1421 if (!IS_ERR(rt) && rt->dst.dev) {
1422 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1423 ip_rt_put(rt);
1424 }
1425 break;
1426 }
1427#if IS_ENABLED(CONFIG_IPV6)
1428 case AF_INET6: {
1429 struct rt6_info *rt = rt6_lookup(geneve->net,
1430 &info->key.u.ipv6.dst, NULL, 0,
1431 NULL, 0);
1432
1433 if (rt && rt->dst.dev)
1434 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1435 ip6_rt_put(rt);
1436 break;
1437 }
1438#endif
1439 }
1440
1441 if (ldev_mtu <= 0)
1442 return;
1443
1444 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1445}
1446
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001447static int geneve_newlink(struct net *net, struct net_device *dev,
1448 struct nlattr *tb[], struct nlattr *data[],
1449 struct netlink_ext_ack *extack)
1450{
1451 bool use_udp6_rx_checksums = false;
1452 struct ip_tunnel_info info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001453 bool ttl_inherit = false;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001454 bool metadata = false;
1455 int err;
1456
1457 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001458 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001459 &use_udp6_rx_checksums, &ttl_inherit, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001460 if (err)
1461 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001462
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001463 err = geneve_configure(net, dev, extack, &info, metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001464 use_udp6_rx_checksums, ttl_inherit);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001465 if (err)
1466 return err;
1467
1468 geneve_link_config(dev, &info, tb);
1469
1470 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001471}
1472
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001473/* Quiesces the geneve device data path for both TX and RX.
1474 *
1475 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1476 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1477 * to complete for the existing set of in-flight packets to be transmitted,
1478 * then we would have quiesced the transmit data path. All the future packets
1479 * will get dropped until we unquiesce the data path.
1480 *
1481 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1482 * if we set that to NULL under RCU and wait for synchronize_net() to
1483 * complete, then we would have quiesced the receive data path.
1484 */
1485static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1486 struct geneve_sock **gs6)
1487{
1488 *gs4 = rtnl_dereference(geneve->sock4);
1489 rcu_assign_pointer(geneve->sock4, NULL);
1490 if (*gs4)
1491 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1492#if IS_ENABLED(CONFIG_IPV6)
1493 *gs6 = rtnl_dereference(geneve->sock6);
1494 rcu_assign_pointer(geneve->sock6, NULL);
1495 if (*gs6)
1496 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1497#else
1498 *gs6 = NULL;
1499#endif
1500 synchronize_net();
1501}
1502
1503/* Resumes the geneve device data path for both TX and RX. */
1504static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1505 struct geneve_sock __maybe_unused *gs6)
1506{
1507 rcu_assign_pointer(geneve->sock4, gs4);
1508 if (gs4)
1509 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1510#if IS_ENABLED(CONFIG_IPV6)
1511 rcu_assign_pointer(geneve->sock6, gs6);
1512 if (gs6)
1513 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1514#endif
1515 synchronize_net();
1516}
1517
1518static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1519 struct nlattr *data[],
1520 struct netlink_ext_ack *extack)
1521{
1522 struct geneve_dev *geneve = netdev_priv(dev);
1523 struct geneve_sock *gs4, *gs6;
1524 struct ip_tunnel_info info;
1525 bool metadata;
1526 bool use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001527 bool ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001528 int err;
1529
1530 /* If the geneve device is configured for metadata (or externally
1531 * controlled, for example, OVS), then nothing can be changed.
1532 */
1533 if (geneve->collect_md)
1534 return -EOPNOTSUPP;
1535
1536 /* Start with the existing info. */
1537 memcpy(&info, &geneve->info, sizeof(info));
1538 metadata = geneve->collect_md;
1539 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001540 ttl_inherit = geneve->ttl_inherit;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001541 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001542 &use_udp6_rx_checksums, &ttl_inherit, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001543 if (err)
1544 return err;
1545
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001546 if (!geneve_dst_addr_equal(&geneve->info, &info)) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001547 dst_cache_reset(&info.dst_cache);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001548 geneve_link_config(dev, &info, tb);
1549 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001550
1551 geneve_quiesce(geneve, &gs4, &gs6);
1552 geneve->info = info;
1553 geneve->collect_md = metadata;
1554 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001555 geneve->ttl_inherit = ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001556 geneve_unquiesce(geneve, gs4, gs6);
1557
1558 return 0;
1559}
1560
John W. Linville2d07dc72015-05-13 12:57:30 -04001561static void geneve_dellink(struct net_device *dev, struct list_head *head)
1562{
1563 struct geneve_dev *geneve = netdev_priv(dev);
1564
John W. Linville2d07dc72015-05-13 12:57:30 -04001565 list_del(&geneve->next);
1566 unregister_netdevice_queue(dev, head);
1567}
1568
1569static size_t geneve_get_size(const struct net_device *dev)
1570{
1571 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001572 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001573 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001574 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001575 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001576 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001577 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001578 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1579 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1580 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001581 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001582 0;
1583}
1584
1585static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1586{
1587 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001588 struct ip_tunnel_info *info = &geneve->info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001589 bool ttl_inherit = geneve->ttl_inherit;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001590 bool metadata = geneve->collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001591 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001592 __u32 vni;
1593
pravin shelar9b4437a2016-11-21 11:02:58 -08001594 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1595 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001596 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1597 goto nla_put_failure;
1598
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001599 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001600 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001601 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001602 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001603 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1604 !!(info->key.tun_flags & TUNNEL_CSUM)))
1605 goto nla_put_failure;
1606
John W. Linville8ed66f02015-10-26 17:01:44 -04001607#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001608 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001609 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001610 &info->key.u.ipv6.dst))
1611 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001612 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1613 !(info->key.tun_flags & TUNNEL_CSUM)))
1614 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001615#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001616 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001617
pravin shelar9b4437a2016-11-21 11:02:58 -08001618 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1619 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1620 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001621 goto nla_put_failure;
1622
pravin shelar9b4437a2016-11-21 11:02:58 -08001623 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001624 goto nla_put_failure;
1625
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001626 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001627 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001628
Hangbin Liuf9094b72017-11-23 11:27:24 +08001629#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001630 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1631 !geneve->use_udp6_rx_checksums))
1632 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001633#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001634
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001635 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1636 goto nla_put_failure;
1637
John W. Linville2d07dc72015-05-13 12:57:30 -04001638 return 0;
1639
1640nla_put_failure:
1641 return -EMSGSIZE;
1642}
1643
1644static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1645 .kind = "geneve",
1646 .maxtype = IFLA_GENEVE_MAX,
1647 .policy = geneve_policy,
1648 .priv_size = sizeof(struct geneve_dev),
1649 .setup = geneve_setup,
1650 .validate = geneve_validate,
1651 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001652 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001653 .dellink = geneve_dellink,
1654 .get_size = geneve_get_size,
1655 .fill_info = geneve_fill_info,
1656};
1657
Pravin B Shelare305ac62015-08-26 23:46:52 -07001658struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1659 u8 name_assign_type, u16 dst_port)
1660{
1661 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001662 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001663 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001664 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001665 int err;
1666
1667 memset(tb, 0, sizeof(tb));
1668 dev = rtnl_create_link(net, name, name_assign_type,
1669 &geneve_link_ops, tb);
1670 if (IS_ERR(dev))
1671 return dev;
1672
pravin shelar9b4437a2016-11-21 11:02:58 -08001673 init_tnl_info(&info, dst_port);
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001674 err = geneve_configure(net, dev, NULL, &info, true, true, false);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001675 if (err) {
1676 free_netdev(dev);
1677 return ERR_PTR(err);
1678 }
David Wragg7e059152016-02-10 00:05:58 +00001679
1680 /* openvswitch users expect packet sizes to be unrestricted,
1681 * so set the largest MTU we can.
1682 */
Jarod Wilson91572082016-10-20 13:55:20 -04001683 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001684 if (err)
1685 goto err;
1686
Nicolas Dichtel41009482016-06-13 10:31:07 +02001687 err = rtnl_configure_link(dev, NULL);
1688 if (err < 0)
1689 goto err;
1690
Pravin B Shelare305ac62015-08-26 23:46:52 -07001691 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001692err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001693 geneve_dellink(dev, &list_kill);
1694 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001695 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001696}
1697EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1698
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001699static int geneve_netdevice_event(struct notifier_block *unused,
1700 unsigned long event, void *ptr)
1701{
1702 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1703
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001704 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001705 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001706 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001707 } else if (event == NETDEV_UNREGISTER) {
1708 geneve_offload_rx_ports(dev, false);
1709 } else if (event == NETDEV_REGISTER) {
1710 geneve_offload_rx_ports(dev, true);
1711 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001712
1713 return NOTIFY_DONE;
1714}
1715
1716static struct notifier_block geneve_notifier_block __read_mostly = {
1717 .notifier_call = geneve_netdevice_event,
1718};
1719
John W. Linville2d07dc72015-05-13 12:57:30 -04001720static __net_init int geneve_init_net(struct net *net)
1721{
1722 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001723
1724 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001725 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001726 return 0;
1727}
1728
Haishuang Yan2843a252017-12-16 17:54:50 +08001729static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001730{
1731 struct geneve_net *gn = net_generic(net, geneve_net_id);
1732 struct geneve_dev *geneve, *next;
1733 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001734
1735 /* gather any geneve devices that were moved into this ns */
1736 for_each_netdev_safe(net, dev, aux)
1737 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001738 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001739
1740 /* now gather any other geneve devices that were created in this ns */
1741 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1742 /* If geneve->dev is in the same netns, it was already added
1743 * to the list by the previous loop.
1744 */
1745 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001746 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001747 }
1748
Haishuang Yan2843a252017-12-16 17:54:50 +08001749 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1750}
1751
1752static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1753{
1754 struct net *net;
1755 LIST_HEAD(list);
1756
1757 rtnl_lock();
1758 list_for_each_entry(net, net_list, exit_list)
1759 geneve_destroy_tunnels(net, &list);
1760
John W. Linville2d07dc72015-05-13 12:57:30 -04001761 /* unregister the devices gathered above */
1762 unregister_netdevice_many(&list);
1763 rtnl_unlock();
1764}
1765
1766static struct pernet_operations geneve_net_ops = {
1767 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001768 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001769 .id = &geneve_net_id,
1770 .size = sizeof(struct geneve_net),
1771};
1772
1773static int __init geneve_init_module(void)
1774{
1775 int rc;
1776
1777 rc = register_pernet_subsys(&geneve_net_ops);
1778 if (rc)
1779 goto out1;
1780
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001781 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001782 if (rc)
1783 goto out2;
1784
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001785 rc = rtnl_link_register(&geneve_link_ops);
1786 if (rc)
1787 goto out3;
1788
John W. Linville2d07dc72015-05-13 12:57:30 -04001789 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001790out3:
1791 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001792out2:
1793 unregister_pernet_subsys(&geneve_net_ops);
1794out1:
1795 return rc;
1796}
1797late_initcall(geneve_init_module);
1798
1799static void __exit geneve_cleanup_module(void)
1800{
1801 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001802 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001803 unregister_pernet_subsys(&geneve_net_ops);
1804}
1805module_exit(geneve_cleanup_module);
1806
1807MODULE_LICENSE("GPL");
1808MODULE_VERSION(GENEVE_NETDEV_VER);
1809MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1810MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1811MODULE_ALIAS_RTNL_LINK("geneve");