blob: 70f2a32804cb9567f30e8759172a661a1ab37808 [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,
633 __be16 tun_flags, u8 vni[3],
634 u8 options_len, u8 *options)
635{
636 geneveh->ver = GENEVE_VER;
637 geneveh->opt_len = options_len / 4;
638 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
639 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
640 geneveh->rsvd1 = 0;
641 memcpy(geneveh->vni, vni, 3);
642 geneveh->proto_type = htons(ETH_P_TEB);
643 geneveh->rsvd2 = 0;
644
645 memcpy(geneveh->options, options, options_len);
646}
647
Pravin B Shelar371bd102015-08-26 23:46:54 -0700648static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
649 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
pravin shelar9b4437a2016-11-21 11:02:58 -0800650 bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700651{
pravin shelar9b4437a2016-11-21 11:02:58 -0800652 bool udp_sum = !!(tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700653 struct genevehdr *gnvh;
654 int min_headroom;
655 int err;
656
John W. Linville8ed66f02015-10-26 17:01:44 -0400657 skb_scrub_packet(skb, xnet);
658
Pravin B Shelar371bd102015-08-26 23:46:54 -0700659 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
660 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
661 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400662 if (unlikely(err))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700663 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700664
Alexander Duyckaed069d2016-04-14 15:33:37 -0400665 err = udp_tunnel_handle_offloads(skb, udp_sum);
666 if (err)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700667 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700668
669 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400670 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700671
672 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
673 return 0;
674
675free_rt:
676 ip_rt_put(rt);
677 return err;
678}
679
John W. Linville8ed66f02015-10-26 17:01:44 -0400680#if IS_ENABLED(CONFIG_IPV6)
681static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
682 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
pravin shelar9b4437a2016-11-21 11:02:58 -0800683 bool xnet)
John W. Linville8ed66f02015-10-26 17:01:44 -0400684{
pravin shelar9b4437a2016-11-21 11:02:58 -0800685 bool udp_sum = !!(tun_flags & TUNNEL_CSUM);
John W. Linville8ed66f02015-10-26 17:01:44 -0400686 struct genevehdr *gnvh;
687 int min_headroom;
688 int err;
689
690 skb_scrub_packet(skb, xnet);
691
692 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
693 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
694 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400695 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400696 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400697
Alexander Duyckaed069d2016-04-14 15:33:37 -0400698 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300699 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400700 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400701
702 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
703 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
704
705 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
706 return 0;
707
708free_dst:
709 dst_release(dst);
710 return err;
711}
712#endif
713
714static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
715 struct net_device *dev,
716 struct flowi4 *fl4,
717 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700718{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100719 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700720 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100721 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700722 struct rtable *rt = NULL;
723 __u8 tos;
724
pravin shelarfceb9c32016-10-28 09:59:16 -0700725 if (!rcu_dereference(geneve->sock4))
726 return ERR_PTR(-EIO);
727
Pravin B Shelare305ac62015-08-26 23:46:52 -0700728 memset(fl4, 0, sizeof(*fl4));
729 fl4->flowi4_mark = skb->mark;
730 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800731 fl4->daddr = info->key.u.ipv4.dst;
732 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700733
pravin shelar9b4437a2016-11-21 11:02:58 -0800734 tos = info->key.tos;
735 if ((tos == 1) && !geneve->collect_md) {
736 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
737 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100738 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800739 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100740
pravin shelar9b4437a2016-11-21 11:02:58 -0800741 dst_cache = &info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100742 if (use_cache) {
743 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
744 if (rt)
745 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700746 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700747 rt = ip_route_output_key(geneve->net, fl4);
748 if (IS_ERR(rt)) {
749 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700750 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700751 }
752 if (rt->dst.dev == dev) { /* is this necessary? */
753 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700755 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700756 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100757 if (use_cache)
758 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700759 return rt;
760}
761
John W. Linville8ed66f02015-10-26 17:01:44 -0400762#if IS_ENABLED(CONFIG_IPV6)
763static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
764 struct net_device *dev,
765 struct flowi6 *fl6,
766 struct ip_tunnel_info *info)
767{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100768 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400769 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400770 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100771 struct dst_cache *dst_cache;
pravin shelarfceb9c32016-10-28 09:59:16 -0700772 struct geneve_sock *gs6;
John W. Linville3a56f862015-10-26 17:01:45 -0400773 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400774
pravin shelarfceb9c32016-10-28 09:59:16 -0700775 gs6 = rcu_dereference(geneve->sock6);
776 if (!gs6)
777 return ERR_PTR(-EIO);
778
John W. Linville8ed66f02015-10-26 17:01:44 -0400779 memset(fl6, 0, sizeof(*fl6));
780 fl6->flowi6_mark = skb->mark;
781 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800782 fl6->daddr = info->key.u.ipv6.dst;
783 fl6->saddr = info->key.u.ipv6.src;
784 prio = info->key.tos;
785 if ((prio == 1) && !geneve->collect_md) {
786 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
787 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100788 }
789
pravin shelar9b4437a2016-11-21 11:02:58 -0800790 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
791 info->key.label);
792 dst_cache = &info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100793 if (use_cache) {
794 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
795 if (dst)
796 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400797 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400798 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
799 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
800 return ERR_PTR(-ENETUNREACH);
801 }
802 if (dst->dev == dev) { /* is this necessary? */
803 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
804 dst_release(dst);
805 return ERR_PTR(-ELOOP);
806 }
807
Paolo Abeni468dfff2016-02-12 15:43:58 +0100808 if (use_cache)
809 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400810 return dst;
811}
812#endif
813
pravin shelar9b4437a2016-11-21 11:02:58 -0800814static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
815 struct geneve_dev *geneve, struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700816{
pravin shelar9b4437a2016-11-21 11:02:58 -0800817 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
818 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
819 const struct ip_tunnel_key *key = &info->key;
820 struct rtable *rt;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700821 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400822 struct flowi4 fl4;
pravin shelar9b4437a2016-11-21 11:02:58 -0800823 u8 *opts = NULL;
John W. Linville8760ce52015-06-01 15:51:34 -0400824 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700825 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700826 __be16 df;
pravin shelar9b4437a2016-11-21 11:02:58 -0800827 u8 vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -0400828
pravin shelarfceb9c32016-10-28 09:59:16 -0700829 if (!gs4)
pravin shelar9b4437a2016-11-21 11:02:58 -0800830 return err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400831
John W. Linville8ed66f02015-10-26 17:01:44 -0400832 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800833 if (IS_ERR(rt))
834 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700835
836 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800837 if (geneve->collect_md) {
838 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700839 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700840 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800841 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
842 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400843 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
845
846 tunnel_id_to_vni(key->tun_id, vni);
847 if (info->options_len)
848 opts = ip_tunnel_info_opts(info);
849
850 skb_reset_mac_header(skb);
851 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
852 info->options_len, opts, xnet);
853 if (unlikely(err))
854 return err;
855
Pravin B Shelar039f5062015-12-24 14:34:54 -0800856 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800857 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800858 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800859 !(info->key.tun_flags & TUNNEL_CSUM));
860 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400861}
862
John W. Linville8ed66f02015-10-26 17:01:44 -0400863#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800864static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
865 struct geneve_dev *geneve, struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400866{
pravin shelar9b4437a2016-11-21 11:02:58 -0800867 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
868 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
869 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400870 struct dst_entry *dst = NULL;
871 int err = -EINVAL;
872 struct flowi6 fl6;
pravin shelar9b4437a2016-11-21 11:02:58 -0800873 u8 *opts = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400874 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 __be16 sport;
pravin shelar9b4437a2016-11-21 11:02:58 -0800876 u8 vni[3];
John W. Linville8ed66f02015-10-26 17:01:44 -0400877
pravin shelarfceb9c32016-10-28 09:59:16 -0700878 if (!gs6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800879 return err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400880
881 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800882 if (IS_ERR(dst))
883 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400884
885 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800886 if (geneve->collect_md) {
887 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400888 ttl = key->ttl;
889 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100890 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800891 ip_hdr(skb), skb);
892 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400893 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800894 tunnel_id_to_vni(key->tun_id, vni);
895 if (info->options_len)
896 opts = ip_tunnel_info_opts(info);
897
898 skb_reset_mac_header(skb);
899 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
900 info->options_len, opts, xnet);
901 if (unlikely(err))
902 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100903
Pravin B Shelar039f5062015-12-24 14:34:54 -0800904 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800905 &fl6.saddr, &fl6.daddr, prio, ttl,
906 info->key.label, sport, geneve->info.key.tp_dst,
907 !(info->key.tun_flags & TUNNEL_CSUM));
908 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400909}
910#endif
911
912static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
913{
914 struct geneve_dev *geneve = netdev_priv(dev);
915 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800916 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400917
pravin shelar9b4437a2016-11-21 11:02:58 -0800918 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400919 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800920 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
921 err = -EINVAL;
922 netdev_dbg(dev, "no tunnel metadata\n");
923 goto tx_error;
924 }
925 } else {
926 info = &geneve->info;
927 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400928
929#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800930 if (info->mode & IP_TUNNEL_INFO_IPV6)
931 err = geneve6_xmit_skb(skb, dev, geneve, info);
932 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400933#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800934 err = geneve_xmit_skb(skb, dev, geneve, info);
935
936 if (likely(!err))
937 return NETDEV_TX_OK;
938tx_error:
939 dev_kfree_skb(skb);
940
941 if (err == -ELOOP)
942 dev->stats.collisions++;
943 else if (err == -ENETUNREACH)
944 dev->stats.tx_carrier_errors++;
945
946 dev->stats.tx_errors++;
947 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400948}
949
Jarod Wilson91572082016-10-20 13:55:20 -0400950static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000951{
Jarod Wilson91572082016-10-20 13:55:20 -0400952 /* Only possible if called internally, ndo_change_mtu path's new_mtu
953 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
David Wragg55e5bfb2016-02-10 00:05:57 +0000954 */
Jarod Wilson91572082016-10-20 13:55:20 -0400955 if (new_mtu > dev->max_mtu)
956 new_mtu = dev->max_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000957
David Wragg55e5bfb2016-02-10 00:05:57 +0000958 dev->mtu = new_mtu;
959 return 0;
960}
961
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700962static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
963{
964 struct ip_tunnel_info *info = skb_tunnel_info(skb);
965 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700966
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400967 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800968 struct rtable *rt;
969 struct flowi4 fl4;
970
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400971 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
972 if (IS_ERR(rt))
973 return PTR_ERR(rt);
974
975 ip_rt_put(rt);
976 info->key.u.ipv4.src = fl4.saddr;
977#if IS_ENABLED(CONFIG_IPV6)
978 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800979 struct dst_entry *dst;
980 struct flowi6 fl6;
981
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400982 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
983 if (IS_ERR(dst))
984 return PTR_ERR(dst);
985
986 dst_release(dst);
987 info->key.u.ipv6.src = fl6.saddr;
988#endif
989 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700990 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400991 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700992
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700993 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
994 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800995 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700996 return 0;
997}
998
John W. Linville2d07dc72015-05-13 12:57:30 -0400999static const struct net_device_ops geneve_netdev_ops = {
1000 .ndo_init = geneve_init,
1001 .ndo_uninit = geneve_uninit,
1002 .ndo_open = geneve_open,
1003 .ndo_stop = geneve_stop,
1004 .ndo_start_xmit = geneve_xmit,
1005 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001006 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001007 .ndo_validate_addr = eth_validate_addr,
1008 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001009 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001010};
1011
1012static void geneve_get_drvinfo(struct net_device *dev,
1013 struct ethtool_drvinfo *drvinfo)
1014{
1015 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1016 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1017}
1018
1019static const struct ethtool_ops geneve_ethtool_ops = {
1020 .get_drvinfo = geneve_get_drvinfo,
1021 .get_link = ethtool_op_get_link,
1022};
1023
1024/* Info for udev, that this is a virtual tunnel endpoint */
1025static struct device_type geneve_type = {
1026 .name = "geneve",
1027};
1028
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001029/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001030 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001031 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001032 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001033static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001034{
1035 struct net *net = dev_net(dev);
1036 struct geneve_net *gn = net_generic(net, geneve_net_id);
1037 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001038
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001039 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -07001040 list_for_each_entry_rcu(gs, &gn->sock_list, list)
1041 udp_tunnel_push_rx_port(dev, gs->sock,
1042 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001043 rcu_read_unlock();
1044}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001045
John W. Linville2d07dc72015-05-13 12:57:30 -04001046/* Initialize the device structure. */
1047static void geneve_setup(struct net_device *dev)
1048{
1049 ether_setup(dev);
1050
1051 dev->netdev_ops = &geneve_netdev_ops;
1052 dev->ethtool_ops = &geneve_ethtool_ops;
1053 dev->destructor = free_netdev;
1054
1055 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1056
John W. Linville2d07dc72015-05-13 12:57:30 -04001057 dev->features |= NETIF_F_LLTX;
1058 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1059 dev->features |= NETIF_F_RXCSUM;
1060 dev->features |= NETIF_F_GSO_SOFTWARE;
1061
John W. Linville2d07dc72015-05-13 12:57:30 -04001062 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1063 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001064
Jarod Wilson91572082016-10-20 13:55:20 -04001065 /* MTU range: 68 - (something less than 65535) */
1066 dev->min_mtu = ETH_MIN_MTU;
1067 /* The max_mtu calculation does not take account of GENEVE
1068 * options, to avoid excluding potentially valid
1069 * configurations. This will be further reduced by IPvX hdr size.
1070 */
1071 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1072
John W. Linville2d07dc72015-05-13 12:57:30 -04001073 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001074 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001075 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001076 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001077}
1078
1079static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1080 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1081 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001082 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001083 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001084 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001085 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001086 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001087 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001088 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1089 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1090 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001091};
1092
1093static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1094{
1095 if (tb[IFLA_ADDRESS]) {
1096 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1097 return -EINVAL;
1098
1099 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1100 return -EADDRNOTAVAIL;
1101 }
1102
1103 if (!data)
1104 return -EINVAL;
1105
1106 if (data[IFLA_GENEVE_ID]) {
1107 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1108
1109 if (vni >= GENEVE_VID_MASK)
1110 return -ERANGE;
1111 }
1112
1113 return 0;
1114}
1115
Pravin B Shelar371bd102015-08-26 23:46:54 -07001116static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001117 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001118 bool *tun_on_same_port,
1119 bool *tun_collect_md)
1120{
pravin shelar9b4437a2016-11-21 11:02:58 -08001121 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001122
1123 *tun_on_same_port = false;
1124 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001125 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001126 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001127 *tun_collect_md = geneve->collect_md;
1128 *tun_on_same_port = true;
1129 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001130 if (info->key.tun_id == geneve->info.key.tun_id &&
1131 info->key.tp_dst == geneve->info.key.tp_dst &&
1132 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001133 t = geneve;
1134 }
1135 return t;
1136}
1137
pravin shelar9b4437a2016-11-21 11:02:58 -08001138static bool is_all_zero(const u8 *fp, size_t size)
1139{
1140 int i;
1141
1142 for (i = 0; i < size; i++)
1143 if (fp[i])
1144 return false;
1145 return true;
1146}
1147
1148static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1149{
1150 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1151 info->key.ttl || info->key.label || info->key.tp_src ||
1152 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1153 return false;
1154 else
1155 return true;
1156}
1157
Pravin B Shelare305ac62015-08-26 23:46:52 -07001158static int geneve_configure(struct net *net, struct net_device *dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001159 const struct ip_tunnel_info *info,
1160 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001161{
1162 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001163 struct geneve_dev *t, *geneve = netdev_priv(dev);
1164 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001165 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001166
pravin shelar9b4437a2016-11-21 11:02:58 -08001167 if (metadata && !is_tnl_info_zero(info))
John W. Linville8ed66f02015-10-26 17:01:44 -04001168 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001169
1170 geneve->net = net;
1171 geneve->dev = dev;
1172
pravin shelar9b4437a2016-11-21 11:02:58 -08001173 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001174 if (t)
1175 return -EBUSY;
1176
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001177 /* make enough headroom for basic scenario */
1178 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
pravin shelar9b4437a2016-11-21 11:02:58 -08001179 if (ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001180 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001181 dev->max_mtu -= sizeof(struct iphdr);
1182 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001183 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001184 dev->max_mtu -= sizeof(struct ipv6hdr);
1185 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001186 dev->needed_headroom = encap_len + ETH_HLEN;
1187
Pravin B Shelar371bd102015-08-26 23:46:54 -07001188 if (metadata) {
1189 if (tun_on_same_port)
1190 return -EPERM;
1191 } else {
1192 if (tun_collect_md)
1193 return -EPERM;
1194 }
1195
pravin shelar9b4437a2016-11-21 11:02:58 -08001196 dst_cache_reset(&geneve->info.dst_cache);
1197 geneve->info = *info;
1198 geneve->collect_md = metadata;
1199 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001200
John W. Linville2d07dc72015-05-13 12:57:30 -04001201 err = register_netdevice(dev);
1202 if (err)
1203 return err;
1204
1205 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001206 return 0;
1207}
1208
pravin shelar9b4437a2016-11-21 11:02:58 -08001209static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1210{
1211 memset(info, 0, sizeof(*info));
1212 info->key.tp_dst = htons(dst_port);
1213}
1214
Pravin B Shelare305ac62015-08-26 23:46:52 -07001215static int geneve_newlink(struct net *net, struct net_device *dev,
1216 struct nlattr *tb[], struct nlattr *data[])
1217{
pravin shelar9b4437a2016-11-21 11:02:58 -08001218 bool use_udp6_rx_checksums = false;
1219 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001220 bool metadata = false;
pravin shelar9b4437a2016-11-21 11:02:58 -08001221
1222 init_tnl_info(&info, GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001223
John W. Linville8ed66f02015-10-26 17:01:44 -04001224 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1225 return -EINVAL;
1226
1227 if (data[IFLA_GENEVE_REMOTE]) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001228 info.key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001229 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001230
pravin shelar9b4437a2016-11-21 11:02:58 -08001231 if (IN_MULTICAST(ntohl(info.key.u.ipv4.dst))) {
1232 netdev_dbg(dev, "multicast remote is unsupported\n");
John W. Linville8ed66f02015-10-26 17:01:44 -04001233 return -EINVAL;
1234 }
1235 }
1236
pravin shelar9b4437a2016-11-21 11:02:58 -08001237 if (data[IFLA_GENEVE_REMOTE6]) {
1238 #if IS_ENABLED(CONFIG_IPV6)
1239 info.mode = IP_TUNNEL_INFO_IPV6;
1240 info.key.u.ipv6.dst =
1241 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001242
pravin shelar9b4437a2016-11-21 11:02:58 -08001243 if (ipv6_addr_type(&info.key.u.ipv6.dst) &
1244 IPV6_ADDR_LINKLOCAL) {
1245 netdev_dbg(dev, "link-local remote is unsupported\n");
1246 return -EINVAL;
1247 }
1248 if (ipv6_addr_is_multicast(&info.key.u.ipv6.dst)) {
1249 netdev_dbg(dev, "multicast remote is unsupported\n");
1250 return -EINVAL;
1251 }
1252 info.key.tun_flags |= TUNNEL_CSUM;
1253 use_udp6_rx_checksums = true;
1254#else
1255 return -EPFNOSUPPORT;
1256#endif
1257 }
1258
1259 if (data[IFLA_GENEVE_ID]) {
1260 __u32 vni;
1261 __u8 tvni[3];
1262
1263 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1264 tvni[0] = (vni & 0x00ff0000) >> 16;
1265 tvni[1] = (vni & 0x0000ff00) >> 8;
1266 tvni[2] = vni & 0x000000ff;
1267
1268 info.key.tun_id = vni_to_tunnel_id(tvni);
1269 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001270 if (data[IFLA_GENEVE_TTL])
pravin shelar9b4437a2016-11-21 11:02:58 -08001271 info.key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001272
1273 if (data[IFLA_GENEVE_TOS])
pravin shelar9b4437a2016-11-21 11:02:58 -08001274 info.key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001275
pravin shelar9b4437a2016-11-21 11:02:58 -08001276 if (data[IFLA_GENEVE_LABEL]) {
1277 info.key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1278 IPV6_FLOWLABEL_MASK;
1279 if (info.key.label && (!(info.mode & IP_TUNNEL_INFO_IPV6)))
1280 return -EINVAL;
1281 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001282
Pravin B Shelare305ac62015-08-26 23:46:52 -07001283 if (data[IFLA_GENEVE_PORT])
pravin shelar9b4437a2016-11-21 11:02:58 -08001284 info.key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001285
1286 if (data[IFLA_GENEVE_COLLECT_METADATA])
1287 metadata = true;
1288
Tom Herbertabe492b2015-12-10 12:37:45 -08001289 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001290 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001291 info.key.tun_flags |= TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001292
1293 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1294 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001295 info.key.tun_flags &= ~TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001296
1297 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1298 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001299 use_udp6_rx_checksums = false;
Tom Herbertabe492b2015-12-10 12:37:45 -08001300
pravin shelar9b4437a2016-11-21 11:02:58 -08001301 return geneve_configure(net, dev, &info, metadata, use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001302}
1303
John W. Linville2d07dc72015-05-13 12:57:30 -04001304static void geneve_dellink(struct net_device *dev, struct list_head *head)
1305{
1306 struct geneve_dev *geneve = netdev_priv(dev);
1307
John W. Linville2d07dc72015-05-13 12:57:30 -04001308 list_del(&geneve->next);
1309 unregister_netdevice_queue(dev, head);
1310}
1311
1312static size_t geneve_get_size(const struct net_device *dev)
1313{
1314 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001315 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001316 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001317 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001318 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001319 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001320 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001321 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1322 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1323 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001324 0;
1325}
1326
1327static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1328{
1329 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001330 struct ip_tunnel_info *info = &geneve->info;
1331 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001332 __u32 vni;
1333
pravin shelar9b4437a2016-11-21 11:02:58 -08001334 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1335 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001336 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1337 goto nla_put_failure;
1338
pravin shelar9b4437a2016-11-21 11:02:58 -08001339 if (ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001340 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001341 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001342 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001343
1344 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1345 !!(info->key.tun_flags & TUNNEL_CSUM)))
1346 goto nla_put_failure;
1347
John W. Linville8ed66f02015-10-26 17:01:44 -04001348#if IS_ENABLED(CONFIG_IPV6)
1349 } else {
1350 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001351 &info->key.u.ipv6.dst))
1352 goto nla_put_failure;
1353
1354 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1355 !(info->key.tun_flags & TUNNEL_CSUM)))
1356 goto nla_put_failure;
1357
1358 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1359 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001360 goto nla_put_failure;
1361#endif
1362 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001363
pravin shelar9b4437a2016-11-21 11:02:58 -08001364 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1365 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1366 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001367 goto nla_put_failure;
1368
pravin shelar9b4437a2016-11-21 11:02:58 -08001369 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001370 goto nla_put_failure;
1371
Pravin B Shelare305ac62015-08-26 23:46:52 -07001372 if (geneve->collect_md) {
1373 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1374 goto nla_put_failure;
1375 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001376 return 0;
1377
1378nla_put_failure:
1379 return -EMSGSIZE;
1380}
1381
1382static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1383 .kind = "geneve",
1384 .maxtype = IFLA_GENEVE_MAX,
1385 .policy = geneve_policy,
1386 .priv_size = sizeof(struct geneve_dev),
1387 .setup = geneve_setup,
1388 .validate = geneve_validate,
1389 .newlink = geneve_newlink,
1390 .dellink = geneve_dellink,
1391 .get_size = geneve_get_size,
1392 .fill_info = geneve_fill_info,
1393};
1394
Pravin B Shelare305ac62015-08-26 23:46:52 -07001395struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1396 u8 name_assign_type, u16 dst_port)
1397{
1398 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001399 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001400 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001401 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001402 int err;
1403
1404 memset(tb, 0, sizeof(tb));
1405 dev = rtnl_create_link(net, name, name_assign_type,
1406 &geneve_link_ops, tb);
1407 if (IS_ERR(dev))
1408 return dev;
1409
pravin shelar9b4437a2016-11-21 11:02:58 -08001410 init_tnl_info(&info, dst_port);
1411 err = geneve_configure(net, dev, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001412 if (err) {
1413 free_netdev(dev);
1414 return ERR_PTR(err);
1415 }
David Wragg7e059152016-02-10 00:05:58 +00001416
1417 /* openvswitch users expect packet sizes to be unrestricted,
1418 * so set the largest MTU we can.
1419 */
Jarod Wilson91572082016-10-20 13:55:20 -04001420 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001421 if (err)
1422 goto err;
1423
Nicolas Dichtel41009482016-06-13 10:31:07 +02001424 err = rtnl_configure_link(dev, NULL);
1425 if (err < 0)
1426 goto err;
1427
Pravin B Shelare305ac62015-08-26 23:46:52 -07001428 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001429err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001430 geneve_dellink(dev, &list_kill);
1431 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001432 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001433}
1434EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1435
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001436static int geneve_netdevice_event(struct notifier_block *unused,
1437 unsigned long event, void *ptr)
1438{
1439 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1440
Alexander Duyck7c46a642016-06-16 12:21:00 -07001441 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001442 geneve_push_rx_ports(dev);
1443
1444 return NOTIFY_DONE;
1445}
1446
1447static struct notifier_block geneve_notifier_block __read_mostly = {
1448 .notifier_call = geneve_netdevice_event,
1449};
1450
John W. Linville2d07dc72015-05-13 12:57:30 -04001451static __net_init int geneve_init_net(struct net *net)
1452{
1453 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001454
1455 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001456 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001457 return 0;
1458}
1459
1460static void __net_exit geneve_exit_net(struct net *net)
1461{
1462 struct geneve_net *gn = net_generic(net, geneve_net_id);
1463 struct geneve_dev *geneve, *next;
1464 struct net_device *dev, *aux;
1465 LIST_HEAD(list);
1466
1467 rtnl_lock();
1468
1469 /* gather any geneve devices that were moved into this ns */
1470 for_each_netdev_safe(net, dev, aux)
1471 if (dev->rtnl_link_ops == &geneve_link_ops)
1472 unregister_netdevice_queue(dev, &list);
1473
1474 /* now gather any other geneve devices that were created in this ns */
1475 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1476 /* If geneve->dev is in the same netns, it was already added
1477 * to the list by the previous loop.
1478 */
1479 if (!net_eq(dev_net(geneve->dev), net))
1480 unregister_netdevice_queue(geneve->dev, &list);
1481 }
1482
1483 /* unregister the devices gathered above */
1484 unregister_netdevice_many(&list);
1485 rtnl_unlock();
1486}
1487
1488static struct pernet_operations geneve_net_ops = {
1489 .init = geneve_init_net,
1490 .exit = geneve_exit_net,
1491 .id = &geneve_net_id,
1492 .size = sizeof(struct geneve_net),
1493};
1494
1495static int __init geneve_init_module(void)
1496{
1497 int rc;
1498
1499 rc = register_pernet_subsys(&geneve_net_ops);
1500 if (rc)
1501 goto out1;
1502
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001503 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001504 if (rc)
1505 goto out2;
1506
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001507 rc = rtnl_link_register(&geneve_link_ops);
1508 if (rc)
1509 goto out3;
1510
John W. Linville2d07dc72015-05-13 12:57:30 -04001511 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001512out3:
1513 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001514out2:
1515 unregister_pernet_subsys(&geneve_net_ops);
1516out1:
1517 return rc;
1518}
1519late_initcall(geneve_init_module);
1520
1521static void __exit geneve_cleanup_module(void)
1522{
1523 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001524 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001525 unregister_pernet_subsys(&geneve_net_ops);
1526}
1527module_exit(geneve_cleanup_module);
1528
1529MODULE_LICENSE("GPL");
1530MODULE_VERSION(GENEVE_NETDEV_VER);
1531MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1532MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1533MODULE_ALIAS_RTNL_LINK("geneve");