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