2 * Geneve: Generic Network Virtualization Encapsulation
4 * Copyright (c) 2014 Nicira, Inc.
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/list.h>
21 #include <linux/netdevice.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/ethtool.h>
30 #include <linux/mutex.h>
32 #include <net/ndisc.h>
34 #include <net/ip_tunnels.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)
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
53 /* Protects sock_list and refcounts. */
54 static DEFINE_MUTEX(geneve_mutex);
56 /* per-network namespace private data for this module */
58 struct list_head sock_list;
61 static int geneve_net_id;
63 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
65 return (struct genevehdr *)(udp_hdr(skb) + 1);
68 /* Find geneve socket based on network namespace and UDP port */
69 static struct geneve_sock *geneve_find_sock(struct net *net, __be16 port)
71 struct geneve_net *gn = net_generic(net, geneve_net_id);
72 struct geneve_sock *gs;
74 list_for_each_entry(gs, &gn->sock_list, list) {
75 if (inet_sk(gs->sock->sk)->inet_sport == port)
82 static void geneve_build_header(struct genevehdr *geneveh,
83 __be16 tun_flags, u8 vni[3],
84 u8 options_len, u8 *options)
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);
91 memcpy(geneveh->vni, vni, 3);
92 geneveh->proto_type = htons(ETH_P_TEB);
95 memcpy(geneveh->options, options, options_len);
98 /* Transmit a fully formatted Geneve frame.
100 * When calling this function. The skb->data should point
101 * to the geneve header which is fully formed.
103 * This function will add other UDP tunnel headers.
105 int 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,
111 struct genevehdr *gnvh;
115 skb = udp_tunnel_handle_offloads(skb, !gs->sock->sk->sk_no_check_tx);
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);
123 err = skb_cow_head(skb, min_headroom);
129 skb = vlan_hwaccel_push_inside(skb);
133 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
134 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
136 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
138 return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst,
139 tos, ttl, df, src_port, dst_port, xnet);
141 EXPORT_SYMBOL_GPL(geneve_xmit_skb);
143 static int geneve_hlen(struct genevehdr *gh)
145 return sizeof(*gh) + gh->opt_len * 4;
148 static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
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;
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);
167 if (gh->ver != GENEVE_VER || gh->oam)
169 gh_len = geneve_hlen(gh);
171 hlen = off_gnv + gh_len;
172 if (skb_gro_header_hard(skb, hlen)) {
173 gh = skb_gro_header_slow(skb, hlen, off_gnv);
180 for (p = *head; p; p = p->next) {
181 if (!NAPI_GRO_CB(p)->same_flow)
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;
192 type = gh->proto_type;
195 ptype = gro_find_receive_by_type(type);
201 skb_gro_pull(skb, gh_len);
202 skb_gro_postpull_rcsum(skb, gh, gh_len);
203 pp = ptype->callbacks.gro_receive(head, skb);
208 NAPI_GRO_CB(skb)->flush |= flush;
213 static int geneve_gro_complete(struct sk_buff *skb, int nhoff)
215 struct genevehdr *gh;
216 struct packet_offload *ptype;
221 udp_tunnel_gro_complete(skb, nhoff);
223 gh = (struct genevehdr *)(skb->data + nhoff);
224 gh_len = geneve_hlen(gh);
225 type = gh->proto_type;
228 ptype = gro_find_complete_by_type(type);
230 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
236 static void geneve_notify_add_rx_port(struct geneve_sock *gs)
238 struct sock *sk = gs->sock->sk;
239 sa_family_t sa_family = sk->sk_family;
242 if (sa_family == AF_INET) {
243 err = udp_add_offload(&gs->udp_offloads);
245 pr_warn("geneve: udp_add_offload failed with status %d\n",
250 static void geneve_notify_del_rx_port(struct geneve_sock *gs)
252 struct sock *sk = gs->sock->sk;
253 sa_family_t sa_family = sk->sk_family;
255 if (sa_family == AF_INET)
256 udp_del_offload(&gs->udp_offloads);
259 /* Callback from net/ipv4/udp.c to receive packets */
260 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
262 struct genevehdr *geneveh;
263 struct geneve_sock *gs;
266 /* Need Geneve and inner Ethernet header to be present */
267 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
270 /* Return packets with reserved bits set */
271 geneveh = geneve_hdr(skb);
273 if (unlikely(geneveh->ver != GENEVE_VER))
276 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
279 opts_len = geneveh->opt_len * 4;
280 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
284 gs = rcu_dereference_sk_user_data(sk);
292 /* Consume bad packet */
297 /* Let the UDP layer deal with the skb */
301 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
305 struct udp_port_cfg udp_conf;
308 memset(&udp_conf, 0, sizeof(udp_conf));
311 udp_conf.family = AF_INET6;
313 udp_conf.family = AF_INET;
314 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
317 udp_conf.local_udp_port = port;
319 /* Open UDP socket */
320 err = udp_sock_create(net, &udp_conf, &sock);
327 /* Create new listen socket if needed */
328 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
329 geneve_rcv_t *rcv, void *data,
332 struct geneve_net *gn = net_generic(net, geneve_net_id);
333 struct geneve_sock *gs;
335 struct udp_tunnel_sock_cfg tunnel_cfg;
337 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
339 return ERR_PTR(-ENOMEM);
341 sock = geneve_create_sock(net, ipv6, port);
344 return ERR_CAST(sock);
352 /* Initialize the geneve udp offloads structure */
353 gs->udp_offloads.port = port;
354 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
355 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
356 geneve_notify_add_rx_port(gs);
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);
365 list_add(&gs->list, &gn->sock_list);
370 struct geneve_sock *geneve_sock_add(struct net *net, __be16 port,
371 geneve_rcv_t *rcv, void *data,
372 bool no_share, bool ipv6)
374 struct geneve_sock *gs;
376 mutex_lock(&geneve_mutex);
378 gs = geneve_find_sock(net, port);
380 if (!no_share && gs->rcv == rcv)
383 gs = ERR_PTR(-EBUSY);
385 gs = geneve_socket_create(net, port, rcv, data, ipv6);
388 mutex_unlock(&geneve_mutex);
392 EXPORT_SYMBOL_GPL(geneve_sock_add);
394 void geneve_sock_release(struct geneve_sock *gs)
396 mutex_lock(&geneve_mutex);
402 geneve_notify_del_rx_port(gs);
403 udp_tunnel_sock_release(gs->sock);
407 mutex_unlock(&geneve_mutex);
409 EXPORT_SYMBOL_GPL(geneve_sock_release);
411 static __net_init int geneve_init_net(struct net *net)
413 struct geneve_net *gn = net_generic(net, geneve_net_id);
415 INIT_LIST_HEAD(&gn->sock_list);
420 static struct pernet_operations geneve_net_ops = {
421 .init = geneve_init_net,
422 .id = &geneve_net_id,
423 .size = sizeof(struct geneve_net),
426 static int __init geneve_init_module(void)
430 rc = register_pernet_subsys(&geneve_net_ops);
434 pr_info("Geneve driver\n");
438 module_init(geneve_init_module);
440 static void __exit geneve_cleanup_module(void)
442 unregister_pernet_subsys(&geneve_net_ops);
444 module_exit(geneve_cleanup_module);
446 MODULE_LICENSE("GPL");
447 MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>");
448 MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic");
449 MODULE_ALIAS_RTNL_LINK("geneve");