blob: 4357bae732d738f275788dde25cdc72a9ae0b622 [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>
John W. Linville2d07dc72015-05-13 12:57:30 -040019#include <net/rtnetlink.h>
20#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070021#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040022
23#define GENEVE_NETDEV_VER "0.6"
24
25#define GENEVE_UDP_PORT 6081
26
27#define GENEVE_N_VID (1u << 24)
28#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
29
30#define VNI_HASH_BITS 10
31#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
32
33static bool log_ecn_error = true;
34module_param(log_ecn_error, bool, 0644);
35MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
36
Pravin B Shelar371bd102015-08-26 23:46:54 -070037#define GENEVE_VER 0
38#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
39
John W. Linville2d07dc72015-05-13 12:57:30 -040040/* per-network namespace private data for this module */
41struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070042 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040044};
45
Pravin B Shelar371bd102015-08-26 23:46:54 -070046static int geneve_net_id;
47
John W. Linville2d07dc72015-05-13 12:57:30 -040048/* Pseudo network device */
49struct geneve_dev {
50 struct hlist_node hlist; /* vni hash table */
51 struct net *net; /* netns for packet i/o */
52 struct net_device *dev; /* netdev for geneve tunnel */
53 struct geneve_sock *sock; /* socket used for geneve tunnel */
John W. Linville8760ce52015-06-01 15:51:34 -040054 u8 vni[3]; /* virtual network ID for tunnel */
55 u8 ttl; /* TTL override */
John W. Linvilled8951122015-06-01 15:51:35 -040056 u8 tos; /* TOS override */
John W. Linville2d07dc72015-05-13 12:57:30 -040057 struct sockaddr_in remote; /* IPv4 address for link partner */
58 struct list_head next; /* geneve's per namespace list */
Pravin B Shelarcd7918b2015-08-26 23:46:51 -070059 __be16 dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -070060 bool collect_md;
John W. Linville2d07dc72015-05-13 12:57:30 -040061};
62
Pravin B Shelar371bd102015-08-26 23:46:54 -070063struct geneve_sock {
64 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070065 struct list_head list;
66 struct socket *sock;
67 struct rcu_head rcu;
68 int refcnt;
69 struct udp_offload udp_offloads;
Pravin B Shelar66d47002015-08-26 23:46:55 -070070 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070071};
John W. Linville2d07dc72015-05-13 12:57:30 -040072
73static inline __u32 geneve_net_vni_hash(u8 vni[3])
74{
75 __u32 vnid;
76
77 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
78 return hash_32(vnid, VNI_HASH_BITS);
79}
80
Pravin B Shelare305ac62015-08-26 23:46:52 -070081static __be64 vni_to_tunnel_id(const __u8 *vni)
82{
83#ifdef __BIG_ENDIAN
84 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
85#else
86 return (__force __be64)(((__force u64)vni[0] << 40) |
87 ((__force u64)vni[1] << 48) |
88 ((__force u64)vni[2] << 56));
89#endif
90}
91
Pravin B Shelar66d47002015-08-26 23:46:55 -070092static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -070093 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -040094{
John W. Linville2d07dc72015-05-13 12:57:30 -040095 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -070096 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -040097 __u32 hash;
98
John W. Linville2d07dc72015-05-13 12:57:30 -040099 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700100 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700101 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700102 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700103 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
Pravin B Shelar66d47002015-08-26 23:46:55 -0700104 addr == geneve->remote.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700105 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400106 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700107 return NULL;
108}
109
Pravin B Shelar371bd102015-08-26 23:46:54 -0700110static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
111{
112 return (struct genevehdr *)(udp_hdr(skb) + 1);
113}
114
Pravin B Shelare305ac62015-08-26 23:46:52 -0700115/* geneve receive/decap routine */
116static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
117{
118 struct genevehdr *gnvh = geneve_hdr(skb);
119 struct metadata_dst *tun_dst = NULL;
120 struct geneve_dev *geneve = NULL;
121 struct pcpu_sw_netstats *stats;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700122 struct iphdr *iph;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700123 u8 *vni;
124 __be32 addr;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700125 int err;
126
Pravin B Shelar371bd102015-08-26 23:46:54 -0700127 if (gs->collect_md) {
128 static u8 zero_vni[3];
129
130 vni = zero_vni;
131 addr = 0;
132 } else {
133 vni = gnvh->vni;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700134 iph = ip_hdr(skb); /* Still outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700135 addr = iph->saddr;
136 }
137
Pravin B Shelar66d47002015-08-26 23:46:55 -0700138 geneve = geneve_lookup(gs, addr, vni);
John W. Linville2d07dc72015-05-13 12:57:30 -0400139 if (!geneve)
140 goto drop;
141
Pravin B Shelar371bd102015-08-26 23:46:54 -0700142 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700143 __be16 flags;
144 void *opts;
145
146 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
147 (gnvh->oam ? TUNNEL_OAM : 0) |
148 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
149
150 tun_dst = udp_tun_rx_dst(skb, AF_INET, flags,
151 vni_to_tunnel_id(gnvh->vni),
152 gnvh->opt_len * 4);
153 if (!tun_dst)
154 goto drop;
155
156 /* Update tunnel dst according to Geneve options. */
157 opts = ip_tunnel_info_opts(&tun_dst->u.tun_info,
158 gnvh->opt_len * 4);
159 memcpy(opts, gnvh->options, gnvh->opt_len * 4);
160 } else {
161 /* Drop packets w/ critical options,
162 * since we don't support any...
163 */
164 if (gnvh->critical)
165 goto drop;
166 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400167
168 skb_reset_mac_header(skb);
169 skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
170 skb->protocol = eth_type_trans(skb, geneve->dev);
171 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
172
Pravin B Shelare305ac62015-08-26 23:46:52 -0700173 if (tun_dst)
174 skb_dst_set(skb, &tun_dst->dst);
175
John W. Linville2d07dc72015-05-13 12:57:30 -0400176 /* Ignore packet loops (and multicast echo) */
177 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
178 goto drop;
179
180 skb_reset_network_header(skb);
181
182 iph = ip_hdr(skb); /* Now inner IP header... */
183 err = IP_ECN_decapsulate(iph, skb);
184
185 if (unlikely(err)) {
186 if (log_ecn_error)
187 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
188 &iph->saddr, iph->tos);
189 if (err > 1) {
190 ++geneve->dev->stats.rx_frame_errors;
191 ++geneve->dev->stats.rx_errors;
192 goto drop;
193 }
194 }
195
196 stats = this_cpu_ptr(geneve->dev->tstats);
197 u64_stats_update_begin(&stats->syncp);
198 stats->rx_packets++;
199 stats->rx_bytes += skb->len;
200 u64_stats_update_end(&stats->syncp);
201
202 netif_rx(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400203 return;
204drop:
205 /* Consume bad packet */
206 kfree_skb(skb);
207}
208
209/* Setup stats when device is created */
210static int geneve_init(struct net_device *dev)
211{
212 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
213 if (!dev->tstats)
214 return -ENOMEM;
John W. Linville2d07dc72015-05-13 12:57:30 -0400215 return 0;
216}
217
218static void geneve_uninit(struct net_device *dev)
219{
220 free_percpu(dev->tstats);
221}
222
Pravin B Shelar371bd102015-08-26 23:46:54 -0700223/* Callback from net/ipv4/udp.c to receive packets */
224static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
225{
226 struct genevehdr *geneveh;
227 struct geneve_sock *gs;
228 int opts_len;
229
230 /* Need Geneve and inner Ethernet header to be present */
231 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
232 goto error;
233
234 /* Return packets with reserved bits set */
235 geneveh = geneve_hdr(skb);
236 if (unlikely(geneveh->ver != GENEVE_VER))
237 goto error;
238
239 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
240 goto error;
241
242 opts_len = geneveh->opt_len * 4;
243 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
244 htons(ETH_P_TEB)))
245 goto drop;
246
247 gs = rcu_dereference_sk_user_data(sk);
248 if (!gs)
249 goto drop;
250
251 geneve_rx(gs, skb);
252 return 0;
253
254drop:
255 /* Consume bad packet */
256 kfree_skb(skb);
257 return 0;
258
259error:
260 /* Let the UDP layer deal with the skb */
261 return 1;
262}
263
264static struct socket *geneve_create_sock(struct net *net, bool ipv6,
265 __be16 port)
266{
267 struct socket *sock;
268 struct udp_port_cfg udp_conf;
269 int err;
270
271 memset(&udp_conf, 0, sizeof(udp_conf));
272
273 if (ipv6) {
274 udp_conf.family = AF_INET6;
275 } else {
276 udp_conf.family = AF_INET;
277 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
278 }
279
280 udp_conf.local_udp_port = port;
281
282 /* Open UDP socket */
283 err = udp_sock_create(net, &udp_conf, &sock);
284 if (err < 0)
285 return ERR_PTR(err);
286
287 return sock;
288}
289
290static void geneve_notify_add_rx_port(struct geneve_sock *gs)
291{
292 struct sock *sk = gs->sock->sk;
293 sa_family_t sa_family = sk->sk_family;
294 int err;
295
296 if (sa_family == AF_INET) {
297 err = udp_add_offload(&gs->udp_offloads);
298 if (err)
299 pr_warn("geneve: udp_add_offload failed with status %d\n",
300 err);
301 }
302}
303
304static int geneve_hlen(struct genevehdr *gh)
305{
306 return sizeof(*gh) + gh->opt_len * 4;
307}
308
309static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
310 struct sk_buff *skb,
311 struct udp_offload *uoff)
312{
313 struct sk_buff *p, **pp = NULL;
314 struct genevehdr *gh, *gh2;
315 unsigned int hlen, gh_len, off_gnv;
316 const struct packet_offload *ptype;
317 __be16 type;
318 int flush = 1;
319
320 off_gnv = skb_gro_offset(skb);
321 hlen = off_gnv + sizeof(*gh);
322 gh = skb_gro_header_fast(skb, off_gnv);
323 if (skb_gro_header_hard(skb, hlen)) {
324 gh = skb_gro_header_slow(skb, hlen, off_gnv);
325 if (unlikely(!gh))
326 goto out;
327 }
328
329 if (gh->ver != GENEVE_VER || gh->oam)
330 goto out;
331 gh_len = geneve_hlen(gh);
332
333 hlen = off_gnv + gh_len;
334 if (skb_gro_header_hard(skb, hlen)) {
335 gh = skb_gro_header_slow(skb, hlen, off_gnv);
336 if (unlikely(!gh))
337 goto out;
338 }
339
340 flush = 0;
341
342 for (p = *head; p; p = p->next) {
343 if (!NAPI_GRO_CB(p)->same_flow)
344 continue;
345
346 gh2 = (struct genevehdr *)(p->data + off_gnv);
347 if (gh->opt_len != gh2->opt_len ||
348 memcmp(gh, gh2, gh_len)) {
349 NAPI_GRO_CB(p)->same_flow = 0;
350 continue;
351 }
352 }
353
354 type = gh->proto_type;
355
356 rcu_read_lock();
357 ptype = gro_find_receive_by_type(type);
358 if (!ptype) {
359 flush = 1;
360 goto out_unlock;
361 }
362
363 skb_gro_pull(skb, gh_len);
364 skb_gro_postpull_rcsum(skb, gh, gh_len);
365 pp = ptype->callbacks.gro_receive(head, skb);
366
367out_unlock:
368 rcu_read_unlock();
369out:
370 NAPI_GRO_CB(skb)->flush |= flush;
371
372 return pp;
373}
374
375static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
376 struct udp_offload *uoff)
377{
378 struct genevehdr *gh;
379 struct packet_offload *ptype;
380 __be16 type;
381 int gh_len;
382 int err = -ENOSYS;
383
384 udp_tunnel_gro_complete(skb, nhoff);
385
386 gh = (struct genevehdr *)(skb->data + nhoff);
387 gh_len = geneve_hlen(gh);
388 type = gh->proto_type;
389
390 rcu_read_lock();
391 ptype = gro_find_complete_by_type(type);
392 if (ptype)
393 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
394
395 rcu_read_unlock();
396 return err;
397}
398
399/* Create new listen socket if needed */
400static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
401 bool ipv6)
402{
403 struct geneve_net *gn = net_generic(net, geneve_net_id);
404 struct geneve_sock *gs;
405 struct socket *sock;
406 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700407 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700408
409 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
410 if (!gs)
411 return ERR_PTR(-ENOMEM);
412
413 sock = geneve_create_sock(net, ipv6, port);
414 if (IS_ERR(sock)) {
415 kfree(gs);
416 return ERR_CAST(sock);
417 }
418
419 gs->sock = sock;
420 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700421 for (h = 0; h < VNI_HASH_SIZE; ++h)
422 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700423
424 /* Initialize the geneve udp offloads structure */
425 gs->udp_offloads.port = port;
426 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
427 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
428 geneve_notify_add_rx_port(gs);
429
430 /* Mark socket as an encapsulation socket */
431 tunnel_cfg.sk_user_data = gs;
432 tunnel_cfg.encap_type = 1;
433 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
434 tunnel_cfg.encap_destroy = NULL;
435 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700436 list_add(&gs->list, &gn->sock_list);
437 return gs;
438}
439
440static void geneve_notify_del_rx_port(struct geneve_sock *gs)
441{
442 struct sock *sk = gs->sock->sk;
443 sa_family_t sa_family = sk->sk_family;
444
445 if (sa_family == AF_INET)
446 udp_del_offload(&gs->udp_offloads);
447}
448
449static void geneve_sock_release(struct geneve_sock *gs)
450{
451 if (--gs->refcnt)
452 return;
453
454 list_del(&gs->list);
455 geneve_notify_del_rx_port(gs);
456 udp_tunnel_sock_release(gs->sock);
457 kfree_rcu(gs, rcu);
458}
459
460static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
461 __be16 dst_port)
462{
463 struct geneve_sock *gs;
464
465 list_for_each_entry(gs, &gn->sock_list, list) {
466 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
467 inet_sk(gs->sock->sk)->sk.sk_family == AF_INET) {
468 return gs;
469 }
470 }
471 return NULL;
472}
473
John W. Linville2d07dc72015-05-13 12:57:30 -0400474static int geneve_open(struct net_device *dev)
475{
476 struct geneve_dev *geneve = netdev_priv(dev);
477 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700478 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400479 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700480 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400481
Pravin B Shelar371bd102015-08-26 23:46:54 -0700482 gs = geneve_find_sock(gn, geneve->dst_port);
483 if (gs) {
484 gs->refcnt++;
485 goto out;
486 }
487
488 gs = geneve_socket_create(net, geneve->dst_port, false);
John W. Linville2d07dc72015-05-13 12:57:30 -0400489 if (IS_ERR(gs))
490 return PTR_ERR(gs);
491
Pravin B Shelar371bd102015-08-26 23:46:54 -0700492out:
493 gs->collect_md = geneve->collect_md;
John W. Linville2d07dc72015-05-13 12:57:30 -0400494 geneve->sock = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700495
496 hash = geneve_net_vni_hash(geneve->vni);
497 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400498 return 0;
499}
500
501static int geneve_stop(struct net_device *dev)
502{
503 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700504 struct geneve_sock *gs = geneve->sock;
John W. Linville2d07dc72015-05-13 12:57:30 -0400505
Pravin B Shelar66d47002015-08-26 23:46:55 -0700506 if (!hlist_unhashed(&geneve->hlist))
507 hlist_del_rcu(&geneve->hlist);
508 geneve_sock_release(gs);
John W. Linville2d07dc72015-05-13 12:57:30 -0400509 return 0;
510}
511
Pravin B Shelar371bd102015-08-26 23:46:54 -0700512static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
513 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
514 bool csum)
515{
516 struct genevehdr *gnvh;
517 int min_headroom;
518 int err;
519
520 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
521 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
522 err = skb_cow_head(skb, min_headroom);
523 if (unlikely(err)) {
524 kfree_skb(skb);
525 goto free_rt;
526 }
527
528 skb = udp_tunnel_handle_offloads(skb, csum);
529 if (IS_ERR(skb)) {
530 err = PTR_ERR(skb);
531 goto free_rt;
532 }
533
534 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
535 gnvh->ver = GENEVE_VER;
536 gnvh->opt_len = opt_len / 4;
537 gnvh->oam = !!(tun_flags & TUNNEL_OAM);
538 gnvh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
539 gnvh->rsvd1 = 0;
540 memcpy(gnvh->vni, vni, 3);
541 gnvh->proto_type = htons(ETH_P_TEB);
542 gnvh->rsvd2 = 0;
543 memcpy(gnvh->options, opt, opt_len);
544
545 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
546 return 0;
547
548free_rt:
549 ip_rt_put(rt);
550 return err;
551}
552
Pravin B Shelare305ac62015-08-26 23:46:52 -0700553static struct rtable *geneve_get_rt(struct sk_buff *skb,
554 struct net_device *dev,
555 struct flowi4 *fl4,
556 struct ip_tunnel_info *info)
557{
558 struct geneve_dev *geneve = netdev_priv(dev);
559 struct rtable *rt = NULL;
560 __u8 tos;
561
562 memset(fl4, 0, sizeof(*fl4));
563 fl4->flowi4_mark = skb->mark;
564 fl4->flowi4_proto = IPPROTO_UDP;
565
566 if (info) {
567 fl4->daddr = info->key.u.ipv4.dst;
568 fl4->saddr = info->key.u.ipv4.src;
569 fl4->flowi4_tos = RT_TOS(info->key.tos);
570 } else {
571 tos = geneve->tos;
572 if (tos == 1) {
573 const struct iphdr *iip = ip_hdr(skb);
574
575 tos = ip_tunnel_get_dsfield(iip, skb);
576 }
577
578 fl4->flowi4_tos = RT_TOS(tos);
579 fl4->daddr = geneve->remote.sin_addr.s_addr;
580 }
581
582 rt = ip_route_output_key(geneve->net, fl4);
583 if (IS_ERR(rt)) {
584 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
585 dev->stats.tx_carrier_errors++;
586 return rt;
587 }
588 if (rt->dst.dev == dev) { /* is this necessary? */
589 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
590 dev->stats.collisions++;
591 ip_rt_put(rt);
592 return ERR_PTR(-EINVAL);
593 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700594 return rt;
595}
596
597/* Convert 64 bit tunnel ID to 24 bit VNI. */
598static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
599{
600#ifdef __BIG_ENDIAN
601 vni[0] = (__force __u8)(tun_id >> 16);
602 vni[1] = (__force __u8)(tun_id >> 8);
603 vni[2] = (__force __u8)tun_id;
604#else
605 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
606 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
607 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
608#endif
609}
610
John W. Linville2d07dc72015-05-13 12:57:30 -0400611static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
612{
613 struct geneve_dev *geneve = netdev_priv(dev);
614 struct geneve_sock *gs = geneve->sock;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700615 struct ip_tunnel_info *info = NULL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400616 struct rtable *rt = NULL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400617 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400618 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700619 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700620 bool udp_csum;
621 __be16 df;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700622 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400623
Pravin B Shelare305ac62015-08-26 23:46:52 -0700624 if (geneve->collect_md) {
625 info = skb_tunnel_info(skb);
626 if (unlikely(info && info->mode != IP_TUNNEL_INFO_TX)) {
627 netdev_dbg(dev, "no tunnel metadata\n");
628 goto tx_error;
629 }
630 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400631
Pravin B Shelare305ac62015-08-26 23:46:52 -0700632 rt = geneve_get_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400633 if (IS_ERR(rt)) {
634 netdev_dbg(dev, "no route to %pI4\n", &fl4.daddr);
635 dev->stats.tx_carrier_errors++;
636 goto tx_error;
637 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700638
639 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700640 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700641
642 if (info) {
643 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700644 u8 *opts = NULL;
645 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700646
647 tunnel_id_to_vni(key->tun_id, vni);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700648 if (key->tun_flags & TUNNEL_GENEVE_OPT)
649 opts = ip_tunnel_info_opts(info, info->options_len);
650
Pravin B Shelar371bd102015-08-26 23:46:54 -0700651 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
652 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
653 info->options_len, opts, udp_csum);
654 if (unlikely(err))
655 goto err;
656
657 tos = key->tos;
658 ttl = key->ttl;
659 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700660 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700661 const struct iphdr *iip; /* interior IP header */
662
663 udp_csum = false;
664 err = geneve_build_skb(rt, skb, 0, geneve->vni,
665 0, NULL, udp_csum);
666 if (unlikely(err))
667 goto err;
668
Pravin B Shelare305ac62015-08-26 23:46:52 -0700669 iip = ip_hdr(skb);
670 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700671 ttl = geneve->ttl;
672 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
673 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700674 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700675 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400676 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700677 err = udp_tunnel_xmit_skb(rt, gs->sock->sk, skb, fl4.saddr, fl4.daddr,
678 tos, ttl, df, sport, geneve->dst_port,
679 !net_eq(geneve->net, dev_net(geneve->dev)),
680 !udp_csum);
John W. Linville2d07dc72015-05-13 12:57:30 -0400681
682 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
John W. Linville2d07dc72015-05-13 12:57:30 -0400683 return NETDEV_TX_OK;
684
John W. Linville2d07dc72015-05-13 12:57:30 -0400685tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400686 dev_kfree_skb(skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700687err:
688 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400689 return NETDEV_TX_OK;
690}
691
692static const struct net_device_ops geneve_netdev_ops = {
693 .ndo_init = geneve_init,
694 .ndo_uninit = geneve_uninit,
695 .ndo_open = geneve_open,
696 .ndo_stop = geneve_stop,
697 .ndo_start_xmit = geneve_xmit,
698 .ndo_get_stats64 = ip_tunnel_get_stats64,
699 .ndo_change_mtu = eth_change_mtu,
700 .ndo_validate_addr = eth_validate_addr,
701 .ndo_set_mac_address = eth_mac_addr,
702};
703
704static void geneve_get_drvinfo(struct net_device *dev,
705 struct ethtool_drvinfo *drvinfo)
706{
707 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
708 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
709}
710
711static const struct ethtool_ops geneve_ethtool_ops = {
712 .get_drvinfo = geneve_get_drvinfo,
713 .get_link = ethtool_op_get_link,
714};
715
716/* Info for udev, that this is a virtual tunnel endpoint */
717static struct device_type geneve_type = {
718 .name = "geneve",
719};
720
721/* Initialize the device structure. */
722static void geneve_setup(struct net_device *dev)
723{
724 ether_setup(dev);
725
726 dev->netdev_ops = &geneve_netdev_ops;
727 dev->ethtool_ops = &geneve_ethtool_ops;
728 dev->destructor = free_netdev;
729
730 SET_NETDEV_DEVTYPE(dev, &geneve_type);
731
John W. Linville2d07dc72015-05-13 12:57:30 -0400732 dev->features |= NETIF_F_LLTX;
733 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
734 dev->features |= NETIF_F_RXCSUM;
735 dev->features |= NETIF_F_GSO_SOFTWARE;
736
737 dev->vlan_features = dev->features;
738 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
739
740 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
741 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
742 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
743
744 netif_keep_dst(dev);
Phil Suttered961ac2015-08-18 10:30:31 +0200745 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -0700746 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400747}
748
749static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
750 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
751 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8760ce52015-06-01 15:51:34 -0400752 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -0400753 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -0700754 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -0700755 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
John W. Linville2d07dc72015-05-13 12:57:30 -0400756};
757
758static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
759{
760 if (tb[IFLA_ADDRESS]) {
761 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
762 return -EINVAL;
763
764 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
765 return -EADDRNOTAVAIL;
766 }
767
768 if (!data)
769 return -EINVAL;
770
771 if (data[IFLA_GENEVE_ID]) {
772 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
773
774 if (vni >= GENEVE_VID_MASK)
775 return -ERANGE;
776 }
777
778 return 0;
779}
780
Pravin B Shelar371bd102015-08-26 23:46:54 -0700781static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
782 __be16 dst_port,
783 __be32 rem_addr,
784 u8 vni[],
785 bool *tun_on_same_port,
786 bool *tun_collect_md)
787{
788 struct geneve_dev *geneve, *t;
789
790 *tun_on_same_port = false;
791 *tun_collect_md = false;
792 t = NULL;
793 list_for_each_entry(geneve, &gn->geneve_list, next) {
794 if (geneve->dst_port == dst_port) {
795 *tun_collect_md = geneve->collect_md;
796 *tun_on_same_port = true;
797 }
798 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
799 rem_addr == geneve->remote.sin_addr.s_addr &&
800 dst_port == geneve->dst_port)
801 t = geneve;
802 }
803 return t;
804}
805
Pravin B Shelare305ac62015-08-26 23:46:52 -0700806static int geneve_configure(struct net *net, struct net_device *dev,
807 __be32 rem_addr, __u32 vni, __u8 ttl, __u8 tos,
808 __u16 dst_port, bool metadata)
John W. Linville2d07dc72015-05-13 12:57:30 -0400809{
810 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700811 struct geneve_dev *t, *geneve = netdev_priv(dev);
812 bool tun_collect_md, tun_on_same_port;
John W. Linville2d07dc72015-05-13 12:57:30 -0400813 int err;
814
Pravin B Shelare305ac62015-08-26 23:46:52 -0700815 if (metadata) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700816 if (rem_addr || vni || tos || ttl)
817 return -EINVAL;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700818 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400819
820 geneve->net = net;
821 geneve->dev = dev;
822
John W. Linville2d07dc72015-05-13 12:57:30 -0400823 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
824 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
825 geneve->vni[2] = vni & 0x000000ff;
826
Pravin B Shelare305ac62015-08-26 23:46:52 -0700827 geneve->remote.sin_addr.s_addr = rem_addr;
John W. Linville2d07dc72015-05-13 12:57:30 -0400828 if (IN_MULTICAST(ntohl(geneve->remote.sin_addr.s_addr)))
829 return -EINVAL;
830
Pravin B Shelare305ac62015-08-26 23:46:52 -0700831 geneve->ttl = ttl;
832 geneve->tos = tos;
833 geneve->dst_port = htons(dst_port);
834 geneve->collect_md = metadata;
835
Pravin B Shelar371bd102015-08-26 23:46:54 -0700836 t = geneve_find_dev(gn, htons(dst_port), rem_addr, geneve->vni,
837 &tun_on_same_port, &tun_collect_md);
838 if (t)
839 return -EBUSY;
840
841 if (metadata) {
842 if (tun_on_same_port)
843 return -EPERM;
844 } else {
845 if (tun_collect_md)
846 return -EPERM;
847 }
848
John W. Linville2d07dc72015-05-13 12:57:30 -0400849 err = register_netdevice(dev);
850 if (err)
851 return err;
852
853 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -0400854 return 0;
855}
856
Pravin B Shelare305ac62015-08-26 23:46:52 -0700857static int geneve_newlink(struct net *net, struct net_device *dev,
858 struct nlattr *tb[], struct nlattr *data[])
859{
860 __u16 dst_port = GENEVE_UDP_PORT;
861 __u8 ttl = 0, tos = 0;
862 bool metadata = false;
863 __be32 rem_addr;
864 __u32 vni;
865
866 if (!data[IFLA_GENEVE_ID] || !data[IFLA_GENEVE_REMOTE])
867 return -EINVAL;
868
869 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
870 rem_addr = nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
871
872 if (data[IFLA_GENEVE_TTL])
873 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
874
875 if (data[IFLA_GENEVE_TOS])
876 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
877
878 if (data[IFLA_GENEVE_PORT])
879 dst_port = nla_get_u16(data[IFLA_GENEVE_PORT]);
880
881 if (data[IFLA_GENEVE_COLLECT_METADATA])
882 metadata = true;
883
884 return geneve_configure(net, dev, rem_addr, vni,
885 ttl, tos, dst_port, metadata);
886}
887
John W. Linville2d07dc72015-05-13 12:57:30 -0400888static void geneve_dellink(struct net_device *dev, struct list_head *head)
889{
890 struct geneve_dev *geneve = netdev_priv(dev);
891
John W. Linville2d07dc72015-05-13 12:57:30 -0400892 list_del(&geneve->next);
893 unregister_netdevice_queue(dev, head);
894}
895
896static size_t geneve_get_size(const struct net_device *dev)
897{
898 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
899 nla_total_size(sizeof(struct in_addr)) + /* IFLA_GENEVE_REMOTE */
John W. Linville8760ce52015-06-01 15:51:34 -0400900 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -0400901 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Pravin B Shelarcd7918b2015-08-26 23:46:51 -0700902 nla_total_size(sizeof(__u16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -0700903 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
John W. Linville2d07dc72015-05-13 12:57:30 -0400904 0;
905}
906
907static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
908{
909 struct geneve_dev *geneve = netdev_priv(dev);
910 __u32 vni;
911
912 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
913 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
914 goto nla_put_failure;
915
916 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
917 geneve->remote.sin_addr.s_addr))
918 goto nla_put_failure;
919
John W. Linvilled8951122015-06-01 15:51:35 -0400920 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
921 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
John W. Linville8760ce52015-06-01 15:51:34 -0400922 goto nla_put_failure;
923
Pravin B Shelarcd7918b2015-08-26 23:46:51 -0700924 if (nla_put_u16(skb, IFLA_GENEVE_PORT, ntohs(geneve->dst_port)))
925 goto nla_put_failure;
926
Pravin B Shelare305ac62015-08-26 23:46:52 -0700927 if (geneve->collect_md) {
928 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
929 goto nla_put_failure;
930 }
931
John W. Linville2d07dc72015-05-13 12:57:30 -0400932 return 0;
933
934nla_put_failure:
935 return -EMSGSIZE;
936}
937
938static struct rtnl_link_ops geneve_link_ops __read_mostly = {
939 .kind = "geneve",
940 .maxtype = IFLA_GENEVE_MAX,
941 .policy = geneve_policy,
942 .priv_size = sizeof(struct geneve_dev),
943 .setup = geneve_setup,
944 .validate = geneve_validate,
945 .newlink = geneve_newlink,
946 .dellink = geneve_dellink,
947 .get_size = geneve_get_size,
948 .fill_info = geneve_fill_info,
949};
950
Pravin B Shelare305ac62015-08-26 23:46:52 -0700951struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
952 u8 name_assign_type, u16 dst_port)
953{
954 struct nlattr *tb[IFLA_MAX + 1];
955 struct net_device *dev;
956 int err;
957
958 memset(tb, 0, sizeof(tb));
959 dev = rtnl_create_link(net, name, name_assign_type,
960 &geneve_link_ops, tb);
961 if (IS_ERR(dev))
962 return dev;
963
964 err = geneve_configure(net, dev, 0, 0, 0, 0, dst_port, true);
965 if (err) {
966 free_netdev(dev);
967 return ERR_PTR(err);
968 }
969 return dev;
970}
971EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
972
John W. Linville2d07dc72015-05-13 12:57:30 -0400973static __net_init int geneve_init_net(struct net *net)
974{
975 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400976
977 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700978 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -0400979 return 0;
980}
981
982static void __net_exit geneve_exit_net(struct net *net)
983{
984 struct geneve_net *gn = net_generic(net, geneve_net_id);
985 struct geneve_dev *geneve, *next;
986 struct net_device *dev, *aux;
987 LIST_HEAD(list);
988
989 rtnl_lock();
990
991 /* gather any geneve devices that were moved into this ns */
992 for_each_netdev_safe(net, dev, aux)
993 if (dev->rtnl_link_ops == &geneve_link_ops)
994 unregister_netdevice_queue(dev, &list);
995
996 /* now gather any other geneve devices that were created in this ns */
997 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
998 /* If geneve->dev is in the same netns, it was already added
999 * to the list by the previous loop.
1000 */
1001 if (!net_eq(dev_net(geneve->dev), net))
1002 unregister_netdevice_queue(geneve->dev, &list);
1003 }
1004
1005 /* unregister the devices gathered above */
1006 unregister_netdevice_many(&list);
1007 rtnl_unlock();
1008}
1009
1010static struct pernet_operations geneve_net_ops = {
1011 .init = geneve_init_net,
1012 .exit = geneve_exit_net,
1013 .id = &geneve_net_id,
1014 .size = sizeof(struct geneve_net),
1015};
1016
1017static int __init geneve_init_module(void)
1018{
1019 int rc;
1020
1021 rc = register_pernet_subsys(&geneve_net_ops);
1022 if (rc)
1023 goto out1;
1024
1025 rc = rtnl_link_register(&geneve_link_ops);
1026 if (rc)
1027 goto out2;
1028
1029 return 0;
1030out2:
1031 unregister_pernet_subsys(&geneve_net_ops);
1032out1:
1033 return rc;
1034}
1035late_initcall(geneve_init_module);
1036
1037static void __exit geneve_cleanup_module(void)
1038{
1039 rtnl_link_unregister(&geneve_link_ops);
1040 unregister_pernet_subsys(&geneve_net_ops);
1041}
1042module_exit(geneve_cleanup_module);
1043
1044MODULE_LICENSE("GPL");
1045MODULE_VERSION(GENEVE_NETDEV_VER);
1046MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1047MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1048MODULE_ALIAS_RTNL_LINK("geneve");