blob: 95dd19a3cafef35e79232fe2295a85563c0f918f [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
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100106static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
107{
108 return gs->sock->sk->sk_family;
109}
110
Pravin B Shelar66d47002015-08-26 23:46:55 -0700111static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700112 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400113{
pravin shelar9b4437a2016-11-21 11:02:58 -0800114 __be64 id = vni_to_tunnel_id(vni);
John W. Linville2d07dc72015-05-13 12:57:30 -0400115 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700116 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400117 __u32 hash;
118
John W. Linville2d07dc72015-05-13 12:57:30 -0400119 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700120 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700121 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700122 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800123 if (!memcmp(&id, &geneve->info.key.tun_id, sizeof(id)) &&
124 addr == geneve->info.key.u.ipv4.dst)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700125 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400126 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700127 return NULL;
128}
129
John W. Linville8ed66f02015-10-26 17:01:44 -0400130#if IS_ENABLED(CONFIG_IPV6)
131static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
132 struct in6_addr addr6, u8 vni[])
133{
pravin shelar9b4437a2016-11-21 11:02:58 -0800134 __be64 id = vni_to_tunnel_id(vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400135 struct hlist_head *vni_list_head;
136 struct geneve_dev *geneve;
137 __u32 hash;
138
139 /* Find the device for this VNI */
140 hash = geneve_net_vni_hash(vni);
141 vni_list_head = &gs->vni_list[hash];
142 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800143 if (!memcmp(&id, &geneve->info.key.tun_id, sizeof(id)) &&
144 ipv6_addr_equal(&addr6, &geneve->info.key.u.ipv6.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -0400145 return geneve;
146 }
147 return NULL;
148}
149#endif
150
Pravin B Shelar371bd102015-08-26 23:46:54 -0700151static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
152{
153 return (struct genevehdr *)(udp_hdr(skb) + 1);
154}
155
Jiri Benc9fc47542016-02-18 11:22:50 +0100156static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
157 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700158{
John W. Linville8ed66f02015-10-26 17:01:44 -0400159 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800160 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700161
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100162 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100163 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800164 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100165
John W. Linville8ed66f02015-10-26 17:01:44 -0400166 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700167
John W. Linville8ed66f02015-10-26 17:01:44 -0400168 if (gs->collect_md) {
169 vni = zero_vni;
170 addr = 0;
171 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100172 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400173 addr = iph->saddr;
174 }
175
Jiri Benc9fc47542016-02-18 11:22:50 +0100176 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400177#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100178 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800179 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100180 struct ipv6hdr *ip6h;
181 struct in6_addr addr6;
182
John W. Linville8ed66f02015-10-26 17:01:44 -0400183 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
184
185 if (gs->collect_md) {
186 vni = zero_vni;
187 addr6 = zero_addr6;
188 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100189 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400190 addr6 = ip6h->saddr;
191 }
192
Jiri Benc9fc47542016-02-18 11:22:50 +0100193 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400194#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700195 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100196 return NULL;
197}
198
199/* geneve receive/decap routine */
200static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
201 struct sk_buff *skb)
202{
203 struct genevehdr *gnvh = geneve_hdr(skb);
204 struct metadata_dst *tun_dst = NULL;
205 struct pcpu_sw_netstats *stats;
206 int err = 0;
207 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400208
Pravin B Shelar371bd102015-08-26 23:46:54 -0700209 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700210 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700211
212 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
213 (gnvh->oam ? TUNNEL_OAM : 0) |
214 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
215
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100216 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700217 vni_to_tunnel_id(gnvh->vni),
218 gnvh->opt_len * 4);
219 if (!tun_dst)
220 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700221 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700222 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
223 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700224 } else {
225 /* Drop packets w/ critical options,
226 * since we don't support any...
227 */
228 if (gnvh->critical)
229 goto drop;
230 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400231
232 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400233 skb->protocol = eth_type_trans(skb, geneve->dev);
234 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
235
Pravin B Shelare305ac62015-08-26 23:46:52 -0700236 if (tun_dst)
237 skb_dst_set(skb, &tun_dst->dst);
238
John W. Linville2d07dc72015-05-13 12:57:30 -0400239 /* Ignore packet loops (and multicast echo) */
240 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
241 goto drop;
242
Jiri Benc9fc47542016-02-18 11:22:50 +0100243 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400244 skb_reset_network_header(skb);
245
Jiri Benc9fc47542016-02-18 11:22:50 +0100246 if (geneve_get_sk_family(gs) == AF_INET)
247 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400248#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100249 else
250 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400251#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400252
253 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400254 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100255 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400256 net_info_ratelimited("non-ECT from %pI4 "
257 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100258 &((struct iphdr *)oiph)->saddr,
259 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400260#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100261 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400262 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100263 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400264#endif
265 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400266 if (err > 1) {
267 ++geneve->dev->stats.rx_frame_errors;
268 ++geneve->dev->stats.rx_errors;
269 goto drop;
270 }
271 }
272
273 stats = this_cpu_ptr(geneve->dev->tstats);
274 u64_stats_update_begin(&stats->syncp);
275 stats->rx_packets++;
276 stats->rx_bytes += skb->len;
277 u64_stats_update_end(&stats->syncp);
278
Jesse Gross8e816df2015-08-28 16:54:40 -0700279 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400280 return;
281drop:
282 /* Consume bad packet */
283 kfree_skb(skb);
284}
285
286/* Setup stats when device is created */
287static int geneve_init(struct net_device *dev)
288{
Jesse Gross8e816df2015-08-28 16:54:40 -0700289 struct geneve_dev *geneve = netdev_priv(dev);
290 int err;
291
John W. Linville2d07dc72015-05-13 12:57:30 -0400292 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
293 if (!dev->tstats)
294 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700295
296 err = gro_cells_init(&geneve->gro_cells, dev);
297 if (err) {
298 free_percpu(dev->tstats);
299 return err;
300 }
301
pravin shelar9b4437a2016-11-21 11:02:58 -0800302 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100303 if (err) {
304 free_percpu(dev->tstats);
305 gro_cells_destroy(&geneve->gro_cells);
306 return err;
307 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400308 return 0;
309}
310
311static void geneve_uninit(struct net_device *dev)
312{
Jesse Gross8e816df2015-08-28 16:54:40 -0700313 struct geneve_dev *geneve = netdev_priv(dev);
314
pravin shelar9b4437a2016-11-21 11:02:58 -0800315 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700316 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400317 free_percpu(dev->tstats);
318}
319
Pravin B Shelar371bd102015-08-26 23:46:54 -0700320/* Callback from net/ipv4/udp.c to receive packets */
321static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
322{
323 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100324 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700325 struct geneve_sock *gs;
326 int opts_len;
327
328 /* Need Geneve and inner Ethernet header to be present */
329 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200330 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700331
332 /* Return packets with reserved bits set */
333 geneveh = geneve_hdr(skb);
334 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200335 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700336
337 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200338 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700339
Jiri Benc9fc47542016-02-18 11:22:50 +0100340 gs = rcu_dereference_sk_user_data(sk);
341 if (!gs)
342 goto drop;
343
344 geneve = geneve_lookup_skb(gs, skb);
345 if (!geneve)
346 goto drop;
347
Pravin B Shelar371bd102015-08-26 23:46:54 -0700348 opts_len = geneveh->opt_len * 4;
349 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100350 htons(ETH_P_TEB),
351 !net_eq(geneve->net, dev_net(geneve->dev))))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700352 goto drop;
353
Jiri Benc9fc47542016-02-18 11:22:50 +0100354 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700355 return 0;
356
357drop:
358 /* Consume bad packet */
359 kfree_skb(skb);
360 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700361}
362
363static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800364 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700365{
366 struct socket *sock;
367 struct udp_port_cfg udp_conf;
368 int err;
369
370 memset(&udp_conf, 0, sizeof(udp_conf));
371
372 if (ipv6) {
373 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400374 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800375 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700376 } else {
377 udp_conf.family = AF_INET;
378 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
379 }
380
381 udp_conf.local_udp_port = port;
382
383 /* Open UDP socket */
384 err = udp_sock_create(net, &udp_conf, &sock);
385 if (err < 0)
386 return ERR_PTR(err);
387
388 return sock;
389}
390
Pravin B Shelar371bd102015-08-26 23:46:54 -0700391static int geneve_hlen(struct genevehdr *gh)
392{
393 return sizeof(*gh) + gh->opt_len * 4;
394}
395
Tom Herbert4a0090a2016-04-05 08:22:55 -0700396static struct sk_buff **geneve_gro_receive(struct sock *sk,
397 struct sk_buff **head,
398 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700399{
400 struct sk_buff *p, **pp = NULL;
401 struct genevehdr *gh, *gh2;
402 unsigned int hlen, gh_len, off_gnv;
403 const struct packet_offload *ptype;
404 __be16 type;
405 int flush = 1;
406
407 off_gnv = skb_gro_offset(skb);
408 hlen = off_gnv + sizeof(*gh);
409 gh = skb_gro_header_fast(skb, off_gnv);
410 if (skb_gro_header_hard(skb, hlen)) {
411 gh = skb_gro_header_slow(skb, hlen, off_gnv);
412 if (unlikely(!gh))
413 goto out;
414 }
415
416 if (gh->ver != GENEVE_VER || gh->oam)
417 goto out;
418 gh_len = geneve_hlen(gh);
419
420 hlen = off_gnv + gh_len;
421 if (skb_gro_header_hard(skb, hlen)) {
422 gh = skb_gro_header_slow(skb, hlen, off_gnv);
423 if (unlikely(!gh))
424 goto out;
425 }
426
Pravin B Shelar371bd102015-08-26 23:46:54 -0700427 for (p = *head; p; p = p->next) {
428 if (!NAPI_GRO_CB(p)->same_flow)
429 continue;
430
431 gh2 = (struct genevehdr *)(p->data + off_gnv);
432 if (gh->opt_len != gh2->opt_len ||
433 memcmp(gh, gh2, gh_len)) {
434 NAPI_GRO_CB(p)->same_flow = 0;
435 continue;
436 }
437 }
438
439 type = gh->proto_type;
440
441 rcu_read_lock();
442 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800443 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700444 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700445
446 skb_gro_pull(skb, gh_len);
447 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200448 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800449 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700450
451out_unlock:
452 rcu_read_unlock();
453out:
454 NAPI_GRO_CB(skb)->flush |= flush;
455
456 return pp;
457}
458
Tom Herbert4a0090a2016-04-05 08:22:55 -0700459static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
460 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700461{
462 struct genevehdr *gh;
463 struct packet_offload *ptype;
464 __be16 type;
465 int gh_len;
466 int err = -ENOSYS;
467
Pravin B Shelar371bd102015-08-26 23:46:54 -0700468 gh = (struct genevehdr *)(skb->data + nhoff);
469 gh_len = geneve_hlen(gh);
470 type = gh->proto_type;
471
472 rcu_read_lock();
473 ptype = gro_find_complete_by_type(type);
474 if (ptype)
475 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
476
477 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700478
479 skb_set_inner_mac_header(skb, nhoff + gh_len);
480
Pravin B Shelar371bd102015-08-26 23:46:54 -0700481 return err;
482}
483
484/* Create new listen socket if needed */
485static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800486 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700487{
488 struct geneve_net *gn = net_generic(net, geneve_net_id);
489 struct geneve_sock *gs;
490 struct socket *sock;
491 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700492 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700493
494 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
495 if (!gs)
496 return ERR_PTR(-ENOMEM);
497
pravin shelar9b4437a2016-11-21 11:02:58 -0800498 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700499 if (IS_ERR(sock)) {
500 kfree(gs);
501 return ERR_CAST(sock);
502 }
503
504 gs->sock = sock;
505 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700506 for (h = 0; h < VNI_HASH_SIZE; ++h)
507 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700508
509 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700510 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700511
512 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700513 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700514 tunnel_cfg.sk_user_data = gs;
515 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700516 tunnel_cfg.gro_receive = geneve_gro_receive;
517 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700518 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
519 tunnel_cfg.encap_destroy = NULL;
520 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700521 list_add(&gs->list, &gn->sock_list);
522 return gs;
523}
524
John W. Linville8ed66f02015-10-26 17:01:44 -0400525static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700526{
John W. Linville8ed66f02015-10-26 17:01:44 -0400527 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528 return;
529
530 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700531 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700532 udp_tunnel_sock_release(gs->sock);
533 kfree_rcu(gs, rcu);
534}
535
John W. Linville8ed66f02015-10-26 17:01:44 -0400536static void geneve_sock_release(struct geneve_dev *geneve)
537{
pravin shelarfceb9c32016-10-28 09:59:16 -0700538 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400539#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700540 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
541
542 rcu_assign_pointer(geneve->sock6, NULL);
543#endif
544
545 rcu_assign_pointer(geneve->sock4, NULL);
546 synchronize_net();
547
548 __geneve_sock_release(gs4);
549#if IS_ENABLED(CONFIG_IPV6)
550 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400551#endif
552}
553
Pravin B Shelar371bd102015-08-26 23:46:54 -0700554static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400555 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700556 __be16 dst_port)
557{
558 struct geneve_sock *gs;
559
560 list_for_each_entry(gs, &gn->sock_list, list) {
561 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100562 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700563 return gs;
564 }
565 }
566 return NULL;
567}
568
John W. Linville8ed66f02015-10-26 17:01:44 -0400569static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400570{
John W. Linville2d07dc72015-05-13 12:57:30 -0400571 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700572 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400573 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800574 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700575 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400576
pravin shelar9b4437a2016-11-21 11:02:58 -0800577 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700578 if (gs) {
579 gs->refcnt++;
580 goto out;
581 }
582
pravin shelar9b4437a2016-11-21 11:02:58 -0800583 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
584 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400585 if (IS_ERR(gs))
586 return PTR_ERR(gs);
587
Pravin B Shelar371bd102015-08-26 23:46:54 -0700588out:
589 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400590#if IS_ENABLED(CONFIG_IPV6)
591 if (ipv6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700592 rcu_assign_pointer(geneve->sock6, gs);
John W. Linville8ed66f02015-10-26 17:01:44 -0400593 else
594#endif
pravin shelarfceb9c32016-10-28 09:59:16 -0700595 rcu_assign_pointer(geneve->sock4, gs);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700596
pravin shelar9b4437a2016-11-21 11:02:58 -0800597 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
598 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700599 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400600 return 0;
601}
602
John W. Linville8ed66f02015-10-26 17:01:44 -0400603static int geneve_open(struct net_device *dev)
604{
605 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800606 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400607 bool metadata = geneve->collect_md;
608 int ret = 0;
609
John W. Linville8ed66f02015-10-26 17:01:44 -0400610#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400611 if (ipv6 || metadata)
612 ret = geneve_sock_add(geneve, true);
613#endif
614 if (!ret && (!ipv6 || metadata))
615 ret = geneve_sock_add(geneve, false);
616 if (ret < 0)
617 geneve_sock_release(geneve);
618
619 return ret;
620}
621
John W. Linville2d07dc72015-05-13 12:57:30 -0400622static int geneve_stop(struct net_device *dev)
623{
624 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400625
Pravin B Shelar66d47002015-08-26 23:46:55 -0700626 if (!hlist_unhashed(&geneve->hlist))
627 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400628 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400629 return 0;
630}
631
John W. Linville8ed66f02015-10-26 17:01:44 -0400632static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800633 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400634{
635 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800636 geneveh->opt_len = info->options_len / 4;
637 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
638 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400639 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800640 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400641 geneveh->proto_type = htons(ETH_P_TEB);
642 geneveh->rsvd2 = 0;
643
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800644 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400645}
646
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800647static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
648 const struct ip_tunnel_info *info,
649 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700650{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800651 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700652 struct genevehdr *gnvh;
653 int min_headroom;
654 int err;
655
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800656 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400657 skb_scrub_packet(skb, xnet);
658
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800659 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
660 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400661 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400662 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400663 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400664
Alexander Duyckaed069d2016-04-14 15:33:37 -0400665 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300666 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400667 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400668
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800669 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) +
670 info->options_len);
671 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400672 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
673 return 0;
674
675free_dst:
676 dst_release(dst);
677 return err;
678}
John W. Linville8ed66f02015-10-26 17:01:44 -0400679
680static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
681 struct net_device *dev,
682 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800683 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700684{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100685 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700686 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100687 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700688 struct rtable *rt = NULL;
689 __u8 tos;
690
pravin shelarfceb9c32016-10-28 09:59:16 -0700691 if (!rcu_dereference(geneve->sock4))
692 return ERR_PTR(-EIO);
693
Pravin B Shelare305ac62015-08-26 23:46:52 -0700694 memset(fl4, 0, sizeof(*fl4));
695 fl4->flowi4_mark = skb->mark;
696 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800697 fl4->daddr = info->key.u.ipv4.dst;
698 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700699
pravin shelar9b4437a2016-11-21 11:02:58 -0800700 tos = info->key.tos;
701 if ((tos == 1) && !geneve->collect_md) {
702 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
703 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100704 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800705 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100706
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800707 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100708 if (use_cache) {
709 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
710 if (rt)
711 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700712 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700713 rt = ip_route_output_key(geneve->net, fl4);
714 if (IS_ERR(rt)) {
715 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700716 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700717 }
718 if (rt->dst.dev == dev) { /* is this necessary? */
719 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700720 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700721 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700722 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100723 if (use_cache)
724 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700725 return rt;
726}
727
John W. Linville8ed66f02015-10-26 17:01:44 -0400728#if IS_ENABLED(CONFIG_IPV6)
729static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
730 struct net_device *dev,
731 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800732 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400733{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100734 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400735 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400736 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100737 struct dst_cache *dst_cache;
pravin shelarfceb9c32016-10-28 09:59:16 -0700738 struct geneve_sock *gs6;
John W. Linville3a56f862015-10-26 17:01:45 -0400739 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400740
pravin shelarfceb9c32016-10-28 09:59:16 -0700741 gs6 = rcu_dereference(geneve->sock6);
742 if (!gs6)
743 return ERR_PTR(-EIO);
744
John W. Linville8ed66f02015-10-26 17:01:44 -0400745 memset(fl6, 0, sizeof(*fl6));
746 fl6->flowi6_mark = skb->mark;
747 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800748 fl6->daddr = info->key.u.ipv6.dst;
749 fl6->saddr = info->key.u.ipv6.src;
750 prio = info->key.tos;
751 if ((prio == 1) && !geneve->collect_md) {
752 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
753 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100754 }
755
pravin shelar9b4437a2016-11-21 11:02:58 -0800756 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
757 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800758 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100759 if (use_cache) {
760 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
761 if (dst)
762 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400763 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400764 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
765 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
766 return ERR_PTR(-ENETUNREACH);
767 }
768 if (dst->dev == dev) { /* is this necessary? */
769 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
770 dst_release(dst);
771 return ERR_PTR(-ELOOP);
772 }
773
Paolo Abeni468dfff2016-02-12 15:43:58 +0100774 if (use_cache)
775 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400776 return dst;
777}
778#endif
779
pravin shelar9b4437a2016-11-21 11:02:58 -0800780static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800781 struct geneve_dev *geneve,
782 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700783{
pravin shelar9b4437a2016-11-21 11:02:58 -0800784 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
785 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
786 const struct ip_tunnel_key *key = &info->key;
787 struct rtable *rt;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700788 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400789 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400790 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700791 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700792 __be16 df;
John W. Linville2d07dc72015-05-13 12:57:30 -0400793
pravin shelarfceb9c32016-10-28 09:59:16 -0700794 if (!gs4)
pravin shelar9b4437a2016-11-21 11:02:58 -0800795 return err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400796
John W. Linville8ed66f02015-10-26 17:01:44 -0400797 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800798 if (IS_ERR(rt))
799 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700800
801 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800802 if (geneve->collect_md) {
803 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700804 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700805 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800806 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
807 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400808 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800809 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
810
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800811 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800812 if (unlikely(err))
813 return err;
814
Pravin B Shelar039f5062015-12-24 14:34:54 -0800815 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800816 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800817 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800818 !(info->key.tun_flags & TUNNEL_CSUM));
819 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400820}
821
John W. Linville8ed66f02015-10-26 17:01:44 -0400822#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800823static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800824 struct geneve_dev *geneve,
825 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400826{
pravin shelar9b4437a2016-11-21 11:02:58 -0800827 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
828 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
829 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400830 struct dst_entry *dst = NULL;
831 int err = -EINVAL;
832 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400833 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400834 __be16 sport;
John W. Linville8ed66f02015-10-26 17:01:44 -0400835
pravin shelarfceb9c32016-10-28 09:59:16 -0700836 if (!gs6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800837 return err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400838
839 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800840 if (IS_ERR(dst))
841 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400842
843 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 if (geneve->collect_md) {
845 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400846 ttl = key->ttl;
847 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100848 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800849 ip_hdr(skb), skb);
850 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400851 }
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800852 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800853 if (unlikely(err))
854 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100855
Pravin B Shelar039f5062015-12-24 14:34:54 -0800856 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800857 &fl6.saddr, &fl6.daddr, prio, ttl,
858 info->key.label, sport, geneve->info.key.tp_dst,
859 !(info->key.tun_flags & TUNNEL_CSUM));
860 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400861}
862#endif
863
864static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
865{
866 struct geneve_dev *geneve = netdev_priv(dev);
867 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800868 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400869
pravin shelar9b4437a2016-11-21 11:02:58 -0800870 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400871 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800872 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
873 err = -EINVAL;
874 netdev_dbg(dev, "no tunnel metadata\n");
875 goto tx_error;
876 }
877 } else {
878 info = &geneve->info;
879 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400880
881#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800882 if (info->mode & IP_TUNNEL_INFO_IPV6)
883 err = geneve6_xmit_skb(skb, dev, geneve, info);
884 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400885#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800886 err = geneve_xmit_skb(skb, dev, geneve, info);
887
888 if (likely(!err))
889 return NETDEV_TX_OK;
890tx_error:
891 dev_kfree_skb(skb);
892
893 if (err == -ELOOP)
894 dev->stats.collisions++;
895 else if (err == -ENETUNREACH)
896 dev->stats.tx_carrier_errors++;
897
898 dev->stats.tx_errors++;
899 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400900}
901
Jarod Wilson91572082016-10-20 13:55:20 -0400902static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000903{
Jarod Wilson91572082016-10-20 13:55:20 -0400904 /* Only possible if called internally, ndo_change_mtu path's new_mtu
905 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
David Wragg55e5bfb2016-02-10 00:05:57 +0000906 */
Jarod Wilson91572082016-10-20 13:55:20 -0400907 if (new_mtu > dev->max_mtu)
908 new_mtu = dev->max_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000909
David Wragg55e5bfb2016-02-10 00:05:57 +0000910 dev->mtu = new_mtu;
911 return 0;
912}
913
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700914static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
915{
916 struct ip_tunnel_info *info = skb_tunnel_info(skb);
917 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700918
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400919 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800920 struct rtable *rt;
921 struct flowi4 fl4;
922
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400923 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
924 if (IS_ERR(rt))
925 return PTR_ERR(rt);
926
927 ip_rt_put(rt);
928 info->key.u.ipv4.src = fl4.saddr;
929#if IS_ENABLED(CONFIG_IPV6)
930 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800931 struct dst_entry *dst;
932 struct flowi6 fl6;
933
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400934 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
935 if (IS_ERR(dst))
936 return PTR_ERR(dst);
937
938 dst_release(dst);
939 info->key.u.ipv6.src = fl6.saddr;
940#endif
941 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700942 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400943 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700944
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700945 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
946 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800947 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700948 return 0;
949}
950
John W. Linville2d07dc72015-05-13 12:57:30 -0400951static const struct net_device_ops geneve_netdev_ops = {
952 .ndo_init = geneve_init,
953 .ndo_uninit = geneve_uninit,
954 .ndo_open = geneve_open,
955 .ndo_stop = geneve_stop,
956 .ndo_start_xmit = geneve_xmit,
957 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +0000958 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -0400959 .ndo_validate_addr = eth_validate_addr,
960 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700961 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -0400962};
963
964static void geneve_get_drvinfo(struct net_device *dev,
965 struct ethtool_drvinfo *drvinfo)
966{
967 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
968 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
969}
970
971static const struct ethtool_ops geneve_ethtool_ops = {
972 .get_drvinfo = geneve_get_drvinfo,
973 .get_link = ethtool_op_get_link,
974};
975
976/* Info for udev, that this is a virtual tunnel endpoint */
977static struct device_type geneve_type = {
978 .name = "geneve",
979};
980
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +0200981/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800982 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +0200983 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800984 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +0200985static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800986{
987 struct net *net = dev_net(dev);
988 struct geneve_net *gn = net_generic(net, geneve_net_id);
989 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +0200990
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800991 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -0700992 list_for_each_entry_rcu(gs, &gn->sock_list, list)
993 udp_tunnel_push_rx_port(dev, gs->sock,
994 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800995 rcu_read_unlock();
996}
Singhai, Anjali05ca4022015-12-14 12:21:20 -0800997
John W. Linville2d07dc72015-05-13 12:57:30 -0400998/* Initialize the device structure. */
999static void geneve_setup(struct net_device *dev)
1000{
1001 ether_setup(dev);
1002
1003 dev->netdev_ops = &geneve_netdev_ops;
1004 dev->ethtool_ops = &geneve_ethtool_ops;
1005 dev->destructor = free_netdev;
1006
1007 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1008
John W. Linville2d07dc72015-05-13 12:57:30 -04001009 dev->features |= NETIF_F_LLTX;
1010 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1011 dev->features |= NETIF_F_RXCSUM;
1012 dev->features |= NETIF_F_GSO_SOFTWARE;
1013
John W. Linville2d07dc72015-05-13 12:57:30 -04001014 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1015 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001016
Jarod Wilson91572082016-10-20 13:55:20 -04001017 /* MTU range: 68 - (something less than 65535) */
1018 dev->min_mtu = ETH_MIN_MTU;
1019 /* The max_mtu calculation does not take account of GENEVE
1020 * options, to avoid excluding potentially valid
1021 * configurations. This will be further reduced by IPvX hdr size.
1022 */
1023 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1024
John W. Linville2d07dc72015-05-13 12:57:30 -04001025 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001026 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001027 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001028 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001029}
1030
1031static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1032 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1033 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001034 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001035 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001036 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001037 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001038 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001039 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001040 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1041 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1042 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001043};
1044
1045static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1046{
1047 if (tb[IFLA_ADDRESS]) {
1048 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1049 return -EINVAL;
1050
1051 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1052 return -EADDRNOTAVAIL;
1053 }
1054
1055 if (!data)
1056 return -EINVAL;
1057
1058 if (data[IFLA_GENEVE_ID]) {
1059 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1060
1061 if (vni >= GENEVE_VID_MASK)
1062 return -ERANGE;
1063 }
1064
1065 return 0;
1066}
1067
Pravin B Shelar371bd102015-08-26 23:46:54 -07001068static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001069 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001070 bool *tun_on_same_port,
1071 bool *tun_collect_md)
1072{
pravin shelar9b4437a2016-11-21 11:02:58 -08001073 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001074
1075 *tun_on_same_port = false;
1076 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001077 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001078 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001079 *tun_collect_md = geneve->collect_md;
1080 *tun_on_same_port = true;
1081 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001082 if (info->key.tun_id == geneve->info.key.tun_id &&
1083 info->key.tp_dst == geneve->info.key.tp_dst &&
1084 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001085 t = geneve;
1086 }
1087 return t;
1088}
1089
pravin shelar9b4437a2016-11-21 11:02:58 -08001090static bool is_all_zero(const u8 *fp, size_t size)
1091{
1092 int i;
1093
1094 for (i = 0; i < size; i++)
1095 if (fp[i])
1096 return false;
1097 return true;
1098}
1099
1100static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1101{
1102 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1103 info->key.ttl || info->key.label || info->key.tp_src ||
1104 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1105 return false;
1106 else
1107 return true;
1108}
1109
Pravin B Shelare305ac62015-08-26 23:46:52 -07001110static int geneve_configure(struct net *net, struct net_device *dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001111 const struct ip_tunnel_info *info,
1112 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001113{
1114 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001115 struct geneve_dev *t, *geneve = netdev_priv(dev);
1116 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001117 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001118
pravin shelar9b4437a2016-11-21 11:02:58 -08001119 if (metadata && !is_tnl_info_zero(info))
John W. Linville8ed66f02015-10-26 17:01:44 -04001120 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001121
1122 geneve->net = net;
1123 geneve->dev = dev;
1124
pravin shelar9b4437a2016-11-21 11:02:58 -08001125 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001126 if (t)
1127 return -EBUSY;
1128
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001129 /* make enough headroom for basic scenario */
1130 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
pravin shelar9b4437a2016-11-21 11:02:58 -08001131 if (ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001132 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001133 dev->max_mtu -= sizeof(struct iphdr);
1134 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001135 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001136 dev->max_mtu -= sizeof(struct ipv6hdr);
1137 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001138 dev->needed_headroom = encap_len + ETH_HLEN;
1139
Pravin B Shelar371bd102015-08-26 23:46:54 -07001140 if (metadata) {
1141 if (tun_on_same_port)
1142 return -EPERM;
1143 } else {
1144 if (tun_collect_md)
1145 return -EPERM;
1146 }
1147
pravin shelar9b4437a2016-11-21 11:02:58 -08001148 dst_cache_reset(&geneve->info.dst_cache);
1149 geneve->info = *info;
1150 geneve->collect_md = metadata;
1151 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001152
John W. Linville2d07dc72015-05-13 12:57:30 -04001153 err = register_netdevice(dev);
1154 if (err)
1155 return err;
1156
1157 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001158 return 0;
1159}
1160
pravin shelar9b4437a2016-11-21 11:02:58 -08001161static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1162{
1163 memset(info, 0, sizeof(*info));
1164 info->key.tp_dst = htons(dst_port);
1165}
1166
Pravin B Shelare305ac62015-08-26 23:46:52 -07001167static int geneve_newlink(struct net *net, struct net_device *dev,
1168 struct nlattr *tb[], struct nlattr *data[])
1169{
pravin shelar9b4437a2016-11-21 11:02:58 -08001170 bool use_udp6_rx_checksums = false;
1171 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001172 bool metadata = false;
pravin shelar9b4437a2016-11-21 11:02:58 -08001173
1174 init_tnl_info(&info, GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001175
John W. Linville8ed66f02015-10-26 17:01:44 -04001176 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1177 return -EINVAL;
1178
1179 if (data[IFLA_GENEVE_REMOTE]) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001180 info.key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001181 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001182
pravin shelar9b4437a2016-11-21 11:02:58 -08001183 if (IN_MULTICAST(ntohl(info.key.u.ipv4.dst))) {
1184 netdev_dbg(dev, "multicast remote is unsupported\n");
John W. Linville8ed66f02015-10-26 17:01:44 -04001185 return -EINVAL;
1186 }
1187 }
1188
pravin shelar9b4437a2016-11-21 11:02:58 -08001189 if (data[IFLA_GENEVE_REMOTE6]) {
1190 #if IS_ENABLED(CONFIG_IPV6)
1191 info.mode = IP_TUNNEL_INFO_IPV6;
1192 info.key.u.ipv6.dst =
1193 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001194
pravin shelar9b4437a2016-11-21 11:02:58 -08001195 if (ipv6_addr_type(&info.key.u.ipv6.dst) &
1196 IPV6_ADDR_LINKLOCAL) {
1197 netdev_dbg(dev, "link-local remote is unsupported\n");
1198 return -EINVAL;
1199 }
1200 if (ipv6_addr_is_multicast(&info.key.u.ipv6.dst)) {
1201 netdev_dbg(dev, "multicast remote is unsupported\n");
1202 return -EINVAL;
1203 }
1204 info.key.tun_flags |= TUNNEL_CSUM;
1205 use_udp6_rx_checksums = true;
1206#else
1207 return -EPFNOSUPPORT;
1208#endif
1209 }
1210
1211 if (data[IFLA_GENEVE_ID]) {
1212 __u32 vni;
1213 __u8 tvni[3];
1214
1215 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1216 tvni[0] = (vni & 0x00ff0000) >> 16;
1217 tvni[1] = (vni & 0x0000ff00) >> 8;
1218 tvni[2] = vni & 0x000000ff;
1219
1220 info.key.tun_id = vni_to_tunnel_id(tvni);
1221 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001222 if (data[IFLA_GENEVE_TTL])
pravin shelar9b4437a2016-11-21 11:02:58 -08001223 info.key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001224
1225 if (data[IFLA_GENEVE_TOS])
pravin shelar9b4437a2016-11-21 11:02:58 -08001226 info.key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001227
pravin shelar9b4437a2016-11-21 11:02:58 -08001228 if (data[IFLA_GENEVE_LABEL]) {
1229 info.key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1230 IPV6_FLOWLABEL_MASK;
1231 if (info.key.label && (!(info.mode & IP_TUNNEL_INFO_IPV6)))
1232 return -EINVAL;
1233 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001234
Pravin B Shelare305ac62015-08-26 23:46:52 -07001235 if (data[IFLA_GENEVE_PORT])
pravin shelar9b4437a2016-11-21 11:02:58 -08001236 info.key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001237
1238 if (data[IFLA_GENEVE_COLLECT_METADATA])
1239 metadata = true;
1240
Tom Herbertabe492b2015-12-10 12:37:45 -08001241 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001242 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001243 info.key.tun_flags |= TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001244
1245 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1246 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001247 info.key.tun_flags &= ~TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001248
1249 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1250 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001251 use_udp6_rx_checksums = false;
Tom Herbertabe492b2015-12-10 12:37:45 -08001252
pravin shelar9b4437a2016-11-21 11:02:58 -08001253 return geneve_configure(net, dev, &info, metadata, use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001254}
1255
John W. Linville2d07dc72015-05-13 12:57:30 -04001256static void geneve_dellink(struct net_device *dev, struct list_head *head)
1257{
1258 struct geneve_dev *geneve = netdev_priv(dev);
1259
John W. Linville2d07dc72015-05-13 12:57:30 -04001260 list_del(&geneve->next);
1261 unregister_netdevice_queue(dev, head);
1262}
1263
1264static size_t geneve_get_size(const struct net_device *dev)
1265{
1266 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001267 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001268 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001269 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001270 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001271 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001272 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001273 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1274 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1275 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001276 0;
1277}
1278
1279static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1280{
1281 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001282 struct ip_tunnel_info *info = &geneve->info;
1283 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001284 __u32 vni;
1285
pravin shelar9b4437a2016-11-21 11:02:58 -08001286 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1287 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001288 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1289 goto nla_put_failure;
1290
pravin shelar9b4437a2016-11-21 11:02:58 -08001291 if (ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001292 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001293 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001294 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001295
1296 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1297 !!(info->key.tun_flags & TUNNEL_CSUM)))
1298 goto nla_put_failure;
1299
John W. Linville8ed66f02015-10-26 17:01:44 -04001300#if IS_ENABLED(CONFIG_IPV6)
1301 } else {
1302 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001303 &info->key.u.ipv6.dst))
1304 goto nla_put_failure;
1305
1306 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1307 !(info->key.tun_flags & TUNNEL_CSUM)))
1308 goto nla_put_failure;
1309
1310 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1311 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001312 goto nla_put_failure;
1313#endif
1314 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001315
pravin shelar9b4437a2016-11-21 11:02:58 -08001316 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1317 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1318 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001319 goto nla_put_failure;
1320
pravin shelar9b4437a2016-11-21 11:02:58 -08001321 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001322 goto nla_put_failure;
1323
Pravin B Shelare305ac62015-08-26 23:46:52 -07001324 if (geneve->collect_md) {
1325 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1326 goto nla_put_failure;
1327 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001328 return 0;
1329
1330nla_put_failure:
1331 return -EMSGSIZE;
1332}
1333
1334static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1335 .kind = "geneve",
1336 .maxtype = IFLA_GENEVE_MAX,
1337 .policy = geneve_policy,
1338 .priv_size = sizeof(struct geneve_dev),
1339 .setup = geneve_setup,
1340 .validate = geneve_validate,
1341 .newlink = geneve_newlink,
1342 .dellink = geneve_dellink,
1343 .get_size = geneve_get_size,
1344 .fill_info = geneve_fill_info,
1345};
1346
Pravin B Shelare305ac62015-08-26 23:46:52 -07001347struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1348 u8 name_assign_type, u16 dst_port)
1349{
1350 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001351 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001352 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001353 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001354 int err;
1355
1356 memset(tb, 0, sizeof(tb));
1357 dev = rtnl_create_link(net, name, name_assign_type,
1358 &geneve_link_ops, tb);
1359 if (IS_ERR(dev))
1360 return dev;
1361
pravin shelar9b4437a2016-11-21 11:02:58 -08001362 init_tnl_info(&info, dst_port);
1363 err = geneve_configure(net, dev, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001364 if (err) {
1365 free_netdev(dev);
1366 return ERR_PTR(err);
1367 }
David Wragg7e059152016-02-10 00:05:58 +00001368
1369 /* openvswitch users expect packet sizes to be unrestricted,
1370 * so set the largest MTU we can.
1371 */
Jarod Wilson91572082016-10-20 13:55:20 -04001372 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001373 if (err)
1374 goto err;
1375
Nicolas Dichtel41009482016-06-13 10:31:07 +02001376 err = rtnl_configure_link(dev, NULL);
1377 if (err < 0)
1378 goto err;
1379
Pravin B Shelare305ac62015-08-26 23:46:52 -07001380 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001381err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001382 geneve_dellink(dev, &list_kill);
1383 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001384 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001385}
1386EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1387
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001388static int geneve_netdevice_event(struct notifier_block *unused,
1389 unsigned long event, void *ptr)
1390{
1391 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1392
Alexander Duyck7c46a642016-06-16 12:21:00 -07001393 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001394 geneve_push_rx_ports(dev);
1395
1396 return NOTIFY_DONE;
1397}
1398
1399static struct notifier_block geneve_notifier_block __read_mostly = {
1400 .notifier_call = geneve_netdevice_event,
1401};
1402
John W. Linville2d07dc72015-05-13 12:57:30 -04001403static __net_init int geneve_init_net(struct net *net)
1404{
1405 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001406
1407 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001408 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001409 return 0;
1410}
1411
1412static void __net_exit geneve_exit_net(struct net *net)
1413{
1414 struct geneve_net *gn = net_generic(net, geneve_net_id);
1415 struct geneve_dev *geneve, *next;
1416 struct net_device *dev, *aux;
1417 LIST_HEAD(list);
1418
1419 rtnl_lock();
1420
1421 /* gather any geneve devices that were moved into this ns */
1422 for_each_netdev_safe(net, dev, aux)
1423 if (dev->rtnl_link_ops == &geneve_link_ops)
1424 unregister_netdevice_queue(dev, &list);
1425
1426 /* now gather any other geneve devices that were created in this ns */
1427 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1428 /* If geneve->dev is in the same netns, it was already added
1429 * to the list by the previous loop.
1430 */
1431 if (!net_eq(dev_net(geneve->dev), net))
1432 unregister_netdevice_queue(geneve->dev, &list);
1433 }
1434
1435 /* unregister the devices gathered above */
1436 unregister_netdevice_many(&list);
1437 rtnl_unlock();
1438}
1439
1440static struct pernet_operations geneve_net_ops = {
1441 .init = geneve_init_net,
1442 .exit = geneve_exit_net,
1443 .id = &geneve_net_id,
1444 .size = sizeof(struct geneve_net),
1445};
1446
1447static int __init geneve_init_module(void)
1448{
1449 int rc;
1450
1451 rc = register_pernet_subsys(&geneve_net_ops);
1452 if (rc)
1453 goto out1;
1454
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001455 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001456 if (rc)
1457 goto out2;
1458
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001459 rc = rtnl_link_register(&geneve_link_ops);
1460 if (rc)
1461 goto out3;
1462
John W. Linville2d07dc72015-05-13 12:57:30 -04001463 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001464out3:
1465 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001466out2:
1467 unregister_pernet_subsys(&geneve_net_ops);
1468out1:
1469 return rc;
1470}
1471late_initcall(geneve_init_module);
1472
1473static void __exit geneve_cleanup_module(void)
1474{
1475 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001476 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001477 unregister_pernet_subsys(&geneve_net_ops);
1478}
1479module_exit(geneve_cleanup_module);
1480
1481MODULE_LICENSE("GPL");
1482MODULE_VERSION(GENEVE_NETDEV_VER);
1483MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1484MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1485MODULE_ALIAS_RTNL_LINK("geneve");