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