blob: 33185b9a435e86a0f665eeef81b43042c0d20d90 [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>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/hash.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070018#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070019#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040020#include <net/rtnetlink.h>
21#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070022#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040023
24#define GENEVE_NETDEV_VER "0.6"
25
26#define GENEVE_UDP_PORT 6081
27
28#define GENEVE_N_VID (1u << 24)
29#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
30
31#define VNI_HASH_BITS 10
32#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
33
34static bool log_ecn_error = true;
35module_param(log_ecn_error, bool, 0644);
36MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
37
Pravin B Shelar371bd102015-08-26 23:46:54 -070038#define GENEVE_VER 0
39#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
40
John W. Linville2d07dc72015-05-13 12:57:30 -040041/* per-network namespace private data for this module */
42struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070044 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040045};
46
Pravin B Shelar371bd102015-08-26 23:46:54 -070047static int geneve_net_id;
48
John W. Linville8ed66f02015-10-26 17:01:44 -040049union geneve_addr {
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
52 struct sockaddr sa;
53};
54
55static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
56
John W. Linville2d07dc72015-05-13 12:57:30 -040057/* Pseudo network device */
58struct geneve_dev {
59 struct hlist_node hlist; /* vni hash table */
60 struct net *net; /* netns for packet i/o */
61 struct net_device *dev; /* netdev for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040062 struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */
63#if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */
65#endif
John W. Linville8760ce52015-06-01 15:51:34 -040066 u8 vni[3]; /* virtual network ID for tunnel */
67 u8 ttl; /* TTL override */
John W. Linvilled8951122015-06-01 15:51:35 -040068 u8 tos; /* TOS override */
John W. Linville8ed66f02015-10-26 17:01:44 -040069 union geneve_addr remote; /* IP address for link partner */
John W. Linville2d07dc72015-05-13 12:57:30 -040070 struct list_head next; /* geneve's per namespace list */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +010071 __be32 label; /* IPv6 flowlabel override */
Pravin B Shelarcd7918b2015-08-26 23:46:51 -070072 __be16 dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -070073 bool collect_md;
Jesse Gross8e816df2015-08-28 16:54:40 -070074 struct gro_cells gro_cells;
Tom Herbertabe492b2015-12-10 12:37:45 -080075 u32 flags;
Paolo Abeni468dfff2016-02-12 15:43:58 +010076 struct dst_cache dst_cache;
John W. Linville2d07dc72015-05-13 12:57:30 -040077};
78
Tom Herbertabe492b2015-12-10 12:37:45 -080079/* Geneve device flags */
Alexander Duyck14f1f722016-02-19 11:26:24 -080080#define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
Tom Herbertabe492b2015-12-10 12:37:45 -080081#define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
82#define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
83
Pravin B Shelar371bd102015-08-26 23:46:54 -070084struct geneve_sock {
85 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070086 struct list_head list;
87 struct socket *sock;
88 struct rcu_head rcu;
89 int refcnt;
90 struct udp_offload udp_offloads;
Pravin B Shelar66d47002015-08-26 23:46:55 -070091 struct hlist_head vni_list[VNI_HASH_SIZE];
Tom Herbertabe492b2015-12-10 12:37:45 -080092 u32 flags;
Pravin B Shelar371bd102015-08-26 23:46:54 -070093};
John W. Linville2d07dc72015-05-13 12:57:30 -040094
95static inline __u32 geneve_net_vni_hash(u8 vni[3])
96{
97 __u32 vnid;
98
99 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
100 return hash_32(vnid, VNI_HASH_BITS);
101}
102
Pravin B Shelare305ac62015-08-26 23:46:52 -0700103static __be64 vni_to_tunnel_id(const __u8 *vni)
104{
105#ifdef __BIG_ENDIAN
106 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
107#else
108 return (__force __be64)(((__force u64)vni[0] << 40) |
109 ((__force u64)vni[1] << 48) |
110 ((__force u64)vni[2] << 56));
111#endif
112}
113
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100114static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
115{
116 return gs->sock->sk->sk_family;
117}
118
Pravin B Shelar66d47002015-08-26 23:46:55 -0700119static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700120 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400121{
John W. Linville2d07dc72015-05-13 12:57:30 -0400122 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700123 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400124 __u32 hash;
125
John W. Linville2d07dc72015-05-13 12:57:30 -0400126 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700127 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700128 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700129 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700130 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400131 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700132 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400133 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700134 return NULL;
135}
136
John W. Linville8ed66f02015-10-26 17:01:44 -0400137#if IS_ENABLED(CONFIG_IPV6)
138static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
139 struct in6_addr addr6, u8 vni[])
140{
141 struct hlist_head *vni_list_head;
142 struct geneve_dev *geneve;
143 __u32 hash;
144
145 /* Find the device for this VNI */
146 hash = geneve_net_vni_hash(vni);
147 vni_list_head = &gs->vni_list[hash];
148 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
149 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
150 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
151 return geneve;
152 }
153 return NULL;
154}
155#endif
156
Pravin B Shelar371bd102015-08-26 23:46:54 -0700157static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
158{
159 return (struct genevehdr *)(udp_hdr(skb) + 1);
160}
161
Jiri Benc9fc47542016-02-18 11:22:50 +0100162static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
163 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700164{
Jiri Benc9fc47542016-02-18 11:22:50 +0100165 u8 *vni;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700166 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400167 static u8 zero_vni[3];
John W. Linville8ed66f02015-10-26 17:01:44 -0400168#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400169 static struct in6_addr zero_addr6;
170#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700171
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100172 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100173 struct iphdr *iph;
174
John W. Linville8ed66f02015-10-26 17:01:44 -0400175 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700176
John W. Linville8ed66f02015-10-26 17:01:44 -0400177 if (gs->collect_md) {
178 vni = zero_vni;
179 addr = 0;
180 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100181 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400182 addr = iph->saddr;
183 }
184
Jiri Benc9fc47542016-02-18 11:22:50 +0100185 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400186#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100187 } else if (geneve_get_sk_family(gs) == AF_INET6) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100188 struct ipv6hdr *ip6h;
189 struct in6_addr addr6;
190
John W. Linville8ed66f02015-10-26 17:01:44 -0400191 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
192
193 if (gs->collect_md) {
194 vni = zero_vni;
195 addr6 = zero_addr6;
196 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100197 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400198 addr6 = ip6h->saddr;
199 }
200
Jiri Benc9fc47542016-02-18 11:22:50 +0100201 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400202#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700203 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100204 return NULL;
205}
206
207/* geneve receive/decap routine */
208static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
209 struct sk_buff *skb)
210{
211 struct genevehdr *gnvh = geneve_hdr(skb);
212 struct metadata_dst *tun_dst = NULL;
213 struct pcpu_sw_netstats *stats;
214 int err = 0;
215 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400216
Pravin B Shelar371bd102015-08-26 23:46:54 -0700217 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700218 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700219
220 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
221 (gnvh->oam ? TUNNEL_OAM : 0) |
222 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
223
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100224 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700225 vni_to_tunnel_id(gnvh->vni),
226 gnvh->opt_len * 4);
227 if (!tun_dst)
228 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700230 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
231 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700232 } else {
233 /* Drop packets w/ critical options,
234 * since we don't support any...
235 */
236 if (gnvh->critical)
237 goto drop;
238 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400239
240 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400241 skb->protocol = eth_type_trans(skb, geneve->dev);
242 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
243
Pravin B Shelare305ac62015-08-26 23:46:52 -0700244 if (tun_dst)
245 skb_dst_set(skb, &tun_dst->dst);
246
John W. Linville2d07dc72015-05-13 12:57:30 -0400247 /* Ignore packet loops (and multicast echo) */
248 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
249 goto drop;
250
Jiri Benc9fc47542016-02-18 11:22:50 +0100251 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400252 skb_reset_network_header(skb);
253
Jiri Benc9fc47542016-02-18 11:22:50 +0100254 if (geneve_get_sk_family(gs) == AF_INET)
255 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400256#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100257 else
258 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400259#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400260
261 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400262 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100263 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400264 net_info_ratelimited("non-ECT from %pI4 "
265 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100266 &((struct iphdr *)oiph)->saddr,
267 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400268#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400270 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100271 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400272#endif
273 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400274 if (err > 1) {
275 ++geneve->dev->stats.rx_frame_errors;
276 ++geneve->dev->stats.rx_errors;
277 goto drop;
278 }
279 }
280
281 stats = this_cpu_ptr(geneve->dev->tstats);
282 u64_stats_update_begin(&stats->syncp);
283 stats->rx_packets++;
284 stats->rx_bytes += skb->len;
285 u64_stats_update_end(&stats->syncp);
286
Jesse Gross8e816df2015-08-28 16:54:40 -0700287 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400288 return;
289drop:
290 /* Consume bad packet */
291 kfree_skb(skb);
292}
293
294/* Setup stats when device is created */
295static int geneve_init(struct net_device *dev)
296{
Jesse Gross8e816df2015-08-28 16:54:40 -0700297 struct geneve_dev *geneve = netdev_priv(dev);
298 int err;
299
John W. Linville2d07dc72015-05-13 12:57:30 -0400300 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
301 if (!dev->tstats)
302 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700303
304 err = gro_cells_init(&geneve->gro_cells, dev);
305 if (err) {
306 free_percpu(dev->tstats);
307 return err;
308 }
309
Paolo Abeni468dfff2016-02-12 15:43:58 +0100310 err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL);
311 if (err) {
312 free_percpu(dev->tstats);
313 gro_cells_destroy(&geneve->gro_cells);
314 return err;
315 }
316
John W. Linville2d07dc72015-05-13 12:57:30 -0400317 return 0;
318}
319
320static void geneve_uninit(struct net_device *dev)
321{
Jesse Gross8e816df2015-08-28 16:54:40 -0700322 struct geneve_dev *geneve = netdev_priv(dev);
323
Paolo Abeni468dfff2016-02-12 15:43:58 +0100324 dst_cache_destroy(&geneve->dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700325 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400326 free_percpu(dev->tstats);
327}
328
Pravin B Shelar371bd102015-08-26 23:46:54 -0700329/* Callback from net/ipv4/udp.c to receive packets */
330static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
331{
332 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100333 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700334 struct geneve_sock *gs;
335 int opts_len;
336
337 /* Need Geneve and inner Ethernet header to be present */
338 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
339 goto error;
340
341 /* Return packets with reserved bits set */
342 geneveh = geneve_hdr(skb);
343 if (unlikely(geneveh->ver != GENEVE_VER))
344 goto error;
345
346 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
347 goto error;
348
Jiri Benc9fc47542016-02-18 11:22:50 +0100349 gs = rcu_dereference_sk_user_data(sk);
350 if (!gs)
351 goto drop;
352
353 geneve = geneve_lookup_skb(gs, skb);
354 if (!geneve)
355 goto drop;
356
Pravin B Shelar371bd102015-08-26 23:46:54 -0700357 opts_len = geneveh->opt_len * 4;
358 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100359 htons(ETH_P_TEB),
360 !net_eq(geneve->net, dev_net(geneve->dev))))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700361 goto drop;
362
Jiri Benc9fc47542016-02-18 11:22:50 +0100363 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700364 return 0;
365
366drop:
367 /* Consume bad packet */
368 kfree_skb(skb);
369 return 0;
370
371error:
372 /* Let the UDP layer deal with the skb */
373 return 1;
374}
375
376static struct socket *geneve_create_sock(struct net *net, bool ipv6,
Tom Herbertabe492b2015-12-10 12:37:45 -0800377 __be16 port, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700378{
379 struct socket *sock;
380 struct udp_port_cfg udp_conf;
381 int err;
382
383 memset(&udp_conf, 0, sizeof(udp_conf));
384
385 if (ipv6) {
386 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400387 udp_conf.ipv6_v6only = 1;
Tom Herbertabe492b2015-12-10 12:37:45 -0800388 udp_conf.use_udp6_rx_checksums =
389 !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700390 } else {
391 udp_conf.family = AF_INET;
392 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
393 }
394
395 udp_conf.local_udp_port = port;
396
397 /* Open UDP socket */
398 err = udp_sock_create(net, &udp_conf, &sock);
399 if (err < 0)
400 return ERR_PTR(err);
401
402 return sock;
403}
404
405static void geneve_notify_add_rx_port(struct geneve_sock *gs)
406{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800407 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700408 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800409 struct net *net = sock_net(sk);
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100410 sa_family_t sa_family = geneve_get_sk_family(gs);
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800411 __be16 port = inet_sk(sk)->inet_sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700412 int err;
413
414 if (sa_family == AF_INET) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100415 err = udp_add_offload(sock_net(sk), &gs->udp_offloads);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700416 if (err)
417 pr_warn("geneve: udp_add_offload failed with status %d\n",
418 err);
419 }
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800420
421 rcu_read_lock();
422 for_each_netdev_rcu(net, dev) {
423 if (dev->netdev_ops->ndo_add_geneve_port)
424 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family,
425 port);
426 }
427 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700428}
429
430static int geneve_hlen(struct genevehdr *gh)
431{
432 return sizeof(*gh) + gh->opt_len * 4;
433}
434
435static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
436 struct sk_buff *skb,
437 struct udp_offload *uoff)
438{
439 struct sk_buff *p, **pp = NULL;
440 struct genevehdr *gh, *gh2;
441 unsigned int hlen, gh_len, off_gnv;
442 const struct packet_offload *ptype;
443 __be16 type;
444 int flush = 1;
445
446 off_gnv = skb_gro_offset(skb);
447 hlen = off_gnv + sizeof(*gh);
448 gh = skb_gro_header_fast(skb, off_gnv);
449 if (skb_gro_header_hard(skb, hlen)) {
450 gh = skb_gro_header_slow(skb, hlen, off_gnv);
451 if (unlikely(!gh))
452 goto out;
453 }
454
455 if (gh->ver != GENEVE_VER || gh->oam)
456 goto out;
457 gh_len = geneve_hlen(gh);
458
459 hlen = off_gnv + gh_len;
460 if (skb_gro_header_hard(skb, hlen)) {
461 gh = skb_gro_header_slow(skb, hlen, off_gnv);
462 if (unlikely(!gh))
463 goto out;
464 }
465
466 flush = 0;
467
468 for (p = *head; p; p = p->next) {
469 if (!NAPI_GRO_CB(p)->same_flow)
470 continue;
471
472 gh2 = (struct genevehdr *)(p->data + off_gnv);
473 if (gh->opt_len != gh2->opt_len ||
474 memcmp(gh, gh2, gh_len)) {
475 NAPI_GRO_CB(p)->same_flow = 0;
476 continue;
477 }
478 }
479
480 type = gh->proto_type;
481
482 rcu_read_lock();
483 ptype = gro_find_receive_by_type(type);
484 if (!ptype) {
485 flush = 1;
486 goto out_unlock;
487 }
488
489 skb_gro_pull(skb, gh_len);
490 skb_gro_postpull_rcsum(skb, gh, gh_len);
491 pp = ptype->callbacks.gro_receive(head, skb);
492
493out_unlock:
494 rcu_read_unlock();
495out:
496 NAPI_GRO_CB(skb)->flush |= flush;
497
498 return pp;
499}
500
501static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
502 struct udp_offload *uoff)
503{
504 struct genevehdr *gh;
505 struct packet_offload *ptype;
506 __be16 type;
507 int gh_len;
508 int err = -ENOSYS;
509
510 udp_tunnel_gro_complete(skb, nhoff);
511
512 gh = (struct genevehdr *)(skb->data + nhoff);
513 gh_len = geneve_hlen(gh);
514 type = gh->proto_type;
515
516 rcu_read_lock();
517 ptype = gro_find_complete_by_type(type);
518 if (ptype)
519 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
520
521 rcu_read_unlock();
522 return err;
523}
524
525/* Create new listen socket if needed */
526static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
Tom Herbertabe492b2015-12-10 12:37:45 -0800527 bool ipv6, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528{
529 struct geneve_net *gn = net_generic(net, geneve_net_id);
530 struct geneve_sock *gs;
531 struct socket *sock;
532 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700533 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534
535 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
536 if (!gs)
537 return ERR_PTR(-ENOMEM);
538
Tom Herbertabe492b2015-12-10 12:37:45 -0800539 sock = geneve_create_sock(net, ipv6, port, flags);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700540 if (IS_ERR(sock)) {
541 kfree(gs);
542 return ERR_CAST(sock);
543 }
544
545 gs->sock = sock;
546 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700547 for (h = 0; h < VNI_HASH_SIZE; ++h)
548 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700549
550 /* Initialize the geneve udp offloads structure */
551 gs->udp_offloads.port = port;
552 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
553 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
554 geneve_notify_add_rx_port(gs);
555
556 /* Mark socket as an encapsulation socket */
557 tunnel_cfg.sk_user_data = gs;
558 tunnel_cfg.encap_type = 1;
559 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
560 tunnel_cfg.encap_destroy = NULL;
561 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700562 list_add(&gs->list, &gn->sock_list);
563 return gs;
564}
565
566static void geneve_notify_del_rx_port(struct geneve_sock *gs)
567{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800568 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700569 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800570 struct net *net = sock_net(sk);
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100571 sa_family_t sa_family = geneve_get_sk_family(gs);
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800572 __be16 port = inet_sk(sk)->inet_sport;
573
574 rcu_read_lock();
575 for_each_netdev_rcu(net, dev) {
576 if (dev->netdev_ops->ndo_del_geneve_port)
577 dev->netdev_ops->ndo_del_geneve_port(dev, sa_family,
578 port);
579 }
580
581 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700582
583 if (sa_family == AF_INET)
584 udp_del_offload(&gs->udp_offloads);
585}
586
John W. Linville8ed66f02015-10-26 17:01:44 -0400587static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700588{
John W. Linville8ed66f02015-10-26 17:01:44 -0400589 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590 return;
591
592 list_del(&gs->list);
593 geneve_notify_del_rx_port(gs);
594 udp_tunnel_sock_release(gs->sock);
595 kfree_rcu(gs, rcu);
596}
597
John W. Linville8ed66f02015-10-26 17:01:44 -0400598static void geneve_sock_release(struct geneve_dev *geneve)
599{
600 __geneve_sock_release(geneve->sock4);
601#if IS_ENABLED(CONFIG_IPV6)
602 __geneve_sock_release(geneve->sock6);
603#endif
604}
605
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400607 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700608 __be16 dst_port)
609{
610 struct geneve_sock *gs;
611
612 list_for_each_entry(gs, &gn->sock_list, list) {
613 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100614 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700615 return gs;
616 }
617 }
618 return NULL;
619}
620
John W. Linville8ed66f02015-10-26 17:01:44 -0400621static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400622{
John W. Linville2d07dc72015-05-13 12:57:30 -0400623 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700624 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400625 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700626 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400627
John W. Linville8ed66f02015-10-26 17:01:44 -0400628 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700629 if (gs) {
630 gs->refcnt++;
631 goto out;
632 }
633
Tom Herbertabe492b2015-12-10 12:37:45 -0800634 gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
John W. Linville2d07dc72015-05-13 12:57:30 -0400635 if (IS_ERR(gs))
636 return PTR_ERR(gs);
637
Pravin B Shelar371bd102015-08-26 23:46:54 -0700638out:
639 gs->collect_md = geneve->collect_md;
Tom Herbertabe492b2015-12-10 12:37:45 -0800640 gs->flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400641#if IS_ENABLED(CONFIG_IPV6)
642 if (ipv6)
643 geneve->sock6 = gs;
644 else
645#endif
646 geneve->sock4 = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700647
648 hash = geneve_net_vni_hash(geneve->vni);
649 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400650 return 0;
651}
652
John W. Linville8ed66f02015-10-26 17:01:44 -0400653static int geneve_open(struct net_device *dev)
654{
655 struct geneve_dev *geneve = netdev_priv(dev);
656 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
657 bool metadata = geneve->collect_md;
658 int ret = 0;
659
660 geneve->sock4 = NULL;
661#if IS_ENABLED(CONFIG_IPV6)
662 geneve->sock6 = NULL;
663 if (ipv6 || metadata)
664 ret = geneve_sock_add(geneve, true);
665#endif
666 if (!ret && (!ipv6 || metadata))
667 ret = geneve_sock_add(geneve, false);
668 if (ret < 0)
669 geneve_sock_release(geneve);
670
671 return ret;
672}
673
John W. Linville2d07dc72015-05-13 12:57:30 -0400674static int geneve_stop(struct net_device *dev)
675{
676 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400677
Pravin B Shelar66d47002015-08-26 23:46:55 -0700678 if (!hlist_unhashed(&geneve->hlist))
679 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400680 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400681 return 0;
682}
683
John W. Linville8ed66f02015-10-26 17:01:44 -0400684static void geneve_build_header(struct genevehdr *geneveh,
685 __be16 tun_flags, u8 vni[3],
686 u8 options_len, u8 *options)
687{
688 geneveh->ver = GENEVE_VER;
689 geneveh->opt_len = options_len / 4;
690 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
691 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
692 geneveh->rsvd1 = 0;
693 memcpy(geneveh->vni, vni, 3);
694 geneveh->proto_type = htons(ETH_P_TEB);
695 geneveh->rsvd2 = 0;
696
697 memcpy(geneveh->options, options, options_len);
698}
699
Pravin B Shelar371bd102015-08-26 23:46:54 -0700700static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
701 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800702 u32 flags, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700703{
704 struct genevehdr *gnvh;
705 int min_headroom;
706 int err;
Alexander Duyck14f1f722016-02-19 11:26:24 -0800707 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700708
John W. Linville8ed66f02015-10-26 17:01:44 -0400709 skb_scrub_packet(skb, xnet);
710
Pravin B Shelar371bd102015-08-26 23:46:54 -0700711 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
712 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
713 err = skb_cow_head(skb, min_headroom);
714 if (unlikely(err)) {
715 kfree_skb(skb);
716 goto free_rt;
717 }
718
Tom Herbertabe492b2015-12-10 12:37:45 -0800719 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700720 if (IS_ERR(skb)) {
721 err = PTR_ERR(skb);
722 goto free_rt;
723 }
724
725 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400726 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700727
728 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
729 return 0;
730
731free_rt:
732 ip_rt_put(rt);
733 return err;
734}
735
John W. Linville8ed66f02015-10-26 17:01:44 -0400736#if IS_ENABLED(CONFIG_IPV6)
737static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
738 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800739 u32 flags, bool xnet)
John W. Linville8ed66f02015-10-26 17:01:44 -0400740{
741 struct genevehdr *gnvh;
742 int min_headroom;
743 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800744 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
John W. Linville8ed66f02015-10-26 17:01:44 -0400745
746 skb_scrub_packet(skb, xnet);
747
748 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
749 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
750 err = skb_cow_head(skb, min_headroom);
751 if (unlikely(err)) {
752 kfree_skb(skb);
753 goto free_dst;
754 }
755
Tom Herbertabe492b2015-12-10 12:37:45 -0800756 skb = udp_tunnel_handle_offloads(skb, udp_sum);
John W. Linville8ed66f02015-10-26 17:01:44 -0400757 if (IS_ERR(skb)) {
758 err = PTR_ERR(skb);
759 goto free_dst;
760 }
761
762 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
763 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
764
765 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
766 return 0;
767
768free_dst:
769 dst_release(dst);
770 return err;
771}
772#endif
773
774static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
775 struct net_device *dev,
776 struct flowi4 *fl4,
777 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700778{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100779 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700780 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100781 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700782 struct rtable *rt = NULL;
783 __u8 tos;
784
785 memset(fl4, 0, sizeof(*fl4));
786 fl4->flowi4_mark = skb->mark;
787 fl4->flowi4_proto = IPPROTO_UDP;
788
789 if (info) {
790 fl4->daddr = info->key.u.ipv4.dst;
791 fl4->saddr = info->key.u.ipv4.src;
792 fl4->flowi4_tos = RT_TOS(info->key.tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100793 dst_cache = &info->dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700794 } else {
795 tos = geneve->tos;
796 if (tos == 1) {
797 const struct iphdr *iip = ip_hdr(skb);
798
799 tos = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100800 use_cache = false;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700801 }
802
803 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400804 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100805 dst_cache = &geneve->dst_cache;
806 }
807
Paolo Abeni468dfff2016-02-12 15:43:58 +0100808 if (use_cache) {
809 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
810 if (rt)
811 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700812 }
813
814 rt = ip_route_output_key(geneve->net, fl4);
815 if (IS_ERR(rt)) {
816 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700817 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700818 }
819 if (rt->dst.dev == dev) { /* is this necessary? */
820 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700821 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700822 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700823 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100824 if (use_cache)
825 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700826 return rt;
827}
828
John W. Linville8ed66f02015-10-26 17:01:44 -0400829#if IS_ENABLED(CONFIG_IPV6)
830static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
831 struct net_device *dev,
832 struct flowi6 *fl6,
833 struct ip_tunnel_info *info)
834{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100835 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400836 struct geneve_dev *geneve = netdev_priv(dev);
837 struct geneve_sock *gs6 = geneve->sock6;
838 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100839 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400840 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400841
842 memset(fl6, 0, sizeof(*fl6));
843 fl6->flowi6_mark = skb->mark;
844 fl6->flowi6_proto = IPPROTO_UDP;
845
846 if (info) {
847 fl6->daddr = info->key.u.ipv6.dst;
848 fl6->saddr = info->key.u.ipv6.src;
John W. Linville3a56f862015-10-26 17:01:45 -0400849 fl6->flowi6_tos = RT_TOS(info->key.tos);
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100850 fl6->flowlabel = info->key.label;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100851 dst_cache = &info->dst_cache;
John W. Linville8ed66f02015-10-26 17:01:44 -0400852 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400853 prio = geneve->tos;
854 if (prio == 1) {
855 const struct iphdr *iip = ip_hdr(skb);
856
857 prio = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100858 use_cache = false;
John W. Linville3a56f862015-10-26 17:01:45 -0400859 }
860
861 fl6->flowi6_tos = RT_TOS(prio);
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100862 fl6->flowlabel = geneve->label;
John W. Linville8ed66f02015-10-26 17:01:44 -0400863 fl6->daddr = geneve->remote.sin6.sin6_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100864 dst_cache = &geneve->dst_cache;
865 }
866
Paolo Abeni468dfff2016-02-12 15:43:58 +0100867 if (use_cache) {
868 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
869 if (dst)
870 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400871 }
872
873 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
874 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
875 return ERR_PTR(-ENETUNREACH);
876 }
877 if (dst->dev == dev) { /* is this necessary? */
878 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
879 dst_release(dst);
880 return ERR_PTR(-ELOOP);
881 }
882
Paolo Abeni468dfff2016-02-12 15:43:58 +0100883 if (use_cache)
884 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400885 return dst;
886}
887#endif
888
Pravin B Shelare305ac62015-08-26 23:46:52 -0700889/* Convert 64 bit tunnel ID to 24 bit VNI. */
890static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
891{
892#ifdef __BIG_ENDIAN
893 vni[0] = (__force __u8)(tun_id >> 16);
894 vni[1] = (__force __u8)(tun_id >> 8);
895 vni[2] = (__force __u8)tun_id;
896#else
897 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
898 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
899 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
900#endif
901}
902
John W. Linville8ed66f02015-10-26 17:01:44 -0400903static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
904 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400905{
906 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400907 struct geneve_sock *gs4 = geneve->sock4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400908 struct rtable *rt = NULL;
John W. Linville08399ef2015-09-21 10:29:09 -0400909 const struct iphdr *iip; /* interior IP header */
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700910 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400911 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400912 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700913 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700914 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400915 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800916 u32 flags = geneve->flags;
John W. Linville2d07dc72015-05-13 12:57:30 -0400917
Pravin B Shelare305ac62015-08-26 23:46:52 -0700918 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400919 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700920 netdev_dbg(dev, "no tunnel metadata\n");
921 goto tx_error;
922 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200923 if (info && ip_tunnel_info_af(info) != AF_INET)
924 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700925 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400926
John W. Linville8ed66f02015-10-26 17:01:44 -0400927 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400928 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700929 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400930 goto tx_error;
931 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700932
933 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700934 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700935
John W. Linville08399ef2015-09-21 10:29:09 -0400936 iip = ip_hdr(skb);
937
Pravin B Shelare305ac62015-08-26 23:46:52 -0700938 if (info) {
939 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700940 u8 *opts = NULL;
941 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700942
943 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +0100944 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700945 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700946
Tom Herbertabe492b2015-12-10 12:37:45 -0800947 if (key->tun_flags & TUNNEL_CSUM)
Alexander Duyck14f1f722016-02-19 11:26:24 -0800948 flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800949 else
Alexander Duyck14f1f722016-02-19 11:26:24 -0800950 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800951
Pravin B Shelar371bd102015-08-26 23:46:54 -0700952 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800953 info->options_len, opts, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700954 if (unlikely(err))
955 goto err;
956
John W. Linville08399ef2015-09-21 10:29:09 -0400957 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700958 ttl = key->ttl;
959 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700960 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700961 err = geneve_build_skb(rt, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800962 0, NULL, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700963 if (unlikely(err))
964 goto err;
965
Pravin B Shelare305ac62015-08-26 23:46:52 -0700966 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700967 ttl = geneve->ttl;
968 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
969 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700970 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700971 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400972 }
Pravin B Shelar039f5062015-12-24 14:34:54 -0800973 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
974 tos, ttl, df, sport, geneve->dst_port,
975 !net_eq(geneve->net, dev_net(geneve->dev)),
Alexander Duyck14f1f722016-02-19 11:26:24 -0800976 !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX));
John W. Linville2d07dc72015-05-13 12:57:30 -0400977
John W. Linville2d07dc72015-05-13 12:57:30 -0400978 return NETDEV_TX_OK;
979
John W. Linville2d07dc72015-05-13 12:57:30 -0400980tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400981 dev_kfree_skb(skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700982err:
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700983 if (err == -ELOOP)
984 dev->stats.collisions++;
985 else if (err == -ENETUNREACH)
986 dev->stats.tx_carrier_errors++;
987 else
988 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400989 return NETDEV_TX_OK;
990}
991
John W. Linville8ed66f02015-10-26 17:01:44 -0400992#if IS_ENABLED(CONFIG_IPV6)
993static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
994 struct ip_tunnel_info *info)
995{
996 struct geneve_dev *geneve = netdev_priv(dev);
997 struct geneve_sock *gs6 = geneve->sock6;
998 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400999 const struct iphdr *iip; /* interior IP header */
John W. Linville8ed66f02015-10-26 17:01:44 -04001000 int err = -EINVAL;
1001 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -04001002 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -04001003 __be16 sport;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001004 __be32 label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001005 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -08001006 u32 flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -04001007
1008 if (geneve->collect_md) {
1009 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
1010 netdev_dbg(dev, "no tunnel metadata\n");
1011 goto tx_error;
1012 }
1013 }
1014
1015 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1016 if (IS_ERR(dst)) {
1017 err = PTR_ERR(dst);
1018 goto tx_error;
1019 }
1020
1021 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
1022 skb_reset_mac_header(skb);
1023
John W. Linville3a56f862015-10-26 17:01:45 -04001024 iip = ip_hdr(skb);
1025
John W. Linville8ed66f02015-10-26 17:01:44 -04001026 if (info) {
1027 const struct ip_tunnel_key *key = &info->key;
1028 u8 *opts = NULL;
1029 u8 vni[3];
1030
1031 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001032 if (info->options_len)
John W. Linville8ed66f02015-10-26 17:01:44 -04001033 opts = ip_tunnel_info_opts(info);
1034
Tom Herbertabe492b2015-12-10 12:37:45 -08001035 if (key->tun_flags & TUNNEL_CSUM)
Jesse Gross35e2d112016-01-20 16:22:47 -08001036 flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001037 else
Jesse Gross35e2d112016-01-20 16:22:47 -08001038 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001039
John W. Linville8ed66f02015-10-26 17:01:44 -04001040 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
1041 info->options_len, opts,
Tom Herbertabe492b2015-12-10 12:37:45 -08001042 flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001043 if (unlikely(err))
1044 goto err;
1045
John W. Linville3a56f862015-10-26 17:01:45 -04001046 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001047 ttl = key->ttl;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001048 label = info->key.label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001049 } else {
John W. Linville8ed66f02015-10-26 17:01:44 -04001050 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -08001051 0, NULL, flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001052 if (unlikely(err))
1053 goto err;
1054
John W. Linville3a56f862015-10-26 17:01:45 -04001055 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001056 ttl = geneve->ttl;
1057 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
1058 ttl = 1;
1059 ttl = ttl ? : ip6_dst_hoplimit(dst);
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001060 label = geneve->label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001061 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001062
Pravin B Shelar039f5062015-12-24 14:34:54 -08001063 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001064 &fl6.saddr, &fl6.daddr, prio, ttl, label,
Pravin B Shelar039f5062015-12-24 14:34:54 -08001065 sport, geneve->dst_port,
1066 !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
John W. Linville8ed66f02015-10-26 17:01:44 -04001067 return NETDEV_TX_OK;
1068
1069tx_error:
1070 dev_kfree_skb(skb);
1071err:
1072 if (err == -ELOOP)
1073 dev->stats.collisions++;
1074 else if (err == -ENETUNREACH)
1075 dev->stats.tx_carrier_errors++;
1076 else
1077 dev->stats.tx_errors++;
1078 return NETDEV_TX_OK;
1079}
1080#endif
1081
1082static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1083{
1084 struct geneve_dev *geneve = netdev_priv(dev);
1085 struct ip_tunnel_info *info = NULL;
1086
1087 if (geneve->collect_md)
1088 info = skb_tunnel_info(skb);
1089
1090#if IS_ENABLED(CONFIG_IPV6)
1091 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
1092 (!info && geneve->remote.sa.sa_family == AF_INET6))
1093 return geneve6_xmit_skb(skb, dev, info);
1094#endif
1095 return geneve_xmit_skb(skb, dev, info);
1096}
1097
David Wraggaeee0e62016-02-18 17:43:29 +00001098static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict)
David Wragg55e5bfb2016-02-10 00:05:57 +00001099{
David Wraggaeee0e62016-02-18 17:43:29 +00001100 /* The max_mtu calculation does not take account of GENEVE
1101 * options, to avoid excluding potentially valid
1102 * configurations.
David Wragg55e5bfb2016-02-10 00:05:57 +00001103 */
David Wraggaeee0e62016-02-18 17:43:29 +00001104 int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - sizeof(struct iphdr)
1105 - dev->hard_header_len;
1106
1107 if (new_mtu < 68)
David Wragg55e5bfb2016-02-10 00:05:57 +00001108 return -EINVAL;
David Wraggaeee0e62016-02-18 17:43:29 +00001109
1110 if (new_mtu > max_mtu) {
1111 if (strict)
1112 return -EINVAL;
1113
1114 new_mtu = max_mtu;
1115 }
1116
David Wragg55e5bfb2016-02-10 00:05:57 +00001117 dev->mtu = new_mtu;
1118 return 0;
1119}
1120
David Wraggaeee0e62016-02-18 17:43:29 +00001121static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1122{
1123 return __geneve_change_mtu(dev, new_mtu, true);
1124}
1125
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001126static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1127{
1128 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1129 struct geneve_dev *geneve = netdev_priv(dev);
1130 struct rtable *rt;
1131 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001132#if IS_ENABLED(CONFIG_IPV6)
1133 struct dst_entry *dst;
1134 struct flowi6 fl6;
1135#endif
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001136
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001137 if (ip_tunnel_info_af(info) == AF_INET) {
1138 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1139 if (IS_ERR(rt))
1140 return PTR_ERR(rt);
1141
1142 ip_rt_put(rt);
1143 info->key.u.ipv4.src = fl4.saddr;
1144#if IS_ENABLED(CONFIG_IPV6)
1145 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1146 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1147 if (IS_ERR(dst))
1148 return PTR_ERR(dst);
1149
1150 dst_release(dst);
1151 info->key.u.ipv6.src = fl6.saddr;
1152#endif
1153 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001154 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001155 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001156
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001157 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1158 1, USHRT_MAX, true);
1159 info->key.tp_dst = geneve->dst_port;
1160 return 0;
1161}
1162
John W. Linville2d07dc72015-05-13 12:57:30 -04001163static const struct net_device_ops geneve_netdev_ops = {
1164 .ndo_init = geneve_init,
1165 .ndo_uninit = geneve_uninit,
1166 .ndo_open = geneve_open,
1167 .ndo_stop = geneve_stop,
1168 .ndo_start_xmit = geneve_xmit,
1169 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001170 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001171 .ndo_validate_addr = eth_validate_addr,
1172 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001173 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001174};
1175
1176static void geneve_get_drvinfo(struct net_device *dev,
1177 struct ethtool_drvinfo *drvinfo)
1178{
1179 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1180 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1181}
1182
1183static const struct ethtool_ops geneve_ethtool_ops = {
1184 .get_drvinfo = geneve_get_drvinfo,
1185 .get_link = ethtool_op_get_link,
1186};
1187
1188/* Info for udev, that this is a virtual tunnel endpoint */
1189static struct device_type geneve_type = {
1190 .name = "geneve",
1191};
1192
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001193/* Calls the ndo_add_geneve_port of the caller in order to
1194 * supply the listening GENEVE udp ports. Callers are expected
1195 * to implement the ndo_add_geneve_port.
1196 */
1197void geneve_get_rx_port(struct net_device *dev)
1198{
1199 struct net *net = dev_net(dev);
1200 struct geneve_net *gn = net_generic(net, geneve_net_id);
1201 struct geneve_sock *gs;
1202 sa_family_t sa_family;
1203 struct sock *sk;
1204 __be16 port;
1205
1206 rcu_read_lock();
1207 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1208 sk = gs->sock->sk;
1209 sa_family = sk->sk_family;
1210 port = inet_sk(sk)->inet_sport;
1211 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port);
1212 }
1213 rcu_read_unlock();
1214}
1215EXPORT_SYMBOL_GPL(geneve_get_rx_port);
1216
John W. Linville2d07dc72015-05-13 12:57:30 -04001217/* Initialize the device structure. */
1218static void geneve_setup(struct net_device *dev)
1219{
1220 ether_setup(dev);
1221
1222 dev->netdev_ops = &geneve_netdev_ops;
1223 dev->ethtool_ops = &geneve_ethtool_ops;
1224 dev->destructor = free_netdev;
1225
1226 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1227
John W. Linville2d07dc72015-05-13 12:57:30 -04001228 dev->features |= NETIF_F_LLTX;
1229 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1230 dev->features |= NETIF_F_RXCSUM;
1231 dev->features |= NETIF_F_GSO_SOFTWARE;
1232
John W. Linville2d07dc72015-05-13 12:57:30 -04001233 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1234 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001235
1236 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001237 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001238 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001239 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001240}
1241
1242static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1243 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1244 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001245 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001246 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001247 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001248 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001249 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001250 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001251 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1252 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1253 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001254};
1255
1256static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1257{
1258 if (tb[IFLA_ADDRESS]) {
1259 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1260 return -EINVAL;
1261
1262 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1263 return -EADDRNOTAVAIL;
1264 }
1265
1266 if (!data)
1267 return -EINVAL;
1268
1269 if (data[IFLA_GENEVE_ID]) {
1270 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1271
1272 if (vni >= GENEVE_VID_MASK)
1273 return -ERANGE;
1274 }
1275
1276 return 0;
1277}
1278
Pravin B Shelar371bd102015-08-26 23:46:54 -07001279static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1280 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001281 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001282 u8 vni[],
1283 bool *tun_on_same_port,
1284 bool *tun_collect_md)
1285{
1286 struct geneve_dev *geneve, *t;
1287
1288 *tun_on_same_port = false;
1289 *tun_collect_md = false;
1290 t = NULL;
1291 list_for_each_entry(geneve, &gn->geneve_list, next) {
1292 if (geneve->dst_port == dst_port) {
1293 *tun_collect_md = geneve->collect_md;
1294 *tun_on_same_port = true;
1295 }
1296 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001297 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001298 dst_port == geneve->dst_port)
1299 t = geneve;
1300 }
1301 return t;
1302}
1303
Pravin B Shelare305ac62015-08-26 23:46:52 -07001304static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001305 union geneve_addr *remote,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001306 __u32 vni, __u8 ttl, __u8 tos, __be32 label,
1307 __be16 dst_port, bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001308{
1309 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001310 struct geneve_dev *t, *geneve = netdev_priv(dev);
1311 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001312 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001313
John W. Linville8ed66f02015-10-26 17:01:44 -04001314 if (!remote)
1315 return -EINVAL;
1316 if (metadata &&
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001317 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl || label))
John W. Linville8ed66f02015-10-26 17:01:44 -04001318 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001319
1320 geneve->net = net;
1321 geneve->dev = dev;
1322
John W. Linville2d07dc72015-05-13 12:57:30 -04001323 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1324 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1325 geneve->vni[2] = vni & 0x000000ff;
1326
John W. Linville8ed66f02015-10-26 17:01:44 -04001327 if ((remote->sa.sa_family == AF_INET &&
1328 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1329 (remote->sa.sa_family == AF_INET6 &&
1330 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001331 return -EINVAL;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001332 if (label && remote->sa.sa_family != AF_INET6)
1333 return -EINVAL;
1334
John W. Linville8ed66f02015-10-26 17:01:44 -04001335 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001336
Pravin B Shelare305ac62015-08-26 23:46:52 -07001337 geneve->ttl = ttl;
1338 geneve->tos = tos;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001339 geneve->label = label;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001340 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001341 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001342 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001343
John W. Linville8ed66f02015-10-26 17:01:44 -04001344 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001345 &tun_on_same_port, &tun_collect_md);
1346 if (t)
1347 return -EBUSY;
1348
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001349 /* make enough headroom for basic scenario */
1350 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1351 if (remote->sa.sa_family == AF_INET)
1352 encap_len += sizeof(struct iphdr);
1353 else
1354 encap_len += sizeof(struct ipv6hdr);
1355 dev->needed_headroom = encap_len + ETH_HLEN;
1356
Pravin B Shelar371bd102015-08-26 23:46:54 -07001357 if (metadata) {
1358 if (tun_on_same_port)
1359 return -EPERM;
1360 } else {
1361 if (tun_collect_md)
1362 return -EPERM;
1363 }
1364
Paolo Abeni468dfff2016-02-12 15:43:58 +01001365 dst_cache_reset(&geneve->dst_cache);
1366
John W. Linville2d07dc72015-05-13 12:57:30 -04001367 err = register_netdevice(dev);
1368 if (err)
1369 return err;
1370
1371 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001372 return 0;
1373}
1374
Pravin B Shelare305ac62015-08-26 23:46:52 -07001375static int geneve_newlink(struct net *net, struct net_device *dev,
1376 struct nlattr *tb[], struct nlattr *data[])
1377{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001378 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001379 __u8 ttl = 0, tos = 0;
1380 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001381 union geneve_addr remote = geneve_remote_unspec;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001382 __be32 label = 0;
Jesse Grosse277de52015-10-16 16:36:00 -07001383 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001384 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001385
John W. Linville8ed66f02015-10-26 17:01:44 -04001386 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1387 return -EINVAL;
1388
1389 if (data[IFLA_GENEVE_REMOTE]) {
1390 remote.sa.sa_family = AF_INET;
1391 remote.sin.sin_addr.s_addr =
1392 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1393 }
1394
1395 if (data[IFLA_GENEVE_REMOTE6]) {
1396 if (!IS_ENABLED(CONFIG_IPV6))
1397 return -EPFNOSUPPORT;
1398
1399 remote.sa.sa_family = AF_INET6;
1400 remote.sin6.sin6_addr =
1401 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1402
1403 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1404 IPV6_ADDR_LINKLOCAL) {
1405 netdev_dbg(dev, "link-local remote is unsupported\n");
1406 return -EINVAL;
1407 }
1408 }
1409
Jesse Grosse277de52015-10-16 16:36:00 -07001410 if (data[IFLA_GENEVE_ID])
1411 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001412
Pravin B Shelare305ac62015-08-26 23:46:52 -07001413 if (data[IFLA_GENEVE_TTL])
1414 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1415
1416 if (data[IFLA_GENEVE_TOS])
1417 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1418
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001419 if (data[IFLA_GENEVE_LABEL])
1420 label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1421 IPV6_FLOWLABEL_MASK;
1422
Pravin B Shelare305ac62015-08-26 23:46:52 -07001423 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001424 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001425
1426 if (data[IFLA_GENEVE_COLLECT_METADATA])
1427 metadata = true;
1428
Tom Herbertabe492b2015-12-10 12:37:45 -08001429 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001430 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1431 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001432
1433 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1434 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1435 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1436
1437 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1438 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1439 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1440
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001441 return geneve_configure(net, dev, &remote, vni, ttl, tos, label,
1442 dst_port, metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001443}
1444
John W. Linville2d07dc72015-05-13 12:57:30 -04001445static void geneve_dellink(struct net_device *dev, struct list_head *head)
1446{
1447 struct geneve_dev *geneve = netdev_priv(dev);
1448
John W. Linville2d07dc72015-05-13 12:57:30 -04001449 list_del(&geneve->next);
1450 unregister_netdevice_queue(dev, head);
1451}
1452
1453static size_t geneve_get_size(const struct net_device *dev)
1454{
1455 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001456 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001457 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001458 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001459 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001460 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001461 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001462 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1463 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1464 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001465 0;
1466}
1467
1468static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1469{
1470 struct geneve_dev *geneve = netdev_priv(dev);
1471 __u32 vni;
1472
1473 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1474 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1475 goto nla_put_failure;
1476
John W. Linville8ed66f02015-10-26 17:01:44 -04001477 if (geneve->remote.sa.sa_family == AF_INET) {
1478 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1479 geneve->remote.sin.sin_addr.s_addr))
1480 goto nla_put_failure;
1481#if IS_ENABLED(CONFIG_IPV6)
1482 } else {
1483 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1484 &geneve->remote.sin6.sin6_addr))
1485 goto nla_put_failure;
1486#endif
1487 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001488
John W. Linvilled8951122015-06-01 15:51:35 -04001489 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001490 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos) ||
1491 nla_put_be32(skb, IFLA_GENEVE_LABEL, geneve->label))
John W. Linville8760ce52015-06-01 15:51:34 -04001492 goto nla_put_failure;
1493
John W. Linville7bbe33f2015-09-22 13:09:32 -04001494 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001495 goto nla_put_failure;
1496
Pravin B Shelare305ac62015-08-26 23:46:52 -07001497 if (geneve->collect_md) {
1498 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1499 goto nla_put_failure;
1500 }
1501
Tom Herbertabe492b2015-12-10 12:37:45 -08001502 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
Alexander Duyck14f1f722016-02-19 11:26:24 -08001503 !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbertabe492b2015-12-10 12:37:45 -08001504 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1505 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1506 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1507 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1508 goto nla_put_failure;
1509
John W. Linville2d07dc72015-05-13 12:57:30 -04001510 return 0;
1511
1512nla_put_failure:
1513 return -EMSGSIZE;
1514}
1515
1516static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1517 .kind = "geneve",
1518 .maxtype = IFLA_GENEVE_MAX,
1519 .policy = geneve_policy,
1520 .priv_size = sizeof(struct geneve_dev),
1521 .setup = geneve_setup,
1522 .validate = geneve_validate,
1523 .newlink = geneve_newlink,
1524 .dellink = geneve_dellink,
1525 .get_size = geneve_get_size,
1526 .fill_info = geneve_fill_info,
1527};
1528
Pravin B Shelare305ac62015-08-26 23:46:52 -07001529struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1530 u8 name_assign_type, u16 dst_port)
1531{
1532 struct nlattr *tb[IFLA_MAX + 1];
1533 struct net_device *dev;
1534 int err;
1535
1536 memset(tb, 0, sizeof(tb));
1537 dev = rtnl_create_link(net, name, name_assign_type,
1538 &geneve_link_ops, tb);
1539 if (IS_ERR(dev))
1540 return dev;
1541
John W. Linville8ed66f02015-10-26 17:01:44 -04001542 err = geneve_configure(net, dev, &geneve_remote_unspec,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001543 0, 0, 0, 0, htons(dst_port), true,
Paolo Abenic868ee72016-02-17 19:30:01 +01001544 GENEVE_F_UDP_ZERO_CSUM6_RX);
David Wragg7e059152016-02-10 00:05:58 +00001545 if (err)
1546 goto err;
1547
1548 /* openvswitch users expect packet sizes to be unrestricted,
1549 * so set the largest MTU we can.
1550 */
David Wraggaeee0e62016-02-18 17:43:29 +00001551 err = __geneve_change_mtu(dev, IP_MAX_MTU, false);
David Wragg7e059152016-02-10 00:05:58 +00001552 if (err)
1553 goto err;
1554
Pravin B Shelare305ac62015-08-26 23:46:52 -07001555 return dev;
David Wragg7e059152016-02-10 00:05:58 +00001556
1557 err:
1558 free_netdev(dev);
1559 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001560}
1561EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1562
John W. Linville2d07dc72015-05-13 12:57:30 -04001563static __net_init int geneve_init_net(struct net *net)
1564{
1565 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001566
1567 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001568 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001569 return 0;
1570}
1571
1572static void __net_exit geneve_exit_net(struct net *net)
1573{
1574 struct geneve_net *gn = net_generic(net, geneve_net_id);
1575 struct geneve_dev *geneve, *next;
1576 struct net_device *dev, *aux;
1577 LIST_HEAD(list);
1578
1579 rtnl_lock();
1580
1581 /* gather any geneve devices that were moved into this ns */
1582 for_each_netdev_safe(net, dev, aux)
1583 if (dev->rtnl_link_ops == &geneve_link_ops)
1584 unregister_netdevice_queue(dev, &list);
1585
1586 /* now gather any other geneve devices that were created in this ns */
1587 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1588 /* If geneve->dev is in the same netns, it was already added
1589 * to the list by the previous loop.
1590 */
1591 if (!net_eq(dev_net(geneve->dev), net))
1592 unregister_netdevice_queue(geneve->dev, &list);
1593 }
1594
1595 /* unregister the devices gathered above */
1596 unregister_netdevice_many(&list);
1597 rtnl_unlock();
1598}
1599
1600static struct pernet_operations geneve_net_ops = {
1601 .init = geneve_init_net,
1602 .exit = geneve_exit_net,
1603 .id = &geneve_net_id,
1604 .size = sizeof(struct geneve_net),
1605};
1606
1607static int __init geneve_init_module(void)
1608{
1609 int rc;
1610
1611 rc = register_pernet_subsys(&geneve_net_ops);
1612 if (rc)
1613 goto out1;
1614
1615 rc = rtnl_link_register(&geneve_link_ops);
1616 if (rc)
1617 goto out2;
1618
1619 return 0;
1620out2:
1621 unregister_pernet_subsys(&geneve_net_ops);
1622out1:
1623 return rc;
1624}
1625late_initcall(geneve_init_module);
1626
1627static void __exit geneve_cleanup_module(void)
1628{
1629 rtnl_link_unregister(&geneve_link_ops);
1630 unregister_pernet_subsys(&geneve_net_ops);
1631}
1632module_exit(geneve_cleanup_module);
1633
1634MODULE_LICENSE("GPL");
1635MODULE_VERSION(GENEVE_NETDEV_VER);
1636MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1637MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1638MODULE_ALIAS_RTNL_LINK("geneve");