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