blob: ad8dbae11d0119f5dbbda429838d215a724185e7 [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>
Jesse Gross829a3ad2015-01-02 18:26:03 -080020#include <linux/list.h>
Andy Zhou0b5e8b82014-10-03 15:35:28 -070021#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>
Jesse Gross829a3ad2015-01-02 18:26:03 -080031#include <linux/mutex.h>
Andy Zhou0b5e8b82014-10-03 15:35:28 -070032#include <net/arp.h>
33#include <net/ndisc.h>
34#include <net/ip.h>
35#include <net/ip_tunnels.h>
36#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44#include <net/geneve.h>
45#include <net/protocol.h>
46#include <net/udp_tunnel.h>
47#if IS_ENABLED(CONFIG_IPV6)
48#include <net/ipv6.h>
49#include <net/addrconf.h>
50#include <net/ip6_tunnel.h>
51#include <net/ip6_checksum.h>
52#endif
53
Jesse Gross829a3ad2015-01-02 18:26:03 -080054/* Protects sock_list and refcounts. */
55static DEFINE_MUTEX(geneve_mutex);
56
Andy Zhou0b5e8b82014-10-03 15:35:28 -070057#define PORT_HASH_BITS 8
58#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
59
60/* per-network namespace private data for this module */
61struct geneve_net {
62 struct hlist_head sock_list[PORT_HASH_SIZE];
Andy Zhou0b5e8b82014-10-03 15:35:28 -070063};
64
65static int geneve_net_id;
66
Andy Zhou0b5e8b82014-10-03 15:35:28 -070067static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
68{
69 return (struct genevehdr *)(udp_hdr(skb) + 1);
70}
71
72static struct hlist_head *gs_head(struct net *net, __be16 port)
73{
74 struct geneve_net *gn = net_generic(net, geneve_net_id);
75
76 return &gn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
77}
78
79/* Find geneve socket based on network namespace and UDP port */
80static struct geneve_sock *geneve_find_sock(struct net *net, __be16 port)
81{
82 struct geneve_sock *gs;
83
Jesse Gross829a3ad2015-01-02 18:26:03 -080084 hlist_for_each_entry(gs, gs_head(net, port), hlist) {
Andy Zhou0b5e8b82014-10-03 15:35:28 -070085 if (inet_sk(gs->sock->sk)->inet_sport == port)
86 return gs;
87 }
88
89 return NULL;
90}
91
92static void geneve_build_header(struct genevehdr *geneveh,
93 __be16 tun_flags, u8 vni[3],
94 u8 options_len, u8 *options)
95{
96 geneveh->ver = GENEVE_VER;
97 geneveh->opt_len = options_len / 4;
98 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
99 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
100 geneveh->rsvd1 = 0;
101 memcpy(geneveh->vni, vni, 3);
102 geneveh->proto_type = htons(ETH_P_TEB);
103 geneveh->rsvd2 = 0;
104
105 memcpy(geneveh->options, options, options_len);
106}
107
stephen hemmingerf4e715c2014-10-29 16:05:06 -0700108/* Transmit a fully formatted Geneve frame.
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700109 *
110 * When calling this function. The skb->data should point
111 * to the geneve header which is fully formed.
112 *
113 * This function will add other UDP tunnel headers.
114 */
115int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
116 struct sk_buff *skb, __be32 src, __be32 dst, __u8 tos,
117 __u8 ttl, __be16 df, __be16 src_port, __be16 dst_port,
118 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
119 bool xnet)
120{
121 struct genevehdr *gnvh;
122 int min_headroom;
123 int err;
124
125 skb = udp_tunnel_handle_offloads(skb, !gs->sock->sk->sk_no_check_tx);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800126 if (IS_ERR(skb))
127 return PTR_ERR(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700128
129 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
130 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr)
131 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
132
133 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800134 if (unlikely(err)) {
135 kfree_skb(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700136 return err;
Pravin B Shelar997e0682014-12-23 16:20:32 -0800137 }
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700138
Jiri Pirko59682502014-11-19 14:04:59 +0100139 skb = vlan_hwaccel_push_inside(skb);
140 if (unlikely(!skb))
141 return -ENOMEM;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700142
143 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
144 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
145
Jesse Gross45cac462014-11-03 19:38:37 -0800146 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
147
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700148 return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst,
149 tos, ttl, df, src_port, dst_port, xnet);
150}
151EXPORT_SYMBOL_GPL(geneve_xmit_skb);
152
Joe Stringera4c9ea52014-12-30 19:10:16 -0800153static int geneve_hlen(struct genevehdr *gh)
154{
155 return sizeof(*gh) + gh->opt_len * 4;
156}
157
158static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
159 struct sk_buff *skb)
160{
161 struct sk_buff *p, **pp = NULL;
162 struct genevehdr *gh, *gh2;
163 unsigned int hlen, gh_len, off_gnv;
164 const struct packet_offload *ptype;
165 __be16 type;
166 int flush = 1;
167
168 off_gnv = skb_gro_offset(skb);
169 hlen = off_gnv + sizeof(*gh);
170 gh = skb_gro_header_fast(skb, off_gnv);
171 if (skb_gro_header_hard(skb, hlen)) {
172 gh = skb_gro_header_slow(skb, hlen, off_gnv);
173 if (unlikely(!gh))
174 goto out;
175 }
176
177 if (gh->ver != GENEVE_VER || gh->oam)
178 goto out;
179 gh_len = geneve_hlen(gh);
180
181 hlen = off_gnv + gh_len;
182 if (skb_gro_header_hard(skb, hlen)) {
183 gh = skb_gro_header_slow(skb, hlen, off_gnv);
184 if (unlikely(!gh))
185 goto out;
186 }
187
188 flush = 0;
189
190 for (p = *head; p; p = p->next) {
191 if (!NAPI_GRO_CB(p)->same_flow)
192 continue;
193
194 gh2 = (struct genevehdr *)(p->data + off_gnv);
195 if (gh->opt_len != gh2->opt_len ||
196 memcmp(gh, gh2, gh_len)) {
197 NAPI_GRO_CB(p)->same_flow = 0;
198 continue;
199 }
200 }
201
202 type = gh->proto_type;
203
204 rcu_read_lock();
205 ptype = gro_find_receive_by_type(type);
206 if (ptype == NULL) {
207 flush = 1;
208 goto out_unlock;
209 }
210
211 skb_gro_pull(skb, gh_len);
212 skb_gro_postpull_rcsum(skb, gh, gh_len);
213 pp = ptype->callbacks.gro_receive(head, skb);
214
215out_unlock:
216 rcu_read_unlock();
217out:
218 NAPI_GRO_CB(skb)->flush |= flush;
219
220 return pp;
221}
222
223static int geneve_gro_complete(struct sk_buff *skb, int nhoff)
224{
225 struct genevehdr *gh;
226 struct packet_offload *ptype;
227 __be16 type;
228 int gh_len;
229 int err = -ENOSYS;
230
231 udp_tunnel_gro_complete(skb, nhoff);
232
233 gh = (struct genevehdr *)(skb->data + nhoff);
234 gh_len = geneve_hlen(gh);
235 type = gh->proto_type;
236
237 rcu_read_lock();
238 ptype = gro_find_complete_by_type(type);
239 if (ptype != NULL)
240 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
241
242 rcu_read_unlock();
243 return err;
244}
245
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700246static void geneve_notify_add_rx_port(struct geneve_sock *gs)
247{
248 struct sock *sk = gs->sock->sk;
249 sa_family_t sa_family = sk->sk_family;
250 int err;
251
252 if (sa_family == AF_INET) {
253 err = udp_add_offload(&gs->udp_offloads);
254 if (err)
255 pr_warn("geneve: udp_add_offload failed with status %d\n",
256 err);
257 }
258}
259
Jesse Gross7ed767f2014-12-16 18:25:31 -0800260static void geneve_notify_del_rx_port(struct geneve_sock *gs)
261{
262 struct sock *sk = gs->sock->sk;
263 sa_family_t sa_family = sk->sk_family;
264
265 if (sa_family == AF_INET)
266 udp_del_offload(&gs->udp_offloads);
267}
268
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700269/* Callback from net/ipv4/udp.c to receive packets */
270static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
271{
272 struct genevehdr *geneveh;
273 struct geneve_sock *gs;
274 int opts_len;
275
276 /* Need Geneve and inner Ethernet header to be present */
277 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
278 goto error;
279
280 /* Return packets with reserved bits set */
281 geneveh = geneve_hdr(skb);
282
283 if (unlikely(geneveh->ver != GENEVE_VER))
284 goto error;
285
286 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
287 goto error;
288
289 opts_len = geneveh->opt_len * 4;
290 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
291 htons(ETH_P_TEB)))
292 goto drop;
293
294 gs = rcu_dereference_sk_user_data(sk);
295 if (!gs)
296 goto drop;
297
298 gs->rcv(gs, skb);
299 return 0;
300
301drop:
302 /* Consume bad packet */
303 kfree_skb(skb);
304 return 0;
305
306error:
307 /* Let the UDP layer deal with the skb */
308 return 1;
309}
310
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700311static struct socket *geneve_create_sock(struct net *net, bool ipv6,
312 __be16 port)
313{
314 struct socket *sock;
315 struct udp_port_cfg udp_conf;
316 int err;
317
318 memset(&udp_conf, 0, sizeof(udp_conf));
319
320 if (ipv6) {
321 udp_conf.family = AF_INET6;
322 } else {
323 udp_conf.family = AF_INET;
Andy Zhou42350dc2014-10-06 13:22:50 -0700324 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700325 }
326
327 udp_conf.local_udp_port = port;
328
329 /* Open UDP socket */
330 err = udp_sock_create(net, &udp_conf, &sock);
331 if (err < 0)
332 return ERR_PTR(err);
333
334 return sock;
335}
336
337/* Create new listen socket if needed */
338static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
339 geneve_rcv_t *rcv, void *data,
340 bool ipv6)
341{
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700342 struct geneve_sock *gs;
343 struct socket *sock;
344 struct udp_tunnel_sock_cfg tunnel_cfg;
345
346 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
347 if (!gs)
348 return ERR_PTR(-ENOMEM);
349
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700350 sock = geneve_create_sock(net, ipv6, port);
351 if (IS_ERR(sock)) {
352 kfree(gs);
353 return ERR_CAST(sock);
354 }
355
356 gs->sock = sock;
Jesse Gross829a3ad2015-01-02 18:26:03 -0800357 gs->refcnt = 1;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700358 gs->rcv = rcv;
359 gs->rcv_data = data;
360
361 /* Initialize the geneve udp offloads structure */
362 gs->udp_offloads.port = port;
Joe Stringera4c9ea52014-12-30 19:10:16 -0800363 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
364 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700365 geneve_notify_add_rx_port(gs);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700366
367 /* Mark socket as an encapsulation socket */
368 tunnel_cfg.sk_user_data = gs;
369 tunnel_cfg.encap_type = 1;
370 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
371 tunnel_cfg.encap_destroy = NULL;
372 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
373
Jesse Gross829a3ad2015-01-02 18:26:03 -0800374 hlist_add_head(&gs->hlist, gs_head(net, port));
375
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700376 return gs;
377}
378
379struct geneve_sock *geneve_sock_add(struct net *net, __be16 port,
380 geneve_rcv_t *rcv, void *data,
381 bool no_share, bool ipv6)
382{
383 struct geneve_sock *gs;
384
Jesse Gross829a3ad2015-01-02 18:26:03 -0800385 mutex_lock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700386
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700387 gs = geneve_find_sock(net, port);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800388 if (gs) {
389 if (!no_share && gs->rcv == rcv)
390 gs->refcnt++;
391 else
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700392 gs = ERR_PTR(-EBUSY);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800393 } else {
394 gs = geneve_socket_create(net, port, rcv, data, ipv6);
395 }
Jesse Gross12069402014-12-16 18:25:32 -0800396
Jesse Gross829a3ad2015-01-02 18:26:03 -0800397 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700398
399 return gs;
400}
401EXPORT_SYMBOL_GPL(geneve_sock_add);
402
403void geneve_sock_release(struct geneve_sock *gs)
404{
Jesse Gross829a3ad2015-01-02 18:26:03 -0800405 mutex_lock(&geneve_mutex);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800406
Jesse Gross829a3ad2015-01-02 18:26:03 -0800407 if (--gs->refcnt)
408 goto unlock;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700409
Jesse Gross829a3ad2015-01-02 18:26:03 -0800410 hlist_del(&gs->hlist);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800411 geneve_notify_del_rx_port(gs);
Jesse Gross61f3cad2015-01-02 18:26:02 -0800412 udp_tunnel_sock_release(gs->sock);
413 kfree_rcu(gs, rcu);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800414
415unlock:
416 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700417}
418EXPORT_SYMBOL_GPL(geneve_sock_release);
419
420static __net_init int geneve_init_net(struct net *net)
421{
422 struct geneve_net *gn = net_generic(net, geneve_net_id);
423 unsigned int h;
424
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700425 for (h = 0; h < PORT_HASH_SIZE; ++h)
426 INIT_HLIST_HEAD(&gn->sock_list[h]);
427
428 return 0;
429}
430
431static struct pernet_operations geneve_net_ops = {
432 .init = geneve_init_net,
433 .exit = NULL,
434 .id = &geneve_net_id,
435 .size = sizeof(struct geneve_net),
436};
437
438static int __init geneve_init_module(void)
439{
440 int rc;
441
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700442 rc = register_pernet_subsys(&geneve_net_ops);
443 if (rc)
444 return rc;
445
446 pr_info("Geneve driver\n");
447
448 return 0;
449}
Jesse Gross829a3ad2015-01-02 18:26:03 -0800450module_init(geneve_init_module);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700451
452static void __exit geneve_cleanup_module(void)
453{
Jesse Grossd3ca9ea2014-11-03 19:38:38 -0800454 unregister_pernet_subsys(&geneve_net_ops);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700455}
456module_exit(geneve_cleanup_module);
457
458MODULE_LICENSE("GPL");
459MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>");
460MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic");
461MODULE_ALIAS_RTNL_LINK("geneve");