James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * L2TP core. |
| 3 | * |
| 4 | * Copyright (c) 2008,2009,2010 Katalix Systems Ltd |
| 5 | * |
| 6 | * This file contains some code of the original L2TPv2 pppol2tp |
| 7 | * driver, which has the following copyright: |
| 8 | * |
| 9 | * Authors: Martijn van Oosterhout <kleptog@svana.org> |
| 10 | * James Chapman (jchapman@katalix.com) |
| 11 | * Contributors: |
| 12 | * Michal Ostrowski <mostrows@speakeasy.net> |
| 13 | * Arnaldo Carvalho de Melo <acme@xconectiva.com.br> |
| 14 | * David S. Miller (davem@redhat.com) |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2 as |
| 18 | * published by the Free Software Foundation. |
| 19 | */ |
| 20 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/list.h> |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 26 | #include <linux/rculist.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/kthread.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/errno.h> |
| 35 | #include <linux/jiffies.h> |
| 36 | |
| 37 | #include <linux/netdevice.h> |
| 38 | #include <linux/net.h> |
| 39 | #include <linux/inetdevice.h> |
| 40 | #include <linux/skbuff.h> |
| 41 | #include <linux/init.h> |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 42 | #include <linux/in.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 43 | #include <linux/ip.h> |
| 44 | #include <linux/udp.h> |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 45 | #include <linux/l2tp.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 46 | #include <linux/hash.h> |
| 47 | #include <linux/sort.h> |
| 48 | #include <linux/file.h> |
| 49 | #include <linux/nsproxy.h> |
| 50 | #include <net/net_namespace.h> |
| 51 | #include <net/netns/generic.h> |
| 52 | #include <net/dst.h> |
| 53 | #include <net/ip.h> |
| 54 | #include <net/udp.h> |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 55 | #include <net/udp_tunnel.h> |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 56 | #include <net/inet_common.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 57 | #include <net/xfrm.h> |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 58 | #include <net/protocol.h> |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 59 | #include <net/inet6_connection_sock.h> |
| 60 | #include <net/inet_ecn.h> |
| 61 | #include <net/ip6_route.h> |
David S. Miller | d499bd2 | 2012-04-30 13:21:28 -0400 | [diff] [blame] | 62 | #include <net/ip6_checksum.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 63 | |
| 64 | #include <asm/byteorder.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 65 | #include <linux/atomic.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 66 | |
| 67 | #include "l2tp_core.h" |
| 68 | |
| 69 | #define L2TP_DRV_VERSION "V2.0" |
| 70 | |
| 71 | /* L2TP header constants */ |
| 72 | #define L2TP_HDRFLAG_T 0x8000 |
| 73 | #define L2TP_HDRFLAG_L 0x4000 |
| 74 | #define L2TP_HDRFLAG_S 0x0800 |
| 75 | #define L2TP_HDRFLAG_O 0x0200 |
| 76 | #define L2TP_HDRFLAG_P 0x0100 |
| 77 | |
| 78 | #define L2TP_HDR_VER_MASK 0x000F |
| 79 | #define L2TP_HDR_VER_2 0x0002 |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 80 | #define L2TP_HDR_VER_3 0x0003 |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 81 | |
| 82 | /* L2TPv3 default L2-specific sublayer */ |
| 83 | #define L2TP_SLFLAG_S 0x40000000 |
| 84 | #define L2TP_SL_SEQ_MASK 0x00ffffff |
| 85 | |
| 86 | #define L2TP_HDR_SIZE_SEQ 10 |
| 87 | #define L2TP_HDR_SIZE_NOSEQ 6 |
| 88 | |
| 89 | /* Default trace flags */ |
| 90 | #define L2TP_DEFAULT_DEBUG_FLAGS 0 |
| 91 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 92 | /* Private data stored for received packets in the skb. |
| 93 | */ |
| 94 | struct l2tp_skb_cb { |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 95 | u32 ns; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 96 | u16 has_seq; |
| 97 | u16 length; |
| 98 | unsigned long expires; |
| 99 | }; |
| 100 | |
| 101 | #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) |
| 102 | |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 103 | static struct workqueue_struct *l2tp_wq; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 104 | |
| 105 | /* per-net private data for this module */ |
| 106 | static unsigned int l2tp_net_id; |
| 107 | struct l2tp_net { |
| 108 | struct list_head l2tp_tunnel_list; |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 109 | spinlock_t l2tp_tunnel_list_lock; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 110 | struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 111 | spinlock_t l2tp_session_hlist_lock; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
stephen hemminger | fc13084 | 2010-10-21 07:50:46 +0000 | [diff] [blame] | 114 | |
David S. Miller | 8d8a51e | 2013-10-08 15:44:26 -0400 | [diff] [blame] | 115 | static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk) |
| 116 | { |
| 117 | return sk->sk_user_data; |
| 118 | } |
| 119 | |
Guillaume Nault | 9aaef50 | 2017-04-12 10:05:29 +0200 | [diff] [blame] | 120 | static inline struct l2tp_net *l2tp_pernet(const struct net *net) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 121 | { |
| 122 | BUG_ON(!net); |
| 123 | |
| 124 | return net_generic(net, l2tp_net_id); |
| 125 | } |
| 126 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 127 | /* Session hash global list for L2TPv3. |
| 128 | * The session_id SHOULD be random according to RFC3931, but several |
| 129 | * L2TP implementations use incrementing session_ids. So we do a real |
| 130 | * hash on the session_id, rather than a simple bitmask. |
| 131 | */ |
| 132 | static inline struct hlist_head * |
| 133 | l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) |
| 134 | { |
| 135 | return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; |
| 136 | |
| 137 | } |
| 138 | |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 139 | /* Lookup the tunnel socket, possibly involving the fs code if the socket is |
| 140 | * owned by userspace. A struct sock returned from this function must be |
| 141 | * released using l2tp_tunnel_sock_put once you're done with it. |
| 142 | */ |
stephen hemminger | b5d2b28 | 2014-01-09 22:22:27 -0800 | [diff] [blame] | 143 | static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel) |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 144 | { |
| 145 | int err = 0; |
| 146 | struct socket *sock = NULL; |
| 147 | struct sock *sk = NULL; |
| 148 | |
| 149 | if (!tunnel) |
| 150 | goto out; |
| 151 | |
| 152 | if (tunnel->fd >= 0) { |
| 153 | /* Socket is owned by userspace, who might be in the process |
| 154 | * of closing it. Look the socket up using the fd to ensure |
| 155 | * consistency. |
| 156 | */ |
| 157 | sock = sockfd_lookup(tunnel->fd, &err); |
| 158 | if (sock) |
| 159 | sk = sock->sk; |
| 160 | } else { |
| 161 | /* Socket is owned by kernelspace */ |
| 162 | sk = tunnel->sock; |
Tom Parkin | 8abbbe8 | 2013-03-19 06:11:17 +0000 | [diff] [blame] | 163 | sock_hold(sk); |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | out: |
| 167 | return sk; |
| 168 | } |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 169 | |
| 170 | /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */ |
stephen hemminger | b5d2b28 | 2014-01-09 22:22:27 -0800 | [diff] [blame] | 171 | static void l2tp_tunnel_sock_put(struct sock *sk) |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 172 | { |
| 173 | struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); |
| 174 | if (tunnel) { |
| 175 | if (tunnel->fd >= 0) { |
| 176 | /* Socket is owned by userspace */ |
| 177 | sockfd_put(sk->sk_socket); |
| 178 | } |
| 179 | sock_put(sk); |
| 180 | } |
Tom Parkin | 8abbbe8 | 2013-03-19 06:11:17 +0000 | [diff] [blame] | 181 | sock_put(sk); |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 182 | } |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 183 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 184 | /* Session hash list. |
| 185 | * The session_id SHOULD be random according to RFC2661, but several |
| 186 | * L2TP implementations (Cisco and Microsoft) use incrementing |
| 187 | * session_ids. So we do a real hash on the session_id, rather than a |
| 188 | * simple bitmask. |
| 189 | */ |
| 190 | static inline struct hlist_head * |
| 191 | l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) |
| 192 | { |
| 193 | return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; |
| 194 | } |
| 195 | |
Guillaume Nault | 54652eb | 2017-08-25 16:51:40 +0200 | [diff] [blame] | 196 | /* Lookup a tunnel. A new reference is held on the returned tunnel. */ |
| 197 | struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id) |
| 198 | { |
| 199 | const struct l2tp_net *pn = l2tp_pernet(net); |
| 200 | struct l2tp_tunnel *tunnel; |
| 201 | |
| 202 | rcu_read_lock_bh(); |
| 203 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { |
| 204 | if (tunnel->tunnel_id == tunnel_id) { |
| 205 | l2tp_tunnel_inc_refcount(tunnel); |
| 206 | rcu_read_unlock_bh(); |
| 207 | |
| 208 | return tunnel; |
| 209 | } |
| 210 | } |
| 211 | rcu_read_unlock_bh(); |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(l2tp_tunnel_get); |
| 216 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 217 | /* Lookup a session. A new reference is held on the returned session. */ |
Guillaume Nault | 9aaef50 | 2017-04-12 10:05:29 +0200 | [diff] [blame] | 218 | struct l2tp_session *l2tp_session_get(const struct net *net, |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 219 | struct l2tp_tunnel *tunnel, |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 220 | u32 session_id) |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 221 | { |
| 222 | struct hlist_head *session_list; |
| 223 | struct l2tp_session *session; |
| 224 | |
| 225 | if (!tunnel) { |
| 226 | struct l2tp_net *pn = l2tp_pernet(net); |
| 227 | |
| 228 | session_list = l2tp_session_id_hash_2(pn, session_id); |
| 229 | |
| 230 | rcu_read_lock_bh(); |
| 231 | hlist_for_each_entry_rcu(session, session_list, global_hlist) { |
| 232 | if (session->session_id == session_id) { |
| 233 | l2tp_session_inc_refcount(session); |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 234 | rcu_read_unlock_bh(); |
| 235 | |
| 236 | return session; |
| 237 | } |
| 238 | } |
| 239 | rcu_read_unlock_bh(); |
| 240 | |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | session_list = l2tp_session_id_hash(tunnel, session_id); |
| 245 | read_lock_bh(&tunnel->hlist_lock); |
| 246 | hlist_for_each_entry(session, session_list, hlist) { |
| 247 | if (session->session_id == session_id) { |
| 248 | l2tp_session_inc_refcount(session); |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 249 | read_unlock_bh(&tunnel->hlist_lock); |
| 250 | |
| 251 | return session; |
| 252 | } |
| 253 | } |
| 254 | read_unlock_bh(&tunnel->hlist_lock); |
| 255 | |
| 256 | return NULL; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(l2tp_session_get); |
| 259 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 260 | struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 261 | { |
| 262 | int hash; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 263 | struct l2tp_session *session; |
| 264 | int count = 0; |
| 265 | |
| 266 | read_lock_bh(&tunnel->hlist_lock); |
| 267 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 268 | hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 269 | if (++count > nth) { |
Guillaume Nault | e08293a | 2017-04-03 12:03:13 +0200 | [diff] [blame] | 270 | l2tp_session_inc_refcount(session); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 271 | read_unlock_bh(&tunnel->hlist_lock); |
| 272 | return session; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | read_unlock_bh(&tunnel->hlist_lock); |
| 278 | |
| 279 | return NULL; |
| 280 | } |
Guillaume Nault | e08293a | 2017-04-03 12:03:13 +0200 | [diff] [blame] | 281 | EXPORT_SYMBOL_GPL(l2tp_session_get_nth); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 282 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 283 | /* Lookup a session by interface name. |
| 284 | * This is very inefficient but is only used by management interfaces. |
| 285 | */ |
Guillaume Nault | 9aaef50 | 2017-04-12 10:05:29 +0200 | [diff] [blame] | 286 | struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 287 | const char *ifname) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 288 | { |
| 289 | struct l2tp_net *pn = l2tp_pernet(net); |
| 290 | int hash; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 291 | struct l2tp_session *session; |
| 292 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 293 | rcu_read_lock_bh(); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 294 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 295 | hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 296 | if (!strcmp(session->ifname, ifname)) { |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 297 | l2tp_session_inc_refcount(session); |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 298 | rcu_read_unlock_bh(); |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 299 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 300 | return session; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 305 | rcu_read_unlock_bh(); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 306 | |
| 307 | return NULL; |
| 308 | } |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 310 | |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 311 | int l2tp_session_register(struct l2tp_session *session, |
| 312 | struct l2tp_tunnel *tunnel) |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 313 | { |
| 314 | struct l2tp_session *session_walk; |
| 315 | struct hlist_head *g_head; |
| 316 | struct hlist_head *head; |
| 317 | struct l2tp_net *pn; |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 318 | int err; |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 319 | |
| 320 | head = l2tp_session_id_hash(tunnel, session->session_id); |
| 321 | |
| 322 | write_lock_bh(&tunnel->hlist_lock); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 323 | if (!tunnel->acpt_newsess) { |
| 324 | err = -ENODEV; |
| 325 | goto err_tlock; |
| 326 | } |
| 327 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 328 | hlist_for_each_entry(session_walk, head, hlist) |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 329 | if (session_walk->session_id == session->session_id) { |
| 330 | err = -EEXIST; |
| 331 | goto err_tlock; |
| 332 | } |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 333 | |
| 334 | if (tunnel->version == L2TP_HDR_VER_3) { |
| 335 | pn = l2tp_pernet(tunnel->l2tp_net); |
| 336 | g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net), |
| 337 | session->session_id); |
| 338 | |
| 339 | spin_lock_bh(&pn->l2tp_session_hlist_lock); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 340 | |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 341 | hlist_for_each_entry(session_walk, g_head, global_hlist) |
| 342 | if (session_walk->session_id == session->session_id) { |
| 343 | err = -EEXIST; |
| 344 | goto err_tlock_pnlock; |
| 345 | } |
| 346 | |
| 347 | l2tp_tunnel_inc_refcount(tunnel); |
| 348 | sock_hold(tunnel->sock); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 349 | hlist_add_head_rcu(&session->global_hlist, g_head); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 350 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 351 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 352 | } else { |
| 353 | l2tp_tunnel_inc_refcount(tunnel); |
| 354 | sock_hold(tunnel->sock); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | hlist_add_head(&session->hlist, head); |
| 358 | write_unlock_bh(&tunnel->hlist_lock); |
| 359 | |
| 360 | return 0; |
| 361 | |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 362 | err_tlock_pnlock: |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 363 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 364 | err_tlock: |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 365 | write_unlock_bh(&tunnel->hlist_lock); |
| 366 | |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 367 | return err; |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 368 | } |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 369 | EXPORT_SYMBOL_GPL(l2tp_session_register); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 370 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 371 | /* Lookup a tunnel by id |
| 372 | */ |
Guillaume Nault | 2f858b9 | 2017-04-12 10:05:30 +0200 | [diff] [blame] | 373 | struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 374 | { |
| 375 | struct l2tp_tunnel *tunnel; |
| 376 | struct l2tp_net *pn = l2tp_pernet(net); |
| 377 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 378 | rcu_read_lock_bh(); |
| 379 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 380 | if (tunnel->tunnel_id == tunnel_id) { |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 381 | rcu_read_unlock_bh(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 382 | return tunnel; |
| 383 | } |
| 384 | } |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 385 | rcu_read_unlock_bh(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | EXPORT_SYMBOL_GPL(l2tp_tunnel_find); |
| 390 | |
Guillaume Nault | 2f858b9 | 2017-04-12 10:05:30 +0200 | [diff] [blame] | 391 | struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 392 | { |
| 393 | struct l2tp_net *pn = l2tp_pernet(net); |
| 394 | struct l2tp_tunnel *tunnel; |
| 395 | int count = 0; |
| 396 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 397 | rcu_read_lock_bh(); |
| 398 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 399 | if (++count > nth) { |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 400 | rcu_read_unlock_bh(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 401 | return tunnel; |
| 402 | } |
| 403 | } |
| 404 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 405 | rcu_read_unlock_bh(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 406 | |
| 407 | return NULL; |
| 408 | } |
| 409 | EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); |
| 410 | |
| 411 | /***************************************************************************** |
| 412 | * Receive data handling |
| 413 | *****************************************************************************/ |
| 414 | |
| 415 | /* Queue a skb in order. We come here only if the skb has an L2TP sequence |
| 416 | * number. |
| 417 | */ |
| 418 | static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) |
| 419 | { |
| 420 | struct sk_buff *skbp; |
| 421 | struct sk_buff *tmp; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 422 | u32 ns = L2TP_SKB_CB(skb)->ns; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 423 | |
| 424 | spin_lock_bh(&session->reorder_q.lock); |
| 425 | skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { |
| 426 | if (L2TP_SKB_CB(skbp)->ns > ns) { |
| 427 | __skb_queue_before(&session->reorder_q, skbp, skb); |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 428 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 429 | "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", |
| 430 | session->name, ns, L2TP_SKB_CB(skbp)->ns, |
| 431 | skb_queue_len(&session->reorder_q)); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 432 | atomic_long_inc(&session->stats.rx_oos_packets); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 433 | goto out; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | __skb_queue_tail(&session->reorder_q, skb); |
| 438 | |
| 439 | out: |
| 440 | spin_unlock_bh(&session->reorder_q.lock); |
| 441 | } |
| 442 | |
| 443 | /* Dequeue a single skb. |
| 444 | */ |
| 445 | static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) |
| 446 | { |
| 447 | struct l2tp_tunnel *tunnel = session->tunnel; |
| 448 | int length = L2TP_SKB_CB(skb)->length; |
| 449 | |
| 450 | /* We're about to requeue the skb, so return resources |
| 451 | * to its current owner (a socket receive buffer). |
| 452 | */ |
| 453 | skb_orphan(skb); |
| 454 | |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 455 | atomic_long_inc(&tunnel->stats.rx_packets); |
| 456 | atomic_long_add(length, &tunnel->stats.rx_bytes); |
| 457 | atomic_long_inc(&session->stats.rx_packets); |
| 458 | atomic_long_add(length, &session->stats.rx_bytes); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 459 | |
| 460 | if (L2TP_SKB_CB(skb)->has_seq) { |
| 461 | /* Bump our Nr */ |
| 462 | session->nr++; |
James Chapman | 8a1631d | 2013-07-02 20:28:59 +0100 | [diff] [blame] | 463 | session->nr &= session->nr_max; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 464 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 465 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", |
| 466 | session->name, session->nr); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* call private receive handler */ |
| 470 | if (session->recv_skb != NULL) |
| 471 | (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); |
| 472 | else |
| 473 | kfree_skb(skb); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Dequeue skbs from the session's reorder_q, subject to packet order. |
| 477 | * Skbs that have been in the queue for too long are simply discarded. |
| 478 | */ |
| 479 | static void l2tp_recv_dequeue(struct l2tp_session *session) |
| 480 | { |
| 481 | struct sk_buff *skb; |
| 482 | struct sk_buff *tmp; |
| 483 | |
| 484 | /* If the pkt at the head of the queue has the nr that we |
| 485 | * expect to send up next, dequeue it and any other |
| 486 | * in-sequence packets behind it. |
| 487 | */ |
Eric Dumazet | e2e210c | 2011-11-02 22:47:44 +0000 | [diff] [blame] | 488 | start: |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 489 | spin_lock_bh(&session->reorder_q.lock); |
| 490 | skb_queue_walk_safe(&session->reorder_q, skb, tmp) { |
| 491 | if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 492 | atomic_long_inc(&session->stats.rx_seq_discards); |
| 493 | atomic_long_inc(&session->stats.rx_errors); |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 494 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 495 | "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", |
| 496 | session->name, L2TP_SKB_CB(skb)->ns, |
| 497 | L2TP_SKB_CB(skb)->length, session->nr, |
| 498 | skb_queue_len(&session->reorder_q)); |
James Chapman | 38d40b3 | 2012-05-09 23:43:08 +0000 | [diff] [blame] | 499 | session->reorder_skip = 1; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 500 | __skb_unlink(skb, &session->reorder_q); |
| 501 | kfree_skb(skb); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 502 | continue; |
| 503 | } |
| 504 | |
| 505 | if (L2TP_SKB_CB(skb)->has_seq) { |
James Chapman | 38d40b3 | 2012-05-09 23:43:08 +0000 | [diff] [blame] | 506 | if (session->reorder_skip) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 507 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 508 | "%s: advancing nr to next pkt: %u -> %u", |
| 509 | session->name, session->nr, |
| 510 | L2TP_SKB_CB(skb)->ns); |
James Chapman | 38d40b3 | 2012-05-09 23:43:08 +0000 | [diff] [blame] | 511 | session->reorder_skip = 0; |
| 512 | session->nr = L2TP_SKB_CB(skb)->ns; |
| 513 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 514 | if (L2TP_SKB_CB(skb)->ns != session->nr) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 515 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 516 | "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", |
| 517 | session->name, L2TP_SKB_CB(skb)->ns, |
| 518 | L2TP_SKB_CB(skb)->length, session->nr, |
| 519 | skb_queue_len(&session->reorder_q)); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 520 | goto out; |
| 521 | } |
| 522 | } |
| 523 | __skb_unlink(skb, &session->reorder_q); |
| 524 | |
| 525 | /* Process the skb. We release the queue lock while we |
| 526 | * do so to let other contexts process the queue. |
| 527 | */ |
| 528 | spin_unlock_bh(&session->reorder_q.lock); |
| 529 | l2tp_recv_dequeue_skb(session, skb); |
Eric Dumazet | e2e210c | 2011-11-02 22:47:44 +0000 | [diff] [blame] | 530 | goto start; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | out: |
| 534 | spin_unlock_bh(&session->reorder_q.lock); |
| 535 | } |
| 536 | |
James Chapman | 8a1631d | 2013-07-02 20:28:59 +0100 | [diff] [blame] | 537 | static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) |
| 538 | { |
| 539 | u32 nws; |
| 540 | |
| 541 | if (nr >= session->nr) |
| 542 | nws = nr - session->nr; |
| 543 | else |
| 544 | nws = (session->nr_max + 1) - (session->nr - nr); |
| 545 | |
| 546 | return nws < session->nr_window_size; |
| 547 | } |
| 548 | |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 549 | /* If packet has sequence numbers, queue it if acceptable. Returns 0 if |
| 550 | * acceptable, else non-zero. |
| 551 | */ |
| 552 | static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) |
| 553 | { |
James Chapman | 8a1631d | 2013-07-02 20:28:59 +0100 | [diff] [blame] | 554 | if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { |
| 555 | /* Packet sequence number is outside allowed window. |
| 556 | * Discard it. |
| 557 | */ |
| 558 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 559 | "%s: pkt %u len %d discarded, outside window, nr=%u\n", |
| 560 | session->name, L2TP_SKB_CB(skb)->ns, |
| 561 | L2TP_SKB_CB(skb)->length, session->nr); |
| 562 | goto discard; |
| 563 | } |
| 564 | |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 565 | if (session->reorder_timeout != 0) { |
| 566 | /* Packet reordering enabled. Add skb to session's |
| 567 | * reorder queue, in order of ns. |
| 568 | */ |
| 569 | l2tp_recv_queue_skb(session, skb); |
James Chapman | a0dbd82 | 2013-07-02 20:29:00 +0100 | [diff] [blame] | 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | /* Packet reordering disabled. Discard out-of-sequence packets, while |
| 574 | * tracking the number if in-sequence packets after the first OOS packet |
| 575 | * is seen. After nr_oos_count_max in-sequence packets, reset the |
| 576 | * sequence number to re-enable packet reception. |
| 577 | */ |
| 578 | if (L2TP_SKB_CB(skb)->ns == session->nr) { |
| 579 | skb_queue_tail(&session->reorder_q, skb); |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 580 | } else { |
James Chapman | a0dbd82 | 2013-07-02 20:29:00 +0100 | [diff] [blame] | 581 | u32 nr_oos = L2TP_SKB_CB(skb)->ns; |
| 582 | u32 nr_next = (session->nr_oos + 1) & session->nr_max; |
| 583 | |
| 584 | if (nr_oos == nr_next) |
| 585 | session->nr_oos_count++; |
| 586 | else |
| 587 | session->nr_oos_count = 0; |
| 588 | |
| 589 | session->nr_oos = nr_oos; |
| 590 | if (session->nr_oos_count > session->nr_oos_count_max) { |
| 591 | session->reorder_skip = 1; |
| 592 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 593 | "%s: %d oos packets received. Resetting sequence numbers\n", |
| 594 | session->name, session->nr_oos_count); |
| 595 | } |
| 596 | if (!session->reorder_skip) { |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 597 | atomic_long_inc(&session->stats.rx_seq_discards); |
| 598 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 599 | "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", |
| 600 | session->name, L2TP_SKB_CB(skb)->ns, |
| 601 | L2TP_SKB_CB(skb)->length, session->nr, |
| 602 | skb_queue_len(&session->reorder_q)); |
| 603 | goto discard; |
| 604 | } |
| 605 | skb_queue_tail(&session->reorder_q, skb); |
| 606 | } |
| 607 | |
James Chapman | a0dbd82 | 2013-07-02 20:29:00 +0100 | [diff] [blame] | 608 | out: |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 609 | return 0; |
| 610 | |
| 611 | discard: |
| 612 | return 1; |
| 613 | } |
| 614 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 615 | /* Do receive processing of L2TP data frames. We handle both L2TPv2 |
| 616 | * and L2TPv3 data frames here. |
| 617 | * |
| 618 | * L2TPv2 Data Message Header |
| 619 | * |
| 620 | * 0 1 2 3 |
| 621 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 622 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 623 | * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | |
| 624 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 625 | * | Tunnel ID | Session ID | |
| 626 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 627 | * | Ns (opt) | Nr (opt) | |
| 628 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 629 | * | Offset Size (opt) | Offset pad... (opt) |
| 630 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 631 | * |
| 632 | * Data frames are marked by T=0. All other fields are the same as |
| 633 | * those in L2TP control frames. |
| 634 | * |
| 635 | * L2TPv3 Data Message Header |
| 636 | * |
| 637 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 638 | * | L2TP Session Header | |
| 639 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 640 | * | L2-Specific Sublayer | |
| 641 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 642 | * | Tunnel Payload ... |
| 643 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 644 | * |
| 645 | * L2TPv3 Session Header Over IP |
| 646 | * |
| 647 | * 0 1 2 3 |
| 648 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 649 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 650 | * | Session ID | |
| 651 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 652 | * | Cookie (optional, maximum 64 bits)... |
| 653 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 654 | * | |
| 655 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 656 | * |
| 657 | * L2TPv3 L2-Specific Sublayer Format |
| 658 | * |
| 659 | * 0 1 2 3 |
| 660 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 661 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 662 | * |x|S|x|x|x|x|x|x| Sequence Number | |
| 663 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 664 | * |
Guillaume Nault | 23fe846 | 2018-01-05 19:47:14 +0100 | [diff] [blame] | 665 | * Cookie value and sublayer format are negotiated with the peer when |
| 666 | * the session is set up. Unlike L2TPv2, we do not need to parse the |
| 667 | * packet header to determine if optional fields are present. |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 668 | * |
| 669 | * Caller must already have parsed the frame and determined that it is |
| 670 | * a data (not control) frame before coming here. Fields up to the |
| 671 | * session-id have already been parsed and ptr points to the data |
| 672 | * after the session-id. |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 673 | */ |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 674 | void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, |
| 675 | unsigned char *ptr, unsigned char *optr, u16 hdrflags, |
| 676 | int length, int (*payload_hook)(struct sk_buff *skb)) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 677 | { |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 678 | struct l2tp_tunnel *tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 679 | int offset; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 680 | u32 ns, nr; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 681 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 682 | /* Parse and check optional cookie */ |
| 683 | if (session->peer_cookie_len > 0) { |
| 684 | if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 685 | l2tp_info(tunnel, L2TP_MSG_DATA, |
| 686 | "%s: cookie mismatch (%u/%u). Discarding.\n", |
| 687 | tunnel->name, tunnel->tunnel_id, |
| 688 | session->session_id); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 689 | atomic_long_inc(&session->stats.rx_cookie_discards); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 690 | goto discard; |
| 691 | } |
| 692 | ptr += session->peer_cookie_len; |
| 693 | } |
| 694 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 695 | /* Handle the optional sequence numbers. Sequence numbers are |
| 696 | * in different places for L2TPv2 and L2TPv3. |
| 697 | * |
| 698 | * If we are the LAC, enable/disable sequence numbers under |
| 699 | * the control of the LNS. If no sequence numbers present but |
| 700 | * we were expecting them, discard frame. |
| 701 | */ |
| 702 | ns = nr = 0; |
| 703 | L2TP_SKB_CB(skb)->has_seq = 0; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 704 | if (tunnel->version == L2TP_HDR_VER_2) { |
| 705 | if (hdrflags & L2TP_HDRFLAG_S) { |
| 706 | ns = ntohs(*(__be16 *) ptr); |
| 707 | ptr += 2; |
| 708 | nr = ntohs(*(__be16 *) ptr); |
| 709 | ptr += 2; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 710 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 711 | /* Store L2TP info in the skb */ |
| 712 | L2TP_SKB_CB(skb)->ns = ns; |
| 713 | L2TP_SKB_CB(skb)->has_seq = 1; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 714 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 715 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 716 | "%s: recv data ns=%u, nr=%u, session nr=%u\n", |
| 717 | session->name, ns, nr, session->nr); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 718 | } |
| 719 | } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { |
| 720 | u32 l2h = ntohl(*(__be32 *) ptr); |
| 721 | |
| 722 | if (l2h & 0x40000000) { |
| 723 | ns = l2h & 0x00ffffff; |
| 724 | |
| 725 | /* Store L2TP info in the skb */ |
| 726 | L2TP_SKB_CB(skb)->ns = ns; |
| 727 | L2TP_SKB_CB(skb)->has_seq = 1; |
| 728 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 729 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 730 | "%s: recv data ns=%u, session nr=%u\n", |
| 731 | session->name, ns, session->nr); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 732 | } |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 733 | ptr += 4; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | if (L2TP_SKB_CB(skb)->has_seq) { |
| 737 | /* Received a packet with sequence numbers. If we're the LNS, |
| 738 | * check if we sre sending sequence numbers and if not, |
| 739 | * configure it so. |
| 740 | */ |
| 741 | if ((!session->lns_mode) && (!session->send_seq)) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 742 | l2tp_info(session, L2TP_MSG_SEQ, |
| 743 | "%s: requested to enable seq numbers by LNS\n", |
| 744 | session->name); |
Asbjørn Sloth Tønnesen | 3f9b977 | 2016-11-07 20:39:28 +0000 | [diff] [blame] | 745 | session->send_seq = 1; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 746 | l2tp_session_set_header_len(session, tunnel->version); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 747 | } |
| 748 | } else { |
| 749 | /* No sequence numbers. |
| 750 | * If user has configured mandatory sequence numbers, discard. |
| 751 | */ |
| 752 | if (session->recv_seq) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 753 | l2tp_warn(session, L2TP_MSG_SEQ, |
| 754 | "%s: recv data has no seq numbers when required. Discarding.\n", |
| 755 | session->name); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 756 | atomic_long_inc(&session->stats.rx_seq_discards); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 757 | goto discard; |
| 758 | } |
| 759 | |
| 760 | /* If we're the LAC and we're sending sequence numbers, the |
| 761 | * LNS has requested that we no longer send sequence numbers. |
| 762 | * If we're the LNS and we're sending sequence numbers, the |
| 763 | * LAC is broken. Discard the frame. |
| 764 | */ |
| 765 | if ((!session->lns_mode) && (session->send_seq)) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 766 | l2tp_info(session, L2TP_MSG_SEQ, |
| 767 | "%s: requested to disable seq numbers by LNS\n", |
| 768 | session->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 769 | session->send_seq = 0; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 770 | l2tp_session_set_header_len(session, tunnel->version); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 771 | } else if (session->send_seq) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 772 | l2tp_warn(session, L2TP_MSG_SEQ, |
| 773 | "%s: recv data has no seq numbers when required. Discarding.\n", |
| 774 | session->name); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 775 | atomic_long_inc(&session->stats.rx_seq_discards); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 776 | goto discard; |
| 777 | } |
| 778 | } |
| 779 | |
James Chapman | 900631e | 2018-01-03 22:48:06 +0000 | [diff] [blame] | 780 | /* Session data offset is defined only for L2TPv2 and is |
| 781 | * indicated by an optional 16-bit value in the header. |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 782 | */ |
| 783 | if (tunnel->version == L2TP_HDR_VER_2) { |
| 784 | /* If offset bit set, skip it. */ |
| 785 | if (hdrflags & L2TP_HDRFLAG_O) { |
| 786 | offset = ntohs(*(__be16 *)ptr); |
| 787 | ptr += 2 + offset; |
| 788 | } |
James Chapman | 900631e | 2018-01-03 22:48:06 +0000 | [diff] [blame] | 789 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 790 | |
| 791 | offset = ptr - optr; |
| 792 | if (!pskb_may_pull(skb, offset)) |
| 793 | goto discard; |
| 794 | |
| 795 | __skb_pull(skb, offset); |
| 796 | |
| 797 | /* If caller wants to process the payload before we queue the |
| 798 | * packet, do so now. |
| 799 | */ |
| 800 | if (payload_hook) |
| 801 | if ((*payload_hook)(skb)) |
| 802 | goto discard; |
| 803 | |
| 804 | /* Prepare skb for adding to the session's reorder_q. Hold |
| 805 | * packets for max reorder_timeout or 1 second if not |
| 806 | * reordering. |
| 807 | */ |
| 808 | L2TP_SKB_CB(skb)->length = length; |
| 809 | L2TP_SKB_CB(skb)->expires = jiffies + |
| 810 | (session->reorder_timeout ? session->reorder_timeout : HZ); |
| 811 | |
| 812 | /* Add packet to the session's receive queue. Reordering is done here, if |
| 813 | * enabled. Saved L2TP protocol info is stored in skb->sb[]. |
| 814 | */ |
| 815 | if (L2TP_SKB_CB(skb)->has_seq) { |
James Chapman | b6dc01a | 2013-07-02 20:28:58 +0100 | [diff] [blame] | 816 | if (l2tp_recv_data_seq(session, skb)) |
| 817 | goto discard; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 818 | } else { |
| 819 | /* No sequence numbers. Add the skb to the tail of the |
| 820 | * reorder queue. This ensures that it will be |
| 821 | * delivered after all previous sequenced skbs. |
| 822 | */ |
| 823 | skb_queue_tail(&session->reorder_q, skb); |
| 824 | } |
| 825 | |
| 826 | /* Try to dequeue as many skbs from reorder_q as we can. */ |
| 827 | l2tp_recv_dequeue(session); |
| 828 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 829 | return; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 830 | |
| 831 | discard: |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 832 | atomic_long_inc(&session->stats.rx_errors); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 833 | kfree_skb(skb); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 834 | } |
| 835 | EXPORT_SYMBOL(l2tp_recv_common); |
| 836 | |
Tom Parkin | 48f72f9 | 2013-03-19 06:11:19 +0000 | [diff] [blame] | 837 | /* Drop skbs from the session's reorder_q |
| 838 | */ |
| 839 | int l2tp_session_queue_purge(struct l2tp_session *session) |
| 840 | { |
| 841 | struct sk_buff *skb = NULL; |
| 842 | BUG_ON(!session); |
| 843 | BUG_ON(session->magic != L2TP_SESSION_MAGIC); |
| 844 | while ((skb = skb_dequeue(&session->reorder_q))) { |
| 845 | atomic_long_inc(&session->stats.rx_errors); |
| 846 | kfree_skb(skb); |
Tom Parkin | 48f72f9 | 2013-03-19 06:11:19 +0000 | [diff] [blame] | 847 | } |
| 848 | return 0; |
| 849 | } |
| 850 | EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); |
| 851 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 852 | /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame |
| 853 | * here. The skb is not on a list when we get here. |
| 854 | * Returns 0 if the packet was a data packet and was successfully passed on. |
| 855 | * Returns 1 if the packet was not a good data packet and could not be |
| 856 | * forwarded. All such packets are passed up to userspace to deal with. |
| 857 | */ |
stephen hemminger | fc13084 | 2010-10-21 07:50:46 +0000 | [diff] [blame] | 858 | static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, |
| 859 | int (*payload_hook)(struct sk_buff *skb)) |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 860 | { |
| 861 | struct l2tp_session *session = NULL; |
| 862 | unsigned char *ptr, *optr; |
| 863 | u16 hdrflags; |
| 864 | u32 tunnel_id, session_id; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 865 | u16 version; |
| 866 | int length; |
| 867 | |
Tom Herbert | 58d6085 | 2014-05-07 16:52:48 -0700 | [diff] [blame] | 868 | /* UDP has verifed checksum */ |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 869 | |
| 870 | /* UDP always verifies the packet length. */ |
| 871 | __skb_pull(skb, sizeof(struct udphdr)); |
| 872 | |
| 873 | /* Short packet? */ |
| 874 | if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 875 | l2tp_info(tunnel, L2TP_MSG_DATA, |
| 876 | "%s: recv short packet (len=%d)\n", |
| 877 | tunnel->name, skb->len); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 878 | goto error; |
| 879 | } |
| 880 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 881 | /* Trace packet contents, if enabled */ |
| 882 | if (tunnel->debug & L2TP_MSG_DATA) { |
| 883 | length = min(32u, skb->len); |
| 884 | if (!pskb_may_pull(skb, length)) |
| 885 | goto error; |
| 886 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 887 | pr_debug("%s: recv\n", tunnel->name); |
| 888 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Eric Dumazet | e50e705 | 2011-11-08 13:59:44 -0500 | [diff] [blame] | 891 | /* Point to L2TP header */ |
| 892 | optr = ptr = skb->data; |
| 893 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 894 | /* Get L2TP header flags */ |
| 895 | hdrflags = ntohs(*(__be16 *) ptr); |
| 896 | |
| 897 | /* Check protocol version */ |
| 898 | version = hdrflags & L2TP_HDR_VER_MASK; |
| 899 | if (version != tunnel->version) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 900 | l2tp_info(tunnel, L2TP_MSG_DATA, |
| 901 | "%s: recv protocol version mismatch: got %d expected %d\n", |
| 902 | tunnel->name, version, tunnel->version); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 903 | goto error; |
| 904 | } |
| 905 | |
| 906 | /* Get length of L2TP packet */ |
| 907 | length = skb->len; |
| 908 | |
| 909 | /* If type is control packet, it is handled by userspace. */ |
| 910 | if (hdrflags & L2TP_HDRFLAG_T) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 911 | l2tp_dbg(tunnel, L2TP_MSG_DATA, |
| 912 | "%s: recv control packet, len=%d\n", |
| 913 | tunnel->name, length); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 914 | goto error; |
| 915 | } |
| 916 | |
| 917 | /* Skip flags */ |
| 918 | ptr += 2; |
| 919 | |
| 920 | if (tunnel->version == L2TP_HDR_VER_2) { |
| 921 | /* If length is present, skip it */ |
| 922 | if (hdrflags & L2TP_HDRFLAG_L) |
| 923 | ptr += 2; |
| 924 | |
| 925 | /* Extract tunnel and session ID */ |
| 926 | tunnel_id = ntohs(*(__be16 *) ptr); |
| 927 | ptr += 2; |
| 928 | session_id = ntohs(*(__be16 *) ptr); |
| 929 | ptr += 2; |
| 930 | } else { |
| 931 | ptr += 2; /* skip reserved bits */ |
| 932 | tunnel_id = tunnel->tunnel_id; |
| 933 | session_id = ntohl(*(__be32 *) ptr); |
| 934 | ptr += 4; |
| 935 | } |
| 936 | |
| 937 | /* Find the session context */ |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 938 | session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 939 | if (!session || !session->recv_skb) { |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 940 | if (session) |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 941 | l2tp_session_dec_refcount(session); |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 942 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 943 | /* Not found? Pass to userspace to deal with */ |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 944 | l2tp_info(tunnel, L2TP_MSG_DATA, |
| 945 | "%s: no session found (%u/%u). Passing up.\n", |
| 946 | tunnel->name, tunnel_id, session_id); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 947 | goto error; |
| 948 | } |
| 949 | |
| 950 | l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); |
Guillaume Nault | 61b9a04 | 2017-03-31 13:02:25 +0200 | [diff] [blame] | 951 | l2tp_session_dec_refcount(session); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 952 | |
| 953 | return 0; |
| 954 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 955 | error: |
| 956 | /* Put UDP header back */ |
| 957 | __skb_push(skb, sizeof(struct udphdr)); |
| 958 | |
| 959 | return 1; |
| 960 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 961 | |
| 962 | /* UDP encapsulation receive handler. See net/ipv4/udp.c. |
| 963 | * Return codes: |
| 964 | * 0 : success. |
| 965 | * <0: error |
| 966 | * >0: skb should be passed up to userspace as UDP. |
| 967 | */ |
| 968 | int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 969 | { |
| 970 | struct l2tp_tunnel *tunnel; |
| 971 | |
| 972 | tunnel = l2tp_sock_to_tunnel(sk); |
| 973 | if (tunnel == NULL) |
| 974 | goto pass_up; |
| 975 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 976 | l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", |
| 977 | tunnel->name, skb->len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 978 | |
| 979 | if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) |
| 980 | goto pass_up_put; |
| 981 | |
| 982 | sock_put(sk); |
| 983 | return 0; |
| 984 | |
| 985 | pass_up_put: |
| 986 | sock_put(sk); |
| 987 | pass_up: |
| 988 | return 1; |
| 989 | } |
| 990 | EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); |
| 991 | |
| 992 | /************************************************************************ |
| 993 | * Transmit handling |
| 994 | ***********************************************************************/ |
| 995 | |
| 996 | /* Build an L2TP header for the session into the buffer provided. |
| 997 | */ |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 998 | static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 999 | { |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1000 | struct l2tp_tunnel *tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1001 | __be16 *bufp = buf; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1002 | __be16 *optr = buf; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1003 | u16 flags = L2TP_HDR_VER_2; |
| 1004 | u32 tunnel_id = tunnel->peer_tunnel_id; |
| 1005 | u32 session_id = session->peer_session_id; |
| 1006 | |
| 1007 | if (session->send_seq) |
| 1008 | flags |= L2TP_HDRFLAG_S; |
| 1009 | |
| 1010 | /* Setup L2TP header. */ |
| 1011 | *bufp++ = htons(flags); |
| 1012 | *bufp++ = htons(tunnel_id); |
| 1013 | *bufp++ = htons(session_id); |
| 1014 | if (session->send_seq) { |
| 1015 | *bufp++ = htons(session->ns); |
| 1016 | *bufp++ = 0; |
| 1017 | session->ns++; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1018 | session->ns &= 0xffff; |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1019 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", |
| 1020 | session->name, session->ns); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1021 | } |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1022 | |
| 1023 | return bufp - optr; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1026 | static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1027 | { |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1028 | struct l2tp_tunnel *tunnel = session->tunnel; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1029 | char *bufp = buf; |
| 1030 | char *optr = bufp; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1031 | |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1032 | /* Setup L2TP header. The header differs slightly for UDP and |
| 1033 | * IP encapsulations. For UDP, there is 4 bytes of flags. |
| 1034 | */ |
| 1035 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { |
| 1036 | u16 flags = L2TP_HDR_VER_3; |
| 1037 | *((__be16 *) bufp) = htons(flags); |
| 1038 | bufp += 2; |
| 1039 | *((__be16 *) bufp) = 0; |
| 1040 | bufp += 2; |
| 1041 | } |
| 1042 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1043 | *((__be32 *) bufp) = htonl(session->peer_session_id); |
| 1044 | bufp += 4; |
| 1045 | if (session->cookie_len) { |
| 1046 | memcpy(bufp, &session->cookie[0], session->cookie_len); |
| 1047 | bufp += session->cookie_len; |
| 1048 | } |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 1049 | if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { |
| 1050 | u32 l2h = 0; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1051 | |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 1052 | if (session->send_seq) { |
| 1053 | l2h = 0x40000000 | session->ns; |
| 1054 | session->ns++; |
| 1055 | session->ns &= 0xffffff; |
| 1056 | l2tp_dbg(session, L2TP_MSG_SEQ, |
| 1057 | "%s: updated ns to %u\n", |
| 1058 | session->name, session->ns); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1059 | } |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 1060 | |
| 1061 | *((__be32 *)bufp) = htonl(l2h); |
| 1062 | bufp += 4; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1063 | } |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1064 | |
| 1065 | return bufp - optr; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1066 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1067 | |
stephen hemminger | fc13084 | 2010-10-21 07:50:46 +0000 | [diff] [blame] | 1068 | static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, |
David S. Miller | d9d8da8 | 2011-05-06 22:23:20 -0700 | [diff] [blame] | 1069 | struct flowi *fl, size_t data_len) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1070 | { |
| 1071 | struct l2tp_tunnel *tunnel = session->tunnel; |
| 1072 | unsigned int len = skb->len; |
| 1073 | int error; |
| 1074 | |
| 1075 | /* Debug */ |
| 1076 | if (session->send_seq) |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1077 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1078 | session->name, data_len, session->ns - 1); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1079 | else |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1080 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1081 | session->name, data_len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (session->debug & L2TP_MSG_DATA) { |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1084 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
| 1085 | unsigned char *datap = skb->data + uhlen; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1086 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1087 | pr_debug("%s: xmit\n", session->name); |
| 1088 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1089 | datap, min_t(size_t, 32, len - uhlen)); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* Queue the packet to IP for output */ |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 1093 | skb->ignore_df = 1; |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 1094 | #if IS_ENABLED(CONFIG_IPV6) |
Eric Dumazet | 746e349 | 2014-03-07 14:57:43 -0800 | [diff] [blame] | 1095 | if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped) |
Eric Dumazet | b0270e9 | 2014-04-15 12:58:34 -0400 | [diff] [blame] | 1096 | error = inet6_csk_xmit(tunnel->sock, skb, NULL); |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 1097 | else |
| 1098 | #endif |
Eric Dumazet | b0270e9 | 2014-04-15 12:58:34 -0400 | [diff] [blame] | 1099 | error = ip_queue_xmit(tunnel->sock, skb, fl); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1100 | |
| 1101 | /* Update stats */ |
| 1102 | if (error >= 0) { |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1103 | atomic_long_inc(&tunnel->stats.tx_packets); |
| 1104 | atomic_long_add(len, &tunnel->stats.tx_bytes); |
| 1105 | atomic_long_inc(&session->stats.tx_packets); |
| 1106 | atomic_long_add(len, &session->stats.tx_bytes); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1107 | } else { |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1108 | atomic_long_inc(&tunnel->stats.tx_errors); |
| 1109 | atomic_long_inc(&session->stats.tx_errors); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1114 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1115 | /* If caller requires the skb to have a ppp header, the header must be |
| 1116 | * inserted in the skb data before calling this function. |
| 1117 | */ |
| 1118 | int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) |
| 1119 | { |
| 1120 | int data_len = skb->len; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1121 | struct l2tp_tunnel *tunnel = session->tunnel; |
| 1122 | struct sock *sk = tunnel->sock; |
David S. Miller | d9d8da8 | 2011-05-06 22:23:20 -0700 | [diff] [blame] | 1123 | struct flowi *fl; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1124 | struct udphdr *uh; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1125 | struct inet_sock *inet; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1126 | int headroom; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1127 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
| 1128 | int udp_len; |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 1129 | int ret = NET_XMIT_SUCCESS; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Check that there's enough headroom in the skb to insert IP, |
| 1132 | * UDP and L2TP headers. If not enough, expand it to |
| 1133 | * make room. Adjust truesize. |
| 1134 | */ |
| 1135 | headroom = NET_SKB_PAD + sizeof(struct iphdr) + |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1136 | uhlen + hdr_len; |
Eric Dumazet | 835acf5 | 2011-10-07 05:35:46 +0000 | [diff] [blame] | 1137 | if (skb_cow_head(skb, headroom)) { |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 1138 | kfree_skb(skb); |
| 1139 | return NET_XMIT_DROP; |
Eric Dumazet | 835acf5 | 2011-10-07 05:35:46 +0000 | [diff] [blame] | 1140 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1141 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1142 | /* Setup L2TP header */ |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1143 | session->build_header(session, __skb_push(skb, hdr_len)); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1144 | |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1145 | /* Reset skb netfilter state */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1146 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 1147 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | |
| 1148 | IPSKB_REROUTED); |
| 1149 | nf_reset(skb); |
| 1150 | |
David S. Miller | 6af88da | 2011-05-08 13:45:20 -0700 | [diff] [blame] | 1151 | bh_lock_sock(sk); |
| 1152 | if (sock_owned_by_user(sk)) { |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 1153 | kfree_skb(skb); |
| 1154 | ret = NET_XMIT_DROP; |
David S. Miller | 6af88da | 2011-05-08 13:45:20 -0700 | [diff] [blame] | 1155 | goto out_unlock; |
| 1156 | } |
| 1157 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1158 | /* Get routing info from the tunnel socket */ |
| 1159 | skb_dst_drop(skb); |
Florian Westphal | 71b1391 | 2011-11-25 06:47:16 +0000 | [diff] [blame] | 1160 | skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1161 | |
David S. Miller | d9d8da8 | 2011-05-06 22:23:20 -0700 | [diff] [blame] | 1162 | inet = inet_sk(sk); |
| 1163 | fl = &inet->cork.fl; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1164 | switch (tunnel->encap) { |
| 1165 | case L2TP_ENCAPTYPE_UDP: |
| 1166 | /* Setup UDP header */ |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1167 | __skb_push(skb, sizeof(*uh)); |
| 1168 | skb_reset_transport_header(skb); |
| 1169 | uh = udp_hdr(skb); |
| 1170 | uh->source = inet->inet_sport; |
| 1171 | uh->dest = inet->inet_dport; |
| 1172 | udp_len = uhlen + hdr_len + data_len; |
| 1173 | uh->len = htons(udp_len); |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* Calculate UDP checksum if configured to do so */ |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 1176 | #if IS_ENABLED(CONFIG_IPV6) |
François Cachereul | e18503f | 2013-10-02 10:16:02 +0200 | [diff] [blame] | 1177 | if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) |
Tom Herbert | 77157e1 | 2014-06-04 17:19:56 -0700 | [diff] [blame] | 1178 | udp6_set_csum(udp_get_no_check6_tx(sk), |
| 1179 | skb, &inet6_sk(sk)->saddr, |
| 1180 | &sk->sk_v6_daddr, udp_len); |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 1181 | else |
| 1182 | #endif |
Tom Herbert | 77157e1 | 2014-06-04 17:19:56 -0700 | [diff] [blame] | 1183 | udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, |
| 1184 | inet->inet_daddr, udp_len); |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | |
| 1187 | case L2TP_ENCAPTYPE_IP: |
| 1188 | break; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
David S. Miller | d9d8da8 | 2011-05-06 22:23:20 -0700 | [diff] [blame] | 1191 | l2tp_xmit_core(session, skb, fl, data_len); |
David S. Miller | 6af88da | 2011-05-08 13:45:20 -0700 | [diff] [blame] | 1192 | out_unlock: |
| 1193 | bh_unlock_sock(sk); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1194 | |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 1195 | return ret; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1196 | } |
| 1197 | EXPORT_SYMBOL_GPL(l2tp_xmit_skb); |
| 1198 | |
| 1199 | /***************************************************************************** |
| 1200 | * Tinnel and session create/destroy. |
| 1201 | *****************************************************************************/ |
| 1202 | |
| 1203 | /* Tunnel socket destruct hook. |
| 1204 | * The tunnel context is deleted only when all session sockets have been |
| 1205 | * closed. |
| 1206 | */ |
stephen hemminger | fc13084 | 2010-10-21 07:50:46 +0000 | [diff] [blame] | 1207 | static void l2tp_tunnel_destruct(struct sock *sk) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1208 | { |
David S. Miller | 8d8a51e | 2013-10-08 15:44:26 -0400 | [diff] [blame] | 1209 | struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1210 | struct l2tp_net *pn; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1211 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1212 | if (tunnel == NULL) |
| 1213 | goto end; |
| 1214 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1215 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1216 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1217 | |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1218 | /* Disable udp encapsulation */ |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1219 | switch (tunnel->encap) { |
| 1220 | case L2TP_ENCAPTYPE_UDP: |
| 1221 | /* No longer an encapsulation socket. See net/ipv4/udp.c */ |
| 1222 | (udp_sk(sk))->encap_type = 0; |
| 1223 | (udp_sk(sk))->encap_rcv = NULL; |
Tom Parkin | 9980d00 | 2013-03-19 06:11:13 +0000 | [diff] [blame] | 1224 | (udp_sk(sk))->encap_destroy = NULL; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1225 | break; |
| 1226 | case L2TP_ENCAPTYPE_IP: |
| 1227 | break; |
| 1228 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* Remove hooks into tunnel socket */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1231 | sk->sk_destruct = tunnel->old_sk_destruct; |
| 1232 | sk->sk_user_data = NULL; |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1233 | |
| 1234 | /* Remove the tunnel struct from the tunnel list */ |
| 1235 | pn = l2tp_pernet(tunnel->l2tp_net); |
| 1236 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); |
| 1237 | list_del_rcu(&tunnel->list); |
| 1238 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1239 | |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 1240 | tunnel->sock = NULL; |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1241 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* Call the original destructor */ |
| 1244 | if (sk->sk_destruct) |
| 1245 | (*sk->sk_destruct)(sk); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1246 | end: |
| 1247 | return; |
| 1248 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* When the tunnel is closed, all the attached sessions need to go too. |
| 1251 | */ |
Tom Parkin | e34f4c7 | 2013-03-19 06:11:14 +0000 | [diff] [blame] | 1252 | void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1253 | { |
| 1254 | int hash; |
| 1255 | struct hlist_node *walk; |
| 1256 | struct hlist_node *tmp; |
| 1257 | struct l2tp_session *session; |
| 1258 | |
| 1259 | BUG_ON(tunnel == NULL); |
| 1260 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1261 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", |
| 1262 | tunnel->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1263 | |
| 1264 | write_lock_bh(&tunnel->hlist_lock); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 1265 | tunnel->acpt_newsess = false; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1266 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { |
| 1267 | again: |
| 1268 | hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { |
| 1269 | session = hlist_entry(walk, struct l2tp_session, hlist); |
| 1270 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1271 | l2tp_info(session, L2TP_MSG_CONTROL, |
| 1272 | "%s: closing session\n", session->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1273 | |
| 1274 | hlist_del_init(&session->hlist); |
| 1275 | |
Guillaume Nault | b228a94 | 2017-09-22 15:39:24 +0200 | [diff] [blame] | 1276 | if (test_and_set_bit(0, &session->dead)) |
| 1277 | goto again; |
| 1278 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1279 | write_unlock_bh(&tunnel->hlist_lock); |
| 1280 | |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 1281 | __l2tp_session_unhash(session); |
Tom Parkin | 4c6e2fd | 2013-03-19 06:11:20 +0000 | [diff] [blame] | 1282 | l2tp_session_queue_purge(session); |
| 1283 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1284 | if (session->session_close != NULL) |
| 1285 | (*session->session_close)(session); |
| 1286 | |
Tom Parkin | 9980d00 | 2013-03-19 06:11:13 +0000 | [diff] [blame] | 1287 | l2tp_session_dec_refcount(session); |
| 1288 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1289 | write_lock_bh(&tunnel->hlist_lock); |
| 1290 | |
| 1291 | /* Now restart from the beginning of this hash |
| 1292 | * chain. We always remove a session from the |
| 1293 | * list so we are guaranteed to make forward |
| 1294 | * progress. |
| 1295 | */ |
| 1296 | goto again; |
| 1297 | } |
| 1298 | } |
| 1299 | write_unlock_bh(&tunnel->hlist_lock); |
| 1300 | } |
Tom Parkin | e34f4c7 | 2013-03-19 06:11:14 +0000 | [diff] [blame] | 1301 | EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1302 | |
Tom Parkin | 9980d00 | 2013-03-19 06:11:13 +0000 | [diff] [blame] | 1303 | /* Tunnel socket destroy hook for UDP encapsulation */ |
| 1304 | static void l2tp_udp_encap_destroy(struct sock *sk) |
| 1305 | { |
| 1306 | struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); |
| 1307 | if (tunnel) { |
| 1308 | l2tp_tunnel_closeall(tunnel); |
| 1309 | sock_put(sk); |
| 1310 | } |
| 1311 | } |
| 1312 | |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1313 | /* Workqueue tunnel deletion function */ |
| 1314 | static void l2tp_tunnel_del_work(struct work_struct *work) |
| 1315 | { |
| 1316 | struct l2tp_tunnel *tunnel = NULL; |
| 1317 | struct socket *sock = NULL; |
| 1318 | struct sock *sk = NULL; |
| 1319 | |
| 1320 | tunnel = container_of(work, struct l2tp_tunnel, del_work); |
Ridge Kennedy | 12d656a | 2017-02-22 14:59:49 +1300 | [diff] [blame] | 1321 | |
| 1322 | l2tp_tunnel_closeall(tunnel); |
| 1323 | |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1324 | sk = l2tp_tunnel_sock_lookup(tunnel); |
| 1325 | if (!sk) |
Alexander Couzens | 06a15f5 | 2015-09-28 11:32:42 +0200 | [diff] [blame] | 1326 | goto out; |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1327 | |
| 1328 | sock = sk->sk_socket; |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1329 | |
Tom Parkin | 02d13ed | 2013-03-19 06:11:18 +0000 | [diff] [blame] | 1330 | /* If the tunnel socket was created by userspace, then go through the |
| 1331 | * inet layer to shut the socket down, and let userspace close it. |
| 1332 | * Otherwise, if we created the socket directly within the kernel, use |
| 1333 | * the sk API to release it here. |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1334 | * In either case the tunnel resources are freed in the socket |
| 1335 | * destructor when the tunnel socket goes away. |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1336 | */ |
Tom Parkin | 02d13ed | 2013-03-19 06:11:18 +0000 | [diff] [blame] | 1337 | if (tunnel->fd >= 0) { |
| 1338 | if (sock) |
| 1339 | inet_shutdown(sock, 2); |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1340 | } else { |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 1341 | if (sock) { |
Tom Parkin | 02d13ed | 2013-03-19 06:11:18 +0000 | [diff] [blame] | 1342 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 1343 | sock_release(sock); |
| 1344 | } |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1345 | } |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1346 | |
| 1347 | l2tp_tunnel_sock_put(sk); |
Alexander Couzens | 06a15f5 | 2015-09-28 11:32:42 +0200 | [diff] [blame] | 1348 | out: |
| 1349 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1350 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1351 | |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1352 | /* Create a socket for the tunnel, if one isn't set up by |
| 1353 | * userspace. This is used for static tunnels where there is no |
| 1354 | * managing L2TP daemon. |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1355 | * |
| 1356 | * Since we don't want these sockets to keep a namespace alive by |
| 1357 | * themselves, we drop the socket's namespace refcount after creation. |
| 1358 | * These sockets are freed when the namespace exits using the pernet |
| 1359 | * exit hook. |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1360 | */ |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1361 | static int l2tp_tunnel_sock_create(struct net *net, |
| 1362 | u32 tunnel_id, |
| 1363 | u32 peer_tunnel_id, |
| 1364 | struct l2tp_tunnel_cfg *cfg, |
| 1365 | struct socket **sockp) |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1366 | { |
| 1367 | int err = -EINVAL; |
Eric Dumazet | 7bddd0d | 2010-04-04 01:02:46 -0700 | [diff] [blame] | 1368 | struct socket *sock = NULL; |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1369 | struct udp_port_cfg udp_conf; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1370 | |
| 1371 | switch (cfg->encap) { |
| 1372 | case L2TP_ENCAPTYPE_UDP: |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1373 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 1374 | |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 1375 | #if IS_ENABLED(CONFIG_IPV6) |
| 1376 | if (cfg->local_ip6 && cfg->peer_ip6) { |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1377 | udp_conf.family = AF_INET6; |
| 1378 | memcpy(&udp_conf.local_ip6, cfg->local_ip6, |
| 1379 | sizeof(udp_conf.local_ip6)); |
| 1380 | memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, |
| 1381 | sizeof(udp_conf.peer_ip6)); |
| 1382 | udp_conf.use_udp6_tx_checksums = |
Wang Shanker | 018f825 | 2016-04-29 01:29:43 +0800 | [diff] [blame] | 1383 | ! cfg->udp6_zero_tx_checksums; |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1384 | udp_conf.use_udp6_rx_checksums = |
Wang Shanker | 018f825 | 2016-04-29 01:29:43 +0800 | [diff] [blame] | 1385 | ! cfg->udp6_zero_rx_checksums; |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 1386 | } else |
| 1387 | #endif |
| 1388 | { |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1389 | udp_conf.family = AF_INET; |
| 1390 | udp_conf.local_ip = cfg->local_ip; |
| 1391 | udp_conf.peer_ip = cfg->peer_ip; |
| 1392 | udp_conf.use_udp_checksums = cfg->use_udp_checksums; |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 1393 | } |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1394 | |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1395 | udp_conf.local_udp_port = htons(cfg->local_udp_port); |
| 1396 | udp_conf.peer_udp_port = htons(cfg->peer_udp_port); |
| 1397 | |
| 1398 | err = udp_sock_create(net, &udp_conf, &sock); |
| 1399 | if (err < 0) |
| 1400 | goto out; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1401 | |
| 1402 | break; |
| 1403 | |
| 1404 | case L2TP_ENCAPTYPE_IP: |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 1405 | #if IS_ENABLED(CONFIG_IPV6) |
| 1406 | if (cfg->local_ip6 && cfg->peer_ip6) { |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1407 | struct sockaddr_l2tpip6 ip6_addr = {0}; |
| 1408 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 1409 | err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1410 | IPPROTO_L2TP, &sock); |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1411 | if (err < 0) |
| 1412 | goto out; |
| 1413 | |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1414 | ip6_addr.l2tp_family = AF_INET6; |
| 1415 | memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, |
| 1416 | sizeof(ip6_addr.l2tp_addr)); |
| 1417 | ip6_addr.l2tp_conn_id = tunnel_id; |
| 1418 | err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, |
| 1419 | sizeof(ip6_addr)); |
| 1420 | if (err < 0) |
| 1421 | goto out; |
| 1422 | |
| 1423 | ip6_addr.l2tp_family = AF_INET6; |
| 1424 | memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, |
| 1425 | sizeof(ip6_addr.l2tp_addr)); |
| 1426 | ip6_addr.l2tp_conn_id = peer_tunnel_id; |
| 1427 | err = kernel_connect(sock, |
| 1428 | (struct sockaddr *) &ip6_addr, |
| 1429 | sizeof(ip6_addr), 0); |
| 1430 | if (err < 0) |
| 1431 | goto out; |
| 1432 | } else |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 1433 | #endif |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1434 | { |
Tom Herbert | 85644b4 | 2014-07-13 19:49:48 -0700 | [diff] [blame] | 1435 | struct sockaddr_l2tpip ip_addr = {0}; |
| 1436 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 1437 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1438 | IPPROTO_L2TP, &sock); |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1439 | if (err < 0) |
| 1440 | goto out; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1441 | |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1442 | ip_addr.l2tp_family = AF_INET; |
| 1443 | ip_addr.l2tp_addr = cfg->local_ip; |
| 1444 | ip_addr.l2tp_conn_id = tunnel_id; |
| 1445 | err = kernel_bind(sock, (struct sockaddr *) &ip_addr, |
| 1446 | sizeof(ip_addr)); |
| 1447 | if (err < 0) |
| 1448 | goto out; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1449 | |
James Chapman | 5dac94e | 2012-04-29 21:48:55 +0000 | [diff] [blame] | 1450 | ip_addr.l2tp_family = AF_INET; |
| 1451 | ip_addr.l2tp_addr = cfg->peer_ip; |
| 1452 | ip_addr.l2tp_conn_id = peer_tunnel_id; |
| 1453 | err = kernel_connect(sock, (struct sockaddr *) &ip_addr, |
| 1454 | sizeof(ip_addr), 0); |
| 1455 | if (err < 0) |
| 1456 | goto out; |
| 1457 | } |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | |
| 1460 | default: |
| 1461 | goto out; |
| 1462 | } |
| 1463 | |
| 1464 | out: |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1465 | *sockp = sock; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1466 | if ((err < 0) && sock) { |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1467 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 1468 | sock_release(sock); |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1469 | *sockp = NULL; |
| 1470 | } |
| 1471 | |
| 1472 | return err; |
| 1473 | } |
| 1474 | |
Eric Dumazet | 37159ef | 2012-09-04 07:18:57 +0000 | [diff] [blame] | 1475 | static struct lock_class_key l2tp_socket_class; |
| 1476 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1477 | int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) |
| 1478 | { |
| 1479 | struct l2tp_tunnel *tunnel = NULL; |
| 1480 | int err; |
| 1481 | struct socket *sock = NULL; |
| 1482 | struct sock *sk = NULL; |
| 1483 | struct l2tp_net *pn; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1484 | enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Get the tunnel socket from the fd, which was opened by |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1487 | * the userspace L2TP daemon. If not specified, create a |
| 1488 | * kernel socket. |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1489 | */ |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1490 | if (fd < 0) { |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1491 | err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, |
| 1492 | cfg, &sock); |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1493 | if (err < 0) |
| 1494 | goto err; |
| 1495 | } else { |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1496 | sock = sockfd_lookup(fd, &err); |
| 1497 | if (!sock) { |
Tom Parkin | cbb95e0 | 2013-01-31 23:43:02 +0000 | [diff] [blame] | 1498 | pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1499 | tunnel_id, fd, err); |
Tom Parkin | cbb95e0 | 2013-01-31 23:43:02 +0000 | [diff] [blame] | 1500 | err = -EBADF; |
| 1501 | goto err; |
| 1502 | } |
| 1503 | |
| 1504 | /* Reject namespace mismatches */ |
| 1505 | if (!net_eq(sock_net(sock->sk), net)) { |
| 1506 | pr_err("tunl %u: netns mismatch\n", tunnel_id); |
| 1507 | err = -EINVAL; |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1508 | goto err; |
| 1509 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | sk = sock->sk; |
| 1513 | |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1514 | if (cfg != NULL) |
| 1515 | encap = cfg->encap; |
| 1516 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1517 | /* Quick sanity checks */ |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1518 | switch (encap) { |
| 1519 | case L2TP_ENCAPTYPE_UDP: |
| 1520 | err = -EPROTONOSUPPORT; |
| 1521 | if (sk->sk_protocol != IPPROTO_UDP) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1522 | pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1523 | tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); |
| 1524 | goto err; |
| 1525 | } |
| 1526 | break; |
| 1527 | case L2TP_ENCAPTYPE_IP: |
| 1528 | err = -EPROTONOSUPPORT; |
| 1529 | if (sk->sk_protocol != IPPROTO_L2TP) { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1530 | pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1531 | tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); |
| 1532 | goto err; |
| 1533 | } |
| 1534 | break; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* Check if this socket has already been prepped */ |
David S. Miller | 8d8a51e | 2013-10-08 15:44:26 -0400 | [diff] [blame] | 1538 | tunnel = l2tp_tunnel(sk); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1539 | if (tunnel != NULL) { |
| 1540 | /* This socket has already been prepped */ |
| 1541 | err = -EBUSY; |
| 1542 | goto err; |
| 1543 | } |
| 1544 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1545 | tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); |
| 1546 | if (tunnel == NULL) { |
| 1547 | err = -ENOMEM; |
| 1548 | goto err; |
| 1549 | } |
| 1550 | |
| 1551 | tunnel->version = version; |
| 1552 | tunnel->tunnel_id = tunnel_id; |
| 1553 | tunnel->peer_tunnel_id = peer_tunnel_id; |
| 1554 | tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; |
| 1555 | |
| 1556 | tunnel->magic = L2TP_TUNNEL_MAGIC; |
| 1557 | sprintf(&tunnel->name[0], "tunl %u", tunnel_id); |
| 1558 | rwlock_init(&tunnel->hlist_lock); |
Guillaume Nault | f3c66d4 | 2017-09-01 17:58:48 +0200 | [diff] [blame] | 1559 | tunnel->acpt_newsess = true; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* The net we belong to */ |
| 1562 | tunnel->l2tp_net = net; |
| 1563 | pn = l2tp_pernet(net); |
| 1564 | |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1565 | if (cfg != NULL) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1566 | tunnel->debug = cfg->debug; |
| 1567 | |
François Cachereul | e18503f | 2013-10-02 10:16:02 +0200 | [diff] [blame] | 1568 | #if IS_ENABLED(CONFIG_IPV6) |
| 1569 | if (sk->sk_family == PF_INET6) { |
| 1570 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 1571 | |
| 1572 | if (ipv6_addr_v4mapped(&np->saddr) && |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 1573 | ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { |
François Cachereul | e18503f | 2013-10-02 10:16:02 +0200 | [diff] [blame] | 1574 | struct inet_sock *inet = inet_sk(sk); |
| 1575 | |
| 1576 | tunnel->v4mapped = true; |
| 1577 | inet->inet_saddr = np->saddr.s6_addr32[3]; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 1578 | inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3]; |
| 1579 | inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3]; |
François Cachereul | e18503f | 2013-10-02 10:16:02 +0200 | [diff] [blame] | 1580 | } else { |
| 1581 | tunnel->v4mapped = false; |
| 1582 | } |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1586 | /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1587 | tunnel->encap = encap; |
| 1588 | if (encap == L2TP_ENCAPTYPE_UDP) { |
Guillaume Nault | a5c5e2d | 2016-06-08 12:59:17 +0200 | [diff] [blame] | 1589 | struct udp_tunnel_sock_cfg udp_cfg = { }; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1590 | |
Andy Zhou | c8fffce | 2014-09-16 17:31:19 -0700 | [diff] [blame] | 1591 | udp_cfg.sk_user_data = tunnel; |
| 1592 | udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP; |
| 1593 | udp_cfg.encap_rcv = l2tp_udp_encap_recv; |
| 1594 | udp_cfg.encap_destroy = l2tp_udp_encap_destroy; |
| 1595 | |
| 1596 | setup_udp_tunnel_sock(net, sock, &udp_cfg); |
| 1597 | } else { |
| 1598 | sk->sk_user_data = tunnel; |
| 1599 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1600 | |
| 1601 | /* Hook on the tunnel socket destructor so that we can cleanup |
| 1602 | * if the tunnel socket goes away. |
| 1603 | */ |
| 1604 | tunnel->old_sk_destruct = sk->sk_destruct; |
| 1605 | sk->sk_destruct = &l2tp_tunnel_destruct; |
| 1606 | tunnel->sock = sk; |
Tom Parkin | 80d84ef | 2013-01-22 05:13:48 +0000 | [diff] [blame] | 1607 | tunnel->fd = fd; |
Eric Dumazet | 37159ef | 2012-09-04 07:18:57 +0000 | [diff] [blame] | 1608 | lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); |
| 1609 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1610 | sk->sk_allocation = GFP_ATOMIC; |
| 1611 | |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1612 | /* Init delete workqueue struct */ |
| 1613 | INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); |
| 1614 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1615 | /* Add tunnel to our list */ |
| 1616 | INIT_LIST_HEAD(&tunnel->list); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1617 | |
| 1618 | /* Bump the reference count. The tunnel context is deleted |
Eric Dumazet | 1769192 | 2011-05-11 18:22:36 +0000 | [diff] [blame] | 1619 | * only when this drops to zero. Must be done before list insertion |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1620 | */ |
Reshetova, Elena | fbea9e0 | 2017-07-04 15:52:57 +0300 | [diff] [blame] | 1621 | refcount_set(&tunnel->ref_count, 1); |
Eric Dumazet | 1769192 | 2011-05-11 18:22:36 +0000 | [diff] [blame] | 1622 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); |
| 1623 | list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); |
| 1624 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1625 | |
| 1626 | err = 0; |
| 1627 | err: |
| 1628 | if (tunnelp) |
| 1629 | *tunnelp = tunnel; |
| 1630 | |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 1631 | /* If tunnel's socket was created by the kernel, it doesn't |
| 1632 | * have a file. |
| 1633 | */ |
| 1634 | if (sock && sock->file) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1635 | sockfd_put(sock); |
| 1636 | |
| 1637 | return err; |
| 1638 | } |
| 1639 | EXPORT_SYMBOL_GPL(l2tp_tunnel_create); |
| 1640 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1641 | /* This function is used by the netlink TUNNEL_DELETE command. |
| 1642 | */ |
Sabrina Dubroca | 62b982e | 2017-09-26 16:16:43 +0200 | [diff] [blame] | 1643 | void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1644 | { |
Sabrina Dubroca | 62b982e | 2017-09-26 16:16:43 +0200 | [diff] [blame] | 1645 | if (!test_and_set_bit(0, &tunnel->dead)) { |
| 1646 | l2tp_tunnel_inc_refcount(tunnel); |
| 1647 | queue_work(l2tp_wq, &tunnel->del_work); |
Alexander Couzens | 06a15f5 | 2015-09-28 11:32:42 +0200 | [diff] [blame] | 1648 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1649 | } |
| 1650 | EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); |
| 1651 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1652 | /* Really kill the session. |
| 1653 | */ |
| 1654 | void l2tp_session_free(struct l2tp_session *session) |
| 1655 | { |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 1656 | struct l2tp_tunnel *tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1657 | |
Reshetova, Elena | fbea9e0 | 2017-07-04 15:52:57 +0300 | [diff] [blame] | 1658 | BUG_ON(refcount_read(&session->ref_count) != 0); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1659 | |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 1660 | if (tunnel) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1661 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1662 | sock_put(tunnel->sock); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1663 | session->tunnel = NULL; |
| 1664 | l2tp_tunnel_dec_refcount(tunnel); |
| 1665 | } |
| 1666 | |
| 1667 | kfree(session); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1668 | } |
| 1669 | EXPORT_SYMBOL_GPL(l2tp_session_free); |
| 1670 | |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 1671 | /* Remove an l2tp session from l2tp_core's hash lists. |
| 1672 | * Provides a tidyup interface for pseudowire code which can't just route all |
| 1673 | * shutdown via. l2tp_session_delete and a pseudowire-specific session_close |
| 1674 | * callback. |
| 1675 | */ |
| 1676 | void __l2tp_session_unhash(struct l2tp_session *session) |
| 1677 | { |
| 1678 | struct l2tp_tunnel *tunnel = session->tunnel; |
| 1679 | |
| 1680 | /* Remove the session from core hashes */ |
| 1681 | if (tunnel) { |
| 1682 | /* Remove from the per-tunnel hash */ |
| 1683 | write_lock_bh(&tunnel->hlist_lock); |
| 1684 | hlist_del_init(&session->hlist); |
| 1685 | write_unlock_bh(&tunnel->hlist_lock); |
| 1686 | |
| 1687 | /* For L2TPv3 we have a per-net hash: remove from there, too */ |
| 1688 | if (tunnel->version != L2TP_HDR_VER_2) { |
| 1689 | struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); |
| 1690 | spin_lock_bh(&pn->l2tp_session_hlist_lock); |
| 1691 | hlist_del_init_rcu(&session->global_hlist); |
| 1692 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
| 1693 | synchronize_rcu(); |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | EXPORT_SYMBOL_GPL(__l2tp_session_unhash); |
| 1698 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1699 | /* This function is used by the netlink SESSION_DELETE command and by |
| 1700 | pseudowire modules. |
| 1701 | */ |
| 1702 | int l2tp_session_delete(struct l2tp_session *session) |
| 1703 | { |
Guillaume Nault | b228a94 | 2017-09-22 15:39:24 +0200 | [diff] [blame] | 1704 | if (test_and_set_bit(0, &session->dead)) |
| 1705 | return 0; |
| 1706 | |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 1707 | __l2tp_session_unhash(session); |
Tom Parkin | 4c6e2fd | 2013-03-19 06:11:20 +0000 | [diff] [blame] | 1708 | l2tp_session_queue_purge(session); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1709 | if (session->session_close != NULL) |
| 1710 | (*session->session_close)(session); |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 1711 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1712 | l2tp_session_dec_refcount(session); |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 1713 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | EXPORT_SYMBOL_GPL(l2tp_session_delete); |
| 1717 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1718 | /* We come here whenever a session's send_seq, cookie_len or |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 1719 | * l2specific_type parameters are set. |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1720 | */ |
Guillaume Nault | bb5016e | 2014-03-06 11:14:30 +0100 | [diff] [blame] | 1721 | void l2tp_session_set_header_len(struct l2tp_session *session, int version) |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1722 | { |
| 1723 | if (version == L2TP_HDR_VER_2) { |
| 1724 | session->hdr_len = 6; |
| 1725 | if (session->send_seq) |
| 1726 | session->hdr_len += 4; |
| 1727 | } else { |
Lorenzo Bianconi | 62e7b6a | 2018-01-16 23:01:55 +0100 | [diff] [blame^] | 1728 | session->hdr_len = 4 + session->cookie_len; |
| 1729 | session->hdr_len += l2tp_get_l2specific_len(session); |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 1730 | if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) |
| 1731 | session->hdr_len += 4; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | } |
Guillaume Nault | bb5016e | 2014-03-06 11:14:30 +0100 | [diff] [blame] | 1735 | EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1736 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1737 | struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) |
| 1738 | { |
| 1739 | struct l2tp_session *session; |
| 1740 | |
| 1741 | session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); |
| 1742 | if (session != NULL) { |
| 1743 | session->magic = L2TP_SESSION_MAGIC; |
| 1744 | session->tunnel = tunnel; |
| 1745 | |
| 1746 | session->session_id = session_id; |
| 1747 | session->peer_session_id = peer_session_id; |
James Chapman | d301e32 | 2012-05-09 23:43:09 +0000 | [diff] [blame] | 1748 | session->nr = 0; |
James Chapman | 8a1631d | 2013-07-02 20:28:59 +0100 | [diff] [blame] | 1749 | if (tunnel->version == L2TP_HDR_VER_2) |
| 1750 | session->nr_max = 0xffff; |
| 1751 | else |
| 1752 | session->nr_max = 0xffffff; |
| 1753 | session->nr_window_size = session->nr_max / 2; |
James Chapman | a0dbd82 | 2013-07-02 20:29:00 +0100 | [diff] [blame] | 1754 | session->nr_oos_count_max = 4; |
| 1755 | |
| 1756 | /* Use NR of first received packet */ |
| 1757 | session->reorder_skip = 1; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1758 | |
| 1759 | sprintf(&session->name[0], "sess %u/%u", |
| 1760 | tunnel->tunnel_id, session->session_id); |
| 1761 | |
| 1762 | skb_queue_head_init(&session->reorder_q); |
| 1763 | |
| 1764 | INIT_HLIST_NODE(&session->hlist); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1765 | INIT_HLIST_NODE(&session->global_hlist); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1766 | |
| 1767 | /* Inherit debug options from tunnel */ |
| 1768 | session->debug = tunnel->debug; |
| 1769 | |
| 1770 | if (cfg) { |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1771 | session->pwtype = cfg->pw_type; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1772 | session->debug = cfg->debug; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1773 | session->mtu = cfg->mtu; |
| 1774 | session->mru = cfg->mru; |
| 1775 | session->send_seq = cfg->send_seq; |
| 1776 | session->recv_seq = cfg->recv_seq; |
| 1777 | session->lns_mode = cfg->lns_mode; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1778 | session->reorder_timeout = cfg->reorder_timeout; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1779 | session->l2specific_type = cfg->l2specific_type; |
| 1780 | session->l2specific_len = cfg->l2specific_len; |
| 1781 | session->cookie_len = cfg->cookie_len; |
| 1782 | memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); |
| 1783 | session->peer_cookie_len = cfg->peer_cookie_len; |
| 1784 | memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1787 | if (tunnel->version == L2TP_HDR_VER_2) |
| 1788 | session->build_header = l2tp_build_l2tpv2_header; |
| 1789 | else |
| 1790 | session->build_header = l2tp_build_l2tpv3_header; |
| 1791 | |
| 1792 | l2tp_session_set_header_len(session, tunnel->version); |
| 1793 | |
Guillaume Nault | 9ee369a | 2017-08-25 16:22:17 +0200 | [diff] [blame] | 1794 | refcount_set(&session->ref_count, 1); |
| 1795 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 1796 | return session; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 1799 | return ERR_PTR(-ENOMEM); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1800 | } |
| 1801 | EXPORT_SYMBOL_GPL(l2tp_session_create); |
| 1802 | |
| 1803 | /***************************************************************************** |
| 1804 | * Init and cleanup |
| 1805 | *****************************************************************************/ |
| 1806 | |
| 1807 | static __net_init int l2tp_init_net(struct net *net) |
| 1808 | { |
Jiri Pirko | e773aaf | 2010-04-23 00:53:39 +0000 | [diff] [blame] | 1809 | struct l2tp_net *pn = net_generic(net, l2tp_net_id); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1810 | int hash; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1811 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1812 | INIT_LIST_HEAD(&pn->l2tp_tunnel_list); |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 1813 | spin_lock_init(&pn->l2tp_tunnel_list_lock); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1814 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1815 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) |
| 1816 | INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); |
| 1817 | |
James Chapman | e02d494 | 2010-04-02 06:19:16 +0000 | [diff] [blame] | 1818 | spin_lock_init(&pn->l2tp_session_hlist_lock); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1819 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1820 | return 0; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1823 | static __net_exit void l2tp_exit_net(struct net *net) |
| 1824 | { |
| 1825 | struct l2tp_net *pn = l2tp_pernet(net); |
| 1826 | struct l2tp_tunnel *tunnel = NULL; |
Vasily Averin | 1e7af3b | 2017-11-12 22:30:31 +0300 | [diff] [blame] | 1827 | int hash; |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1828 | |
| 1829 | rcu_read_lock_bh(); |
| 1830 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { |
Jiri Slaby | 4dc12ff | 2017-10-25 15:57:55 +0200 | [diff] [blame] | 1831 | l2tp_tunnel_delete(tunnel); |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1832 | } |
| 1833 | rcu_read_unlock_bh(); |
Sabrina Dubroca | 2f86953 | 2016-09-02 10:22:54 +0200 | [diff] [blame] | 1834 | |
| 1835 | flush_workqueue(l2tp_wq); |
| 1836 | rcu_barrier(); |
Vasily Averin | 1e7af3b | 2017-11-12 22:30:31 +0300 | [diff] [blame] | 1837 | |
| 1838 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) |
| 1839 | WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1842 | static struct pernet_operations l2tp_net_ops = { |
| 1843 | .init = l2tp_init_net, |
Tom Parkin | 167eb17 | 2013-01-31 23:43:03 +0000 | [diff] [blame] | 1844 | .exit = l2tp_exit_net, |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1845 | .id = &l2tp_net_id, |
| 1846 | .size = sizeof(struct l2tp_net), |
| 1847 | }; |
| 1848 | |
| 1849 | static int __init l2tp_init(void) |
| 1850 | { |
| 1851 | int rc = 0; |
| 1852 | |
| 1853 | rc = register_pernet_device(&l2tp_net_ops); |
| 1854 | if (rc) |
| 1855 | goto out; |
| 1856 | |
ZhangZhen | 59ff3eb | 2014-03-27 09:41:47 +0800 | [diff] [blame] | 1857 | l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1858 | if (!l2tp_wq) { |
| 1859 | pr_err("alloc_workqueue failed\n"); |
WANG Cong | 67e04c2 | 2015-04-03 13:46:09 -0700 | [diff] [blame] | 1860 | unregister_pernet_device(&l2tp_net_ops); |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1861 | rc = -ENOMEM; |
| 1862 | goto out; |
| 1863 | } |
| 1864 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1865 | pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1866 | |
| 1867 | out: |
| 1868 | return rc; |
| 1869 | } |
| 1870 | |
| 1871 | static void __exit l2tp_exit(void) |
| 1872 | { |
| 1873 | unregister_pernet_device(&l2tp_net_ops); |
Tom Parkin | f8ccac0 | 2013-01-31 23:43:00 +0000 | [diff] [blame] | 1874 | if (l2tp_wq) { |
| 1875 | destroy_workqueue(l2tp_wq); |
| 1876 | l2tp_wq = NULL; |
| 1877 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | module_init(l2tp_init); |
| 1881 | module_exit(l2tp_exit); |
| 1882 | |
| 1883 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 1884 | MODULE_DESCRIPTION("L2TP core"); |
| 1885 | MODULE_LICENSE("GPL"); |
| 1886 | MODULE_VERSION(L2TP_DRV_VERSION); |
| 1887 | |