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