blob: 23744c7a97188a0cd3576d29cb82682c1df0fc2f [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
Jesse Gross46b1e4f2015-01-02 18:26:05 -080068static struct geneve_sock *geneve_find_sock(struct net *net,
69 sa_family_t family, __be16 port)
Andy Zhou0b5e8b82014-10-03 15:35:28 -070070{
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) {
Jesse Gross46b1e4f2015-01-02 18:26:05 -080075 if (inet_sk(gs->sock->sk)->inet_sport == port &&
76 inet_sk(gs->sock->sk)->sk.sk_family == family)
Andy Zhou0b5e8b82014-10-03 15:35:28 -070077 return gs;
78 }
79
80 return NULL;
81}
82
83static void geneve_build_header(struct genevehdr *geneveh,
84 __be16 tun_flags, u8 vni[3],
85 u8 options_len, u8 *options)
86{
87 geneveh->ver = GENEVE_VER;
88 geneveh->opt_len = options_len / 4;
89 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
90 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
91 geneveh->rsvd1 = 0;
92 memcpy(geneveh->vni, vni, 3);
93 geneveh->proto_type = htons(ETH_P_TEB);
94 geneveh->rsvd2 = 0;
95
96 memcpy(geneveh->options, options, options_len);
97}
98
stephen hemmingerf4e715c2014-10-29 16:05:06 -070099/* Transmit a fully formatted Geneve frame.
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700100 *
101 * When calling this function. The skb->data should point
102 * to the geneve header which is fully formed.
103 *
104 * This function will add other UDP tunnel headers.
105 */
106int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
107 struct sk_buff *skb, __be32 src, __be32 dst, __u8 tos,
108 __u8 ttl, __be16 df, __be16 src_port, __be16 dst_port,
109 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
110 bool xnet)
111{
112 struct genevehdr *gnvh;
113 int min_headroom;
114 int err;
115
116 skb = udp_tunnel_handle_offloads(skb, !gs->sock->sk->sk_no_check_tx);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800117 if (IS_ERR(skb))
118 return PTR_ERR(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700119
120 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
121 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100122 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700123
124 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800125 if (unlikely(err)) {
126 kfree_skb(skb);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700127 return err;
Pravin B Shelar997e0682014-12-23 16:20:32 -0800128 }
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700129
Jiri Pirko59682502014-11-19 14:04:59 +0100130 skb = vlan_hwaccel_push_inside(skb);
131 if (unlikely(!skb))
132 return -ENOMEM;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700133
134 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
135 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
136
Jesse Gross45cac462014-11-03 19:38:37 -0800137 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
138
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700139 return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst,
140 tos, ttl, df, src_port, dst_port, xnet);
141}
142EXPORT_SYMBOL_GPL(geneve_xmit_skb);
143
Joe Stringera4c9ea52014-12-30 19:10:16 -0800144static int geneve_hlen(struct genevehdr *gh)
145{
146 return sizeof(*gh) + gh->opt_len * 4;
147}
148
149static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
150 struct sk_buff *skb)
151{
152 struct sk_buff *p, **pp = NULL;
153 struct genevehdr *gh, *gh2;
154 unsigned int hlen, gh_len, off_gnv;
155 const struct packet_offload *ptype;
156 __be16 type;
157 int flush = 1;
158
159 off_gnv = skb_gro_offset(skb);
160 hlen = off_gnv + sizeof(*gh);
161 gh = skb_gro_header_fast(skb, off_gnv);
162 if (skb_gro_header_hard(skb, hlen)) {
163 gh = skb_gro_header_slow(skb, hlen, off_gnv);
164 if (unlikely(!gh))
165 goto out;
166 }
167
168 if (gh->ver != GENEVE_VER || gh->oam)
169 goto out;
170 gh_len = geneve_hlen(gh);
171
172 hlen = off_gnv + gh_len;
173 if (skb_gro_header_hard(skb, hlen)) {
174 gh = skb_gro_header_slow(skb, hlen, off_gnv);
175 if (unlikely(!gh))
176 goto out;
177 }
178
179 flush = 0;
180
181 for (p = *head; p; p = p->next) {
182 if (!NAPI_GRO_CB(p)->same_flow)
183 continue;
184
185 gh2 = (struct genevehdr *)(p->data + off_gnv);
186 if (gh->opt_len != gh2->opt_len ||
187 memcmp(gh, gh2, gh_len)) {
188 NAPI_GRO_CB(p)->same_flow = 0;
189 continue;
190 }
191 }
192
193 type = gh->proto_type;
194
195 rcu_read_lock();
196 ptype = gro_find_receive_by_type(type);
197 if (ptype == NULL) {
198 flush = 1;
199 goto out_unlock;
200 }
201
202 skb_gro_pull(skb, gh_len);
203 skb_gro_postpull_rcsum(skb, gh, gh_len);
204 pp = ptype->callbacks.gro_receive(head, skb);
205
206out_unlock:
207 rcu_read_unlock();
208out:
209 NAPI_GRO_CB(skb)->flush |= flush;
210
211 return pp;
212}
213
214static int geneve_gro_complete(struct sk_buff *skb, int nhoff)
215{
216 struct genevehdr *gh;
217 struct packet_offload *ptype;
218 __be16 type;
219 int gh_len;
220 int err = -ENOSYS;
221
222 udp_tunnel_gro_complete(skb, nhoff);
223
224 gh = (struct genevehdr *)(skb->data + nhoff);
225 gh_len = geneve_hlen(gh);
226 type = gh->proto_type;
227
228 rcu_read_lock();
229 ptype = gro_find_complete_by_type(type);
230 if (ptype != NULL)
231 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
232
233 rcu_read_unlock();
234 return err;
235}
236
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700237static void geneve_notify_add_rx_port(struct geneve_sock *gs)
238{
239 struct sock *sk = gs->sock->sk;
240 sa_family_t sa_family = sk->sk_family;
241 int err;
242
243 if (sa_family == AF_INET) {
244 err = udp_add_offload(&gs->udp_offloads);
245 if (err)
246 pr_warn("geneve: udp_add_offload failed with status %d\n",
247 err);
248 }
249}
250
Jesse Gross7ed767f2014-12-16 18:25:31 -0800251static void geneve_notify_del_rx_port(struct geneve_sock *gs)
252{
253 struct sock *sk = gs->sock->sk;
254 sa_family_t sa_family = sk->sk_family;
255
256 if (sa_family == AF_INET)
257 udp_del_offload(&gs->udp_offloads);
258}
259
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700260/* Callback from net/ipv4/udp.c to receive packets */
261static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
262{
263 struct genevehdr *geneveh;
264 struct geneve_sock *gs;
265 int opts_len;
266
267 /* Need Geneve and inner Ethernet header to be present */
268 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
269 goto error;
270
271 /* Return packets with reserved bits set */
272 geneveh = geneve_hdr(skb);
273
274 if (unlikely(geneveh->ver != GENEVE_VER))
275 goto error;
276
277 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
278 goto error;
279
280 opts_len = geneveh->opt_len * 4;
281 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
282 htons(ETH_P_TEB)))
283 goto drop;
284
285 gs = rcu_dereference_sk_user_data(sk);
286 if (!gs)
287 goto drop;
288
289 gs->rcv(gs, skb);
290 return 0;
291
292drop:
293 /* Consume bad packet */
294 kfree_skb(skb);
295 return 0;
296
297error:
298 /* Let the UDP layer deal with the skb */
299 return 1;
300}
301
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700302static struct socket *geneve_create_sock(struct net *net, bool ipv6,
303 __be16 port)
304{
305 struct socket *sock;
306 struct udp_port_cfg udp_conf;
307 int err;
308
309 memset(&udp_conf, 0, sizeof(udp_conf));
310
311 if (ipv6) {
312 udp_conf.family = AF_INET6;
313 } else {
314 udp_conf.family = AF_INET;
Andy Zhou42350dc2014-10-06 13:22:50 -0700315 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700316 }
317
318 udp_conf.local_udp_port = port;
319
320 /* Open UDP socket */
321 err = udp_sock_create(net, &udp_conf, &sock);
322 if (err < 0)
323 return ERR_PTR(err);
324
325 return sock;
326}
327
328/* Create new listen socket if needed */
329static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
330 geneve_rcv_t *rcv, void *data,
331 bool ipv6)
332{
Jesse Grossdf5dba82015-01-02 18:26:04 -0800333 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700334 struct geneve_sock *gs;
335 struct socket *sock;
336 struct udp_tunnel_sock_cfg tunnel_cfg;
337
338 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
339 if (!gs)
340 return ERR_PTR(-ENOMEM);
341
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700342 sock = geneve_create_sock(net, ipv6, port);
343 if (IS_ERR(sock)) {
344 kfree(gs);
345 return ERR_CAST(sock);
346 }
347
348 gs->sock = sock;
Jesse Gross829a3ad2015-01-02 18:26:03 -0800349 gs->refcnt = 1;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700350 gs->rcv = rcv;
351 gs->rcv_data = data;
352
353 /* Initialize the geneve udp offloads structure */
354 gs->udp_offloads.port = port;
Joe Stringera4c9ea52014-12-30 19:10:16 -0800355 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
356 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700357 geneve_notify_add_rx_port(gs);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700358
359 /* Mark socket as an encapsulation socket */
360 tunnel_cfg.sk_user_data = gs;
361 tunnel_cfg.encap_type = 1;
362 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
363 tunnel_cfg.encap_destroy = NULL;
364 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
365
Jesse Grossdf5dba82015-01-02 18:26:04 -0800366 list_add(&gs->list, &gn->sock_list);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800367
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700368 return gs;
369}
370
371struct geneve_sock *geneve_sock_add(struct net *net, __be16 port,
372 geneve_rcv_t *rcv, void *data,
373 bool no_share, bool ipv6)
374{
375 struct geneve_sock *gs;
376
Jesse Gross829a3ad2015-01-02 18:26:03 -0800377 mutex_lock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700378
Jesse Gross46b1e4f2015-01-02 18:26:05 -0800379 gs = geneve_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800380 if (gs) {
381 if (!no_share && gs->rcv == rcv)
382 gs->refcnt++;
383 else
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700384 gs = ERR_PTR(-EBUSY);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800385 } else {
386 gs = geneve_socket_create(net, port, rcv, data, ipv6);
387 }
Jesse Gross12069402014-12-16 18:25:32 -0800388
Jesse Gross829a3ad2015-01-02 18:26:03 -0800389 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700390
391 return gs;
392}
393EXPORT_SYMBOL_GPL(geneve_sock_add);
394
395void geneve_sock_release(struct geneve_sock *gs)
396{
Jesse Gross829a3ad2015-01-02 18:26:03 -0800397 mutex_lock(&geneve_mutex);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800398
Jesse Gross829a3ad2015-01-02 18:26:03 -0800399 if (--gs->refcnt)
400 goto unlock;
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700401
Jesse Grossdf5dba82015-01-02 18:26:04 -0800402 list_del(&gs->list);
Jesse Gross7ed767f2014-12-16 18:25:31 -0800403 geneve_notify_del_rx_port(gs);
Jesse Gross61f3cad2015-01-02 18:26:02 -0800404 udp_tunnel_sock_release(gs->sock);
405 kfree_rcu(gs, rcu);
Jesse Gross829a3ad2015-01-02 18:26:03 -0800406
407unlock:
408 mutex_unlock(&geneve_mutex);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700409}
410EXPORT_SYMBOL_GPL(geneve_sock_release);
411
412static __net_init int geneve_init_net(struct net *net)
413{
414 struct geneve_net *gn = net_generic(net, geneve_net_id);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700415
Jesse Grossdf5dba82015-01-02 18:26:04 -0800416 INIT_LIST_HEAD(&gn->sock_list);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700417
418 return 0;
419}
420
421static struct pernet_operations geneve_net_ops = {
422 .init = geneve_init_net,
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700423 .id = &geneve_net_id,
424 .size = sizeof(struct geneve_net),
425};
426
427static int __init geneve_init_module(void)
428{
429 int rc;
430
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700431 rc = register_pernet_subsys(&geneve_net_ops);
432 if (rc)
433 return rc;
434
435 pr_info("Geneve driver\n");
436
437 return 0;
438}
Jesse Gross829a3ad2015-01-02 18:26:03 -0800439module_init(geneve_init_module);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700440
441static void __exit geneve_cleanup_module(void)
442{
Jesse Grossd3ca9ea2014-11-03 19:38:38 -0800443 unregister_pernet_subsys(&geneve_net_ops);
Andy Zhou0b5e8b82014-10-03 15:35:28 -0700444}
445module_exit(geneve_cleanup_module);
446
447MODULE_LICENSE("GPL");
448MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>");
449MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic");
450MODULE_ALIAS_RTNL_LINK("geneve");