blob: 19e256e1dd92f374266d52612d483667de684cf7 [file] [log] [blame]
Andy Zhou0b5e8b82014-10-03 15:35:28 -07001/*
2 * Geneve: Generic Network Virtualization Encapsulation
3 *
4 * Copyright (c) 2014 Nicira, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/module.h>
17#include <linux/errno.h>
18#include <linux/slab.h>
19#include <linux/skbuff.h>
20#include <linux/rculist.h>
21#include <linux/netdevice.h>
22#include <linux/in.h>
23#include <linux/ip.h>
24#include <linux/udp.h>
25#include <linux/igmp.h>
26#include <linux/etherdevice.h>
27#include <linux/if_ether.h>
28#include <linux/if_vlan.h>
29#include <linux/hash.h>
30#include <linux/ethtool.h>
31#include <net/arp.h>
32#include <net/ndisc.h>
33#include <net/ip.h>
34#include <net/ip_tunnels.h>
35#include <net/icmp.h>
36#include <net/udp.h>
37#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
43#include <net/geneve.h>
44#include <net/protocol.h>
45#include <net/udp_tunnel.h>
46#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
50#include <net/ip6_checksum.h>
51#endif
52
53#define PORT_HASH_BITS 8
54#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
55
56/* per-network namespace private data for this module */
57struct geneve_net {
58 struct hlist_head sock_list[PORT_HASH_SIZE];
59 spinlock_t sock_lock; /* Protects sock_list */
60};
61
62static int geneve_net_id;
63
64static struct workqueue_struct *geneve_wq;
65
66static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
67{
68 return (struct genevehdr *)(udp_hdr(skb) + 1);
69}
70
71static struct hlist_head *gs_head(struct net *net, __be16 port)
72{
73 struct geneve_net *gn = net_generic(net, geneve_net_id);
74
75 return &gn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
76}
77
78/* Find geneve socket based on network namespace and UDP port */
79static struct geneve_sock *geneve_find_sock(struct net *net, __be16 port)
80{
81 struct geneve_sock *gs;
82
83 hlist_for_each_entry_rcu(gs, gs_head(net, port), hlist) {
84 if (inet_sk(gs->sock->sk)->inet_sport == port)
85 return gs;
86 }
87
88 return NULL;
89}
90
91static void geneve_build_header(struct genevehdr *geneveh,
92 __be16 tun_flags, u8 vni[3],
93 u8 options_len, u8 *options)
94{
95 geneveh->ver = GENEVE_VER;
96 geneveh->opt_len = options_len / 4;
97 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
98 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
99 geneveh->rsvd1 = 0;
100 memcpy(geneveh->vni, vni, 3);
101 geneveh->proto_type = htons(ETH_P_TEB);
102 geneveh->rsvd2 = 0;
103
104 memcpy(geneveh->options, options, options_len);
105}
106
stephen hemmingerf4e715c2014-10-29 16:05:06 -0700107/* Transmit a fully formatted Geneve frame.
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700108 *
109 * When calling this function. The skb->data should point
110 * to the geneve header which is fully formed.
111 *
112 * This function will add other UDP tunnel headers.
113 */
114int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
115 struct sk_buff *skb, __be32 src, __be32 dst, __u8 tos,
116 __u8 ttl, __be16 df, __be16 src_port, __be16 dst_port,
117 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
118 bool xnet)
119{
120 struct genevehdr *gnvh;
121 int min_headroom;
122 int err;
123
124 skb = udp_tunnel_handle_offloads(skb, !gs->sock->sk->sk_no_check_tx);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800125 if (IS_ERR(skb))
126 return PTR_ERR(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700127
128 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
129 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr)
130 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
131
132 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800133 if (unlikely(err)) {
134 kfree_skb(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700135 return err;
Pravin B Shelar997e0682014-12-23 16:20:32 -0800136 }
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700137
Jiri Pirko59682502014-11-19 14:04:59 +0100138 skb = vlan_hwaccel_push_inside(skb);
139 if (unlikely(!skb))
140 return -ENOMEM;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700141
142 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
143 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
144
Jesse Gross45cac462014-11-03 19:38:37 -0800145 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
146
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700147 return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst,
148 tos, ttl, df, src_port, dst_port, xnet);
149}
150EXPORT_SYMBOL_GPL(geneve_xmit_skb);
151
Joe Stringera4c9ea52014-12-30 19:10:16 -0800152static int geneve_hlen(struct genevehdr *gh)
153{
154 return sizeof(*gh) + gh->opt_len * 4;
155}
156
157static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
158 struct sk_buff *skb)
159{
160 struct sk_buff *p, **pp = NULL;
161 struct genevehdr *gh, *gh2;
162 unsigned int hlen, gh_len, off_gnv;
163 const struct packet_offload *ptype;
164 __be16 type;
165 int flush = 1;
166
167 off_gnv = skb_gro_offset(skb);
168 hlen = off_gnv + sizeof(*gh);
169 gh = skb_gro_header_fast(skb, off_gnv);
170 if (skb_gro_header_hard(skb, hlen)) {
171 gh = skb_gro_header_slow(skb, hlen, off_gnv);
172 if (unlikely(!gh))
173 goto out;
174 }
175
176 if (gh->ver != GENEVE_VER || gh->oam)
177 goto out;
178 gh_len = geneve_hlen(gh);
179
180 hlen = off_gnv + gh_len;
181 if (skb_gro_header_hard(skb, hlen)) {
182 gh = skb_gro_header_slow(skb, hlen, off_gnv);
183 if (unlikely(!gh))
184 goto out;
185 }
186
187 flush = 0;
188
189 for (p = *head; p; p = p->next) {
190 if (!NAPI_GRO_CB(p)->same_flow)
191 continue;
192
193 gh2 = (struct genevehdr *)(p->data + off_gnv);
194 if (gh->opt_len != gh2->opt_len ||
195 memcmp(gh, gh2, gh_len)) {
196 NAPI_GRO_CB(p)->same_flow = 0;
197 continue;
198 }
199 }
200
201 type = gh->proto_type;
202
203 rcu_read_lock();
204 ptype = gro_find_receive_by_type(type);
205 if (ptype == NULL) {
206 flush = 1;
207 goto out_unlock;
208 }
209
210 skb_gro_pull(skb, gh_len);
211 skb_gro_postpull_rcsum(skb, gh, gh_len);
212 pp = ptype->callbacks.gro_receive(head, skb);
213
214out_unlock:
215 rcu_read_unlock();
216out:
217 NAPI_GRO_CB(skb)->flush |= flush;
218
219 return pp;
220}
221
222static int geneve_gro_complete(struct sk_buff *skb, int nhoff)
223{
224 struct genevehdr *gh;
225 struct packet_offload *ptype;
226 __be16 type;
227 int gh_len;
228 int err = -ENOSYS;
229
230 udp_tunnel_gro_complete(skb, nhoff);
231
232 gh = (struct genevehdr *)(skb->data + nhoff);
233 gh_len = geneve_hlen(gh);
234 type = gh->proto_type;
235
236 rcu_read_lock();
237 ptype = gro_find_complete_by_type(type);
238 if (ptype != NULL)
239 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
240
241 rcu_read_unlock();
242 return err;
243}
244
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700245static void geneve_notify_add_rx_port(struct geneve_sock *gs)
246{
247 struct sock *sk = gs->sock->sk;
248 sa_family_t sa_family = sk->sk_family;
249 int err;
250
251 if (sa_family == AF_INET) {
252 err = udp_add_offload(&gs->udp_offloads);
253 if (err)
254 pr_warn("geneve: udp_add_offload failed with status %d\n",
255 err);
256 }
257}
258
Jesse Gross7ed767f2014-12-16 18:25:31 -0800259static void geneve_notify_del_rx_port(struct geneve_sock *gs)
260{
261 struct sock *sk = gs->sock->sk;
262 sa_family_t sa_family = sk->sk_family;
263
264 if (sa_family == AF_INET)
265 udp_del_offload(&gs->udp_offloads);
266}
267
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700268/* Callback from net/ipv4/udp.c to receive packets */
269static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
270{
271 struct genevehdr *geneveh;
272 struct geneve_sock *gs;
273 int opts_len;
274
275 /* Need Geneve and inner Ethernet header to be present */
276 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
277 goto error;
278
279 /* Return packets with reserved bits set */
280 geneveh = geneve_hdr(skb);
281
282 if (unlikely(geneveh->ver != GENEVE_VER))
283 goto error;
284
285 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
286 goto error;
287
288 opts_len = geneveh->opt_len * 4;
289 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
290 htons(ETH_P_TEB)))
291 goto drop;
292
293 gs = rcu_dereference_sk_user_data(sk);
294 if (!gs)
295 goto drop;
296
297 gs->rcv(gs, skb);
298 return 0;
299
300drop:
301 /* Consume bad packet */
302 kfree_skb(skb);
303 return 0;
304
305error:
306 /* Let the UDP layer deal with the skb */
307 return 1;
308}
309
310static void geneve_del_work(struct work_struct *work)
311{
312 struct geneve_sock *gs = container_of(work, struct geneve_sock,
313 del_work);
314
315 udp_tunnel_sock_release(gs->sock);
316 kfree_rcu(gs, rcu);
317}
318
319static struct socket *geneve_create_sock(struct net *net, bool ipv6,
320 __be16 port)
321{
322 struct socket *sock;
323 struct udp_port_cfg udp_conf;
324 int err;
325
326 memset(&udp_conf, 0, sizeof(udp_conf));
327
328 if (ipv6) {
329 udp_conf.family = AF_INET6;
330 } else {
331 udp_conf.family = AF_INET;
Andy Zhou42350dc2014-10-06 13:22:50 -0700332 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700333 }
334
335 udp_conf.local_udp_port = port;
336
337 /* Open UDP socket */
338 err = udp_sock_create(net, &udp_conf, &sock);
339 if (err < 0)
340 return ERR_PTR(err);
341
342 return sock;
343}
344
345/* Create new listen socket if needed */
346static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
347 geneve_rcv_t *rcv, void *data,
348 bool ipv6)
349{
350 struct geneve_net *gn = net_generic(net, geneve_net_id);
351 struct geneve_sock *gs;
352 struct socket *sock;
353 struct udp_tunnel_sock_cfg tunnel_cfg;
354
355 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
356 if (!gs)
357 return ERR_PTR(-ENOMEM);
358
359 INIT_WORK(&gs->del_work, geneve_del_work);
360
361 sock = geneve_create_sock(net, ipv6, port);
362 if (IS_ERR(sock)) {
363 kfree(gs);
364 return ERR_CAST(sock);
365 }
366
367 gs->sock = sock;
368 atomic_set(&gs->refcnt, 1);
369 gs->rcv = rcv;
370 gs->rcv_data = data;
371
372 /* Initialize the geneve udp offloads structure */
373 gs->udp_offloads.port = port;
Joe Stringera4c9ea52014-12-30 19:10:16 -0800374 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
375 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700376
377 spin_lock(&gn->sock_lock);
378 hlist_add_head_rcu(&gs->hlist, gs_head(net, port));
379 geneve_notify_add_rx_port(gs);
380 spin_unlock(&gn->sock_lock);
381
382 /* Mark socket as an encapsulation socket */
383 tunnel_cfg.sk_user_data = gs;
384 tunnel_cfg.encap_type = 1;
385 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
386 tunnel_cfg.encap_destroy = NULL;
387 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
388
389 return gs;
390}
391
392struct geneve_sock *geneve_sock_add(struct net *net, __be16 port,
393 geneve_rcv_t *rcv, void *data,
394 bool no_share, bool ipv6)
395{
Jesse Gross12069402014-12-16 18:25:32 -0800396 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700397 struct geneve_sock *gs;
398
399 gs = geneve_socket_create(net, port, rcv, data, ipv6);
400 if (!IS_ERR(gs))
401 return gs;
402
403 if (no_share) /* Return error if sharing is not allowed. */
404 return ERR_PTR(-EINVAL);
405
Jesse Gross12069402014-12-16 18:25:32 -0800406 spin_lock(&gn->sock_lock);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700407 gs = geneve_find_sock(net, port);
Jesse Gross12069402014-12-16 18:25:32 -0800408 if (gs && ((gs->rcv != rcv) ||
409 !atomic_add_unless(&gs->refcnt, 1, 0)))
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700410 gs = ERR_PTR(-EBUSY);
Jesse Gross12069402014-12-16 18:25:32 -0800411 spin_unlock(&gn->sock_lock);
412
413 if (!gs)
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700414 gs = ERR_PTR(-EINVAL);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700415
416 return gs;
417}
418EXPORT_SYMBOL_GPL(geneve_sock_add);
419
420void geneve_sock_release(struct geneve_sock *gs)
421{
Jesse Gross7ed767f2014-12-16 18:25:31 -0800422 struct net *net = sock_net(gs->sock->sk);
423 struct geneve_net *gn = net_generic(net, geneve_net_id);
424
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700425 if (!atomic_dec_and_test(&gs->refcnt))
426 return;
427
Jesse Gross7ed767f2014-12-16 18:25:31 -0800428 spin_lock(&gn->sock_lock);
429 hlist_del_rcu(&gs->hlist);
430 geneve_notify_del_rx_port(gs);
431 spin_unlock(&gn->sock_lock);
432
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700433 queue_work(geneve_wq, &gs->del_work);
434}
435EXPORT_SYMBOL_GPL(geneve_sock_release);
436
437static __net_init int geneve_init_net(struct net *net)
438{
439 struct geneve_net *gn = net_generic(net, geneve_net_id);
440 unsigned int h;
441
442 spin_lock_init(&gn->sock_lock);
443
444 for (h = 0; h < PORT_HASH_SIZE; ++h)
445 INIT_HLIST_HEAD(&gn->sock_list[h]);
446
447 return 0;
448}
449
450static struct pernet_operations geneve_net_ops = {
451 .init = geneve_init_net,
452 .exit = NULL,
453 .id = &geneve_net_id,
454 .size = sizeof(struct geneve_net),
455};
456
457static int __init geneve_init_module(void)
458{
459 int rc;
460
461 geneve_wq = alloc_workqueue("geneve", 0, 0);
462 if (!geneve_wq)
463 return -ENOMEM;
464
465 rc = register_pernet_subsys(&geneve_net_ops);
466 if (rc)
467 return rc;
468
469 pr_info("Geneve driver\n");
470
471 return 0;
472}
473late_initcall(geneve_init_module);
474
475static void __exit geneve_cleanup_module(void)
476{
477 destroy_workqueue(geneve_wq);
Jesse Grossd3ca9ea2014-11-03 19:38:38 -0800478 unregister_pernet_subsys(&geneve_net_ops);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700479}
480module_exit(geneve_cleanup_module);
481
482MODULE_LICENSE("GPL");
483MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>");
484MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic");
485MODULE_ALIAS_RTNL_LINK("geneve");