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