]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
20dcb110 | 2 | /* L2TP core. |
fd558d18 JC |
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) | |
fd558d18 JC |
15 | */ |
16 | ||
a4ca44fa JP |
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
18 | ||
fd558d18 JC |
19 | #include <linux/module.h> |
20 | #include <linux/string.h> | |
21 | #include <linux/list.h> | |
e02d494d | 22 | #include <linux/rculist.h> |
fd558d18 JC |
23 | #include <linux/uaccess.h> |
24 | ||
25 | #include <linux/kernel.h> | |
26 | #include <linux/spinlock.h> | |
27 | #include <linux/kthread.h> | |
28 | #include <linux/sched.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/errno.h> | |
31 | #include <linux/jiffies.h> | |
32 | ||
33 | #include <linux/netdevice.h> | |
34 | #include <linux/net.h> | |
35 | #include <linux/inetdevice.h> | |
36 | #include <linux/skbuff.h> | |
37 | #include <linux/init.h> | |
0d76751f | 38 | #include <linux/in.h> |
fd558d18 JC |
39 | #include <linux/ip.h> |
40 | #include <linux/udp.h> | |
0d76751f | 41 | #include <linux/l2tp.h> |
fd558d18 JC |
42 | #include <linux/hash.h> |
43 | #include <linux/sort.h> | |
44 | #include <linux/file.h> | |
45 | #include <linux/nsproxy.h> | |
46 | #include <net/net_namespace.h> | |
47 | #include <net/netns/generic.h> | |
48 | #include <net/dst.h> | |
49 | #include <net/ip.h> | |
50 | #include <net/udp.h> | |
85644b4d | 51 | #include <net/udp_tunnel.h> |
309795f4 | 52 | #include <net/inet_common.h> |
fd558d18 | 53 | #include <net/xfrm.h> |
0d76751f | 54 | #include <net/protocol.h> |
d2cf3361 BL |
55 | #include <net/inet6_connection_sock.h> |
56 | #include <net/inet_ecn.h> | |
57 | #include <net/ip6_route.h> | |
d499bd2e | 58 | #include <net/ip6_checksum.h> |
fd558d18 JC |
59 | |
60 | #include <asm/byteorder.h> | |
60063497 | 61 | #include <linux/atomic.h> |
fd558d18 JC |
62 | |
63 | #include "l2tp_core.h" | |
6b7bdcd7 | 64 | #include "trace.h" |
fd558d18 | 65 | |
3f117d6f TP |
66 | #define CREATE_TRACE_POINTS |
67 | #include "trace.h" | |
68 | ||
fd558d18 JC |
69 | #define L2TP_DRV_VERSION "V2.0" |
70 | ||
71 | /* L2TP header constants */ | |
72 | #define L2TP_HDRFLAG_T 0x8000 | |
73 | #define L2TP_HDRFLAG_L 0x4000 | |
74 | #define L2TP_HDRFLAG_S 0x0800 | |
75 | #define L2TP_HDRFLAG_O 0x0200 | |
76 | #define L2TP_HDRFLAG_P 0x0100 | |
77 | ||
78 | #define L2TP_HDR_VER_MASK 0x000F | |
79 | #define L2TP_HDR_VER_2 0x0002 | |
f7faffa3 | 80 | #define L2TP_HDR_VER_3 0x0003 |
fd558d18 JC |
81 | |
82 | /* L2TPv3 default L2-specific sublayer */ | |
83 | #define L2TP_SLFLAG_S 0x40000000 | |
84 | #define L2TP_SL_SEQ_MASK 0x00ffffff | |
85 | ||
91c52470 | 86 | #define L2TP_HDR_SIZE_MAX 14 |
fd558d18 JC |
87 | |
88 | /* Default trace flags */ | |
89 | #define L2TP_DEFAULT_DEBUG_FLAGS 0 | |
90 | ||
fd558d18 JC |
91 | /* Private data stored for received packets in the skb. |
92 | */ | |
93 | struct l2tp_skb_cb { | |
f7faffa3 | 94 | u32 ns; |
fd558d18 JC |
95 | u16 has_seq; |
96 | u16 length; | |
97 | unsigned long expires; | |
98 | }; | |
99 | ||
efcd8c85 | 100 | #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)]) |
fd558d18 | 101 | |
f8ccac0e | 102 | static struct workqueue_struct *l2tp_wq; |
fd558d18 JC |
103 | |
104 | /* per-net private data for this module */ | |
105 | static unsigned int l2tp_net_id; | |
106 | struct l2tp_net { | |
107 | struct list_head l2tp_tunnel_list; | |
20dcb110 | 108 | /* Lock for write access to l2tp_tunnel_list */ |
e02d494d | 109 | spinlock_t l2tp_tunnel_list_lock; |
f7faffa3 | 110 | struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; |
20dcb110 | 111 | /* Lock for write access to l2tp_session_hlist */ |
e02d494d | 112 | spinlock_t l2tp_session_hlist_lock; |
fd558d18 JC |
113 | }; |
114 | ||
b954f940 PA |
115 | #if IS_ENABLED(CONFIG_IPV6) |
116 | static bool l2tp_sk_is_v6(struct sock *sk) | |
117 | { | |
118 | return sk->sk_family == PF_INET6 && | |
119 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr); | |
120 | } | |
121 | #endif | |
fc130840 | 122 | |
9aaef50c | 123 | static inline struct l2tp_net *l2tp_pernet(const struct net *net) |
fd558d18 | 124 | { |
fd558d18 JC |
125 | return net_generic(net, l2tp_net_id); |
126 | } | |
127 | ||
f7faffa3 JC |
128 | /* Session hash global list for L2TPv3. |
129 | * The session_id SHOULD be random according to RFC3931, but several | |
130 | * L2TP implementations use incrementing session_ids. So we do a real | |
131 | * hash on the session_id, rather than a simple bitmask. | |
132 | */ | |
133 | static inline struct hlist_head * | |
134 | l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) | |
135 | { | |
136 | return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; | |
f7faffa3 JC |
137 | } |
138 | ||
fd558d18 JC |
139 | /* Session hash list. |
140 | * The session_id SHOULD be random according to RFC2661, but several | |
141 | * L2TP implementations (Cisco and Microsoft) use incrementing | |
142 | * session_ids. So we do a real hash on the session_id, rather than a | |
143 | * simple bitmask. | |
144 | */ | |
145 | static inline struct hlist_head * | |
146 | l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) | |
147 | { | |
148 | return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; | |
149 | } | |
150 | ||
52016e25 | 151 | static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) |
d00fa9ad | 152 | { |
6b7bdcd7 | 153 | trace_free_tunnel(tunnel); |
d00fa9ad JC |
154 | sock_put(tunnel->sock); |
155 | /* the tunnel is freed in the socket destructor */ | |
156 | } | |
52016e25 TP |
157 | |
158 | static void l2tp_session_free(struct l2tp_session *session) | |
159 | { | |
6b7bdcd7 | 160 | trace_free_session(session); |
45faeff1 TP |
161 | if (session->tunnel) |
162 | l2tp_tunnel_dec_refcount(session->tunnel); | |
163 | kfree(session); | |
164 | } | |
6b7bdcd7 | 165 | |
45faeff1 TP |
166 | struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk) |
167 | { | |
168 | struct l2tp_tunnel *tunnel = sk->sk_user_data; | |
169 | ||
170 | if (tunnel) | |
52016e25 | 171 | if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC)) |
45faeff1 | 172 | return NULL; |
52016e25 | 173 | |
45faeff1 | 174 | return tunnel; |
52016e25 | 175 | } |
45faeff1 | 176 | EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel); |
52016e25 TP |
177 | |
178 | void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel) | |
179 | { | |
180 | refcount_inc(&tunnel->ref_count); | |
181 | } | |
182 | EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount); | |
183 | ||
184 | void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel) | |
185 | { | |
186 | if (refcount_dec_and_test(&tunnel->ref_count)) | |
187 | l2tp_tunnel_free(tunnel); | |
188 | } | |
189 | EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount); | |
190 | ||
191 | void l2tp_session_inc_refcount(struct l2tp_session *session) | |
192 | { | |
193 | refcount_inc(&session->ref_count); | |
194 | } | |
195 | EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount); | |
196 | ||
197 | void l2tp_session_dec_refcount(struct l2tp_session *session) | |
198 | { | |
199 | if (refcount_dec_and_test(&session->ref_count)) | |
200 | l2tp_session_free(session); | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount); | |
d00fa9ad | 203 | |
54652eb1 GN |
204 | /* Lookup a tunnel. A new reference is held on the returned tunnel. */ |
205 | struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id) | |
206 | { | |
207 | const struct l2tp_net *pn = l2tp_pernet(net); | |
208 | struct l2tp_tunnel *tunnel; | |
209 | ||
210 | rcu_read_lock_bh(); | |
211 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { | |
a622b400 ED |
212 | if (tunnel->tunnel_id == tunnel_id && |
213 | refcount_inc_not_zero(&tunnel->ref_count)) { | |
54652eb1 GN |
214 | rcu_read_unlock_bh(); |
215 | ||
216 | return tunnel; | |
217 | } | |
218 | } | |
219 | rcu_read_unlock_bh(); | |
220 | ||
221 | return NULL; | |
222 | } | |
223 | EXPORT_SYMBOL_GPL(l2tp_tunnel_get); | |
224 | ||
5846c131 GN |
225 | struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth) |
226 | { | |
227 | const struct l2tp_net *pn = l2tp_pernet(net); | |
228 | struct l2tp_tunnel *tunnel; | |
229 | int count = 0; | |
230 | ||
231 | rcu_read_lock_bh(); | |
232 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { | |
a622b400 ED |
233 | if (++count > nth && |
234 | refcount_inc_not_zero(&tunnel->ref_count)) { | |
5846c131 GN |
235 | rcu_read_unlock_bh(); |
236 | return tunnel; | |
237 | } | |
238 | } | |
239 | rcu_read_unlock_bh(); | |
240 | ||
241 | return NULL; | |
242 | } | |
243 | EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth); | |
244 | ||
01e28b92 GN |
245 | struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel, |
246 | u32 session_id) | |
61b9a047 GN |
247 | { |
248 | struct hlist_head *session_list; | |
249 | struct l2tp_session *session; | |
250 | ||
01e28b92 | 251 | session_list = l2tp_session_id_hash(tunnel, session_id); |
61b9a047 | 252 | |
01e28b92 GN |
253 | read_lock_bh(&tunnel->hlist_lock); |
254 | hlist_for_each_entry(session, session_list, hlist) | |
255 | if (session->session_id == session_id) { | |
256 | l2tp_session_inc_refcount(session); | |
257 | read_unlock_bh(&tunnel->hlist_lock); | |
61b9a047 | 258 | |
01e28b92 | 259 | return session; |
61b9a047 | 260 | } |
01e28b92 | 261 | read_unlock_bh(&tunnel->hlist_lock); |
61b9a047 | 262 | |
01e28b92 GN |
263 | return NULL; |
264 | } | |
265 | EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session); | |
61b9a047 | 266 | |
01e28b92 GN |
267 | struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id) |
268 | { | |
269 | struct hlist_head *session_list; | |
270 | struct l2tp_session *session; | |
271 | ||
272 | session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id); | |
273 | ||
274 | rcu_read_lock_bh(); | |
275 | hlist_for_each_entry_rcu(session, session_list, global_hlist) | |
61b9a047 GN |
276 | if (session->session_id == session_id) { |
277 | l2tp_session_inc_refcount(session); | |
01e28b92 | 278 | rcu_read_unlock_bh(); |
61b9a047 GN |
279 | |
280 | return session; | |
281 | } | |
01e28b92 | 282 | rcu_read_unlock_bh(); |
61b9a047 GN |
283 | |
284 | return NULL; | |
285 | } | |
286 | EXPORT_SYMBOL_GPL(l2tp_session_get); | |
287 | ||
a4346210 | 288 | struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth) |
fd558d18 JC |
289 | { |
290 | int hash; | |
fd558d18 JC |
291 | struct l2tp_session *session; |
292 | int count = 0; | |
293 | ||
294 | read_lock_bh(&tunnel->hlist_lock); | |
295 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { | |
b67bfe0d | 296 | hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { |
fd558d18 | 297 | if (++count > nth) { |
e08293a4 | 298 | l2tp_session_inc_refcount(session); |
fd558d18 JC |
299 | read_unlock_bh(&tunnel->hlist_lock); |
300 | return session; | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
305 | read_unlock_bh(&tunnel->hlist_lock); | |
306 | ||
307 | return NULL; | |
308 | } | |
e08293a4 | 309 | EXPORT_SYMBOL_GPL(l2tp_session_get_nth); |
fd558d18 | 310 | |
309795f4 JC |
311 | /* Lookup a session by interface name. |
312 | * This is very inefficient but is only used by management interfaces. | |
313 | */ | |
9aaef50c | 314 | struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, |
a4346210 | 315 | const char *ifname) |
309795f4 JC |
316 | { |
317 | struct l2tp_net *pn = l2tp_pernet(net); | |
318 | int hash; | |
309795f4 JC |
319 | struct l2tp_session *session; |
320 | ||
e02d494d | 321 | rcu_read_lock_bh(); |
309795f4 | 322 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { |
b67bfe0d | 323 | hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { |
309795f4 | 324 | if (!strcmp(session->ifname, ifname)) { |
2777e2ab | 325 | l2tp_session_inc_refcount(session); |
e02d494d | 326 | rcu_read_unlock_bh(); |
2777e2ab | 327 | |
309795f4 JC |
328 | return session; |
329 | } | |
330 | } | |
331 | } | |
332 | ||
e02d494d | 333 | rcu_read_unlock_bh(); |
309795f4 JC |
334 | |
335 | return NULL; | |
336 | } | |
2777e2ab | 337 | EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); |
309795f4 | 338 | |
3953ae7b GN |
339 | int l2tp_session_register(struct l2tp_session *session, |
340 | struct l2tp_tunnel *tunnel) | |
dbdbc73b GN |
341 | { |
342 | struct l2tp_session *session_walk; | |
343 | struct hlist_head *g_head; | |
344 | struct hlist_head *head; | |
345 | struct l2tp_net *pn; | |
f3c66d4e | 346 | int err; |
dbdbc73b GN |
347 | |
348 | head = l2tp_session_id_hash(tunnel, session->session_id); | |
349 | ||
350 | write_lock_bh(&tunnel->hlist_lock); | |
f3c66d4e GN |
351 | if (!tunnel->acpt_newsess) { |
352 | err = -ENODEV; | |
353 | goto err_tlock; | |
354 | } | |
355 | ||
dbdbc73b | 356 | hlist_for_each_entry(session_walk, head, hlist) |
f3c66d4e GN |
357 | if (session_walk->session_id == session->session_id) { |
358 | err = -EEXIST; | |
359 | goto err_tlock; | |
360 | } | |
dbdbc73b GN |
361 | |
362 | if (tunnel->version == L2TP_HDR_VER_3) { | |
363 | pn = l2tp_pernet(tunnel->l2tp_net); | |
363a341d | 364 | g_head = l2tp_session_id_hash_2(pn, session->session_id); |
dbdbc73b GN |
365 | |
366 | spin_lock_bh(&pn->l2tp_session_hlist_lock); | |
f3c66d4e | 367 | |
0d0d9a38 RK |
368 | /* IP encap expects session IDs to be globally unique, while |
369 | * UDP encap doesn't. | |
370 | */ | |
dbdbc73b | 371 | hlist_for_each_entry(session_walk, g_head, global_hlist) |
0d0d9a38 RK |
372 | if (session_walk->session_id == session->session_id && |
373 | (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP || | |
374 | tunnel->encap == L2TP_ENCAPTYPE_IP)) { | |
f3c66d4e GN |
375 | err = -EEXIST; |
376 | goto err_tlock_pnlock; | |
377 | } | |
dbdbc73b | 378 | |
f3c66d4e | 379 | l2tp_tunnel_inc_refcount(tunnel); |
dbdbc73b | 380 | hlist_add_head_rcu(&session->global_hlist, g_head); |
f3c66d4e | 381 | |
dbdbc73b | 382 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
f3c66d4e GN |
383 | } else { |
384 | l2tp_tunnel_inc_refcount(tunnel); | |
dbdbc73b GN |
385 | } |
386 | ||
387 | hlist_add_head(&session->hlist, head); | |
388 | write_unlock_bh(&tunnel->hlist_lock); | |
389 | ||
6b7bdcd7 TP |
390 | trace_register_session(session); |
391 | ||
dbdbc73b GN |
392 | return 0; |
393 | ||
f3c66d4e | 394 | err_tlock_pnlock: |
dbdbc73b | 395 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
f3c66d4e | 396 | err_tlock: |
dbdbc73b GN |
397 | write_unlock_bh(&tunnel->hlist_lock); |
398 | ||
f3c66d4e | 399 | return err; |
dbdbc73b | 400 | } |
3953ae7b | 401 | EXPORT_SYMBOL_GPL(l2tp_session_register); |
dbdbc73b | 402 | |
fd558d18 JC |
403 | /***************************************************************************** |
404 | * Receive data handling | |
405 | *****************************************************************************/ | |
406 | ||
407 | /* Queue a skb in order. We come here only if the skb has an L2TP sequence | |
408 | * number. | |
409 | */ | |
410 | static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) | |
411 | { | |
412 | struct sk_buff *skbp; | |
413 | struct sk_buff *tmp; | |
f7faffa3 | 414 | u32 ns = L2TP_SKB_CB(skb)->ns; |
fd558d18 JC |
415 | |
416 | spin_lock_bh(&session->reorder_q.lock); | |
417 | skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { | |
418 | if (L2TP_SKB_CB(skbp)->ns > ns) { | |
419 | __skb_queue_before(&session->reorder_q, skbp, skb); | |
7b7c0719 | 420 | atomic_long_inc(&session->stats.rx_oos_packets); |
fd558d18 JC |
421 | goto out; |
422 | } | |
423 | } | |
424 | ||
425 | __skb_queue_tail(&session->reorder_q, skb); | |
426 | ||
427 | out: | |
428 | spin_unlock_bh(&session->reorder_q.lock); | |
429 | } | |
430 | ||
431 | /* Dequeue a single skb. | |
432 | */ | |
433 | static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) | |
434 | { | |
435 | struct l2tp_tunnel *tunnel = session->tunnel; | |
436 | int length = L2TP_SKB_CB(skb)->length; | |
437 | ||
438 | /* We're about to requeue the skb, so return resources | |
439 | * to its current owner (a socket receive buffer). | |
440 | */ | |
441 | skb_orphan(skb); | |
442 | ||
7b7c0719 TP |
443 | atomic_long_inc(&tunnel->stats.rx_packets); |
444 | atomic_long_add(length, &tunnel->stats.rx_bytes); | |
445 | atomic_long_inc(&session->stats.rx_packets); | |
446 | atomic_long_add(length, &session->stats.rx_bytes); | |
fd558d18 JC |
447 | |
448 | if (L2TP_SKB_CB(skb)->has_seq) { | |
449 | /* Bump our Nr */ | |
450 | session->nr++; | |
8a1631d5 | 451 | session->nr &= session->nr_max; |
6b7bdcd7 | 452 | trace_session_seqnum_update(session); |
fd558d18 JC |
453 | } |
454 | ||
455 | /* call private receive handler */ | |
0febc7b3 | 456 | if (session->recv_skb) |
fd558d18 JC |
457 | (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); |
458 | else | |
459 | kfree_skb(skb); | |
fd558d18 JC |
460 | } |
461 | ||
462 | /* Dequeue skbs from the session's reorder_q, subject to packet order. | |
463 | * Skbs that have been in the queue for too long are simply discarded. | |
464 | */ | |
465 | static void l2tp_recv_dequeue(struct l2tp_session *session) | |
466 | { | |
467 | struct sk_buff *skb; | |
468 | struct sk_buff *tmp; | |
469 | ||
470 | /* If the pkt at the head of the queue has the nr that we | |
471 | * expect to send up next, dequeue it and any other | |
472 | * in-sequence packets behind it. | |
473 | */ | |
e2e210c0 | 474 | start: |
fd558d18 JC |
475 | spin_lock_bh(&session->reorder_q.lock); |
476 | skb_queue_walk_safe(&session->reorder_q, skb, tmp) { | |
6b7bdcd7 TP |
477 | struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb); |
478 | ||
479 | /* If the packet has been pending on the queue for too long, discard it */ | |
480 | if (time_after(jiffies, cb->expires)) { | |
7b7c0719 TP |
481 | atomic_long_inc(&session->stats.rx_seq_discards); |
482 | atomic_long_inc(&session->stats.rx_errors); | |
6b7bdcd7 | 483 | trace_session_pkt_expired(session, cb->ns); |
38d40b3f | 484 | session->reorder_skip = 1; |
fd558d18 JC |
485 | __skb_unlink(skb, &session->reorder_q); |
486 | kfree_skb(skb); | |
fd558d18 JC |
487 | continue; |
488 | } | |
489 | ||
6b7bdcd7 | 490 | if (cb->has_seq) { |
38d40b3f | 491 | if (session->reorder_skip) { |
38d40b3f | 492 | session->reorder_skip = 0; |
6b7bdcd7 TP |
493 | session->nr = cb->ns; |
494 | trace_session_seqnum_reset(session); | |
38d40b3f | 495 | } |
6b7bdcd7 | 496 | if (cb->ns != session->nr) |
fd558d18 | 497 | goto out; |
fd558d18 JC |
498 | } |
499 | __skb_unlink(skb, &session->reorder_q); | |
500 | ||
501 | /* Process the skb. We release the queue lock while we | |
502 | * do so to let other contexts process the queue. | |
503 | */ | |
504 | spin_unlock_bh(&session->reorder_q.lock); | |
505 | l2tp_recv_dequeue_skb(session, skb); | |
e2e210c0 | 506 | goto start; |
fd558d18 JC |
507 | } |
508 | ||
509 | out: | |
510 | spin_unlock_bh(&session->reorder_q.lock); | |
511 | } | |
512 | ||
8a1631d5 JC |
513 | static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) |
514 | { | |
515 | u32 nws; | |
516 | ||
517 | if (nr >= session->nr) | |
518 | nws = nr - session->nr; | |
519 | else | |
520 | nws = (session->nr_max + 1) - (session->nr - nr); | |
521 | ||
522 | return nws < session->nr_window_size; | |
523 | } | |
524 | ||
b6dc01a4 JC |
525 | /* If packet has sequence numbers, queue it if acceptable. Returns 0 if |
526 | * acceptable, else non-zero. | |
527 | */ | |
528 | static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) | |
529 | { | |
6b7bdcd7 TP |
530 | struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb); |
531 | ||
532 | if (!l2tp_seq_check_rx_window(session, cb->ns)) { | |
8a1631d5 JC |
533 | /* Packet sequence number is outside allowed window. |
534 | * Discard it. | |
535 | */ | |
6b7bdcd7 | 536 | trace_session_pkt_outside_rx_window(session, cb->ns); |
8a1631d5 JC |
537 | goto discard; |
538 | } | |
539 | ||
b6dc01a4 JC |
540 | if (session->reorder_timeout != 0) { |
541 | /* Packet reordering enabled. Add skb to session's | |
542 | * reorder queue, in order of ns. | |
543 | */ | |
544 | l2tp_recv_queue_skb(session, skb); | |
a0dbd822 JC |
545 | goto out; |
546 | } | |
547 | ||
548 | /* Packet reordering disabled. Discard out-of-sequence packets, while | |
549 | * tracking the number if in-sequence packets after the first OOS packet | |
550 | * is seen. After nr_oos_count_max in-sequence packets, reset the | |
551 | * sequence number to re-enable packet reception. | |
552 | */ | |
6b7bdcd7 | 553 | if (cb->ns == session->nr) { |
a0dbd822 | 554 | skb_queue_tail(&session->reorder_q, skb); |
b6dc01a4 | 555 | } else { |
6b7bdcd7 | 556 | u32 nr_oos = cb->ns; |
a0dbd822 JC |
557 | u32 nr_next = (session->nr_oos + 1) & session->nr_max; |
558 | ||
559 | if (nr_oos == nr_next) | |
560 | session->nr_oos_count++; | |
561 | else | |
562 | session->nr_oos_count = 0; | |
563 | ||
564 | session->nr_oos = nr_oos; | |
565 | if (session->nr_oos_count > session->nr_oos_count_max) { | |
566 | session->reorder_skip = 1; | |
a0dbd822 JC |
567 | } |
568 | if (!session->reorder_skip) { | |
b6dc01a4 | 569 | atomic_long_inc(&session->stats.rx_seq_discards); |
6b7bdcd7 | 570 | trace_session_pkt_oos(session, cb->ns); |
b6dc01a4 JC |
571 | goto discard; |
572 | } | |
573 | skb_queue_tail(&session->reorder_q, skb); | |
574 | } | |
575 | ||
a0dbd822 | 576 | out: |
b6dc01a4 JC |
577 | return 0; |
578 | ||
579 | discard: | |
580 | return 1; | |
581 | } | |
582 | ||
f7faffa3 JC |
583 | /* Do receive processing of L2TP data frames. We handle both L2TPv2 |
584 | * and L2TPv3 data frames here. | |
585 | * | |
586 | * L2TPv2 Data Message Header | |
587 | * | |
588 | * 0 1 2 3 | |
589 | * 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 | |
590 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
591 | * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | | |
592 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
593 | * | Tunnel ID | Session ID | | |
594 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
595 | * | Ns (opt) | Nr (opt) | | |
596 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
597 | * | Offset Size (opt) | Offset pad... (opt) | |
598 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
599 | * | |
600 | * Data frames are marked by T=0. All other fields are the same as | |
601 | * those in L2TP control frames. | |
602 | * | |
603 | * L2TPv3 Data Message Header | |
604 | * | |
605 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
606 | * | L2TP Session Header | | |
607 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
608 | * | L2-Specific Sublayer | | |
609 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
610 | * | Tunnel Payload ... | |
611 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
612 | * | |
613 | * L2TPv3 Session Header Over IP | |
614 | * | |
615 | * 0 1 2 3 | |
616 | * 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 | |
617 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
618 | * | Session ID | | |
619 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
620 | * | Cookie (optional, maximum 64 bits)... | |
621 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
622 | * | | |
623 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
624 | * | |
625 | * L2TPv3 L2-Specific Sublayer Format | |
626 | * | |
627 | * 0 1 2 3 | |
628 | * 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 | |
629 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
630 | * |x|S|x|x|x|x|x|x| Sequence Number | | |
631 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
632 | * | |
23fe846f GN |
633 | * Cookie value and sublayer format are negotiated with the peer when |
634 | * the session is set up. Unlike L2TPv2, we do not need to parse the | |
635 | * packet header to determine if optional fields are present. | |
f7faffa3 JC |
636 | * |
637 | * Caller must already have parsed the frame and determined that it is | |
638 | * a data (not control) frame before coming here. Fields up to the | |
639 | * session-id have already been parsed and ptr points to the data | |
640 | * after the session-id. | |
fd558d18 | 641 | */ |
f7faffa3 JC |
642 | void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, |
643 | unsigned char *ptr, unsigned char *optr, u16 hdrflags, | |
2b139e6b | 644 | int length) |
fd558d18 | 645 | { |
f7faffa3 | 646 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 647 | int offset; |
fd558d18 | 648 | |
f7faffa3 JC |
649 | /* Parse and check optional cookie */ |
650 | if (session->peer_cookie_len > 0) { | |
651 | if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { | |
238936a7 MS |
652 | pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n", |
653 | tunnel->name, tunnel->tunnel_id, | |
654 | session->session_id); | |
7b7c0719 | 655 | atomic_long_inc(&session->stats.rx_cookie_discards); |
f7faffa3 JC |
656 | goto discard; |
657 | } | |
658 | ptr += session->peer_cookie_len; | |
659 | } | |
660 | ||
fd558d18 JC |
661 | /* Handle the optional sequence numbers. Sequence numbers are |
662 | * in different places for L2TPv2 and L2TPv3. | |
663 | * | |
664 | * If we are the LAC, enable/disable sequence numbers under | |
665 | * the control of the LNS. If no sequence numbers present but | |
666 | * we were expecting them, discard frame. | |
667 | */ | |
fd558d18 | 668 | L2TP_SKB_CB(skb)->has_seq = 0; |
f7faffa3 JC |
669 | if (tunnel->version == L2TP_HDR_VER_2) { |
670 | if (hdrflags & L2TP_HDRFLAG_S) { | |
f7faffa3 | 671 | /* Store L2TP info in the skb */ |
12923365 | 672 | L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr); |
f7faffa3 | 673 | L2TP_SKB_CB(skb)->has_seq = 1; |
12923365 TP |
674 | ptr += 2; |
675 | /* Skip past nr in the header */ | |
676 | ptr += 2; | |
fd558d18 | 677 | |
f7faffa3 JC |
678 | } |
679 | } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { | |
b71a61cc | 680 | u32 l2h = ntohl(*(__be32 *)ptr); |
f7faffa3 JC |
681 | |
682 | if (l2h & 0x40000000) { | |
f7faffa3 | 683 | /* Store L2TP info in the skb */ |
12923365 | 684 | L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff; |
f7faffa3 | 685 | L2TP_SKB_CB(skb)->has_seq = 1; |
f7faffa3 | 686 | } |
62e7b6a5 | 687 | ptr += 4; |
fd558d18 JC |
688 | } |
689 | ||
690 | if (L2TP_SKB_CB(skb)->has_seq) { | |
20dcb110 | 691 | /* Received a packet with sequence numbers. If we're the LAC, |
fd558d18 JC |
692 | * check if we sre sending sequence numbers and if not, |
693 | * configure it so. | |
694 | */ | |
6c0ec37b | 695 | if (!session->lns_mode && !session->send_seq) { |
6b7bdcd7 | 696 | trace_session_seqnum_lns_enable(session); |
3f9b9770 | 697 | session->send_seq = 1; |
f7faffa3 | 698 | l2tp_session_set_header_len(session, tunnel->version); |
fd558d18 JC |
699 | } |
700 | } else { | |
701 | /* No sequence numbers. | |
702 | * If user has configured mandatory sequence numbers, discard. | |
703 | */ | |
704 | if (session->recv_seq) { | |
238936a7 MS |
705 | pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n", |
706 | session->name); | |
7b7c0719 | 707 | atomic_long_inc(&session->stats.rx_seq_discards); |
fd558d18 JC |
708 | goto discard; |
709 | } | |
710 | ||
711 | /* If we're the LAC and we're sending sequence numbers, the | |
712 | * LNS has requested that we no longer send sequence numbers. | |
713 | * If we're the LNS and we're sending sequence numbers, the | |
714 | * LAC is broken. Discard the frame. | |
715 | */ | |
6c0ec37b | 716 | if (!session->lns_mode && session->send_seq) { |
6b7bdcd7 | 717 | trace_session_seqnum_lns_disable(session); |
fd558d18 | 718 | session->send_seq = 0; |
f7faffa3 | 719 | l2tp_session_set_header_len(session, tunnel->version); |
fd558d18 | 720 | } else if (session->send_seq) { |
238936a7 MS |
721 | pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n", |
722 | session->name); | |
7b7c0719 | 723 | atomic_long_inc(&session->stats.rx_seq_discards); |
fd558d18 JC |
724 | goto discard; |
725 | } | |
726 | } | |
727 | ||
900631ee JC |
728 | /* Session data offset is defined only for L2TPv2 and is |
729 | * indicated by an optional 16-bit value in the header. | |
f7faffa3 JC |
730 | */ |
731 | if (tunnel->version == L2TP_HDR_VER_2) { | |
732 | /* If offset bit set, skip it. */ | |
733 | if (hdrflags & L2TP_HDRFLAG_O) { | |
734 | offset = ntohs(*(__be16 *)ptr); | |
735 | ptr += 2 + offset; | |
736 | } | |
900631ee | 737 | } |
fd558d18 JC |
738 | |
739 | offset = ptr - optr; | |
740 | if (!pskb_may_pull(skb, offset)) | |
741 | goto discard; | |
742 | ||
743 | __skb_pull(skb, offset); | |
744 | ||
fd558d18 JC |
745 | /* Prepare skb for adding to the session's reorder_q. Hold |
746 | * packets for max reorder_timeout or 1 second if not | |
747 | * reordering. | |
748 | */ | |
749 | L2TP_SKB_CB(skb)->length = length; | |
750 | L2TP_SKB_CB(skb)->expires = jiffies + | |
751 | (session->reorder_timeout ? session->reorder_timeout : HZ); | |
752 | ||
753 | /* Add packet to the session's receive queue. Reordering is done here, if | |
754 | * enabled. Saved L2TP protocol info is stored in skb->sb[]. | |
755 | */ | |
756 | if (L2TP_SKB_CB(skb)->has_seq) { | |
b6dc01a4 JC |
757 | if (l2tp_recv_data_seq(session, skb)) |
758 | goto discard; | |
fd558d18 JC |
759 | } else { |
760 | /* No sequence numbers. Add the skb to the tail of the | |
761 | * reorder queue. This ensures that it will be | |
762 | * delivered after all previous sequenced skbs. | |
763 | */ | |
764 | skb_queue_tail(&session->reorder_q, skb); | |
765 | } | |
766 | ||
767 | /* Try to dequeue as many skbs from reorder_q as we can. */ | |
768 | l2tp_recv_dequeue(session); | |
769 | ||
f7faffa3 | 770 | return; |
fd558d18 JC |
771 | |
772 | discard: | |
7b7c0719 | 773 | atomic_long_inc(&session->stats.rx_errors); |
fd558d18 | 774 | kfree_skb(skb); |
f7faffa3 | 775 | } |
ca7885db | 776 | EXPORT_SYMBOL_GPL(l2tp_recv_common); |
f7faffa3 | 777 | |
48f72f92 TP |
778 | /* Drop skbs from the session's reorder_q |
779 | */ | |
493048f5 | 780 | static void l2tp_session_queue_purge(struct l2tp_session *session) |
48f72f92 TP |
781 | { |
782 | struct sk_buff *skb = NULL; | |
b71a61cc | 783 | |
48f72f92 TP |
784 | while ((skb = skb_dequeue(&session->reorder_q))) { |
785 | atomic_long_inc(&session->stats.rx_errors); | |
786 | kfree_skb(skb); | |
48f72f92 | 787 | } |
48f72f92 | 788 | } |
48f72f92 | 789 | |
f7faffa3 JC |
790 | /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame |
791 | * here. The skb is not on a list when we get here. | |
792 | * Returns 0 if the packet was a data packet and was successfully passed on. | |
793 | * Returns 1 if the packet was not a good data packet and could not be | |
794 | * forwarded. All such packets are passed up to userspace to deal with. | |
795 | */ | |
2b139e6b | 796 | static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb) |
f7faffa3 JC |
797 | { |
798 | struct l2tp_session *session = NULL; | |
799 | unsigned char *ptr, *optr; | |
800 | u16 hdrflags; | |
801 | u32 tunnel_id, session_id; | |
f7faffa3 JC |
802 | u16 version; |
803 | int length; | |
804 | ||
58d6085c | 805 | /* UDP has verifed checksum */ |
f7faffa3 JC |
806 | |
807 | /* UDP always verifies the packet length. */ | |
808 | __skb_pull(skb, sizeof(struct udphdr)); | |
809 | ||
810 | /* Short packet? */ | |
91c52470 | 811 | if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) { |
238936a7 MS |
812 | pr_debug_ratelimited("%s: recv short packet (len=%d)\n", |
813 | tunnel->name, skb->len); | |
814 | goto invalid; | |
f7faffa3 JC |
815 | } |
816 | ||
e50e705c | 817 | /* Point to L2TP header */ |
95075150 TP |
818 | optr = skb->data; |
819 | ptr = skb->data; | |
e50e705c | 820 | |
f7faffa3 | 821 | /* Get L2TP header flags */ |
b71a61cc | 822 | hdrflags = ntohs(*(__be16 *)ptr); |
f7faffa3 JC |
823 | |
824 | /* Check protocol version */ | |
825 | version = hdrflags & L2TP_HDR_VER_MASK; | |
826 | if (version != tunnel->version) { | |
238936a7 MS |
827 | pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n", |
828 | tunnel->name, version, tunnel->version); | |
829 | goto invalid; | |
f7faffa3 JC |
830 | } |
831 | ||
832 | /* Get length of L2TP packet */ | |
833 | length = skb->len; | |
834 | ||
835 | /* If type is control packet, it is handled by userspace. */ | |
12923365 | 836 | if (hdrflags & L2TP_HDRFLAG_T) |
238936a7 | 837 | goto pass; |
f7faffa3 JC |
838 | |
839 | /* Skip flags */ | |
840 | ptr += 2; | |
841 | ||
842 | if (tunnel->version == L2TP_HDR_VER_2) { | |
843 | /* If length is present, skip it */ | |
844 | if (hdrflags & L2TP_HDRFLAG_L) | |
845 | ptr += 2; | |
846 | ||
847 | /* Extract tunnel and session ID */ | |
b71a61cc | 848 | tunnel_id = ntohs(*(__be16 *)ptr); |
f7faffa3 | 849 | ptr += 2; |
b71a61cc | 850 | session_id = ntohs(*(__be16 *)ptr); |
f7faffa3 JC |
851 | ptr += 2; |
852 | } else { | |
853 | ptr += 2; /* skip reserved bits */ | |
854 | tunnel_id = tunnel->tunnel_id; | |
b71a61cc | 855 | session_id = ntohl(*(__be32 *)ptr); |
f7faffa3 JC |
856 | ptr += 4; |
857 | } | |
858 | ||
859 | /* Find the session context */ | |
01e28b92 | 860 | session = l2tp_tunnel_get_session(tunnel, session_id); |
309795f4 | 861 | if (!session || !session->recv_skb) { |
a4346210 | 862 | if (session) |
61b9a047 | 863 | l2tp_session_dec_refcount(session); |
61b9a047 | 864 | |
f7faffa3 | 865 | /* Not found? Pass to userspace to deal with */ |
238936a7 MS |
866 | pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n", |
867 | tunnel->name, tunnel_id, session_id); | |
868 | goto pass; | |
f7faffa3 JC |
869 | } |
870 | ||
4522a70d JW |
871 | if (tunnel->version == L2TP_HDR_VER_3 && |
872 | l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) | |
238936a7 | 873 | goto invalid; |
4522a70d | 874 | |
2b139e6b | 875 | l2tp_recv_common(session, skb, ptr, optr, hdrflags, length); |
61b9a047 | 876 | l2tp_session_dec_refcount(session); |
fd558d18 JC |
877 | |
878 | return 0; | |
879 | ||
238936a7 MS |
880 | invalid: |
881 | atomic_long_inc(&tunnel->stats.rx_invalid); | |
882 | ||
883 | pass: | |
fd558d18 JC |
884 | /* Put UDP header back */ |
885 | __skb_push(skb, sizeof(struct udphdr)); | |
886 | ||
887 | return 1; | |
888 | } | |
fd558d18 JC |
889 | |
890 | /* UDP encapsulation receive handler. See net/ipv4/udp.c. | |
891 | * Return codes: | |
892 | * 0 : success. | |
893 | * <0: error | |
894 | * >0: skb should be passed up to userspace as UDP. | |
895 | */ | |
896 | int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
897 | { | |
898 | struct l2tp_tunnel *tunnel; | |
899 | ||
45faeff1 TP |
900 | /* Note that this is called from the encap_rcv hook inside an |
901 | * RCU-protected region, but without the socket being locked. | |
902 | * Hence we use rcu_dereference_sk_user_data to access the | |
903 | * tunnel data structure rather the usual l2tp_sk_to_tunnel | |
904 | * accessor function. | |
905 | */ | |
c1c47721 | 906 | tunnel = rcu_dereference_sk_user_data(sk); |
0febc7b3 | 907 | if (!tunnel) |
fd558d18 | 908 | goto pass_up; |
45faeff1 TP |
909 | if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC)) |
910 | goto pass_up; | |
fd558d18 | 911 | |
2b139e6b | 912 | if (l2tp_udp_recv_core(tunnel, skb)) |
d00fa9ad | 913 | goto pass_up; |
fd558d18 | 914 | |
fd558d18 JC |
915 | return 0; |
916 | ||
fd558d18 JC |
917 | pass_up: |
918 | return 1; | |
919 | } | |
920 | EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); | |
921 | ||
922 | /************************************************************************ | |
923 | * Transmit handling | |
924 | ***********************************************************************/ | |
925 | ||
926 | /* Build an L2TP header for the session into the buffer provided. | |
927 | */ | |
f7faffa3 | 928 | static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) |
fd558d18 | 929 | { |
f7faffa3 | 930 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 931 | __be16 *bufp = buf; |
f7faffa3 | 932 | __be16 *optr = buf; |
fd558d18 JC |
933 | u16 flags = L2TP_HDR_VER_2; |
934 | u32 tunnel_id = tunnel->peer_tunnel_id; | |
935 | u32 session_id = session->peer_session_id; | |
936 | ||
937 | if (session->send_seq) | |
938 | flags |= L2TP_HDRFLAG_S; | |
939 | ||
940 | /* Setup L2TP header. */ | |
941 | *bufp++ = htons(flags); | |
942 | *bufp++ = htons(tunnel_id); | |
943 | *bufp++ = htons(session_id); | |
944 | if (session->send_seq) { | |
945 | *bufp++ = htons(session->ns); | |
946 | *bufp++ = 0; | |
947 | session->ns++; | |
f7faffa3 | 948 | session->ns &= 0xffff; |
6b7bdcd7 | 949 | trace_session_seqnum_update(session); |
fd558d18 | 950 | } |
f7faffa3 JC |
951 | |
952 | return bufp - optr; | |
fd558d18 JC |
953 | } |
954 | ||
f7faffa3 | 955 | static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) |
fd558d18 | 956 | { |
0d76751f | 957 | struct l2tp_tunnel *tunnel = session->tunnel; |
f7faffa3 JC |
958 | char *bufp = buf; |
959 | char *optr = bufp; | |
f7faffa3 | 960 | |
0d76751f JC |
961 | /* Setup L2TP header. The header differs slightly for UDP and |
962 | * IP encapsulations. For UDP, there is 4 bytes of flags. | |
963 | */ | |
964 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { | |
965 | u16 flags = L2TP_HDR_VER_3; | |
b71a61cc | 966 | *((__be16 *)bufp) = htons(flags); |
0d76751f | 967 | bufp += 2; |
b71a61cc | 968 | *((__be16 *)bufp) = 0; |
0d76751f JC |
969 | bufp += 2; |
970 | } | |
971 | ||
b71a61cc | 972 | *((__be32 *)bufp) = htonl(session->peer_session_id); |
f7faffa3 JC |
973 | bufp += 4; |
974 | if (session->cookie_len) { | |
975 | memcpy(bufp, &session->cookie[0], session->cookie_len); | |
976 | bufp += session->cookie_len; | |
977 | } | |
62e7b6a5 LB |
978 | if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { |
979 | u32 l2h = 0; | |
f7faffa3 | 980 | |
62e7b6a5 LB |
981 | if (session->send_seq) { |
982 | l2h = 0x40000000 | session->ns; | |
983 | session->ns++; | |
984 | session->ns &= 0xffffff; | |
6b7bdcd7 | 985 | trace_session_seqnum_update(session); |
f7faffa3 | 986 | } |
62e7b6a5 LB |
987 | |
988 | *((__be32 *)bufp) = htonl(l2h); | |
989 | bufp += 4; | |
f7faffa3 | 990 | } |
fd558d18 | 991 | |
f7faffa3 | 992 | return bufp - optr; |
fd558d18 | 993 | } |
fd558d18 | 994 | |
de68b039 TP |
995 | /* Queue the packet to IP for output: tunnel socket lock must be held */ |
996 | static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl) | |
fd558d18 | 997 | { |
de68b039 | 998 | int err; |
fd558d18 | 999 | |
60ff7467 | 1000 | skb->ignore_df = 1; |
27d53323 | 1001 | skb_dst_drop(skb); |
d2cf3361 | 1002 | #if IS_ENABLED(CONFIG_IPV6) |
b954f940 | 1003 | if (l2tp_sk_is_v6(tunnel->sock)) |
de68b039 | 1004 | err = inet6_csk_xmit(tunnel->sock, skb, NULL); |
d2cf3361 BL |
1005 | else |
1006 | #endif | |
de68b039 | 1007 | err = ip_queue_xmit(tunnel->sock, skb, fl); |
fd558d18 | 1008 | |
de68b039 | 1009 | return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP; |
fd558d18 | 1010 | } |
fd558d18 | 1011 | |
f52e4b27 | 1012 | static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len) |
fd558d18 | 1013 | { |
0d76751f | 1014 | struct l2tp_tunnel *tunnel = session->tunnel; |
de68b039 | 1015 | unsigned int data_len = skb->len; |
0d76751f | 1016 | struct sock *sk = tunnel->sock; |
de68b039 | 1017 | int headroom, uhlen, udp_len; |
b8c84307 | 1018 | int ret = NET_XMIT_SUCCESS; |
de68b039 TP |
1019 | struct inet_sock *inet; |
1020 | struct udphdr *uh; | |
fd558d18 JC |
1021 | |
1022 | /* Check that there's enough headroom in the skb to insert IP, | |
1023 | * UDP and L2TP headers. If not enough, expand it to | |
1024 | * make room. Adjust truesize. | |
1025 | */ | |
de68b039 TP |
1026 | uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0; |
1027 | headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len; | |
835acf5d | 1028 | if (skb_cow_head(skb, headroom)) { |
b8c84307 ED |
1029 | kfree_skb(skb); |
1030 | return NET_XMIT_DROP; | |
835acf5d | 1031 | } |
fd558d18 | 1032 | |
fd558d18 | 1033 | /* Setup L2TP header */ |
2dedab6f | 1034 | if (tunnel->version == L2TP_HDR_VER_2) |
efe05278 | 1035 | l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len)); |
2dedab6f | 1036 | else |
efe05278 | 1037 | l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len)); |
fd558d18 | 1038 | |
0d76751f | 1039 | /* Reset skb netfilter state */ |
fd558d18 | 1040 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
de68b039 | 1041 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED); |
895b5c9f | 1042 | nf_reset_ct(skb); |
fd558d18 | 1043 | |
6af88da1 DM |
1044 | bh_lock_sock(sk); |
1045 | if (sock_owned_by_user(sk)) { | |
b8c84307 ED |
1046 | kfree_skb(skb); |
1047 | ret = NET_XMIT_DROP; | |
6af88da1 DM |
1048 | goto out_unlock; |
1049 | } | |
1050 | ||
b954f940 PA |
1051 | /* The user-space may change the connection status for the user-space |
1052 | * provided socket at run time: we must check it under the socket lock | |
1053 | */ | |
1054 | if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) { | |
1055 | kfree_skb(skb); | |
1056 | ret = NET_XMIT_DROP; | |
1057 | goto out_unlock; | |
1058 | } | |
1059 | ||
f52e4b27 TP |
1060 | /* Report transmitted length before we add encap header, which keeps |
1061 | * statistics consistent for both UDP and IP encap tx/rx paths. | |
1062 | */ | |
1063 | *len = skb->len; | |
1064 | ||
d9d8da80 | 1065 | inet = inet_sk(sk); |
0d76751f JC |
1066 | switch (tunnel->encap) { |
1067 | case L2TP_ENCAPTYPE_UDP: | |
1068 | /* Setup UDP header */ | |
0d76751f JC |
1069 | __skb_push(skb, sizeof(*uh)); |
1070 | skb_reset_transport_header(skb); | |
1071 | uh = udp_hdr(skb); | |
1072 | uh->source = inet->inet_sport; | |
1073 | uh->dest = inet->inet_dport; | |
efe05278 | 1074 | udp_len = uhlen + session->hdr_len + data_len; |
0d76751f | 1075 | uh->len = htons(udp_len); |
0d76751f JC |
1076 | |
1077 | /* Calculate UDP checksum if configured to do so */ | |
d2cf3361 | 1078 | #if IS_ENABLED(CONFIG_IPV6) |
b954f940 | 1079 | if (l2tp_sk_is_v6(sk)) |
77157e19 TH |
1080 | udp6_set_csum(udp_get_no_check6_tx(sk), |
1081 | skb, &inet6_sk(sk)->saddr, | |
1082 | &sk->sk_v6_daddr, udp_len); | |
d2cf3361 BL |
1083 | else |
1084 | #endif | |
0864e331 TP |
1085 | udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, |
1086 | inet->inet_daddr, udp_len); | |
0d76751f JC |
1087 | break; |
1088 | ||
1089 | case L2TP_ENCAPTYPE_IP: | |
1090 | break; | |
fd558d18 JC |
1091 | } |
1092 | ||
de68b039 TP |
1093 | ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl); |
1094 | ||
6af88da1 DM |
1095 | out_unlock: |
1096 | bh_unlock_sock(sk); | |
fd558d18 | 1097 | |
b8c84307 | 1098 | return ret; |
fd558d18 | 1099 | } |
de68b039 TP |
1100 | |
1101 | /* If caller requires the skb to have a ppp header, the header must be | |
1102 | * inserted in the skb data before calling this function. | |
1103 | */ | |
1104 | int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb) | |
1105 | { | |
f52e4b27 | 1106 | unsigned int len = 0; |
de68b039 TP |
1107 | int ret; |
1108 | ||
f52e4b27 | 1109 | ret = l2tp_xmit_core(session, skb, &len); |
de68b039 TP |
1110 | if (ret == NET_XMIT_SUCCESS) { |
1111 | atomic_long_inc(&session->tunnel->stats.tx_packets); | |
1112 | atomic_long_add(len, &session->tunnel->stats.tx_bytes); | |
1113 | atomic_long_inc(&session->stats.tx_packets); | |
1114 | atomic_long_add(len, &session->stats.tx_bytes); | |
1115 | } else { | |
1116 | atomic_long_inc(&session->tunnel->stats.tx_errors); | |
1117 | atomic_long_inc(&session->stats.tx_errors); | |
1118 | } | |
1119 | return ret; | |
1120 | } | |
fd558d18 JC |
1121 | EXPORT_SYMBOL_GPL(l2tp_xmit_skb); |
1122 | ||
1123 | /***************************************************************************** | |
1124 | * Tinnel and session create/destroy. | |
1125 | *****************************************************************************/ | |
1126 | ||
1127 | /* Tunnel socket destruct hook. | |
1128 | * The tunnel context is deleted only when all session sockets have been | |
1129 | * closed. | |
1130 | */ | |
fc130840 | 1131 | static void l2tp_tunnel_destruct(struct sock *sk) |
fd558d18 | 1132 | { |
45faeff1 | 1133 | struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); |
fd558d18 | 1134 | |
0febc7b3 | 1135 | if (!tunnel) |
fd558d18 JC |
1136 | goto end; |
1137 | ||
f8ccac0e | 1138 | /* Disable udp encapsulation */ |
0d76751f JC |
1139 | switch (tunnel->encap) { |
1140 | case L2TP_ENCAPTYPE_UDP: | |
1141 | /* No longer an encapsulation socket. See net/ipv4/udp.c */ | |
1142 | (udp_sk(sk))->encap_type = 0; | |
1143 | (udp_sk(sk))->encap_rcv = NULL; | |
9980d001 | 1144 | (udp_sk(sk))->encap_destroy = NULL; |
0d76751f JC |
1145 | break; |
1146 | case L2TP_ENCAPTYPE_IP: | |
1147 | break; | |
1148 | } | |
fd558d18 JC |
1149 | |
1150 | /* Remove hooks into tunnel socket */ | |
fd558d18 JC |
1151 | sk->sk_destruct = tunnel->old_sk_destruct; |
1152 | sk->sk_user_data = NULL; | |
1153 | ||
f8ccac0e TP |
1154 | /* Call the original destructor */ |
1155 | if (sk->sk_destruct) | |
1156 | (*sk->sk_destruct)(sk); | |
d00fa9ad JC |
1157 | |
1158 | kfree_rcu(tunnel, rcu); | |
fd558d18 JC |
1159 | end: |
1160 | return; | |
1161 | } | |
fd558d18 | 1162 | |
b2aecfe8 TP |
1163 | /* Remove an l2tp session from l2tp_core's hash lists. */ |
1164 | static void l2tp_session_unhash(struct l2tp_session *session) | |
1165 | { | |
1166 | struct l2tp_tunnel *tunnel = session->tunnel; | |
1167 | ||
1168 | /* Remove the session from core hashes */ | |
1169 | if (tunnel) { | |
1170 | /* Remove from the per-tunnel hash */ | |
1171 | write_lock_bh(&tunnel->hlist_lock); | |
1172 | hlist_del_init(&session->hlist); | |
1173 | write_unlock_bh(&tunnel->hlist_lock); | |
1174 | ||
1175 | /* For L2TPv3 we have a per-net hash: remove from there, too */ | |
1176 | if (tunnel->version != L2TP_HDR_VER_2) { | |
1177 | struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); | |
1178 | ||
1179 | spin_lock_bh(&pn->l2tp_session_hlist_lock); | |
1180 | hlist_del_init_rcu(&session->global_hlist); | |
1181 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); | |
1182 | synchronize_rcu(); | |
1183 | } | |
1184 | } | |
1185 | } | |
1186 | ||
fd558d18 JC |
1187 | /* When the tunnel is closed, all the attached sessions need to go too. |
1188 | */ | |
d08532bb | 1189 | static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) |
fd558d18 JC |
1190 | { |
1191 | int hash; | |
1192 | struct hlist_node *walk; | |
1193 | struct hlist_node *tmp; | |
1194 | struct l2tp_session *session; | |
1195 | ||
fd558d18 | 1196 | write_lock_bh(&tunnel->hlist_lock); |
f3c66d4e | 1197 | tunnel->acpt_newsess = false; |
fd558d18 JC |
1198 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { |
1199 | again: | |
1200 | hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { | |
1201 | session = hlist_entry(walk, struct l2tp_session, hlist); | |
fd558d18 JC |
1202 | hlist_del_init(&session->hlist); |
1203 | ||
fd558d18 | 1204 | write_unlock_bh(&tunnel->hlist_lock); |
9d319a8e | 1205 | l2tp_session_delete(session); |
fd558d18 JC |
1206 | write_lock_bh(&tunnel->hlist_lock); |
1207 | ||
1208 | /* Now restart from the beginning of this hash | |
1209 | * chain. We always remove a session from the | |
1210 | * list so we are guaranteed to make forward | |
1211 | * progress. | |
1212 | */ | |
1213 | goto again; | |
1214 | } | |
1215 | } | |
1216 | write_unlock_bh(&tunnel->hlist_lock); | |
1217 | } | |
fd558d18 | 1218 | |
9980d001 TP |
1219 | /* Tunnel socket destroy hook for UDP encapsulation */ |
1220 | static void l2tp_udp_encap_destroy(struct sock *sk) | |
1221 | { | |
45faeff1 | 1222 | struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); |
d00fa9ad JC |
1223 | |
1224 | if (tunnel) | |
1225 | l2tp_tunnel_delete(tunnel); | |
9980d001 TP |
1226 | } |
1227 | ||
f8ccac0e TP |
1228 | /* Workqueue tunnel deletion function */ |
1229 | static void l2tp_tunnel_del_work(struct work_struct *work) | |
1230 | { | |
d00fa9ad JC |
1231 | struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel, |
1232 | del_work); | |
1233 | struct sock *sk = tunnel->sock; | |
1234 | struct socket *sock = sk->sk_socket; | |
28f5bfb8 | 1235 | struct l2tp_net *pn; |
12d656af RK |
1236 | |
1237 | l2tp_tunnel_closeall(tunnel); | |
1238 | ||
76a6abdb | 1239 | /* If the tunnel socket was created within the kernel, use |
02d13ed5 | 1240 | * the sk API to release it here. |
f8ccac0e | 1241 | */ |
76a6abdb | 1242 | if (tunnel->fd < 0) { |
26abe143 | 1243 | if (sock) { |
02d13ed5 | 1244 | kernel_sock_shutdown(sock, SHUT_RDWR); |
26abe143 EB |
1245 | sock_release(sock); |
1246 | } | |
167eb17e | 1247 | } |
f8ccac0e | 1248 | |
28f5bfb8 JC |
1249 | /* Remove the tunnel struct from the tunnel list */ |
1250 | pn = l2tp_pernet(tunnel->l2tp_net); | |
1251 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); | |
1252 | list_del_rcu(&tunnel->list); | |
1253 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1254 | ||
d00fa9ad JC |
1255 | /* drop initial ref */ |
1256 | l2tp_tunnel_dec_refcount(tunnel); | |
1257 | ||
1258 | /* drop workqueue ref */ | |
06a15f51 | 1259 | l2tp_tunnel_dec_refcount(tunnel); |
fd558d18 | 1260 | } |
fd558d18 | 1261 | |
789a4a2c JC |
1262 | /* Create a socket for the tunnel, if one isn't set up by |
1263 | * userspace. This is used for static tunnels where there is no | |
1264 | * managing L2TP daemon. | |
167eb17e TP |
1265 | * |
1266 | * Since we don't want these sockets to keep a namespace alive by | |
1267 | * themselves, we drop the socket's namespace refcount after creation. | |
1268 | * These sockets are freed when the namespace exits using the pernet | |
1269 | * exit hook. | |
789a4a2c | 1270 | */ |
167eb17e | 1271 | static int l2tp_tunnel_sock_create(struct net *net, |
8ce9825a TP |
1272 | u32 tunnel_id, |
1273 | u32 peer_tunnel_id, | |
1274 | struct l2tp_tunnel_cfg *cfg, | |
1275 | struct socket **sockp) | |
789a4a2c JC |
1276 | { |
1277 | int err = -EINVAL; | |
167eb17e | 1278 | struct socket *sock = NULL; |
85644b4d | 1279 | struct udp_port_cfg udp_conf; |
789a4a2c JC |
1280 | |
1281 | switch (cfg->encap) { | |
1282 | case L2TP_ENCAPTYPE_UDP: | |
85644b4d TH |
1283 | memset(&udp_conf, 0, sizeof(udp_conf)); |
1284 | ||
f9bac8df CE |
1285 | #if IS_ENABLED(CONFIG_IPV6) |
1286 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
85644b4d TH |
1287 | udp_conf.family = AF_INET6; |
1288 | memcpy(&udp_conf.local_ip6, cfg->local_ip6, | |
1289 | sizeof(udp_conf.local_ip6)); | |
1290 | memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, | |
1291 | sizeof(udp_conf.peer_ip6)); | |
1292 | udp_conf.use_udp6_tx_checksums = | |
b71a61cc | 1293 | !cfg->udp6_zero_tx_checksums; |
85644b4d | 1294 | udp_conf.use_udp6_rx_checksums = |
b71a61cc | 1295 | !cfg->udp6_zero_rx_checksums; |
f9bac8df CE |
1296 | } else |
1297 | #endif | |
1298 | { | |
85644b4d TH |
1299 | udp_conf.family = AF_INET; |
1300 | udp_conf.local_ip = cfg->local_ip; | |
1301 | udp_conf.peer_ip = cfg->peer_ip; | |
1302 | udp_conf.use_udp_checksums = cfg->use_udp_checksums; | |
f9bac8df | 1303 | } |
789a4a2c | 1304 | |
85644b4d TH |
1305 | udp_conf.local_udp_port = htons(cfg->local_udp_port); |
1306 | udp_conf.peer_udp_port = htons(cfg->peer_udp_port); | |
1307 | ||
1308 | err = udp_sock_create(net, &udp_conf, &sock); | |
1309 | if (err < 0) | |
1310 | goto out; | |
789a4a2c JC |
1311 | |
1312 | break; | |
1313 | ||
1314 | case L2TP_ENCAPTYPE_IP: | |
f9bac8df CE |
1315 | #if IS_ENABLED(CONFIG_IPV6) |
1316 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
85644b4d TH |
1317 | struct sockaddr_l2tpip6 ip6_addr = {0}; |
1318 | ||
26abe143 | 1319 | err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, |
8ce9825a | 1320 | IPPROTO_L2TP, &sock); |
5dac94e1 JC |
1321 | if (err < 0) |
1322 | goto out; | |
789a4a2c | 1323 | |
5dac94e1 JC |
1324 | ip6_addr.l2tp_family = AF_INET6; |
1325 | memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, | |
1326 | sizeof(ip6_addr.l2tp_addr)); | |
1327 | ip6_addr.l2tp_conn_id = tunnel_id; | |
b71a61cc | 1328 | err = kernel_bind(sock, (struct sockaddr *)&ip6_addr, |
5dac94e1 JC |
1329 | sizeof(ip6_addr)); |
1330 | if (err < 0) | |
1331 | goto out; | |
789a4a2c | 1332 | |
5dac94e1 JC |
1333 | ip6_addr.l2tp_family = AF_INET6; |
1334 | memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, | |
1335 | sizeof(ip6_addr.l2tp_addr)); | |
1336 | ip6_addr.l2tp_conn_id = peer_tunnel_id; | |
1337 | err = kernel_connect(sock, | |
b71a61cc | 1338 | (struct sockaddr *)&ip6_addr, |
5dac94e1 JC |
1339 | sizeof(ip6_addr), 0); |
1340 | if (err < 0) | |
1341 | goto out; | |
1342 | } else | |
1343 | #endif | |
1344 | { | |
85644b4d TH |
1345 | struct sockaddr_l2tpip ip_addr = {0}; |
1346 | ||
26abe143 | 1347 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, |
8ce9825a | 1348 | IPPROTO_L2TP, &sock); |
5dac94e1 JC |
1349 | if (err < 0) |
1350 | goto out; | |
789a4a2c | 1351 | |
5dac94e1 JC |
1352 | ip_addr.l2tp_family = AF_INET; |
1353 | ip_addr.l2tp_addr = cfg->local_ip; | |
1354 | ip_addr.l2tp_conn_id = tunnel_id; | |
b71a61cc | 1355 | err = kernel_bind(sock, (struct sockaddr *)&ip_addr, |
5dac94e1 JC |
1356 | sizeof(ip_addr)); |
1357 | if (err < 0) | |
1358 | goto out; | |
1359 | ||
1360 | ip_addr.l2tp_family = AF_INET; | |
1361 | ip_addr.l2tp_addr = cfg->peer_ip; | |
1362 | ip_addr.l2tp_conn_id = peer_tunnel_id; | |
b71a61cc | 1363 | err = kernel_connect(sock, (struct sockaddr *)&ip_addr, |
5dac94e1 JC |
1364 | sizeof(ip_addr), 0); |
1365 | if (err < 0) | |
1366 | goto out; | |
1367 | } | |
789a4a2c JC |
1368 | break; |
1369 | ||
1370 | default: | |
1371 | goto out; | |
1372 | } | |
1373 | ||
1374 | out: | |
167eb17e | 1375 | *sockp = sock; |
6c0ec37b | 1376 | if (err < 0 && sock) { |
167eb17e | 1377 | kernel_sock_shutdown(sock, SHUT_RDWR); |
26abe143 | 1378 | sock_release(sock); |
789a4a2c JC |
1379 | *sockp = NULL; |
1380 | } | |
1381 | ||
1382 | return err; | |
1383 | } | |
1384 | ||
37159ef2 ED |
1385 | static struct lock_class_key l2tp_socket_class; |
1386 | ||
c9ccd4c6 | 1387 | int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, |
c0235fb3 | 1388 | struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) |
fd558d18 JC |
1389 | { |
1390 | struct l2tp_tunnel *tunnel = NULL; | |
1391 | int err; | |
0d76751f | 1392 | enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; |
fd558d18 | 1393 | |
0febc7b3 | 1394 | if (cfg) |
0d76751f JC |
1395 | encap = cfg->encap; |
1396 | ||
70c05bfa | 1397 | tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); |
0febc7b3 | 1398 | if (!tunnel) { |
fd558d18 JC |
1399 | err = -ENOMEM; |
1400 | goto err; | |
1401 | } | |
1402 | ||
1403 | tunnel->version = version; | |
1404 | tunnel->tunnel_id = tunnel_id; | |
1405 | tunnel->peer_tunnel_id = peer_tunnel_id; | |
fd558d18 JC |
1406 | |
1407 | tunnel->magic = L2TP_TUNNEL_MAGIC; | |
1408 | sprintf(&tunnel->name[0], "tunl %u", tunnel_id); | |
1409 | rwlock_init(&tunnel->hlist_lock); | |
f3c66d4e | 1410 | tunnel->acpt_newsess = true; |
fd558d18 | 1411 | |
0d76751f | 1412 | tunnel->encap = encap; |
fd558d18 | 1413 | |
d00fa9ad | 1414 | refcount_set(&tunnel->ref_count, 1); |
d00fa9ad JC |
1415 | tunnel->fd = fd; |
1416 | ||
f8ccac0e TP |
1417 | /* Init delete workqueue struct */ |
1418 | INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); | |
1419 | ||
fd558d18 | 1420 | INIT_LIST_HEAD(&tunnel->list); |
fd558d18 JC |
1421 | |
1422 | err = 0; | |
1423 | err: | |
1424 | if (tunnelp) | |
1425 | *tunnelp = tunnel; | |
1426 | ||
fd558d18 JC |
1427 | return err; |
1428 | } | |
1429 | EXPORT_SYMBOL_GPL(l2tp_tunnel_create); | |
1430 | ||
6b9f3423 GN |
1431 | static int l2tp_validate_socket(const struct sock *sk, const struct net *net, |
1432 | enum l2tp_encap_type encap) | |
1433 | { | |
1434 | if (!net_eq(sock_net(sk), net)) | |
1435 | return -EINVAL; | |
1436 | ||
1437 | if (sk->sk_type != SOCK_DGRAM) | |
1438 | return -EPROTONOSUPPORT; | |
1439 | ||
d9a81a22 ED |
1440 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) |
1441 | return -EPROTONOSUPPORT; | |
1442 | ||
6b9f3423 GN |
1443 | if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) || |
1444 | (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) | |
1445 | return -EPROTONOSUPPORT; | |
1446 | ||
1447 | if (sk->sk_user_data) | |
1448 | return -EBUSY; | |
1449 | ||
1450 | return 0; | |
1451 | } | |
1452 | ||
1453 | int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, | |
1454 | struct l2tp_tunnel_cfg *cfg) | |
1455 | { | |
f6cd651b | 1456 | struct l2tp_tunnel *tunnel_walk; |
6b9f3423 GN |
1457 | struct l2tp_net *pn; |
1458 | struct socket *sock; | |
1459 | struct sock *sk; | |
1460 | int ret; | |
1461 | ||
1462 | if (tunnel->fd < 0) { | |
1463 | ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id, | |
1464 | tunnel->peer_tunnel_id, cfg, | |
1465 | &sock); | |
1466 | if (ret < 0) | |
1467 | goto err; | |
1468 | } else { | |
1469 | sock = sockfd_lookup(tunnel->fd, &ret); | |
1470 | if (!sock) | |
1471 | goto err; | |
1472 | ||
1473 | ret = l2tp_validate_socket(sock->sk, net, tunnel->encap); | |
1474 | if (ret < 0) | |
1475 | goto err_sock; | |
1476 | } | |
1477 | ||
6b9f3423 | 1478 | tunnel->l2tp_net = net; |
6b9f3423 | 1479 | pn = l2tp_pernet(net); |
f6cd651b | 1480 | |
6b9f3423 | 1481 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); |
f6cd651b GN |
1482 | list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) { |
1483 | if (tunnel_walk->tunnel_id == tunnel->tunnel_id) { | |
1484 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1485 | ||
1486 | ret = -EEXIST; | |
1487 | goto err_sock; | |
1488 | } | |
1489 | } | |
6b9f3423 GN |
1490 | list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); |
1491 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1492 | ||
f8504f4c XL |
1493 | sk = sock->sk; |
1494 | sock_hold(sk); | |
1495 | tunnel->sock = sk; | |
1496 | ||
6b9f3423 GN |
1497 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { |
1498 | struct udp_tunnel_sock_cfg udp_cfg = { | |
1499 | .sk_user_data = tunnel, | |
1500 | .encap_type = UDP_ENCAP_L2TPINUDP, | |
1501 | .encap_rcv = l2tp_udp_encap_recv, | |
1502 | .encap_destroy = l2tp_udp_encap_destroy, | |
1503 | }; | |
1504 | ||
1505 | setup_udp_tunnel_sock(net, sock, &udp_cfg); | |
1506 | } else { | |
1507 | sk->sk_user_data = tunnel; | |
1508 | } | |
1509 | ||
1510 | tunnel->old_sk_destruct = sk->sk_destruct; | |
1511 | sk->sk_destruct = &l2tp_tunnel_destruct; | |
1512 | lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, | |
1513 | "l2tp_sock"); | |
1514 | sk->sk_allocation = GFP_ATOMIC; | |
1515 | ||
6b7bdcd7 TP |
1516 | trace_register_tunnel(tunnel); |
1517 | ||
6b9f3423 GN |
1518 | if (tunnel->fd >= 0) |
1519 | sockfd_put(sock); | |
1520 | ||
1521 | return 0; | |
1522 | ||
1523 | err_sock: | |
f6cd651b GN |
1524 | if (tunnel->fd < 0) |
1525 | sock_release(sock); | |
1526 | else | |
1527 | sockfd_put(sock); | |
6b9f3423 GN |
1528 | err: |
1529 | return ret; | |
1530 | } | |
1531 | EXPORT_SYMBOL_GPL(l2tp_tunnel_register); | |
1532 | ||
309795f4 JC |
1533 | /* This function is used by the netlink TUNNEL_DELETE command. |
1534 | */ | |
62b982ee | 1535 | void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) |
309795f4 | 1536 | { |
62b982ee | 1537 | if (!test_and_set_bit(0, &tunnel->dead)) { |
6b7bdcd7 | 1538 | trace_delete_tunnel(tunnel); |
62b982ee SD |
1539 | l2tp_tunnel_inc_refcount(tunnel); |
1540 | queue_work(l2tp_wq, &tunnel->del_work); | |
06a15f51 | 1541 | } |
309795f4 JC |
1542 | } |
1543 | EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); | |
1544 | ||
628703f5 | 1545 | void l2tp_session_delete(struct l2tp_session *session) |
309795f4 | 1546 | { |
b228a940 | 1547 | if (test_and_set_bit(0, &session->dead)) |
628703f5 | 1548 | return; |
b228a940 | 1549 | |
6b7bdcd7 | 1550 | trace_delete_session(session); |
b2aecfe8 | 1551 | l2tp_session_unhash(session); |
4c6e2fd3 | 1552 | l2tp_session_queue_purge(session); |
0febc7b3 | 1553 | if (session->session_close) |
309795f4 | 1554 | (*session->session_close)(session); |
a4346210 | 1555 | |
309795f4 | 1556 | l2tp_session_dec_refcount(session); |
309795f4 JC |
1557 | } |
1558 | EXPORT_SYMBOL_GPL(l2tp_session_delete); | |
1559 | ||
f7faffa3 | 1560 | /* We come here whenever a session's send_seq, cookie_len or |
62e7b6a5 | 1561 | * l2specific_type parameters are set. |
f7faffa3 | 1562 | */ |
bb5016ea | 1563 | void l2tp_session_set_header_len(struct l2tp_session *session, int version) |
f7faffa3 JC |
1564 | { |
1565 | if (version == L2TP_HDR_VER_2) { | |
1566 | session->hdr_len = 6; | |
1567 | if (session->send_seq) | |
1568 | session->hdr_len += 4; | |
1569 | } else { | |
62e7b6a5 LB |
1570 | session->hdr_len = 4 + session->cookie_len; |
1571 | session->hdr_len += l2tp_get_l2specific_len(session); | |
0d76751f JC |
1572 | if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) |
1573 | session->hdr_len += 4; | |
f7faffa3 | 1574 | } |
f7faffa3 | 1575 | } |
bb5016ea | 1576 | EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); |
f7faffa3 | 1577 | |
c0235fb3 TP |
1578 | struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, |
1579 | u32 peer_session_id, struct l2tp_session_cfg *cfg) | |
fd558d18 JC |
1580 | { |
1581 | struct l2tp_session *session; | |
1582 | ||
70c05bfa | 1583 | session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL); |
0febc7b3 | 1584 | if (session) { |
fd558d18 JC |
1585 | session->magic = L2TP_SESSION_MAGIC; |
1586 | session->tunnel = tunnel; | |
1587 | ||
1588 | session->session_id = session_id; | |
1589 | session->peer_session_id = peer_session_id; | |
d301e325 | 1590 | session->nr = 0; |
8a1631d5 JC |
1591 | if (tunnel->version == L2TP_HDR_VER_2) |
1592 | session->nr_max = 0xffff; | |
1593 | else | |
1594 | session->nr_max = 0xffffff; | |
1595 | session->nr_window_size = session->nr_max / 2; | |
a0dbd822 JC |
1596 | session->nr_oos_count_max = 4; |
1597 | ||
1598 | /* Use NR of first received packet */ | |
1599 | session->reorder_skip = 1; | |
fd558d18 JC |
1600 | |
1601 | sprintf(&session->name[0], "sess %u/%u", | |
1602 | tunnel->tunnel_id, session->session_id); | |
1603 | ||
1604 | skb_queue_head_init(&session->reorder_q); | |
1605 | ||
1606 | INIT_HLIST_NODE(&session->hlist); | |
f7faffa3 | 1607 | INIT_HLIST_NODE(&session->global_hlist); |
fd558d18 | 1608 | |
fd558d18 | 1609 | if (cfg) { |
f7faffa3 | 1610 | session->pwtype = cfg->pw_type; |
fd558d18 JC |
1611 | session->send_seq = cfg->send_seq; |
1612 | session->recv_seq = cfg->recv_seq; | |
1613 | session->lns_mode = cfg->lns_mode; | |
f7faffa3 | 1614 | session->reorder_timeout = cfg->reorder_timeout; |
f7faffa3 | 1615 | session->l2specific_type = cfg->l2specific_type; |
f7faffa3 JC |
1616 | session->cookie_len = cfg->cookie_len; |
1617 | memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); | |
1618 | session->peer_cookie_len = cfg->peer_cookie_len; | |
1619 | memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); | |
fd558d18 JC |
1620 | } |
1621 | ||
f7faffa3 JC |
1622 | l2tp_session_set_header_len(session, tunnel->version); |
1623 | ||
9ee369a4 GN |
1624 | refcount_set(&session->ref_count, 1); |
1625 | ||
dbdbc73b | 1626 | return session; |
fd558d18 JC |
1627 | } |
1628 | ||
dbdbc73b | 1629 | return ERR_PTR(-ENOMEM); |
fd558d18 JC |
1630 | } |
1631 | EXPORT_SYMBOL_GPL(l2tp_session_create); | |
1632 | ||
1633 | /***************************************************************************** | |
1634 | * Init and cleanup | |
1635 | *****************************************************************************/ | |
1636 | ||
1637 | static __net_init int l2tp_init_net(struct net *net) | |
1638 | { | |
e773aaff | 1639 | struct l2tp_net *pn = net_generic(net, l2tp_net_id); |
f7faffa3 | 1640 | int hash; |
fd558d18 | 1641 | |
fd558d18 | 1642 | INIT_LIST_HEAD(&pn->l2tp_tunnel_list); |
e02d494d | 1643 | spin_lock_init(&pn->l2tp_tunnel_list_lock); |
fd558d18 | 1644 | |
f7faffa3 JC |
1645 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) |
1646 | INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); | |
1647 | ||
e02d494d | 1648 | spin_lock_init(&pn->l2tp_session_hlist_lock); |
f7faffa3 | 1649 | |
fd558d18 | 1650 | return 0; |
fd558d18 JC |
1651 | } |
1652 | ||
167eb17e TP |
1653 | static __net_exit void l2tp_exit_net(struct net *net) |
1654 | { | |
1655 | struct l2tp_net *pn = l2tp_pernet(net); | |
1656 | struct l2tp_tunnel *tunnel = NULL; | |
1e7af3b2 | 1657 | int hash; |
167eb17e TP |
1658 | |
1659 | rcu_read_lock_bh(); | |
1660 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { | |
4dc12ffe | 1661 | l2tp_tunnel_delete(tunnel); |
167eb17e TP |
1662 | } |
1663 | rcu_read_unlock_bh(); | |
2f86953e | 1664 | |
638a3a1e Y |
1665 | if (l2tp_wq) |
1666 | flush_workqueue(l2tp_wq); | |
2f86953e | 1667 | rcu_barrier(); |
1e7af3b2 VA |
1668 | |
1669 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) | |
1670 | WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); | |
167eb17e TP |
1671 | } |
1672 | ||
fd558d18 JC |
1673 | static struct pernet_operations l2tp_net_ops = { |
1674 | .init = l2tp_init_net, | |
167eb17e | 1675 | .exit = l2tp_exit_net, |
fd558d18 JC |
1676 | .id = &l2tp_net_id, |
1677 | .size = sizeof(struct l2tp_net), | |
1678 | }; | |
1679 | ||
1680 | static int __init l2tp_init(void) | |
1681 | { | |
1682 | int rc = 0; | |
1683 | ||
1684 | rc = register_pernet_device(&l2tp_net_ops); | |
1685 | if (rc) | |
1686 | goto out; | |
1687 | ||
59ff3eb6 | 1688 | l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); |
f8ccac0e TP |
1689 | if (!l2tp_wq) { |
1690 | pr_err("alloc_workqueue failed\n"); | |
67e04c29 | 1691 | unregister_pernet_device(&l2tp_net_ops); |
f8ccac0e TP |
1692 | rc = -ENOMEM; |
1693 | goto out; | |
1694 | } | |
1695 | ||
a4ca44fa | 1696 | pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); |
fd558d18 JC |
1697 | |
1698 | out: | |
1699 | return rc; | |
1700 | } | |
1701 | ||
1702 | static void __exit l2tp_exit(void) | |
1703 | { | |
1704 | unregister_pernet_device(&l2tp_net_ops); | |
f8ccac0e TP |
1705 | if (l2tp_wq) { |
1706 | destroy_workqueue(l2tp_wq); | |
1707 | l2tp_wq = NULL; | |
1708 | } | |
fd558d18 JC |
1709 | } |
1710 | ||
1711 | module_init(l2tp_init); | |
1712 | module_exit(l2tp_exit); | |
1713 | ||
1714 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); | |
1715 | MODULE_DESCRIPTION("L2TP core"); | |
1716 | MODULE_LICENSE("GPL"); | |
1717 | MODULE_VERSION(L2TP_DRV_VERSION); |