blob: d2031ce8baea3772f43578d7ab8d58e9c57c01aa [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 */
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;
John W. Linville2d07dc72015-05-13 12:57:30 -040075};
76
Tom Herbertabe492b2015-12-10 12:37:45 -080077/* Geneve device flags */
78#define GENEVE_F_UDP_CSUM BIT(0)
79#define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
80#define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
81
Pravin B Shelar371bd102015-08-26 23:46:54 -070082struct geneve_sock {
83 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070084 struct list_head list;
85 struct socket *sock;
86 struct rcu_head rcu;
87 int refcnt;
88 struct udp_offload udp_offloads;
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
Pravin B Shelar66d47002015-08-26 23:46:55 -0700112static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700113 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400114{
John W. Linville2d07dc72015-05-13 12:57:30 -0400115 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700116 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400117 __u32 hash;
118
John W. Linville2d07dc72015-05-13 12:57:30 -0400119 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700120 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700121 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700122 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700123 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400124 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700125 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400126 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700127 return NULL;
128}
129
John W. Linville8ed66f02015-10-26 17:01:44 -0400130#if IS_ENABLED(CONFIG_IPV6)
131static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
132 struct in6_addr addr6, u8 vni[])
133{
134 struct hlist_head *vni_list_head;
135 struct geneve_dev *geneve;
136 __u32 hash;
137
138 /* Find the device for this VNI */
139 hash = geneve_net_vni_hash(vni);
140 vni_list_head = &gs->vni_list[hash];
141 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
142 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
143 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
144 return geneve;
145 }
146 return NULL;
147}
148#endif
149
Pravin B Shelar371bd102015-08-26 23:46:54 -0700150static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
151{
152 return (struct genevehdr *)(udp_hdr(skb) + 1);
153}
154
Pravin B Shelare305ac62015-08-26 23:46:52 -0700155/* geneve receive/decap routine */
156static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
157{
158 struct genevehdr *gnvh = geneve_hdr(skb);
159 struct metadata_dst *tun_dst = NULL;
160 struct geneve_dev *geneve = NULL;
161 struct pcpu_sw_netstats *stats;
John W. Linville8ed66f02015-10-26 17:01:44 -0400162 struct iphdr *iph = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700163 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400164 static u8 zero_vni[3];
165 u8 *vni;
166 int err = 0;
167 sa_family_t sa_family;
168#if IS_ENABLED(CONFIG_IPV6)
169 struct ipv6hdr *ip6h = NULL;
170 struct in6_addr addr6;
171 static struct in6_addr zero_addr6;
172#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700173
John W. Linville8ed66f02015-10-26 17:01:44 -0400174 sa_family = gs->sock->sk->sk_family;
John W. Linville08399ef2015-09-21 10:29:09 -0400175
John W. Linville8ed66f02015-10-26 17:01:44 -0400176 if (sa_family == AF_INET) {
177 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700178
John W. Linville8ed66f02015-10-26 17:01:44 -0400179 if (gs->collect_md) {
180 vni = zero_vni;
181 addr = 0;
182 } else {
183 vni = gnvh->vni;
184
185 addr = iph->saddr;
186 }
187
188 geneve = geneve_lookup(gs, addr, vni);
189#if IS_ENABLED(CONFIG_IPV6)
190 } else if (sa_family == AF_INET6) {
191 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
192
193 if (gs->collect_md) {
194 vni = zero_vni;
195 addr6 = zero_addr6;
196 } else {
197 vni = gnvh->vni;
198
199 addr6 = ip6h->saddr;
200 }
201
202 geneve = geneve6_lookup(gs, addr6, vni);
203#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700204 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400205 if (!geneve)
206 goto drop;
207
Pravin B Shelar371bd102015-08-26 23:46:54 -0700208 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700209 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700210
211 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
212 (gnvh->oam ? TUNNEL_OAM : 0) |
213 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
214
John W. Linville8ed66f02015-10-26 17:01:44 -0400215 tun_dst = udp_tun_rx_dst(skb, sa_family, flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700216 vni_to_tunnel_id(gnvh->vni),
217 gnvh->opt_len * 4);
218 if (!tun_dst)
219 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700220 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700221 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
222 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700223 } else {
224 /* Drop packets w/ critical options,
225 * since we don't support any...
226 */
227 if (gnvh->critical)
228 goto drop;
229 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400230
231 skb_reset_mac_header(skb);
232 skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
233 skb->protocol = eth_type_trans(skb, geneve->dev);
234 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
235
Pravin B Shelare305ac62015-08-26 23:46:52 -0700236 if (tun_dst)
237 skb_dst_set(skb, &tun_dst->dst);
238
John W. Linville2d07dc72015-05-13 12:57:30 -0400239 /* Ignore packet loops (and multicast echo) */
240 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
241 goto drop;
242
243 skb_reset_network_header(skb);
244
John W. Linville8ed66f02015-10-26 17:01:44 -0400245 if (iph)
246 err = IP_ECN_decapsulate(iph, skb);
247#if IS_ENABLED(CONFIG_IPV6)
248 if (ip6h)
249 err = IP6_ECN_decapsulate(ip6h, skb);
250#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400251
252 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400253 if (log_ecn_error) {
254 if (iph)
255 net_info_ratelimited("non-ECT from %pI4 "
256 "with TOS=%#x\n",
257 &iph->saddr, iph->tos);
258#if IS_ENABLED(CONFIG_IPV6)
259 if (ip6h)
260 net_info_ratelimited("non-ECT from %pI6\n",
261 &ip6h->saddr);
262#endif
263 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400264 if (err > 1) {
265 ++geneve->dev->stats.rx_frame_errors;
266 ++geneve->dev->stats.rx_errors;
267 goto drop;
268 }
269 }
270
271 stats = this_cpu_ptr(geneve->dev->tstats);
272 u64_stats_update_begin(&stats->syncp);
273 stats->rx_packets++;
274 stats->rx_bytes += skb->len;
275 u64_stats_update_end(&stats->syncp);
276
Jesse Gross8e816df2015-08-28 16:54:40 -0700277 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400278 return;
279drop:
280 /* Consume bad packet */
281 kfree_skb(skb);
282}
283
284/* Setup stats when device is created */
285static int geneve_init(struct net_device *dev)
286{
Jesse Gross8e816df2015-08-28 16:54:40 -0700287 struct geneve_dev *geneve = netdev_priv(dev);
288 int err;
289
John W. Linville2d07dc72015-05-13 12:57:30 -0400290 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
291 if (!dev->tstats)
292 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700293
294 err = gro_cells_init(&geneve->gro_cells, dev);
295 if (err) {
296 free_percpu(dev->tstats);
297 return err;
298 }
299
John W. Linville2d07dc72015-05-13 12:57:30 -0400300 return 0;
301}
302
303static void geneve_uninit(struct net_device *dev)
304{
Jesse Gross8e816df2015-08-28 16:54:40 -0700305 struct geneve_dev *geneve = netdev_priv(dev);
306
307 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400308 free_percpu(dev->tstats);
309}
310
Pravin B Shelar371bd102015-08-26 23:46:54 -0700311/* Callback from net/ipv4/udp.c to receive packets */
312static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
313{
314 struct genevehdr *geneveh;
315 struct geneve_sock *gs;
316 int opts_len;
317
318 /* Need Geneve and inner Ethernet header to be present */
319 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
320 goto error;
321
322 /* Return packets with reserved bits set */
323 geneveh = geneve_hdr(skb);
324 if (unlikely(geneveh->ver != GENEVE_VER))
325 goto error;
326
327 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
328 goto error;
329
330 opts_len = geneveh->opt_len * 4;
331 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
332 htons(ETH_P_TEB)))
333 goto drop;
334
335 gs = rcu_dereference_sk_user_data(sk);
336 if (!gs)
337 goto drop;
338
339 geneve_rx(gs, skb);
340 return 0;
341
342drop:
343 /* Consume bad packet */
344 kfree_skb(skb);
345 return 0;
346
347error:
348 /* Let the UDP layer deal with the skb */
349 return 1;
350}
351
352static struct socket *geneve_create_sock(struct net *net, bool ipv6,
Tom Herbertabe492b2015-12-10 12:37:45 -0800353 __be16 port, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700354{
355 struct socket *sock;
356 struct udp_port_cfg udp_conf;
357 int err;
358
359 memset(&udp_conf, 0, sizeof(udp_conf));
360
361 if (ipv6) {
362 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400363 udp_conf.ipv6_v6only = 1;
Tom Herbertabe492b2015-12-10 12:37:45 -0800364 udp_conf.use_udp6_rx_checksums =
365 !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700366 } else {
367 udp_conf.family = AF_INET;
368 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
369 }
370
371 udp_conf.local_udp_port = port;
372
373 /* Open UDP socket */
374 err = udp_sock_create(net, &udp_conf, &sock);
375 if (err < 0)
376 return ERR_PTR(err);
377
378 return sock;
379}
380
381static void geneve_notify_add_rx_port(struct geneve_sock *gs)
382{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800383 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800385 struct net *net = sock_net(sk);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700386 sa_family_t sa_family = sk->sk_family;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800387 __be16 port = inet_sk(sk)->inet_sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700388 int err;
389
390 if (sa_family == AF_INET) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100391 err = udp_add_offload(sock_net(sk), &gs->udp_offloads);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700392 if (err)
393 pr_warn("geneve: udp_add_offload failed with status %d\n",
394 err);
395 }
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800396
397 rcu_read_lock();
398 for_each_netdev_rcu(net, dev) {
399 if (dev->netdev_ops->ndo_add_geneve_port)
400 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family,
401 port);
402 }
403 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700404}
405
406static int geneve_hlen(struct genevehdr *gh)
407{
408 return sizeof(*gh) + gh->opt_len * 4;
409}
410
411static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
412 struct sk_buff *skb,
413 struct udp_offload *uoff)
414{
415 struct sk_buff *p, **pp = NULL;
416 struct genevehdr *gh, *gh2;
417 unsigned int hlen, gh_len, off_gnv;
418 const struct packet_offload *ptype;
419 __be16 type;
420 int flush = 1;
421
422 off_gnv = skb_gro_offset(skb);
423 hlen = off_gnv + sizeof(*gh);
424 gh = skb_gro_header_fast(skb, off_gnv);
425 if (skb_gro_header_hard(skb, hlen)) {
426 gh = skb_gro_header_slow(skb, hlen, off_gnv);
427 if (unlikely(!gh))
428 goto out;
429 }
430
431 if (gh->ver != GENEVE_VER || gh->oam)
432 goto out;
433 gh_len = geneve_hlen(gh);
434
435 hlen = off_gnv + gh_len;
436 if (skb_gro_header_hard(skb, hlen)) {
437 gh = skb_gro_header_slow(skb, hlen, off_gnv);
438 if (unlikely(!gh))
439 goto out;
440 }
441
442 flush = 0;
443
444 for (p = *head; p; p = p->next) {
445 if (!NAPI_GRO_CB(p)->same_flow)
446 continue;
447
448 gh2 = (struct genevehdr *)(p->data + off_gnv);
449 if (gh->opt_len != gh2->opt_len ||
450 memcmp(gh, gh2, gh_len)) {
451 NAPI_GRO_CB(p)->same_flow = 0;
452 continue;
453 }
454 }
455
456 type = gh->proto_type;
457
458 rcu_read_lock();
459 ptype = gro_find_receive_by_type(type);
460 if (!ptype) {
461 flush = 1;
462 goto out_unlock;
463 }
464
465 skb_gro_pull(skb, gh_len);
466 skb_gro_postpull_rcsum(skb, gh, gh_len);
467 pp = ptype->callbacks.gro_receive(head, skb);
468
469out_unlock:
470 rcu_read_unlock();
471out:
472 NAPI_GRO_CB(skb)->flush |= flush;
473
474 return pp;
475}
476
477static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
478 struct udp_offload *uoff)
479{
480 struct genevehdr *gh;
481 struct packet_offload *ptype;
482 __be16 type;
483 int gh_len;
484 int err = -ENOSYS;
485
486 udp_tunnel_gro_complete(skb, nhoff);
487
488 gh = (struct genevehdr *)(skb->data + nhoff);
489 gh_len = geneve_hlen(gh);
490 type = gh->proto_type;
491
492 rcu_read_lock();
493 ptype = gro_find_complete_by_type(type);
494 if (ptype)
495 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
496
497 rcu_read_unlock();
498 return err;
499}
500
501/* Create new listen socket if needed */
502static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
Tom Herbertabe492b2015-12-10 12:37:45 -0800503 bool ipv6, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700504{
505 struct geneve_net *gn = net_generic(net, geneve_net_id);
506 struct geneve_sock *gs;
507 struct socket *sock;
508 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700509 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700510
511 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
512 if (!gs)
513 return ERR_PTR(-ENOMEM);
514
Tom Herbertabe492b2015-12-10 12:37:45 -0800515 sock = geneve_create_sock(net, ipv6, port, flags);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700516 if (IS_ERR(sock)) {
517 kfree(gs);
518 return ERR_CAST(sock);
519 }
520
521 gs->sock = sock;
522 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700523 for (h = 0; h < VNI_HASH_SIZE; ++h)
524 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700525
526 /* Initialize the geneve udp offloads structure */
527 gs->udp_offloads.port = port;
528 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
529 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
530 geneve_notify_add_rx_port(gs);
531
532 /* Mark socket as an encapsulation socket */
533 tunnel_cfg.sk_user_data = gs;
534 tunnel_cfg.encap_type = 1;
535 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
536 tunnel_cfg.encap_destroy = NULL;
537 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700538 list_add(&gs->list, &gn->sock_list);
539 return gs;
540}
541
542static void geneve_notify_del_rx_port(struct geneve_sock *gs)
543{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800544 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700545 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800546 struct net *net = sock_net(sk);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700547 sa_family_t sa_family = sk->sk_family;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800548 __be16 port = inet_sk(sk)->inet_sport;
549
550 rcu_read_lock();
551 for_each_netdev_rcu(net, dev) {
552 if (dev->netdev_ops->ndo_del_geneve_port)
553 dev->netdev_ops->ndo_del_geneve_port(dev, sa_family,
554 port);
555 }
556
557 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700558
559 if (sa_family == AF_INET)
560 udp_del_offload(&gs->udp_offloads);
561}
562
John W. Linville8ed66f02015-10-26 17:01:44 -0400563static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700564{
John W. Linville8ed66f02015-10-26 17:01:44 -0400565 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700566 return;
567
568 list_del(&gs->list);
569 geneve_notify_del_rx_port(gs);
570 udp_tunnel_sock_release(gs->sock);
571 kfree_rcu(gs, rcu);
572}
573
John W. Linville8ed66f02015-10-26 17:01:44 -0400574static void geneve_sock_release(struct geneve_dev *geneve)
575{
576 __geneve_sock_release(geneve->sock4);
577#if IS_ENABLED(CONFIG_IPV6)
578 __geneve_sock_release(geneve->sock6);
579#endif
580}
581
Pravin B Shelar371bd102015-08-26 23:46:54 -0700582static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400583 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584 __be16 dst_port)
585{
586 struct geneve_sock *gs;
587
588 list_for_each_entry(gs, &gn->sock_list, list) {
589 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400590 inet_sk(gs->sock->sk)->sk.sk_family == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700591 return gs;
592 }
593 }
594 return NULL;
595}
596
John W. Linville8ed66f02015-10-26 17:01:44 -0400597static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400598{
John W. Linville2d07dc72015-05-13 12:57:30 -0400599 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700600 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400601 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700602 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400603
John W. Linville8ed66f02015-10-26 17:01:44 -0400604 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700605 if (gs) {
606 gs->refcnt++;
607 goto out;
608 }
609
Tom Herbertabe492b2015-12-10 12:37:45 -0800610 gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
John W. Linville2d07dc72015-05-13 12:57:30 -0400611 if (IS_ERR(gs))
612 return PTR_ERR(gs);
613
Pravin B Shelar371bd102015-08-26 23:46:54 -0700614out:
615 gs->collect_md = geneve->collect_md;
Tom Herbertabe492b2015-12-10 12:37:45 -0800616 gs->flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400617#if IS_ENABLED(CONFIG_IPV6)
618 if (ipv6)
619 geneve->sock6 = gs;
620 else
621#endif
622 geneve->sock4 = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700623
624 hash = geneve_net_vni_hash(geneve->vni);
625 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400626 return 0;
627}
628
John W. Linville8ed66f02015-10-26 17:01:44 -0400629static int geneve_open(struct net_device *dev)
630{
631 struct geneve_dev *geneve = netdev_priv(dev);
632 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
633 bool metadata = geneve->collect_md;
634 int ret = 0;
635
636 geneve->sock4 = NULL;
637#if IS_ENABLED(CONFIG_IPV6)
638 geneve->sock6 = NULL;
639 if (ipv6 || metadata)
640 ret = geneve_sock_add(geneve, true);
641#endif
642 if (!ret && (!ipv6 || metadata))
643 ret = geneve_sock_add(geneve, false);
644 if (ret < 0)
645 geneve_sock_release(geneve);
646
647 return ret;
648}
649
John W. Linville2d07dc72015-05-13 12:57:30 -0400650static int geneve_stop(struct net_device *dev)
651{
652 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400653
Pravin B Shelar66d47002015-08-26 23:46:55 -0700654 if (!hlist_unhashed(&geneve->hlist))
655 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400656 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400657 return 0;
658}
659
John W. Linville8ed66f02015-10-26 17:01:44 -0400660static void geneve_build_header(struct genevehdr *geneveh,
661 __be16 tun_flags, u8 vni[3],
662 u8 options_len, u8 *options)
663{
664 geneveh->ver = GENEVE_VER;
665 geneveh->opt_len = options_len / 4;
666 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
667 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
668 geneveh->rsvd1 = 0;
669 memcpy(geneveh->vni, vni, 3);
670 geneveh->proto_type = htons(ETH_P_TEB);
671 geneveh->rsvd2 = 0;
672
673 memcpy(geneveh->options, options, options_len);
674}
675
Pravin B Shelar371bd102015-08-26 23:46:54 -0700676static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
677 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800678 u32 flags, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700679{
680 struct genevehdr *gnvh;
681 int min_headroom;
682 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800683 bool udp_sum = !!(flags & GENEVE_F_UDP_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700684
John W. Linville8ed66f02015-10-26 17:01:44 -0400685 skb_scrub_packet(skb, xnet);
686
Pravin B Shelar371bd102015-08-26 23:46:54 -0700687 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
688 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
689 err = skb_cow_head(skb, min_headroom);
690 if (unlikely(err)) {
691 kfree_skb(skb);
692 goto free_rt;
693 }
694
Tom Herbertabe492b2015-12-10 12:37:45 -0800695 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700696 if (IS_ERR(skb)) {
697 err = PTR_ERR(skb);
698 goto free_rt;
699 }
700
701 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400702 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700703
704 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
705 return 0;
706
707free_rt:
708 ip_rt_put(rt);
709 return err;
710}
711
John W. Linville8ed66f02015-10-26 17:01:44 -0400712#if IS_ENABLED(CONFIG_IPV6)
713static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
714 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800715 u32 flags, bool xnet)
John W. Linville8ed66f02015-10-26 17:01:44 -0400716{
717 struct genevehdr *gnvh;
718 int min_headroom;
719 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800720 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
John W. Linville8ed66f02015-10-26 17:01:44 -0400721
722 skb_scrub_packet(skb, xnet);
723
724 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
725 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
726 err = skb_cow_head(skb, min_headroom);
727 if (unlikely(err)) {
728 kfree_skb(skb);
729 goto free_dst;
730 }
731
Tom Herbertabe492b2015-12-10 12:37:45 -0800732 skb = udp_tunnel_handle_offloads(skb, udp_sum);
John W. Linville8ed66f02015-10-26 17:01:44 -0400733 if (IS_ERR(skb)) {
734 err = PTR_ERR(skb);
735 goto free_dst;
736 }
737
738 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
739 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
740
741 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
742 return 0;
743
744free_dst:
745 dst_release(dst);
746 return err;
747}
748#endif
749
750static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
751 struct net_device *dev,
752 struct flowi4 *fl4,
753 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754{
755 struct geneve_dev *geneve = netdev_priv(dev);
756 struct rtable *rt = NULL;
757 __u8 tos;
758
759 memset(fl4, 0, sizeof(*fl4));
760 fl4->flowi4_mark = skb->mark;
761 fl4->flowi4_proto = IPPROTO_UDP;
762
763 if (info) {
764 fl4->daddr = info->key.u.ipv4.dst;
765 fl4->saddr = info->key.u.ipv4.src;
766 fl4->flowi4_tos = RT_TOS(info->key.tos);
767 } else {
768 tos = geneve->tos;
769 if (tos == 1) {
770 const struct iphdr *iip = ip_hdr(skb);
771
772 tos = ip_tunnel_get_dsfield(iip, skb);
773 }
774
775 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400776 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777 }
778
779 rt = ip_route_output_key(geneve->net, fl4);
780 if (IS_ERR(rt)) {
781 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700782 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700783 }
784 if (rt->dst.dev == dev) { /* is this necessary? */
785 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700786 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700787 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700788 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700789 return rt;
790}
791
John W. Linville8ed66f02015-10-26 17:01:44 -0400792#if IS_ENABLED(CONFIG_IPV6)
793static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
794 struct net_device *dev,
795 struct flowi6 *fl6,
796 struct ip_tunnel_info *info)
797{
798 struct geneve_dev *geneve = netdev_priv(dev);
799 struct geneve_sock *gs6 = geneve->sock6;
800 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400801 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400802
803 memset(fl6, 0, sizeof(*fl6));
804 fl6->flowi6_mark = skb->mark;
805 fl6->flowi6_proto = IPPROTO_UDP;
806
807 if (info) {
808 fl6->daddr = info->key.u.ipv6.dst;
809 fl6->saddr = info->key.u.ipv6.src;
John W. Linville3a56f862015-10-26 17:01:45 -0400810 fl6->flowi6_tos = RT_TOS(info->key.tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400811 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400812 prio = geneve->tos;
813 if (prio == 1) {
814 const struct iphdr *iip = ip_hdr(skb);
815
816 prio = ip_tunnel_get_dsfield(iip, skb);
817 }
818
819 fl6->flowi6_tos = RT_TOS(prio);
John W. Linville8ed66f02015-10-26 17:01:44 -0400820 fl6->daddr = geneve->remote.sin6.sin6_addr;
821 }
822
823 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
824 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
825 return ERR_PTR(-ENETUNREACH);
826 }
827 if (dst->dev == dev) { /* is this necessary? */
828 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
829 dst_release(dst);
830 return ERR_PTR(-ELOOP);
831 }
832
833 return dst;
834}
835#endif
836
Pravin B Shelare305ac62015-08-26 23:46:52 -0700837/* Convert 64 bit tunnel ID to 24 bit VNI. */
838static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
839{
840#ifdef __BIG_ENDIAN
841 vni[0] = (__force __u8)(tun_id >> 16);
842 vni[1] = (__force __u8)(tun_id >> 8);
843 vni[2] = (__force __u8)tun_id;
844#else
845 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
846 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
847 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
848#endif
849}
850
John W. Linville8ed66f02015-10-26 17:01:44 -0400851static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
852 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400853{
854 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400855 struct geneve_sock *gs4 = geneve->sock4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400856 struct rtable *rt = NULL;
John W. Linville08399ef2015-09-21 10:29:09 -0400857 const struct iphdr *iip; /* interior IP header */
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700858 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400859 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400860 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700861 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700862 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400863 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800864 u32 flags = geneve->flags;
John W. Linville2d07dc72015-05-13 12:57:30 -0400865
Pravin B Shelare305ac62015-08-26 23:46:52 -0700866 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400867 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700868 netdev_dbg(dev, "no tunnel metadata\n");
869 goto tx_error;
870 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200871 if (info && ip_tunnel_info_af(info) != AF_INET)
872 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700873 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400874
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400876 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700877 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400878 goto tx_error;
879 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700880
881 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700882 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700883
John W. Linville08399ef2015-09-21 10:29:09 -0400884 iip = ip_hdr(skb);
885
Pravin B Shelare305ac62015-08-26 23:46:52 -0700886 if (info) {
887 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700888 u8 *opts = NULL;
889 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700890
891 tunnel_id_to_vni(key->tun_id, vni);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700892 if (key->tun_flags & TUNNEL_GENEVE_OPT)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700893 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700894
Tom Herbertabe492b2015-12-10 12:37:45 -0800895 if (key->tun_flags & TUNNEL_CSUM)
896 flags |= GENEVE_F_UDP_CSUM;
897 else
898 flags &= ~GENEVE_F_UDP_CSUM;
899
Pravin B Shelar371bd102015-08-26 23:46:54 -0700900 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800901 info->options_len, opts, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700902 if (unlikely(err))
903 goto err;
904
John W. Linville08399ef2015-09-21 10:29:09 -0400905 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700906 ttl = key->ttl;
907 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700908 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700909 err = geneve_build_skb(rt, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800910 0, NULL, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700911 if (unlikely(err))
912 goto err;
913
Pravin B Shelare305ac62015-08-26 23:46:52 -0700914 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700915 ttl = geneve->ttl;
916 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
917 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700918 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700919 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400920 }
Pravin B Shelar039f5062015-12-24 14:34:54 -0800921 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
922 tos, ttl, df, sport, geneve->dst_port,
923 !net_eq(geneve->net, dev_net(geneve->dev)),
924 !(flags & GENEVE_F_UDP_CSUM));
John W. Linville2d07dc72015-05-13 12:57:30 -0400925
John W. Linville2d07dc72015-05-13 12:57:30 -0400926 return NETDEV_TX_OK;
927
John W. Linville2d07dc72015-05-13 12:57:30 -0400928tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400929 dev_kfree_skb(skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700930err:
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700931 if (err == -ELOOP)
932 dev->stats.collisions++;
933 else if (err == -ENETUNREACH)
934 dev->stats.tx_carrier_errors++;
935 else
936 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400937 return NETDEV_TX_OK;
938}
939
John W. Linville8ed66f02015-10-26 17:01:44 -0400940#if IS_ENABLED(CONFIG_IPV6)
941static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
942 struct ip_tunnel_info *info)
943{
944 struct geneve_dev *geneve = netdev_priv(dev);
945 struct geneve_sock *gs6 = geneve->sock6;
946 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400947 const struct iphdr *iip; /* interior IP header */
John W. Linville8ed66f02015-10-26 17:01:44 -0400948 int err = -EINVAL;
949 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400950 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400951 __be16 sport;
John W. Linville8ed66f02015-10-26 17:01:44 -0400952 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800953 u32 flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400954
955 if (geneve->collect_md) {
956 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
957 netdev_dbg(dev, "no tunnel metadata\n");
958 goto tx_error;
959 }
960 }
961
962 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
963 if (IS_ERR(dst)) {
964 err = PTR_ERR(dst);
965 goto tx_error;
966 }
967
968 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
969 skb_reset_mac_header(skb);
970
John W. Linville3a56f862015-10-26 17:01:45 -0400971 iip = ip_hdr(skb);
972
John W. Linville8ed66f02015-10-26 17:01:44 -0400973 if (info) {
974 const struct ip_tunnel_key *key = &info->key;
975 u8 *opts = NULL;
976 u8 vni[3];
977
978 tunnel_id_to_vni(key->tun_id, vni);
979 if (key->tun_flags & TUNNEL_GENEVE_OPT)
980 opts = ip_tunnel_info_opts(info);
981
Tom Herbertabe492b2015-12-10 12:37:45 -0800982 if (key->tun_flags & TUNNEL_CSUM)
Jesse Gross35e2d112016-01-20 16:22:47 -0800983 flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800984 else
Jesse Gross35e2d112016-01-20 16:22:47 -0800985 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800986
John W. Linville8ed66f02015-10-26 17:01:44 -0400987 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
988 info->options_len, opts,
Tom Herbertabe492b2015-12-10 12:37:45 -0800989 flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -0400990 if (unlikely(err))
991 goto err;
992
John W. Linville3a56f862015-10-26 17:01:45 -0400993 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400994 ttl = key->ttl;
995 } else {
John W. Linville8ed66f02015-10-26 17:01:44 -0400996 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800997 0, NULL, flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -0400998 if (unlikely(err))
999 goto err;
1000
John W. Linville3a56f862015-10-26 17:01:45 -04001001 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001002 ttl = geneve->ttl;
1003 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
1004 ttl = 1;
1005 ttl = ttl ? : ip6_dst_hoplimit(dst);
1006 }
Pravin B Shelar039f5062015-12-24 14:34:54 -08001007 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
1008 &fl6.saddr, &fl6.daddr, prio, ttl,
1009 sport, geneve->dst_port,
1010 !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
John W. Linville8ed66f02015-10-26 17:01:44 -04001011 return NETDEV_TX_OK;
1012
1013tx_error:
1014 dev_kfree_skb(skb);
1015err:
1016 if (err == -ELOOP)
1017 dev->stats.collisions++;
1018 else if (err == -ENETUNREACH)
1019 dev->stats.tx_carrier_errors++;
1020 else
1021 dev->stats.tx_errors++;
1022 return NETDEV_TX_OK;
1023}
1024#endif
1025
1026static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1027{
1028 struct geneve_dev *geneve = netdev_priv(dev);
1029 struct ip_tunnel_info *info = NULL;
1030
1031 if (geneve->collect_md)
1032 info = skb_tunnel_info(skb);
1033
1034#if IS_ENABLED(CONFIG_IPV6)
1035 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
1036 (!info && geneve->remote.sa.sa_family == AF_INET6))
1037 return geneve6_xmit_skb(skb, dev, info);
1038#endif
1039 return geneve_xmit_skb(skb, dev, info);
1040}
1041
David Wragg55e5bfb2016-02-10 00:05:57 +00001042static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1043{
1044 /* GENEVE overhead is not fixed, so we can't enforce a more
1045 * precise max MTU.
1046 */
1047 if (new_mtu < 68 || new_mtu > IP_MAX_MTU)
1048 return -EINVAL;
1049 dev->mtu = new_mtu;
1050 return 0;
1051}
1052
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001053static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1054{
1055 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1056 struct geneve_dev *geneve = netdev_priv(dev);
1057 struct rtable *rt;
1058 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001059#if IS_ENABLED(CONFIG_IPV6)
1060 struct dst_entry *dst;
1061 struct flowi6 fl6;
1062#endif
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001063
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001064 if (ip_tunnel_info_af(info) == AF_INET) {
1065 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1066 if (IS_ERR(rt))
1067 return PTR_ERR(rt);
1068
1069 ip_rt_put(rt);
1070 info->key.u.ipv4.src = fl4.saddr;
1071#if IS_ENABLED(CONFIG_IPV6)
1072 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1073 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1074 if (IS_ERR(dst))
1075 return PTR_ERR(dst);
1076
1077 dst_release(dst);
1078 info->key.u.ipv6.src = fl6.saddr;
1079#endif
1080 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001081 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001082 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001083
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001084 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1085 1, USHRT_MAX, true);
1086 info->key.tp_dst = geneve->dst_port;
1087 return 0;
1088}
1089
John W. Linville2d07dc72015-05-13 12:57:30 -04001090static const struct net_device_ops geneve_netdev_ops = {
1091 .ndo_init = geneve_init,
1092 .ndo_uninit = geneve_uninit,
1093 .ndo_open = geneve_open,
1094 .ndo_stop = geneve_stop,
1095 .ndo_start_xmit = geneve_xmit,
1096 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001097 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001098 .ndo_validate_addr = eth_validate_addr,
1099 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001100 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001101};
1102
1103static void geneve_get_drvinfo(struct net_device *dev,
1104 struct ethtool_drvinfo *drvinfo)
1105{
1106 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1107 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1108}
1109
1110static const struct ethtool_ops geneve_ethtool_ops = {
1111 .get_drvinfo = geneve_get_drvinfo,
1112 .get_link = ethtool_op_get_link,
1113};
1114
1115/* Info for udev, that this is a virtual tunnel endpoint */
1116static struct device_type geneve_type = {
1117 .name = "geneve",
1118};
1119
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001120/* Calls the ndo_add_geneve_port of the caller in order to
1121 * supply the listening GENEVE udp ports. Callers are expected
1122 * to implement the ndo_add_geneve_port.
1123 */
1124void geneve_get_rx_port(struct net_device *dev)
1125{
1126 struct net *net = dev_net(dev);
1127 struct geneve_net *gn = net_generic(net, geneve_net_id);
1128 struct geneve_sock *gs;
1129 sa_family_t sa_family;
1130 struct sock *sk;
1131 __be16 port;
1132
1133 rcu_read_lock();
1134 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1135 sk = gs->sock->sk;
1136 sa_family = sk->sk_family;
1137 port = inet_sk(sk)->inet_sport;
1138 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port);
1139 }
1140 rcu_read_unlock();
1141}
1142EXPORT_SYMBOL_GPL(geneve_get_rx_port);
1143
John W. Linville2d07dc72015-05-13 12:57:30 -04001144/* Initialize the device structure. */
1145static void geneve_setup(struct net_device *dev)
1146{
1147 ether_setup(dev);
1148
1149 dev->netdev_ops = &geneve_netdev_ops;
1150 dev->ethtool_ops = &geneve_ethtool_ops;
1151 dev->destructor = free_netdev;
1152
1153 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1154
John W. Linville2d07dc72015-05-13 12:57:30 -04001155 dev->features |= NETIF_F_LLTX;
1156 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1157 dev->features |= NETIF_F_RXCSUM;
1158 dev->features |= NETIF_F_GSO_SOFTWARE;
1159
John W. Linville2d07dc72015-05-13 12:57:30 -04001160 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1161 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001162
1163 netif_keep_dst(dev);
Phil Suttered961ac2015-08-18 10:30:31 +02001164 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001165 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001166}
1167
1168static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1169 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1170 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001171 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001172 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001173 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001174 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001175 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001176 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1177 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1178 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001179};
1180
1181static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1182{
1183 if (tb[IFLA_ADDRESS]) {
1184 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1185 return -EINVAL;
1186
1187 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1188 return -EADDRNOTAVAIL;
1189 }
1190
1191 if (!data)
1192 return -EINVAL;
1193
1194 if (data[IFLA_GENEVE_ID]) {
1195 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1196
1197 if (vni >= GENEVE_VID_MASK)
1198 return -ERANGE;
1199 }
1200
1201 return 0;
1202}
1203
Pravin B Shelar371bd102015-08-26 23:46:54 -07001204static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1205 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001206 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001207 u8 vni[],
1208 bool *tun_on_same_port,
1209 bool *tun_collect_md)
1210{
1211 struct geneve_dev *geneve, *t;
1212
1213 *tun_on_same_port = false;
1214 *tun_collect_md = false;
1215 t = NULL;
1216 list_for_each_entry(geneve, &gn->geneve_list, next) {
1217 if (geneve->dst_port == dst_port) {
1218 *tun_collect_md = geneve->collect_md;
1219 *tun_on_same_port = true;
1220 }
1221 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001222 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001223 dst_port == geneve->dst_port)
1224 t = geneve;
1225 }
1226 return t;
1227}
1228
Pravin B Shelare305ac62015-08-26 23:46:52 -07001229static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001230 union geneve_addr *remote,
1231 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001232 bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001233{
1234 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001235 struct geneve_dev *t, *geneve = netdev_priv(dev);
1236 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001237 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001238
John W. Linville8ed66f02015-10-26 17:01:44 -04001239 if (!remote)
1240 return -EINVAL;
1241 if (metadata &&
1242 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1243 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001244
1245 geneve->net = net;
1246 geneve->dev = dev;
1247
John W. Linville2d07dc72015-05-13 12:57:30 -04001248 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1249 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1250 geneve->vni[2] = vni & 0x000000ff;
1251
John W. Linville8ed66f02015-10-26 17:01:44 -04001252 if ((remote->sa.sa_family == AF_INET &&
1253 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1254 (remote->sa.sa_family == AF_INET6 &&
1255 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001256 return -EINVAL;
John W. Linville8ed66f02015-10-26 17:01:44 -04001257 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001258
Pravin B Shelare305ac62015-08-26 23:46:52 -07001259 geneve->ttl = ttl;
1260 geneve->tos = tos;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001261 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001262 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001263 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001264
John W. Linville8ed66f02015-10-26 17:01:44 -04001265 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001266 &tun_on_same_port, &tun_collect_md);
1267 if (t)
1268 return -EBUSY;
1269
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001270 /* make enough headroom for basic scenario */
1271 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1272 if (remote->sa.sa_family == AF_INET)
1273 encap_len += sizeof(struct iphdr);
1274 else
1275 encap_len += sizeof(struct ipv6hdr);
1276 dev->needed_headroom = encap_len + ETH_HLEN;
1277
Pravin B Shelar371bd102015-08-26 23:46:54 -07001278 if (metadata) {
1279 if (tun_on_same_port)
1280 return -EPERM;
1281 } else {
1282 if (tun_collect_md)
1283 return -EPERM;
1284 }
1285
John W. Linville2d07dc72015-05-13 12:57:30 -04001286 err = register_netdevice(dev);
1287 if (err)
1288 return err;
1289
1290 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001291 return 0;
1292}
1293
Pravin B Shelare305ac62015-08-26 23:46:52 -07001294static int geneve_newlink(struct net *net, struct net_device *dev,
1295 struct nlattr *tb[], struct nlattr *data[])
1296{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001297 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001298 __u8 ttl = 0, tos = 0;
1299 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001300 union geneve_addr remote = geneve_remote_unspec;
Jesse Grosse277de52015-10-16 16:36:00 -07001301 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001302 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001303
John W. Linville8ed66f02015-10-26 17:01:44 -04001304 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1305 return -EINVAL;
1306
1307 if (data[IFLA_GENEVE_REMOTE]) {
1308 remote.sa.sa_family = AF_INET;
1309 remote.sin.sin_addr.s_addr =
1310 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1311 }
1312
1313 if (data[IFLA_GENEVE_REMOTE6]) {
1314 if (!IS_ENABLED(CONFIG_IPV6))
1315 return -EPFNOSUPPORT;
1316
1317 remote.sa.sa_family = AF_INET6;
1318 remote.sin6.sin6_addr =
1319 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1320
1321 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1322 IPV6_ADDR_LINKLOCAL) {
1323 netdev_dbg(dev, "link-local remote is unsupported\n");
1324 return -EINVAL;
1325 }
1326 }
1327
Jesse Grosse277de52015-10-16 16:36:00 -07001328 if (data[IFLA_GENEVE_ID])
1329 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001330
Pravin B Shelare305ac62015-08-26 23:46:52 -07001331 if (data[IFLA_GENEVE_TTL])
1332 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1333
1334 if (data[IFLA_GENEVE_TOS])
1335 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1336
1337 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001338 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001339
1340 if (data[IFLA_GENEVE_COLLECT_METADATA])
1341 metadata = true;
1342
Tom Herbertabe492b2015-12-10 12:37:45 -08001343 if (data[IFLA_GENEVE_UDP_CSUM] &&
1344 nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1345 flags |= GENEVE_F_UDP_CSUM;
1346
1347 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1348 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1349 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1350
1351 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1352 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1353 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1354
John W. Linville8ed66f02015-10-26 17:01:44 -04001355 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001356 metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001357}
1358
John W. Linville2d07dc72015-05-13 12:57:30 -04001359static void geneve_dellink(struct net_device *dev, struct list_head *head)
1360{
1361 struct geneve_dev *geneve = netdev_priv(dev);
1362
John W. Linville2d07dc72015-05-13 12:57:30 -04001363 list_del(&geneve->next);
1364 unregister_netdevice_queue(dev, head);
1365}
1366
1367static size_t geneve_get_size(const struct net_device *dev)
1368{
1369 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001370 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001371 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001372 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001373 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001374 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001375 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1376 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1377 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001378 0;
1379}
1380
1381static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1382{
1383 struct geneve_dev *geneve = netdev_priv(dev);
1384 __u32 vni;
1385
1386 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1387 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1388 goto nla_put_failure;
1389
John W. Linville8ed66f02015-10-26 17:01:44 -04001390 if (geneve->remote.sa.sa_family == AF_INET) {
1391 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1392 geneve->remote.sin.sin_addr.s_addr))
1393 goto nla_put_failure;
1394#if IS_ENABLED(CONFIG_IPV6)
1395 } else {
1396 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1397 &geneve->remote.sin6.sin6_addr))
1398 goto nla_put_failure;
1399#endif
1400 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001401
John W. Linvilled8951122015-06-01 15:51:35 -04001402 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1403 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
John W. Linville8760ce52015-06-01 15:51:34 -04001404 goto nla_put_failure;
1405
John W. Linville7bbe33f2015-09-22 13:09:32 -04001406 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001407 goto nla_put_failure;
1408
Pravin B Shelare305ac62015-08-26 23:46:52 -07001409 if (geneve->collect_md) {
1410 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1411 goto nla_put_failure;
1412 }
1413
Tom Herbertabe492b2015-12-10 12:37:45 -08001414 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1415 !!(geneve->flags & GENEVE_F_UDP_CSUM)) ||
1416 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1417 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1418 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1419 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1420 goto nla_put_failure;
1421
John W. Linville2d07dc72015-05-13 12:57:30 -04001422 return 0;
1423
1424nla_put_failure:
1425 return -EMSGSIZE;
1426}
1427
1428static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1429 .kind = "geneve",
1430 .maxtype = IFLA_GENEVE_MAX,
1431 .policy = geneve_policy,
1432 .priv_size = sizeof(struct geneve_dev),
1433 .setup = geneve_setup,
1434 .validate = geneve_validate,
1435 .newlink = geneve_newlink,
1436 .dellink = geneve_dellink,
1437 .get_size = geneve_get_size,
1438 .fill_info = geneve_fill_info,
1439};
1440
Pravin B Shelare305ac62015-08-26 23:46:52 -07001441struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1442 u8 name_assign_type, u16 dst_port)
1443{
1444 struct nlattr *tb[IFLA_MAX + 1];
1445 struct net_device *dev;
1446 int err;
1447
1448 memset(tb, 0, sizeof(tb));
1449 dev = rtnl_create_link(net, name, name_assign_type,
1450 &geneve_link_ops, tb);
1451 if (IS_ERR(dev))
1452 return dev;
1453
John W. Linville8ed66f02015-10-26 17:01:44 -04001454 err = geneve_configure(net, dev, &geneve_remote_unspec,
Tom Herbertabe492b2015-12-10 12:37:45 -08001455 0, 0, 0, htons(dst_port), true, 0);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001456 if (err) {
1457 free_netdev(dev);
1458 return ERR_PTR(err);
1459 }
1460 return dev;
1461}
1462EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1463
John W. Linville2d07dc72015-05-13 12:57:30 -04001464static __net_init int geneve_init_net(struct net *net)
1465{
1466 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001467
1468 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001469 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001470 return 0;
1471}
1472
1473static void __net_exit geneve_exit_net(struct net *net)
1474{
1475 struct geneve_net *gn = net_generic(net, geneve_net_id);
1476 struct geneve_dev *geneve, *next;
1477 struct net_device *dev, *aux;
1478 LIST_HEAD(list);
1479
1480 rtnl_lock();
1481
1482 /* gather any geneve devices that were moved into this ns */
1483 for_each_netdev_safe(net, dev, aux)
1484 if (dev->rtnl_link_ops == &geneve_link_ops)
1485 unregister_netdevice_queue(dev, &list);
1486
1487 /* now gather any other geneve devices that were created in this ns */
1488 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1489 /* If geneve->dev is in the same netns, it was already added
1490 * to the list by the previous loop.
1491 */
1492 if (!net_eq(dev_net(geneve->dev), net))
1493 unregister_netdevice_queue(geneve->dev, &list);
1494 }
1495
1496 /* unregister the devices gathered above */
1497 unregister_netdevice_many(&list);
1498 rtnl_unlock();
1499}
1500
1501static struct pernet_operations geneve_net_ops = {
1502 .init = geneve_init_net,
1503 .exit = geneve_exit_net,
1504 .id = &geneve_net_id,
1505 .size = sizeof(struct geneve_net),
1506};
1507
1508static int __init geneve_init_module(void)
1509{
1510 int rc;
1511
1512 rc = register_pernet_subsys(&geneve_net_ops);
1513 if (rc)
1514 goto out1;
1515
1516 rc = rtnl_link_register(&geneve_link_ops);
1517 if (rc)
1518 goto out2;
1519
1520 return 0;
1521out2:
1522 unregister_pernet_subsys(&geneve_net_ops);
1523out1:
1524 return rc;
1525}
1526late_initcall(geneve_init_module);
1527
1528static void __exit geneve_cleanup_module(void)
1529{
1530 rtnl_link_unregister(&geneve_link_ops);
1531 unregister_pernet_subsys(&geneve_net_ops);
1532}
1533module_exit(geneve_cleanup_module);
1534
1535MODULE_LICENSE("GPL");
1536MODULE_VERSION(GENEVE_NETDEV_VER);
1537MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1538MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1539MODULE_ALIAS_RTNL_LINK("geneve");