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