blob: 16af1ce99233ae725852c0f41c1a5c58eb5f0a92 [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
Pravin B Shelar371bd102015-08-26 23:46:54 -070046static int geneve_net_id;
47
John W. Linville8ed66f02015-10-26 17:01:44 -040048union geneve_addr {
49 struct sockaddr_in sin;
50 struct sockaddr_in6 sin6;
51 struct sockaddr sa;
52};
53
54static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
55
John W. Linville2d07dc72015-05-13 12:57:30 -040056/* Pseudo network device */
57struct geneve_dev {
58 struct hlist_node hlist; /* vni hash table */
59 struct net *net; /* netns for packet i/o */
60 struct net_device *dev; /* netdev for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040061 struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */
62#if IS_ENABLED(CONFIG_IPV6)
63 struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */
64#endif
John W. Linville8760ce52015-06-01 15:51:34 -040065 u8 vni[3]; /* virtual network ID for tunnel */
66 u8 ttl; /* TTL override */
John W. Linvilled8951122015-06-01 15:51:35 -040067 u8 tos; /* TOS override */
John W. Linville8ed66f02015-10-26 17:01:44 -040068 union geneve_addr remote; /* IP address for link partner */
John W. Linville2d07dc72015-05-13 12:57:30 -040069 struct list_head next; /* geneve's per namespace list */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +010070 __be32 label; /* IPv6 flowlabel override */
Pravin B Shelarcd7918b2015-08-26 23:46:51 -070071 __be16 dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -070072 bool collect_md;
Jesse Gross8e816df2015-08-28 16:54:40 -070073 struct gro_cells gro_cells;
Tom Herbertabe492b2015-12-10 12:37:45 -080074 u32 flags;
Paolo Abeni468dfff2016-02-12 15:43:58 +010075 struct dst_cache dst_cache;
John W. Linville2d07dc72015-05-13 12:57:30 -040076};
77
Tom Herbertabe492b2015-12-10 12:37:45 -080078/* Geneve device flags */
Alexander Duyck14f1f722016-02-19 11:26:24 -080079#define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
Tom Herbertabe492b2015-12-10 12:37:45 -080080#define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
81#define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
82
Pravin B Shelar371bd102015-08-26 23:46:54 -070083struct geneve_sock {
84 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070085 struct list_head list;
86 struct socket *sock;
87 struct rcu_head rcu;
88 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070089 struct hlist_head vni_list[VNI_HASH_SIZE];
Tom Herbertabe492b2015-12-10 12:37:45 -080090 u32 flags;
Pravin B Shelar371bd102015-08-26 23:46:54 -070091};
John W. Linville2d07dc72015-05-13 12:57:30 -040092
93static inline __u32 geneve_net_vni_hash(u8 vni[3])
94{
95 __u32 vnid;
96
97 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
98 return hash_32(vnid, VNI_HASH_BITS);
99}
100
Pravin B Shelare305ac62015-08-26 23:46:52 -0700101static __be64 vni_to_tunnel_id(const __u8 *vni)
102{
103#ifdef __BIG_ENDIAN
104 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
105#else
106 return (__force __be64)(((__force u64)vni[0] << 40) |
107 ((__force u64)vni[1] << 48) |
108 ((__force u64)vni[2] << 56));
109#endif
110}
111
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100112static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
113{
114 return gs->sock->sk->sk_family;
115}
116
Pravin B Shelar66d47002015-08-26 23:46:55 -0700117static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700118 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400119{
John W. Linville2d07dc72015-05-13 12:57:30 -0400120 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700121 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400122 __u32 hash;
123
John W. Linville2d07dc72015-05-13 12:57:30 -0400124 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700125 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700126 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700127 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700128 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400129 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700130 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700132 return NULL;
133}
134
John W. Linville8ed66f02015-10-26 17:01:44 -0400135#if IS_ENABLED(CONFIG_IPV6)
136static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
137 struct in6_addr addr6, u8 vni[])
138{
139 struct hlist_head *vni_list_head;
140 struct geneve_dev *geneve;
141 __u32 hash;
142
143 /* Find the device for this VNI */
144 hash = geneve_net_vni_hash(vni);
145 vni_list_head = &gs->vni_list[hash];
146 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
147 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
148 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
149 return geneve;
150 }
151 return NULL;
152}
153#endif
154
Pravin B Shelar371bd102015-08-26 23:46:54 -0700155static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
156{
157 return (struct genevehdr *)(udp_hdr(skb) + 1);
158}
159
Jiri Benc9fc47542016-02-18 11:22:50 +0100160static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
161 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700162{
Jiri Benc9fc47542016-02-18 11:22:50 +0100163 u8 *vni;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700164 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400165 static u8 zero_vni[3];
John W. Linville8ed66f02015-10-26 17:01:44 -0400166#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400167 static struct in6_addr zero_addr6;
168#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700169
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100170 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100171 struct iphdr *iph;
172
John W. Linville8ed66f02015-10-26 17:01:44 -0400173 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700174
John W. Linville8ed66f02015-10-26 17:01:44 -0400175 if (gs->collect_md) {
176 vni = zero_vni;
177 addr = 0;
178 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100179 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400180 addr = iph->saddr;
181 }
182
Jiri Benc9fc47542016-02-18 11:22:50 +0100183 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400184#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100185 } else if (geneve_get_sk_family(gs) == AF_INET6) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100186 struct ipv6hdr *ip6h;
187 struct in6_addr addr6;
188
John W. Linville8ed66f02015-10-26 17:01:44 -0400189 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
190
191 if (gs->collect_md) {
192 vni = zero_vni;
193 addr6 = zero_addr6;
194 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100195 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400196 addr6 = ip6h->saddr;
197 }
198
Jiri Benc9fc47542016-02-18 11:22:50 +0100199 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400200#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700201 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100202 return NULL;
203}
204
205/* geneve receive/decap routine */
206static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
207 struct sk_buff *skb)
208{
209 struct genevehdr *gnvh = geneve_hdr(skb);
210 struct metadata_dst *tun_dst = NULL;
211 struct pcpu_sw_netstats *stats;
212 int err = 0;
213 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400214
Pravin B Shelar371bd102015-08-26 23:46:54 -0700215 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700216 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700217
218 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
219 (gnvh->oam ? TUNNEL_OAM : 0) |
220 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
221
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100222 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700223 vni_to_tunnel_id(gnvh->vni),
224 gnvh->opt_len * 4);
225 if (!tun_dst)
226 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700227 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700228 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
229 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700230 } else {
231 /* Drop packets w/ critical options,
232 * since we don't support any...
233 */
234 if (gnvh->critical)
235 goto drop;
236 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400237
238 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400239 skb->protocol = eth_type_trans(skb, geneve->dev);
240 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
241
Pravin B Shelare305ac62015-08-26 23:46:52 -0700242 if (tun_dst)
243 skb_dst_set(skb, &tun_dst->dst);
244
John W. Linville2d07dc72015-05-13 12:57:30 -0400245 /* Ignore packet loops (and multicast echo) */
246 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
247 goto drop;
248
Jiri Benc9fc47542016-02-18 11:22:50 +0100249 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400250 skb_reset_network_header(skb);
251
Jiri Benc9fc47542016-02-18 11:22:50 +0100252 if (geneve_get_sk_family(gs) == AF_INET)
253 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400254#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100255 else
256 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400257#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400258
259 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400260 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100261 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400262 net_info_ratelimited("non-ECT from %pI4 "
263 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100264 &((struct iphdr *)oiph)->saddr,
265 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400266#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100267 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400268 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400270#endif
271 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400272 if (err > 1) {
273 ++geneve->dev->stats.rx_frame_errors;
274 ++geneve->dev->stats.rx_errors;
275 goto drop;
276 }
277 }
278
279 stats = this_cpu_ptr(geneve->dev->tstats);
280 u64_stats_update_begin(&stats->syncp);
281 stats->rx_packets++;
282 stats->rx_bytes += skb->len;
283 u64_stats_update_end(&stats->syncp);
284
Jesse Gross8e816df2015-08-28 16:54:40 -0700285 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400286 return;
287drop:
288 /* Consume bad packet */
289 kfree_skb(skb);
290}
291
292/* Setup stats when device is created */
293static int geneve_init(struct net_device *dev)
294{
Jesse Gross8e816df2015-08-28 16:54:40 -0700295 struct geneve_dev *geneve = netdev_priv(dev);
296 int err;
297
John W. Linville2d07dc72015-05-13 12:57:30 -0400298 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
299 if (!dev->tstats)
300 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700301
302 err = gro_cells_init(&geneve->gro_cells, dev);
303 if (err) {
304 free_percpu(dev->tstats);
305 return err;
306 }
307
Paolo Abeni468dfff2016-02-12 15:43:58 +0100308 err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL);
309 if (err) {
310 free_percpu(dev->tstats);
311 gro_cells_destroy(&geneve->gro_cells);
312 return err;
313 }
314
John W. Linville2d07dc72015-05-13 12:57:30 -0400315 return 0;
316}
317
318static void geneve_uninit(struct net_device *dev)
319{
Jesse Gross8e816df2015-08-28 16:54:40 -0700320 struct geneve_dev *geneve = netdev_priv(dev);
321
Paolo Abeni468dfff2016-02-12 15:43:58 +0100322 dst_cache_destroy(&geneve->dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700323 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400324 free_percpu(dev->tstats);
325}
326
Pravin B Shelar371bd102015-08-26 23:46:54 -0700327/* Callback from net/ipv4/udp.c to receive packets */
328static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
329{
330 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100331 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700332 struct geneve_sock *gs;
333 int opts_len;
334
335 /* Need Geneve and inner Ethernet header to be present */
336 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200337 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700338
339 /* Return packets with reserved bits set */
340 geneveh = geneve_hdr(skb);
341 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200342 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700343
344 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200345 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700346
Jiri Benc9fc47542016-02-18 11:22:50 +0100347 gs = rcu_dereference_sk_user_data(sk);
348 if (!gs)
349 goto drop;
350
351 geneve = geneve_lookup_skb(gs, skb);
352 if (!geneve)
353 goto drop;
354
Pravin B Shelar371bd102015-08-26 23:46:54 -0700355 opts_len = geneveh->opt_len * 4;
356 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100357 htons(ETH_P_TEB),
358 !net_eq(geneve->net, dev_net(geneve->dev))))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700359 goto drop;
360
Jiri Benc9fc47542016-02-18 11:22:50 +0100361 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700362 return 0;
363
364drop:
365 /* Consume bad packet */
366 kfree_skb(skb);
367 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700368}
369
370static struct socket *geneve_create_sock(struct net *net, bool ipv6,
Tom Herbertabe492b2015-12-10 12:37:45 -0800371 __be16 port, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700372{
373 struct socket *sock;
374 struct udp_port_cfg udp_conf;
375 int err;
376
377 memset(&udp_conf, 0, sizeof(udp_conf));
378
379 if (ipv6) {
380 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400381 udp_conf.ipv6_v6only = 1;
Tom Herbertabe492b2015-12-10 12:37:45 -0800382 udp_conf.use_udp6_rx_checksums =
383 !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384 } else {
385 udp_conf.family = AF_INET;
386 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
387 }
388
389 udp_conf.local_udp_port = port;
390
391 /* Open UDP socket */
392 err = udp_sock_create(net, &udp_conf, &sock);
393 if (err < 0)
394 return ERR_PTR(err);
395
396 return sock;
397}
398
Pravin B Shelar371bd102015-08-26 23:46:54 -0700399static int geneve_hlen(struct genevehdr *gh)
400{
401 return sizeof(*gh) + gh->opt_len * 4;
402}
403
Tom Herbert4a0090a2016-04-05 08:22:55 -0700404static struct sk_buff **geneve_gro_receive(struct sock *sk,
405 struct sk_buff **head,
406 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700407{
408 struct sk_buff *p, **pp = NULL;
409 struct genevehdr *gh, *gh2;
410 unsigned int hlen, gh_len, off_gnv;
411 const struct packet_offload *ptype;
412 __be16 type;
413 int flush = 1;
414
415 off_gnv = skb_gro_offset(skb);
416 hlen = off_gnv + sizeof(*gh);
417 gh = skb_gro_header_fast(skb, off_gnv);
418 if (skb_gro_header_hard(skb, hlen)) {
419 gh = skb_gro_header_slow(skb, hlen, off_gnv);
420 if (unlikely(!gh))
421 goto out;
422 }
423
424 if (gh->ver != GENEVE_VER || gh->oam)
425 goto out;
426 gh_len = geneve_hlen(gh);
427
428 hlen = off_gnv + gh_len;
429 if (skb_gro_header_hard(skb, hlen)) {
430 gh = skb_gro_header_slow(skb, hlen, off_gnv);
431 if (unlikely(!gh))
432 goto out;
433 }
434
Pravin B Shelar371bd102015-08-26 23:46:54 -0700435 for (p = *head; p; p = p->next) {
436 if (!NAPI_GRO_CB(p)->same_flow)
437 continue;
438
439 gh2 = (struct genevehdr *)(p->data + off_gnv);
440 if (gh->opt_len != gh2->opt_len ||
441 memcmp(gh, gh2, gh_len)) {
442 NAPI_GRO_CB(p)->same_flow = 0;
443 continue;
444 }
445 }
446
447 type = gh->proto_type;
448
449 rcu_read_lock();
450 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800451 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700452 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700453
454 skb_gro_pull(skb, gh_len);
455 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200456 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800457 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700458
459out_unlock:
460 rcu_read_unlock();
461out:
462 NAPI_GRO_CB(skb)->flush |= flush;
463
464 return pp;
465}
466
Tom Herbert4a0090a2016-04-05 08:22:55 -0700467static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
468 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700469{
470 struct genevehdr *gh;
471 struct packet_offload *ptype;
472 __be16 type;
473 int gh_len;
474 int err = -ENOSYS;
475
Pravin B Shelar371bd102015-08-26 23:46:54 -0700476 gh = (struct genevehdr *)(skb->data + nhoff);
477 gh_len = geneve_hlen(gh);
478 type = gh->proto_type;
479
480 rcu_read_lock();
481 ptype = gro_find_complete_by_type(type);
482 if (ptype)
483 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
484
485 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700486
487 skb_set_inner_mac_header(skb, nhoff + gh_len);
488
Pravin B Shelar371bd102015-08-26 23:46:54 -0700489 return err;
490}
491
492/* Create new listen socket if needed */
493static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
Tom Herbertabe492b2015-12-10 12:37:45 -0800494 bool ipv6, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700495{
496 struct geneve_net *gn = net_generic(net, geneve_net_id);
497 struct geneve_sock *gs;
498 struct socket *sock;
499 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700500 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700501
502 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
503 if (!gs)
504 return ERR_PTR(-ENOMEM);
505
Tom Herbertabe492b2015-12-10 12:37:45 -0800506 sock = geneve_create_sock(net, ipv6, port, flags);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700507 if (IS_ERR(sock)) {
508 kfree(gs);
509 return ERR_CAST(sock);
510 }
511
512 gs->sock = sock;
513 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700514 for (h = 0; h < VNI_HASH_SIZE; ++h)
515 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700516
517 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700518 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700519
520 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700521 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700522 tunnel_cfg.sk_user_data = gs;
523 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700524 tunnel_cfg.gro_receive = geneve_gro_receive;
525 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700526 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
527 tunnel_cfg.encap_destroy = NULL;
528 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700529 list_add(&gs->list, &gn->sock_list);
530 return gs;
531}
532
John W. Linville8ed66f02015-10-26 17:01:44 -0400533static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534{
John W. Linville8ed66f02015-10-26 17:01:44 -0400535 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700536 return;
537
538 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700539 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700540 udp_tunnel_sock_release(gs->sock);
541 kfree_rcu(gs, rcu);
542}
543
John W. Linville8ed66f02015-10-26 17:01:44 -0400544static void geneve_sock_release(struct geneve_dev *geneve)
545{
546 __geneve_sock_release(geneve->sock4);
547#if IS_ENABLED(CONFIG_IPV6)
548 __geneve_sock_release(geneve->sock6);
549#endif
550}
551
Pravin B Shelar371bd102015-08-26 23:46:54 -0700552static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400553 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700554 __be16 dst_port)
555{
556 struct geneve_sock *gs;
557
558 list_for_each_entry(gs, &gn->sock_list, list) {
559 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100560 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700561 return gs;
562 }
563 }
564 return NULL;
565}
566
John W. Linville8ed66f02015-10-26 17:01:44 -0400567static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400568{
John W. Linville2d07dc72015-05-13 12:57:30 -0400569 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700570 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400571 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700572 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400573
John W. Linville8ed66f02015-10-26 17:01:44 -0400574 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700575 if (gs) {
576 gs->refcnt++;
577 goto out;
578 }
579
Tom Herbertabe492b2015-12-10 12:37:45 -0800580 gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
John W. Linville2d07dc72015-05-13 12:57:30 -0400581 if (IS_ERR(gs))
582 return PTR_ERR(gs);
583
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584out:
585 gs->collect_md = geneve->collect_md;
Tom Herbertabe492b2015-12-10 12:37:45 -0800586 gs->flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400587#if IS_ENABLED(CONFIG_IPV6)
588 if (ipv6)
589 geneve->sock6 = gs;
590 else
591#endif
592 geneve->sock4 = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700593
594 hash = geneve_net_vni_hash(geneve->vni);
595 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400596 return 0;
597}
598
John W. Linville8ed66f02015-10-26 17:01:44 -0400599static int geneve_open(struct net_device *dev)
600{
601 struct geneve_dev *geneve = netdev_priv(dev);
602 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
603 bool metadata = geneve->collect_md;
604 int ret = 0;
605
606 geneve->sock4 = NULL;
607#if IS_ENABLED(CONFIG_IPV6)
608 geneve->sock6 = NULL;
609 if (ipv6 || metadata)
610 ret = geneve_sock_add(geneve, true);
611#endif
612 if (!ret && (!ipv6 || metadata))
613 ret = geneve_sock_add(geneve, false);
614 if (ret < 0)
615 geneve_sock_release(geneve);
616
617 return ret;
618}
619
John W. Linville2d07dc72015-05-13 12:57:30 -0400620static int geneve_stop(struct net_device *dev)
621{
622 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400623
Pravin B Shelar66d47002015-08-26 23:46:55 -0700624 if (!hlist_unhashed(&geneve->hlist))
625 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400626 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400627 return 0;
628}
629
John W. Linville8ed66f02015-10-26 17:01:44 -0400630static void geneve_build_header(struct genevehdr *geneveh,
631 __be16 tun_flags, u8 vni[3],
632 u8 options_len, u8 *options)
633{
634 geneveh->ver = GENEVE_VER;
635 geneveh->opt_len = options_len / 4;
636 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
637 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
638 geneveh->rsvd1 = 0;
639 memcpy(geneveh->vni, vni, 3);
640 geneveh->proto_type = htons(ETH_P_TEB);
641 geneveh->rsvd2 = 0;
642
643 memcpy(geneveh->options, options, options_len);
644}
645
Pravin B Shelar371bd102015-08-26 23:46:54 -0700646static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
647 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800648 u32 flags, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700649{
650 struct genevehdr *gnvh;
651 int min_headroom;
652 int err;
Alexander Duyck14f1f722016-02-19 11:26:24 -0800653 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700654
John W. Linville8ed66f02015-10-26 17:01:44 -0400655 skb_scrub_packet(skb, xnet);
656
Pravin B Shelar371bd102015-08-26 23:46:54 -0700657 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
658 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
659 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400660 if (unlikely(err))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700661 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700662
Alexander Duyckaed069d2016-04-14 15:33:37 -0400663 err = udp_tunnel_handle_offloads(skb, udp_sum);
664 if (err)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700665 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700666
667 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400668 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700669
670 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
671 return 0;
672
673free_rt:
674 ip_rt_put(rt);
675 return err;
676}
677
John W. Linville8ed66f02015-10-26 17:01:44 -0400678#if IS_ENABLED(CONFIG_IPV6)
679static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
680 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800681 u32 flags, bool xnet)
John W. Linville8ed66f02015-10-26 17:01:44 -0400682{
683 struct genevehdr *gnvh;
684 int min_headroom;
685 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800686 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
John W. Linville8ed66f02015-10-26 17:01:44 -0400687
688 skb_scrub_packet(skb, xnet);
689
690 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
691 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
692 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400693 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400695
Alexander Duyckaed069d2016-04-14 15:33:37 -0400696 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300697 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400699
700 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
701 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
702
703 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
704 return 0;
705
706free_dst:
707 dst_release(dst);
708 return err;
709}
710#endif
711
712static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
713 struct net_device *dev,
714 struct flowi4 *fl4,
715 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700716{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100717 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700718 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100719 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700720 struct rtable *rt = NULL;
721 __u8 tos;
722
723 memset(fl4, 0, sizeof(*fl4));
724 fl4->flowi4_mark = skb->mark;
725 fl4->flowi4_proto = IPPROTO_UDP;
726
727 if (info) {
728 fl4->daddr = info->key.u.ipv4.dst;
729 fl4->saddr = info->key.u.ipv4.src;
730 fl4->flowi4_tos = RT_TOS(info->key.tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100731 dst_cache = &info->dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700732 } else {
733 tos = geneve->tos;
734 if (tos == 1) {
735 const struct iphdr *iip = ip_hdr(skb);
736
737 tos = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100738 use_cache = false;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700739 }
740
741 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400742 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100743 dst_cache = &geneve->dst_cache;
744 }
745
Paolo Abeni468dfff2016-02-12 15:43:58 +0100746 if (use_cache) {
747 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
748 if (rt)
749 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700750 }
751
752 rt = ip_route_output_key(geneve->net, fl4);
753 if (IS_ERR(rt)) {
754 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700755 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700756 }
757 if (rt->dst.dev == dev) { /* is this necessary? */
758 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700759 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700760 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700761 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100762 if (use_cache)
763 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700764 return rt;
765}
766
John W. Linville8ed66f02015-10-26 17:01:44 -0400767#if IS_ENABLED(CONFIG_IPV6)
768static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
769 struct net_device *dev,
770 struct flowi6 *fl6,
771 struct ip_tunnel_info *info)
772{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100773 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400774 struct geneve_dev *geneve = netdev_priv(dev);
775 struct geneve_sock *gs6 = geneve->sock6;
776 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100777 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400778 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400779
780 memset(fl6, 0, sizeof(*fl6));
781 fl6->flowi6_mark = skb->mark;
782 fl6->flowi6_proto = IPPROTO_UDP;
783
784 if (info) {
785 fl6->daddr = info->key.u.ipv6.dst;
786 fl6->saddr = info->key.u.ipv6.src;
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100787 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(info->key.tos),
788 info->key.label);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100789 dst_cache = &info->dst_cache;
John W. Linville8ed66f02015-10-26 17:01:44 -0400790 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400791 prio = geneve->tos;
792 if (prio == 1) {
793 const struct iphdr *iip = ip_hdr(skb);
794
795 prio = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100796 use_cache = false;
John W. Linville3a56f862015-10-26 17:01:45 -0400797 }
798
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100799 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
800 geneve->label);
John W. Linville8ed66f02015-10-26 17:01:44 -0400801 fl6->daddr = geneve->remote.sin6.sin6_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100802 dst_cache = &geneve->dst_cache;
803 }
804
Paolo Abeni468dfff2016-02-12 15:43:58 +0100805 if (use_cache) {
806 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
807 if (dst)
808 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400809 }
810
811 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
812 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
813 return ERR_PTR(-ENETUNREACH);
814 }
815 if (dst->dev == dev) { /* is this necessary? */
816 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
817 dst_release(dst);
818 return ERR_PTR(-ELOOP);
819 }
820
Paolo Abeni468dfff2016-02-12 15:43:58 +0100821 if (use_cache)
822 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400823 return dst;
824}
825#endif
826
Pravin B Shelare305ac62015-08-26 23:46:52 -0700827/* Convert 64 bit tunnel ID to 24 bit VNI. */
828static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
829{
830#ifdef __BIG_ENDIAN
831 vni[0] = (__force __u8)(tun_id >> 16);
832 vni[1] = (__force __u8)(tun_id >> 8);
833 vni[2] = (__force __u8)tun_id;
834#else
835 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
836 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
837 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
838#endif
839}
840
John W. Linville8ed66f02015-10-26 17:01:44 -0400841static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
842 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400843{
844 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400845 struct geneve_sock *gs4 = geneve->sock4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400846 struct rtable *rt = NULL;
John W. Linville08399ef2015-09-21 10:29:09 -0400847 const struct iphdr *iip; /* interior IP header */
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700848 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400849 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400850 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700851 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700852 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400853 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800854 u32 flags = geneve->flags;
John W. Linville2d07dc72015-05-13 12:57:30 -0400855
Pravin B Shelare305ac62015-08-26 23:46:52 -0700856 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400857 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700858 netdev_dbg(dev, "no tunnel metadata\n");
859 goto tx_error;
860 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200861 if (info && ip_tunnel_info_af(info) != AF_INET)
862 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700863 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400864
John W. Linville8ed66f02015-10-26 17:01:44 -0400865 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400866 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700867 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400868 goto tx_error;
869 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700870
871 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700872 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700873
John W. Linville08399ef2015-09-21 10:29:09 -0400874 iip = ip_hdr(skb);
875
Pravin B Shelare305ac62015-08-26 23:46:52 -0700876 if (info) {
877 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700878 u8 *opts = NULL;
879 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700880
881 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +0100882 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700883 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700884
Tom Herbertabe492b2015-12-10 12:37:45 -0800885 if (key->tun_flags & TUNNEL_CSUM)
Alexander Duyck14f1f722016-02-19 11:26:24 -0800886 flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800887 else
Alexander Duyck14f1f722016-02-19 11:26:24 -0800888 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800889
Pravin B Shelar371bd102015-08-26 23:46:54 -0700890 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800891 info->options_len, opts, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700892 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400893 goto tx_error;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700894
John W. Linville08399ef2015-09-21 10:29:09 -0400895 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700896 ttl = key->ttl;
897 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700898 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700899 err = geneve_build_skb(rt, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800900 0, NULL, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700901 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400902 goto tx_error;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700903
Pravin B Shelare305ac62015-08-26 23:46:52 -0700904 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700905 ttl = geneve->ttl;
906 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
907 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700908 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700909 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400910 }
Pravin B Shelar039f5062015-12-24 14:34:54 -0800911 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
912 tos, ttl, df, sport, geneve->dst_port,
913 !net_eq(geneve->net, dev_net(geneve->dev)),
Alexander Duyck14f1f722016-02-19 11:26:24 -0800914 !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX));
John W. Linville2d07dc72015-05-13 12:57:30 -0400915
John W. Linville2d07dc72015-05-13 12:57:30 -0400916 return NETDEV_TX_OK;
917
John W. Linville2d07dc72015-05-13 12:57:30 -0400918tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400919 dev_kfree_skb(skb);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400920
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700921 if (err == -ELOOP)
922 dev->stats.collisions++;
923 else if (err == -ENETUNREACH)
924 dev->stats.tx_carrier_errors++;
Haishuang Yanefeb2262016-06-21 16:26:49 +0800925
926 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400927 return NETDEV_TX_OK;
928}
929
John W. Linville8ed66f02015-10-26 17:01:44 -0400930#if IS_ENABLED(CONFIG_IPV6)
931static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
932 struct ip_tunnel_info *info)
933{
934 struct geneve_dev *geneve = netdev_priv(dev);
935 struct geneve_sock *gs6 = geneve->sock6;
936 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400937 const struct iphdr *iip; /* interior IP header */
John W. Linville8ed66f02015-10-26 17:01:44 -0400938 int err = -EINVAL;
939 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400940 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400941 __be16 sport;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100942 __be32 label;
John W. Linville8ed66f02015-10-26 17:01:44 -0400943 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800944 u32 flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400945
946 if (geneve->collect_md) {
947 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
948 netdev_dbg(dev, "no tunnel metadata\n");
949 goto tx_error;
950 }
951 }
952
953 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
954 if (IS_ERR(dst)) {
955 err = PTR_ERR(dst);
956 goto tx_error;
957 }
958
959 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
960 skb_reset_mac_header(skb);
961
John W. Linville3a56f862015-10-26 17:01:45 -0400962 iip = ip_hdr(skb);
963
John W. Linville8ed66f02015-10-26 17:01:44 -0400964 if (info) {
965 const struct ip_tunnel_key *key = &info->key;
966 u8 *opts = NULL;
967 u8 vni[3];
968
969 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +0100970 if (info->options_len)
John W. Linville8ed66f02015-10-26 17:01:44 -0400971 opts = ip_tunnel_info_opts(info);
972
Tom Herbertabe492b2015-12-10 12:37:45 -0800973 if (key->tun_flags & TUNNEL_CSUM)
Jesse Gross35e2d112016-01-20 16:22:47 -0800974 flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800975 else
Jesse Gross35e2d112016-01-20 16:22:47 -0800976 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800977
John W. Linville8ed66f02015-10-26 17:01:44 -0400978 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
979 info->options_len, opts,
Tom Herbertabe492b2015-12-10 12:37:45 -0800980 flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -0400981 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400982 goto tx_error;
John W. Linville8ed66f02015-10-26 17:01:44 -0400983
John W. Linville3a56f862015-10-26 17:01:45 -0400984 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400985 ttl = key->ttl;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100986 label = info->key.label;
John W. Linville8ed66f02015-10-26 17:01:44 -0400987 } else {
John W. Linville8ed66f02015-10-26 17:01:44 -0400988 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800989 0, NULL, flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -0400990 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400991 goto tx_error;
John W. Linville8ed66f02015-10-26 17:01:44 -0400992
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100993 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
994 iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400995 ttl = geneve->ttl;
996 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
997 ttl = 1;
998 ttl = ttl ? : ip6_dst_hoplimit(dst);
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100999 label = geneve->label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001000 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001001
Pravin B Shelar039f5062015-12-24 14:34:54 -08001002 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001003 &fl6.saddr, &fl6.daddr, prio, ttl, label,
Pravin B Shelar039f5062015-12-24 14:34:54 -08001004 sport, geneve->dst_port,
1005 !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
John W. Linville8ed66f02015-10-26 17:01:44 -04001006 return NETDEV_TX_OK;
1007
1008tx_error:
1009 dev_kfree_skb(skb);
Alexander Duyckaed069d2016-04-14 15:33:37 -04001010
John W. Linville8ed66f02015-10-26 17:01:44 -04001011 if (err == -ELOOP)
1012 dev->stats.collisions++;
1013 else if (err == -ENETUNREACH)
1014 dev->stats.tx_carrier_errors++;
Haishuang Yanefeb2262016-06-21 16:26:49 +08001015
1016 dev->stats.tx_errors++;
John W. Linville8ed66f02015-10-26 17:01:44 -04001017 return NETDEV_TX_OK;
1018}
1019#endif
1020
1021static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1022{
1023 struct geneve_dev *geneve = netdev_priv(dev);
1024 struct ip_tunnel_info *info = NULL;
1025
1026 if (geneve->collect_md)
1027 info = skb_tunnel_info(skb);
1028
1029#if IS_ENABLED(CONFIG_IPV6)
1030 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
1031 (!info && geneve->remote.sa.sa_family == AF_INET6))
1032 return geneve6_xmit_skb(skb, dev, info);
1033#endif
1034 return geneve_xmit_skb(skb, dev, info);
1035}
1036
David Wraggaeee0e62016-02-18 17:43:29 +00001037static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict)
David Wragg55e5bfb2016-02-10 00:05:57 +00001038{
Haishuang Yand5d5e8d2016-07-02 15:02:48 +08001039 struct geneve_dev *geneve = netdev_priv(dev);
David Wraggaeee0e62016-02-18 17:43:29 +00001040 /* The max_mtu calculation does not take account of GENEVE
1041 * options, to avoid excluding potentially valid
1042 * configurations.
David Wragg55e5bfb2016-02-10 00:05:57 +00001043 */
Haishuang Yand5d5e8d2016-07-02 15:02:48 +08001044 int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1045
1046 if (geneve->remote.sa.sa_family == AF_INET6)
1047 max_mtu -= sizeof(struct ipv6hdr);
1048 else
1049 max_mtu -= sizeof(struct iphdr);
David Wraggaeee0e62016-02-18 17:43:29 +00001050
1051 if (new_mtu < 68)
David Wragg55e5bfb2016-02-10 00:05:57 +00001052 return -EINVAL;
David Wraggaeee0e62016-02-18 17:43:29 +00001053
1054 if (new_mtu > max_mtu) {
1055 if (strict)
1056 return -EINVAL;
1057
1058 new_mtu = max_mtu;
1059 }
1060
David Wragg55e5bfb2016-02-10 00:05:57 +00001061 dev->mtu = new_mtu;
1062 return 0;
1063}
1064
David Wraggaeee0e62016-02-18 17:43:29 +00001065static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1066{
1067 return __geneve_change_mtu(dev, new_mtu, true);
1068}
1069
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001070static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1071{
1072 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1073 struct geneve_dev *geneve = netdev_priv(dev);
1074 struct rtable *rt;
1075 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001076#if IS_ENABLED(CONFIG_IPV6)
1077 struct dst_entry *dst;
1078 struct flowi6 fl6;
1079#endif
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001080
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001081 if (ip_tunnel_info_af(info) == AF_INET) {
1082 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1083 if (IS_ERR(rt))
1084 return PTR_ERR(rt);
1085
1086 ip_rt_put(rt);
1087 info->key.u.ipv4.src = fl4.saddr;
1088#if IS_ENABLED(CONFIG_IPV6)
1089 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1090 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1091 if (IS_ERR(dst))
1092 return PTR_ERR(dst);
1093
1094 dst_release(dst);
1095 info->key.u.ipv6.src = fl6.saddr;
1096#endif
1097 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001098 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001099 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001100
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001101 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1102 1, USHRT_MAX, true);
1103 info->key.tp_dst = geneve->dst_port;
1104 return 0;
1105}
1106
John W. Linville2d07dc72015-05-13 12:57:30 -04001107static const struct net_device_ops geneve_netdev_ops = {
1108 .ndo_init = geneve_init,
1109 .ndo_uninit = geneve_uninit,
1110 .ndo_open = geneve_open,
1111 .ndo_stop = geneve_stop,
1112 .ndo_start_xmit = geneve_xmit,
1113 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001114 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001115 .ndo_validate_addr = eth_validate_addr,
1116 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001117 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001118};
1119
1120static void geneve_get_drvinfo(struct net_device *dev,
1121 struct ethtool_drvinfo *drvinfo)
1122{
1123 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1124 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1125}
1126
1127static const struct ethtool_ops geneve_ethtool_ops = {
1128 .get_drvinfo = geneve_get_drvinfo,
1129 .get_link = ethtool_op_get_link,
1130};
1131
1132/* Info for udev, that this is a virtual tunnel endpoint */
1133static struct device_type geneve_type = {
1134 .name = "geneve",
1135};
1136
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001137/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001138 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001139 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001140 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001141static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001142{
1143 struct net *net = dev_net(dev);
1144 struct geneve_net *gn = net_generic(net, geneve_net_id);
1145 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001146
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001147 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -07001148 list_for_each_entry_rcu(gs, &gn->sock_list, list)
1149 udp_tunnel_push_rx_port(dev, gs->sock,
1150 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001151 rcu_read_unlock();
1152}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001153
John W. Linville2d07dc72015-05-13 12:57:30 -04001154/* Initialize the device structure. */
1155static void geneve_setup(struct net_device *dev)
1156{
1157 ether_setup(dev);
1158
1159 dev->netdev_ops = &geneve_netdev_ops;
1160 dev->ethtool_ops = &geneve_ethtool_ops;
1161 dev->destructor = free_netdev;
1162
1163 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1164
John W. Linville2d07dc72015-05-13 12:57:30 -04001165 dev->features |= NETIF_F_LLTX;
1166 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1167 dev->features |= NETIF_F_RXCSUM;
1168 dev->features |= NETIF_F_GSO_SOFTWARE;
1169
John W. Linville2d07dc72015-05-13 12:57:30 -04001170 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1171 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001172
1173 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001174 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001175 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001176 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001177}
1178
1179static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1180 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1181 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001182 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001183 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001184 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001185 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001186 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001187 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001188 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1189 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1190 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001191};
1192
1193static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1194{
1195 if (tb[IFLA_ADDRESS]) {
1196 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1197 return -EINVAL;
1198
1199 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1200 return -EADDRNOTAVAIL;
1201 }
1202
1203 if (!data)
1204 return -EINVAL;
1205
1206 if (data[IFLA_GENEVE_ID]) {
1207 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1208
1209 if (vni >= GENEVE_VID_MASK)
1210 return -ERANGE;
1211 }
1212
1213 return 0;
1214}
1215
Pravin B Shelar371bd102015-08-26 23:46:54 -07001216static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1217 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001218 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001219 u8 vni[],
1220 bool *tun_on_same_port,
1221 bool *tun_collect_md)
1222{
1223 struct geneve_dev *geneve, *t;
1224
1225 *tun_on_same_port = false;
1226 *tun_collect_md = false;
1227 t = NULL;
1228 list_for_each_entry(geneve, &gn->geneve_list, next) {
1229 if (geneve->dst_port == dst_port) {
1230 *tun_collect_md = geneve->collect_md;
1231 *tun_on_same_port = true;
1232 }
1233 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001234 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001235 dst_port == geneve->dst_port)
1236 t = geneve;
1237 }
1238 return t;
1239}
1240
Pravin B Shelare305ac62015-08-26 23:46:52 -07001241static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001242 union geneve_addr *remote,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001243 __u32 vni, __u8 ttl, __u8 tos, __be32 label,
1244 __be16 dst_port, bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001245{
1246 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001247 struct geneve_dev *t, *geneve = netdev_priv(dev);
1248 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001249 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001250
John W. Linville8ed66f02015-10-26 17:01:44 -04001251 if (!remote)
1252 return -EINVAL;
1253 if (metadata &&
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001254 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl || label))
John W. Linville8ed66f02015-10-26 17:01:44 -04001255 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001256
1257 geneve->net = net;
1258 geneve->dev = dev;
1259
John W. Linville2d07dc72015-05-13 12:57:30 -04001260 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1261 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1262 geneve->vni[2] = vni & 0x000000ff;
1263
John W. Linville8ed66f02015-10-26 17:01:44 -04001264 if ((remote->sa.sa_family == AF_INET &&
1265 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1266 (remote->sa.sa_family == AF_INET6 &&
1267 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001268 return -EINVAL;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001269 if (label && remote->sa.sa_family != AF_INET6)
1270 return -EINVAL;
1271
John W. Linville8ed66f02015-10-26 17:01:44 -04001272 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001273
Pravin B Shelare305ac62015-08-26 23:46:52 -07001274 geneve->ttl = ttl;
1275 geneve->tos = tos;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001276 geneve->label = label;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001277 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001278 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001279 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001280
John W. Linville8ed66f02015-10-26 17:01:44 -04001281 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001282 &tun_on_same_port, &tun_collect_md);
1283 if (t)
1284 return -EBUSY;
1285
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001286 /* make enough headroom for basic scenario */
1287 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1288 if (remote->sa.sa_family == AF_INET)
1289 encap_len += sizeof(struct iphdr);
1290 else
1291 encap_len += sizeof(struct ipv6hdr);
1292 dev->needed_headroom = encap_len + ETH_HLEN;
1293
Pravin B Shelar371bd102015-08-26 23:46:54 -07001294 if (metadata) {
1295 if (tun_on_same_port)
1296 return -EPERM;
1297 } else {
1298 if (tun_collect_md)
1299 return -EPERM;
1300 }
1301
Paolo Abeni468dfff2016-02-12 15:43:58 +01001302 dst_cache_reset(&geneve->dst_cache);
1303
John W. Linville2d07dc72015-05-13 12:57:30 -04001304 err = register_netdevice(dev);
1305 if (err)
1306 return err;
1307
1308 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001309 return 0;
1310}
1311
Pravin B Shelare305ac62015-08-26 23:46:52 -07001312static int geneve_newlink(struct net *net, struct net_device *dev,
1313 struct nlattr *tb[], struct nlattr *data[])
1314{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001315 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001316 __u8 ttl = 0, tos = 0;
1317 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001318 union geneve_addr remote = geneve_remote_unspec;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001319 __be32 label = 0;
Jesse Grosse277de52015-10-16 16:36:00 -07001320 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001321 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001322
John W. Linville8ed66f02015-10-26 17:01:44 -04001323 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1324 return -EINVAL;
1325
1326 if (data[IFLA_GENEVE_REMOTE]) {
1327 remote.sa.sa_family = AF_INET;
1328 remote.sin.sin_addr.s_addr =
1329 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1330 }
1331
1332 if (data[IFLA_GENEVE_REMOTE6]) {
1333 if (!IS_ENABLED(CONFIG_IPV6))
1334 return -EPFNOSUPPORT;
1335
1336 remote.sa.sa_family = AF_INET6;
1337 remote.sin6.sin6_addr =
1338 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1339
1340 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1341 IPV6_ADDR_LINKLOCAL) {
1342 netdev_dbg(dev, "link-local remote is unsupported\n");
1343 return -EINVAL;
1344 }
1345 }
1346
Jesse Grosse277de52015-10-16 16:36:00 -07001347 if (data[IFLA_GENEVE_ID])
1348 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001349
Pravin B Shelare305ac62015-08-26 23:46:52 -07001350 if (data[IFLA_GENEVE_TTL])
1351 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1352
1353 if (data[IFLA_GENEVE_TOS])
1354 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1355
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001356 if (data[IFLA_GENEVE_LABEL])
1357 label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1358 IPV6_FLOWLABEL_MASK;
1359
Pravin B Shelare305ac62015-08-26 23:46:52 -07001360 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001361 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001362
1363 if (data[IFLA_GENEVE_COLLECT_METADATA])
1364 metadata = true;
1365
Tom Herbertabe492b2015-12-10 12:37:45 -08001366 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001367 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1368 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001369
1370 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1371 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1372 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1373
1374 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1375 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1376 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1377
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001378 return geneve_configure(net, dev, &remote, vni, ttl, tos, label,
1379 dst_port, metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001380}
1381
John W. Linville2d07dc72015-05-13 12:57:30 -04001382static void geneve_dellink(struct net_device *dev, struct list_head *head)
1383{
1384 struct geneve_dev *geneve = netdev_priv(dev);
1385
John W. Linville2d07dc72015-05-13 12:57:30 -04001386 list_del(&geneve->next);
1387 unregister_netdevice_queue(dev, head);
1388}
1389
1390static size_t geneve_get_size(const struct net_device *dev)
1391{
1392 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001393 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001394 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001395 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001396 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001397 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001398 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001399 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1400 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1401 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001402 0;
1403}
1404
1405static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1406{
1407 struct geneve_dev *geneve = netdev_priv(dev);
1408 __u32 vni;
1409
1410 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1411 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1412 goto nla_put_failure;
1413
John W. Linville8ed66f02015-10-26 17:01:44 -04001414 if (geneve->remote.sa.sa_family == AF_INET) {
1415 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1416 geneve->remote.sin.sin_addr.s_addr))
1417 goto nla_put_failure;
1418#if IS_ENABLED(CONFIG_IPV6)
1419 } else {
1420 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1421 &geneve->remote.sin6.sin6_addr))
1422 goto nla_put_failure;
1423#endif
1424 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001425
John W. Linvilled8951122015-06-01 15:51:35 -04001426 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001427 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos) ||
1428 nla_put_be32(skb, IFLA_GENEVE_LABEL, geneve->label))
John W. Linville8760ce52015-06-01 15:51:34 -04001429 goto nla_put_failure;
1430
John W. Linville7bbe33f2015-09-22 13:09:32 -04001431 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001432 goto nla_put_failure;
1433
Pravin B Shelare305ac62015-08-26 23:46:52 -07001434 if (geneve->collect_md) {
1435 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1436 goto nla_put_failure;
1437 }
1438
Tom Herbertabe492b2015-12-10 12:37:45 -08001439 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
Alexander Duyck14f1f722016-02-19 11:26:24 -08001440 !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbertabe492b2015-12-10 12:37:45 -08001441 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1442 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1443 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1444 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1445 goto nla_put_failure;
1446
John W. Linville2d07dc72015-05-13 12:57:30 -04001447 return 0;
1448
1449nla_put_failure:
1450 return -EMSGSIZE;
1451}
1452
1453static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1454 .kind = "geneve",
1455 .maxtype = IFLA_GENEVE_MAX,
1456 .policy = geneve_policy,
1457 .priv_size = sizeof(struct geneve_dev),
1458 .setup = geneve_setup,
1459 .validate = geneve_validate,
1460 .newlink = geneve_newlink,
1461 .dellink = geneve_dellink,
1462 .get_size = geneve_get_size,
1463 .fill_info = geneve_fill_info,
1464};
1465
Pravin B Shelare305ac62015-08-26 23:46:52 -07001466struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1467 u8 name_assign_type, u16 dst_port)
1468{
1469 struct nlattr *tb[IFLA_MAX + 1];
1470 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001471 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001472 int err;
1473
1474 memset(tb, 0, sizeof(tb));
1475 dev = rtnl_create_link(net, name, name_assign_type,
1476 &geneve_link_ops, tb);
1477 if (IS_ERR(dev))
1478 return dev;
1479
John W. Linville8ed66f02015-10-26 17:01:44 -04001480 err = geneve_configure(net, dev, &geneve_remote_unspec,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001481 0, 0, 0, 0, htons(dst_port), true,
Paolo Abenic868ee72016-02-17 19:30:01 +01001482 GENEVE_F_UDP_ZERO_CSUM6_RX);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001483 if (err) {
1484 free_netdev(dev);
1485 return ERR_PTR(err);
1486 }
David Wragg7e059152016-02-10 00:05:58 +00001487
1488 /* openvswitch users expect packet sizes to be unrestricted,
1489 * so set the largest MTU we can.
1490 */
David Wraggaeee0e62016-02-18 17:43:29 +00001491 err = __geneve_change_mtu(dev, IP_MAX_MTU, false);
David Wragg7e059152016-02-10 00:05:58 +00001492 if (err)
1493 goto err;
1494
Nicolas Dichtel41009482016-06-13 10:31:07 +02001495 err = rtnl_configure_link(dev, NULL);
1496 if (err < 0)
1497 goto err;
1498
Pravin B Shelare305ac62015-08-26 23:46:52 -07001499 return dev;
David Wragg7e059152016-02-10 00:05:58 +00001500
1501 err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001502 geneve_dellink(dev, &list_kill);
1503 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001504 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001505}
1506EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1507
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001508static int geneve_netdevice_event(struct notifier_block *unused,
1509 unsigned long event, void *ptr)
1510{
1511 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1512
Alexander Duyck7c46a642016-06-16 12:21:00 -07001513 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001514 geneve_push_rx_ports(dev);
1515
1516 return NOTIFY_DONE;
1517}
1518
1519static struct notifier_block geneve_notifier_block __read_mostly = {
1520 .notifier_call = geneve_netdevice_event,
1521};
1522
John W. Linville2d07dc72015-05-13 12:57:30 -04001523static __net_init int geneve_init_net(struct net *net)
1524{
1525 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001526
1527 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001528 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001529 return 0;
1530}
1531
1532static void __net_exit geneve_exit_net(struct net *net)
1533{
1534 struct geneve_net *gn = net_generic(net, geneve_net_id);
1535 struct geneve_dev *geneve, *next;
1536 struct net_device *dev, *aux;
1537 LIST_HEAD(list);
1538
1539 rtnl_lock();
1540
1541 /* gather any geneve devices that were moved into this ns */
1542 for_each_netdev_safe(net, dev, aux)
1543 if (dev->rtnl_link_ops == &geneve_link_ops)
1544 unregister_netdevice_queue(dev, &list);
1545
1546 /* now gather any other geneve devices that were created in this ns */
1547 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1548 /* If geneve->dev is in the same netns, it was already added
1549 * to the list by the previous loop.
1550 */
1551 if (!net_eq(dev_net(geneve->dev), net))
1552 unregister_netdevice_queue(geneve->dev, &list);
1553 }
1554
1555 /* unregister the devices gathered above */
1556 unregister_netdevice_many(&list);
1557 rtnl_unlock();
1558}
1559
1560static struct pernet_operations geneve_net_ops = {
1561 .init = geneve_init_net,
1562 .exit = geneve_exit_net,
1563 .id = &geneve_net_id,
1564 .size = sizeof(struct geneve_net),
1565};
1566
1567static int __init geneve_init_module(void)
1568{
1569 int rc;
1570
1571 rc = register_pernet_subsys(&geneve_net_ops);
1572 if (rc)
1573 goto out1;
1574
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001575 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001576 if (rc)
1577 goto out2;
1578
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001579 rc = rtnl_link_register(&geneve_link_ops);
1580 if (rc)
1581 goto out3;
1582
John W. Linville2d07dc72015-05-13 12:57:30 -04001583 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001584
1585out3:
1586 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001587out2:
1588 unregister_pernet_subsys(&geneve_net_ops);
1589out1:
1590 return rc;
1591}
1592late_initcall(geneve_init_module);
1593
1594static void __exit geneve_cleanup_module(void)
1595{
1596 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001597 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001598 unregister_pernet_subsys(&geneve_net_ops);
1599}
1600module_exit(geneve_cleanup_module);
1601
1602MODULE_LICENSE("GPL");
1603MODULE_VERSION(GENEVE_NETDEV_VER);
1604MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1605MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1606MODULE_ALIAS_RTNL_LINK("geneve");