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