blob: ff626dbde23fface3e916555abeb76ed4f4ced3a [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))
39
John W. Linville2d07dc72015-05-13 12:57:30 -040040/* per-network namespace private data for this module */
41struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070042 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040044};
45
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030046static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070047
John W. Linville2d07dc72015-05-13 12:57:30 -040048/* Pseudo network device */
49struct geneve_dev {
50 struct hlist_node hlist; /* vni hash table */
51 struct net *net; /* netns for packet i/o */
52 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelar9b4437a2016-11-21 11:02:58 -080053 struct ip_tunnel_info info;
pravin shelarfceb9c32016-10-28 09:59:16 -070054 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040055#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070056 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040057#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040058 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070059 struct gro_cells gro_cells;
pravin shelar9b4437a2016-11-21 11:02:58 -080060 bool collect_md;
61 bool use_udp6_rx_checksums;
John W. Linville2d07dc72015-05-13 12:57:30 -040062};
63
Pravin B Shelar371bd102015-08-26 23:46:54 -070064struct geneve_sock {
65 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070066 struct list_head list;
67 struct socket *sock;
68 struct rcu_head rcu;
69 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070070 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070071};
John W. Linville2d07dc72015-05-13 12:57:30 -040072
73static inline __u32 geneve_net_vni_hash(u8 vni[3])
74{
75 __u32 vnid;
76
77 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
78 return hash_32(vnid, VNI_HASH_BITS);
79}
80
Pravin B Shelare305ac62015-08-26 23:46:52 -070081static __be64 vni_to_tunnel_id(const __u8 *vni)
82{
83#ifdef __BIG_ENDIAN
84 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
85#else
86 return (__force __be64)(((__force u64)vni[0] << 40) |
87 ((__force u64)vni[1] << 48) |
88 ((__force u64)vni[2] << 56));
89#endif
90}
91
pravin shelar9b4437a2016-11-21 11:02:58 -080092/* Convert 64 bit tunnel ID to 24 bit VNI. */
93static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
94{
95#ifdef __BIG_ENDIAN
96 vni[0] = (__force __u8)(tun_id >> 16);
97 vni[1] = (__force __u8)(tun_id >> 8);
98 vni[2] = (__force __u8)tun_id;
99#else
100 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
101 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
102 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
103#endif
104}
105
pravin shelar2e0b26e2016-11-21 11:03:01 -0800106static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
107{
108#ifdef __BIG_ENDIAN
109 return (vni[0] == tun_id[2]) &&
110 (vni[1] == tun_id[1]) &&
111 (vni[2] == tun_id[0]);
112#else
113 return !memcmp(vni, &tun_id[5], 3);
114#endif
115}
116
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100117static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
118{
119 return gs->sock->sk->sk_family;
120}
121
Pravin B Shelar66d47002015-08-26 23:46:55 -0700122static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700123 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400124{
John W. Linville2d07dc72015-05-13 12:57:30 -0400125 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700126 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400127 __u32 hash;
128
John W. Linville2d07dc72015-05-13 12:57:30 -0400129 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700130 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700131 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700132 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
pravin shelar2e0b26e2016-11-21 11:03:01 -0800133 if (eq_tun_id_and_vni((u8 *)&geneve->info.key.tun_id, vni) &&
pravin shelar9b4437a2016-11-21 11:02:58 -0800134 addr == geneve->info.key.u.ipv4.dst)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700135 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400136 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700137 return NULL;
138}
139
John W. Linville8ed66f02015-10-26 17:01:44 -0400140#if IS_ENABLED(CONFIG_IPV6)
141static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
142 struct in6_addr addr6, u8 vni[])
143{
144 struct hlist_head *vni_list_head;
145 struct geneve_dev *geneve;
146 __u32 hash;
147
148 /* Find the device for this VNI */
149 hash = geneve_net_vni_hash(vni);
150 vni_list_head = &gs->vni_list[hash];
151 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
pravin shelar2e0b26e2016-11-21 11:03:01 -0800152 if (eq_tun_id_and_vni((u8 *)&geneve->info.key.tun_id, vni) &&
pravin shelar9b4437a2016-11-21 11:02:58 -0800153 ipv6_addr_equal(&addr6, &geneve->info.key.u.ipv6.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -0400154 return geneve;
155 }
156 return NULL;
157}
158#endif
159
Pravin B Shelar371bd102015-08-26 23:46:54 -0700160static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
161{
162 return (struct genevehdr *)(udp_hdr(skb) + 1);
163}
164
Jiri Benc9fc47542016-02-18 11:22:50 +0100165static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
166 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700167{
John W. Linville8ed66f02015-10-26 17:01:44 -0400168 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800169 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700170
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100171 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100172 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800173 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100174
John W. Linville8ed66f02015-10-26 17:01:44 -0400175 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700176
John W. Linville8ed66f02015-10-26 17:01:44 -0400177 if (gs->collect_md) {
178 vni = zero_vni;
179 addr = 0;
180 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100181 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400182 addr = iph->saddr;
183 }
184
Jiri Benc9fc47542016-02-18 11:22:50 +0100185 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400186#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100187 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800188 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100189 struct ipv6hdr *ip6h;
190 struct in6_addr addr6;
191
John W. Linville8ed66f02015-10-26 17:01:44 -0400192 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
193
194 if (gs->collect_md) {
195 vni = zero_vni;
196 addr6 = zero_addr6;
197 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100198 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400199 addr6 = ip6h->saddr;
200 }
201
Jiri Benc9fc47542016-02-18 11:22:50 +0100202 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400203#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700204 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100205 return NULL;
206}
207
208/* geneve receive/decap routine */
209static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
210 struct sk_buff *skb)
211{
212 struct genevehdr *gnvh = geneve_hdr(skb);
213 struct metadata_dst *tun_dst = NULL;
214 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700215 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100216 int err = 0;
217 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400218
Pravin B Shelar371bd102015-08-26 23:46:54 -0700219 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700220 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700221
222 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
223 (gnvh->oam ? TUNNEL_OAM : 0) |
224 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
225
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100226 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700227 vni_to_tunnel_id(gnvh->vni),
228 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700229 if (!tun_dst) {
230 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700231 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700232 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700234 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
235 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700236 } else {
237 /* Drop packets w/ critical options,
238 * since we don't support any...
239 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700240 if (gnvh->critical) {
241 geneve->dev->stats.rx_frame_errors++;
242 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700243 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700244 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700245 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400246
247 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400248 skb->protocol = eth_type_trans(skb, geneve->dev);
249 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
250
Pravin B Shelare305ac62015-08-26 23:46:52 -0700251 if (tun_dst)
252 skb_dst_set(skb, &tun_dst->dst);
253
John W. Linville2d07dc72015-05-13 12:57:30 -0400254 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700255 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
256 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400257 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700258 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400259
Jiri Benc9fc47542016-02-18 11:22:50 +0100260 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400261 skb_reset_network_header(skb);
262
Jiri Benc9fc47542016-02-18 11:22:50 +0100263 if (geneve_get_sk_family(gs) == AF_INET)
264 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400265#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100266 else
267 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400268#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400269
270 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400271 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100272 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400273 net_info_ratelimited("non-ECT from %pI4 "
274 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100275 &((struct iphdr *)oiph)->saddr,
276 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400277#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100278 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400279 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100280 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400281#endif
282 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400283 if (err > 1) {
284 ++geneve->dev->stats.rx_frame_errors;
285 ++geneve->dev->stats.rx_errors;
286 goto drop;
287 }
288 }
289
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700290 len = skb->len;
291 err = gro_cells_receive(&geneve->gro_cells, skb);
292 if (likely(err == NET_RX_SUCCESS)) {
293 stats = this_cpu_ptr(geneve->dev->tstats);
294 u64_stats_update_begin(&stats->syncp);
295 stats->rx_packets++;
296 stats->rx_bytes += len;
297 u64_stats_update_end(&stats->syncp);
298 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400299 return;
300drop:
301 /* Consume bad packet */
302 kfree_skb(skb);
303}
304
305/* Setup stats when device is created */
306static int geneve_init(struct net_device *dev)
307{
Jesse Gross8e816df2015-08-28 16:54:40 -0700308 struct geneve_dev *geneve = netdev_priv(dev);
309 int err;
310
John W. Linville2d07dc72015-05-13 12:57:30 -0400311 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
312 if (!dev->tstats)
313 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700314
315 err = gro_cells_init(&geneve->gro_cells, dev);
316 if (err) {
317 free_percpu(dev->tstats);
318 return err;
319 }
320
pravin shelar9b4437a2016-11-21 11:02:58 -0800321 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100322 if (err) {
323 free_percpu(dev->tstats);
324 gro_cells_destroy(&geneve->gro_cells);
325 return err;
326 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400327 return 0;
328}
329
330static void geneve_uninit(struct net_device *dev)
331{
Jesse Gross8e816df2015-08-28 16:54:40 -0700332 struct geneve_dev *geneve = netdev_priv(dev);
333
pravin shelar9b4437a2016-11-21 11:02:58 -0800334 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700335 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400336 free_percpu(dev->tstats);
337}
338
Pravin B Shelar371bd102015-08-26 23:46:54 -0700339/* Callback from net/ipv4/udp.c to receive packets */
340static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
341{
342 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100343 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700344 struct geneve_sock *gs;
345 int opts_len;
346
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700347 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700348 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200349 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700350
351 /* Return packets with reserved bits set */
352 geneveh = geneve_hdr(skb);
353 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200354 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700355
356 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200357 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700358
Jiri Benc9fc47542016-02-18 11:22:50 +0100359 gs = rcu_dereference_sk_user_data(sk);
360 if (!gs)
361 goto drop;
362
363 geneve = geneve_lookup_skb(gs, skb);
364 if (!geneve)
365 goto drop;
366
Pravin B Shelar371bd102015-08-26 23:46:54 -0700367 opts_len = geneveh->opt_len * 4;
368 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100369 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700370 !net_eq(geneve->net, dev_net(geneve->dev)))) {
371 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700372 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700373 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700374
Jiri Benc9fc47542016-02-18 11:22:50 +0100375 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700376 return 0;
377
378drop:
379 /* Consume bad packet */
380 kfree_skb(skb);
381 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700382}
383
384static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800385 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700386{
387 struct socket *sock;
388 struct udp_port_cfg udp_conf;
389 int err;
390
391 memset(&udp_conf, 0, sizeof(udp_conf));
392
393 if (ipv6) {
394 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400395 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800396 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700397 } else {
398 udp_conf.family = AF_INET;
399 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
400 }
401
402 udp_conf.local_udp_port = port;
403
404 /* Open UDP socket */
405 err = udp_sock_create(net, &udp_conf, &sock);
406 if (err < 0)
407 return ERR_PTR(err);
408
409 return sock;
410}
411
Pravin B Shelar371bd102015-08-26 23:46:54 -0700412static int geneve_hlen(struct genevehdr *gh)
413{
414 return sizeof(*gh) + gh->opt_len * 4;
415}
416
Tom Herbert4a0090a2016-04-05 08:22:55 -0700417static struct sk_buff **geneve_gro_receive(struct sock *sk,
418 struct sk_buff **head,
419 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700420{
421 struct sk_buff *p, **pp = NULL;
422 struct genevehdr *gh, *gh2;
423 unsigned int hlen, gh_len, off_gnv;
424 const struct packet_offload *ptype;
425 __be16 type;
426 int flush = 1;
427
428 off_gnv = skb_gro_offset(skb);
429 hlen = off_gnv + sizeof(*gh);
430 gh = skb_gro_header_fast(skb, off_gnv);
431 if (skb_gro_header_hard(skb, hlen)) {
432 gh = skb_gro_header_slow(skb, hlen, off_gnv);
433 if (unlikely(!gh))
434 goto out;
435 }
436
437 if (gh->ver != GENEVE_VER || gh->oam)
438 goto out;
439 gh_len = geneve_hlen(gh);
440
441 hlen = off_gnv + gh_len;
442 if (skb_gro_header_hard(skb, hlen)) {
443 gh = skb_gro_header_slow(skb, hlen, off_gnv);
444 if (unlikely(!gh))
445 goto out;
446 }
447
Pravin B Shelar371bd102015-08-26 23:46:54 -0700448 for (p = *head; p; p = p->next) {
449 if (!NAPI_GRO_CB(p)->same_flow)
450 continue;
451
452 gh2 = (struct genevehdr *)(p->data + off_gnv);
453 if (gh->opt_len != gh2->opt_len ||
454 memcmp(gh, gh2, gh_len)) {
455 NAPI_GRO_CB(p)->same_flow = 0;
456 continue;
457 }
458 }
459
460 type = gh->proto_type;
461
462 rcu_read_lock();
463 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800464 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700465 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700466
467 skb_gro_pull(skb, gh_len);
468 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200469 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800470 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700471
472out_unlock:
473 rcu_read_unlock();
474out:
475 NAPI_GRO_CB(skb)->flush |= flush;
476
477 return pp;
478}
479
Tom Herbert4a0090a2016-04-05 08:22:55 -0700480static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
481 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700482{
483 struct genevehdr *gh;
484 struct packet_offload *ptype;
485 __be16 type;
486 int gh_len;
487 int err = -ENOSYS;
488
Pravin B Shelar371bd102015-08-26 23:46:54 -0700489 gh = (struct genevehdr *)(skb->data + nhoff);
490 gh_len = geneve_hlen(gh);
491 type = gh->proto_type;
492
493 rcu_read_lock();
494 ptype = gro_find_complete_by_type(type);
495 if (ptype)
496 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
497
498 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700499
500 skb_set_inner_mac_header(skb, nhoff + gh_len);
501
Pravin B Shelar371bd102015-08-26 23:46:54 -0700502 return err;
503}
504
505/* Create new listen socket if needed */
506static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800507 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700508{
509 struct geneve_net *gn = net_generic(net, geneve_net_id);
510 struct geneve_sock *gs;
511 struct socket *sock;
512 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700513 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700514
515 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
516 if (!gs)
517 return ERR_PTR(-ENOMEM);
518
pravin shelar9b4437a2016-11-21 11:02:58 -0800519 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700520 if (IS_ERR(sock)) {
521 kfree(gs);
522 return ERR_CAST(sock);
523 }
524
525 gs->sock = sock;
526 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700527 for (h = 0; h < VNI_HASH_SIZE; ++h)
528 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700529
530 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700531 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700532
533 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700534 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700535 tunnel_cfg.sk_user_data = gs;
536 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700537 tunnel_cfg.gro_receive = geneve_gro_receive;
538 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700539 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
540 tunnel_cfg.encap_destroy = NULL;
541 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700542 list_add(&gs->list, &gn->sock_list);
543 return gs;
544}
545
John W. Linville8ed66f02015-10-26 17:01:44 -0400546static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700547{
John W. Linville8ed66f02015-10-26 17:01:44 -0400548 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700549 return;
550
551 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700552 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700553 udp_tunnel_sock_release(gs->sock);
554 kfree_rcu(gs, rcu);
555}
556
John W. Linville8ed66f02015-10-26 17:01:44 -0400557static void geneve_sock_release(struct geneve_dev *geneve)
558{
pravin shelarfceb9c32016-10-28 09:59:16 -0700559 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400560#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700561 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
562
563 rcu_assign_pointer(geneve->sock6, NULL);
564#endif
565
566 rcu_assign_pointer(geneve->sock4, NULL);
567 synchronize_net();
568
569 __geneve_sock_release(gs4);
570#if IS_ENABLED(CONFIG_IPV6)
571 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400572#endif
573}
574
Pravin B Shelar371bd102015-08-26 23:46:54 -0700575static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400576 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700577 __be16 dst_port)
578{
579 struct geneve_sock *gs;
580
581 list_for_each_entry(gs, &gn->sock_list, list) {
582 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100583 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584 return gs;
585 }
586 }
587 return NULL;
588}
589
John W. Linville8ed66f02015-10-26 17:01:44 -0400590static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400591{
John W. Linville2d07dc72015-05-13 12:57:30 -0400592 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700593 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400594 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800595 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700596 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400597
pravin shelar9b4437a2016-11-21 11:02:58 -0800598 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700599 if (gs) {
600 gs->refcnt++;
601 goto out;
602 }
603
pravin shelar9b4437a2016-11-21 11:02:58 -0800604 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
605 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400606 if (IS_ERR(gs))
607 return PTR_ERR(gs);
608
Pravin B Shelar371bd102015-08-26 23:46:54 -0700609out:
610 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400611#if IS_ENABLED(CONFIG_IPV6)
612 if (ipv6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700613 rcu_assign_pointer(geneve->sock6, gs);
John W. Linville8ed66f02015-10-26 17:01:44 -0400614 else
615#endif
pravin shelarfceb9c32016-10-28 09:59:16 -0700616 rcu_assign_pointer(geneve->sock4, gs);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700617
pravin shelar9b4437a2016-11-21 11:02:58 -0800618 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
619 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700620 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400621 return 0;
622}
623
John W. Linville8ed66f02015-10-26 17:01:44 -0400624static int geneve_open(struct net_device *dev)
625{
626 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800627 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400628 bool metadata = geneve->collect_md;
629 int ret = 0;
630
John W. Linville8ed66f02015-10-26 17:01:44 -0400631#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400632 if (ipv6 || metadata)
633 ret = geneve_sock_add(geneve, true);
634#endif
635 if (!ret && (!ipv6 || metadata))
636 ret = geneve_sock_add(geneve, false);
637 if (ret < 0)
638 geneve_sock_release(geneve);
639
640 return ret;
641}
642
John W. Linville2d07dc72015-05-13 12:57:30 -0400643static int geneve_stop(struct net_device *dev)
644{
645 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400646
Pravin B Shelar66d47002015-08-26 23:46:55 -0700647 if (!hlist_unhashed(&geneve->hlist))
648 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400649 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400650 return 0;
651}
652
John W. Linville8ed66f02015-10-26 17:01:44 -0400653static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800654 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400655{
656 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800657 geneveh->opt_len = info->options_len / 4;
658 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
659 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400660 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800661 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400662 geneveh->proto_type = htons(ETH_P_TEB);
663 geneveh->rsvd2 = 0;
664
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800665 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400666}
667
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800668static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
669 const struct ip_tunnel_info *info,
670 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700671{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800672 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700673 struct genevehdr *gnvh;
674 int min_headroom;
675 int err;
676
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800677 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400678 skb_scrub_packet(skb, xnet);
679
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800680 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
681 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400682 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400683 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400684 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400685
Alexander Duyckaed069d2016-04-14 15:33:37 -0400686 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300687 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400688 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400689
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800690 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) +
691 info->options_len);
692 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400693 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
694 return 0;
695
696free_dst:
697 dst_release(dst);
698 return err;
699}
John W. Linville8ed66f02015-10-26 17:01:44 -0400700
701static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
702 struct net_device *dev,
703 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800704 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700705{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100706 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700707 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100708 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700709 struct rtable *rt = NULL;
710 __u8 tos;
711
pravin shelarfceb9c32016-10-28 09:59:16 -0700712 if (!rcu_dereference(geneve->sock4))
713 return ERR_PTR(-EIO);
714
Pravin B Shelare305ac62015-08-26 23:46:52 -0700715 memset(fl4, 0, sizeof(*fl4));
716 fl4->flowi4_mark = skb->mark;
717 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800718 fl4->daddr = info->key.u.ipv4.dst;
719 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700720
pravin shelar9b4437a2016-11-21 11:02:58 -0800721 tos = info->key.tos;
722 if ((tos == 1) && !geneve->collect_md) {
723 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
724 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100725 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800726 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100727
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800728 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100729 if (use_cache) {
730 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
731 if (rt)
732 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700733 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700734 rt = ip_route_output_key(geneve->net, fl4);
735 if (IS_ERR(rt)) {
736 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700737 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700738 }
739 if (rt->dst.dev == dev) { /* is this necessary? */
740 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700741 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700742 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700743 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100744 if (use_cache)
745 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700746 return rt;
747}
748
John W. Linville8ed66f02015-10-26 17:01:44 -0400749#if IS_ENABLED(CONFIG_IPV6)
750static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
751 struct net_device *dev,
752 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800753 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400754{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100755 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400756 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400757 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100758 struct dst_cache *dst_cache;
pravin shelarfceb9c32016-10-28 09:59:16 -0700759 struct geneve_sock *gs6;
John W. Linville3a56f862015-10-26 17:01:45 -0400760 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400761
pravin shelarfceb9c32016-10-28 09:59:16 -0700762 gs6 = rcu_dereference(geneve->sock6);
763 if (!gs6)
764 return ERR_PTR(-EIO);
765
John W. Linville8ed66f02015-10-26 17:01:44 -0400766 memset(fl6, 0, sizeof(*fl6));
767 fl6->flowi6_mark = skb->mark;
768 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800769 fl6->daddr = info->key.u.ipv6.dst;
770 fl6->saddr = info->key.u.ipv6.src;
771 prio = info->key.tos;
772 if ((prio == 1) && !geneve->collect_md) {
773 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
774 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100775 }
776
pravin shelar9b4437a2016-11-21 11:02:58 -0800777 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
778 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800779 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100780 if (use_cache) {
781 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
782 if (dst)
783 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400784 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400785 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
786 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
787 return ERR_PTR(-ENETUNREACH);
788 }
789 if (dst->dev == dev) { /* is this necessary? */
790 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
791 dst_release(dst);
792 return ERR_PTR(-ELOOP);
793 }
794
Paolo Abeni468dfff2016-02-12 15:43:58 +0100795 if (use_cache)
796 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400797 return dst;
798}
799#endif
800
pravin shelar9b4437a2016-11-21 11:02:58 -0800801static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800802 struct geneve_dev *geneve,
803 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700804{
pravin shelar9b4437a2016-11-21 11:02:58 -0800805 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
806 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
807 const struct ip_tunnel_key *key = &info->key;
808 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400809 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400810 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700811 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700812 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800813 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400814
John W. Linville8ed66f02015-10-26 17:01:44 -0400815 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800816 if (IS_ERR(rt))
817 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700818
819 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800820 if (geneve->collect_md) {
821 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700822 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700823 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800824 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
825 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400826 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800827 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
828
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800829 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800830 if (unlikely(err))
831 return err;
832
Pravin B Shelar039f5062015-12-24 14:34:54 -0800833 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800834 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800835 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800836 !(info->key.tun_flags & TUNNEL_CSUM));
837 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400838}
839
John W. Linville8ed66f02015-10-26 17:01:44 -0400840#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800841static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800842 struct geneve_dev *geneve,
843 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400844{
pravin shelar9b4437a2016-11-21 11:02:58 -0800845 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
846 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
847 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400848 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400849 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400850 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400851 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800852 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400853
854 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800855 if (IS_ERR(dst))
856 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400857
858 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800859 if (geneve->collect_md) {
860 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400861 ttl = key->ttl;
862 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100863 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800864 ip_hdr(skb), skb);
865 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400866 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800867 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800868 if (unlikely(err))
869 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100870
Pravin B Shelar039f5062015-12-24 14:34:54 -0800871 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800872 &fl6.saddr, &fl6.daddr, prio, ttl,
873 info->key.label, sport, geneve->info.key.tp_dst,
874 !(info->key.tun_flags & TUNNEL_CSUM));
875 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400876}
877#endif
878
879static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
880{
881 struct geneve_dev *geneve = netdev_priv(dev);
882 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400884
pravin shelar9b4437a2016-11-21 11:02:58 -0800885 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400886 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800887 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
888 err = -EINVAL;
889 netdev_dbg(dev, "no tunnel metadata\n");
890 goto tx_error;
891 }
892 } else {
893 info = &geneve->info;
894 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400895
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800896 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400897#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800898 if (info->mode & IP_TUNNEL_INFO_IPV6)
899 err = geneve6_xmit_skb(skb, dev, geneve, info);
900 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400901#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800902 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800903 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800904
905 if (likely(!err))
906 return NETDEV_TX_OK;
907tx_error:
908 dev_kfree_skb(skb);
909
910 if (err == -ELOOP)
911 dev->stats.collisions++;
912 else if (err == -ENETUNREACH)
913 dev->stats.tx_carrier_errors++;
914
915 dev->stats.tx_errors++;
916 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400917}
918
Jarod Wilson91572082016-10-20 13:55:20 -0400919static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000920{
Jarod Wilson91572082016-10-20 13:55:20 -0400921 /* Only possible if called internally, ndo_change_mtu path's new_mtu
922 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
David Wragg55e5bfb2016-02-10 00:05:57 +0000923 */
Jarod Wilson91572082016-10-20 13:55:20 -0400924 if (new_mtu > dev->max_mtu)
925 new_mtu = dev->max_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000926
David Wragg55e5bfb2016-02-10 00:05:57 +0000927 dev->mtu = new_mtu;
928 return 0;
929}
930
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700931static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
932{
933 struct ip_tunnel_info *info = skb_tunnel_info(skb);
934 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700935
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400936 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800937 struct rtable *rt;
938 struct flowi4 fl4;
939
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400940 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
941 if (IS_ERR(rt))
942 return PTR_ERR(rt);
943
944 ip_rt_put(rt);
945 info->key.u.ipv4.src = fl4.saddr;
946#if IS_ENABLED(CONFIG_IPV6)
947 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800948 struct dst_entry *dst;
949 struct flowi6 fl6;
950
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400951 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
952 if (IS_ERR(dst))
953 return PTR_ERR(dst);
954
955 dst_release(dst);
956 info->key.u.ipv6.src = fl6.saddr;
957#endif
958 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700959 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400960 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700961
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700962 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
963 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800964 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700965 return 0;
966}
967
John W. Linville2d07dc72015-05-13 12:57:30 -0400968static const struct net_device_ops geneve_netdev_ops = {
969 .ndo_init = geneve_init,
970 .ndo_uninit = geneve_uninit,
971 .ndo_open = geneve_open,
972 .ndo_stop = geneve_stop,
973 .ndo_start_xmit = geneve_xmit,
974 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +0000975 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -0400976 .ndo_validate_addr = eth_validate_addr,
977 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700978 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -0400979};
980
981static void geneve_get_drvinfo(struct net_device *dev,
982 struct ethtool_drvinfo *drvinfo)
983{
984 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
985 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
986}
987
988static const struct ethtool_ops geneve_ethtool_ops = {
989 .get_drvinfo = geneve_get_drvinfo,
990 .get_link = ethtool_op_get_link,
991};
992
993/* Info for udev, that this is a virtual tunnel endpoint */
994static struct device_type geneve_type = {
995 .name = "geneve",
996};
997
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +0200998/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800999 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001000 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001001 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001002static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001003{
1004 struct net *net = dev_net(dev);
1005 struct geneve_net *gn = net_generic(net, geneve_net_id);
1006 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001007
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001008 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -07001009 list_for_each_entry_rcu(gs, &gn->sock_list, list)
1010 udp_tunnel_push_rx_port(dev, gs->sock,
1011 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001012 rcu_read_unlock();
1013}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001014
John W. Linville2d07dc72015-05-13 12:57:30 -04001015/* Initialize the device structure. */
1016static void geneve_setup(struct net_device *dev)
1017{
1018 ether_setup(dev);
1019
1020 dev->netdev_ops = &geneve_netdev_ops;
1021 dev->ethtool_ops = &geneve_ethtool_ops;
1022 dev->destructor = free_netdev;
1023
1024 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1025
John W. Linville2d07dc72015-05-13 12:57:30 -04001026 dev->features |= NETIF_F_LLTX;
1027 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1028 dev->features |= NETIF_F_RXCSUM;
1029 dev->features |= NETIF_F_GSO_SOFTWARE;
1030
John W. Linville2d07dc72015-05-13 12:57:30 -04001031 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1032 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001033
Jarod Wilson91572082016-10-20 13:55:20 -04001034 /* MTU range: 68 - (something less than 65535) */
1035 dev->min_mtu = ETH_MIN_MTU;
1036 /* The max_mtu calculation does not take account of GENEVE
1037 * options, to avoid excluding potentially valid
1038 * configurations. This will be further reduced by IPvX hdr size.
1039 */
1040 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1041
John W. Linville2d07dc72015-05-13 12:57:30 -04001042 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001043 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001044 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001045 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001046}
1047
1048static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1049 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1050 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001051 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001052 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001053 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001054 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001055 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001056 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001057 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1058 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1059 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001060};
1061
1062static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1063{
1064 if (tb[IFLA_ADDRESS]) {
1065 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1066 return -EINVAL;
1067
1068 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1069 return -EADDRNOTAVAIL;
1070 }
1071
1072 if (!data)
1073 return -EINVAL;
1074
1075 if (data[IFLA_GENEVE_ID]) {
1076 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1077
1078 if (vni >= GENEVE_VID_MASK)
1079 return -ERANGE;
1080 }
1081
1082 return 0;
1083}
1084
Pravin B Shelar371bd102015-08-26 23:46:54 -07001085static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001086 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001087 bool *tun_on_same_port,
1088 bool *tun_collect_md)
1089{
pravin shelar9b4437a2016-11-21 11:02:58 -08001090 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001091
1092 *tun_on_same_port = false;
1093 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001094 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001095 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001096 *tun_collect_md = geneve->collect_md;
1097 *tun_on_same_port = true;
1098 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001099 if (info->key.tun_id == geneve->info.key.tun_id &&
1100 info->key.tp_dst == geneve->info.key.tp_dst &&
1101 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001102 t = geneve;
1103 }
1104 return t;
1105}
1106
pravin shelar9b4437a2016-11-21 11:02:58 -08001107static bool is_all_zero(const u8 *fp, size_t size)
1108{
1109 int i;
1110
1111 for (i = 0; i < size; i++)
1112 if (fp[i])
1113 return false;
1114 return true;
1115}
1116
1117static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1118{
1119 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1120 info->key.ttl || info->key.label || info->key.tp_src ||
1121 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1122 return false;
1123 else
1124 return true;
1125}
1126
Pravin B Shelare305ac62015-08-26 23:46:52 -07001127static int geneve_configure(struct net *net, struct net_device *dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001128 const struct ip_tunnel_info *info,
1129 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001130{
1131 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001132 struct geneve_dev *t, *geneve = netdev_priv(dev);
1133 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001134 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001135
pravin shelar9b4437a2016-11-21 11:02:58 -08001136 if (metadata && !is_tnl_info_zero(info))
John W. Linville8ed66f02015-10-26 17:01:44 -04001137 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001138
1139 geneve->net = net;
1140 geneve->dev = dev;
1141
pravin shelar9b4437a2016-11-21 11:02:58 -08001142 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001143 if (t)
1144 return -EBUSY;
1145
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001146 /* make enough headroom for basic scenario */
1147 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001148 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001149 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001150 dev->max_mtu -= sizeof(struct iphdr);
1151 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001152 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001153 dev->max_mtu -= sizeof(struct ipv6hdr);
1154 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001155 dev->needed_headroom = encap_len + ETH_HLEN;
1156
Pravin B Shelar371bd102015-08-26 23:46:54 -07001157 if (metadata) {
1158 if (tun_on_same_port)
1159 return -EPERM;
1160 } else {
1161 if (tun_collect_md)
1162 return -EPERM;
1163 }
1164
pravin shelar9b4437a2016-11-21 11:02:58 -08001165 dst_cache_reset(&geneve->info.dst_cache);
1166 geneve->info = *info;
1167 geneve->collect_md = metadata;
1168 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001169
John W. Linville2d07dc72015-05-13 12:57:30 -04001170 err = register_netdevice(dev);
1171 if (err)
1172 return err;
1173
1174 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001175 return 0;
1176}
1177
pravin shelar9b4437a2016-11-21 11:02:58 -08001178static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1179{
1180 memset(info, 0, sizeof(*info));
1181 info->key.tp_dst = htons(dst_port);
1182}
1183
Pravin B Shelare305ac62015-08-26 23:46:52 -07001184static int geneve_newlink(struct net *net, struct net_device *dev,
1185 struct nlattr *tb[], struct nlattr *data[])
1186{
pravin shelar9b4437a2016-11-21 11:02:58 -08001187 bool use_udp6_rx_checksums = false;
1188 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001189 bool metadata = false;
pravin shelar9b4437a2016-11-21 11:02:58 -08001190
1191 init_tnl_info(&info, GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001192
John W. Linville8ed66f02015-10-26 17:01:44 -04001193 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1194 return -EINVAL;
1195
1196 if (data[IFLA_GENEVE_REMOTE]) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001197 info.key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001198 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001199
pravin shelar9b4437a2016-11-21 11:02:58 -08001200 if (IN_MULTICAST(ntohl(info.key.u.ipv4.dst))) {
1201 netdev_dbg(dev, "multicast remote is unsupported\n");
John W. Linville8ed66f02015-10-26 17:01:44 -04001202 return -EINVAL;
1203 }
1204 }
1205
pravin shelar9b4437a2016-11-21 11:02:58 -08001206 if (data[IFLA_GENEVE_REMOTE6]) {
1207 #if IS_ENABLED(CONFIG_IPV6)
1208 info.mode = IP_TUNNEL_INFO_IPV6;
1209 info.key.u.ipv6.dst =
1210 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001211
pravin shelar9b4437a2016-11-21 11:02:58 -08001212 if (ipv6_addr_type(&info.key.u.ipv6.dst) &
1213 IPV6_ADDR_LINKLOCAL) {
1214 netdev_dbg(dev, "link-local remote is unsupported\n");
1215 return -EINVAL;
1216 }
1217 if (ipv6_addr_is_multicast(&info.key.u.ipv6.dst)) {
1218 netdev_dbg(dev, "multicast remote is unsupported\n");
1219 return -EINVAL;
1220 }
1221 info.key.tun_flags |= TUNNEL_CSUM;
1222 use_udp6_rx_checksums = true;
1223#else
1224 return -EPFNOSUPPORT;
1225#endif
1226 }
1227
1228 if (data[IFLA_GENEVE_ID]) {
1229 __u32 vni;
1230 __u8 tvni[3];
1231
1232 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1233 tvni[0] = (vni & 0x00ff0000) >> 16;
1234 tvni[1] = (vni & 0x0000ff00) >> 8;
1235 tvni[2] = vni & 0x000000ff;
1236
1237 info.key.tun_id = vni_to_tunnel_id(tvni);
1238 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001239 if (data[IFLA_GENEVE_TTL])
pravin shelar9b4437a2016-11-21 11:02:58 -08001240 info.key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001241
1242 if (data[IFLA_GENEVE_TOS])
pravin shelar9b4437a2016-11-21 11:02:58 -08001243 info.key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001244
pravin shelar9b4437a2016-11-21 11:02:58 -08001245 if (data[IFLA_GENEVE_LABEL]) {
1246 info.key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1247 IPV6_FLOWLABEL_MASK;
1248 if (info.key.label && (!(info.mode & IP_TUNNEL_INFO_IPV6)))
1249 return -EINVAL;
1250 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001251
Pravin B Shelare305ac62015-08-26 23:46:52 -07001252 if (data[IFLA_GENEVE_PORT])
pravin shelar9b4437a2016-11-21 11:02:58 -08001253 info.key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001254
1255 if (data[IFLA_GENEVE_COLLECT_METADATA])
1256 metadata = true;
1257
Tom Herbertabe492b2015-12-10 12:37:45 -08001258 if (data[IFLA_GENEVE_UDP_CSUM] &&
Girish Moodalbail5e0740c2017-04-27 14:11:53 -07001259 nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001260 info.key.tun_flags |= TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001261
1262 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1263 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001264 info.key.tun_flags &= ~TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001265
1266 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1267 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001268 use_udp6_rx_checksums = false;
Tom Herbertabe492b2015-12-10 12:37:45 -08001269
pravin shelar9b4437a2016-11-21 11:02:58 -08001270 return geneve_configure(net, dev, &info, metadata, use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001271}
1272
John W. Linville2d07dc72015-05-13 12:57:30 -04001273static void geneve_dellink(struct net_device *dev, struct list_head *head)
1274{
1275 struct geneve_dev *geneve = netdev_priv(dev);
1276
John W. Linville2d07dc72015-05-13 12:57:30 -04001277 list_del(&geneve->next);
1278 unregister_netdevice_queue(dev, head);
1279}
1280
1281static size_t geneve_get_size(const struct net_device *dev)
1282{
1283 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001284 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001285 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001286 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001287 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001288 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001289 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001290 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1291 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1292 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001293 0;
1294}
1295
1296static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1297{
1298 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001299 struct ip_tunnel_info *info = &geneve->info;
1300 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001301 __u32 vni;
1302
pravin shelar9b4437a2016-11-21 11:02:58 -08001303 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1304 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001305 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1306 goto nla_put_failure;
1307
Eric Garver11387fe2017-05-23 18:37:27 -04001308 if (rtnl_dereference(geneve->sock4)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001309 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001310 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001311 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001312
1313 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1314 !!(info->key.tun_flags & TUNNEL_CSUM)))
1315 goto nla_put_failure;
1316
Eric Garver11387fe2017-05-23 18:37:27 -04001317 }
1318
John W. Linville8ed66f02015-10-26 17:01:44 -04001319#if IS_ENABLED(CONFIG_IPV6)
Eric Garver11387fe2017-05-23 18:37:27 -04001320 if (rtnl_dereference(geneve->sock6)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001321 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001322 &info->key.u.ipv6.dst))
1323 goto nla_put_failure;
1324
1325 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1326 !(info->key.tun_flags & TUNNEL_CSUM)))
1327 goto nla_put_failure;
1328
1329 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1330 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001331 goto nla_put_failure;
John W. Linville8ed66f02015-10-26 17:01:44 -04001332 }
Eric Garver11387fe2017-05-23 18:37:27 -04001333#endif
John W. Linville2d07dc72015-05-13 12:57:30 -04001334
pravin shelar9b4437a2016-11-21 11:02:58 -08001335 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1336 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1337 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001338 goto nla_put_failure;
1339
pravin shelar9b4437a2016-11-21 11:02:58 -08001340 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001341 goto nla_put_failure;
1342
Pravin B Shelare305ac62015-08-26 23:46:52 -07001343 if (geneve->collect_md) {
1344 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1345 goto nla_put_failure;
1346 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001347 return 0;
1348
1349nla_put_failure:
1350 return -EMSGSIZE;
1351}
1352
1353static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1354 .kind = "geneve",
1355 .maxtype = IFLA_GENEVE_MAX,
1356 .policy = geneve_policy,
1357 .priv_size = sizeof(struct geneve_dev),
1358 .setup = geneve_setup,
1359 .validate = geneve_validate,
1360 .newlink = geneve_newlink,
1361 .dellink = geneve_dellink,
1362 .get_size = geneve_get_size,
1363 .fill_info = geneve_fill_info,
1364};
1365
Pravin B Shelare305ac62015-08-26 23:46:52 -07001366struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1367 u8 name_assign_type, u16 dst_port)
1368{
1369 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001370 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001371 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001372 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001373 int err;
1374
1375 memset(tb, 0, sizeof(tb));
1376 dev = rtnl_create_link(net, name, name_assign_type,
1377 &geneve_link_ops, tb);
1378 if (IS_ERR(dev))
1379 return dev;
1380
pravin shelar9b4437a2016-11-21 11:02:58 -08001381 init_tnl_info(&info, dst_port);
1382 err = geneve_configure(net, dev, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001383 if (err) {
1384 free_netdev(dev);
1385 return ERR_PTR(err);
1386 }
David Wragg7e059152016-02-10 00:05:58 +00001387
1388 /* openvswitch users expect packet sizes to be unrestricted,
1389 * so set the largest MTU we can.
1390 */
Jarod Wilson91572082016-10-20 13:55:20 -04001391 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001392 if (err)
1393 goto err;
1394
Nicolas Dichtel41009482016-06-13 10:31:07 +02001395 err = rtnl_configure_link(dev, NULL);
1396 if (err < 0)
1397 goto err;
1398
Pravin B Shelare305ac62015-08-26 23:46:52 -07001399 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001400err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001401 geneve_dellink(dev, &list_kill);
1402 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001403 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001404}
1405EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1406
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001407static int geneve_netdevice_event(struct notifier_block *unused,
1408 unsigned long event, void *ptr)
1409{
1410 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1411
Alexander Duyck7c46a642016-06-16 12:21:00 -07001412 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001413 geneve_push_rx_ports(dev);
1414
1415 return NOTIFY_DONE;
1416}
1417
1418static struct notifier_block geneve_notifier_block __read_mostly = {
1419 .notifier_call = geneve_netdevice_event,
1420};
1421
John W. Linville2d07dc72015-05-13 12:57:30 -04001422static __net_init int geneve_init_net(struct net *net)
1423{
1424 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001425
1426 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001427 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001428 return 0;
1429}
1430
1431static void __net_exit geneve_exit_net(struct net *net)
1432{
1433 struct geneve_net *gn = net_generic(net, geneve_net_id);
1434 struct geneve_dev *geneve, *next;
1435 struct net_device *dev, *aux;
1436 LIST_HEAD(list);
1437
1438 rtnl_lock();
1439
1440 /* gather any geneve devices that were moved into this ns */
1441 for_each_netdev_safe(net, dev, aux)
1442 if (dev->rtnl_link_ops == &geneve_link_ops)
1443 unregister_netdevice_queue(dev, &list);
1444
1445 /* now gather any other geneve devices that were created in this ns */
1446 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1447 /* If geneve->dev is in the same netns, it was already added
1448 * to the list by the previous loop.
1449 */
1450 if (!net_eq(dev_net(geneve->dev), net))
1451 unregister_netdevice_queue(geneve->dev, &list);
1452 }
1453
1454 /* unregister the devices gathered above */
1455 unregister_netdevice_many(&list);
1456 rtnl_unlock();
1457}
1458
1459static struct pernet_operations geneve_net_ops = {
1460 .init = geneve_init_net,
1461 .exit = geneve_exit_net,
1462 .id = &geneve_net_id,
1463 .size = sizeof(struct geneve_net),
1464};
1465
1466static int __init geneve_init_module(void)
1467{
1468 int rc;
1469
1470 rc = register_pernet_subsys(&geneve_net_ops);
1471 if (rc)
1472 goto out1;
1473
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001474 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001475 if (rc)
1476 goto out2;
1477
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001478 rc = rtnl_link_register(&geneve_link_ops);
1479 if (rc)
1480 goto out3;
1481
John W. Linville2d07dc72015-05-13 12:57:30 -04001482 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001483out3:
1484 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001485out2:
1486 unregister_pernet_subsys(&geneve_net_ops);
1487out1:
1488 return rc;
1489}
1490late_initcall(geneve_init_module);
1491
1492static void __exit geneve_cleanup_module(void)
1493{
1494 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001495 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001496 unregister_pernet_subsys(&geneve_net_ops);
1497}
1498module_exit(geneve_cleanup_module);
1499
1500MODULE_LICENSE("GPL");
1501MODULE_VERSION(GENEVE_NETDEV_VER);
1502MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1503MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1504MODULE_ALIAS_RTNL_LINK("geneve");