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> |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 20 | #include <linux/list.h> |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 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> |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 29 | #include <linux/ethtool.h> |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 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 | |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 53 | /* Protects sock_list and refcounts. */ |
| 54 | static DEFINE_MUTEX(geneve_mutex); |
| 55 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 56 | /* per-network namespace private data for this module */ |
| 57 | struct geneve_net { |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 58 | struct list_head sock_list; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int geneve_net_id; |
| 62 | |
Jesse Gross | 46b1e4f | 2015-01-02 18:26:05 -0800 | [diff] [blame] | 63 | static struct geneve_sock *geneve_find_sock(struct net *net, |
| 64 | sa_family_t family, __be16 port) |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 65 | { |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 66 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 67 | struct geneve_sock *gs; |
| 68 | |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 69 | list_for_each_entry(gs, &gn->sock_list, list) { |
Jesse Gross | 46b1e4f | 2015-01-02 18:26:05 -0800 | [diff] [blame] | 70 | if (inet_sk(gs->sock->sk)->inet_sport == port && |
| 71 | inet_sk(gs->sock->sk)->sk.sk_family == family) |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 72 | return gs; |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | static void geneve_build_header(struct genevehdr *geneveh, |
| 79 | __be16 tun_flags, u8 vni[3], |
| 80 | u8 options_len, u8 *options) |
| 81 | { |
| 82 | geneveh->ver = GENEVE_VER; |
| 83 | geneveh->opt_len = options_len / 4; |
| 84 | geneveh->oam = !!(tun_flags & TUNNEL_OAM); |
| 85 | geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT); |
| 86 | geneveh->rsvd1 = 0; |
| 87 | memcpy(geneveh->vni, vni, 3); |
| 88 | geneveh->proto_type = htons(ETH_P_TEB); |
| 89 | geneveh->rsvd2 = 0; |
| 90 | |
| 91 | memcpy(geneveh->options, options, options_len); |
| 92 | } |
| 93 | |
stephen hemminger | f4e715c | 2014-10-29 16:05:06 -0700 | [diff] [blame] | 94 | /* Transmit a fully formatted Geneve frame. |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 95 | * |
| 96 | * When calling this function. The skb->data should point |
| 97 | * to the geneve header which is fully formed. |
| 98 | * |
| 99 | * This function will add other UDP tunnel headers. |
| 100 | */ |
| 101 | int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt, |
| 102 | struct sk_buff *skb, __be32 src, __be32 dst, __u8 tos, |
| 103 | __u8 ttl, __be16 df, __be16 src_port, __be16 dst_port, |
| 104 | __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, |
Jesse Gross | b869387 | 2015-01-28 16:32:46 -0800 | [diff] [blame] | 105 | bool csum, bool xnet) |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 106 | { |
| 107 | struct genevehdr *gnvh; |
| 108 | int min_headroom; |
| 109 | int err; |
| 110 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 111 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
| 112 | + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr) |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 113 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 114 | |
| 115 | err = skb_cow_head(skb, min_headroom); |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 116 | if (unlikely(err)) { |
| 117 | kfree_skb(skb); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 118 | return err; |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 119 | } |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 120 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 121 | skb = vlan_hwaccel_push_inside(skb); |
| 122 | if (unlikely(!skb)) |
| 123 | return -ENOMEM; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 124 | |
Jesse Gross | b736a62 | 2015-04-09 11:19:14 -0700 | [diff] [blame] | 125 | skb = udp_tunnel_handle_offloads(skb, csum); |
| 126 | if (IS_ERR(skb)) |
| 127 | return PTR_ERR(skb); |
| 128 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 129 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); |
| 130 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); |
| 131 | |
Jesse Gross | 45cac46 | 2014-11-03 19:38:37 -0800 | [diff] [blame] | 132 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 133 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 134 | return udp_tunnel_xmit_skb(rt, gs->sock->sk, skb, src, dst, |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 135 | tos, ttl, df, src_port, dst_port, xnet, |
Jesse Gross | b869387 | 2015-01-28 16:32:46 -0800 | [diff] [blame] | 136 | !csum); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL_GPL(geneve_xmit_skb); |
| 139 | |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 140 | static int geneve_hlen(struct genevehdr *gh) |
| 141 | { |
| 142 | return sizeof(*gh) + gh->opt_len * 4; |
| 143 | } |
| 144 | |
| 145 | static struct sk_buff **geneve_gro_receive(struct sk_buff **head, |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 146 | struct sk_buff *skb, |
| 147 | struct udp_offload *uoff) |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 148 | { |
| 149 | struct sk_buff *p, **pp = NULL; |
| 150 | struct genevehdr *gh, *gh2; |
| 151 | unsigned int hlen, gh_len, off_gnv; |
| 152 | const struct packet_offload *ptype; |
| 153 | __be16 type; |
| 154 | int flush = 1; |
| 155 | |
| 156 | off_gnv = skb_gro_offset(skb); |
| 157 | hlen = off_gnv + sizeof(*gh); |
| 158 | gh = skb_gro_header_fast(skb, off_gnv); |
| 159 | if (skb_gro_header_hard(skb, hlen)) { |
| 160 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 161 | if (unlikely(!gh)) |
| 162 | goto out; |
| 163 | } |
| 164 | |
| 165 | if (gh->ver != GENEVE_VER || gh->oam) |
| 166 | goto out; |
| 167 | gh_len = geneve_hlen(gh); |
| 168 | |
| 169 | hlen = off_gnv + gh_len; |
| 170 | if (skb_gro_header_hard(skb, hlen)) { |
| 171 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 172 | if (unlikely(!gh)) |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | flush = 0; |
| 177 | |
| 178 | for (p = *head; p; p = p->next) { |
| 179 | if (!NAPI_GRO_CB(p)->same_flow) |
| 180 | continue; |
| 181 | |
| 182 | gh2 = (struct genevehdr *)(p->data + off_gnv); |
| 183 | if (gh->opt_len != gh2->opt_len || |
| 184 | memcmp(gh, gh2, gh_len)) { |
| 185 | NAPI_GRO_CB(p)->same_flow = 0; |
| 186 | continue; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | type = gh->proto_type; |
| 191 | |
| 192 | rcu_read_lock(); |
| 193 | ptype = gro_find_receive_by_type(type); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 194 | if (!ptype) { |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 195 | flush = 1; |
| 196 | goto out_unlock; |
| 197 | } |
| 198 | |
| 199 | skb_gro_pull(skb, gh_len); |
| 200 | skb_gro_postpull_rcsum(skb, gh, gh_len); |
| 201 | pp = ptype->callbacks.gro_receive(head, skb); |
| 202 | |
| 203 | out_unlock: |
| 204 | rcu_read_unlock(); |
| 205 | out: |
| 206 | NAPI_GRO_CB(skb)->flush |= flush; |
| 207 | |
| 208 | return pp; |
| 209 | } |
| 210 | |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 211 | static int geneve_gro_complete(struct sk_buff *skb, int nhoff, |
| 212 | struct udp_offload *uoff) |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 213 | { |
| 214 | struct genevehdr *gh; |
| 215 | struct packet_offload *ptype; |
| 216 | __be16 type; |
| 217 | int gh_len; |
| 218 | int err = -ENOSYS; |
| 219 | |
| 220 | udp_tunnel_gro_complete(skb, nhoff); |
| 221 | |
| 222 | gh = (struct genevehdr *)(skb->data + nhoff); |
| 223 | gh_len = geneve_hlen(gh); |
| 224 | type = gh->proto_type; |
| 225 | |
| 226 | rcu_read_lock(); |
| 227 | ptype = gro_find_complete_by_type(type); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 228 | if (ptype) |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 229 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); |
| 230 | |
| 231 | rcu_read_unlock(); |
| 232 | return err; |
| 233 | } |
| 234 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 235 | static void geneve_notify_add_rx_port(struct geneve_sock *gs) |
| 236 | { |
| 237 | struct sock *sk = gs->sock->sk; |
| 238 | sa_family_t sa_family = sk->sk_family; |
| 239 | int err; |
| 240 | |
| 241 | if (sa_family == AF_INET) { |
| 242 | err = udp_add_offload(&gs->udp_offloads); |
| 243 | if (err) |
| 244 | pr_warn("geneve: udp_add_offload failed with status %d\n", |
| 245 | err); |
| 246 | } |
| 247 | } |
| 248 | |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 249 | static void geneve_notify_del_rx_port(struct geneve_sock *gs) |
| 250 | { |
| 251 | struct sock *sk = gs->sock->sk; |
| 252 | sa_family_t sa_family = sk->sk_family; |
| 253 | |
| 254 | if (sa_family == AF_INET) |
| 255 | udp_del_offload(&gs->udp_offloads); |
| 256 | } |
| 257 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 258 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 259 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 260 | { |
| 261 | struct genevehdr *geneveh; |
| 262 | struct geneve_sock *gs; |
| 263 | int opts_len; |
| 264 | |
| 265 | /* Need Geneve and inner Ethernet header to be present */ |
| 266 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
| 267 | goto error; |
| 268 | |
| 269 | /* Return packets with reserved bits set */ |
| 270 | geneveh = geneve_hdr(skb); |
| 271 | |
| 272 | if (unlikely(geneveh->ver != GENEVE_VER)) |
| 273 | goto error; |
| 274 | |
| 275 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) |
| 276 | goto error; |
| 277 | |
| 278 | opts_len = geneveh->opt_len * 4; |
| 279 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, |
| 280 | htons(ETH_P_TEB))) |
| 281 | goto drop; |
| 282 | |
| 283 | gs = rcu_dereference_sk_user_data(sk); |
| 284 | if (!gs) |
| 285 | goto drop; |
| 286 | |
| 287 | gs->rcv(gs, skb); |
| 288 | return 0; |
| 289 | |
| 290 | drop: |
| 291 | /* Consume bad packet */ |
| 292 | kfree_skb(skb); |
| 293 | return 0; |
| 294 | |
| 295 | error: |
| 296 | /* Let the UDP layer deal with the skb */ |
| 297 | return 1; |
| 298 | } |
| 299 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 300 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
| 301 | __be16 port) |
| 302 | { |
| 303 | struct socket *sock; |
| 304 | struct udp_port_cfg udp_conf; |
| 305 | int err; |
| 306 | |
| 307 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 308 | |
| 309 | if (ipv6) { |
| 310 | udp_conf.family = AF_INET6; |
| 311 | } else { |
| 312 | udp_conf.family = AF_INET; |
Andy Zhou | 42350dc | 2014-10-06 13:22:50 -0700 | [diff] [blame] | 313 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | udp_conf.local_udp_port = port; |
| 317 | |
| 318 | /* Open UDP socket */ |
| 319 | err = udp_sock_create(net, &udp_conf, &sock); |
| 320 | if (err < 0) |
| 321 | return ERR_PTR(err); |
| 322 | |
| 323 | return sock; |
| 324 | } |
| 325 | |
| 326 | /* Create new listen socket if needed */ |
| 327 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, |
| 328 | geneve_rcv_t *rcv, void *data, |
| 329 | bool ipv6) |
| 330 | { |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 331 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 332 | struct geneve_sock *gs; |
| 333 | struct socket *sock; |
| 334 | struct udp_tunnel_sock_cfg tunnel_cfg; |
| 335 | |
| 336 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); |
| 337 | if (!gs) |
| 338 | return ERR_PTR(-ENOMEM); |
| 339 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 340 | sock = geneve_create_sock(net, ipv6, port); |
| 341 | if (IS_ERR(sock)) { |
| 342 | kfree(gs); |
| 343 | return ERR_CAST(sock); |
| 344 | } |
| 345 | |
| 346 | gs->sock = sock; |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 347 | gs->refcnt = 1; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 348 | gs->rcv = rcv; |
| 349 | gs->rcv_data = data; |
| 350 | |
| 351 | /* Initialize the geneve udp offloads structure */ |
| 352 | gs->udp_offloads.port = port; |
Joe Stringer | a4c9ea5 | 2014-12-30 19:10:16 -0800 | [diff] [blame] | 353 | gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive; |
| 354 | gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 355 | geneve_notify_add_rx_port(gs); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 356 | |
| 357 | /* Mark socket as an encapsulation socket */ |
| 358 | tunnel_cfg.sk_user_data = gs; |
| 359 | tunnel_cfg.encap_type = 1; |
| 360 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
| 361 | tunnel_cfg.encap_destroy = NULL; |
| 362 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
| 363 | |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 364 | list_add(&gs->list, &gn->sock_list); |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 365 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 366 | return gs; |
| 367 | } |
| 368 | |
| 369 | struct geneve_sock *geneve_sock_add(struct net *net, __be16 port, |
| 370 | geneve_rcv_t *rcv, void *data, |
| 371 | bool no_share, bool ipv6) |
| 372 | { |
| 373 | struct geneve_sock *gs; |
| 374 | |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 375 | mutex_lock(&geneve_mutex); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 376 | |
Jesse Gross | 46b1e4f | 2015-01-02 18:26:05 -0800 | [diff] [blame] | 377 | gs = geneve_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port); |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 378 | if (gs) { |
| 379 | if (!no_share && gs->rcv == rcv) |
| 380 | gs->refcnt++; |
| 381 | else |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 382 | gs = ERR_PTR(-EBUSY); |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 383 | } else { |
| 384 | gs = geneve_socket_create(net, port, rcv, data, ipv6); |
| 385 | } |
Jesse Gross | 1206940 | 2014-12-16 18:25:32 -0800 | [diff] [blame] | 386 | |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 387 | mutex_unlock(&geneve_mutex); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 388 | |
| 389 | return gs; |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(geneve_sock_add); |
| 392 | |
| 393 | void geneve_sock_release(struct geneve_sock *gs) |
| 394 | { |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 395 | mutex_lock(&geneve_mutex); |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 396 | |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 397 | if (--gs->refcnt) |
| 398 | goto unlock; |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 399 | |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 400 | list_del(&gs->list); |
Jesse Gross | 7ed767f | 2014-12-16 18:25:31 -0800 | [diff] [blame] | 401 | geneve_notify_del_rx_port(gs); |
Jesse Gross | 61f3cad | 2015-01-02 18:26:02 -0800 | [diff] [blame] | 402 | udp_tunnel_sock_release(gs->sock); |
| 403 | kfree_rcu(gs, rcu); |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 404 | |
| 405 | unlock: |
| 406 | mutex_unlock(&geneve_mutex); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL_GPL(geneve_sock_release); |
| 409 | |
| 410 | static __net_init int geneve_init_net(struct net *net) |
| 411 | { |
| 412 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 413 | |
Jesse Gross | df5dba8 | 2015-01-02 18:26:04 -0800 | [diff] [blame] | 414 | INIT_LIST_HEAD(&gn->sock_list); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static struct pernet_operations geneve_net_ops = { |
| 420 | .init = geneve_init_net, |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 421 | .id = &geneve_net_id, |
| 422 | .size = sizeof(struct geneve_net), |
| 423 | }; |
| 424 | |
| 425 | static int __init geneve_init_module(void) |
| 426 | { |
| 427 | int rc; |
| 428 | |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 429 | rc = register_pernet_subsys(&geneve_net_ops); |
| 430 | if (rc) |
| 431 | return rc; |
| 432 | |
John W. Linville | d37d29c | 2015-05-13 12:57:29 -0400 | [diff] [blame] | 433 | pr_info("Geneve core logic\n"); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | } |
Jesse Gross | 829a3ad | 2015-01-02 18:26:03 -0800 | [diff] [blame] | 437 | module_init(geneve_init_module); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 438 | |
| 439 | static void __exit geneve_cleanup_module(void) |
| 440 | { |
Jesse Gross | d3ca9ea | 2014-11-03 19:38:38 -0800 | [diff] [blame] | 441 | unregister_pernet_subsys(&geneve_net_ops); |
Andy Zhou | 0b5e8b8 | 2014-10-03 15:35:28 -0700 | [diff] [blame] | 442 | } |
| 443 | module_exit(geneve_cleanup_module); |
| 444 | |
| 445 | MODULE_LICENSE("GPL"); |
| 446 | MODULE_AUTHOR("Jesse Gross <jesse@nicira.com>"); |
John W. Linville | d37d29c | 2015-05-13 12:57:29 -0400 | [diff] [blame] | 447 | MODULE_DESCRIPTION("Driver library for GENEVE encapsulated traffic"); |