blob: 0bf7edd99573d336f179031d358f0684cf454cc7 [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 Wraggaeee0e62016-02-18 17:43:29 +00001042static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict)
David Wragg55e5bfb2016-02-10 00:05:57 +00001043{
David Wraggaeee0e62016-02-18 17:43:29 +00001044 /* The max_mtu calculation does not take account of GENEVE
1045 * options, to avoid excluding potentially valid
1046 * configurations.
David Wragg55e5bfb2016-02-10 00:05:57 +00001047 */
David Wraggaeee0e62016-02-18 17:43:29 +00001048 int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - sizeof(struct iphdr)
1049 - dev->hard_header_len;
1050
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
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001137/* Calls the ndo_add_geneve_port of the caller in order to
1138 * supply the listening GENEVE udp ports. Callers are expected
1139 * to implement the ndo_add_geneve_port.
1140 */
1141void geneve_get_rx_port(struct net_device *dev)
1142{
1143 struct net *net = dev_net(dev);
1144 struct geneve_net *gn = net_generic(net, geneve_net_id);
1145 struct geneve_sock *gs;
1146 sa_family_t sa_family;
1147 struct sock *sk;
1148 __be16 port;
1149
1150 rcu_read_lock();
1151 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1152 sk = gs->sock->sk;
1153 sa_family = sk->sk_family;
1154 port = inet_sk(sk)->inet_sport;
1155 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port);
1156 }
1157 rcu_read_unlock();
1158}
1159EXPORT_SYMBOL_GPL(geneve_get_rx_port);
1160
John W. Linville2d07dc72015-05-13 12:57:30 -04001161/* Initialize the device structure. */
1162static void geneve_setup(struct net_device *dev)
1163{
1164 ether_setup(dev);
1165
1166 dev->netdev_ops = &geneve_netdev_ops;
1167 dev->ethtool_ops = &geneve_ethtool_ops;
1168 dev->destructor = free_netdev;
1169
1170 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1171
John W. Linville2d07dc72015-05-13 12:57:30 -04001172 dev->features |= NETIF_F_LLTX;
1173 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1174 dev->features |= NETIF_F_RXCSUM;
1175 dev->features |= NETIF_F_GSO_SOFTWARE;
1176
John W. Linville2d07dc72015-05-13 12:57:30 -04001177 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1178 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001179
1180 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001181 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001182 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001183 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001184}
1185
1186static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1187 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1188 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001189 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001190 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001191 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001192 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001193 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001194 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1195 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1196 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001197};
1198
1199static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1200{
1201 if (tb[IFLA_ADDRESS]) {
1202 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1203 return -EINVAL;
1204
1205 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1206 return -EADDRNOTAVAIL;
1207 }
1208
1209 if (!data)
1210 return -EINVAL;
1211
1212 if (data[IFLA_GENEVE_ID]) {
1213 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1214
1215 if (vni >= GENEVE_VID_MASK)
1216 return -ERANGE;
1217 }
1218
1219 return 0;
1220}
1221
Pravin B Shelar371bd102015-08-26 23:46:54 -07001222static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1223 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001224 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001225 u8 vni[],
1226 bool *tun_on_same_port,
1227 bool *tun_collect_md)
1228{
1229 struct geneve_dev *geneve, *t;
1230
1231 *tun_on_same_port = false;
1232 *tun_collect_md = false;
1233 t = NULL;
1234 list_for_each_entry(geneve, &gn->geneve_list, next) {
1235 if (geneve->dst_port == dst_port) {
1236 *tun_collect_md = geneve->collect_md;
1237 *tun_on_same_port = true;
1238 }
1239 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001240 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001241 dst_port == geneve->dst_port)
1242 t = geneve;
1243 }
1244 return t;
1245}
1246
Pravin B Shelare305ac62015-08-26 23:46:52 -07001247static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001248 union geneve_addr *remote,
1249 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001250 bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001251{
1252 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001253 struct geneve_dev *t, *geneve = netdev_priv(dev);
1254 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001255 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001256
John W. Linville8ed66f02015-10-26 17:01:44 -04001257 if (!remote)
1258 return -EINVAL;
1259 if (metadata &&
1260 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1261 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001262
1263 geneve->net = net;
1264 geneve->dev = dev;
1265
John W. Linville2d07dc72015-05-13 12:57:30 -04001266 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1267 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1268 geneve->vni[2] = vni & 0x000000ff;
1269
John W. Linville8ed66f02015-10-26 17:01:44 -04001270 if ((remote->sa.sa_family == AF_INET &&
1271 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1272 (remote->sa.sa_family == AF_INET6 &&
1273 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001274 return -EINVAL;
John W. Linville8ed66f02015-10-26 17:01:44 -04001275 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001276
Pravin B Shelare305ac62015-08-26 23:46:52 -07001277 geneve->ttl = ttl;
1278 geneve->tos = tos;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001279 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001280 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001281 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001282
John W. Linville8ed66f02015-10-26 17:01:44 -04001283 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001284 &tun_on_same_port, &tun_collect_md);
1285 if (t)
1286 return -EBUSY;
1287
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001288 /* make enough headroom for basic scenario */
1289 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1290 if (remote->sa.sa_family == AF_INET)
1291 encap_len += sizeof(struct iphdr);
1292 else
1293 encap_len += sizeof(struct ipv6hdr);
1294 dev->needed_headroom = encap_len + ETH_HLEN;
1295
Pravin B Shelar371bd102015-08-26 23:46:54 -07001296 if (metadata) {
1297 if (tun_on_same_port)
1298 return -EPERM;
1299 } else {
1300 if (tun_collect_md)
1301 return -EPERM;
1302 }
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;
Jesse Grosse277de52015-10-16 16:36:00 -07001319 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001320 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001321
John W. Linville8ed66f02015-10-26 17:01:44 -04001322 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1323 return -EINVAL;
1324
1325 if (data[IFLA_GENEVE_REMOTE]) {
1326 remote.sa.sa_family = AF_INET;
1327 remote.sin.sin_addr.s_addr =
1328 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1329 }
1330
1331 if (data[IFLA_GENEVE_REMOTE6]) {
1332 if (!IS_ENABLED(CONFIG_IPV6))
1333 return -EPFNOSUPPORT;
1334
1335 remote.sa.sa_family = AF_INET6;
1336 remote.sin6.sin6_addr =
1337 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1338
1339 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1340 IPV6_ADDR_LINKLOCAL) {
1341 netdev_dbg(dev, "link-local remote is unsupported\n");
1342 return -EINVAL;
1343 }
1344 }
1345
Jesse Grosse277de52015-10-16 16:36:00 -07001346 if (data[IFLA_GENEVE_ID])
1347 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001348
Pravin B Shelare305ac62015-08-26 23:46:52 -07001349 if (data[IFLA_GENEVE_TTL])
1350 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1351
1352 if (data[IFLA_GENEVE_TOS])
1353 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1354
1355 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001356 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001357
1358 if (data[IFLA_GENEVE_COLLECT_METADATA])
1359 metadata = true;
1360
Tom Herbertabe492b2015-12-10 12:37:45 -08001361 if (data[IFLA_GENEVE_UDP_CSUM] &&
1362 nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1363 flags |= GENEVE_F_UDP_CSUM;
1364
1365 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1366 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1367 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1368
1369 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1370 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1371 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1372
John W. Linville8ed66f02015-10-26 17:01:44 -04001373 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001374 metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001375}
1376
John W. Linville2d07dc72015-05-13 12:57:30 -04001377static void geneve_dellink(struct net_device *dev, struct list_head *head)
1378{
1379 struct geneve_dev *geneve = netdev_priv(dev);
1380
John W. Linville2d07dc72015-05-13 12:57:30 -04001381 list_del(&geneve->next);
1382 unregister_netdevice_queue(dev, head);
1383}
1384
1385static size_t geneve_get_size(const struct net_device *dev)
1386{
1387 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001388 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001389 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001390 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001391 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001392 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001393 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1394 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1395 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001396 0;
1397}
1398
1399static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1400{
1401 struct geneve_dev *geneve = netdev_priv(dev);
1402 __u32 vni;
1403
1404 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1405 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1406 goto nla_put_failure;
1407
John W. Linville8ed66f02015-10-26 17:01:44 -04001408 if (geneve->remote.sa.sa_family == AF_INET) {
1409 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1410 geneve->remote.sin.sin_addr.s_addr))
1411 goto nla_put_failure;
1412#if IS_ENABLED(CONFIG_IPV6)
1413 } else {
1414 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1415 &geneve->remote.sin6.sin6_addr))
1416 goto nla_put_failure;
1417#endif
1418 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001419
John W. Linvilled8951122015-06-01 15:51:35 -04001420 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1421 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
John W. Linville8760ce52015-06-01 15:51:34 -04001422 goto nla_put_failure;
1423
John W. Linville7bbe33f2015-09-22 13:09:32 -04001424 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001425 goto nla_put_failure;
1426
Pravin B Shelare305ac62015-08-26 23:46:52 -07001427 if (geneve->collect_md) {
1428 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1429 goto nla_put_failure;
1430 }
1431
Tom Herbertabe492b2015-12-10 12:37:45 -08001432 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1433 !!(geneve->flags & GENEVE_F_UDP_CSUM)) ||
1434 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1435 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1436 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1437 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1438 goto nla_put_failure;
1439
John W. Linville2d07dc72015-05-13 12:57:30 -04001440 return 0;
1441
1442nla_put_failure:
1443 return -EMSGSIZE;
1444}
1445
1446static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1447 .kind = "geneve",
1448 .maxtype = IFLA_GENEVE_MAX,
1449 .policy = geneve_policy,
1450 .priv_size = sizeof(struct geneve_dev),
1451 .setup = geneve_setup,
1452 .validate = geneve_validate,
1453 .newlink = geneve_newlink,
1454 .dellink = geneve_dellink,
1455 .get_size = geneve_get_size,
1456 .fill_info = geneve_fill_info,
1457};
1458
Pravin B Shelare305ac62015-08-26 23:46:52 -07001459struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1460 u8 name_assign_type, u16 dst_port)
1461{
1462 struct nlattr *tb[IFLA_MAX + 1];
1463 struct net_device *dev;
1464 int err;
1465
1466 memset(tb, 0, sizeof(tb));
1467 dev = rtnl_create_link(net, name, name_assign_type,
1468 &geneve_link_ops, tb);
1469 if (IS_ERR(dev))
1470 return dev;
1471
John W. Linville8ed66f02015-10-26 17:01:44 -04001472 err = geneve_configure(net, dev, &geneve_remote_unspec,
Paolo Abenic868ee72016-02-17 19:30:01 +01001473 0, 0, 0, htons(dst_port), true,
1474 GENEVE_F_UDP_ZERO_CSUM6_RX);
David Wragg7e059152016-02-10 00:05:58 +00001475 if (err)
1476 goto err;
1477
1478 /* openvswitch users expect packet sizes to be unrestricted,
1479 * so set the largest MTU we can.
1480 */
David Wraggaeee0e62016-02-18 17:43:29 +00001481 err = __geneve_change_mtu(dev, IP_MAX_MTU, false);
David Wragg7e059152016-02-10 00:05:58 +00001482 if (err)
1483 goto err;
1484
Pravin B Shelare305ac62015-08-26 23:46:52 -07001485 return dev;
David Wragg7e059152016-02-10 00:05:58 +00001486
1487 err:
1488 free_netdev(dev);
1489 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001490}
1491EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1492
John W. Linville2d07dc72015-05-13 12:57:30 -04001493static __net_init int geneve_init_net(struct net *net)
1494{
1495 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001496
1497 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001498 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001499 return 0;
1500}
1501
1502static void __net_exit geneve_exit_net(struct net *net)
1503{
1504 struct geneve_net *gn = net_generic(net, geneve_net_id);
1505 struct geneve_dev *geneve, *next;
1506 struct net_device *dev, *aux;
1507 LIST_HEAD(list);
1508
1509 rtnl_lock();
1510
1511 /* gather any geneve devices that were moved into this ns */
1512 for_each_netdev_safe(net, dev, aux)
1513 if (dev->rtnl_link_ops == &geneve_link_ops)
1514 unregister_netdevice_queue(dev, &list);
1515
1516 /* now gather any other geneve devices that were created in this ns */
1517 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1518 /* If geneve->dev is in the same netns, it was already added
1519 * to the list by the previous loop.
1520 */
1521 if (!net_eq(dev_net(geneve->dev), net))
1522 unregister_netdevice_queue(geneve->dev, &list);
1523 }
1524
1525 /* unregister the devices gathered above */
1526 unregister_netdevice_many(&list);
1527 rtnl_unlock();
1528}
1529
1530static struct pernet_operations geneve_net_ops = {
1531 .init = geneve_init_net,
1532 .exit = geneve_exit_net,
1533 .id = &geneve_net_id,
1534 .size = sizeof(struct geneve_net),
1535};
1536
1537static int __init geneve_init_module(void)
1538{
1539 int rc;
1540
1541 rc = register_pernet_subsys(&geneve_net_ops);
1542 if (rc)
1543 goto out1;
1544
1545 rc = rtnl_link_register(&geneve_link_ops);
1546 if (rc)
1547 goto out2;
1548
1549 return 0;
1550out2:
1551 unregister_pernet_subsys(&geneve_net_ops);
1552out1:
1553 return rc;
1554}
1555late_initcall(geneve_init_module);
1556
1557static void __exit geneve_cleanup_module(void)
1558{
1559 rtnl_link_unregister(&geneve_link_ops);
1560 unregister_pernet_subsys(&geneve_net_ops);
1561}
1562module_exit(geneve_cleanup_module);
1563
1564MODULE_LICENSE("GPL");
1565MODULE_VERSION(GENEVE_NETDEV_VER);
1566MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1567MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1568MODULE_ALIAS_RTNL_LINK("geneve");