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