]>
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 | |
dbdbc73b | 329 | hlist_for_each_entry(session_walk, g_head, global_hlist) |
f3c66d4e GN |
330 | if (session_walk->session_id == session->session_id) { |
331 | err = -EEXIST; | |
332 | goto err_tlock_pnlock; | |
333 | } | |
dbdbc73b | 334 | |
f3c66d4e | 335 | l2tp_tunnel_inc_refcount(tunnel); |
dbdbc73b | 336 | hlist_add_head_rcu(&session->global_hlist, g_head); |
f3c66d4e | 337 | |
dbdbc73b | 338 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
f3c66d4e GN |
339 | } else { |
340 | l2tp_tunnel_inc_refcount(tunnel); | |
dbdbc73b GN |
341 | } |
342 | ||
343 | hlist_add_head(&session->hlist, head); | |
344 | write_unlock_bh(&tunnel->hlist_lock); | |
345 | ||
346 | return 0; | |
347 | ||
f3c66d4e | 348 | err_tlock_pnlock: |
dbdbc73b | 349 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); |
f3c66d4e | 350 | err_tlock: |
dbdbc73b GN |
351 | write_unlock_bh(&tunnel->hlist_lock); |
352 | ||
f3c66d4e | 353 | return err; |
dbdbc73b | 354 | } |
3953ae7b | 355 | EXPORT_SYMBOL_GPL(l2tp_session_register); |
dbdbc73b | 356 | |
fd558d18 JC |
357 | /***************************************************************************** |
358 | * Receive data handling | |
359 | *****************************************************************************/ | |
360 | ||
361 | /* Queue a skb in order. We come here only if the skb has an L2TP sequence | |
362 | * number. | |
363 | */ | |
364 | static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) | |
365 | { | |
366 | struct sk_buff *skbp; | |
367 | struct sk_buff *tmp; | |
f7faffa3 | 368 | u32 ns = L2TP_SKB_CB(skb)->ns; |
fd558d18 JC |
369 | |
370 | spin_lock_bh(&session->reorder_q.lock); | |
371 | skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { | |
372 | if (L2TP_SKB_CB(skbp)->ns > ns) { | |
373 | __skb_queue_before(&session->reorder_q, skbp, skb); | |
a4ca44fa JP |
374 | l2tp_dbg(session, L2TP_MSG_SEQ, |
375 | "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", | |
376 | session->name, ns, L2TP_SKB_CB(skbp)->ns, | |
377 | skb_queue_len(&session->reorder_q)); | |
7b7c0719 | 378 | atomic_long_inc(&session->stats.rx_oos_packets); |
fd558d18 JC |
379 | goto out; |
380 | } | |
381 | } | |
382 | ||
383 | __skb_queue_tail(&session->reorder_q, skb); | |
384 | ||
385 | out: | |
386 | spin_unlock_bh(&session->reorder_q.lock); | |
387 | } | |
388 | ||
389 | /* Dequeue a single skb. | |
390 | */ | |
391 | static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) | |
392 | { | |
393 | struct l2tp_tunnel *tunnel = session->tunnel; | |
394 | int length = L2TP_SKB_CB(skb)->length; | |
395 | ||
396 | /* We're about to requeue the skb, so return resources | |
397 | * to its current owner (a socket receive buffer). | |
398 | */ | |
399 | skb_orphan(skb); | |
400 | ||
7b7c0719 TP |
401 | atomic_long_inc(&tunnel->stats.rx_packets); |
402 | atomic_long_add(length, &tunnel->stats.rx_bytes); | |
403 | atomic_long_inc(&session->stats.rx_packets); | |
404 | atomic_long_add(length, &session->stats.rx_bytes); | |
fd558d18 JC |
405 | |
406 | if (L2TP_SKB_CB(skb)->has_seq) { | |
407 | /* Bump our Nr */ | |
408 | session->nr++; | |
8a1631d5 | 409 | session->nr &= session->nr_max; |
f7faffa3 | 410 | |
a4ca44fa JP |
411 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", |
412 | session->name, session->nr); | |
fd558d18 JC |
413 | } |
414 | ||
415 | /* call private receive handler */ | |
416 | if (session->recv_skb != NULL) | |
417 | (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); | |
418 | else | |
419 | kfree_skb(skb); | |
fd558d18 JC |
420 | } |
421 | ||
422 | /* Dequeue skbs from the session's reorder_q, subject to packet order. | |
423 | * Skbs that have been in the queue for too long are simply discarded. | |
424 | */ | |
425 | static void l2tp_recv_dequeue(struct l2tp_session *session) | |
426 | { | |
427 | struct sk_buff *skb; | |
428 | struct sk_buff *tmp; | |
429 | ||
430 | /* If the pkt at the head of the queue has the nr that we | |
431 | * expect to send up next, dequeue it and any other | |
432 | * in-sequence packets behind it. | |
433 | */ | |
e2e210c0 | 434 | start: |
fd558d18 JC |
435 | spin_lock_bh(&session->reorder_q.lock); |
436 | skb_queue_walk_safe(&session->reorder_q, skb, tmp) { | |
437 | if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { | |
7b7c0719 TP |
438 | atomic_long_inc(&session->stats.rx_seq_discards); |
439 | atomic_long_inc(&session->stats.rx_errors); | |
a4ca44fa JP |
440 | l2tp_dbg(session, L2TP_MSG_SEQ, |
441 | "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", | |
442 | session->name, L2TP_SKB_CB(skb)->ns, | |
443 | L2TP_SKB_CB(skb)->length, session->nr, | |
444 | skb_queue_len(&session->reorder_q)); | |
38d40b3f | 445 | session->reorder_skip = 1; |
fd558d18 JC |
446 | __skb_unlink(skb, &session->reorder_q); |
447 | kfree_skb(skb); | |
fd558d18 JC |
448 | continue; |
449 | } | |
450 | ||
451 | if (L2TP_SKB_CB(skb)->has_seq) { | |
38d40b3f | 452 | if (session->reorder_skip) { |
a4ca44fa JP |
453 | l2tp_dbg(session, L2TP_MSG_SEQ, |
454 | "%s: advancing nr to next pkt: %u -> %u", | |
455 | session->name, session->nr, | |
456 | L2TP_SKB_CB(skb)->ns); | |
38d40b3f JC |
457 | session->reorder_skip = 0; |
458 | session->nr = L2TP_SKB_CB(skb)->ns; | |
459 | } | |
fd558d18 | 460 | if (L2TP_SKB_CB(skb)->ns != session->nr) { |
a4ca44fa JP |
461 | l2tp_dbg(session, L2TP_MSG_SEQ, |
462 | "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", | |
463 | session->name, L2TP_SKB_CB(skb)->ns, | |
464 | L2TP_SKB_CB(skb)->length, session->nr, | |
465 | skb_queue_len(&session->reorder_q)); | |
fd558d18 JC |
466 | goto out; |
467 | } | |
468 | } | |
469 | __skb_unlink(skb, &session->reorder_q); | |
470 | ||
471 | /* Process the skb. We release the queue lock while we | |
472 | * do so to let other contexts process the queue. | |
473 | */ | |
474 | spin_unlock_bh(&session->reorder_q.lock); | |
475 | l2tp_recv_dequeue_skb(session, skb); | |
e2e210c0 | 476 | goto start; |
fd558d18 JC |
477 | } |
478 | ||
479 | out: | |
480 | spin_unlock_bh(&session->reorder_q.lock); | |
481 | } | |
482 | ||
8a1631d5 JC |
483 | static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) |
484 | { | |
485 | u32 nws; | |
486 | ||
487 | if (nr >= session->nr) | |
488 | nws = nr - session->nr; | |
489 | else | |
490 | nws = (session->nr_max + 1) - (session->nr - nr); | |
491 | ||
492 | return nws < session->nr_window_size; | |
493 | } | |
494 | ||
b6dc01a4 JC |
495 | /* If packet has sequence numbers, queue it if acceptable. Returns 0 if |
496 | * acceptable, else non-zero. | |
497 | */ | |
498 | static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) | |
499 | { | |
8a1631d5 JC |
500 | if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { |
501 | /* Packet sequence number is outside allowed window. | |
502 | * Discard it. | |
503 | */ | |
504 | l2tp_dbg(session, L2TP_MSG_SEQ, | |
505 | "%s: pkt %u len %d discarded, outside window, nr=%u\n", | |
506 | session->name, L2TP_SKB_CB(skb)->ns, | |
507 | L2TP_SKB_CB(skb)->length, session->nr); | |
508 | goto discard; | |
509 | } | |
510 | ||
b6dc01a4 JC |
511 | if (session->reorder_timeout != 0) { |
512 | /* Packet reordering enabled. Add skb to session's | |
513 | * reorder queue, in order of ns. | |
514 | */ | |
515 | l2tp_recv_queue_skb(session, skb); | |
a0dbd822 JC |
516 | goto out; |
517 | } | |
518 | ||
519 | /* Packet reordering disabled. Discard out-of-sequence packets, while | |
520 | * tracking the number if in-sequence packets after the first OOS packet | |
521 | * is seen. After nr_oos_count_max in-sequence packets, reset the | |
522 | * sequence number to re-enable packet reception. | |
523 | */ | |
524 | if (L2TP_SKB_CB(skb)->ns == session->nr) { | |
525 | skb_queue_tail(&session->reorder_q, skb); | |
b6dc01a4 | 526 | } else { |
a0dbd822 JC |
527 | u32 nr_oos = L2TP_SKB_CB(skb)->ns; |
528 | u32 nr_next = (session->nr_oos + 1) & session->nr_max; | |
529 | ||
530 | if (nr_oos == nr_next) | |
531 | session->nr_oos_count++; | |
532 | else | |
533 | session->nr_oos_count = 0; | |
534 | ||
535 | session->nr_oos = nr_oos; | |
536 | if (session->nr_oos_count > session->nr_oos_count_max) { | |
537 | session->reorder_skip = 1; | |
538 | l2tp_dbg(session, L2TP_MSG_SEQ, | |
539 | "%s: %d oos packets received. Resetting sequence numbers\n", | |
540 | session->name, session->nr_oos_count); | |
541 | } | |
542 | if (!session->reorder_skip) { | |
b6dc01a4 JC |
543 | atomic_long_inc(&session->stats.rx_seq_discards); |
544 | l2tp_dbg(session, L2TP_MSG_SEQ, | |
545 | "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", | |
546 | session->name, L2TP_SKB_CB(skb)->ns, | |
547 | L2TP_SKB_CB(skb)->length, session->nr, | |
548 | skb_queue_len(&session->reorder_q)); | |
549 | goto discard; | |
550 | } | |
551 | skb_queue_tail(&session->reorder_q, skb); | |
552 | } | |
553 | ||
a0dbd822 | 554 | out: |
b6dc01a4 JC |
555 | return 0; |
556 | ||
557 | discard: | |
558 | return 1; | |
559 | } | |
560 | ||
f7faffa3 JC |
561 | /* Do receive processing of L2TP data frames. We handle both L2TPv2 |
562 | * and L2TPv3 data frames here. | |
563 | * | |
564 | * L2TPv2 Data Message Header | |
565 | * | |
566 | * 0 1 2 3 | |
567 | * 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 | |
568 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
569 | * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | | |
570 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
571 | * | Tunnel ID | Session ID | | |
572 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
573 | * | Ns (opt) | Nr (opt) | | |
574 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
575 | * | Offset Size (opt) | Offset pad... (opt) | |
576 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
577 | * | |
578 | * Data frames are marked by T=0. All other fields are the same as | |
579 | * those in L2TP control frames. | |
580 | * | |
581 | * L2TPv3 Data Message Header | |
582 | * | |
583 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
584 | * | L2TP Session Header | | |
585 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
586 | * | L2-Specific Sublayer | | |
587 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
588 | * | Tunnel Payload ... | |
589 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
590 | * | |
591 | * L2TPv3 Session Header Over IP | |
592 | * | |
593 | * 0 1 2 3 | |
594 | * 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 | |
595 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
596 | * | Session ID | | |
597 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
598 | * | Cookie (optional, maximum 64 bits)... | |
599 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
600 | * | | |
601 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
602 | * | |
603 | * L2TPv3 L2-Specific Sublayer Format | |
604 | * | |
605 | * 0 1 2 3 | |
606 | * 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 | |
607 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
608 | * |x|S|x|x|x|x|x|x| Sequence Number | | |
609 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
610 | * | |
611 | * Cookie value, sublayer format and offset (pad) are negotiated with | |
612 | * the peer when the session is set up. Unlike L2TPv2, we do not need | |
613 | * to parse the packet header to determine if optional fields are | |
614 | * present. | |
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 | } |
b25387e0 | 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 | ||
a2043df2 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 | } | |
a2043df2 | 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 | */ | |
786 | int l2tp_session_queue_purge(struct l2tp_session *session) | |
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 | } | |
797 | EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); | |
798 | ||
f7faffa3 JC |
799 | /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame |
800 | * here. The skb is not on a list when we get here. | |
801 | * Returns 0 if the packet was a data packet and was successfully passed on. | |
802 | * Returns 1 if the packet was not a good data packet and could not be | |
803 | * forwarded. All such packets are passed up to userspace to deal with. | |
804 | */ | |
fc130840 | 805 | static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, |
806 | int (*payload_hook)(struct sk_buff *skb)) | |
f7faffa3 JC |
807 | { |
808 | struct l2tp_session *session = NULL; | |
809 | unsigned char *ptr, *optr; | |
810 | u16 hdrflags; | |
811 | u32 tunnel_id, session_id; | |
f7faffa3 JC |
812 | u16 version; |
813 | int length; | |
814 | ||
58d6085c | 815 | /* UDP has verifed checksum */ |
f7faffa3 JC |
816 | |
817 | /* UDP always verifies the packet length. */ | |
818 | __skb_pull(skb, sizeof(struct udphdr)); | |
819 | ||
820 | /* Short packet? */ | |
af872de1 | 821 | if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) { |
a4ca44fa JP |
822 | l2tp_info(tunnel, L2TP_MSG_DATA, |
823 | "%s: recv short packet (len=%d)\n", | |
824 | tunnel->name, skb->len); | |
f7faffa3 JC |
825 | goto error; |
826 | } | |
827 | ||
f7faffa3 JC |
828 | /* Trace packet contents, if enabled */ |
829 | if (tunnel->debug & L2TP_MSG_DATA) { | |
830 | length = min(32u, skb->len); | |
831 | if (!pskb_may_pull(skb, length)) | |
832 | goto error; | |
833 | ||
a4ca44fa JP |
834 | pr_debug("%s: recv\n", tunnel->name); |
835 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); | |
f7faffa3 JC |
836 | } |
837 | ||
e50e705c ED |
838 | /* Point to L2TP header */ |
839 | optr = ptr = skb->data; | |
840 | ||
f7faffa3 JC |
841 | /* Get L2TP header flags */ |
842 | hdrflags = ntohs(*(__be16 *) ptr); | |
843 | ||
844 | /* Check protocol version */ | |
845 | version = hdrflags & L2TP_HDR_VER_MASK; | |
846 | if (version != tunnel->version) { | |
a4ca44fa JP |
847 | l2tp_info(tunnel, L2TP_MSG_DATA, |
848 | "%s: recv protocol version mismatch: got %d expected %d\n", | |
849 | tunnel->name, version, tunnel->version); | |
f7faffa3 JC |
850 | goto error; |
851 | } | |
852 | ||
853 | /* Get length of L2TP packet */ | |
854 | length = skb->len; | |
855 | ||
856 | /* If type is control packet, it is handled by userspace. */ | |
857 | if (hdrflags & L2TP_HDRFLAG_T) { | |
a4ca44fa JP |
858 | l2tp_dbg(tunnel, L2TP_MSG_DATA, |
859 | "%s: recv control packet, len=%d\n", | |
860 | tunnel->name, length); | |
f7faffa3 JC |
861 | goto error; |
862 | } | |
863 | ||
864 | /* Skip flags */ | |
865 | ptr += 2; | |
866 | ||
867 | if (tunnel->version == L2TP_HDR_VER_2) { | |
868 | /* If length is present, skip it */ | |
869 | if (hdrflags & L2TP_HDRFLAG_L) | |
870 | ptr += 2; | |
871 | ||
872 | /* Extract tunnel and session ID */ | |
873 | tunnel_id = ntohs(*(__be16 *) ptr); | |
874 | ptr += 2; | |
875 | session_id = ntohs(*(__be16 *) ptr); | |
876 | ptr += 2; | |
877 | } else { | |
878 | ptr += 2; /* skip reserved bits */ | |
879 | tunnel_id = tunnel->tunnel_id; | |
880 | session_id = ntohl(*(__be32 *) ptr); | |
881 | ptr += 4; | |
882 | } | |
883 | ||
884 | /* Find the session context */ | |
a4346210 | 885 | session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id); |
309795f4 | 886 | if (!session || !session->recv_skb) { |
a4346210 | 887 | if (session) |
61b9a047 | 888 | l2tp_session_dec_refcount(session); |
61b9a047 | 889 | |
f7faffa3 | 890 | /* Not found? Pass to userspace to deal with */ |
a4ca44fa JP |
891 | l2tp_info(tunnel, L2TP_MSG_DATA, |
892 | "%s: no session found (%u/%u). Passing up.\n", | |
893 | tunnel->name, tunnel_id, session_id); | |
f7faffa3 JC |
894 | goto error; |
895 | } | |
896 | ||
1f18aadf JW |
897 | if (tunnel->version == L2TP_HDR_VER_3 && |
898 | l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) | |
899 | goto error; | |
900 | ||
f7faffa3 | 901 | l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); |
61b9a047 | 902 | l2tp_session_dec_refcount(session); |
fd558d18 JC |
903 | |
904 | return 0; | |
905 | ||
fd558d18 JC |
906 | error: |
907 | /* Put UDP header back */ | |
908 | __skb_push(skb, sizeof(struct udphdr)); | |
909 | ||
910 | return 1; | |
911 | } | |
fd558d18 JC |
912 | |
913 | /* UDP encapsulation receive handler. See net/ipv4/udp.c. | |
914 | * Return codes: | |
915 | * 0 : success. | |
916 | * <0: error | |
917 | * >0: skb should be passed up to userspace as UDP. | |
918 | */ | |
919 | int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
920 | { | |
921 | struct l2tp_tunnel *tunnel; | |
922 | ||
074923b5 | 923 | tunnel = rcu_dereference_sk_user_data(sk); |
fd558d18 JC |
924 | if (tunnel == NULL) |
925 | goto pass_up; | |
926 | ||
a4ca44fa JP |
927 | l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", |
928 | tunnel->name, skb->len); | |
fd558d18 JC |
929 | |
930 | if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) | |
02a25fee | 931 | goto pass_up; |
fd558d18 | 932 | |
fd558d18 JC |
933 | return 0; |
934 | ||
fd558d18 JC |
935 | pass_up: |
936 | return 1; | |
937 | } | |
938 | EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); | |
939 | ||
940 | /************************************************************************ | |
941 | * Transmit handling | |
942 | ***********************************************************************/ | |
943 | ||
944 | /* Build an L2TP header for the session into the buffer provided. | |
945 | */ | |
f7faffa3 | 946 | static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) |
fd558d18 | 947 | { |
f7faffa3 | 948 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 949 | __be16 *bufp = buf; |
f7faffa3 | 950 | __be16 *optr = buf; |
fd558d18 JC |
951 | u16 flags = L2TP_HDR_VER_2; |
952 | u32 tunnel_id = tunnel->peer_tunnel_id; | |
953 | u32 session_id = session->peer_session_id; | |
954 | ||
955 | if (session->send_seq) | |
956 | flags |= L2TP_HDRFLAG_S; | |
957 | ||
958 | /* Setup L2TP header. */ | |
959 | *bufp++ = htons(flags); | |
960 | *bufp++ = htons(tunnel_id); | |
961 | *bufp++ = htons(session_id); | |
962 | if (session->send_seq) { | |
963 | *bufp++ = htons(session->ns); | |
964 | *bufp++ = 0; | |
965 | session->ns++; | |
f7faffa3 | 966 | session->ns &= 0xffff; |
a4ca44fa JP |
967 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", |
968 | session->name, session->ns); | |
fd558d18 | 969 | } |
f7faffa3 JC |
970 | |
971 | return bufp - optr; | |
fd558d18 JC |
972 | } |
973 | ||
f7faffa3 | 974 | static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) |
fd558d18 | 975 | { |
0d76751f | 976 | struct l2tp_tunnel *tunnel = session->tunnel; |
f7faffa3 JC |
977 | char *bufp = buf; |
978 | char *optr = bufp; | |
f7faffa3 | 979 | |
0d76751f JC |
980 | /* Setup L2TP header. The header differs slightly for UDP and |
981 | * IP encapsulations. For UDP, there is 4 bytes of flags. | |
982 | */ | |
983 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { | |
984 | u16 flags = L2TP_HDR_VER_3; | |
985 | *((__be16 *) bufp) = htons(flags); | |
986 | bufp += 2; | |
987 | *((__be16 *) bufp) = 0; | |
988 | bufp += 2; | |
989 | } | |
990 | ||
f7faffa3 JC |
991 | *((__be32 *) bufp) = htonl(session->peer_session_id); |
992 | bufp += 4; | |
993 | if (session->cookie_len) { | |
994 | memcpy(bufp, &session->cookie[0], session->cookie_len); | |
995 | bufp += session->cookie_len; | |
996 | } | |
b25387e0 LB |
997 | if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { |
998 | u32 l2h = 0; | |
f7faffa3 | 999 | |
b25387e0 LB |
1000 | if (session->send_seq) { |
1001 | l2h = 0x40000000 | session->ns; | |
1002 | session->ns++; | |
1003 | session->ns &= 0xffffff; | |
1004 | l2tp_dbg(session, L2TP_MSG_SEQ, | |
1005 | "%s: updated ns to %u\n", | |
1006 | session->name, session->ns); | |
f7faffa3 | 1007 | } |
b25387e0 LB |
1008 | |
1009 | *((__be32 *)bufp) = htonl(l2h); | |
1010 | bufp += 4; | |
f7faffa3 | 1011 | } |
fd558d18 | 1012 | |
f7faffa3 | 1013 | return bufp - optr; |
fd558d18 | 1014 | } |
fd558d18 | 1015 | |
fc130840 | 1016 | static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, |
d9d8da80 | 1017 | struct flowi *fl, size_t data_len) |
fd558d18 JC |
1018 | { |
1019 | struct l2tp_tunnel *tunnel = session->tunnel; | |
1020 | unsigned int len = skb->len; | |
1021 | int error; | |
1022 | ||
1023 | /* Debug */ | |
1024 | if (session->send_seq) | |
5b5e0928 | 1025 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", |
a4ca44fa | 1026 | session->name, data_len, session->ns - 1); |
fd558d18 | 1027 | else |
5b5e0928 | 1028 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", |
a4ca44fa | 1029 | session->name, data_len); |
fd558d18 JC |
1030 | |
1031 | if (session->debug & L2TP_MSG_DATA) { | |
0d76751f JC |
1032 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
1033 | unsigned char *datap = skb->data + uhlen; | |
fd558d18 | 1034 | |
a4ca44fa JP |
1035 | pr_debug("%s: xmit\n", session->name); |
1036 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, | |
1037 | datap, min_t(size_t, 32, len - uhlen)); | |
fd558d18 JC |
1038 | } |
1039 | ||
1040 | /* Queue the packet to IP for output */ | |
60ff7467 | 1041 | skb->ignore_df = 1; |
d2cf3361 | 1042 | #if IS_ENABLED(CONFIG_IPV6) |
95f87b3d | 1043 | if (l2tp_sk_is_v6(tunnel->sock)) |
b0270e91 | 1044 | error = inet6_csk_xmit(tunnel->sock, skb, NULL); |
d2cf3361 BL |
1045 | else |
1046 | #endif | |
b0270e91 | 1047 | error = ip_queue_xmit(tunnel->sock, skb, fl); |
fd558d18 JC |
1048 | |
1049 | /* Update stats */ | |
1050 | if (error >= 0) { | |
7b7c0719 TP |
1051 | atomic_long_inc(&tunnel->stats.tx_packets); |
1052 | atomic_long_add(len, &tunnel->stats.tx_bytes); | |
1053 | atomic_long_inc(&session->stats.tx_packets); | |
1054 | atomic_long_add(len, &session->stats.tx_bytes); | |
fd558d18 | 1055 | } else { |
7b7c0719 TP |
1056 | atomic_long_inc(&tunnel->stats.tx_errors); |
1057 | atomic_long_inc(&session->stats.tx_errors); | |
fd558d18 JC |
1058 | } |
1059 | ||
1060 | return 0; | |
1061 | } | |
fd558d18 | 1062 | |
fd558d18 JC |
1063 | /* If caller requires the skb to have a ppp header, the header must be |
1064 | * inserted in the skb data before calling this function. | |
1065 | */ | |
1066 | int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) | |
1067 | { | |
1068 | int data_len = skb->len; | |
0d76751f JC |
1069 | struct l2tp_tunnel *tunnel = session->tunnel; |
1070 | struct sock *sk = tunnel->sock; | |
d9d8da80 | 1071 | struct flowi *fl; |
fd558d18 | 1072 | struct udphdr *uh; |
fd558d18 | 1073 | struct inet_sock *inet; |
fd558d18 | 1074 | int headroom; |
0d76751f JC |
1075 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
1076 | int udp_len; | |
b8c84307 | 1077 | int ret = NET_XMIT_SUCCESS; |
fd558d18 JC |
1078 | |
1079 | /* Check that there's enough headroom in the skb to insert IP, | |
1080 | * UDP and L2TP headers. If not enough, expand it to | |
1081 | * make room. Adjust truesize. | |
1082 | */ | |
1083 | headroom = NET_SKB_PAD + sizeof(struct iphdr) + | |
0d76751f | 1084 | uhlen + hdr_len; |
835acf5d | 1085 | if (skb_cow_head(skb, headroom)) { |
b8c84307 ED |
1086 | kfree_skb(skb); |
1087 | return NET_XMIT_DROP; | |
835acf5d | 1088 | } |
fd558d18 | 1089 | |
fd558d18 | 1090 | /* Setup L2TP header */ |
f7faffa3 | 1091 | session->build_header(session, __skb_push(skb, hdr_len)); |
fd558d18 | 1092 | |
0d76751f | 1093 | /* Reset skb netfilter state */ |
fd558d18 JC |
1094 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
1095 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | |
1096 | IPSKB_REROUTED); | |
1097 | nf_reset(skb); | |
1098 | ||
6af88da1 DM |
1099 | bh_lock_sock(sk); |
1100 | if (sock_owned_by_user(sk)) { | |
b8c84307 ED |
1101 | kfree_skb(skb); |
1102 | ret = NET_XMIT_DROP; | |
6af88da1 DM |
1103 | goto out_unlock; |
1104 | } | |
1105 | ||
95f87b3d PA |
1106 | /* The user-space may change the connection status for the user-space |
1107 | * provided socket at run time: we must check it under the socket lock | |
1108 | */ | |
1109 | if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) { | |
1110 | kfree_skb(skb); | |
1111 | ret = NET_XMIT_DROP; | |
1112 | goto out_unlock; | |
1113 | } | |
1114 | ||
fd558d18 JC |
1115 | /* Get routing info from the tunnel socket */ |
1116 | skb_dst_drop(skb); | |
96596f83 | 1117 | skb_dst_set(skb, sk_dst_check(sk, 0)); |
fd558d18 | 1118 | |
d9d8da80 DM |
1119 | inet = inet_sk(sk); |
1120 | fl = &inet->cork.fl; | |
0d76751f JC |
1121 | switch (tunnel->encap) { |
1122 | case L2TP_ENCAPTYPE_UDP: | |
1123 | /* Setup UDP header */ | |
0d76751f JC |
1124 | __skb_push(skb, sizeof(*uh)); |
1125 | skb_reset_transport_header(skb); | |
1126 | uh = udp_hdr(skb); | |
1127 | uh->source = inet->inet_sport; | |
1128 | uh->dest = inet->inet_dport; | |
1129 | udp_len = uhlen + hdr_len + data_len; | |
1130 | uh->len = htons(udp_len); | |
0d76751f JC |
1131 | |
1132 | /* Calculate UDP checksum if configured to do so */ | |
d2cf3361 | 1133 | #if IS_ENABLED(CONFIG_IPV6) |
95f87b3d | 1134 | if (l2tp_sk_is_v6(sk)) |
77157e19 TH |
1135 | udp6_set_csum(udp_get_no_check6_tx(sk), |
1136 | skb, &inet6_sk(sk)->saddr, | |
1137 | &sk->sk_v6_daddr, udp_len); | |
d2cf3361 BL |
1138 | else |
1139 | #endif | |
77157e19 TH |
1140 | udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, |
1141 | inet->inet_daddr, udp_len); | |
0d76751f JC |
1142 | break; |
1143 | ||
1144 | case L2TP_ENCAPTYPE_IP: | |
1145 | break; | |
fd558d18 JC |
1146 | } |
1147 | ||
d9d8da80 | 1148 | l2tp_xmit_core(session, skb, fl, data_len); |
6af88da1 DM |
1149 | out_unlock: |
1150 | bh_unlock_sock(sk); | |
fd558d18 | 1151 | |
b8c84307 | 1152 | return ret; |
fd558d18 JC |
1153 | } |
1154 | EXPORT_SYMBOL_GPL(l2tp_xmit_skb); | |
1155 | ||
1156 | /***************************************************************************** | |
1157 | * Tinnel and session create/destroy. | |
1158 | *****************************************************************************/ | |
1159 | ||
1160 | /* Tunnel socket destruct hook. | |
1161 | * The tunnel context is deleted only when all session sockets have been | |
1162 | * closed. | |
1163 | */ | |
fc130840 | 1164 | static void l2tp_tunnel_destruct(struct sock *sk) |
fd558d18 | 1165 | { |
8d8a51e2 | 1166 | struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); |
fd558d18 | 1167 | |
fd558d18 JC |
1168 | if (tunnel == NULL) |
1169 | goto end; | |
1170 | ||
a4ca44fa | 1171 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); |
fd558d18 | 1172 | |
f8ccac0e | 1173 | /* Disable udp encapsulation */ |
0d76751f JC |
1174 | switch (tunnel->encap) { |
1175 | case L2TP_ENCAPTYPE_UDP: | |
1176 | /* No longer an encapsulation socket. See net/ipv4/udp.c */ | |
1177 | (udp_sk(sk))->encap_type = 0; | |
1178 | (udp_sk(sk))->encap_rcv = NULL; | |
9980d001 | 1179 | (udp_sk(sk))->encap_destroy = NULL; |
0d76751f JC |
1180 | break; |
1181 | case L2TP_ENCAPTYPE_IP: | |
1182 | break; | |
1183 | } | |
fd558d18 JC |
1184 | |
1185 | /* Remove hooks into tunnel socket */ | |
fd558d18 JC |
1186 | sk->sk_destruct = tunnel->old_sk_destruct; |
1187 | sk->sk_user_data = NULL; | |
1188 | ||
f8ccac0e TP |
1189 | /* Call the original destructor */ |
1190 | if (sk->sk_destruct) | |
1191 | (*sk->sk_destruct)(sk); | |
02a25fee JC |
1192 | |
1193 | kfree_rcu(tunnel, rcu); | |
fd558d18 JC |
1194 | end: |
1195 | return; | |
1196 | } | |
fd558d18 JC |
1197 | |
1198 | /* When the tunnel is closed, all the attached sessions need to go too. | |
1199 | */ | |
e34f4c70 | 1200 | void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) |
fd558d18 JC |
1201 | { |
1202 | int hash; | |
1203 | struct hlist_node *walk; | |
1204 | struct hlist_node *tmp; | |
1205 | struct l2tp_session *session; | |
1206 | ||
1207 | BUG_ON(tunnel == NULL); | |
1208 | ||
a4ca44fa JP |
1209 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", |
1210 | tunnel->name); | |
fd558d18 JC |
1211 | |
1212 | write_lock_bh(&tunnel->hlist_lock); | |
f3c66d4e | 1213 | tunnel->acpt_newsess = false; |
fd558d18 JC |
1214 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { |
1215 | again: | |
1216 | hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { | |
1217 | session = hlist_entry(walk, struct l2tp_session, hlist); | |
1218 | ||
a4ca44fa JP |
1219 | l2tp_info(session, L2TP_MSG_CONTROL, |
1220 | "%s: closing session\n", session->name); | |
fd558d18 JC |
1221 | |
1222 | hlist_del_init(&session->hlist); | |
1223 | ||
b228a940 GN |
1224 | if (test_and_set_bit(0, &session->dead)) |
1225 | goto again; | |
1226 | ||
fd558d18 JC |
1227 | write_unlock_bh(&tunnel->hlist_lock); |
1228 | ||
f6e16b29 | 1229 | __l2tp_session_unhash(session); |
4c6e2fd3 TP |
1230 | l2tp_session_queue_purge(session); |
1231 | ||
fd558d18 JC |
1232 | if (session->session_close != NULL) |
1233 | (*session->session_close)(session); | |
1234 | ||
9980d001 TP |
1235 | l2tp_session_dec_refcount(session); |
1236 | ||
fd558d18 JC |
1237 | write_lock_bh(&tunnel->hlist_lock); |
1238 | ||
1239 | /* Now restart from the beginning of this hash | |
1240 | * chain. We always remove a session from the | |
1241 | * list so we are guaranteed to make forward | |
1242 | * progress. | |
1243 | */ | |
1244 | goto again; | |
1245 | } | |
1246 | } | |
1247 | write_unlock_bh(&tunnel->hlist_lock); | |
1248 | } | |
e34f4c70 | 1249 | EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); |
fd558d18 | 1250 | |
9980d001 TP |
1251 | /* Tunnel socket destroy hook for UDP encapsulation */ |
1252 | static void l2tp_udp_encap_destroy(struct sock *sk) | |
1253 | { | |
02a25fee JC |
1254 | struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); |
1255 | ||
1256 | if (tunnel) | |
1257 | l2tp_tunnel_delete(tunnel); | |
9980d001 TP |
1258 | } |
1259 | ||
f8ccac0e TP |
1260 | /* Workqueue tunnel deletion function */ |
1261 | static void l2tp_tunnel_del_work(struct work_struct *work) | |
1262 | { | |
02a25fee JC |
1263 | struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel, |
1264 | del_work); | |
1265 | struct sock *sk = tunnel->sock; | |
1266 | struct socket *sock = sk->sk_socket; | |
889bf6f9 | 1267 | struct l2tp_net *pn; |
12d656af RK |
1268 | |
1269 | l2tp_tunnel_closeall(tunnel); | |
1270 | ||
376c8af3 | 1271 | /* If the tunnel socket was created within the kernel, use |
02d13ed5 | 1272 | * the sk API to release it here. |
f8ccac0e | 1273 | */ |
376c8af3 | 1274 | if (tunnel->fd < 0) { |
26abe143 | 1275 | if (sock) { |
02d13ed5 | 1276 | kernel_sock_shutdown(sock, SHUT_RDWR); |
26abe143 EB |
1277 | sock_release(sock); |
1278 | } | |
167eb17e | 1279 | } |
f8ccac0e | 1280 | |
889bf6f9 JC |
1281 | /* Remove the tunnel struct from the tunnel list */ |
1282 | pn = l2tp_pernet(tunnel->l2tp_net); | |
1283 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); | |
1284 | list_del_rcu(&tunnel->list); | |
1285 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1286 | ||
02a25fee JC |
1287 | /* drop initial ref */ |
1288 | l2tp_tunnel_dec_refcount(tunnel); | |
1289 | ||
1290 | /* drop workqueue ref */ | |
06a15f51 | 1291 | l2tp_tunnel_dec_refcount(tunnel); |
fd558d18 | 1292 | } |
fd558d18 | 1293 | |
789a4a2c JC |
1294 | /* Create a socket for the tunnel, if one isn't set up by |
1295 | * userspace. This is used for static tunnels where there is no | |
1296 | * managing L2TP daemon. | |
167eb17e TP |
1297 | * |
1298 | * Since we don't want these sockets to keep a namespace alive by | |
1299 | * themselves, we drop the socket's namespace refcount after creation. | |
1300 | * These sockets are freed when the namespace exits using the pernet | |
1301 | * exit hook. | |
789a4a2c | 1302 | */ |
167eb17e TP |
1303 | static int l2tp_tunnel_sock_create(struct net *net, |
1304 | u32 tunnel_id, | |
1305 | u32 peer_tunnel_id, | |
1306 | struct l2tp_tunnel_cfg *cfg, | |
1307 | struct socket **sockp) | |
789a4a2c JC |
1308 | { |
1309 | int err = -EINVAL; | |
167eb17e | 1310 | struct socket *sock = NULL; |
85644b4d | 1311 | struct udp_port_cfg udp_conf; |
789a4a2c JC |
1312 | |
1313 | switch (cfg->encap) { | |
1314 | case L2TP_ENCAPTYPE_UDP: | |
85644b4d TH |
1315 | memset(&udp_conf, 0, sizeof(udp_conf)); |
1316 | ||
f9bac8df CE |
1317 | #if IS_ENABLED(CONFIG_IPV6) |
1318 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
85644b4d TH |
1319 | udp_conf.family = AF_INET6; |
1320 | memcpy(&udp_conf.local_ip6, cfg->local_ip6, | |
1321 | sizeof(udp_conf.local_ip6)); | |
1322 | memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, | |
1323 | sizeof(udp_conf.peer_ip6)); | |
1324 | udp_conf.use_udp6_tx_checksums = | |
018f8258 | 1325 | ! cfg->udp6_zero_tx_checksums; |
85644b4d | 1326 | udp_conf.use_udp6_rx_checksums = |
018f8258 | 1327 | ! cfg->udp6_zero_rx_checksums; |
f9bac8df CE |
1328 | } else |
1329 | #endif | |
1330 | { | |
85644b4d TH |
1331 | udp_conf.family = AF_INET; |
1332 | udp_conf.local_ip = cfg->local_ip; | |
1333 | udp_conf.peer_ip = cfg->peer_ip; | |
1334 | udp_conf.use_udp_checksums = cfg->use_udp_checksums; | |
f9bac8df | 1335 | } |
789a4a2c | 1336 | |
85644b4d TH |
1337 | udp_conf.local_udp_port = htons(cfg->local_udp_port); |
1338 | udp_conf.peer_udp_port = htons(cfg->peer_udp_port); | |
1339 | ||
1340 | err = udp_sock_create(net, &udp_conf, &sock); | |
1341 | if (err < 0) | |
1342 | goto out; | |
789a4a2c JC |
1343 | |
1344 | break; | |
1345 | ||
1346 | case L2TP_ENCAPTYPE_IP: | |
f9bac8df CE |
1347 | #if IS_ENABLED(CONFIG_IPV6) |
1348 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
85644b4d TH |
1349 | struct sockaddr_l2tpip6 ip6_addr = {0}; |
1350 | ||
26abe143 | 1351 | err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, |
167eb17e | 1352 | IPPROTO_L2TP, &sock); |
5dac94e1 JC |
1353 | if (err < 0) |
1354 | goto out; | |
789a4a2c | 1355 | |
5dac94e1 JC |
1356 | ip6_addr.l2tp_family = AF_INET6; |
1357 | memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, | |
1358 | sizeof(ip6_addr.l2tp_addr)); | |
1359 | ip6_addr.l2tp_conn_id = tunnel_id; | |
1360 | err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, | |
1361 | sizeof(ip6_addr)); | |
1362 | if (err < 0) | |
1363 | goto out; | |
789a4a2c | 1364 | |
5dac94e1 JC |
1365 | ip6_addr.l2tp_family = AF_INET6; |
1366 | memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, | |
1367 | sizeof(ip6_addr.l2tp_addr)); | |
1368 | ip6_addr.l2tp_conn_id = peer_tunnel_id; | |
1369 | err = kernel_connect(sock, | |
1370 | (struct sockaddr *) &ip6_addr, | |
1371 | sizeof(ip6_addr), 0); | |
1372 | if (err < 0) | |
1373 | goto out; | |
1374 | } else | |
1375 | #endif | |
1376 | { | |
85644b4d TH |
1377 | struct sockaddr_l2tpip ip_addr = {0}; |
1378 | ||
26abe143 | 1379 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, |
167eb17e | 1380 | IPPROTO_L2TP, &sock); |
5dac94e1 JC |
1381 | if (err < 0) |
1382 | goto out; | |
789a4a2c | 1383 | |
5dac94e1 JC |
1384 | ip_addr.l2tp_family = AF_INET; |
1385 | ip_addr.l2tp_addr = cfg->local_ip; | |
1386 | ip_addr.l2tp_conn_id = tunnel_id; | |
1387 | err = kernel_bind(sock, (struct sockaddr *) &ip_addr, | |
1388 | sizeof(ip_addr)); | |
1389 | if (err < 0) | |
1390 | goto out; | |
1391 | ||
1392 | ip_addr.l2tp_family = AF_INET; | |
1393 | ip_addr.l2tp_addr = cfg->peer_ip; | |
1394 | ip_addr.l2tp_conn_id = peer_tunnel_id; | |
1395 | err = kernel_connect(sock, (struct sockaddr *) &ip_addr, | |
1396 | sizeof(ip_addr), 0); | |
1397 | if (err < 0) | |
1398 | goto out; | |
1399 | } | |
789a4a2c JC |
1400 | break; |
1401 | ||
1402 | default: | |
1403 | goto out; | |
1404 | } | |
1405 | ||
1406 | out: | |
167eb17e | 1407 | *sockp = sock; |
789a4a2c | 1408 | if ((err < 0) && sock) { |
167eb17e | 1409 | kernel_sock_shutdown(sock, SHUT_RDWR); |
26abe143 | 1410 | sock_release(sock); |
789a4a2c JC |
1411 | *sockp = NULL; |
1412 | } | |
1413 | ||
1414 | return err; | |
1415 | } | |
1416 | ||
37159ef2 ED |
1417 | static struct lock_class_key l2tp_socket_class; |
1418 | ||
fd558d18 JC |
1419 | 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) |
1420 | { | |
1421 | struct l2tp_tunnel *tunnel = NULL; | |
1422 | int err; | |
0d76751f | 1423 | enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; |
fd558d18 | 1424 | |
0d76751f JC |
1425 | if (cfg != NULL) |
1426 | encap = cfg->encap; | |
1427 | ||
fd558d18 JC |
1428 | tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); |
1429 | if (tunnel == NULL) { | |
1430 | err = -ENOMEM; | |
1431 | goto err; | |
1432 | } | |
1433 | ||
1434 | tunnel->version = version; | |
1435 | tunnel->tunnel_id = tunnel_id; | |
1436 | tunnel->peer_tunnel_id = peer_tunnel_id; | |
1437 | tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; | |
1438 | ||
1439 | tunnel->magic = L2TP_TUNNEL_MAGIC; | |
1440 | sprintf(&tunnel->name[0], "tunl %u", tunnel_id); | |
1441 | rwlock_init(&tunnel->hlist_lock); | |
f3c66d4e | 1442 | tunnel->acpt_newsess = true; |
fd558d18 | 1443 | |
0d76751f | 1444 | if (cfg != NULL) |
fd558d18 JC |
1445 | tunnel->debug = cfg->debug; |
1446 | ||
0d76751f | 1447 | tunnel->encap = encap; |
fd558d18 | 1448 | |
02a25fee | 1449 | refcount_set(&tunnel->ref_count, 1); |
02a25fee JC |
1450 | tunnel->fd = fd; |
1451 | ||
f8ccac0e TP |
1452 | /* Init delete workqueue struct */ |
1453 | INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); | |
1454 | ||
fd558d18 | 1455 | INIT_LIST_HEAD(&tunnel->list); |
fd558d18 JC |
1456 | |
1457 | err = 0; | |
1458 | err: | |
1459 | if (tunnelp) | |
1460 | *tunnelp = tunnel; | |
1461 | ||
fd558d18 JC |
1462 | return err; |
1463 | } | |
1464 | EXPORT_SYMBOL_GPL(l2tp_tunnel_create); | |
1465 | ||
98aa4345 GN |
1466 | static int l2tp_validate_socket(const struct sock *sk, const struct net *net, |
1467 | enum l2tp_encap_type encap) | |
1468 | { | |
1469 | if (!net_eq(sock_net(sk), net)) | |
1470 | return -EINVAL; | |
1471 | ||
1472 | if (sk->sk_type != SOCK_DGRAM) | |
1473 | return -EPROTONOSUPPORT; | |
1474 | ||
1475 | if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) || | |
1476 | (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) | |
1477 | return -EPROTONOSUPPORT; | |
1478 | ||
1479 | if (sk->sk_user_data) | |
1480 | return -EBUSY; | |
1481 | ||
1482 | return 0; | |
1483 | } | |
1484 | ||
1485 | int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, | |
1486 | struct l2tp_tunnel_cfg *cfg) | |
1487 | { | |
39bfa1a2 | 1488 | struct l2tp_tunnel *tunnel_walk; |
98aa4345 GN |
1489 | struct l2tp_net *pn; |
1490 | struct socket *sock; | |
1491 | struct sock *sk; | |
1492 | int ret; | |
1493 | ||
1494 | if (tunnel->fd < 0) { | |
1495 | ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id, | |
1496 | tunnel->peer_tunnel_id, cfg, | |
1497 | &sock); | |
1498 | if (ret < 0) | |
1499 | goto err; | |
1500 | } else { | |
1501 | sock = sockfd_lookup(tunnel->fd, &ret); | |
1502 | if (!sock) | |
1503 | goto err; | |
1504 | ||
1505 | ret = l2tp_validate_socket(sock->sk, net, tunnel->encap); | |
1506 | if (ret < 0) | |
1507 | goto err_sock; | |
1508 | } | |
1509 | ||
98aa4345 | 1510 | tunnel->l2tp_net = net; |
98aa4345 | 1511 | pn = l2tp_pernet(net); |
39bfa1a2 | 1512 | |
98aa4345 | 1513 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); |
39bfa1a2 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 | } | |
98aa4345 GN |
1522 | list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); |
1523 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1524 | ||
a106c35c XL |
1525 | sk = sock->sk; |
1526 | sock_hold(sk); | |
1527 | tunnel->sock = sk; | |
1528 | ||
98aa4345 GN |
1529 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { |
1530 | struct udp_tunnel_sock_cfg udp_cfg = { | |
1531 | .sk_user_data = tunnel, | |
1532 | .encap_type = UDP_ENCAP_L2TPINUDP, | |
1533 | .encap_rcv = l2tp_udp_encap_recv, | |
1534 | .encap_destroy = l2tp_udp_encap_destroy, | |
1535 | }; | |
1536 | ||
1537 | setup_udp_tunnel_sock(net, sock, &udp_cfg); | |
1538 | } else { | |
1539 | sk->sk_user_data = tunnel; | |
1540 | } | |
1541 | ||
1542 | tunnel->old_sk_destruct = sk->sk_destruct; | |
1543 | sk->sk_destruct = &l2tp_tunnel_destruct; | |
1544 | lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, | |
1545 | "l2tp_sock"); | |
1546 | sk->sk_allocation = GFP_ATOMIC; | |
1547 | ||
1548 | if (tunnel->fd >= 0) | |
1549 | sockfd_put(sock); | |
1550 | ||
1551 | return 0; | |
1552 | ||
1553 | err_sock: | |
39bfa1a2 GN |
1554 | if (tunnel->fd < 0) |
1555 | sock_release(sock); | |
1556 | else | |
1557 | sockfd_put(sock); | |
98aa4345 GN |
1558 | err: |
1559 | return ret; | |
1560 | } | |
1561 | EXPORT_SYMBOL_GPL(l2tp_tunnel_register); | |
1562 | ||
309795f4 JC |
1563 | /* This function is used by the netlink TUNNEL_DELETE command. |
1564 | */ | |
62b982ee | 1565 | void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) |
309795f4 | 1566 | { |
62b982ee SD |
1567 | if (!test_and_set_bit(0, &tunnel->dead)) { |
1568 | l2tp_tunnel_inc_refcount(tunnel); | |
1569 | queue_work(l2tp_wq, &tunnel->del_work); | |
06a15f51 | 1570 | } |
309795f4 JC |
1571 | } |
1572 | EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); | |
1573 | ||
fd558d18 JC |
1574 | /* Really kill the session. |
1575 | */ | |
1576 | void l2tp_session_free(struct l2tp_session *session) | |
1577 | { | |
f6e16b29 | 1578 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 1579 | |
fbea9e07 | 1580 | BUG_ON(refcount_read(&session->ref_count) != 0); |
fd558d18 | 1581 | |
f6e16b29 | 1582 | if (tunnel) { |
fd558d18 | 1583 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); |
f6e16b29 TP |
1584 | l2tp_tunnel_dec_refcount(tunnel); |
1585 | } | |
1586 | ||
1587 | kfree(session); | |
f6e16b29 TP |
1588 | } |
1589 | EXPORT_SYMBOL_GPL(l2tp_session_free); | |
1590 | ||
1591 | /* Remove an l2tp session from l2tp_core's hash lists. | |
1592 | * Provides a tidyup interface for pseudowire code which can't just route all | |
1593 | * shutdown via. l2tp_session_delete and a pseudowire-specific session_close | |
1594 | * callback. | |
1595 | */ | |
1596 | void __l2tp_session_unhash(struct l2tp_session *session) | |
1597 | { | |
1598 | struct l2tp_tunnel *tunnel = session->tunnel; | |
fd558d18 | 1599 | |
f6e16b29 TP |
1600 | /* Remove the session from core hashes */ |
1601 | if (tunnel) { | |
1602 | /* Remove from the per-tunnel hash */ | |
fd558d18 JC |
1603 | write_lock_bh(&tunnel->hlist_lock); |
1604 | hlist_del_init(&session->hlist); | |
1605 | write_unlock_bh(&tunnel->hlist_lock); | |
1606 | ||
f6e16b29 | 1607 | /* For L2TPv3 we have a per-net hash: remove from there, too */ |
f7faffa3 JC |
1608 | if (tunnel->version != L2TP_HDR_VER_2) { |
1609 | struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); | |
e02d494d JC |
1610 | spin_lock_bh(&pn->l2tp_session_hlist_lock); |
1611 | hlist_del_init_rcu(&session->global_hlist); | |
1612 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); | |
1613 | synchronize_rcu(); | |
f7faffa3 | 1614 | } |
fd558d18 | 1615 | } |
fd558d18 | 1616 | } |
f6e16b29 | 1617 | EXPORT_SYMBOL_GPL(__l2tp_session_unhash); |
fd558d18 | 1618 | |
309795f4 JC |
1619 | /* This function is used by the netlink SESSION_DELETE command and by |
1620 | pseudowire modules. | |
1621 | */ | |
1622 | int l2tp_session_delete(struct l2tp_session *session) | |
1623 | { | |
b228a940 GN |
1624 | if (test_and_set_bit(0, &session->dead)) |
1625 | return 0; | |
1626 | ||
f6e16b29 | 1627 | __l2tp_session_unhash(session); |
4c6e2fd3 | 1628 | l2tp_session_queue_purge(session); |
309795f4 JC |
1629 | if (session->session_close != NULL) |
1630 | (*session->session_close)(session); | |
a4346210 | 1631 | |
309795f4 | 1632 | l2tp_session_dec_refcount(session); |
a4346210 | 1633 | |
309795f4 JC |
1634 | return 0; |
1635 | } | |
1636 | EXPORT_SYMBOL_GPL(l2tp_session_delete); | |
1637 | ||
f7faffa3 | 1638 | /* We come here whenever a session's send_seq, cookie_len or |
b25387e0 | 1639 | * l2specific_type parameters are set. |
f7faffa3 | 1640 | */ |
bb5016ea | 1641 | void l2tp_session_set_header_len(struct l2tp_session *session, int version) |
f7faffa3 JC |
1642 | { |
1643 | if (version == L2TP_HDR_VER_2) { | |
1644 | session->hdr_len = 6; | |
1645 | if (session->send_seq) | |
1646 | session->hdr_len += 4; | |
1647 | } else { | |
b25387e0 LB |
1648 | session->hdr_len = 4 + session->cookie_len; |
1649 | session->hdr_len += l2tp_get_l2specific_len(session); | |
0d76751f JC |
1650 | if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) |
1651 | session->hdr_len += 4; | |
f7faffa3 JC |
1652 | } |
1653 | ||
1654 | } | |
bb5016ea | 1655 | EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); |
f7faffa3 | 1656 | |
fd558d18 JC |
1657 | 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) |
1658 | { | |
1659 | struct l2tp_session *session; | |
1660 | ||
1661 | session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); | |
1662 | if (session != NULL) { | |
1663 | session->magic = L2TP_SESSION_MAGIC; | |
1664 | session->tunnel = tunnel; | |
1665 | ||
1666 | session->session_id = session_id; | |
1667 | session->peer_session_id = peer_session_id; | |
d301e325 | 1668 | session->nr = 0; |
8a1631d5 JC |
1669 | if (tunnel->version == L2TP_HDR_VER_2) |
1670 | session->nr_max = 0xffff; | |
1671 | else | |
1672 | session->nr_max = 0xffffff; | |
1673 | session->nr_window_size = session->nr_max / 2; | |
a0dbd822 JC |
1674 | session->nr_oos_count_max = 4; |
1675 | ||
1676 | /* Use NR of first received packet */ | |
1677 | session->reorder_skip = 1; | |
fd558d18 JC |
1678 | |
1679 | sprintf(&session->name[0], "sess %u/%u", | |
1680 | tunnel->tunnel_id, session->session_id); | |
1681 | ||
1682 | skb_queue_head_init(&session->reorder_q); | |
1683 | ||
1684 | INIT_HLIST_NODE(&session->hlist); | |
f7faffa3 | 1685 | INIT_HLIST_NODE(&session->global_hlist); |
fd558d18 JC |
1686 | |
1687 | /* Inherit debug options from tunnel */ | |
1688 | session->debug = tunnel->debug; | |
1689 | ||
1690 | if (cfg) { | |
f7faffa3 | 1691 | session->pwtype = cfg->pw_type; |
fd558d18 | 1692 | session->debug = cfg->debug; |
fd558d18 JC |
1693 | session->mtu = cfg->mtu; |
1694 | session->mru = cfg->mru; | |
1695 | session->send_seq = cfg->send_seq; | |
1696 | session->recv_seq = cfg->recv_seq; | |
1697 | session->lns_mode = cfg->lns_mode; | |
f7faffa3 | 1698 | session->reorder_timeout = cfg->reorder_timeout; |
f7faffa3 JC |
1699 | session->l2specific_type = cfg->l2specific_type; |
1700 | session->l2specific_len = cfg->l2specific_len; | |
1701 | session->cookie_len = cfg->cookie_len; | |
1702 | memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); | |
1703 | session->peer_cookie_len = cfg->peer_cookie_len; | |
1704 | memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); | |
fd558d18 JC |
1705 | } |
1706 | ||
f7faffa3 JC |
1707 | if (tunnel->version == L2TP_HDR_VER_2) |
1708 | session->build_header = l2tp_build_l2tpv2_header; | |
1709 | else | |
1710 | session->build_header = l2tp_build_l2tpv3_header; | |
1711 | ||
1712 | l2tp_session_set_header_len(session, tunnel->version); | |
1713 | ||
9ee369a4 GN |
1714 | refcount_set(&session->ref_count, 1); |
1715 | ||
dbdbc73b | 1716 | return session; |
fd558d18 JC |
1717 | } |
1718 | ||
dbdbc73b | 1719 | return ERR_PTR(-ENOMEM); |
fd558d18 JC |
1720 | } |
1721 | EXPORT_SYMBOL_GPL(l2tp_session_create); | |
1722 | ||
1723 | /***************************************************************************** | |
1724 | * Init and cleanup | |
1725 | *****************************************************************************/ | |
1726 | ||
1727 | static __net_init int l2tp_init_net(struct net *net) | |
1728 | { | |
e773aaff | 1729 | struct l2tp_net *pn = net_generic(net, l2tp_net_id); |
f7faffa3 | 1730 | int hash; |
fd558d18 | 1731 | |
fd558d18 | 1732 | INIT_LIST_HEAD(&pn->l2tp_tunnel_list); |
e02d494d | 1733 | spin_lock_init(&pn->l2tp_tunnel_list_lock); |
fd558d18 | 1734 | |
f7faffa3 JC |
1735 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) |
1736 | INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); | |
1737 | ||
e02d494d | 1738 | spin_lock_init(&pn->l2tp_session_hlist_lock); |
f7faffa3 | 1739 | |
fd558d18 | 1740 | return 0; |
fd558d18 JC |
1741 | } |
1742 | ||
167eb17e TP |
1743 | static __net_exit void l2tp_exit_net(struct net *net) |
1744 | { | |
1745 | struct l2tp_net *pn = l2tp_pernet(net); | |
1746 | struct l2tp_tunnel *tunnel = NULL; | |
1e7af3b2 | 1747 | int hash; |
167eb17e TP |
1748 | |
1749 | rcu_read_lock_bh(); | |
1750 | list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { | |
4dc12ffe | 1751 | l2tp_tunnel_delete(tunnel); |
167eb17e TP |
1752 | } |
1753 | rcu_read_unlock_bh(); | |
2f86953e | 1754 | |
67c05c4f Y |
1755 | if (l2tp_wq) |
1756 | flush_workqueue(l2tp_wq); | |
2f86953e | 1757 | rcu_barrier(); |
1e7af3b2 VA |
1758 | |
1759 | for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) | |
1760 | WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); | |
167eb17e TP |
1761 | } |
1762 | ||
fd558d18 JC |
1763 | static struct pernet_operations l2tp_net_ops = { |
1764 | .init = l2tp_init_net, | |
167eb17e | 1765 | .exit = l2tp_exit_net, |
fd558d18 JC |
1766 | .id = &l2tp_net_id, |
1767 | .size = sizeof(struct l2tp_net), | |
1768 | }; | |
1769 | ||
1770 | static int __init l2tp_init(void) | |
1771 | { | |
1772 | int rc = 0; | |
1773 | ||
1774 | rc = register_pernet_device(&l2tp_net_ops); | |
1775 | if (rc) | |
1776 | goto out; | |
1777 | ||
59ff3eb6 | 1778 | l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); |
f8ccac0e TP |
1779 | if (!l2tp_wq) { |
1780 | pr_err("alloc_workqueue failed\n"); | |
67e04c29 | 1781 | unregister_pernet_device(&l2tp_net_ops); |
f8ccac0e TP |
1782 | rc = -ENOMEM; |
1783 | goto out; | |
1784 | } | |
1785 | ||
a4ca44fa | 1786 | pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); |
fd558d18 JC |
1787 | |
1788 | out: | |
1789 | return rc; | |
1790 | } | |
1791 | ||
1792 | static void __exit l2tp_exit(void) | |
1793 | { | |
1794 | unregister_pernet_device(&l2tp_net_ops); | |
f8ccac0e TP |
1795 | if (l2tp_wq) { |
1796 | destroy_workqueue(l2tp_wq); | |
1797 | l2tp_wq = NULL; | |
1798 | } | |
fd558d18 JC |
1799 | } |
1800 | ||
1801 | module_init(l2tp_init); | |
1802 | module_exit(l2tp_exit); | |
1803 | ||
1804 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); | |
1805 | MODULE_DESCRIPTION("L2TP core"); | |
1806 | MODULE_LICENSE("GPL"); | |
1807 | MODULE_VERSION(L2TP_DRV_VERSION); | |
1808 |