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