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