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; |
5de7aee5 | 377 | struct l2tp_stats *sstats; |
fd558d18 JC |
378 | |
379 | spin_lock_bh(&session->reorder_q.lock); | |
5de7aee5 | 380 | sstats = &session->stats; |
fd558d18 JC |
381 | skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { |
382 | if (L2TP_SKB_CB(skbp)->ns > ns) { | |
383 | __skb_queue_before(&session->reorder_q, skbp, skb); | |
a4ca44fa JP |
384 | l2tp_dbg(session, L2TP_MSG_SEQ, |
385 | "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", | |
386 | session->name, ns, L2TP_SKB_CB(skbp)->ns, | |
387 | skb_queue_len(&session->reorder_q)); | |
5de7aee5 JC |
388 | u64_stats_update_begin(&sstats->syncp); |
389 | sstats->rx_oos_packets++; | |
390 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
391 | goto out; |
392 | } | |
393 | } | |
394 | ||
395 | __skb_queue_tail(&session->reorder_q, skb); | |
396 | ||
397 | out: | |
398 | spin_unlock_bh(&session->reorder_q.lock); | |
399 | } | |
400 | ||
401 | /* Dequeue a single skb. | |
402 | */ | |
403 | static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) | |
404 | { | |
405 | struct l2tp_tunnel *tunnel = session->tunnel; | |
406 | int length = L2TP_SKB_CB(skb)->length; | |
5de7aee5 | 407 | struct l2tp_stats *tstats, *sstats; |
fd558d18 JC |
408 | |
409 | /* We're about to requeue the skb, so return resources | |
410 | * to its current owner (a socket receive buffer). | |
411 | */ | |
412 | skb_orphan(skb); | |
413 | ||
5de7aee5 JC |
414 | tstats = &tunnel->stats; |
415 | u64_stats_update_begin(&tstats->syncp); | |
416 | sstats = &session->stats; | |
417 | u64_stats_update_begin(&sstats->syncp); | |
418 | tstats->rx_packets++; | |
419 | tstats->rx_bytes += length; | |
420 | sstats->rx_packets++; | |
421 | sstats->rx_bytes += length; | |
422 | u64_stats_update_end(&tstats->syncp); | |
423 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
424 | |
425 | if (L2TP_SKB_CB(skb)->has_seq) { | |
426 | /* Bump our Nr */ | |
427 | session->nr++; | |
f7faffa3 JC |
428 | if (tunnel->version == L2TP_HDR_VER_2) |
429 | session->nr &= 0xffff; | |
430 | else | |
431 | session->nr &= 0xffffff; | |
432 | ||
a4ca44fa JP |
433 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", |
434 | session->name, session->nr); | |
fd558d18 JC |
435 | } |
436 | ||
437 | /* call private receive handler */ | |
438 | if (session->recv_skb != NULL) | |
439 | (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); | |
440 | else | |
441 | kfree_skb(skb); | |
442 | ||
443 | if (session->deref) | |
444 | (*session->deref)(session); | |
445 | } | |
446 | ||
447 | /* Dequeue skbs from the session's reorder_q, subject to packet order. | |
448 | * Skbs that have been in the queue for too long are simply discarded. | |
449 | */ | |
450 | static void l2tp_recv_dequeue(struct l2tp_session *session) | |
451 | { | |
452 | struct sk_buff *skb; | |
453 | struct sk_buff *tmp; | |
5de7aee5 | 454 | struct l2tp_stats *sstats; |
fd558d18 JC |
455 | |
456 | /* If the pkt at the head of the queue has the nr that we | |
457 | * expect to send up next, dequeue it and any other | |
458 | * in-sequence packets behind it. | |
459 | */ | |
e2e210c0 | 460 | start: |
fd558d18 | 461 | spin_lock_bh(&session->reorder_q.lock); |
5de7aee5 | 462 | sstats = &session->stats; |
fd558d18 JC |
463 | skb_queue_walk_safe(&session->reorder_q, skb, tmp) { |
464 | if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { | |
5de7aee5 JC |
465 | u64_stats_update_begin(&sstats->syncp); |
466 | sstats->rx_seq_discards++; | |
467 | sstats->rx_errors++; | |
468 | u64_stats_update_end(&sstats->syncp); | |
a4ca44fa JP |
469 | l2tp_dbg(session, L2TP_MSG_SEQ, |
470 | "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", | |
471 | session->name, L2TP_SKB_CB(skb)->ns, | |
472 | L2TP_SKB_CB(skb)->length, session->nr, | |
473 | skb_queue_len(&session->reorder_q)); | |
38d40b3f | 474 | session->reorder_skip = 1; |
fd558d18 JC |
475 | __skb_unlink(skb, &session->reorder_q); |
476 | kfree_skb(skb); | |
477 | if (session->deref) | |
478 | (*session->deref)(session); | |
479 | continue; | |
480 | } | |
481 | ||
482 | if (L2TP_SKB_CB(skb)->has_seq) { | |
38d40b3f | 483 | if (session->reorder_skip) { |
a4ca44fa JP |
484 | l2tp_dbg(session, L2TP_MSG_SEQ, |
485 | "%s: advancing nr to next pkt: %u -> %u", | |
486 | session->name, session->nr, | |
487 | L2TP_SKB_CB(skb)->ns); | |
38d40b3f JC |
488 | session->reorder_skip = 0; |
489 | session->nr = L2TP_SKB_CB(skb)->ns; | |
490 | } | |
fd558d18 | 491 | if (L2TP_SKB_CB(skb)->ns != session->nr) { |
a4ca44fa JP |
492 | l2tp_dbg(session, L2TP_MSG_SEQ, |
493 | "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", | |
494 | session->name, L2TP_SKB_CB(skb)->ns, | |
495 | L2TP_SKB_CB(skb)->length, session->nr, | |
496 | skb_queue_len(&session->reorder_q)); | |
fd558d18 JC |
497 | goto out; |
498 | } | |
499 | } | |
500 | __skb_unlink(skb, &session->reorder_q); | |
501 | ||
502 | /* Process the skb. We release the queue lock while we | |
503 | * do so to let other contexts process the queue. | |
504 | */ | |
505 | spin_unlock_bh(&session->reorder_q.lock); | |
506 | l2tp_recv_dequeue_skb(session, skb); | |
e2e210c0 | 507 | goto start; |
fd558d18 JC |
508 | } |
509 | ||
510 | out: | |
511 | spin_unlock_bh(&session->reorder_q.lock); | |
512 | } | |
513 | ||
514 | static inline int l2tp_verify_udp_checksum(struct sock *sk, | |
515 | struct sk_buff *skb) | |
516 | { | |
517 | struct udphdr *uh = udp_hdr(skb); | |
518 | u16 ulen = ntohs(uh->len); | |
fd558d18 JC |
519 | __wsum psum; |
520 | ||
d2cf3361 | 521 | if (sk->sk_no_check || skb_csum_unnecessary(skb)) |
fd558d18 JC |
522 | return 0; |
523 | ||
d2cf3361 BL |
524 | #if IS_ENABLED(CONFIG_IPV6) |
525 | if (sk->sk_family == PF_INET6) { | |
526 | if (!uh->check) { | |
527 | LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n"); | |
528 | return 1; | |
529 | } | |
530 | if ((skb->ip_summed == CHECKSUM_COMPLETE) && | |
531 | !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, | |
532 | &ipv6_hdr(skb)->daddr, ulen, | |
533 | IPPROTO_UDP, skb->csum)) { | |
534 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
535 | return 0; | |
536 | } | |
537 | skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, | |
538 | &ipv6_hdr(skb)->daddr, | |
539 | skb->len, IPPROTO_UDP, | |
540 | 0)); | |
541 | } else | |
542 | #endif | |
543 | { | |
544 | struct inet_sock *inet; | |
545 | if (!uh->check) | |
546 | return 0; | |
547 | inet = inet_sk(sk); | |
548 | psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, | |
549 | ulen, IPPROTO_UDP, 0); | |
550 | ||
551 | if ((skb->ip_summed == CHECKSUM_COMPLETE) && | |
552 | !csum_fold(csum_add(psum, skb->csum))) | |
553 | return 0; | |
554 | skb->csum = psum; | |
555 | } | |
fd558d18 JC |
556 | |
557 | return __skb_checksum_complete(skb); | |
558 | } | |
559 | ||
f7faffa3 JC |
560 | /* Do receive processing of L2TP data frames. We handle both L2TPv2 |
561 | * and L2TPv3 data frames here. | |
562 | * | |
563 | * L2TPv2 Data Message Header | |
564 | * | |
565 | * 0 1 2 3 | |
566 | * 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 | |
567 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
568 | * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | | |
569 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
570 | * | Tunnel ID | Session ID | | |
571 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
572 | * | Ns (opt) | Nr (opt) | | |
573 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
574 | * | Offset Size (opt) | Offset pad... (opt) | |
575 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
576 | * | |
577 | * Data frames are marked by T=0. All other fields are the same as | |
578 | * those in L2TP control frames. | |
579 | * | |
580 | * L2TPv3 Data Message Header | |
581 | * | |
582 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
583 | * | L2TP Session Header | | |
584 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
585 | * | L2-Specific Sublayer | | |
586 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
587 | * | Tunnel Payload ... | |
588 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
589 | * | |
590 | * L2TPv3 Session Header Over IP | |
591 | * | |
592 | * 0 1 2 3 | |
593 | * 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 | |
594 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
595 | * | Session ID | | |
596 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
597 | * | Cookie (optional, maximum 64 bits)... | |
598 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
599 | * | | |
600 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
601 | * | |
602 | * L2TPv3 L2-Specific Sublayer Format | |
603 | * | |
604 | * 0 1 2 3 | |
605 | * 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 | |
606 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
607 | * |x|S|x|x|x|x|x|x| Sequence Number | | |
608 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
609 | * | |
610 | * Cookie value, sublayer format and offset (pad) are negotiated with | |
611 | * the peer when the session is set up. Unlike L2TPv2, we do not need | |
612 | * to parse the packet header to determine if optional fields are | |
613 | * present. | |
614 | * | |
615 | * Caller must already have parsed the frame and determined that it is | |
616 | * a data (not control) frame before coming here. Fields up to the | |
617 | * session-id have already been parsed and ptr points to the data | |
618 | * after the session-id. | |
fd558d18 | 619 | */ |
f7faffa3 JC |
620 | void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, |
621 | unsigned char *ptr, unsigned char *optr, u16 hdrflags, | |
622 | int length, int (*payload_hook)(struct sk_buff *skb)) | |
fd558d18 | 623 | { |
f7faffa3 | 624 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 625 | int offset; |
f7faffa3 | 626 | u32 ns, nr; |
5de7aee5 | 627 | struct l2tp_stats *sstats = &session->stats; |
fd558d18 JC |
628 | |
629 | /* The ref count is increased since we now hold a pointer to | |
630 | * the session. Take care to decrement the refcnt when exiting | |
631 | * this function from now on... | |
632 | */ | |
633 | l2tp_session_inc_refcount(session); | |
634 | if (session->ref) | |
635 | (*session->ref)(session); | |
636 | ||
f7faffa3 JC |
637 | /* Parse and check optional cookie */ |
638 | if (session->peer_cookie_len > 0) { | |
639 | if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { | |
a4ca44fa JP |
640 | l2tp_info(tunnel, L2TP_MSG_DATA, |
641 | "%s: cookie mismatch (%u/%u). Discarding.\n", | |
642 | tunnel->name, tunnel->tunnel_id, | |
643 | session->session_id); | |
5de7aee5 JC |
644 | u64_stats_update_begin(&sstats->syncp); |
645 | sstats->rx_cookie_discards++; | |
646 | u64_stats_update_end(&sstats->syncp); | |
f7faffa3 JC |
647 | goto discard; |
648 | } | |
649 | ptr += session->peer_cookie_len; | |
650 | } | |
651 | ||
fd558d18 JC |
652 | /* Handle the optional sequence numbers. Sequence numbers are |
653 | * in different places for L2TPv2 and L2TPv3. | |
654 | * | |
655 | * If we are the LAC, enable/disable sequence numbers under | |
656 | * the control of the LNS. If no sequence numbers present but | |
657 | * we were expecting them, discard frame. | |
658 | */ | |
659 | ns = nr = 0; | |
660 | L2TP_SKB_CB(skb)->has_seq = 0; | |
f7faffa3 JC |
661 | if (tunnel->version == L2TP_HDR_VER_2) { |
662 | if (hdrflags & L2TP_HDRFLAG_S) { | |
663 | ns = ntohs(*(__be16 *) ptr); | |
664 | ptr += 2; | |
665 | nr = ntohs(*(__be16 *) ptr); | |
666 | ptr += 2; | |
fd558d18 | 667 | |
f7faffa3 JC |
668 | /* Store L2TP info in the skb */ |
669 | L2TP_SKB_CB(skb)->ns = ns; | |
670 | L2TP_SKB_CB(skb)->has_seq = 1; | |
fd558d18 | 671 | |
a4ca44fa JP |
672 | l2tp_dbg(session, L2TP_MSG_SEQ, |
673 | "%s: recv data ns=%u, nr=%u, session nr=%u\n", | |
674 | session->name, ns, nr, session->nr); | |
f7faffa3 JC |
675 | } |
676 | } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { | |
677 | u32 l2h = ntohl(*(__be32 *) ptr); | |
678 | ||
679 | if (l2h & 0x40000000) { | |
680 | ns = l2h & 0x00ffffff; | |
681 | ||
682 | /* Store L2TP info in the skb */ | |
683 | L2TP_SKB_CB(skb)->ns = ns; | |
684 | L2TP_SKB_CB(skb)->has_seq = 1; | |
685 | ||
a4ca44fa JP |
686 | l2tp_dbg(session, L2TP_MSG_SEQ, |
687 | "%s: recv data ns=%u, session nr=%u\n", | |
688 | session->name, ns, session->nr); | |
f7faffa3 | 689 | } |
fd558d18 JC |
690 | } |
691 | ||
f7faffa3 JC |
692 | /* Advance past L2-specific header, if present */ |
693 | ptr += session->l2specific_len; | |
694 | ||
fd558d18 JC |
695 | if (L2TP_SKB_CB(skb)->has_seq) { |
696 | /* Received a packet with sequence numbers. If we're the LNS, | |
697 | * check if we sre sending sequence numbers and if not, | |
698 | * configure it so. | |
699 | */ | |
700 | if ((!session->lns_mode) && (!session->send_seq)) { | |
a4ca44fa JP |
701 | l2tp_info(session, L2TP_MSG_SEQ, |
702 | "%s: requested to enable seq numbers by LNS\n", | |
703 | session->name); | |
fd558d18 | 704 | session->send_seq = -1; |
f7faffa3 | 705 | l2tp_session_set_header_len(session, tunnel->version); |
fd558d18 JC |
706 | } |
707 | } else { | |
708 | /* No sequence numbers. | |
709 | * If user has configured mandatory sequence numbers, discard. | |
710 | */ | |
711 | if (session->recv_seq) { | |
a4ca44fa JP |
712 | l2tp_warn(session, L2TP_MSG_SEQ, |
713 | "%s: recv data has no seq numbers when required. Discarding.\n", | |
714 | session->name); | |
5de7aee5 JC |
715 | u64_stats_update_begin(&sstats->syncp); |
716 | sstats->rx_seq_discards++; | |
717 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
718 | goto discard; |
719 | } | |
720 | ||
721 | /* If we're the LAC and we're sending sequence numbers, the | |
722 | * LNS has requested that we no longer send sequence numbers. | |
723 | * If we're the LNS and we're sending sequence numbers, the | |
724 | * LAC is broken. Discard the frame. | |
725 | */ | |
726 | if ((!session->lns_mode) && (session->send_seq)) { | |
a4ca44fa JP |
727 | l2tp_info(session, L2TP_MSG_SEQ, |
728 | "%s: requested to disable seq numbers by LNS\n", | |
729 | session->name); | |
fd558d18 | 730 | session->send_seq = 0; |
f7faffa3 | 731 | l2tp_session_set_header_len(session, tunnel->version); |
fd558d18 | 732 | } else if (session->send_seq) { |
a4ca44fa JP |
733 | l2tp_warn(session, L2TP_MSG_SEQ, |
734 | "%s: recv data has no seq numbers when required. Discarding.\n", | |
735 | session->name); | |
5de7aee5 JC |
736 | u64_stats_update_begin(&sstats->syncp); |
737 | sstats->rx_seq_discards++; | |
738 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
739 | goto discard; |
740 | } | |
741 | } | |
742 | ||
f7faffa3 JC |
743 | /* Session data offset is handled differently for L2TPv2 and |
744 | * L2TPv3. For L2TPv2, there is an optional 16-bit value in | |
745 | * the header. For L2TPv3, the offset is negotiated using AVPs | |
746 | * in the session setup control protocol. | |
747 | */ | |
748 | if (tunnel->version == L2TP_HDR_VER_2) { | |
749 | /* If offset bit set, skip it. */ | |
750 | if (hdrflags & L2TP_HDRFLAG_O) { | |
751 | offset = ntohs(*(__be16 *)ptr); | |
752 | ptr += 2 + offset; | |
753 | } | |
754 | } else | |
755 | ptr += session->offset; | |
fd558d18 JC |
756 | |
757 | offset = ptr - optr; | |
758 | if (!pskb_may_pull(skb, offset)) | |
759 | goto discard; | |
760 | ||
761 | __skb_pull(skb, offset); | |
762 | ||
763 | /* If caller wants to process the payload before we queue the | |
764 | * packet, do so now. | |
765 | */ | |
766 | if (payload_hook) | |
767 | if ((*payload_hook)(skb)) | |
768 | goto discard; | |
769 | ||
770 | /* Prepare skb for adding to the session's reorder_q. Hold | |
771 | * packets for max reorder_timeout or 1 second if not | |
772 | * reordering. | |
773 | */ | |
774 | L2TP_SKB_CB(skb)->length = length; | |
775 | L2TP_SKB_CB(skb)->expires = jiffies + | |
776 | (session->reorder_timeout ? session->reorder_timeout : HZ); | |
777 | ||
778 | /* Add packet to the session's receive queue. Reordering is done here, if | |
779 | * enabled. Saved L2TP protocol info is stored in skb->sb[]. | |
780 | */ | |
781 | if (L2TP_SKB_CB(skb)->has_seq) { | |
782 | if (session->reorder_timeout != 0) { | |
783 | /* Packet reordering enabled. Add skb to session's | |
784 | * reorder queue, in order of ns. | |
785 | */ | |
786 | l2tp_recv_queue_skb(session, skb); | |
787 | } else { | |
788 | /* Packet reordering disabled. Discard out-of-sequence | |
789 | * packets | |
790 | */ | |
791 | if (L2TP_SKB_CB(skb)->ns != session->nr) { | |
5de7aee5 JC |
792 | u64_stats_update_begin(&sstats->syncp); |
793 | sstats->rx_seq_discards++; | |
794 | u64_stats_update_end(&sstats->syncp); | |
a4ca44fa JP |
795 | l2tp_dbg(session, L2TP_MSG_SEQ, |
796 | "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", | |
797 | session->name, L2TP_SKB_CB(skb)->ns, | |
798 | L2TP_SKB_CB(skb)->length, session->nr, | |
799 | skb_queue_len(&session->reorder_q)); | |
fd558d18 JC |
800 | goto discard; |
801 | } | |
802 | skb_queue_tail(&session->reorder_q, skb); | |
803 | } | |
804 | } else { | |
805 | /* No sequence numbers. Add the skb to the tail of the | |
806 | * reorder queue. This ensures that it will be | |
807 | * delivered after all previous sequenced skbs. | |
808 | */ | |
809 | skb_queue_tail(&session->reorder_q, skb); | |
810 | } | |
811 | ||
812 | /* Try to dequeue as many skbs from reorder_q as we can. */ | |
813 | l2tp_recv_dequeue(session); | |
814 | ||
815 | l2tp_session_dec_refcount(session); | |
816 | ||
f7faffa3 | 817 | return; |
fd558d18 JC |
818 | |
819 | discard: | |
5de7aee5 JC |
820 | u64_stats_update_begin(&sstats->syncp); |
821 | sstats->rx_errors++; | |
822 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
823 | kfree_skb(skb); |
824 | ||
825 | if (session->deref) | |
826 | (*session->deref)(session); | |
827 | ||
828 | l2tp_session_dec_refcount(session); | |
f7faffa3 JC |
829 | } |
830 | EXPORT_SYMBOL(l2tp_recv_common); | |
831 | ||
48f72f92 TP |
832 | /* Drop skbs from the session's reorder_q |
833 | */ | |
834 | int l2tp_session_queue_purge(struct l2tp_session *session) | |
835 | { | |
836 | struct sk_buff *skb = NULL; | |
837 | BUG_ON(!session); | |
838 | BUG_ON(session->magic != L2TP_SESSION_MAGIC); | |
839 | while ((skb = skb_dequeue(&session->reorder_q))) { | |
840 | atomic_long_inc(&session->stats.rx_errors); | |
841 | kfree_skb(skb); | |
842 | if (session->deref) | |
843 | (*session->deref)(session); | |
844 | } | |
845 | return 0; | |
846 | } | |
847 | EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); | |
848 | ||
f7faffa3 JC |
849 | /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame |
850 | * here. The skb is not on a list when we get here. | |
851 | * Returns 0 if the packet was a data packet and was successfully passed on. | |
852 | * Returns 1 if the packet was not a good data packet and could not be | |
853 | * forwarded. All such packets are passed up to userspace to deal with. | |
854 | */ | |
fc130840 | 855 | static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, |
856 | int (*payload_hook)(struct sk_buff *skb)) | |
f7faffa3 JC |
857 | { |
858 | struct l2tp_session *session = NULL; | |
859 | unsigned char *ptr, *optr; | |
860 | u16 hdrflags; | |
861 | u32 tunnel_id, session_id; | |
f7faffa3 JC |
862 | u16 version; |
863 | int length; | |
5de7aee5 | 864 | struct l2tp_stats *tstats; |
f7faffa3 JC |
865 | |
866 | if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb)) | |
867 | goto discard_bad_csum; | |
868 | ||
869 | /* UDP always verifies the packet length. */ | |
870 | __skb_pull(skb, sizeof(struct udphdr)); | |
871 | ||
872 | /* Short packet? */ | |
873 | if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { | |
a4ca44fa JP |
874 | l2tp_info(tunnel, L2TP_MSG_DATA, |
875 | "%s: recv short packet (len=%d)\n", | |
876 | tunnel->name, skb->len); | |
f7faffa3 JC |
877 | goto error; |
878 | } | |
879 | ||
f7faffa3 JC |
880 | /* Trace packet contents, if enabled */ |
881 | if (tunnel->debug & L2TP_MSG_DATA) { | |
882 | length = min(32u, skb->len); | |
883 | if (!pskb_may_pull(skb, length)) | |
884 | goto error; | |
885 | ||
a4ca44fa JP |
886 | pr_debug("%s: recv\n", tunnel->name); |
887 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); | |
f7faffa3 JC |
888 | } |
889 | ||
e50e705c ED |
890 | /* Point to L2TP header */ |
891 | optr = ptr = skb->data; | |
892 | ||
f7faffa3 JC |
893 | /* Get L2TP header flags */ |
894 | hdrflags = ntohs(*(__be16 *) ptr); | |
895 | ||
896 | /* Check protocol version */ | |
897 | version = hdrflags & L2TP_HDR_VER_MASK; | |
898 | if (version != tunnel->version) { | |
a4ca44fa JP |
899 | l2tp_info(tunnel, L2TP_MSG_DATA, |
900 | "%s: recv protocol version mismatch: got %d expected %d\n", | |
901 | tunnel->name, version, tunnel->version); | |
f7faffa3 JC |
902 | goto error; |
903 | } | |
904 | ||
905 | /* Get length of L2TP packet */ | |
906 | length = skb->len; | |
907 | ||
908 | /* If type is control packet, it is handled by userspace. */ | |
909 | if (hdrflags & L2TP_HDRFLAG_T) { | |
a4ca44fa JP |
910 | l2tp_dbg(tunnel, L2TP_MSG_DATA, |
911 | "%s: recv control packet, len=%d\n", | |
912 | tunnel->name, length); | |
f7faffa3 JC |
913 | goto error; |
914 | } | |
915 | ||
916 | /* Skip flags */ | |
917 | ptr += 2; | |
918 | ||
919 | if (tunnel->version == L2TP_HDR_VER_2) { | |
920 | /* If length is present, skip it */ | |
921 | if (hdrflags & L2TP_HDRFLAG_L) | |
922 | ptr += 2; | |
923 | ||
924 | /* Extract tunnel and session ID */ | |
925 | tunnel_id = ntohs(*(__be16 *) ptr); | |
926 | ptr += 2; | |
927 | session_id = ntohs(*(__be16 *) ptr); | |
928 | ptr += 2; | |
929 | } else { | |
930 | ptr += 2; /* skip reserved bits */ | |
931 | tunnel_id = tunnel->tunnel_id; | |
932 | session_id = ntohl(*(__be32 *) ptr); | |
933 | ptr += 4; | |
934 | } | |
935 | ||
936 | /* Find the session context */ | |
937 | session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); | |
309795f4 | 938 | if (!session || !session->recv_skb) { |
f7faffa3 | 939 | /* Not found? Pass to userspace to deal with */ |
a4ca44fa JP |
940 | l2tp_info(tunnel, L2TP_MSG_DATA, |
941 | "%s: no session found (%u/%u). Passing up.\n", | |
942 | tunnel->name, tunnel_id, session_id); | |
f7faffa3 JC |
943 | goto error; |
944 | } | |
945 | ||
946 | l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); | |
fd558d18 JC |
947 | |
948 | return 0; | |
949 | ||
950 | discard_bad_csum: | |
951 | LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name); | |
952 | UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0); | |
5de7aee5 JC |
953 | tstats = &tunnel->stats; |
954 | u64_stats_update_begin(&tstats->syncp); | |
955 | tstats->rx_errors++; | |
956 | u64_stats_update_end(&tstats->syncp); | |
fd558d18 JC |
957 | kfree_skb(skb); |
958 | ||
959 | return 0; | |
960 | ||
961 | error: | |
962 | /* Put UDP header back */ | |
963 | __skb_push(skb, sizeof(struct udphdr)); | |
964 | ||
965 | return 1; | |
966 | } | |
fd558d18 JC |
967 | |
968 | /* UDP encapsulation receive handler. See net/ipv4/udp.c. | |
969 | * Return codes: | |
970 | * 0 : success. | |
971 | * <0: error | |
972 | * >0: skb should be passed up to userspace as UDP. | |
973 | */ | |
974 | int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
975 | { | |
976 | struct l2tp_tunnel *tunnel; | |
977 | ||
978 | tunnel = l2tp_sock_to_tunnel(sk); | |
979 | if (tunnel == NULL) | |
980 | goto pass_up; | |
981 | ||
a4ca44fa JP |
982 | l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", |
983 | tunnel->name, skb->len); | |
fd558d18 JC |
984 | |
985 | if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) | |
986 | goto pass_up_put; | |
987 | ||
988 | sock_put(sk); | |
989 | return 0; | |
990 | ||
991 | pass_up_put: | |
992 | sock_put(sk); | |
993 | pass_up: | |
994 | return 1; | |
995 | } | |
996 | EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); | |
997 | ||
998 | /************************************************************************ | |
999 | * Transmit handling | |
1000 | ***********************************************************************/ | |
1001 | ||
1002 | /* Build an L2TP header for the session into the buffer provided. | |
1003 | */ | |
f7faffa3 | 1004 | static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) |
fd558d18 | 1005 | { |
f7faffa3 | 1006 | struct l2tp_tunnel *tunnel = session->tunnel; |
fd558d18 | 1007 | __be16 *bufp = buf; |
f7faffa3 | 1008 | __be16 *optr = buf; |
fd558d18 JC |
1009 | u16 flags = L2TP_HDR_VER_2; |
1010 | u32 tunnel_id = tunnel->peer_tunnel_id; | |
1011 | u32 session_id = session->peer_session_id; | |
1012 | ||
1013 | if (session->send_seq) | |
1014 | flags |= L2TP_HDRFLAG_S; | |
1015 | ||
1016 | /* Setup L2TP header. */ | |
1017 | *bufp++ = htons(flags); | |
1018 | *bufp++ = htons(tunnel_id); | |
1019 | *bufp++ = htons(session_id); | |
1020 | if (session->send_seq) { | |
1021 | *bufp++ = htons(session->ns); | |
1022 | *bufp++ = 0; | |
1023 | session->ns++; | |
f7faffa3 | 1024 | session->ns &= 0xffff; |
a4ca44fa JP |
1025 | l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", |
1026 | session->name, session->ns); | |
fd558d18 | 1027 | } |
f7faffa3 JC |
1028 | |
1029 | return bufp - optr; | |
fd558d18 JC |
1030 | } |
1031 | ||
f7faffa3 | 1032 | static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) |
fd558d18 | 1033 | { |
0d76751f | 1034 | struct l2tp_tunnel *tunnel = session->tunnel; |
f7faffa3 JC |
1035 | char *bufp = buf; |
1036 | char *optr = bufp; | |
f7faffa3 | 1037 | |
0d76751f JC |
1038 | /* Setup L2TP header. The header differs slightly for UDP and |
1039 | * IP encapsulations. For UDP, there is 4 bytes of flags. | |
1040 | */ | |
1041 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { | |
1042 | u16 flags = L2TP_HDR_VER_3; | |
1043 | *((__be16 *) bufp) = htons(flags); | |
1044 | bufp += 2; | |
1045 | *((__be16 *) bufp) = 0; | |
1046 | bufp += 2; | |
1047 | } | |
1048 | ||
f7faffa3 JC |
1049 | *((__be32 *) bufp) = htonl(session->peer_session_id); |
1050 | bufp += 4; | |
1051 | if (session->cookie_len) { | |
1052 | memcpy(bufp, &session->cookie[0], session->cookie_len); | |
1053 | bufp += session->cookie_len; | |
1054 | } | |
1055 | if (session->l2specific_len) { | |
1056 | if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { | |
1057 | u32 l2h = 0; | |
1058 | if (session->send_seq) { | |
1059 | l2h = 0x40000000 | session->ns; | |
1060 | session->ns++; | |
1061 | session->ns &= 0xffffff; | |
a4ca44fa JP |
1062 | l2tp_dbg(session, L2TP_MSG_SEQ, |
1063 | "%s: updated ns to %u\n", | |
1064 | session->name, session->ns); | |
f7faffa3 JC |
1065 | } |
1066 | ||
1067 | *((__be32 *) bufp) = htonl(l2h); | |
1068 | } | |
1069 | bufp += session->l2specific_len; | |
1070 | } | |
1071 | if (session->offset) | |
1072 | bufp += session->offset; | |
fd558d18 | 1073 | |
f7faffa3 | 1074 | return bufp - optr; |
fd558d18 | 1075 | } |
fd558d18 | 1076 | |
fc130840 | 1077 | static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, |
d9d8da80 | 1078 | struct flowi *fl, size_t data_len) |
fd558d18 JC |
1079 | { |
1080 | struct l2tp_tunnel *tunnel = session->tunnel; | |
1081 | unsigned int len = skb->len; | |
1082 | int error; | |
5de7aee5 | 1083 | struct l2tp_stats *tstats, *sstats; |
fd558d18 JC |
1084 | |
1085 | /* Debug */ | |
1086 | if (session->send_seq) | |
a4ca44fa JP |
1087 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n", |
1088 | session->name, data_len, session->ns - 1); | |
fd558d18 | 1089 | else |
a4ca44fa JP |
1090 | l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n", |
1091 | session->name, data_len); | |
fd558d18 JC |
1092 | |
1093 | if (session->debug & L2TP_MSG_DATA) { | |
0d76751f JC |
1094 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
1095 | unsigned char *datap = skb->data + uhlen; | |
fd558d18 | 1096 | |
a4ca44fa JP |
1097 | pr_debug("%s: xmit\n", session->name); |
1098 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, | |
1099 | datap, min_t(size_t, 32, len - uhlen)); | |
fd558d18 JC |
1100 | } |
1101 | ||
1102 | /* Queue the packet to IP for output */ | |
4e15ed4d | 1103 | skb->local_df = 1; |
d2cf3361 BL |
1104 | #if IS_ENABLED(CONFIG_IPV6) |
1105 | if (skb->sk->sk_family == PF_INET6) | |
1106 | error = inet6_csk_xmit(skb, NULL); | |
1107 | else | |
1108 | #endif | |
1109 | error = ip_queue_xmit(skb, fl); | |
fd558d18 JC |
1110 | |
1111 | /* Update stats */ | |
5de7aee5 JC |
1112 | tstats = &tunnel->stats; |
1113 | u64_stats_update_begin(&tstats->syncp); | |
1114 | sstats = &session->stats; | |
1115 | u64_stats_update_begin(&sstats->syncp); | |
fd558d18 | 1116 | if (error >= 0) { |
5de7aee5 JC |
1117 | tstats->tx_packets++; |
1118 | tstats->tx_bytes += len; | |
1119 | sstats->tx_packets++; | |
1120 | sstats->tx_bytes += len; | |
fd558d18 | 1121 | } else { |
5de7aee5 JC |
1122 | tstats->tx_errors++; |
1123 | sstats->tx_errors++; | |
fd558d18 | 1124 | } |
5de7aee5 JC |
1125 | u64_stats_update_end(&tstats->syncp); |
1126 | u64_stats_update_end(&sstats->syncp); | |
fd558d18 JC |
1127 | |
1128 | return 0; | |
1129 | } | |
fd558d18 JC |
1130 | |
1131 | /* Automatically called when the skb is freed. | |
1132 | */ | |
1133 | static void l2tp_sock_wfree(struct sk_buff *skb) | |
1134 | { | |
1135 | sock_put(skb->sk); | |
1136 | } | |
1137 | ||
1138 | /* For data skbs that we transmit, we associate with the tunnel socket | |
1139 | * but don't do accounting. | |
1140 | */ | |
1141 | static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk) | |
1142 | { | |
1143 | sock_hold(sk); | |
1144 | skb->sk = sk; | |
1145 | skb->destructor = l2tp_sock_wfree; | |
1146 | } | |
1147 | ||
d2cf3361 BL |
1148 | #if IS_ENABLED(CONFIG_IPV6) |
1149 | static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb, | |
1150 | int udp_len) | |
1151 | { | |
1152 | struct ipv6_pinfo *np = inet6_sk(sk); | |
1153 | struct udphdr *uh = udp_hdr(skb); | |
1154 | ||
1155 | if (!skb_dst(skb) || !skb_dst(skb)->dev || | |
1156 | !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) { | |
1157 | __wsum csum = skb_checksum(skb, 0, udp_len, 0); | |
1158 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1159 | uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len, | |
1160 | IPPROTO_UDP, csum); | |
1161 | if (uh->check == 0) | |
1162 | uh->check = CSUM_MANGLED_0; | |
1163 | } else { | |
1164 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1165 | skb->csum_start = skb_transport_header(skb) - skb->head; | |
1166 | skb->csum_offset = offsetof(struct udphdr, check); | |
1167 | uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr, | |
1168 | udp_len, IPPROTO_UDP, 0); | |
1169 | } | |
1170 | } | |
1171 | #endif | |
1172 | ||
fd558d18 JC |
1173 | /* If caller requires the skb to have a ppp header, the header must be |
1174 | * inserted in the skb data before calling this function. | |
1175 | */ | |
1176 | int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) | |
1177 | { | |
1178 | int data_len = skb->len; | |
0d76751f JC |
1179 | struct l2tp_tunnel *tunnel = session->tunnel; |
1180 | struct sock *sk = tunnel->sock; | |
d9d8da80 | 1181 | struct flowi *fl; |
fd558d18 | 1182 | struct udphdr *uh; |
fd558d18 JC |
1183 | struct inet_sock *inet; |
1184 | __wsum csum; | |
fd558d18 | 1185 | int headroom; |
0d76751f JC |
1186 | int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
1187 | int udp_len; | |
b8c84307 | 1188 | int ret = NET_XMIT_SUCCESS; |
fd558d18 JC |
1189 | |
1190 | /* Check that there's enough headroom in the skb to insert IP, | |
1191 | * UDP and L2TP headers. If not enough, expand it to | |
1192 | * make room. Adjust truesize. | |
1193 | */ | |
1194 | headroom = NET_SKB_PAD + sizeof(struct iphdr) + | |
0d76751f | 1195 | uhlen + hdr_len; |
835acf5d | 1196 | if (skb_cow_head(skb, headroom)) { |
b8c84307 ED |
1197 | kfree_skb(skb); |
1198 | return NET_XMIT_DROP; | |
835acf5d | 1199 | } |
fd558d18 | 1200 | |
fd558d18 | 1201 | skb_orphan(skb); |
fd558d18 | 1202 | /* Setup L2TP header */ |
f7faffa3 | 1203 | session->build_header(session, __skb_push(skb, hdr_len)); |
fd558d18 | 1204 | |
0d76751f | 1205 | /* Reset skb netfilter state */ |
fd558d18 JC |
1206 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
1207 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | |
1208 | IPSKB_REROUTED); | |
1209 | nf_reset(skb); | |
1210 | ||
6af88da1 DM |
1211 | bh_lock_sock(sk); |
1212 | if (sock_owned_by_user(sk)) { | |
b8c84307 ED |
1213 | kfree_skb(skb); |
1214 | ret = NET_XMIT_DROP; | |
6af88da1 DM |
1215 | goto out_unlock; |
1216 | } | |
1217 | ||
fd558d18 JC |
1218 | /* Get routing info from the tunnel socket */ |
1219 | skb_dst_drop(skb); | |
71b1391a | 1220 | skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); |
fd558d18 | 1221 | |
d9d8da80 DM |
1222 | inet = inet_sk(sk); |
1223 | fl = &inet->cork.fl; | |
0d76751f JC |
1224 | switch (tunnel->encap) { |
1225 | case L2TP_ENCAPTYPE_UDP: | |
1226 | /* Setup UDP header */ | |
0d76751f JC |
1227 | __skb_push(skb, sizeof(*uh)); |
1228 | skb_reset_transport_header(skb); | |
1229 | uh = udp_hdr(skb); | |
1230 | uh->source = inet->inet_sport; | |
1231 | uh->dest = inet->inet_dport; | |
1232 | udp_len = uhlen + hdr_len + data_len; | |
1233 | uh->len = htons(udp_len); | |
1234 | uh->check = 0; | |
1235 | ||
1236 | /* Calculate UDP checksum if configured to do so */ | |
d2cf3361 BL |
1237 | #if IS_ENABLED(CONFIG_IPV6) |
1238 | if (sk->sk_family == PF_INET6) | |
1239 | l2tp_xmit_ipv6_csum(sk, skb, udp_len); | |
1240 | else | |
1241 | #endif | |
0d76751f JC |
1242 | if (sk->sk_no_check == UDP_CSUM_NOXMIT) |
1243 | skb->ip_summed = CHECKSUM_NONE; | |
1244 | else if ((skb_dst(skb) && skb_dst(skb)->dev) && | |
1245 | (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) { | |
1246 | skb->ip_summed = CHECKSUM_COMPLETE; | |
1247 | csum = skb_checksum(skb, 0, udp_len, 0); | |
1248 | uh->check = csum_tcpudp_magic(inet->inet_saddr, | |
1249 | inet->inet_daddr, | |
1250 | udp_len, IPPROTO_UDP, csum); | |
1251 | if (uh->check == 0) | |
1252 | uh->check = CSUM_MANGLED_0; | |
1253 | } else { | |
1254 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1255 | skb->csum_start = skb_transport_header(skb) - skb->head; | |
1256 | skb->csum_offset = offsetof(struct udphdr, check); | |
1257 | uh->check = ~csum_tcpudp_magic(inet->inet_saddr, | |
1258 | inet->inet_daddr, | |
1259 | udp_len, IPPROTO_UDP, 0); | |
1260 | } | |
1261 | break; | |
1262 | ||
1263 | case L2TP_ENCAPTYPE_IP: | |
1264 | break; | |
fd558d18 JC |
1265 | } |
1266 | ||
0d76751f JC |
1267 | l2tp_skb_set_owner_w(skb, sk); |
1268 | ||
d9d8da80 | 1269 | l2tp_xmit_core(session, skb, fl, data_len); |
6af88da1 DM |
1270 | out_unlock: |
1271 | bh_unlock_sock(sk); | |
fd558d18 | 1272 | |
b8c84307 | 1273 | return ret; |
fd558d18 JC |
1274 | } |
1275 | EXPORT_SYMBOL_GPL(l2tp_xmit_skb); | |
1276 | ||
1277 | /***************************************************************************** | |
1278 | * Tinnel and session create/destroy. | |
1279 | *****************************************************************************/ | |
1280 | ||
1281 | /* Tunnel socket destruct hook. | |
1282 | * The tunnel context is deleted only when all session sockets have been | |
1283 | * closed. | |
1284 | */ | |
fc130840 | 1285 | static void l2tp_tunnel_destruct(struct sock *sk) |
fd558d18 JC |
1286 | { |
1287 | struct l2tp_tunnel *tunnel; | |
f8ccac0e | 1288 | struct l2tp_net *pn; |
fd558d18 JC |
1289 | |
1290 | tunnel = sk->sk_user_data; | |
1291 | if (tunnel == NULL) | |
1292 | goto end; | |
1293 | ||
a4ca44fa | 1294 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); |
fd558d18 | 1295 | |
fd558d18 | 1296 | |
f8ccac0e | 1297 | /* Disable udp encapsulation */ |
0d76751f JC |
1298 | switch (tunnel->encap) { |
1299 | case L2TP_ENCAPTYPE_UDP: | |
1300 | /* No longer an encapsulation socket. See net/ipv4/udp.c */ | |
1301 | (udp_sk(sk))->encap_type = 0; | |
1302 | (udp_sk(sk))->encap_rcv = NULL; | |
9980d001 | 1303 | (udp_sk(sk))->encap_destroy = NULL; |
0d76751f JC |
1304 | break; |
1305 | case L2TP_ENCAPTYPE_IP: | |
1306 | break; | |
1307 | } | |
fd558d18 JC |
1308 | |
1309 | /* Remove hooks into tunnel socket */ | |
fd558d18 JC |
1310 | sk->sk_destruct = tunnel->old_sk_destruct; |
1311 | sk->sk_user_data = NULL; | |
f8ccac0e | 1312 | tunnel->sock = NULL; |
fd558d18 | 1313 | |
f8ccac0e TP |
1314 | /* Remove the tunnel struct from the tunnel list */ |
1315 | pn = l2tp_pernet(tunnel->l2tp_net); | |
1316 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); | |
1317 | list_del_rcu(&tunnel->list); | |
1318 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
1319 | atomic_dec(&l2tp_tunnel_count); | |
fd558d18 | 1320 | |
f8ccac0e | 1321 | l2tp_tunnel_closeall(tunnel); |
fd558d18 JC |
1322 | l2tp_tunnel_dec_refcount(tunnel); |
1323 | ||
f8ccac0e TP |
1324 | /* Call the original destructor */ |
1325 | if (sk->sk_destruct) | |
1326 | (*sk->sk_destruct)(sk); | |
fd558d18 JC |
1327 | end: |
1328 | return; | |
1329 | } | |
fd558d18 JC |
1330 | |
1331 | /* When the tunnel is closed, all the attached sessions need to go too. | |
1332 | */ | |
e34f4c70 | 1333 | void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) |
fd558d18 JC |
1334 | { |
1335 | int hash; | |
1336 | struct hlist_node *walk; | |
1337 | struct hlist_node *tmp; | |
1338 | struct l2tp_session *session; | |
1339 | ||
1340 | BUG_ON(tunnel == NULL); | |
1341 | ||
a4ca44fa JP |
1342 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", |
1343 | tunnel->name); | |
fd558d18 JC |
1344 | |
1345 | write_lock_bh(&tunnel->hlist_lock); | |
1346 | for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { | |
1347 | again: | |
1348 | hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { | |
1349 | session = hlist_entry(walk, struct l2tp_session, hlist); | |
1350 | ||
a4ca44fa JP |
1351 | l2tp_info(session, L2TP_MSG_CONTROL, |
1352 | "%s: closing session\n", session->name); | |
fd558d18 JC |
1353 | |
1354 | hlist_del_init(&session->hlist); | |
1355 | ||
1356 | /* Since we should hold the sock lock while | |
1357 | * doing any unbinding, we need to release the | |
1358 | * lock we're holding before taking that lock. | |
1359 | * Hold a reference to the sock so it doesn't | |
1360 | * disappear as we're jumping between locks. | |
1361 | */ | |
1362 | if (session->ref != NULL) | |
1363 | (*session->ref)(session); | |
1364 | ||
1365 | write_unlock_bh(&tunnel->hlist_lock); | |
1366 | ||
f7faffa3 JC |
1367 | if (tunnel->version != L2TP_HDR_VER_2) { |
1368 | struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); | |
1369 | ||
e02d494d JC |
1370 | spin_lock_bh(&pn->l2tp_session_hlist_lock); |
1371 | hlist_del_init_rcu(&session->global_hlist); | |
1372 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); | |
1373 | synchronize_rcu(); | |
f7faffa3 JC |
1374 | } |
1375 | ||
fd558d18 JC |
1376 | if (session->session_close != NULL) |
1377 | (*session->session_close)(session); | |
1378 | ||
1379 | if (session->deref != NULL) | |
1380 | (*session->deref)(session); | |
1381 | ||
9980d001 TP |
1382 | l2tp_session_dec_refcount(session); |
1383 | ||
fd558d18 JC |
1384 | write_lock_bh(&tunnel->hlist_lock); |
1385 | ||
1386 | /* Now restart from the beginning of this hash | |
1387 | * chain. We always remove a session from the | |
1388 | * list so we are guaranteed to make forward | |
1389 | * progress. | |
1390 | */ | |
1391 | goto again; | |
1392 | } | |
1393 | } | |
1394 | write_unlock_bh(&tunnel->hlist_lock); | |
1395 | } | |
e34f4c70 | 1396 | EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); |
fd558d18 | 1397 | |
9980d001 TP |
1398 | /* Tunnel socket destroy hook for UDP encapsulation */ |
1399 | static void l2tp_udp_encap_destroy(struct sock *sk) | |
1400 | { | |
1401 | struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); | |
1402 | if (tunnel) { | |
1403 | l2tp_tunnel_closeall(tunnel); | |
1404 | sock_put(sk); | |
1405 | } | |
1406 | } | |
1407 | ||
fd558d18 JC |
1408 | /* Really kill the tunnel. |
1409 | * Come here only when all sessions have been cleared from the tunnel. | |
1410 | */ | |
fc130840 | 1411 | static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) |
fd558d18 | 1412 | { |
fd558d18 JC |
1413 | BUG_ON(atomic_read(&tunnel->ref_count) != 0); |
1414 | BUG_ON(tunnel->sock != NULL); | |
a4ca44fa | 1415 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name); |
99469c32 | 1416 | kfree_rcu(tunnel, rcu); |
f8ccac0e | 1417 | } |
fd558d18 | 1418 | |
f8ccac0e TP |
1419 | /* Workqueue tunnel deletion function */ |
1420 | static void l2tp_tunnel_del_work(struct work_struct *work) | |
1421 | { | |
1422 | struct l2tp_tunnel *tunnel = NULL; | |
1423 | struct socket *sock = NULL; | |
1424 | struct sock *sk = NULL; | |
1425 | ||
1426 | tunnel = container_of(work, struct l2tp_tunnel, del_work); | |
1427 | sk = l2tp_tunnel_sock_lookup(tunnel); | |
1428 | if (!sk) | |
1429 | return; | |
1430 | ||
1431 | sock = sk->sk_socket; | |
f8ccac0e | 1432 | |
02d13ed5 TP |
1433 | /* If the tunnel socket was created by userspace, then go through the |
1434 | * inet layer to shut the socket down, and let userspace close it. | |
1435 | * Otherwise, if we created the socket directly within the kernel, use | |
1436 | * the sk API to release it here. | |
167eb17e TP |
1437 | * In either case the tunnel resources are freed in the socket |
1438 | * destructor when the tunnel socket goes away. | |
f8ccac0e | 1439 | */ |
02d13ed5 TP |
1440 | if (tunnel->fd >= 0) { |
1441 | if (sock) | |
1442 | inet_shutdown(sock, 2); | |
167eb17e | 1443 | } else { |
02d13ed5 TP |
1444 | if (sock) |
1445 | kernel_sock_shutdown(sock, SHUT_RDWR); | |
1446 | sk_release_kernel(sk); | |
167eb17e | 1447 | } |
f8ccac0e TP |
1448 | |
1449 | l2tp_tunnel_sock_put(sk); | |
fd558d18 | 1450 | } |
fd558d18 | 1451 | |
789a4a2c JC |
1452 | /* Create a socket for the tunnel, if one isn't set up by |
1453 | * userspace. This is used for static tunnels where there is no | |
1454 | * managing L2TP daemon. | |
167eb17e TP |
1455 | * |
1456 | * Since we don't want these sockets to keep a namespace alive by | |
1457 | * themselves, we drop the socket's namespace refcount after creation. | |
1458 | * These sockets are freed when the namespace exits using the pernet | |
1459 | * exit hook. | |
789a4a2c | 1460 | */ |
167eb17e TP |
1461 | static int l2tp_tunnel_sock_create(struct net *net, |
1462 | u32 tunnel_id, | |
1463 | u32 peer_tunnel_id, | |
1464 | struct l2tp_tunnel_cfg *cfg, | |
1465 | struct socket **sockp) | |
789a4a2c JC |
1466 | { |
1467 | int err = -EINVAL; | |
167eb17e TP |
1468 | struct socket *sock = NULL; |
1469 | struct sockaddr_in udp_addr = {0}; | |
1470 | struct sockaddr_l2tpip ip_addr = {0}; | |
f9bac8df | 1471 | #if IS_ENABLED(CONFIG_IPV6) |
167eb17e TP |
1472 | struct sockaddr_in6 udp6_addr = {0}; |
1473 | struct sockaddr_l2tpip6 ip6_addr = {0}; | |
f9bac8df | 1474 | #endif |
789a4a2c JC |
1475 | |
1476 | switch (cfg->encap) { | |
1477 | case L2TP_ENCAPTYPE_UDP: | |
f9bac8df CE |
1478 | #if IS_ENABLED(CONFIG_IPV6) |
1479 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
167eb17e | 1480 | err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock); |
f9bac8df CE |
1481 | if (err < 0) |
1482 | goto out; | |
789a4a2c | 1483 | |
167eb17e | 1484 | sk_change_net(sock->sk, net); |
789a4a2c | 1485 | |
f9bac8df CE |
1486 | udp6_addr.sin6_family = AF_INET6; |
1487 | memcpy(&udp6_addr.sin6_addr, cfg->local_ip6, | |
1488 | sizeof(udp6_addr.sin6_addr)); | |
1489 | udp6_addr.sin6_port = htons(cfg->local_udp_port); | |
1490 | err = kernel_bind(sock, (struct sockaddr *) &udp6_addr, | |
1491 | sizeof(udp6_addr)); | |
1492 | if (err < 0) | |
1493 | goto out; | |
789a4a2c | 1494 | |
f9bac8df CE |
1495 | udp6_addr.sin6_family = AF_INET6; |
1496 | memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6, | |
1497 | sizeof(udp6_addr.sin6_addr)); | |
1498 | udp6_addr.sin6_port = htons(cfg->peer_udp_port); | |
1499 | err = kernel_connect(sock, | |
1500 | (struct sockaddr *) &udp6_addr, | |
1501 | sizeof(udp6_addr), 0); | |
1502 | if (err < 0) | |
1503 | goto out; | |
1504 | } else | |
1505 | #endif | |
1506 | { | |
167eb17e | 1507 | err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock); |
f9bac8df CE |
1508 | if (err < 0) |
1509 | goto out; | |
1510 | ||
167eb17e | 1511 | sk_change_net(sock->sk, net); |
f9bac8df | 1512 | |
f9bac8df CE |
1513 | udp_addr.sin_family = AF_INET; |
1514 | udp_addr.sin_addr = cfg->local_ip; | |
1515 | udp_addr.sin_port = htons(cfg->local_udp_port); | |
1516 | err = kernel_bind(sock, (struct sockaddr *) &udp_addr, | |
1517 | sizeof(udp_addr)); | |
1518 | if (err < 0) | |
1519 | goto out; | |
1520 | ||
1521 | udp_addr.sin_family = AF_INET; | |
1522 | udp_addr.sin_addr = cfg->peer_ip; | |
1523 | udp_addr.sin_port = htons(cfg->peer_udp_port); | |
1524 | err = kernel_connect(sock, | |
1525 | (struct sockaddr *) &udp_addr, | |
1526 | sizeof(udp_addr), 0); | |
1527 | if (err < 0) | |
1528 | goto out; | |
1529 | } | |
789a4a2c JC |
1530 | |
1531 | if (!cfg->use_udp_checksums) | |
1532 | sock->sk->sk_no_check = UDP_CSUM_NOXMIT; | |
1533 | ||
1534 | break; | |
1535 | ||
1536 | case L2TP_ENCAPTYPE_IP: | |
f9bac8df CE |
1537 | #if IS_ENABLED(CONFIG_IPV6) |
1538 | if (cfg->local_ip6 && cfg->peer_ip6) { | |
167eb17e TP |
1539 | err = sock_create_kern(AF_INET6, SOCK_DGRAM, |
1540 | IPPROTO_L2TP, &sock); | |
5dac94e1 JC |
1541 | if (err < 0) |
1542 | goto out; | |
789a4a2c | 1543 | |
167eb17e | 1544 | sk_change_net(sock->sk, net); |
789a4a2c | 1545 | |
5dac94e1 JC |
1546 | ip6_addr.l2tp_family = AF_INET6; |
1547 | memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, | |
1548 | sizeof(ip6_addr.l2tp_addr)); | |
1549 | ip6_addr.l2tp_conn_id = tunnel_id; | |
1550 | err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, | |
1551 | sizeof(ip6_addr)); | |
1552 | if (err < 0) | |
1553 | goto out; | |
789a4a2c | 1554 | |
5dac94e1 JC |
1555 | ip6_addr.l2tp_family = AF_INET6; |
1556 | memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, | |
1557 | sizeof(ip6_addr.l2tp_addr)); | |
1558 | ip6_addr.l2tp_conn_id = peer_tunnel_id; | |
1559 | err = kernel_connect(sock, | |
1560 | (struct sockaddr *) &ip6_addr, | |
1561 | sizeof(ip6_addr), 0); | |
1562 | if (err < 0) | |
1563 | goto out; | |
1564 | } else | |
1565 | #endif | |
1566 | { | |
167eb17e TP |
1567 | err = sock_create_kern(AF_INET, SOCK_DGRAM, |
1568 | IPPROTO_L2TP, &sock); | |
5dac94e1 JC |
1569 | if (err < 0) |
1570 | goto out; | |
789a4a2c | 1571 | |
167eb17e | 1572 | sk_change_net(sock->sk, net); |
5dac94e1 | 1573 | |
5dac94e1 JC |
1574 | ip_addr.l2tp_family = AF_INET; |
1575 | ip_addr.l2tp_addr = cfg->local_ip; | |
1576 | ip_addr.l2tp_conn_id = tunnel_id; | |
1577 | err = kernel_bind(sock, (struct sockaddr *) &ip_addr, | |
1578 | sizeof(ip_addr)); | |
1579 | if (err < 0) | |
1580 | goto out; | |
1581 | ||
1582 | ip_addr.l2tp_family = AF_INET; | |
1583 | ip_addr.l2tp_addr = cfg->peer_ip; | |
1584 | ip_addr.l2tp_conn_id = peer_tunnel_id; | |
1585 | err = kernel_connect(sock, (struct sockaddr *) &ip_addr, | |
1586 | sizeof(ip_addr), 0); | |
1587 | if (err < 0) | |
1588 | goto out; | |
1589 | } | |
789a4a2c JC |
1590 | break; |
1591 | ||
1592 | default: | |
1593 | goto out; | |
1594 | } | |
1595 | ||
1596 | out: | |
167eb17e | 1597 | *sockp = sock; |
789a4a2c | 1598 | if ((err < 0) && sock) { |
167eb17e TP |
1599 | kernel_sock_shutdown(sock, SHUT_RDWR); |
1600 | sk_release_kernel(sock->sk); | |
789a4a2c JC |
1601 | *sockp = NULL; |
1602 | } | |
1603 | ||
1604 | return err; | |
1605 | } | |
1606 | ||
37159ef2 ED |
1607 | static struct lock_class_key l2tp_socket_class; |
1608 | ||
fd558d18 JC |
1609 | 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) |
1610 | { | |
1611 | struct l2tp_tunnel *tunnel = NULL; | |
1612 | int err; | |
1613 | struct socket *sock = NULL; | |
1614 | struct sock *sk = NULL; | |
1615 | struct l2tp_net *pn; | |
0d76751f | 1616 | enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; |
fd558d18 JC |
1617 | |
1618 | /* Get the tunnel socket from the fd, which was opened by | |
789a4a2c JC |
1619 | * the userspace L2TP daemon. If not specified, create a |
1620 | * kernel socket. | |
fd558d18 | 1621 | */ |
789a4a2c | 1622 | if (fd < 0) { |
167eb17e TP |
1623 | err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, |
1624 | cfg, &sock); | |
789a4a2c JC |
1625 | if (err < 0) |
1626 | goto err; | |
1627 | } else { | |
789a4a2c JC |
1628 | sock = sockfd_lookup(fd, &err); |
1629 | if (!sock) { | |
cbb95e0c | 1630 | pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", |
789a4a2c | 1631 | tunnel_id, fd, err); |
cbb95e0c TP |
1632 | err = -EBADF; |
1633 | goto err; | |
1634 | } | |
1635 | ||
1636 | /* Reject namespace mismatches */ | |
1637 | if (!net_eq(sock_net(sock->sk), net)) { | |
1638 | pr_err("tunl %u: netns mismatch\n", tunnel_id); | |
1639 | err = -EINVAL; | |
789a4a2c JC |
1640 | goto err; |
1641 | } | |
fd558d18 JC |
1642 | } |
1643 | ||
1644 | sk = sock->sk; | |
1645 | ||
0d76751f JC |
1646 | if (cfg != NULL) |
1647 | encap = cfg->encap; | |
1648 | ||
fd558d18 | 1649 | /* Quick sanity checks */ |
0d76751f JC |
1650 | switch (encap) { |
1651 | case L2TP_ENCAPTYPE_UDP: | |
1652 | err = -EPROTONOSUPPORT; | |
1653 | if (sk->sk_protocol != IPPROTO_UDP) { | |
a4ca44fa | 1654 | pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", |
0d76751f JC |
1655 | tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); |
1656 | goto err; | |
1657 | } | |
1658 | break; | |
1659 | case L2TP_ENCAPTYPE_IP: | |
1660 | err = -EPROTONOSUPPORT; | |
1661 | if (sk->sk_protocol != IPPROTO_L2TP) { | |
a4ca44fa | 1662 | pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", |
0d76751f JC |
1663 | tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); |
1664 | goto err; | |
1665 | } | |
1666 | break; | |
fd558d18 JC |
1667 | } |
1668 | ||
1669 | /* Check if this socket has already been prepped */ | |
1670 | tunnel = (struct l2tp_tunnel *)sk->sk_user_data; | |
1671 | if (tunnel != NULL) { | |
1672 | /* This socket has already been prepped */ | |
1673 | err = -EBUSY; | |
1674 | goto err; | |
1675 | } | |
1676 | ||
fd558d18 JC |
1677 | tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); |
1678 | if (tunnel == NULL) { | |
1679 | err = -ENOMEM; | |
1680 | goto err; | |
1681 | } | |
1682 | ||
1683 | tunnel->version = version; | |
1684 | tunnel->tunnel_id = tunnel_id; | |
1685 | tunnel->peer_tunnel_id = peer_tunnel_id; | |
1686 | tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; | |
1687 | ||
1688 | tunnel->magic = L2TP_TUNNEL_MAGIC; | |
1689 | sprintf(&tunnel->name[0], "tunl %u", tunnel_id); | |
1690 | rwlock_init(&tunnel->hlist_lock); | |
1691 | ||
1692 | /* The net we belong to */ | |
1693 | tunnel->l2tp_net = net; | |
1694 | pn = l2tp_pernet(net); | |
1695 | ||
0d76751f | 1696 | if (cfg != NULL) |
fd558d18 JC |
1697 | tunnel->debug = cfg->debug; |
1698 | ||
1699 | /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ | |
0d76751f JC |
1700 | tunnel->encap = encap; |
1701 | if (encap == L2TP_ENCAPTYPE_UDP) { | |
1702 | /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ | |
1703 | udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP; | |
1704 | udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv; | |
9980d001 | 1705 | udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy; |
d2cf3361 BL |
1706 | #if IS_ENABLED(CONFIG_IPV6) |
1707 | if (sk->sk_family == PF_INET6) | |
1708 | udpv6_encap_enable(); | |
1709 | else | |
1710 | #endif | |
447167bf | 1711 | udp_encap_enable(); |
0d76751f | 1712 | } |
fd558d18 JC |
1713 | |
1714 | sk->sk_user_data = tunnel; | |
1715 | ||
1716 | /* Hook on the tunnel socket destructor so that we can cleanup | |
1717 | * if the tunnel socket goes away. | |
1718 | */ | |
1719 | tunnel->old_sk_destruct = sk->sk_destruct; | |
1720 | sk->sk_destruct = &l2tp_tunnel_destruct; | |
1721 | tunnel->sock = sk; | |
80d84ef3 | 1722 | tunnel->fd = fd; |
37159ef2 ED |
1723 | lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); |
1724 | ||
fd558d18 JC |
1725 | sk->sk_allocation = GFP_ATOMIC; |
1726 | ||
f8ccac0e TP |
1727 | /* Init delete workqueue struct */ |
1728 | INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); | |
1729 | ||
fd558d18 JC |
1730 | /* Add tunnel to our list */ |
1731 | INIT_LIST_HEAD(&tunnel->list); | |
fd558d18 JC |
1732 | atomic_inc(&l2tp_tunnel_count); |
1733 | ||
1734 | /* Bump the reference count. The tunnel context is deleted | |
1769192a | 1735 | * only when this drops to zero. Must be done before list insertion |
fd558d18 JC |
1736 | */ |
1737 | l2tp_tunnel_inc_refcount(tunnel); | |
1769192a ED |
1738 | spin_lock_bh(&pn->l2tp_tunnel_list_lock); |
1739 | list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); | |
1740 | spin_unlock_bh(&pn->l2tp_tunnel_list_lock); | |
fd558d18 JC |
1741 | |
1742 | err = 0; | |
1743 | err: | |
1744 | if (tunnelp) | |
1745 | *tunnelp = tunnel; | |
1746 | ||
789a4a2c JC |
1747 | /* If tunnel's socket was created by the kernel, it doesn't |
1748 | * have a file. | |
1749 | */ | |
1750 | if (sock && sock->file) | |
fd558d18 JC |
1751 | sockfd_put(sock); |
1752 | ||
1753 | return err; | |
1754 | } | |
1755 | EXPORT_SYMBOL_GPL(l2tp_tunnel_create); | |
1756 | ||
309795f4 JC |
1757 | /* This function is used by the netlink TUNNEL_DELETE command. |
1758 | */ | |
1759 | int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) | |
1760 | { | |
2b551c6e | 1761 | l2tp_tunnel_closeall(tunnel); |
f8ccac0e | 1762 | return (false == queue_work(l2tp_wq, &tunnel->del_work)); |
309795f4 JC |
1763 | } |
1764 | EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); | |
1765 | ||
fd558d18 JC |
1766 | /* Really kill the session. |
1767 | */ | |
1768 | void l2tp_session_free(struct l2tp_session *session) | |
1769 | { | |
1770 | struct l2tp_tunnel *tunnel; | |
1771 | ||
1772 | BUG_ON(atomic_read(&session->ref_count) != 0); | |
1773 | ||
1774 | tunnel = session->tunnel; | |
1775 | if (tunnel != NULL) { | |
1776 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); | |
1777 | ||
1778 | /* Delete the session from the hash */ | |
1779 | write_lock_bh(&tunnel->hlist_lock); | |
1780 | hlist_del_init(&session->hlist); | |
1781 | write_unlock_bh(&tunnel->hlist_lock); | |
1782 | ||
f7faffa3 JC |
1783 | /* Unlink from the global hash if not L2TPv2 */ |
1784 | if (tunnel->version != L2TP_HDR_VER_2) { | |
1785 | struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); | |
1786 | ||
e02d494d JC |
1787 | spin_lock_bh(&pn->l2tp_session_hlist_lock); |
1788 | hlist_del_init_rcu(&session->global_hlist); | |
1789 | spin_unlock_bh(&pn->l2tp_session_hlist_lock); | |
1790 | synchronize_rcu(); | |
f7faffa3 JC |
1791 | } |
1792 | ||
fd558d18 JC |
1793 | if (session->session_id != 0) |
1794 | atomic_dec(&l2tp_session_count); | |
1795 | ||
1796 | sock_put(tunnel->sock); | |
1797 | ||
1798 | /* This will delete the tunnel context if this | |
1799 | * is the last session on the tunnel. | |
1800 | */ | |
1801 | session->tunnel = NULL; | |
1802 | l2tp_tunnel_dec_refcount(tunnel); | |
1803 | } | |
1804 | ||
1805 | kfree(session); | |
1806 | ||
1807 | return; | |
1808 | } | |
1809 | EXPORT_SYMBOL_GPL(l2tp_session_free); | |
1810 | ||
309795f4 JC |
1811 | /* This function is used by the netlink SESSION_DELETE command and by |
1812 | pseudowire modules. | |
1813 | */ | |
1814 | int l2tp_session_delete(struct l2tp_session *session) | |
1815 | { | |
1816 | if (session->session_close != NULL) | |
1817 | (*session->session_close)(session); | |
1818 | ||
1819 | l2tp_session_dec_refcount(session); | |
1820 | ||
1821 | return 0; | |
1822 | } | |
1823 | EXPORT_SYMBOL_GPL(l2tp_session_delete); | |
1824 | ||
1825 | ||
f7faffa3 JC |
1826 | /* We come here whenever a session's send_seq, cookie_len or |
1827 | * l2specific_len parameters are set. | |
1828 | */ | |
fc130840 | 1829 | static void l2tp_session_set_header_len(struct l2tp_session *session, int version) |
f7faffa3 JC |
1830 | { |
1831 | if (version == L2TP_HDR_VER_2) { | |
1832 | session->hdr_len = 6; | |
1833 | if (session->send_seq) | |
1834 | session->hdr_len += 4; | |
1835 | } else { | |
0d76751f JC |
1836 | session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; |
1837 | if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) | |
1838 | session->hdr_len += 4; | |
f7faffa3 JC |
1839 | } |
1840 | ||
1841 | } | |
f7faffa3 | 1842 | |
fd558d18 JC |
1843 | 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) |
1844 | { | |
1845 | struct l2tp_session *session; | |
1846 | ||
1847 | session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); | |
1848 | if (session != NULL) { | |
1849 | session->magic = L2TP_SESSION_MAGIC; | |
1850 | session->tunnel = tunnel; | |
1851 | ||
1852 | session->session_id = session_id; | |
1853 | session->peer_session_id = peer_session_id; | |
d301e325 | 1854 | session->nr = 0; |
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 |