blob: de5c30c9f059203040cc47e83cce76ae9e5bfde6 [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;
John W. Linville2d07dc72015-05-13 12:57:30 -040074};
75
Pravin B Shelar371bd102015-08-26 23:46:54 -070076struct geneve_sock {
77 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070078 struct list_head list;
79 struct socket *sock;
80 struct rcu_head rcu;
81 int refcnt;
82 struct udp_offload udp_offloads;
Pravin B Shelar66d47002015-08-26 23:46:55 -070083 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070084};
John W. Linville2d07dc72015-05-13 12:57:30 -040085
86static inline __u32 geneve_net_vni_hash(u8 vni[3])
87{
88 __u32 vnid;
89
90 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
91 return hash_32(vnid, VNI_HASH_BITS);
92}
93
Pravin B Shelare305ac62015-08-26 23:46:52 -070094static __be64 vni_to_tunnel_id(const __u8 *vni)
95{
96#ifdef __BIG_ENDIAN
97 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
98#else
99 return (__force __be64)(((__force u64)vni[0] << 40) |
100 ((__force u64)vni[1] << 48) |
101 ((__force u64)vni[2] << 56));
102#endif
103}
104
Pravin B Shelar66d47002015-08-26 23:46:55 -0700105static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700106 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400107{
John W. Linville2d07dc72015-05-13 12:57:30 -0400108 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700109 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400110 __u32 hash;
111
John W. Linville2d07dc72015-05-13 12:57:30 -0400112 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700113 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700114 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700115 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700116 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400117 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700118 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400119 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700120 return NULL;
121}
122
John W. Linville8ed66f02015-10-26 17:01:44 -0400123#if IS_ENABLED(CONFIG_IPV6)
124static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
125 struct in6_addr addr6, u8 vni[])
126{
127 struct hlist_head *vni_list_head;
128 struct geneve_dev *geneve;
129 __u32 hash;
130
131 /* Find the device for this VNI */
132 hash = geneve_net_vni_hash(vni);
133 vni_list_head = &gs->vni_list[hash];
134 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
135 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
136 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
137 return geneve;
138 }
139 return NULL;
140}
141#endif
142
Pravin B Shelar371bd102015-08-26 23:46:54 -0700143static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
144{
145 return (struct genevehdr *)(udp_hdr(skb) + 1);
146}
147
Pravin B Shelare305ac62015-08-26 23:46:52 -0700148/* geneve receive/decap routine */
149static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
150{
151 struct genevehdr *gnvh = geneve_hdr(skb);
152 struct metadata_dst *tun_dst = NULL;
153 struct geneve_dev *geneve = NULL;
154 struct pcpu_sw_netstats *stats;
John W. Linville8ed66f02015-10-26 17:01:44 -0400155 struct iphdr *iph = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700156 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400157 static u8 zero_vni[3];
158 u8 *vni;
159 int err = 0;
160 sa_family_t sa_family;
161#if IS_ENABLED(CONFIG_IPV6)
162 struct ipv6hdr *ip6h = NULL;
163 struct in6_addr addr6;
164 static struct in6_addr zero_addr6;
165#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700166
John W. Linville8ed66f02015-10-26 17:01:44 -0400167 sa_family = gs->sock->sk->sk_family;
John W. Linville08399ef2015-09-21 10:29:09 -0400168
John W. Linville8ed66f02015-10-26 17:01:44 -0400169 if (sa_family == AF_INET) {
170 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700171
John W. Linville8ed66f02015-10-26 17:01:44 -0400172 if (gs->collect_md) {
173 vni = zero_vni;
174 addr = 0;
175 } else {
176 vni = gnvh->vni;
177
178 addr = iph->saddr;
179 }
180
181 geneve = geneve_lookup(gs, addr, vni);
182#if IS_ENABLED(CONFIG_IPV6)
183 } else if (sa_family == AF_INET6) {
184 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
185
186 if (gs->collect_md) {
187 vni = zero_vni;
188 addr6 = zero_addr6;
189 } else {
190 vni = gnvh->vni;
191
192 addr6 = ip6h->saddr;
193 }
194
195 geneve = geneve6_lookup(gs, addr6, vni);
196#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700197 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400198 if (!geneve)
199 goto drop;
200
Pravin B Shelar371bd102015-08-26 23:46:54 -0700201 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700202 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700203
204 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
205 (gnvh->oam ? TUNNEL_OAM : 0) |
206 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
207
John W. Linville8ed66f02015-10-26 17:01:44 -0400208 tun_dst = udp_tun_rx_dst(skb, sa_family, flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700209 vni_to_tunnel_id(gnvh->vni),
210 gnvh->opt_len * 4);
211 if (!tun_dst)
212 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700213 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700214 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
215 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700216 } else {
217 /* Drop packets w/ critical options,
218 * since we don't support any...
219 */
220 if (gnvh->critical)
221 goto drop;
222 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400223
224 skb_reset_mac_header(skb);
225 skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
226 skb->protocol = eth_type_trans(skb, geneve->dev);
227 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
228
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229 if (tun_dst)
230 skb_dst_set(skb, &tun_dst->dst);
231
John W. Linville2d07dc72015-05-13 12:57:30 -0400232 /* Ignore packet loops (and multicast echo) */
233 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
234 goto drop;
235
236 skb_reset_network_header(skb);
237
John W. Linville8ed66f02015-10-26 17:01:44 -0400238 if (iph)
239 err = IP_ECN_decapsulate(iph, skb);
240#if IS_ENABLED(CONFIG_IPV6)
241 if (ip6h)
242 err = IP6_ECN_decapsulate(ip6h, skb);
243#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400244
245 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400246 if (log_ecn_error) {
247 if (iph)
248 net_info_ratelimited("non-ECT from %pI4 "
249 "with TOS=%#x\n",
250 &iph->saddr, iph->tos);
251#if IS_ENABLED(CONFIG_IPV6)
252 if (ip6h)
253 net_info_ratelimited("non-ECT from %pI6\n",
254 &ip6h->saddr);
255#endif
256 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400257 if (err > 1) {
258 ++geneve->dev->stats.rx_frame_errors;
259 ++geneve->dev->stats.rx_errors;
260 goto drop;
261 }
262 }
263
264 stats = this_cpu_ptr(geneve->dev->tstats);
265 u64_stats_update_begin(&stats->syncp);
266 stats->rx_packets++;
267 stats->rx_bytes += skb->len;
268 u64_stats_update_end(&stats->syncp);
269
Jesse Gross8e816df2015-08-28 16:54:40 -0700270 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400271 return;
272drop:
273 /* Consume bad packet */
274 kfree_skb(skb);
275}
276
277/* Setup stats when device is created */
278static int geneve_init(struct net_device *dev)
279{
Jesse Gross8e816df2015-08-28 16:54:40 -0700280 struct geneve_dev *geneve = netdev_priv(dev);
281 int err;
282
John W. Linville2d07dc72015-05-13 12:57:30 -0400283 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
284 if (!dev->tstats)
285 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700286
287 err = gro_cells_init(&geneve->gro_cells, dev);
288 if (err) {
289 free_percpu(dev->tstats);
290 return err;
291 }
292
John W. Linville2d07dc72015-05-13 12:57:30 -0400293 return 0;
294}
295
296static void geneve_uninit(struct net_device *dev)
297{
Jesse Gross8e816df2015-08-28 16:54:40 -0700298 struct geneve_dev *geneve = netdev_priv(dev);
299
300 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400301 free_percpu(dev->tstats);
302}
303
Pravin B Shelar371bd102015-08-26 23:46:54 -0700304/* Callback from net/ipv4/udp.c to receive packets */
305static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
306{
307 struct genevehdr *geneveh;
308 struct geneve_sock *gs;
309 int opts_len;
310
311 /* Need Geneve and inner Ethernet header to be present */
312 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
313 goto error;
314
315 /* Return packets with reserved bits set */
316 geneveh = geneve_hdr(skb);
317 if (unlikely(geneveh->ver != GENEVE_VER))
318 goto error;
319
320 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
321 goto error;
322
323 opts_len = geneveh->opt_len * 4;
324 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
325 htons(ETH_P_TEB)))
326 goto drop;
327
328 gs = rcu_dereference_sk_user_data(sk);
329 if (!gs)
330 goto drop;
331
332 geneve_rx(gs, skb);
333 return 0;
334
335drop:
336 /* Consume bad packet */
337 kfree_skb(skb);
338 return 0;
339
340error:
341 /* Let the UDP layer deal with the skb */
342 return 1;
343}
344
345static struct socket *geneve_create_sock(struct net *net, bool ipv6,
346 __be16 port)
347{
348 struct socket *sock;
349 struct udp_port_cfg udp_conf;
350 int err;
351
352 memset(&udp_conf, 0, sizeof(udp_conf));
353
354 if (ipv6) {
355 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400356 udp_conf.ipv6_v6only = 1;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700357 } else {
358 udp_conf.family = AF_INET;
359 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
360 }
361
362 udp_conf.local_udp_port = port;
363
364 /* Open UDP socket */
365 err = udp_sock_create(net, &udp_conf, &sock);
366 if (err < 0)
367 return ERR_PTR(err);
368
369 return sock;
370}
371
372static void geneve_notify_add_rx_port(struct geneve_sock *gs)
373{
374 struct sock *sk = gs->sock->sk;
375 sa_family_t sa_family = sk->sk_family;
376 int err;
377
378 if (sa_family == AF_INET) {
379 err = udp_add_offload(&gs->udp_offloads);
380 if (err)
381 pr_warn("geneve: udp_add_offload failed with status %d\n",
382 err);
383 }
384}
385
386static int geneve_hlen(struct genevehdr *gh)
387{
388 return sizeof(*gh) + gh->opt_len * 4;
389}
390
391static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
392 struct sk_buff *skb,
393 struct udp_offload *uoff)
394{
395 struct sk_buff *p, **pp = NULL;
396 struct genevehdr *gh, *gh2;
397 unsigned int hlen, gh_len, off_gnv;
398 const struct packet_offload *ptype;
399 __be16 type;
400 int flush = 1;
401
402 off_gnv = skb_gro_offset(skb);
403 hlen = off_gnv + sizeof(*gh);
404 gh = skb_gro_header_fast(skb, off_gnv);
405 if (skb_gro_header_hard(skb, hlen)) {
406 gh = skb_gro_header_slow(skb, hlen, off_gnv);
407 if (unlikely(!gh))
408 goto out;
409 }
410
411 if (gh->ver != GENEVE_VER || gh->oam)
412 goto out;
413 gh_len = geneve_hlen(gh);
414
415 hlen = off_gnv + gh_len;
416 if (skb_gro_header_hard(skb, hlen)) {
417 gh = skb_gro_header_slow(skb, hlen, off_gnv);
418 if (unlikely(!gh))
419 goto out;
420 }
421
422 flush = 0;
423
424 for (p = *head; p; p = p->next) {
425 if (!NAPI_GRO_CB(p)->same_flow)
426 continue;
427
428 gh2 = (struct genevehdr *)(p->data + off_gnv);
429 if (gh->opt_len != gh2->opt_len ||
430 memcmp(gh, gh2, gh_len)) {
431 NAPI_GRO_CB(p)->same_flow = 0;
432 continue;
433 }
434 }
435
436 type = gh->proto_type;
437
438 rcu_read_lock();
439 ptype = gro_find_receive_by_type(type);
440 if (!ptype) {
441 flush = 1;
442 goto out_unlock;
443 }
444
445 skb_gro_pull(skb, gh_len);
446 skb_gro_postpull_rcsum(skb, gh, gh_len);
447 pp = ptype->callbacks.gro_receive(head, skb);
448
449out_unlock:
450 rcu_read_unlock();
451out:
452 NAPI_GRO_CB(skb)->flush |= flush;
453
454 return pp;
455}
456
457static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
458 struct udp_offload *uoff)
459{
460 struct genevehdr *gh;
461 struct packet_offload *ptype;
462 __be16 type;
463 int gh_len;
464 int err = -ENOSYS;
465
466 udp_tunnel_gro_complete(skb, nhoff);
467
468 gh = (struct genevehdr *)(skb->data + nhoff);
469 gh_len = geneve_hlen(gh);
470 type = gh->proto_type;
471
472 rcu_read_lock();
473 ptype = gro_find_complete_by_type(type);
474 if (ptype)
475 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
476
477 rcu_read_unlock();
478 return err;
479}
480
481/* Create new listen socket if needed */
482static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
483 bool ipv6)
484{
485 struct geneve_net *gn = net_generic(net, geneve_net_id);
486 struct geneve_sock *gs;
487 struct socket *sock;
488 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700489 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700490
491 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
492 if (!gs)
493 return ERR_PTR(-ENOMEM);
494
495 sock = geneve_create_sock(net, ipv6, port);
496 if (IS_ERR(sock)) {
497 kfree(gs);
498 return ERR_CAST(sock);
499 }
500
501 gs->sock = sock;
502 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700503 for (h = 0; h < VNI_HASH_SIZE; ++h)
504 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700505
506 /* Initialize the geneve udp offloads structure */
507 gs->udp_offloads.port = port;
508 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
509 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
510 geneve_notify_add_rx_port(gs);
511
512 /* Mark socket as an encapsulation socket */
513 tunnel_cfg.sk_user_data = gs;
514 tunnel_cfg.encap_type = 1;
515 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
516 tunnel_cfg.encap_destroy = NULL;
517 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700518 list_add(&gs->list, &gn->sock_list);
519 return gs;
520}
521
522static void geneve_notify_del_rx_port(struct geneve_sock *gs)
523{
524 struct sock *sk = gs->sock->sk;
525 sa_family_t sa_family = sk->sk_family;
526
527 if (sa_family == AF_INET)
528 udp_del_offload(&gs->udp_offloads);
529}
530
John W. Linville8ed66f02015-10-26 17:01:44 -0400531static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700532{
John W. Linville8ed66f02015-10-26 17:01:44 -0400533 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534 return;
535
536 list_del(&gs->list);
537 geneve_notify_del_rx_port(gs);
538 udp_tunnel_sock_release(gs->sock);
539 kfree_rcu(gs, rcu);
540}
541
John W. Linville8ed66f02015-10-26 17:01:44 -0400542static void geneve_sock_release(struct geneve_dev *geneve)
543{
544 __geneve_sock_release(geneve->sock4);
545#if IS_ENABLED(CONFIG_IPV6)
546 __geneve_sock_release(geneve->sock6);
547#endif
548}
549
Pravin B Shelar371bd102015-08-26 23:46:54 -0700550static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400551 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700552 __be16 dst_port)
553{
554 struct geneve_sock *gs;
555
556 list_for_each_entry(gs, &gn->sock_list, list) {
557 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400558 inet_sk(gs->sock->sk)->sk.sk_family == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700559 return gs;
560 }
561 }
562 return NULL;
563}
564
John W. Linville8ed66f02015-10-26 17:01:44 -0400565static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400566{
John W. Linville2d07dc72015-05-13 12:57:30 -0400567 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700568 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400569 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700570 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400571
John W. Linville8ed66f02015-10-26 17:01:44 -0400572 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700573 if (gs) {
574 gs->refcnt++;
575 goto out;
576 }
577
John W. Linville8ed66f02015-10-26 17:01:44 -0400578 gs = geneve_socket_create(net, geneve->dst_port, ipv6);
John W. Linville2d07dc72015-05-13 12:57:30 -0400579 if (IS_ERR(gs))
580 return PTR_ERR(gs);
581
Pravin B Shelar371bd102015-08-26 23:46:54 -0700582out:
583 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400584#if IS_ENABLED(CONFIG_IPV6)
585 if (ipv6)
586 geneve->sock6 = gs;
587 else
588#endif
589 geneve->sock4 = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700590
591 hash = geneve_net_vni_hash(geneve->vni);
592 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400593 return 0;
594}
595
John W. Linville8ed66f02015-10-26 17:01:44 -0400596static int geneve_open(struct net_device *dev)
597{
598 struct geneve_dev *geneve = netdev_priv(dev);
599 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
600 bool metadata = geneve->collect_md;
601 int ret = 0;
602
603 geneve->sock4 = NULL;
604#if IS_ENABLED(CONFIG_IPV6)
605 geneve->sock6 = NULL;
606 if (ipv6 || metadata)
607 ret = geneve_sock_add(geneve, true);
608#endif
609 if (!ret && (!ipv6 || metadata))
610 ret = geneve_sock_add(geneve, false);
611 if (ret < 0)
612 geneve_sock_release(geneve);
613
614 return ret;
615}
616
John W. Linville2d07dc72015-05-13 12:57:30 -0400617static int geneve_stop(struct net_device *dev)
618{
619 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400620
Pravin B Shelar66d47002015-08-26 23:46:55 -0700621 if (!hlist_unhashed(&geneve->hlist))
622 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400623 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400624 return 0;
625}
626
John W. Linville8ed66f02015-10-26 17:01:44 -0400627static void geneve_build_header(struct genevehdr *geneveh,
628 __be16 tun_flags, u8 vni[3],
629 u8 options_len, u8 *options)
630{
631 geneveh->ver = GENEVE_VER;
632 geneveh->opt_len = options_len / 4;
633 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
634 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
635 geneveh->rsvd1 = 0;
636 memcpy(geneveh->vni, vni, 3);
637 geneveh->proto_type = htons(ETH_P_TEB);
638 geneveh->rsvd2 = 0;
639
640 memcpy(geneveh->options, options, options_len);
641}
642
Pravin B Shelar371bd102015-08-26 23:46:54 -0700643static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
644 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
John W. Linville8ed66f02015-10-26 17:01:44 -0400645 bool csum, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700646{
647 struct genevehdr *gnvh;
648 int min_headroom;
649 int err;
650
John W. Linville8ed66f02015-10-26 17:01:44 -0400651 skb_scrub_packet(skb, xnet);
652
Pravin B Shelar371bd102015-08-26 23:46:54 -0700653 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
654 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
655 err = skb_cow_head(skb, min_headroom);
656 if (unlikely(err)) {
657 kfree_skb(skb);
658 goto free_rt;
659 }
660
661 skb = udp_tunnel_handle_offloads(skb, csum);
662 if (IS_ERR(skb)) {
663 err = PTR_ERR(skb);
664 goto free_rt;
665 }
666
667 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400668 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700669
670 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
671 return 0;
672
673free_rt:
674 ip_rt_put(rt);
675 return err;
676}
677
John W. Linville8ed66f02015-10-26 17:01:44 -0400678#if IS_ENABLED(CONFIG_IPV6)
679static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
680 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
681 bool csum, bool xnet)
682{
683 struct genevehdr *gnvh;
684 int min_headroom;
685 int err;
686
687 skb_scrub_packet(skb, xnet);
688
689 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
690 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
691 err = skb_cow_head(skb, min_headroom);
692 if (unlikely(err)) {
693 kfree_skb(skb);
694 goto free_dst;
695 }
696
697 skb = udp_tunnel_handle_offloads(skb, csum);
698 if (IS_ERR(skb)) {
699 err = PTR_ERR(skb);
700 goto free_dst;
701 }
702
703 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
704 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
705
706 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
707 return 0;
708
709free_dst:
710 dst_release(dst);
711 return err;
712}
713#endif
714
715static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
716 struct net_device *dev,
717 struct flowi4 *fl4,
718 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700719{
720 struct geneve_dev *geneve = netdev_priv(dev);
721 struct rtable *rt = NULL;
722 __u8 tos;
723
724 memset(fl4, 0, sizeof(*fl4));
725 fl4->flowi4_mark = skb->mark;
726 fl4->flowi4_proto = IPPROTO_UDP;
727
728 if (info) {
729 fl4->daddr = info->key.u.ipv4.dst;
730 fl4->saddr = info->key.u.ipv4.src;
731 fl4->flowi4_tos = RT_TOS(info->key.tos);
732 } else {
733 tos = geneve->tos;
734 if (tos == 1) {
735 const struct iphdr *iip = ip_hdr(skb);
736
737 tos = ip_tunnel_get_dsfield(iip, skb);
738 }
739
740 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400741 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700742 }
743
744 rt = ip_route_output_key(geneve->net, fl4);
745 if (IS_ERR(rt)) {
746 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700747 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700748 }
749 if (rt->dst.dev == dev) { /* is this necessary? */
750 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700751 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700752 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700753 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754 return rt;
755}
756
John W. Linville8ed66f02015-10-26 17:01:44 -0400757#if IS_ENABLED(CONFIG_IPV6)
758static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
759 struct net_device *dev,
760 struct flowi6 *fl6,
761 struct ip_tunnel_info *info)
762{
763 struct geneve_dev *geneve = netdev_priv(dev);
764 struct geneve_sock *gs6 = geneve->sock6;
765 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400766 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400767
768 memset(fl6, 0, sizeof(*fl6));
769 fl6->flowi6_mark = skb->mark;
770 fl6->flowi6_proto = IPPROTO_UDP;
771
772 if (info) {
773 fl6->daddr = info->key.u.ipv6.dst;
774 fl6->saddr = info->key.u.ipv6.src;
John W. Linville3a56f862015-10-26 17:01:45 -0400775 fl6->flowi6_tos = RT_TOS(info->key.tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400776 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400777 prio = geneve->tos;
778 if (prio == 1) {
779 const struct iphdr *iip = ip_hdr(skb);
780
781 prio = ip_tunnel_get_dsfield(iip, skb);
782 }
783
784 fl6->flowi6_tos = RT_TOS(prio);
John W. Linville8ed66f02015-10-26 17:01:44 -0400785 fl6->daddr = geneve->remote.sin6.sin6_addr;
786 }
787
788 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
789 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
790 return ERR_PTR(-ENETUNREACH);
791 }
792 if (dst->dev == dev) { /* is this necessary? */
793 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
794 dst_release(dst);
795 return ERR_PTR(-ELOOP);
796 }
797
798 return dst;
799}
800#endif
801
Pravin B Shelare305ac62015-08-26 23:46:52 -0700802/* Convert 64 bit tunnel ID to 24 bit VNI. */
803static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
804{
805#ifdef __BIG_ENDIAN
806 vni[0] = (__force __u8)(tun_id >> 16);
807 vni[1] = (__force __u8)(tun_id >> 8);
808 vni[2] = (__force __u8)tun_id;
809#else
810 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
811 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
812 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
813#endif
814}
815
John W. Linville8ed66f02015-10-26 17:01:44 -0400816static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
817 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400818{
819 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400820 struct geneve_sock *gs4 = geneve->sock4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400821 struct rtable *rt = NULL;
John W. Linville08399ef2015-09-21 10:29:09 -0400822 const struct iphdr *iip; /* interior IP header */
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700823 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400824 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400825 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700826 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700827 bool udp_csum;
828 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400829 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
John W. Linville2d07dc72015-05-13 12:57:30 -0400830
Pravin B Shelare305ac62015-08-26 23:46:52 -0700831 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400832 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700833 netdev_dbg(dev, "no tunnel metadata\n");
834 goto tx_error;
835 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200836 if (info && ip_tunnel_info_af(info) != AF_INET)
837 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700838 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400839
John W. Linville8ed66f02015-10-26 17:01:44 -0400840 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400841 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700842 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400843 goto tx_error;
844 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700845
846 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700847 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700848
John W. Linville08399ef2015-09-21 10:29:09 -0400849 iip = ip_hdr(skb);
850
Pravin B Shelare305ac62015-08-26 23:46:52 -0700851 if (info) {
852 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700853 u8 *opts = NULL;
854 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700855
856 tunnel_id_to_vni(key->tun_id, vni);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700857 if (key->tun_flags & TUNNEL_GENEVE_OPT)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700858 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700859
Pravin B Shelar371bd102015-08-26 23:46:54 -0700860 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
861 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
John W. Linville8ed66f02015-10-26 17:01:44 -0400862 info->options_len, opts, udp_csum, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700863 if (unlikely(err))
864 goto err;
865
John W. Linville08399ef2015-09-21 10:29:09 -0400866 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700867 ttl = key->ttl;
868 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700869 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700870 udp_csum = false;
871 err = geneve_build_skb(rt, skb, 0, geneve->vni,
John W. Linville8ed66f02015-10-26 17:01:44 -0400872 0, NULL, udp_csum, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700873 if (unlikely(err))
874 goto err;
875
Pravin B Shelare305ac62015-08-26 23:46:52 -0700876 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700877 ttl = geneve->ttl;
878 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
879 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700880 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700881 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400882 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400883 err = udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700884 tos, ttl, df, sport, geneve->dst_port,
885 !net_eq(geneve->net, dev_net(geneve->dev)),
886 !udp_csum);
John W. Linville2d07dc72015-05-13 12:57:30 -0400887
888 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
John W. Linville2d07dc72015-05-13 12:57:30 -0400889 return NETDEV_TX_OK;
890
John W. Linville2d07dc72015-05-13 12:57:30 -0400891tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400892 dev_kfree_skb(skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700893err:
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700894 if (err == -ELOOP)
895 dev->stats.collisions++;
896 else if (err == -ENETUNREACH)
897 dev->stats.tx_carrier_errors++;
898 else
899 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400900 return NETDEV_TX_OK;
901}
902
John W. Linville8ed66f02015-10-26 17:01:44 -0400903#if IS_ENABLED(CONFIG_IPV6)
904static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
905 struct ip_tunnel_info *info)
906{
907 struct geneve_dev *geneve = netdev_priv(dev);
908 struct geneve_sock *gs6 = geneve->sock6;
909 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400910 const struct iphdr *iip; /* interior IP header */
John W. Linville8ed66f02015-10-26 17:01:44 -0400911 int err = -EINVAL;
912 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400913 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400914 __be16 sport;
915 bool udp_csum;
916 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
917
918 if (geneve->collect_md) {
919 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
920 netdev_dbg(dev, "no tunnel metadata\n");
921 goto tx_error;
922 }
923 }
924
925 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
926 if (IS_ERR(dst)) {
927 err = PTR_ERR(dst);
928 goto tx_error;
929 }
930
931 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
932 skb_reset_mac_header(skb);
933
John W. Linville3a56f862015-10-26 17:01:45 -0400934 iip = ip_hdr(skb);
935
John W. Linville8ed66f02015-10-26 17:01:44 -0400936 if (info) {
937 const struct ip_tunnel_key *key = &info->key;
938 u8 *opts = NULL;
939 u8 vni[3];
940
941 tunnel_id_to_vni(key->tun_id, vni);
942 if (key->tun_flags & TUNNEL_GENEVE_OPT)
943 opts = ip_tunnel_info_opts(info);
944
945 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
946 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
947 info->options_len, opts,
948 udp_csum, xnet);
949 if (unlikely(err))
950 goto err;
951
John W. Linville3a56f862015-10-26 17:01:45 -0400952 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400953 ttl = key->ttl;
954 } else {
955 udp_csum = false;
956 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
957 0, NULL, udp_csum, xnet);
958 if (unlikely(err))
959 goto err;
960
John W. Linville3a56f862015-10-26 17:01:45 -0400961 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400962 ttl = geneve->ttl;
963 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
964 ttl = 1;
965 ttl = ttl ? : ip6_dst_hoplimit(dst);
966 }
967 err = udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
John W. Linville3a56f862015-10-26 17:01:45 -0400968 &fl6.saddr, &fl6.daddr, prio, ttl,
John W. Linville8ed66f02015-10-26 17:01:44 -0400969 sport, geneve->dst_port, !udp_csum);
970
971 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
972 return NETDEV_TX_OK;
973
974tx_error:
975 dev_kfree_skb(skb);
976err:
977 if (err == -ELOOP)
978 dev->stats.collisions++;
979 else if (err == -ENETUNREACH)
980 dev->stats.tx_carrier_errors++;
981 else
982 dev->stats.tx_errors++;
983 return NETDEV_TX_OK;
984}
985#endif
986
987static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
988{
989 struct geneve_dev *geneve = netdev_priv(dev);
990 struct ip_tunnel_info *info = NULL;
991
992 if (geneve->collect_md)
993 info = skb_tunnel_info(skb);
994
995#if IS_ENABLED(CONFIG_IPV6)
996 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
997 (!info && geneve->remote.sa.sa_family == AF_INET6))
998 return geneve6_xmit_skb(skb, dev, info);
999#endif
1000 return geneve_xmit_skb(skb, dev, info);
1001}
1002
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001003static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1004{
1005 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1006 struct geneve_dev *geneve = netdev_priv(dev);
1007 struct rtable *rt;
1008 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001009#if IS_ENABLED(CONFIG_IPV6)
1010 struct dst_entry *dst;
1011 struct flowi6 fl6;
1012#endif
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001013
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001014 if (ip_tunnel_info_af(info) == AF_INET) {
1015 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1016 if (IS_ERR(rt))
1017 return PTR_ERR(rt);
1018
1019 ip_rt_put(rt);
1020 info->key.u.ipv4.src = fl4.saddr;
1021#if IS_ENABLED(CONFIG_IPV6)
1022 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1023 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1024 if (IS_ERR(dst))
1025 return PTR_ERR(dst);
1026
1027 dst_release(dst);
1028 info->key.u.ipv6.src = fl6.saddr;
1029#endif
1030 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001031 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001032 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001033
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001034 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1035 1, USHRT_MAX, true);
1036 info->key.tp_dst = geneve->dst_port;
1037 return 0;
1038}
1039
John W. Linville2d07dc72015-05-13 12:57:30 -04001040static const struct net_device_ops geneve_netdev_ops = {
1041 .ndo_init = geneve_init,
1042 .ndo_uninit = geneve_uninit,
1043 .ndo_open = geneve_open,
1044 .ndo_stop = geneve_stop,
1045 .ndo_start_xmit = geneve_xmit,
1046 .ndo_get_stats64 = ip_tunnel_get_stats64,
1047 .ndo_change_mtu = eth_change_mtu,
1048 .ndo_validate_addr = eth_validate_addr,
1049 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001050 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001051};
1052
1053static void geneve_get_drvinfo(struct net_device *dev,
1054 struct ethtool_drvinfo *drvinfo)
1055{
1056 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1057 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1058}
1059
1060static const struct ethtool_ops geneve_ethtool_ops = {
1061 .get_drvinfo = geneve_get_drvinfo,
1062 .get_link = ethtool_op_get_link,
1063};
1064
1065/* Info for udev, that this is a virtual tunnel endpoint */
1066static struct device_type geneve_type = {
1067 .name = "geneve",
1068};
1069
1070/* Initialize the device structure. */
1071static void geneve_setup(struct net_device *dev)
1072{
1073 ether_setup(dev);
1074
1075 dev->netdev_ops = &geneve_netdev_ops;
1076 dev->ethtool_ops = &geneve_ethtool_ops;
1077 dev->destructor = free_netdev;
1078
1079 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1080
John W. Linville2d07dc72015-05-13 12:57:30 -04001081 dev->features |= NETIF_F_LLTX;
1082 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1083 dev->features |= NETIF_F_RXCSUM;
1084 dev->features |= NETIF_F_GSO_SOFTWARE;
1085
John W. Linville2d07dc72015-05-13 12:57:30 -04001086 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1087 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001088
1089 netif_keep_dst(dev);
Phil Suttered961ac2015-08-18 10:30:31 +02001090 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001091 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001092}
1093
1094static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1095 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1096 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001097 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001098 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001099 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001100 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001101 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
John W. Linville2d07dc72015-05-13 12:57:30 -04001102};
1103
1104static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1105{
1106 if (tb[IFLA_ADDRESS]) {
1107 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1108 return -EINVAL;
1109
1110 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1111 return -EADDRNOTAVAIL;
1112 }
1113
1114 if (!data)
1115 return -EINVAL;
1116
1117 if (data[IFLA_GENEVE_ID]) {
1118 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1119
1120 if (vni >= GENEVE_VID_MASK)
1121 return -ERANGE;
1122 }
1123
1124 return 0;
1125}
1126
Pravin B Shelar371bd102015-08-26 23:46:54 -07001127static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1128 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001129 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001130 u8 vni[],
1131 bool *tun_on_same_port,
1132 bool *tun_collect_md)
1133{
1134 struct geneve_dev *geneve, *t;
1135
1136 *tun_on_same_port = false;
1137 *tun_collect_md = false;
1138 t = NULL;
1139 list_for_each_entry(geneve, &gn->geneve_list, next) {
1140 if (geneve->dst_port == dst_port) {
1141 *tun_collect_md = geneve->collect_md;
1142 *tun_on_same_port = true;
1143 }
1144 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001145 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001146 dst_port == geneve->dst_port)
1147 t = geneve;
1148 }
1149 return t;
1150}
1151
Pravin B Shelare305ac62015-08-26 23:46:52 -07001152static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001153 union geneve_addr *remote,
1154 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
1155 bool metadata)
John W. Linville2d07dc72015-05-13 12:57:30 -04001156{
1157 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001158 struct geneve_dev *t, *geneve = netdev_priv(dev);
1159 bool tun_collect_md, tun_on_same_port;
John W. Linville2d07dc72015-05-13 12:57:30 -04001160 int err;
1161
John W. Linville8ed66f02015-10-26 17:01:44 -04001162 if (!remote)
1163 return -EINVAL;
1164 if (metadata &&
1165 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1166 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001167
1168 geneve->net = net;
1169 geneve->dev = dev;
1170
John W. Linville2d07dc72015-05-13 12:57:30 -04001171 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1172 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1173 geneve->vni[2] = vni & 0x000000ff;
1174
John W. Linville8ed66f02015-10-26 17:01:44 -04001175 if ((remote->sa.sa_family == AF_INET &&
1176 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1177 (remote->sa.sa_family == AF_INET6 &&
1178 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001179 return -EINVAL;
John W. Linville8ed66f02015-10-26 17:01:44 -04001180 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001181
Pravin B Shelare305ac62015-08-26 23:46:52 -07001182 geneve->ttl = ttl;
1183 geneve->tos = tos;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001184 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001185 geneve->collect_md = metadata;
1186
John W. Linville8ed66f02015-10-26 17:01:44 -04001187 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001188 &tun_on_same_port, &tun_collect_md);
1189 if (t)
1190 return -EBUSY;
1191
1192 if (metadata) {
1193 if (tun_on_same_port)
1194 return -EPERM;
1195 } else {
1196 if (tun_collect_md)
1197 return -EPERM;
1198 }
1199
John W. Linville2d07dc72015-05-13 12:57:30 -04001200 err = register_netdevice(dev);
1201 if (err)
1202 return err;
1203
1204 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001205 return 0;
1206}
1207
Pravin B Shelare305ac62015-08-26 23:46:52 -07001208static int geneve_newlink(struct net *net, struct net_device *dev,
1209 struct nlattr *tb[], struct nlattr *data[])
1210{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001211 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001212 __u8 ttl = 0, tos = 0;
1213 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001214 union geneve_addr remote = geneve_remote_unspec;
Jesse Grosse277de52015-10-16 16:36:00 -07001215 __u32 vni = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001216
John W. Linville8ed66f02015-10-26 17:01:44 -04001217 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1218 return -EINVAL;
1219
1220 if (data[IFLA_GENEVE_REMOTE]) {
1221 remote.sa.sa_family = AF_INET;
1222 remote.sin.sin_addr.s_addr =
1223 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1224 }
1225
1226 if (data[IFLA_GENEVE_REMOTE6]) {
1227 if (!IS_ENABLED(CONFIG_IPV6))
1228 return -EPFNOSUPPORT;
1229
1230 remote.sa.sa_family = AF_INET6;
1231 remote.sin6.sin6_addr =
1232 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1233
1234 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1235 IPV6_ADDR_LINKLOCAL) {
1236 netdev_dbg(dev, "link-local remote is unsupported\n");
1237 return -EINVAL;
1238 }
1239 }
1240
Jesse Grosse277de52015-10-16 16:36:00 -07001241 if (data[IFLA_GENEVE_ID])
1242 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001243
Pravin B Shelare305ac62015-08-26 23:46:52 -07001244 if (data[IFLA_GENEVE_TTL])
1245 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1246
1247 if (data[IFLA_GENEVE_TOS])
1248 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1249
1250 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001251 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001252
1253 if (data[IFLA_GENEVE_COLLECT_METADATA])
1254 metadata = true;
1255
John W. Linville8ed66f02015-10-26 17:01:44 -04001256 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
1257 metadata);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001258}
1259
John W. Linville2d07dc72015-05-13 12:57:30 -04001260static void geneve_dellink(struct net_device *dev, struct list_head *head)
1261{
1262 struct geneve_dev *geneve = netdev_priv(dev);
1263
John W. Linville2d07dc72015-05-13 12:57:30 -04001264 list_del(&geneve->next);
1265 unregister_netdevice_queue(dev, head);
1266}
1267
1268static size_t geneve_get_size(const struct net_device *dev)
1269{
1270 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001271 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001272 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001273 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001274 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001275 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
John W. Linville2d07dc72015-05-13 12:57:30 -04001276 0;
1277}
1278
1279static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1280{
1281 struct geneve_dev *geneve = netdev_priv(dev);
1282 __u32 vni;
1283
1284 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1285 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1286 goto nla_put_failure;
1287
John W. Linville8ed66f02015-10-26 17:01:44 -04001288 if (geneve->remote.sa.sa_family == AF_INET) {
1289 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1290 geneve->remote.sin.sin_addr.s_addr))
1291 goto nla_put_failure;
1292#if IS_ENABLED(CONFIG_IPV6)
1293 } else {
1294 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1295 &geneve->remote.sin6.sin6_addr))
1296 goto nla_put_failure;
1297#endif
1298 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001299
John W. Linvilled8951122015-06-01 15:51:35 -04001300 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1301 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
John W. Linville8760ce52015-06-01 15:51:34 -04001302 goto nla_put_failure;
1303
John W. Linville7bbe33f2015-09-22 13:09:32 -04001304 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001305 goto nla_put_failure;
1306
Pravin B Shelare305ac62015-08-26 23:46:52 -07001307 if (geneve->collect_md) {
1308 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1309 goto nla_put_failure;
1310 }
1311
John W. Linville2d07dc72015-05-13 12:57:30 -04001312 return 0;
1313
1314nla_put_failure:
1315 return -EMSGSIZE;
1316}
1317
1318static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1319 .kind = "geneve",
1320 .maxtype = IFLA_GENEVE_MAX,
1321 .policy = geneve_policy,
1322 .priv_size = sizeof(struct geneve_dev),
1323 .setup = geneve_setup,
1324 .validate = geneve_validate,
1325 .newlink = geneve_newlink,
1326 .dellink = geneve_dellink,
1327 .get_size = geneve_get_size,
1328 .fill_info = geneve_fill_info,
1329};
1330
Pravin B Shelare305ac62015-08-26 23:46:52 -07001331struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1332 u8 name_assign_type, u16 dst_port)
1333{
1334 struct nlattr *tb[IFLA_MAX + 1];
1335 struct net_device *dev;
1336 int err;
1337
1338 memset(tb, 0, sizeof(tb));
1339 dev = rtnl_create_link(net, name, name_assign_type,
1340 &geneve_link_ops, tb);
1341 if (IS_ERR(dev))
1342 return dev;
1343
John W. Linville8ed66f02015-10-26 17:01:44 -04001344 err = geneve_configure(net, dev, &geneve_remote_unspec,
1345 0, 0, 0, htons(dst_port), true);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001346 if (err) {
1347 free_netdev(dev);
1348 return ERR_PTR(err);
1349 }
1350 return dev;
1351}
1352EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1353
John W. Linville2d07dc72015-05-13 12:57:30 -04001354static __net_init int geneve_init_net(struct net *net)
1355{
1356 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001357
1358 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001359 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001360 return 0;
1361}
1362
1363static void __net_exit geneve_exit_net(struct net *net)
1364{
1365 struct geneve_net *gn = net_generic(net, geneve_net_id);
1366 struct geneve_dev *geneve, *next;
1367 struct net_device *dev, *aux;
1368 LIST_HEAD(list);
1369
1370 rtnl_lock();
1371
1372 /* gather any geneve devices that were moved into this ns */
1373 for_each_netdev_safe(net, dev, aux)
1374 if (dev->rtnl_link_ops == &geneve_link_ops)
1375 unregister_netdevice_queue(dev, &list);
1376
1377 /* now gather any other geneve devices that were created in this ns */
1378 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1379 /* If geneve->dev is in the same netns, it was already added
1380 * to the list by the previous loop.
1381 */
1382 if (!net_eq(dev_net(geneve->dev), net))
1383 unregister_netdevice_queue(geneve->dev, &list);
1384 }
1385
1386 /* unregister the devices gathered above */
1387 unregister_netdevice_many(&list);
1388 rtnl_unlock();
1389}
1390
1391static struct pernet_operations geneve_net_ops = {
1392 .init = geneve_init_net,
1393 .exit = geneve_exit_net,
1394 .id = &geneve_net_id,
1395 .size = sizeof(struct geneve_net),
1396};
1397
1398static int __init geneve_init_module(void)
1399{
1400 int rc;
1401
1402 rc = register_pernet_subsys(&geneve_net_ops);
1403 if (rc)
1404 goto out1;
1405
1406 rc = rtnl_link_register(&geneve_link_ops);
1407 if (rc)
1408 goto out2;
1409
1410 return 0;
1411out2:
1412 unregister_pernet_subsys(&geneve_net_ops);
1413out1:
1414 return rc;
1415}
1416late_initcall(geneve_init_module);
1417
1418static void __exit geneve_cleanup_module(void)
1419{
1420 rtnl_link_unregister(&geneve_link_ops);
1421 unregister_pernet_subsys(&geneve_net_ops);
1422}
1423module_exit(geneve_cleanup_module);
1424
1425MODULE_LICENSE("GPL");
1426MODULE_VERSION(GENEVE_NETDEV_VER);
1427MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1428MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1429MODULE_ALIAS_RTNL_LINK("geneve");