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