blob: 4fe5a592821cf94494d14e4852d95fb30ab72081 [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>
Andy Zhou0b5e8b82014-10-03 15:35:28 -070029#include <linux/ethtool.h>
Jesse Gross829a3ad2015-01-02 18:26:03 -080030#include <linux/mutex.h>
Andy Zhou0b5e8b82014-10-03 15:35:28 -070031#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
Jesse Gross829a3ad2015-01-02 18:26:03 -080053/* Protects sock_list and refcounts. */
54static DEFINE_MUTEX(geneve_mutex);
55
Andy Zhou0b5e8b82014-10-03 15:35:28 -070056/* per-network namespace private data for this module */
57struct geneve_net {
Jesse Grossdf5dba82015-01-02 18:26:04 -080058 struct list_head sock_list;
Andy Zhou0b5e8b82014-10-03 15:35:28 -070059};
60
61static int geneve_net_id;
62
Andy Zhou0b5e8b82014-10-03 15:35:28 -070063static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
64{
65 return (struct genevehdr *)(udp_hdr(skb) + 1);
66}
67
Andy Zhou0b5e8b82014-10-03 15:35:28 -070068/* Find geneve socket based on network namespace and UDP port */
69static struct geneve_sock *geneve_find_sock(struct net *net, __be16 port)
70{
Jesse Grossdf5dba82015-01-02 18:26:04 -080071 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -070072 struct geneve_sock *gs;
73
Jesse Grossdf5dba82015-01-02 18:26:04 -080074 list_for_each_entry(gs, &gn->sock_list, list) {
Andy Zhou0b5e8b82014-10-03 15:35:28 -070075 if (inet_sk(gs->sock->sk)->inet_sport == port)
76 return gs;
77 }
78
79 return NULL;
80}
81
82static void geneve_build_header(struct genevehdr *geneveh,
83 __be16 tun_flags, u8 vni[3],
84 u8 options_len, u8 *options)
85{
86 geneveh->ver = GENEVE_VER;
87 geneveh->opt_len = options_len / 4;
88 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
89 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
90 geneveh->rsvd1 = 0;
91 memcpy(geneveh->vni, vni, 3);
92 geneveh->proto_type = htons(ETH_P_TEB);
93 geneveh->rsvd2 = 0;
94
95 memcpy(geneveh->options, options, options_len);
96}
97
stephen hemmingerf4e715c2014-10-29 16:05:06 -070098/* Transmit a fully formatted Geneve frame.
Andy Zhou0b5e8b82014-10-03 15:35:28 -070099 *
100 * When calling this function. The skb->data should point
101 * to the geneve header which is fully formed.
102 *
103 * This function will add other UDP tunnel headers.
104 */
105int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
106 struct sk_buff *skb, __be32 src, __be32 dst, __u8 tos,
107 __u8 ttl, __be16 df, __be16 src_port, __be16 dst_port,
108 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
109 bool xnet)
110{
111 struct genevehdr *gnvh;
112 int min_headroom;
113 int err;
114
115 skb = udp_tunnel_handle_offloads(skb, !gs->sock->sk->sk_no_check_tx);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800116 if (IS_ERR(skb))
117 return PTR_ERR(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700118
119 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
120 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr)
121 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
122
123 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800124 if (unlikely(err)) {
125 kfree_skb(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700126 return err;
Pravin B Shelar997e0682014-12-23 16:20:32 -0800127 }
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700128
Jiri Pirko59682502014-11-19 14:04:59 +0100129 skb = vlan_hwaccel_push_inside(skb);
130 if (unlikely(!skb))
131 return -ENOMEM;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700132
133 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
134 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
135
Jesse Gross45cac462014-11-03 19:38:37 -0800136 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
137
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700138 return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst,
139 tos, ttl, df, src_port, dst_port, xnet);
140}
141EXPORT_SYMBOL_GPL(geneve_xmit_skb);
142
Joe Stringera4c9ea52014-12-30 19:10:16 -0800143static int geneve_hlen(struct genevehdr *gh)
144{
145 return sizeof(*gh) + gh->opt_len * 4;
146}
147
148static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
149 struct sk_buff *skb)
150{
151 struct sk_buff *p, **pp = NULL;
152 struct genevehdr *gh, *gh2;
153 unsigned int hlen, gh_len, off_gnv;
154 const struct packet_offload *ptype;
155 __be16 type;
156 int flush = 1;
157
158 off_gnv = skb_gro_offset(skb);
159 hlen = off_gnv + sizeof(*gh);
160 gh = skb_gro_header_fast(skb, off_gnv);
161 if (skb_gro_header_hard(skb, hlen)) {
162 gh = skb_gro_header_slow(skb, hlen, off_gnv);
163 if (unlikely(!gh))
164 goto out;
165 }
166
167 if (gh->ver != GENEVE_VER || gh->oam)
168 goto out;
169 gh_len = geneve_hlen(gh);
170
171 hlen = off_gnv + gh_len;
172 if (skb_gro_header_hard(skb, hlen)) {
173 gh = skb_gro_header_slow(skb, hlen, off_gnv);
174 if (unlikely(!gh))
175 goto out;
176 }
177
178 flush = 0;
179
180 for (p = *head; p; p = p->next) {
181 if (!NAPI_GRO_CB(p)->same_flow)
182 continue;
183
184 gh2 = (struct genevehdr *)(p->data + off_gnv);
185 if (gh->opt_len != gh2->opt_len ||
186 memcmp(gh, gh2, gh_len)) {
187 NAPI_GRO_CB(p)->same_flow = 0;
188 continue;
189 }
190 }
191
192 type = gh->proto_type;
193
194 rcu_read_lock();
195 ptype = gro_find_receive_by_type(type);
196 if (ptype == NULL) {
197 flush = 1;
198 goto out_unlock;
199 }
200
201 skb_gro_pull(skb, gh_len);
202 skb_gro_postpull_rcsum(skb, gh, gh_len);
203 pp = ptype->callbacks.gro_receive(head, skb);
204
205out_unlock:
206 rcu_read_unlock();
207out:
208 NAPI_GRO_CB(skb)->flush |= flush;
209
210 return pp;
211}
212
213static int geneve_gro_complete(struct sk_buff *skb, int nhoff)
214{
215 struct genevehdr *gh;
216 struct packet_offload *ptype;
217 __be16 type;
218 int gh_len;
219 int err = -ENOSYS;
220
221 udp_tunnel_gro_complete(skb, nhoff);
222
223 gh = (struct genevehdr *)(skb->data + nhoff);
224 gh_len = geneve_hlen(gh);
225 type = gh->proto_type;
226
227 rcu_read_lock();
228 ptype = gro_find_complete_by_type(type);
229 if (ptype != NULL)
230 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
231
232 rcu_read_unlock();
233 return err;
234}
235
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700236static void geneve_notify_add_rx_port(struct geneve_sock *gs)
237{
238 struct sock *sk = gs->sock->sk;
239 sa_family_t sa_family = sk->sk_family;
240 int err;
241
242 if (sa_family == AF_INET) {
243 err = udp_add_offload(&gs->udp_offloads);
244 if (err)
245 pr_warn("geneve: udp_add_offload failed with status %d\n",
246 err);
247 }
248}
249
Jesse Gross7ed767f2014-12-16 18:25:31 -0800250static void geneve_notify_del_rx_port(struct geneve_sock *gs)
251{
252 struct sock *sk = gs->sock->sk;
253 sa_family_t sa_family = sk->sk_family;
254
255 if (sa_family == AF_INET)
256 udp_del_offload(&gs->udp_offloads);
257}
258
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700259/* Callback from net/ipv4/udp.c to receive packets */
260static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
261{
262 struct genevehdr *geneveh;
263 struct geneve_sock *gs;
264 int opts_len;
265
266 /* Need Geneve and inner Ethernet header to be present */
267 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
268 goto error;
269
270 /* Return packets with reserved bits set */
271 geneveh = geneve_hdr(skb);
272
273 if (unlikely(geneveh->ver != GENEVE_VER))
274 goto error;
275
276 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
277 goto error;
278
279 opts_len = geneveh->opt_len * 4;
280 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
281 htons(ETH_P_TEB)))
282 goto drop;
283
284 gs = rcu_dereference_sk_user_data(sk);
285 if (!gs)
286 goto drop;
287
288 gs->rcv(gs, skb);
289 return 0;
290
291drop:
292 /* Consume bad packet */
293 kfree_skb(skb);
294 return 0;
295
296error:
297 /* Let the UDP layer deal with the skb */
298 return 1;
299}
300
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700301static struct socket *geneve_create_sock(struct net *net, bool ipv6,
302 __be16 port)
303{
304 struct socket *sock;
305 struct udp_port_cfg udp_conf;
306 int err;
307
308 memset(&udp_conf, 0, sizeof(udp_conf));
309
310 if (ipv6) {
311 udp_conf.family = AF_INET6;
312 } else {
313 udp_conf.family = AF_INET;
Andy Zhou42350dc2014-10-06 13:22:50 -0700314 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700315 }
316
317 udp_conf.local_udp_port = port;
318
319 /* Open UDP socket */
320 err = udp_sock_create(net, &udp_conf, &sock);
321 if (err < 0)
322 return ERR_PTR(err);
323
324 return sock;
325}
326
327/* Create new listen socket if needed */
328static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
329 geneve_rcv_t *rcv, void *data,
330 bool ipv6)
331{
Jesse Grossdf5dba82015-01-02 18:26:04 -0800332 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700333 struct geneve_sock *gs;
334 struct socket *sock;
335 struct udp_tunnel_sock_cfg tunnel_cfg;
336
337 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
338 if (!gs)
339 return ERR_PTR(-ENOMEM);
340
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700341 sock = geneve_create_sock(net, ipv6, port);
342 if (IS_ERR(sock)) {
343 kfree(gs);
344 return ERR_CAST(sock);
345 }
346
347 gs->sock = sock;
Jesse Gross829a3ad2015-01-02 18:26:03 -0800348 gs->refcnt = 1;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700349 gs->rcv = rcv;
350 gs->rcv_data = data;
351
352 /* Initialize the geneve udp offloads structure */
353 gs->udp_offloads.port = port;
Joe Stringera4c9ea52014-12-30 19:10:16 -0800354 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
355 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700356 geneve_notify_add_rx_port(gs);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700357
358 /* Mark socket as an encapsulation socket */
359 tunnel_cfg.sk_user_data = gs;
360 tunnel_cfg.encap_type = 1;
361 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
362 tunnel_cfg.encap_destroy = NULL;
363 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
364
Jesse Grossdf5dba82015-01-02 18:26:04 -0800365 list_add(&gs->list, &gn->sock_list);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800366
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700367 return gs;
368}
369
370struct geneve_sock *geneve_sock_add(struct net *net, __be16 port,
371 geneve_rcv_t *rcv, void *data,
372 bool no_share, bool ipv6)
373{
374 struct geneve_sock *gs;
375
Jesse Gross829a3ad2015-01-02 18:26:03 -0800376 mutex_lock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700377
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700378 gs = geneve_find_sock(net, port);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800379 if (gs) {
380 if (!no_share && gs->rcv == rcv)
381 gs->refcnt++;
382 else
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700383 gs = ERR_PTR(-EBUSY);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800384 } else {
385 gs = geneve_socket_create(net, port, rcv, data, ipv6);
386 }
Jesse Gross12069402014-12-16 18:25:32 -0800387
Jesse Gross829a3ad2015-01-02 18:26:03 -0800388 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700389
390 return gs;
391}
392EXPORT_SYMBOL_GPL(geneve_sock_add);
393
394void geneve_sock_release(struct geneve_sock *gs)
395{
Jesse Gross829a3ad2015-01-02 18:26:03 -0800396 mutex_lock(&geneve_mutex);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800397
Jesse Gross829a3ad2015-01-02 18:26:03 -0800398 if (--gs->refcnt)
399 goto unlock;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700400
Jesse Grossdf5dba82015-01-02 18:26:04 -0800401 list_del(&gs->list);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800402 geneve_notify_del_rx_port(gs);
Jesse Gross61f3cad2015-01-02 18:26:02 -0800403 udp_tunnel_sock_release(gs->sock);
404 kfree_rcu(gs, rcu);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800405
406unlock:
407 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700408}
409EXPORT_SYMBOL_GPL(geneve_sock_release);
410
411static __net_init int geneve_init_net(struct net *net)
412{
413 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700414
Jesse Grossdf5dba82015-01-02 18:26:04 -0800415 INIT_LIST_HEAD(&gn->sock_list);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700416
417 return 0;
418}
419
420static struct pernet_operations geneve_net_ops = {
421 .init = geneve_init_net,
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700422 .id = &geneve_net_id,
423 .size = sizeof(struct geneve_net),
424};
425
426static int __init geneve_init_module(void)
427{
428 int rc;
429
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700430 rc = register_pernet_subsys(&geneve_net_ops);
431 if (rc)
432 return rc;
433
434 pr_info("Geneve driver\n");
435
436 return 0;
437}
Jesse Gross829a3ad2015-01-02 18:26:03 -0800438module_init(geneve_init_module);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700439
440static void __exit geneve_cleanup_module(void)
441{
Jesse Grossd3ca9ea2014-11-03 19:38:38 -0800442 unregister_pernet_subsys(&geneve_net_ops);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700443}
444module_exit(geneve_cleanup_module);
445
446MODULE_LICENSE("GPL");
447MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>");
448MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic");
449MODULE_ALIAS_RTNL_LINK("geneve");