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