Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 1 | /* |
| 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 */ |
| 57 | struct geneve_net { |
| 58 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
| 59 | spinlock_t sock_lock; /* Protects sock_list */ |
| 60 | }; |
| 61 | |
| 62 | static int geneve_net_id; |
| 63 | |
| 64 | static struct workqueue_struct *geneve_wq; |
| 65 | |
| 66 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
| 67 | { |
| 68 | return (struct genevehdr *)(udp_hdr(skb) + 1); |
| 69 | } |
| 70 | |
| 71 | static 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 */ |
| 79 | static 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 | |
| 91 | static 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 hemminger | f4e715c | 2014-10-29 16:05:06 -0700 | [diff] [blame] | 107 | /* Transmit a fully formatted Geneve frame. |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 108 | * |
| 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 | */ |
| 114 | int 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 Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame^] | 125 | if (IS_ERR(skb)) |
| 126 | return PTR_ERR(skb); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 127 | |
| 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 Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame^] | 133 | if (unlikely(err)) { |
| 134 | kfree_skb(skb); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 135 | return err; |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame^] | 136 | } |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 137 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 138 | skb = vlan_hwaccel_push_inside(skb); |
| 139 | if (unlikely(!skb)) |
| 140 | return -ENOMEM; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 141 | |
| 142 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); |
| 143 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); |
| 144 | |
Jesse Gross | 45cac46 | 2014-11-03 19:38:37 -0800 | [diff] [blame] | 145 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 146 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 147 | return udp_tunnel_xmit_skb(gs->sock, rt, skb, src, dst, |
| 148 | tos, ttl, df, src_port, dst_port, xnet); |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(geneve_xmit_skb); |
| 151 | |
| 152 | static void geneve_notify_add_rx_port(struct geneve_sock *gs) |
| 153 | { |
| 154 | struct sock *sk = gs->sock->sk; |
| 155 | sa_family_t sa_family = sk->sk_family; |
| 156 | int err; |
| 157 | |
| 158 | if (sa_family == AF_INET) { |
| 159 | err = udp_add_offload(&gs->udp_offloads); |
| 160 | if (err) |
| 161 | pr_warn("geneve: udp_add_offload failed with status %d\n", |
| 162 | err); |
| 163 | } |
| 164 | } |
| 165 | |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 166 | static void geneve_notify_del_rx_port(struct geneve_sock *gs) |
| 167 | { |
| 168 | struct sock *sk = gs->sock->sk; |
| 169 | sa_family_t sa_family = sk->sk_family; |
| 170 | |
| 171 | if (sa_family == AF_INET) |
| 172 | udp_del_offload(&gs->udp_offloads); |
| 173 | } |
| 174 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 175 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 176 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 177 | { |
| 178 | struct genevehdr *geneveh; |
| 179 | struct geneve_sock *gs; |
| 180 | int opts_len; |
| 181 | |
| 182 | /* Need Geneve and inner Ethernet header to be present */ |
| 183 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
| 184 | goto error; |
| 185 | |
| 186 | /* Return packets with reserved bits set */ |
| 187 | geneveh = geneve_hdr(skb); |
| 188 | |
| 189 | if (unlikely(geneveh->ver != GENEVE_VER)) |
| 190 | goto error; |
| 191 | |
| 192 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) |
| 193 | goto error; |
| 194 | |
| 195 | opts_len = geneveh->opt_len * 4; |
| 196 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, |
| 197 | htons(ETH_P_TEB))) |
| 198 | goto drop; |
| 199 | |
| 200 | gs = rcu_dereference_sk_user_data(sk); |
| 201 | if (!gs) |
| 202 | goto drop; |
| 203 | |
| 204 | gs->rcv(gs, skb); |
| 205 | return 0; |
| 206 | |
| 207 | drop: |
| 208 | /* Consume bad packet */ |
| 209 | kfree_skb(skb); |
| 210 | return 0; |
| 211 | |
| 212 | error: |
| 213 | /* Let the UDP layer deal with the skb */ |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | static void geneve_del_work(struct work_struct *work) |
| 218 | { |
| 219 | struct geneve_sock *gs = container_of(work, struct geneve_sock, |
| 220 | del_work); |
| 221 | |
| 222 | udp_tunnel_sock_release(gs->sock); |
| 223 | kfree_rcu(gs, rcu); |
| 224 | } |
| 225 | |
| 226 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
| 227 | __be16 port) |
| 228 | { |
| 229 | struct socket *sock; |
| 230 | struct udp_port_cfg udp_conf; |
| 231 | int err; |
| 232 | |
| 233 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 234 | |
| 235 | if (ipv6) { |
| 236 | udp_conf.family = AF_INET6; |
| 237 | } else { |
| 238 | udp_conf.family = AF_INET; |
Andy Zhou | 42350dc | 2014-10-06 13:22:50 -0700 | [diff] [blame] | 239 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | udp_conf.local_udp_port = port; |
| 243 | |
| 244 | /* Open UDP socket */ |
| 245 | err = udp_sock_create(net, &udp_conf, &sock); |
| 246 | if (err < 0) |
| 247 | return ERR_PTR(err); |
| 248 | |
| 249 | return sock; |
| 250 | } |
| 251 | |
| 252 | /* Create new listen socket if needed */ |
| 253 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, |
| 254 | geneve_rcv_t *rcv, void *data, |
| 255 | bool ipv6) |
| 256 | { |
| 257 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 258 | struct geneve_sock *gs; |
| 259 | struct socket *sock; |
| 260 | struct udp_tunnel_sock_cfg tunnel_cfg; |
| 261 | |
| 262 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); |
| 263 | if (!gs) |
| 264 | return ERR_PTR(-ENOMEM); |
| 265 | |
| 266 | INIT_WORK(&gs->del_work, geneve_del_work); |
| 267 | |
| 268 | sock = geneve_create_sock(net, ipv6, port); |
| 269 | if (IS_ERR(sock)) { |
| 270 | kfree(gs); |
| 271 | return ERR_CAST(sock); |
| 272 | } |
| 273 | |
| 274 | gs->sock = sock; |
| 275 | atomic_set(&gs->refcnt, 1); |
| 276 | gs->rcv = rcv; |
| 277 | gs->rcv_data = data; |
| 278 | |
| 279 | /* Initialize the geneve udp offloads structure */ |
| 280 | gs->udp_offloads.port = port; |
| 281 | gs->udp_offloads.callbacks.gro_receive = NULL; |
| 282 | gs->udp_offloads.callbacks.gro_complete = NULL; |
| 283 | |
| 284 | spin_lock(&gn->sock_lock); |
| 285 | hlist_add_head_rcu(&gs->hlist, gs_head(net, port)); |
| 286 | geneve_notify_add_rx_port(gs); |
| 287 | spin_unlock(&gn->sock_lock); |
| 288 | |
| 289 | /* Mark socket as an encapsulation socket */ |
| 290 | tunnel_cfg.sk_user_data = gs; |
| 291 | tunnel_cfg.encap_type = 1; |
| 292 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
| 293 | tunnel_cfg.encap_destroy = NULL; |
| 294 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
| 295 | |
| 296 | return gs; |
| 297 | } |
| 298 | |
| 299 | struct geneve_sock *geneve_sock_add(struct net *net, __be16 port, |
| 300 | geneve_rcv_t *rcv, void *data, |
| 301 | bool no_share, bool ipv6) |
| 302 | { |
Jesse Gross | 1206940 | 2014-12-16 18:25:32 -0800 | [diff] [blame] | 303 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 304 | struct geneve_sock *gs; |
| 305 | |
| 306 | gs = geneve_socket_create(net, port, rcv, data, ipv6); |
| 307 | if (!IS_ERR(gs)) |
| 308 | return gs; |
| 309 | |
| 310 | if (no_share) /* Return error if sharing is not allowed. */ |
| 311 | return ERR_PTR(-EINVAL); |
| 312 | |
Jesse Gross | 1206940 | 2014-12-16 18:25:32 -0800 | [diff] [blame] | 313 | spin_lock(&gn->sock_lock); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 314 | gs = geneve_find_sock(net, port); |
Jesse Gross | 1206940 | 2014-12-16 18:25:32 -0800 | [diff] [blame] | 315 | if (gs && ((gs->rcv != rcv) || |
| 316 | !atomic_add_unless(&gs->refcnt, 1, 0))) |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 317 | gs = ERR_PTR(-EBUSY); |
Jesse Gross | 1206940 | 2014-12-16 18:25:32 -0800 | [diff] [blame] | 318 | spin_unlock(&gn->sock_lock); |
| 319 | |
| 320 | if (!gs) |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 321 | gs = ERR_PTR(-EINVAL); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 322 | |
| 323 | return gs; |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(geneve_sock_add); |
| 326 | |
| 327 | void geneve_sock_release(struct geneve_sock *gs) |
| 328 | { |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 329 | struct net *net = sock_net(gs->sock->sk); |
| 330 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 331 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 332 | if (!atomic_dec_and_test(&gs->refcnt)) |
| 333 | return; |
| 334 | |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 335 | spin_lock(&gn->sock_lock); |
| 336 | hlist_del_rcu(&gs->hlist); |
| 337 | geneve_notify_del_rx_port(gs); |
| 338 | spin_unlock(&gn->sock_lock); |
| 339 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 340 | queue_work(geneve_wq, &gs->del_work); |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(geneve_sock_release); |
| 343 | |
| 344 | static __net_init int geneve_init_net(struct net *net) |
| 345 | { |
| 346 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 347 | unsigned int h; |
| 348 | |
| 349 | spin_lock_init(&gn->sock_lock); |
| 350 | |
| 351 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 352 | INIT_HLIST_HEAD(&gn->sock_list[h]); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static struct pernet_operations geneve_net_ops = { |
| 358 | .init = geneve_init_net, |
| 359 | .exit = NULL, |
| 360 | .id = &geneve_net_id, |
| 361 | .size = sizeof(struct geneve_net), |
| 362 | }; |
| 363 | |
| 364 | static int __init geneve_init_module(void) |
| 365 | { |
| 366 | int rc; |
| 367 | |
| 368 | geneve_wq = alloc_workqueue("geneve", 0, 0); |
| 369 | if (!geneve_wq) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | rc = register_pernet_subsys(&geneve_net_ops); |
| 373 | if (rc) |
| 374 | return rc; |
| 375 | |
| 376 | pr_info("Geneve driver\n"); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | late_initcall(geneve_init_module); |
| 381 | |
| 382 | static void __exit geneve_cleanup_module(void) |
| 383 | { |
| 384 | destroy_workqueue(geneve_wq); |
Jesse Gross | d3ca9ea | 2014-11-03 19:38:38 -0800 | [diff] [blame] | 385 | unregister_pernet_subsys(&geneve_net_ops); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 386 | } |
| 387 | module_exit(geneve_cleanup_module); |
| 388 | |
| 389 | MODULE_LICENSE("GPL"); |
| 390 | MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>"); |
| 391 | MODULE_DESCRIPTION("Driver for GENEVE encapsulated traffic"); |
| 392 | MODULE_ALIAS_RTNL_LINK("geneve"); |