1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
220dcb110STom Parkin /* L2TP core.
3fd558d18SJames Chapman *
4fd558d18SJames Chapman * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5fd558d18SJames Chapman *
6fd558d18SJames Chapman * This file contains some code of the original L2TPv2 pppol2tp
7fd558d18SJames Chapman * driver, which has the following copyright:
8fd558d18SJames Chapman *
9fd558d18SJames Chapman * Authors: Martijn van Oosterhout <kleptog@svana.org>
10fd558d18SJames Chapman * James Chapman (jchapman@katalix.com)
11fd558d18SJames Chapman * Contributors:
12fd558d18SJames Chapman * Michal Ostrowski <mostrows@speakeasy.net>
13fd558d18SJames Chapman * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14fd558d18SJames Chapman * David S. Miller (davem@redhat.com)
15fd558d18SJames Chapman */
16fd558d18SJames Chapman
17a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18a4ca44faSJoe Perches
19fd558d18SJames Chapman #include <linux/module.h>
20fd558d18SJames Chapman #include <linux/string.h>
21fd558d18SJames Chapman #include <linux/list.h>
22e02d494dSJames Chapman #include <linux/rculist.h>
23fd558d18SJames Chapman #include <linux/uaccess.h>
24fd558d18SJames Chapman
25fd558d18SJames Chapman #include <linux/kernel.h>
26fd558d18SJames Chapman #include <linux/spinlock.h>
27fd558d18SJames Chapman #include <linux/kthread.h>
28fd558d18SJames Chapman #include <linux/sched.h>
29fd558d18SJames Chapman #include <linux/slab.h>
30fd558d18SJames Chapman #include <linux/errno.h>
31fd558d18SJames Chapman #include <linux/jiffies.h>
32fd558d18SJames Chapman
33fd558d18SJames Chapman #include <linux/netdevice.h>
34fd558d18SJames Chapman #include <linux/net.h>
35fd558d18SJames Chapman #include <linux/inetdevice.h>
36fd558d18SJames Chapman #include <linux/skbuff.h>
37fd558d18SJames Chapman #include <linux/init.h>
380d76751fSJames Chapman #include <linux/in.h>
39fd558d18SJames Chapman #include <linux/ip.h>
40fd558d18SJames Chapman #include <linux/udp.h>
410d76751fSJames Chapman #include <linux/l2tp.h>
42fd558d18SJames Chapman #include <linux/hash.h>
43fd558d18SJames Chapman #include <linux/sort.h>
44fd558d18SJames Chapman #include <linux/file.h>
45fd558d18SJames Chapman #include <linux/nsproxy.h>
46fd558d18SJames Chapman #include <net/net_namespace.h>
47fd558d18SJames Chapman #include <net/netns/generic.h>
48fd558d18SJames Chapman #include <net/dst.h>
49fd558d18SJames Chapman #include <net/ip.h>
50fd558d18SJames Chapman #include <net/udp.h>
5185644b4dSTom Herbert #include <net/udp_tunnel.h>
52309795f4SJames Chapman #include <net/inet_common.h>
53fd558d18SJames Chapman #include <net/xfrm.h>
540d76751fSJames Chapman #include <net/protocol.h>
55d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h>
56d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h>
57d2cf3361SBenjamin LaHaise #include <net/ip6_route.h>
58d499bd2eSDavid S. Miller #include <net/ip6_checksum.h>
59fd558d18SJames Chapman
60fd558d18SJames Chapman #include <asm/byteorder.h>
6160063497SArun Sharma #include <linux/atomic.h>
62fd558d18SJames Chapman
63fd558d18SJames Chapman #include "l2tp_core.h"
646b7bdcd7STom Parkin #include "trace.h"
65fd558d18SJames Chapman
663f117d6fSTom Parkin #define CREATE_TRACE_POINTS
673f117d6fSTom Parkin #include "trace.h"
683f117d6fSTom Parkin
69fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0"
70fd558d18SJames Chapman
71fd558d18SJames Chapman /* L2TP header constants */
72fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000
73fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000
74fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800
75fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200
76fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100
77fd558d18SJames Chapman
78fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F
79fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002
80f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003
81fd558d18SJames Chapman
82fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */
83fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000
84fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff
85fd558d18SJames Chapman
8691c52470SJacob Wen #define L2TP_HDR_SIZE_MAX 14
87fd558d18SJames Chapman
88fd558d18SJames Chapman /* Default trace flags */
89fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0
90fd558d18SJames Chapman
91*207e8815SJames Chapman #define L2TP_DEPTH_NESTING 2
92*207e8815SJames Chapman #if L2TP_DEPTH_NESTING == SINGLE_DEPTH_NESTING
93*207e8815SJames Chapman #error "L2TP requires its own lockdep subclass"
94*207e8815SJames Chapman #endif
95*207e8815SJames Chapman
96fd558d18SJames Chapman /* Private data stored for received packets in the skb.
97fd558d18SJames Chapman */
98fd558d18SJames Chapman struct l2tp_skb_cb {
99f7faffa3SJames Chapman u32 ns;
100fd558d18SJames Chapman u16 has_seq;
101fd558d18SJames Chapman u16 length;
102fd558d18SJames Chapman unsigned long expires;
103fd558d18SJames Chapman };
104fd558d18SJames Chapman
105efcd8c85STom Parkin #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)])
106fd558d18SJames Chapman
107f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq;
108fd558d18SJames Chapman
109fd558d18SJames Chapman /* per-net private data for this module */
110fd558d18SJames Chapman static unsigned int l2tp_net_id;
111fd558d18SJames Chapman struct l2tp_net {
112c4d48a58SCong Wang /* Lock for write access to l2tp_tunnel_idr */
113c4d48a58SCong Wang spinlock_t l2tp_tunnel_idr_lock;
114c4d48a58SCong Wang struct idr l2tp_tunnel_idr;
115f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
11620dcb110STom Parkin /* Lock for write access to l2tp_session_hlist */
117e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock;
118fd558d18SJames Chapman };
119fd558d18SJames Chapman
120b954f940SPaolo Abeni #if IS_ENABLED(CONFIG_IPV6)
l2tp_sk_is_v6(struct sock * sk)121b954f940SPaolo Abeni static bool l2tp_sk_is_v6(struct sock *sk)
122b954f940SPaolo Abeni {
123b954f940SPaolo Abeni return sk->sk_family == PF_INET6 &&
124b954f940SPaolo Abeni !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
125b954f940SPaolo Abeni }
126b954f940SPaolo Abeni #endif
127fc130840Sstephen hemminger
l2tp_pernet(const struct net * net)1289aaef50cSGuillaume Nault static inline struct l2tp_net *l2tp_pernet(const struct net *net)
129fd558d18SJames Chapman {
130fd558d18SJames Chapman return net_generic(net, l2tp_net_id);
131fd558d18SJames Chapman }
132fd558d18SJames Chapman
133f7faffa3SJames Chapman /* Session hash global list for L2TPv3.
134f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several
135f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real
136f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask.
137f7faffa3SJames Chapman */
138f7faffa3SJames Chapman static inline struct hlist_head *
l2tp_session_id_hash_2(struct l2tp_net * pn,u32 session_id)139f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
140f7faffa3SJames Chapman {
141f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
142f7faffa3SJames Chapman }
143f7faffa3SJames Chapman
144fd558d18SJames Chapman /* Session hash list.
145fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several
146fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing
147fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a
148fd558d18SJames Chapman * simple bitmask.
149fd558d18SJames Chapman */
150fd558d18SJames Chapman static inline struct hlist_head *
l2tp_session_id_hash(struct l2tp_tunnel * tunnel,u32 session_id)151fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
152fd558d18SJames Chapman {
153fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
154fd558d18SJames Chapman }
155fd558d18SJames Chapman
l2tp_tunnel_free(struct l2tp_tunnel * tunnel)15652016e25STom Parkin static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
157d00fa9adSJames Chapman {
1586b7bdcd7STom Parkin trace_free_tunnel(tunnel);
159d00fa9adSJames Chapman sock_put(tunnel->sock);
160d00fa9adSJames Chapman /* the tunnel is freed in the socket destructor */
161d00fa9adSJames Chapman }
16252016e25STom Parkin
l2tp_session_free(struct l2tp_session * session)16352016e25STom Parkin static void l2tp_session_free(struct l2tp_session *session)
16452016e25STom Parkin {
1656b7bdcd7STom Parkin trace_free_session(session);
16645faeff1STom Parkin if (session->tunnel)
16745faeff1STom Parkin l2tp_tunnel_dec_refcount(session->tunnel);
16852016e25STom Parkin kfree(session);
16952016e25STom Parkin }
17052016e25STom Parkin
l2tp_sk_to_tunnel(struct sock * sk)17145faeff1STom Parkin struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk)
17245faeff1STom Parkin {
17345faeff1STom Parkin struct l2tp_tunnel *tunnel = sk->sk_user_data;
17445faeff1STom Parkin
17545faeff1STom Parkin if (tunnel)
17645faeff1STom Parkin if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
17745faeff1STom Parkin return NULL;
17845faeff1STom Parkin
17945faeff1STom Parkin return tunnel;
18045faeff1STom Parkin }
18145faeff1STom Parkin EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
18245faeff1STom Parkin
l2tp_tunnel_inc_refcount(struct l2tp_tunnel * tunnel)18352016e25STom Parkin void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
18452016e25STom Parkin {
18552016e25STom Parkin refcount_inc(&tunnel->ref_count);
18652016e25STom Parkin }
18752016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
18852016e25STom Parkin
l2tp_tunnel_dec_refcount(struct l2tp_tunnel * tunnel)18952016e25STom Parkin void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
19052016e25STom Parkin {
19152016e25STom Parkin if (refcount_dec_and_test(&tunnel->ref_count))
19252016e25STom Parkin l2tp_tunnel_free(tunnel);
19352016e25STom Parkin }
19452016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
19552016e25STom Parkin
l2tp_session_inc_refcount(struct l2tp_session * session)19652016e25STom Parkin void l2tp_session_inc_refcount(struct l2tp_session *session)
19752016e25STom Parkin {
19852016e25STom Parkin refcount_inc(&session->ref_count);
19952016e25STom Parkin }
20052016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
20152016e25STom Parkin
l2tp_session_dec_refcount(struct l2tp_session * session)20252016e25STom Parkin void l2tp_session_dec_refcount(struct l2tp_session *session)
20352016e25STom Parkin {
20452016e25STom Parkin if (refcount_dec_and_test(&session->ref_count))
20552016e25STom Parkin l2tp_session_free(session);
20652016e25STom Parkin }
20752016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
208d00fa9adSJames Chapman
20954652eb1SGuillaume Nault /* Lookup a tunnel. A new reference is held on the returned tunnel. */
l2tp_tunnel_get(const struct net * net,u32 tunnel_id)21054652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
21154652eb1SGuillaume Nault {
21254652eb1SGuillaume Nault const struct l2tp_net *pn = l2tp_pernet(net);
21354652eb1SGuillaume Nault struct l2tp_tunnel *tunnel;
21454652eb1SGuillaume Nault
21554652eb1SGuillaume Nault rcu_read_lock_bh();
216c4d48a58SCong Wang tunnel = idr_find(&pn->l2tp_tunnel_idr, tunnel_id);
217c4d48a58SCong Wang if (tunnel && refcount_inc_not_zero(&tunnel->ref_count)) {
21854652eb1SGuillaume Nault rcu_read_unlock_bh();
21954652eb1SGuillaume Nault return tunnel;
22054652eb1SGuillaume Nault }
22154652eb1SGuillaume Nault rcu_read_unlock_bh();
22254652eb1SGuillaume Nault
22354652eb1SGuillaume Nault return NULL;
22454652eb1SGuillaume Nault }
22554652eb1SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
22654652eb1SGuillaume Nault
l2tp_tunnel_get_nth(const struct net * net,int nth)2275846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
2285846c131SGuillaume Nault {
229c4d48a58SCong Wang struct l2tp_net *pn = l2tp_pernet(net);
230c4d48a58SCong Wang unsigned long tunnel_id, tmp;
2315846c131SGuillaume Nault struct l2tp_tunnel *tunnel;
2325846c131SGuillaume Nault int count = 0;
2335846c131SGuillaume Nault
2345846c131SGuillaume Nault rcu_read_lock_bh();
235c4d48a58SCong Wang idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
236c4d48a58SCong Wang if (tunnel && ++count > nth &&
237a622b400SEric Dumazet refcount_inc_not_zero(&tunnel->ref_count)) {
2385846c131SGuillaume Nault rcu_read_unlock_bh();
2395846c131SGuillaume Nault return tunnel;
2405846c131SGuillaume Nault }
2415846c131SGuillaume Nault }
2425846c131SGuillaume Nault rcu_read_unlock_bh();
2435846c131SGuillaume Nault
2445846c131SGuillaume Nault return NULL;
2455846c131SGuillaume Nault }
2465846c131SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
2475846c131SGuillaume Nault
l2tp_tunnel_get_session(struct l2tp_tunnel * tunnel,u32 session_id)24801e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
249a4346210SGuillaume Nault u32 session_id)
25061b9a047SGuillaume Nault {
25161b9a047SGuillaume Nault struct hlist_head *session_list;
25261b9a047SGuillaume Nault struct l2tp_session *session;
25361b9a047SGuillaume Nault
25401e28b92SGuillaume Nault session_list = l2tp_session_id_hash(tunnel, session_id);
25561b9a047SGuillaume Nault
25607b8ca37STom Parkin rcu_read_lock_bh();
25707b8ca37STom Parkin hlist_for_each_entry_rcu(session, session_list, hlist)
25861b9a047SGuillaume Nault if (session->session_id == session_id) {
25961b9a047SGuillaume Nault l2tp_session_inc_refcount(session);
26007b8ca37STom Parkin rcu_read_unlock_bh();
26161b9a047SGuillaume Nault
26261b9a047SGuillaume Nault return session;
26361b9a047SGuillaume Nault }
26407b8ca37STom Parkin rcu_read_unlock_bh();
26561b9a047SGuillaume Nault
26661b9a047SGuillaume Nault return NULL;
26761b9a047SGuillaume Nault }
26801e28b92SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
26961b9a047SGuillaume Nault
l2tp_session_get(const struct net * net,u32 session_id)27001e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
27101e28b92SGuillaume Nault {
27201e28b92SGuillaume Nault struct hlist_head *session_list;
27301e28b92SGuillaume Nault struct l2tp_session *session;
27401e28b92SGuillaume Nault
27501e28b92SGuillaume Nault session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
27601e28b92SGuillaume Nault
27701e28b92SGuillaume Nault rcu_read_lock_bh();
27801e28b92SGuillaume Nault hlist_for_each_entry_rcu(session, session_list, global_hlist)
27961b9a047SGuillaume Nault if (session->session_id == session_id) {
28061b9a047SGuillaume Nault l2tp_session_inc_refcount(session);
28101e28b92SGuillaume Nault rcu_read_unlock_bh();
28261b9a047SGuillaume Nault
28361b9a047SGuillaume Nault return session;
28461b9a047SGuillaume Nault }
28501e28b92SGuillaume Nault rcu_read_unlock_bh();
28661b9a047SGuillaume Nault
28761b9a047SGuillaume Nault return NULL;
28861b9a047SGuillaume Nault }
28961b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get);
29061b9a047SGuillaume Nault
l2tp_session_get_nth(struct l2tp_tunnel * tunnel,int nth)291a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
292fd558d18SJames Chapman {
293fd558d18SJames Chapman int hash;
294fd558d18SJames Chapman struct l2tp_session *session;
295fd558d18SJames Chapman int count = 0;
296fd558d18SJames Chapman
29707b8ca37STom Parkin rcu_read_lock_bh();
298fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
29907b8ca37STom Parkin hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
300fd558d18SJames Chapman if (++count > nth) {
301e08293a4SGuillaume Nault l2tp_session_inc_refcount(session);
30207b8ca37STom Parkin rcu_read_unlock_bh();
303fd558d18SJames Chapman return session;
304fd558d18SJames Chapman }
305fd558d18SJames Chapman }
306fd558d18SJames Chapman }
307fd558d18SJames Chapman
30807b8ca37STom Parkin rcu_read_unlock_bh();
309fd558d18SJames Chapman
310fd558d18SJames Chapman return NULL;
311fd558d18SJames Chapman }
312e08293a4SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
313fd558d18SJames Chapman
314309795f4SJames Chapman /* Lookup a session by interface name.
315309795f4SJames Chapman * This is very inefficient but is only used by management interfaces.
316309795f4SJames Chapman */
l2tp_session_get_by_ifname(const struct net * net,const char * ifname)3179aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
318a4346210SGuillaume Nault const char *ifname)
319309795f4SJames Chapman {
320309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net);
321309795f4SJames Chapman int hash;
322309795f4SJames Chapman struct l2tp_session *session;
323309795f4SJames Chapman
324e02d494dSJames Chapman rcu_read_lock_bh();
325309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
326b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
327309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) {
3282777e2abSGuillaume Nault l2tp_session_inc_refcount(session);
329e02d494dSJames Chapman rcu_read_unlock_bh();
3302777e2abSGuillaume Nault
331309795f4SJames Chapman return session;
332309795f4SJames Chapman }
333309795f4SJames Chapman }
334309795f4SJames Chapman }
335309795f4SJames Chapman
336e02d494dSJames Chapman rcu_read_unlock_bh();
337309795f4SJames Chapman
338309795f4SJames Chapman return NULL;
339309795f4SJames Chapman }
3402777e2abSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
341309795f4SJames Chapman
l2tp_session_register(struct l2tp_session * session,struct l2tp_tunnel * tunnel)3423953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
3433953ae7bSGuillaume Nault struct l2tp_tunnel *tunnel)
344dbdbc73bSGuillaume Nault {
345dbdbc73bSGuillaume Nault struct l2tp_session *session_walk;
346dbdbc73bSGuillaume Nault struct hlist_head *g_head;
347dbdbc73bSGuillaume Nault struct hlist_head *head;
348dbdbc73bSGuillaume Nault struct l2tp_net *pn;
349f3c66d4eSGuillaume Nault int err;
350dbdbc73bSGuillaume Nault
351dbdbc73bSGuillaume Nault head = l2tp_session_id_hash(tunnel, session->session_id);
352dbdbc73bSGuillaume Nault
35307b8ca37STom Parkin spin_lock_bh(&tunnel->hlist_lock);
354f3c66d4eSGuillaume Nault if (!tunnel->acpt_newsess) {
355f3c66d4eSGuillaume Nault err = -ENODEV;
356f3c66d4eSGuillaume Nault goto err_tlock;
357f3c66d4eSGuillaume Nault }
358f3c66d4eSGuillaume Nault
359dbdbc73bSGuillaume Nault hlist_for_each_entry(session_walk, head, hlist)
360f3c66d4eSGuillaume Nault if (session_walk->session_id == session->session_id) {
361f3c66d4eSGuillaume Nault err = -EEXIST;
362f3c66d4eSGuillaume Nault goto err_tlock;
363f3c66d4eSGuillaume Nault }
364dbdbc73bSGuillaume Nault
365dbdbc73bSGuillaume Nault if (tunnel->version == L2TP_HDR_VER_3) {
366dbdbc73bSGuillaume Nault pn = l2tp_pernet(tunnel->l2tp_net);
367363a341dSGuillaume Nault g_head = l2tp_session_id_hash_2(pn, session->session_id);
368dbdbc73bSGuillaume Nault
369dbdbc73bSGuillaume Nault spin_lock_bh(&pn->l2tp_session_hlist_lock);
370dbdbc73bSGuillaume Nault
3710d0d9a38SRidge Kennedy /* IP encap expects session IDs to be globally unique, while
3720d0d9a38SRidge Kennedy * UDP encap doesn't.
3730d0d9a38SRidge Kennedy */
374f3c66d4eSGuillaume Nault hlist_for_each_entry(session_walk, g_head, global_hlist)
3750d0d9a38SRidge Kennedy if (session_walk->session_id == session->session_id &&
3760d0d9a38SRidge Kennedy (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
3770d0d9a38SRidge Kennedy tunnel->encap == L2TP_ENCAPTYPE_IP)) {
378f3c66d4eSGuillaume Nault err = -EEXIST;
379f3c66d4eSGuillaume Nault goto err_tlock_pnlock;
380f3c66d4eSGuillaume Nault }
381f3c66d4eSGuillaume Nault
382f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel);
383dbdbc73bSGuillaume Nault hlist_add_head_rcu(&session->global_hlist, g_head);
384f3c66d4eSGuillaume Nault
385dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock);
386f3c66d4eSGuillaume Nault } else {
387f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel);
388dbdbc73bSGuillaume Nault }
389dbdbc73bSGuillaume Nault
39007b8ca37STom Parkin hlist_add_head_rcu(&session->hlist, head);
39107b8ca37STom Parkin spin_unlock_bh(&tunnel->hlist_lock);
392dbdbc73bSGuillaume Nault
3936b7bdcd7STom Parkin trace_register_session(session);
3946b7bdcd7STom Parkin
395dbdbc73bSGuillaume Nault return 0;
396dbdbc73bSGuillaume Nault
397f3c66d4eSGuillaume Nault err_tlock_pnlock:
398dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock);
399f3c66d4eSGuillaume Nault err_tlock:
40007b8ca37STom Parkin spin_unlock_bh(&tunnel->hlist_lock);
401dbdbc73bSGuillaume Nault
402f3c66d4eSGuillaume Nault return err;
403dbdbc73bSGuillaume Nault }
4043953ae7bSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_register);
405dbdbc73bSGuillaume Nault
406fd558d18SJames Chapman /*****************************************************************************
407fd558d18SJames Chapman * Receive data handling
408fd558d18SJames Chapman *****************************************************************************/
409fd558d18SJames Chapman
410fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence
411fd558d18SJames Chapman * number.
412fd558d18SJames Chapman */
l2tp_recv_queue_skb(struct l2tp_session * session,struct sk_buff * skb)413fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
414fd558d18SJames Chapman {
415fd558d18SJames Chapman struct sk_buff *skbp;
416fd558d18SJames Chapman struct sk_buff *tmp;
417f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns;
418fd558d18SJames Chapman
419fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock);
420fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
421fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) {
422fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb);
4237b7c0719STom Parkin atomic_long_inc(&session->stats.rx_oos_packets);
424fd558d18SJames Chapman goto out;
425fd558d18SJames Chapman }
426fd558d18SJames Chapman }
427fd558d18SJames Chapman
428fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb);
429fd558d18SJames Chapman
430fd558d18SJames Chapman out:
431fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock);
432fd558d18SJames Chapman }
433fd558d18SJames Chapman
434fd558d18SJames Chapman /* Dequeue a single skb.
435fd558d18SJames Chapman */
l2tp_recv_dequeue_skb(struct l2tp_session * session,struct sk_buff * skb)436fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
437fd558d18SJames Chapman {
438fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel;
439fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length;
440fd558d18SJames Chapman
441fd558d18SJames Chapman /* We're about to requeue the skb, so return resources
442fd558d18SJames Chapman * to its current owner (a socket receive buffer).
443fd558d18SJames Chapman */
444fd558d18SJames Chapman skb_orphan(skb);
445fd558d18SJames Chapman
4467b7c0719STom Parkin atomic_long_inc(&tunnel->stats.rx_packets);
4477b7c0719STom Parkin atomic_long_add(length, &tunnel->stats.rx_bytes);
4487b7c0719STom Parkin atomic_long_inc(&session->stats.rx_packets);
4497b7c0719STom Parkin atomic_long_add(length, &session->stats.rx_bytes);
450fd558d18SJames Chapman
451fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) {
452fd558d18SJames Chapman /* Bump our Nr */
453fd558d18SJames Chapman session->nr++;
4548a1631d5SJames Chapman session->nr &= session->nr_max;
4556b7bdcd7STom Parkin trace_session_seqnum_update(session);
456fd558d18SJames Chapman }
457fd558d18SJames Chapman
458fd558d18SJames Chapman /* call private receive handler */
4590febc7b3STom Parkin if (session->recv_skb)
460fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
461fd558d18SJames Chapman else
462fd558d18SJames Chapman kfree_skb(skb);
463fd558d18SJames Chapman }
464fd558d18SJames Chapman
465fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order.
466fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded.
467fd558d18SJames Chapman */
l2tp_recv_dequeue(struct l2tp_session * session)468fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session)
469fd558d18SJames Chapman {
470fd558d18SJames Chapman struct sk_buff *skb;
471fd558d18SJames Chapman struct sk_buff *tmp;
472fd558d18SJames Chapman
473fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we
474fd558d18SJames Chapman * expect to send up next, dequeue it and any other
475fd558d18SJames Chapman * in-sequence packets behind it.
476fd558d18SJames Chapman */
477e2e210c0SEric Dumazet start:
478fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock);
479fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
4806b7bdcd7STom Parkin struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
4816b7bdcd7STom Parkin
4826b7bdcd7STom Parkin /* If the packet has been pending on the queue for too long, discard it */
4836b7bdcd7STom Parkin if (time_after(jiffies, cb->expires)) {
4847b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards);
4857b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors);
4866b7bdcd7STom Parkin trace_session_pkt_expired(session, cb->ns);
48738d40b3fSJames Chapman session->reorder_skip = 1;
488fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q);
489fd558d18SJames Chapman kfree_skb(skb);
490fd558d18SJames Chapman continue;
491fd558d18SJames Chapman }
492fd558d18SJames Chapman
4936b7bdcd7STom Parkin if (cb->has_seq) {
49438d40b3fSJames Chapman if (session->reorder_skip) {
49538d40b3fSJames Chapman session->reorder_skip = 0;
4966b7bdcd7STom Parkin session->nr = cb->ns;
4976b7bdcd7STom Parkin trace_session_seqnum_reset(session);
49838d40b3fSJames Chapman }
4996b7bdcd7STom Parkin if (cb->ns != session->nr)
500fd558d18SJames Chapman goto out;
501fd558d18SJames Chapman }
502fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q);
503fd558d18SJames Chapman
504fd558d18SJames Chapman /* Process the skb. We release the queue lock while we
505fd558d18SJames Chapman * do so to let other contexts process the queue.
506fd558d18SJames Chapman */
507fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock);
508fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb);
509e2e210c0SEric Dumazet goto start;
510fd558d18SJames Chapman }
511fd558d18SJames Chapman
512fd558d18SJames Chapman out:
513fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock);
514fd558d18SJames Chapman }
515fd558d18SJames Chapman
l2tp_seq_check_rx_window(struct l2tp_session * session,u32 nr)5168a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
5178a1631d5SJames Chapman {
5188a1631d5SJames Chapman u32 nws;
5198a1631d5SJames Chapman
5208a1631d5SJames Chapman if (nr >= session->nr)
5218a1631d5SJames Chapman nws = nr - session->nr;
5228a1631d5SJames Chapman else
5238a1631d5SJames Chapman nws = (session->nr_max + 1) - (session->nr - nr);
5248a1631d5SJames Chapman
5258a1631d5SJames Chapman return nws < session->nr_window_size;
5268a1631d5SJames Chapman }
5278a1631d5SJames Chapman
528b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
529b6dc01a4SJames Chapman * acceptable, else non-zero.
530b6dc01a4SJames Chapman */
l2tp_recv_data_seq(struct l2tp_session * session,struct sk_buff * skb)531b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
532b6dc01a4SJames Chapman {
5336b7bdcd7STom Parkin struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
5346b7bdcd7STom Parkin
5356b7bdcd7STom Parkin if (!l2tp_seq_check_rx_window(session, cb->ns)) {
5368a1631d5SJames Chapman /* Packet sequence number is outside allowed window.
5378a1631d5SJames Chapman * Discard it.
5388a1631d5SJames Chapman */
5396b7bdcd7STom Parkin trace_session_pkt_outside_rx_window(session, cb->ns);
5408a1631d5SJames Chapman goto discard;
5418a1631d5SJames Chapman }
5428a1631d5SJames Chapman
543b6dc01a4SJames Chapman if (session->reorder_timeout != 0) {
544b6dc01a4SJames Chapman /* Packet reordering enabled. Add skb to session's
545b6dc01a4SJames Chapman * reorder queue, in order of ns.
546b6dc01a4SJames Chapman */
547b6dc01a4SJames Chapman l2tp_recv_queue_skb(session, skb);
548a0dbd822SJames Chapman goto out;
549a0dbd822SJames Chapman }
550a0dbd822SJames Chapman
551a0dbd822SJames Chapman /* Packet reordering disabled. Discard out-of-sequence packets, while
552a0dbd822SJames Chapman * tracking the number if in-sequence packets after the first OOS packet
553a0dbd822SJames Chapman * is seen. After nr_oos_count_max in-sequence packets, reset the
554a0dbd822SJames Chapman * sequence number to re-enable packet reception.
555b6dc01a4SJames Chapman */
5566b7bdcd7STom Parkin if (cb->ns == session->nr) {
557a0dbd822SJames Chapman skb_queue_tail(&session->reorder_q, skb);
558a0dbd822SJames Chapman } else {
5596b7bdcd7STom Parkin u32 nr_oos = cb->ns;
560a0dbd822SJames Chapman u32 nr_next = (session->nr_oos + 1) & session->nr_max;
561a0dbd822SJames Chapman
562a0dbd822SJames Chapman if (nr_oos == nr_next)
563a0dbd822SJames Chapman session->nr_oos_count++;
564a0dbd822SJames Chapman else
565a0dbd822SJames Chapman session->nr_oos_count = 0;
566a0dbd822SJames Chapman
567a0dbd822SJames Chapman session->nr_oos = nr_oos;
568a0dbd822SJames Chapman if (session->nr_oos_count > session->nr_oos_count_max) {
569a0dbd822SJames Chapman session->reorder_skip = 1;
570a0dbd822SJames Chapman }
571a0dbd822SJames Chapman if (!session->reorder_skip) {
572b6dc01a4SJames Chapman atomic_long_inc(&session->stats.rx_seq_discards);
5736b7bdcd7STom Parkin trace_session_pkt_oos(session, cb->ns);
574b6dc01a4SJames Chapman goto discard;
575b6dc01a4SJames Chapman }
576b6dc01a4SJames Chapman skb_queue_tail(&session->reorder_q, skb);
577b6dc01a4SJames Chapman }
578b6dc01a4SJames Chapman
579a0dbd822SJames Chapman out:
580b6dc01a4SJames Chapman return 0;
581b6dc01a4SJames Chapman
582b6dc01a4SJames Chapman discard:
583b6dc01a4SJames Chapman return 1;
584b6dc01a4SJames Chapman }
585b6dc01a4SJames Chapman
586f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2
587f7faffa3SJames Chapman * and L2TPv3 data frames here.
588f7faffa3SJames Chapman *
589f7faffa3SJames Chapman * L2TPv2 Data Message Header
590f7faffa3SJames Chapman *
591f7faffa3SJames Chapman * 0 1 2 3
592f7faffa3SJames Chapman * 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
593f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
595f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596f7faffa3SJames Chapman * | Tunnel ID | Session ID |
597f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) |
599f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt)
601f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602f7faffa3SJames Chapman *
603f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as
604f7faffa3SJames Chapman * those in L2TP control frames.
605f7faffa3SJames Chapman *
606f7faffa3SJames Chapman * L2TPv3 Data Message Header
607f7faffa3SJames Chapman *
608f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609f7faffa3SJames Chapman * | L2TP Session Header |
610f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611f7faffa3SJames Chapman * | L2-Specific Sublayer |
612f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
613f7faffa3SJames Chapman * | Tunnel Payload ...
614f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
615f7faffa3SJames Chapman *
616f7faffa3SJames Chapman * L2TPv3 Session Header Over IP
617f7faffa3SJames Chapman *
618f7faffa3SJames Chapman * 0 1 2 3
619f7faffa3SJames Chapman * 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
620f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621f7faffa3SJames Chapman * | Session ID |
622f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)...
624f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625f7faffa3SJames Chapman * |
626f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627f7faffa3SJames Chapman *
628f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format
629f7faffa3SJames Chapman *
630f7faffa3SJames Chapman * 0 1 2 3
631f7faffa3SJames Chapman * 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
632f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number |
634f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
635f7faffa3SJames Chapman *
63623fe846fSGuillaume Nault * Cookie value and sublayer format are negotiated with the peer when
63723fe846fSGuillaume Nault * the session is set up. Unlike L2TPv2, we do not need to parse the
63823fe846fSGuillaume Nault * packet header to determine if optional fields are present.
639f7faffa3SJames Chapman *
640f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is
641f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the
642f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data
643f7faffa3SJames Chapman * after the session-id.
644f7faffa3SJames Chapman */
l2tp_recv_common(struct l2tp_session * session,struct sk_buff * skb,unsigned char * ptr,unsigned char * optr,u16 hdrflags,int length)645f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
646f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags,
6472b139e6bSGuillaume Nault int length)
648f7faffa3SJames Chapman {
649f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel;
650f7faffa3SJames Chapman int offset;
651f7faffa3SJames Chapman
652f7faffa3SJames Chapman /* Parse and check optional cookie */
653f7faffa3SJames Chapman if (session->peer_cookie_len > 0) {
654f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
6553e59e885SMatthias Schiffer pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n",
656a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id,
657a4ca44faSJoe Perches session->session_id);
6587b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards);
659f7faffa3SJames Chapman goto discard;
660f7faffa3SJames Chapman }
661f7faffa3SJames Chapman ptr += session->peer_cookie_len;
662f7faffa3SJames Chapman }
663f7faffa3SJames Chapman
664f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are
665f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3.
666f7faffa3SJames Chapman *
667f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under
668f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but
669f7faffa3SJames Chapman * we were expecting them, discard frame.
670f7faffa3SJames Chapman */
671f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0;
672f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) {
673f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) {
674f7faffa3SJames Chapman /* Store L2TP info in the skb */
67512923365STom Parkin L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr);
676f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1;
67712923365STom Parkin ptr += 2;
67812923365STom Parkin /* Skip past nr in the header */
67912923365STom Parkin ptr += 2;
680f7faffa3SJames Chapman
681f7faffa3SJames Chapman }
682f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
683f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *)ptr);
684f7faffa3SJames Chapman
685f7faffa3SJames Chapman if (l2h & 0x40000000) {
686f7faffa3SJames Chapman /* Store L2TP info in the skb */
68712923365STom Parkin L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff;
688f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1;
689f7faffa3SJames Chapman }
69062e7b6a5SLorenzo Bianconi ptr += 4;
691f7faffa3SJames Chapman }
692f7faffa3SJames Chapman
693f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) {
69420dcb110STom Parkin /* Received a packet with sequence numbers. If we're the LAC,
695f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not,
696f7faffa3SJames Chapman * configure it so.
697f7faffa3SJames Chapman */
6986c0ec37bSTom Parkin if (!session->lns_mode && !session->send_seq) {
6996b7bdcd7STom Parkin trace_session_seqnum_lns_enable(session);
7003f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1;
701f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version);
702f7faffa3SJames Chapman }
703f7faffa3SJames Chapman } else {
704f7faffa3SJames Chapman /* No sequence numbers.
705f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard.
706f7faffa3SJames Chapman */
707f7faffa3SJames Chapman if (session->recv_seq) {
7083e59e885SMatthias Schiffer pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
709a4ca44faSJoe Perches session->name);
7107b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards);
711f7faffa3SJames Chapman goto discard;
712f7faffa3SJames Chapman }
713f7faffa3SJames Chapman
714f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the
715f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers.
716f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the
717f7faffa3SJames Chapman * LAC is broken. Discard the frame.
718f7faffa3SJames Chapman */
7196c0ec37bSTom Parkin if (!session->lns_mode && session->send_seq) {
7206b7bdcd7STom Parkin trace_session_seqnum_lns_disable(session);
721f7faffa3SJames Chapman session->send_seq = 0;
722f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version);
723f7faffa3SJames Chapman } else if (session->send_seq) {
7243e59e885SMatthias Schiffer pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
725a4ca44faSJoe Perches session->name);
7267b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards);
727f7faffa3SJames Chapman goto discard;
728f7faffa3SJames Chapman }
729f7faffa3SJames Chapman }
730f7faffa3SJames Chapman
731900631eeSJames Chapman /* Session data offset is defined only for L2TPv2 and is
732900631eeSJames Chapman * indicated by an optional 16-bit value in the header.
733f7faffa3SJames Chapman */
734f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) {
735f7faffa3SJames Chapman /* If offset bit set, skip it. */
736f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) {
737f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr);
738f7faffa3SJames Chapman ptr += 2 + offset;
739f7faffa3SJames Chapman }
740900631eeSJames Chapman }
741f7faffa3SJames Chapman
742f7faffa3SJames Chapman offset = ptr - optr;
743f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset))
744f7faffa3SJames Chapman goto discard;
745f7faffa3SJames Chapman
746f7faffa3SJames Chapman __skb_pull(skb, offset);
747f7faffa3SJames Chapman
748f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold
749f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not
750f7faffa3SJames Chapman * reordering.
751f7faffa3SJames Chapman */
752f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length;
753f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies +
754f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ);
755f7faffa3SJames Chapman
756f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if
757f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[].
758f7faffa3SJames Chapman */
759f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) {
760b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb))
761f7faffa3SJames Chapman goto discard;
762f7faffa3SJames Chapman } else {
763f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the
764f7faffa3SJames Chapman * reorder queue. This ensures that it will be
765f7faffa3SJames Chapman * delivered after all previous sequenced skbs.
766f7faffa3SJames Chapman */
767f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb);
768f7faffa3SJames Chapman }
769f7faffa3SJames Chapman
770f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */
771f7faffa3SJames Chapman l2tp_recv_dequeue(session);
772f7faffa3SJames Chapman
773f7faffa3SJames Chapman return;
774f7faffa3SJames Chapman
775f7faffa3SJames Chapman discard:
7767b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors);
777f7faffa3SJames Chapman kfree_skb(skb);
778f7faffa3SJames Chapman }
779ca7885dbSTom Parkin EXPORT_SYMBOL_GPL(l2tp_recv_common);
780f7faffa3SJames Chapman
78148f72f92STom Parkin /* Drop skbs from the session's reorder_q
78248f72f92STom Parkin */
l2tp_session_queue_purge(struct l2tp_session * session)783493048f5STom Parkin static void l2tp_session_queue_purge(struct l2tp_session *session)
78448f72f92STom Parkin {
78548f72f92STom Parkin struct sk_buff *skb = NULL;
786b71a61ccSTom Parkin
78748f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) {
78848f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors);
78948f72f92STom Parkin kfree_skb(skb);
79048f72f92STom Parkin }
79148f72f92STom Parkin }
79248f72f92STom Parkin
793fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
794fd558d18SJames Chapman * here. The skb is not on a list when we get here.
795fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on.
796fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be
797fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with.
798fd558d18SJames Chapman */
l2tp_udp_recv_core(struct l2tp_tunnel * tunnel,struct sk_buff * skb)7992b139e6bSGuillaume Nault static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
800fd558d18SJames Chapman {
801fd558d18SJames Chapman struct l2tp_session *session = NULL;
802fd558d18SJames Chapman unsigned char *ptr, *optr;
803fd558d18SJames Chapman u16 hdrflags;
804fd558d18SJames Chapman u32 tunnel_id, session_id;
805fd558d18SJames Chapman u16 version;
806f7faffa3SJames Chapman int length;
807fd558d18SJames Chapman
808aa785f93SBhaskar Chowdhury /* UDP has verified checksum */
809fd558d18SJames Chapman
810fd558d18SJames Chapman /* UDP always verifies the packet length. */
811fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr));
812fd558d18SJames Chapman
813fd558d18SJames Chapman /* Short packet? */
81491c52470SJacob Wen if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
8153e59e885SMatthias Schiffer pr_debug_ratelimited("%s: recv short packet (len=%d)\n",
816a4ca44faSJoe Perches tunnel->name, skb->len);
8173e59e885SMatthias Schiffer goto invalid;
818fd558d18SJames Chapman }
819fd558d18SJames Chapman
820e50e705cSEric Dumazet /* Point to L2TP header */
82195075150STom Parkin optr = skb->data;
82295075150STom Parkin ptr = skb->data;
823e50e705cSEric Dumazet
824fd558d18SJames Chapman /* Get L2TP header flags */
825fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *)ptr);
826fd558d18SJames Chapman
827fd558d18SJames Chapman /* Check protocol version */
828fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK;
829fd558d18SJames Chapman if (version != tunnel->version) {
8303e59e885SMatthias Schiffer pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n",
831fd558d18SJames Chapman tunnel->name, version, tunnel->version);
8323e59e885SMatthias Schiffer goto invalid;
833fd558d18SJames Chapman }
834fd558d18SJames Chapman
835fd558d18SJames Chapman /* Get length of L2TP packet */
836fd558d18SJames Chapman length = skb->len;
837fd558d18SJames Chapman
838fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */
83912923365STom Parkin if (hdrflags & L2TP_HDRFLAG_T)
8403e59e885SMatthias Schiffer goto pass;
841fd558d18SJames Chapman
842fd558d18SJames Chapman /* Skip flags */
843fd558d18SJames Chapman ptr += 2;
844fd558d18SJames Chapman
845f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) {
846fd558d18SJames Chapman /* If length is present, skip it */
847fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L)
848fd558d18SJames Chapman ptr += 2;
849fd558d18SJames Chapman
850fd558d18SJames Chapman /* Extract tunnel and session ID */
851fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *)ptr);
852fd558d18SJames Chapman ptr += 2;
853fd558d18SJames Chapman session_id = ntohs(*(__be16 *)ptr);
854fd558d18SJames Chapman ptr += 2;
855f7faffa3SJames Chapman } else {
856f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */
857f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id;
858f7faffa3SJames Chapman session_id = ntohl(*(__be32 *)ptr);
859f7faffa3SJames Chapman ptr += 4;
860f7faffa3SJames Chapman }
861fd558d18SJames Chapman
862fd558d18SJames Chapman /* Find the session context */
86301e28b92SGuillaume Nault session = l2tp_tunnel_get_session(tunnel, session_id);
864309795f4SJames Chapman if (!session || !session->recv_skb) {
865a4346210SGuillaume Nault if (session)
86661b9a047SGuillaume Nault l2tp_session_dec_refcount(session);
86761b9a047SGuillaume Nault
868fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */
8693e59e885SMatthias Schiffer pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n",
870fd558d18SJames Chapman tunnel->name, tunnel_id, session_id);
8713e59e885SMatthias Schiffer goto pass;
872fd558d18SJames Chapman }
873fd558d18SJames Chapman
8744522a70dSJacob Wen if (tunnel->version == L2TP_HDR_VER_3 &&
8759b6ff7ebSXiyu Yang l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
8769b6ff7ebSXiyu Yang l2tp_session_dec_refcount(session);
8773e59e885SMatthias Schiffer goto invalid;
8789b6ff7ebSXiyu Yang }
8794522a70dSJacob Wen
8802b139e6bSGuillaume Nault l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
88161b9a047SGuillaume Nault l2tp_session_dec_refcount(session);
882fd558d18SJames Chapman
883fd558d18SJames Chapman return 0;
884fd558d18SJames Chapman
8853e59e885SMatthias Schiffer invalid:
8863e59e885SMatthias Schiffer atomic_long_inc(&tunnel->stats.rx_invalid);
8873e59e885SMatthias Schiffer
8883e59e885SMatthias Schiffer pass:
889fd558d18SJames Chapman /* Put UDP header back */
890fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr));
891fd558d18SJames Chapman
892fd558d18SJames Chapman return 1;
893fd558d18SJames Chapman }
894fd558d18SJames Chapman
895fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c.
896fd558d18SJames Chapman * Return codes:
897fd558d18SJames Chapman * 0 : success.
898fd558d18SJames Chapman * <0: error
899fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP.
900fd558d18SJames Chapman */
l2tp_udp_encap_recv(struct sock * sk,struct sk_buff * skb)901fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
902fd558d18SJames Chapman {
903fd558d18SJames Chapman struct l2tp_tunnel *tunnel;
904fd558d18SJames Chapman
90545faeff1STom Parkin /* Note that this is called from the encap_rcv hook inside an
90645faeff1STom Parkin * RCU-protected region, but without the socket being locked.
90745faeff1STom Parkin * Hence we use rcu_dereference_sk_user_data to access the
90845faeff1STom Parkin * tunnel data structure rather the usual l2tp_sk_to_tunnel
90945faeff1STom Parkin * accessor function.
91045faeff1STom Parkin */
911c1c47721SEric Dumazet tunnel = rcu_dereference_sk_user_data(sk);
9120febc7b3STom Parkin if (!tunnel)
913fd558d18SJames Chapman goto pass_up;
91445faeff1STom Parkin if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
91545faeff1STom Parkin goto pass_up;
916fd558d18SJames Chapman
9172b139e6bSGuillaume Nault if (l2tp_udp_recv_core(tunnel, skb))
918d00fa9adSJames Chapman goto pass_up;
919fd558d18SJames Chapman
920fd558d18SJames Chapman return 0;
921fd558d18SJames Chapman
922fd558d18SJames Chapman pass_up:
923fd558d18SJames Chapman return 1;
924fd558d18SJames Chapman }
925fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
926fd558d18SJames Chapman
927fd558d18SJames Chapman /************************************************************************
928fd558d18SJames Chapman * Transmit handling
929fd558d18SJames Chapman ***********************************************************************/
930fd558d18SJames Chapman
931fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided.
932fd558d18SJames Chapman */
l2tp_build_l2tpv2_header(struct l2tp_session * session,void * buf)933f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
934fd558d18SJames Chapman {
935f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel;
936fd558d18SJames Chapman __be16 *bufp = buf;
937f7faffa3SJames Chapman __be16 *optr = buf;
938fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2;
939fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id;
940fd558d18SJames Chapman u32 session_id = session->peer_session_id;
941fd558d18SJames Chapman
942fd558d18SJames Chapman if (session->send_seq)
943fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S;
944fd558d18SJames Chapman
945fd558d18SJames Chapman /* Setup L2TP header. */
946fd558d18SJames Chapman *bufp++ = htons(flags);
947fd558d18SJames Chapman *bufp++ = htons(tunnel_id);
948fd558d18SJames Chapman *bufp++ = htons(session_id);
949fd558d18SJames Chapman if (session->send_seq) {
950fd558d18SJames Chapman *bufp++ = htons(session->ns);
951fd558d18SJames Chapman *bufp++ = 0;
952fd558d18SJames Chapman session->ns++;
953f7faffa3SJames Chapman session->ns &= 0xffff;
9546b7bdcd7STom Parkin trace_session_seqnum_update(session);
955fd558d18SJames Chapman }
956fd558d18SJames Chapman
957f7faffa3SJames Chapman return bufp - optr;
958f7faffa3SJames Chapman }
959f7faffa3SJames Chapman
l2tp_build_l2tpv3_header(struct l2tp_session * session,void * buf)960f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
961fd558d18SJames Chapman {
9620d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel;
963f7faffa3SJames Chapman char *bufp = buf;
964f7faffa3SJames Chapman char *optr = bufp;
965fd558d18SJames Chapman
9660d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and
9670d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags.
9680d76751fSJames Chapman */
9690d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
9700d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3;
971f7faffa3SJames Chapman *((__be16 *)bufp) = htons(flags);
972f7faffa3SJames Chapman bufp += 2;
973f7faffa3SJames Chapman *((__be16 *)bufp) = 0;
974f7faffa3SJames Chapman bufp += 2;
9750d76751fSJames Chapman }
9760d76751fSJames Chapman
977f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(session->peer_session_id);
978f7faffa3SJames Chapman bufp += 4;
979f7faffa3SJames Chapman if (session->cookie_len) {
980f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len);
981f7faffa3SJames Chapman bufp += session->cookie_len;
982fd558d18SJames Chapman }
983f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
984f7faffa3SJames Chapman u32 l2h = 0;
98562e7b6a5SLorenzo Bianconi
986f7faffa3SJames Chapman if (session->send_seq) {
987f7faffa3SJames Chapman l2h = 0x40000000 | session->ns;
988f7faffa3SJames Chapman session->ns++;
989f7faffa3SJames Chapman session->ns &= 0xffffff;
9906b7bdcd7STom Parkin trace_session_seqnum_update(session);
991f7faffa3SJames Chapman }
992f7faffa3SJames Chapman
993f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(l2h);
99462e7b6a5SLorenzo Bianconi bufp += 4;
995f7faffa3SJames Chapman }
996f7faffa3SJames Chapman
997f7faffa3SJames Chapman return bufp - optr;
998f7faffa3SJames Chapman }
999fd558d18SJames Chapman
1000de68b039STom Parkin /* Queue the packet to IP for output: tunnel socket lock must be held */
l2tp_xmit_queue(struct l2tp_tunnel * tunnel,struct sk_buff * skb,struct flowi * fl)1001de68b039STom Parkin static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
1002fd558d18SJames Chapman {
1003de68b039STom Parkin int err;
1004fd558d18SJames Chapman
100560ff7467SWANG Cong skb->ignore_df = 1;
100627d53323SXin Long skb_dst_drop(skb);
1007d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1008b954f940SPaolo Abeni if (l2tp_sk_is_v6(tunnel->sock))
1009de68b039STom Parkin err = inet6_csk_xmit(tunnel->sock, skb, NULL);
1010d2cf3361SBenjamin LaHaise else
1011d2cf3361SBenjamin LaHaise #endif
1012de68b039STom Parkin err = ip_queue_xmit(tunnel->sock, skb, fl);
1013fd558d18SJames Chapman
1014de68b039STom Parkin return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
1015fd558d18SJames Chapman }
1016fd558d18SJames Chapman
l2tp_xmit_core(struct l2tp_session * session,struct sk_buff * skb,unsigned int * len)1017f52e4b27STom Parkin static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
1018fd558d18SJames Chapman {
10190d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel;
1020de68b039STom Parkin unsigned int data_len = skb->len;
10210d76751fSJames Chapman struct sock *sk = tunnel->sock;
1022de68b039STom Parkin int headroom, uhlen, udp_len;
1023b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS;
1024de68b039STom Parkin struct inet_sock *inet;
1025de68b039STom Parkin struct udphdr *uh;
1026fd558d18SJames Chapman
1027fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP,
1028fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to
1029fd558d18SJames Chapman * make room. Adjust truesize.
1030fd558d18SJames Chapman */
1031de68b039STom Parkin uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
1032de68b039STom Parkin headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
1033835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) {
1034b8c84307SEric Dumazet kfree_skb(skb);
1035b8c84307SEric Dumazet return NET_XMIT_DROP;
1036835acf5dSEric Dumazet }
1037fd558d18SJames Chapman
1038fd558d18SJames Chapman /* Setup L2TP header */
10392dedab6fSTom Parkin if (tunnel->version == L2TP_HDR_VER_2)
1040efe05278STom Parkin l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
10412dedab6fSTom Parkin else
1042efe05278STom Parkin l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
1043fd558d18SJames Chapman
10440d76751fSJames Chapman /* Reset skb netfilter state */
1045fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1046de68b039STom Parkin IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
1047895b5c9fSFlorian Westphal nf_reset_ct(skb);
1048fd558d18SJames Chapman
1049*207e8815SJames Chapman /* L2TP uses its own lockdep subclass to avoid lockdep splats caused by
1050*207e8815SJames Chapman * nested socket calls on the same lockdep socket class. This can
1051*207e8815SJames Chapman * happen when data from a user socket is routed over l2tp, which uses
1052*207e8815SJames Chapman * another userspace socket.
1053*207e8815SJames Chapman */
1054*207e8815SJames Chapman spin_lock_nested(&sk->sk_lock.slock, L2TP_DEPTH_NESTING);
1055*207e8815SJames Chapman
10566af88da1SDavid S. Miller if (sock_owned_by_user(sk)) {
1057b8c84307SEric Dumazet kfree_skb(skb);
1058b8c84307SEric Dumazet ret = NET_XMIT_DROP;
10596af88da1SDavid S. Miller goto out_unlock;
10606af88da1SDavid S. Miller }
10616af88da1SDavid S. Miller
1062b954f940SPaolo Abeni /* The user-space may change the connection status for the user-space
1063b954f940SPaolo Abeni * provided socket at run time: we must check it under the socket lock
1064b954f940SPaolo Abeni */
1065b954f940SPaolo Abeni if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1066b954f940SPaolo Abeni kfree_skb(skb);
1067b954f940SPaolo Abeni ret = NET_XMIT_DROP;
1068b954f940SPaolo Abeni goto out_unlock;
1069b954f940SPaolo Abeni }
1070b954f940SPaolo Abeni
1071f52e4b27STom Parkin /* Report transmitted length before we add encap header, which keeps
1072f52e4b27STom Parkin * statistics consistent for both UDP and IP encap tx/rx paths.
1073f52e4b27STom Parkin */
1074f52e4b27STom Parkin *len = skb->len;
1075f52e4b27STom Parkin
1076d9d8da80SDavid S. Miller inet = inet_sk(sk);
10770d76751fSJames Chapman switch (tunnel->encap) {
10780d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP:
10790d76751fSJames Chapman /* Setup UDP header */
10800d76751fSJames Chapman __skb_push(skb, sizeof(*uh));
10810d76751fSJames Chapman skb_reset_transport_header(skb);
10820d76751fSJames Chapman uh = udp_hdr(skb);
10830d76751fSJames Chapman uh->source = inet->inet_sport;
10840d76751fSJames Chapman uh->dest = inet->inet_dport;
1085efe05278STom Parkin udp_len = uhlen + session->hdr_len + data_len;
10860d76751fSJames Chapman uh->len = htons(udp_len);
1087fd558d18SJames Chapman
1088fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */
1089d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1090b954f940SPaolo Abeni if (l2tp_sk_is_v6(sk))
109177157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk),
109277157e19STom Herbert skb, &inet6_sk(sk)->saddr,
109377157e19STom Herbert &sk->sk_v6_daddr, udp_len);
1094d2cf3361SBenjamin LaHaise else
1095d2cf3361SBenjamin LaHaise #endif
109677157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
109777157e19STom Herbert inet->inet_daddr, udp_len);
10980d76751fSJames Chapman break;
10990d76751fSJames Chapman
11000d76751fSJames Chapman case L2TP_ENCAPTYPE_IP:
11010d76751fSJames Chapman break;
11020d76751fSJames Chapman }
11030d76751fSJames Chapman
1104de68b039STom Parkin ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
1105de68b039STom Parkin
11066af88da1SDavid S. Miller out_unlock:
1107*207e8815SJames Chapman spin_unlock(&sk->sk_lock.slock);
1108fd558d18SJames Chapman
1109b8c84307SEric Dumazet return ret;
1110fd558d18SJames Chapman }
1111de68b039STom Parkin
1112de68b039STom Parkin /* If caller requires the skb to have a ppp header, the header must be
1113de68b039STom Parkin * inserted in the skb data before calling this function.
1114de68b039STom Parkin */
l2tp_xmit_skb(struct l2tp_session * session,struct sk_buff * skb)1115de68b039STom Parkin int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
1116de68b039STom Parkin {
1117f52e4b27STom Parkin unsigned int len = 0;
1118de68b039STom Parkin int ret;
1119de68b039STom Parkin
1120f52e4b27STom Parkin ret = l2tp_xmit_core(session, skb, &len);
1121de68b039STom Parkin if (ret == NET_XMIT_SUCCESS) {
1122de68b039STom Parkin atomic_long_inc(&session->tunnel->stats.tx_packets);
1123de68b039STom Parkin atomic_long_add(len, &session->tunnel->stats.tx_bytes);
1124de68b039STom Parkin atomic_long_inc(&session->stats.tx_packets);
1125de68b039STom Parkin atomic_long_add(len, &session->stats.tx_bytes);
1126de68b039STom Parkin } else {
1127de68b039STom Parkin atomic_long_inc(&session->tunnel->stats.tx_errors);
1128de68b039STom Parkin atomic_long_inc(&session->stats.tx_errors);
1129de68b039STom Parkin }
1130de68b039STom Parkin return ret;
1131de68b039STom Parkin }
1132fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1133fd558d18SJames Chapman
1134fd558d18SJames Chapman /*****************************************************************************
1135fd558d18SJames Chapman * Tinnel and session create/destroy.
1136fd558d18SJames Chapman *****************************************************************************/
1137fd558d18SJames Chapman
1138fd558d18SJames Chapman /* Tunnel socket destruct hook.
1139fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been
1140fd558d18SJames Chapman * closed.
1141fd558d18SJames Chapman */
l2tp_tunnel_destruct(struct sock * sk)1142fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk)
1143fd558d18SJames Chapman {
114445faeff1STom Parkin struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
1145fd558d18SJames Chapman
11460febc7b3STom Parkin if (!tunnel)
1147fd558d18SJames Chapman goto end;
1148fd558d18SJames Chapman
1149f8ccac0eSTom Parkin /* Disable udp encapsulation */
11500d76751fSJames Chapman switch (tunnel->encap) {
11510d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP:
1152fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */
11534781a75dSEric Dumazet WRITE_ONCE(udp_sk(sk)->encap_type, 0);
11544781a75dSEric Dumazet udp_sk(sk)->encap_rcv = NULL;
11554781a75dSEric Dumazet udp_sk(sk)->encap_destroy = NULL;
11560d76751fSJames Chapman break;
11570d76751fSJames Chapman case L2TP_ENCAPTYPE_IP:
11580d76751fSJames Chapman break;
11590d76751fSJames Chapman }
1160fd558d18SJames Chapman
1161fd558d18SJames Chapman /* Remove hooks into tunnel socket */
1162b68777d5SJakub Sitnicki write_lock_bh(&sk->sk_callback_lock);
1163fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct;
1164fd558d18SJames Chapman sk->sk_user_data = NULL;
1165b68777d5SJakub Sitnicki write_unlock_bh(&sk->sk_callback_lock);
1166f8ccac0eSTom Parkin
1167fd558d18SJames Chapman /* Call the original destructor */
1168fd558d18SJames Chapman if (sk->sk_destruct)
1169fd558d18SJames Chapman (*sk->sk_destruct)(sk);
1170d00fa9adSJames Chapman
1171d00fa9adSJames Chapman kfree_rcu(tunnel, rcu);
1172fd558d18SJames Chapman end:
1173fd558d18SJames Chapman return;
1174fd558d18SJames Chapman }
1175fd558d18SJames Chapman
1176b2aecfe8STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. */
l2tp_session_unhash(struct l2tp_session * session)1177b2aecfe8STom Parkin static void l2tp_session_unhash(struct l2tp_session *session)
1178b2aecfe8STom Parkin {
1179b2aecfe8STom Parkin struct l2tp_tunnel *tunnel = session->tunnel;
1180b2aecfe8STom Parkin
1181b2aecfe8STom Parkin /* Remove the session from core hashes */
1182b2aecfe8STom Parkin if (tunnel) {
1183b2aecfe8STom Parkin /* Remove from the per-tunnel hash */
118407b8ca37STom Parkin spin_lock_bh(&tunnel->hlist_lock);
118507b8ca37STom Parkin hlist_del_init_rcu(&session->hlist);
118607b8ca37STom Parkin spin_unlock_bh(&tunnel->hlist_lock);
1187b2aecfe8STom Parkin
1188b2aecfe8STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */
1189b2aecfe8STom Parkin if (tunnel->version != L2TP_HDR_VER_2) {
1190b2aecfe8STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1191b2aecfe8STom Parkin
1192b2aecfe8STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock);
1193b2aecfe8STom Parkin hlist_del_init_rcu(&session->global_hlist);
1194b2aecfe8STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1195b2aecfe8STom Parkin }
119607b8ca37STom Parkin
119707b8ca37STom Parkin synchronize_rcu();
1198b2aecfe8STom Parkin }
1199b2aecfe8STom Parkin }
1200b2aecfe8STom Parkin
1201fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too.
1202fd558d18SJames Chapman */
l2tp_tunnel_closeall(struct l2tp_tunnel * tunnel)1203d08532bbSGuillaume Nault static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1204fd558d18SJames Chapman {
1205fd558d18SJames Chapman struct l2tp_session *session;
120607b8ca37STom Parkin int hash;
1207fd558d18SJames Chapman
120807b8ca37STom Parkin spin_lock_bh(&tunnel->hlist_lock);
1209f3c66d4eSGuillaume Nault tunnel->acpt_newsess = false;
1210fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1211fd558d18SJames Chapman again:
121207b8ca37STom Parkin hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
121307b8ca37STom Parkin hlist_del_init_rcu(&session->hlist);
1214fd558d18SJames Chapman
121507b8ca37STom Parkin spin_unlock_bh(&tunnel->hlist_lock);
12169d319a8eSTom Parkin l2tp_session_delete(session);
121707b8ca37STom Parkin spin_lock_bh(&tunnel->hlist_lock);
1218fd558d18SJames Chapman
1219fd558d18SJames Chapman /* Now restart from the beginning of this hash
1220fd558d18SJames Chapman * chain. We always remove a session from the
1221fd558d18SJames Chapman * list so we are guaranteed to make forward
1222fd558d18SJames Chapman * progress.
1223fd558d18SJames Chapman */
1224fd558d18SJames Chapman goto again;
1225fd558d18SJames Chapman }
1226fd558d18SJames Chapman }
122707b8ca37STom Parkin spin_unlock_bh(&tunnel->hlist_lock);
1228fd558d18SJames Chapman }
1229fd558d18SJames Chapman
12309980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */
l2tp_udp_encap_destroy(struct sock * sk)12319980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk)
12329980d001STom Parkin {
123345faeff1STom Parkin struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
1234d00fa9adSJames Chapman
1235d00fa9adSJames Chapman if (tunnel)
1236d00fa9adSJames Chapman l2tp_tunnel_delete(tunnel);
12379980d001STom Parkin }
12389980d001STom Parkin
l2tp_tunnel_remove(struct net * net,struct l2tp_tunnel * tunnel)1239c4d48a58SCong Wang static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
1240c4d48a58SCong Wang {
1241c4d48a58SCong Wang struct l2tp_net *pn = l2tp_pernet(net);
1242c4d48a58SCong Wang
1243c4d48a58SCong Wang spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1244c4d48a58SCong Wang idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
1245c4d48a58SCong Wang spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1246c4d48a58SCong Wang }
1247c4d48a58SCong Wang
1248f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */
l2tp_tunnel_del_work(struct work_struct * work)1249f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work)
1250f8ccac0eSTom Parkin {
1251d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1252d00fa9adSJames Chapman del_work);
1253d00fa9adSJames Chapman struct sock *sk = tunnel->sock;
1254d00fa9adSJames Chapman struct socket *sock = sk->sk_socket;
125512d656afSRidge Kennedy
125612d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel);
125712d656afSRidge Kennedy
125876a6abdbSJames Chapman /* If the tunnel socket was created within the kernel, use
125902d13ed5STom Parkin * the sk API to release it here.
1260f8ccac0eSTom Parkin */
126176a6abdbSJames Chapman if (tunnel->fd < 0) {
126226abe143SEric W. Biederman if (sock) {
1263167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR);
126426abe143SEric W. Biederman sock_release(sock);
126526abe143SEric W. Biederman }
1266167eb17eSTom Parkin }
1267f8ccac0eSTom Parkin
1268c4d48a58SCong Wang l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
1269d00fa9adSJames Chapman /* drop initial ref */
1270d00fa9adSJames Chapman l2tp_tunnel_dec_refcount(tunnel);
1271d00fa9adSJames Chapman
1272d00fa9adSJames Chapman /* drop workqueue ref */
127306a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel);
1274fd558d18SJames Chapman }
1275fd558d18SJames Chapman
1276789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by
1277789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no
1278789a4a2cSJames Chapman * managing L2TP daemon.
1279167eb17eSTom Parkin *
1280167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by
1281167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation.
1282167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet
1283167eb17eSTom Parkin * exit hook.
1284789a4a2cSJames Chapman */
l2tp_tunnel_sock_create(struct net * net,u32 tunnel_id,u32 peer_tunnel_id,struct l2tp_tunnel_cfg * cfg,struct socket ** sockp)1285167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net,
1286167eb17eSTom Parkin u32 tunnel_id,
1287167eb17eSTom Parkin u32 peer_tunnel_id,
1288167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg,
1289167eb17eSTom Parkin struct socket **sockp)
1290789a4a2cSJames Chapman {
1291789a4a2cSJames Chapman int err = -EINVAL;
12927bddd0dbSEric Dumazet struct socket *sock = NULL;
129385644b4dSTom Herbert struct udp_port_cfg udp_conf;
1294789a4a2cSJames Chapman
1295789a4a2cSJames Chapman switch (cfg->encap) {
1296789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP:
129785644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf));
129885644b4dSTom Herbert
1299f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1300f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) {
130185644b4dSTom Herbert udp_conf.family = AF_INET6;
130285644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6,
130385644b4dSTom Herbert sizeof(udp_conf.local_ip6));
130485644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
130585644b4dSTom Herbert sizeof(udp_conf.peer_ip6));
130685644b4dSTom Herbert udp_conf.use_udp6_tx_checksums =
1307018f8258SWang Shanker !cfg->udp6_zero_tx_checksums;
130885644b4dSTom Herbert udp_conf.use_udp6_rx_checksums =
1309018f8258SWang Shanker !cfg->udp6_zero_rx_checksums;
1310f9bac8dfSChris Elston } else
1311f9bac8dfSChris Elston #endif
1312f9bac8dfSChris Elston {
131385644b4dSTom Herbert udp_conf.family = AF_INET;
131485644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip;
131585644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip;
131685644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1317f9bac8dfSChris Elston }
1318789a4a2cSJames Chapman
131985644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port);
132085644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
132185644b4dSTom Herbert
132285644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock);
132385644b4dSTom Herbert if (err < 0)
132485644b4dSTom Herbert goto out;
1325789a4a2cSJames Chapman
1326789a4a2cSJames Chapman break;
1327789a4a2cSJames Chapman
1328789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP:
1329f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1330f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) {
133185644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0};
133285644b4dSTom Herbert
133326abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1334167eb17eSTom Parkin IPPROTO_L2TP, &sock);
13355dac94e1SJames Chapman if (err < 0)
1336f9bac8dfSChris Elston goto out;
13375dac94e1SJames Chapman
13385dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6;
13395dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
13405dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr));
13415dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id;
13425dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
13435dac94e1SJames Chapman sizeof(ip6_addr));
13445dac94e1SJames Chapman if (err < 0)
13455dac94e1SJames Chapman goto out;
13465dac94e1SJames Chapman
13475dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6;
13485dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
13495dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr));
13505dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id;
13515dac94e1SJames Chapman err = kernel_connect(sock,
13525dac94e1SJames Chapman (struct sockaddr *)&ip6_addr,
13535dac94e1SJames Chapman sizeof(ip6_addr), 0);
13545dac94e1SJames Chapman if (err < 0)
13555dac94e1SJames Chapman goto out;
13565dac94e1SJames Chapman } else
1357f9bac8dfSChris Elston #endif
13585dac94e1SJames Chapman {
135985644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0};
136085644b4dSTom Herbert
136126abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1362167eb17eSTom Parkin IPPROTO_L2TP, &sock);
1363789a4a2cSJames Chapman if (err < 0)
1364789a4a2cSJames Chapman goto out;
1365789a4a2cSJames Chapman
1366789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET;
1367789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip;
1368789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id;
13695dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
13705dac94e1SJames Chapman sizeof(ip_addr));
1371789a4a2cSJames Chapman if (err < 0)
1372789a4a2cSJames Chapman goto out;
1373789a4a2cSJames Chapman
1374789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET;
1375789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip;
1376789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id;
13775dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
13785dac94e1SJames Chapman sizeof(ip_addr), 0);
1379789a4a2cSJames Chapman if (err < 0)
1380789a4a2cSJames Chapman goto out;
13815dac94e1SJames Chapman }
1382789a4a2cSJames Chapman break;
1383789a4a2cSJames Chapman
1384789a4a2cSJames Chapman default:
1385789a4a2cSJames Chapman goto out;
1386789a4a2cSJames Chapman }
1387789a4a2cSJames Chapman
1388789a4a2cSJames Chapman out:
1389167eb17eSTom Parkin *sockp = sock;
13906c0ec37bSTom Parkin if (err < 0 && sock) {
1391167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR);
139226abe143SEric W. Biederman sock_release(sock);
1393789a4a2cSJames Chapman *sockp = NULL;
1394789a4a2cSJames Chapman }
1395789a4a2cSJames Chapman
1396789a4a2cSJames Chapman return err;
1397789a4a2cSJames Chapman }
1398789a4a2cSJames Chapman
l2tp_tunnel_create(int fd,int version,u32 tunnel_id,u32 peer_tunnel_id,struct l2tp_tunnel_cfg * cfg,struct l2tp_tunnel ** tunnelp)1399c9ccd4c6STom Parkin int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
1400c0235fb3STom Parkin struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1401fd558d18SJames Chapman {
1402fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL;
1403fd558d18SJames Chapman int err;
14040d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1405fd558d18SJames Chapman
14060febc7b3STom Parkin if (cfg)
14070d76751fSJames Chapman encap = cfg->encap;
14080d76751fSJames Chapman
140970c05bfaSTom Parkin tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
14100febc7b3STom Parkin if (!tunnel) {
1411fd558d18SJames Chapman err = -ENOMEM;
1412fd558d18SJames Chapman goto err;
1413fd558d18SJames Chapman }
1414fd558d18SJames Chapman
1415fd558d18SJames Chapman tunnel->version = version;
1416fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id;
1417fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id;
1418fd558d18SJames Chapman
1419fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC;
1420fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
142107b8ca37STom Parkin spin_lock_init(&tunnel->hlist_lock);
1422f3c66d4eSGuillaume Nault tunnel->acpt_newsess = true;
1423fd558d18SJames Chapman
14240d76751fSJames Chapman tunnel->encap = encap;
1425fd558d18SJames Chapman
1426d00fa9adSJames Chapman refcount_set(&tunnel->ref_count, 1);
1427d00fa9adSJames Chapman tunnel->fd = fd;
1428d00fa9adSJames Chapman
1429f8ccac0eSTom Parkin /* Init delete workqueue struct */
1430f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1431f8ccac0eSTom Parkin
1432fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list);
1433fd558d18SJames Chapman
1434fd558d18SJames Chapman err = 0;
1435fd558d18SJames Chapman err:
1436fd558d18SJames Chapman if (tunnelp)
1437fd558d18SJames Chapman *tunnelp = tunnel;
1438fd558d18SJames Chapman
1439fd558d18SJames Chapman return err;
1440fd558d18SJames Chapman }
1441fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1442fd558d18SJames Chapman
l2tp_validate_socket(const struct sock * sk,const struct net * net,enum l2tp_encap_type encap)14436b9f3423SGuillaume Nault static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
14446b9f3423SGuillaume Nault enum l2tp_encap_type encap)
14456b9f3423SGuillaume Nault {
14466b9f3423SGuillaume Nault if (!net_eq(sock_net(sk), net))
14476b9f3423SGuillaume Nault return -EINVAL;
14486b9f3423SGuillaume Nault
14496b9f3423SGuillaume Nault if (sk->sk_type != SOCK_DGRAM)
14506b9f3423SGuillaume Nault return -EPROTONOSUPPORT;
14516b9f3423SGuillaume Nault
1452d9a81a22SEric Dumazet if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1453d9a81a22SEric Dumazet return -EPROTONOSUPPORT;
1454d9a81a22SEric Dumazet
14556b9f3423SGuillaume Nault if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
14566b9f3423SGuillaume Nault (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
14576b9f3423SGuillaume Nault return -EPROTONOSUPPORT;
14586b9f3423SGuillaume Nault
14596b9f3423SGuillaume Nault if (sk->sk_user_data)
14606b9f3423SGuillaume Nault return -EBUSY;
14616b9f3423SGuillaume Nault
14626b9f3423SGuillaume Nault return 0;
14636b9f3423SGuillaume Nault }
14646b9f3423SGuillaume Nault
l2tp_tunnel_register(struct l2tp_tunnel * tunnel,struct net * net,struct l2tp_tunnel_cfg * cfg)14656b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
14666b9f3423SGuillaume Nault struct l2tp_tunnel_cfg *cfg)
14676b9f3423SGuillaume Nault {
1468c4d48a58SCong Wang struct l2tp_net *pn = l2tp_pernet(net);
1469c4d48a58SCong Wang u32 tunnel_id = tunnel->tunnel_id;
14706b9f3423SGuillaume Nault struct socket *sock;
14716b9f3423SGuillaume Nault struct sock *sk;
14726b9f3423SGuillaume Nault int ret;
14736b9f3423SGuillaume Nault
1474c4d48a58SCong Wang spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1475c4d48a58SCong Wang ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
1476c4d48a58SCong Wang GFP_ATOMIC);
1477c4d48a58SCong Wang spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1478c4d48a58SCong Wang if (ret)
1479c4d48a58SCong Wang return ret == -ENOSPC ? -EEXIST : ret;
1480c4d48a58SCong Wang
14816b9f3423SGuillaume Nault if (tunnel->fd < 0) {
14826b9f3423SGuillaume Nault ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
14836b9f3423SGuillaume Nault tunnel->peer_tunnel_id, cfg,
14846b9f3423SGuillaume Nault &sock);
14856b9f3423SGuillaume Nault if (ret < 0)
14866b9f3423SGuillaume Nault goto err;
14876b9f3423SGuillaume Nault } else {
14886b9f3423SGuillaume Nault sock = sockfd_lookup(tunnel->fd, &ret);
14896b9f3423SGuillaume Nault if (!sock)
14906b9f3423SGuillaume Nault goto err;
1491b68777d5SJakub Sitnicki }
14926b9f3423SGuillaume Nault
1493b68777d5SJakub Sitnicki sk = sock->sk;
14940b2c5972SCong Wang lock_sock(sk);
1495af295e85SJakub Sitnicki write_lock_bh(&sk->sk_callback_lock);
1496b68777d5SJakub Sitnicki ret = l2tp_validate_socket(sk, net, tunnel->encap);
1497b9fb10d1SEric Dumazet if (ret < 0)
1498af295e85SJakub Sitnicki goto err_inval_sock;
1499af295e85SJakub Sitnicki rcu_assign_sk_user_data(sk, tunnel);
1500af295e85SJakub Sitnicki write_unlock_bh(&sk->sk_callback_lock);
15016b9f3423SGuillaume Nault
15026b9f3423SGuillaume Nault if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
15036b9f3423SGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = {
15046b9f3423SGuillaume Nault .sk_user_data = tunnel,
15056b9f3423SGuillaume Nault .encap_type = UDP_ENCAP_L2TPINUDP,
15066b9f3423SGuillaume Nault .encap_rcv = l2tp_udp_encap_recv,
15076b9f3423SGuillaume Nault .encap_destroy = l2tp_udp_encap_destroy,
15086b9f3423SGuillaume Nault };
15096b9f3423SGuillaume Nault
15106b9f3423SGuillaume Nault setup_udp_tunnel_sock(net, sock, &udp_cfg);
15116b9f3423SGuillaume Nault }
15126b9f3423SGuillaume Nault
15136b9f3423SGuillaume Nault tunnel->old_sk_destruct = sk->sk_destruct;
15146b9f3423SGuillaume Nault sk->sk_destruct = &l2tp_tunnel_destruct;
15156b9f3423SGuillaume Nault sk->sk_allocation = GFP_ATOMIC;
15160b2c5972SCong Wang release_sock(sk);
15170b2c5972SCong Wang
15180b2c5972SCong Wang sock_hold(sk);
15190b2c5972SCong Wang tunnel->sock = sk;
15200b2c5972SCong Wang tunnel->l2tp_net = net;
15210b2c5972SCong Wang
15220b2c5972SCong Wang spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
15230b2c5972SCong Wang idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
15240b2c5972SCong Wang spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
15256b9f3423SGuillaume Nault
15266b7bdcd7STom Parkin trace_register_tunnel(tunnel);
15276b7bdcd7STom Parkin
15286b9f3423SGuillaume Nault if (tunnel->fd >= 0)
15296b9f3423SGuillaume Nault sockfd_put(sock);
15306b9f3423SGuillaume Nault
15316b9f3423SGuillaume Nault return 0;
15326b9f3423SGuillaume Nault
1533af295e85SJakub Sitnicki err_inval_sock:
1534af295e85SJakub Sitnicki write_unlock_bh(&sk->sk_callback_lock);
1535b9fb10d1SEric Dumazet release_sock(sk);
1536af295e85SJakub Sitnicki
1537f6cd651bSGuillaume Nault if (tunnel->fd < 0)
1538f6cd651bSGuillaume Nault sock_release(sock);
1539f6cd651bSGuillaume Nault else
15406b9f3423SGuillaume Nault sockfd_put(sock);
15416b9f3423SGuillaume Nault err:
1542c4d48a58SCong Wang l2tp_tunnel_remove(net, tunnel);
15436b9f3423SGuillaume Nault return ret;
15446b9f3423SGuillaume Nault }
15456b9f3423SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
15466b9f3423SGuillaume Nault
1547309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command.
1548309795f4SJames Chapman */
l2tp_tunnel_delete(struct l2tp_tunnel * tunnel)154962b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1550309795f4SJames Chapman {
155162b982eeSSabrina Dubroca if (!test_and_set_bit(0, &tunnel->dead)) {
15526b7bdcd7STom Parkin trace_delete_tunnel(tunnel);
155306a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel);
155462b982eeSSabrina Dubroca queue_work(l2tp_wq, &tunnel->del_work);
155506a15f51SAlexander Couzens }
1556309795f4SJames Chapman }
1557309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1558309795f4SJames Chapman
l2tp_session_delete(struct l2tp_session * session)1559628703f5STom Parkin void l2tp_session_delete(struct l2tp_session *session)
1560309795f4SJames Chapman {
1561b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead))
1562628703f5STom Parkin return;
1563b228a940SGuillaume Nault
15646b7bdcd7STom Parkin trace_delete_session(session);
1565b2aecfe8STom Parkin l2tp_session_unhash(session);
15664c6e2fd3STom Parkin l2tp_session_queue_purge(session);
15670febc7b3STom Parkin if (session->session_close)
1568309795f4SJames Chapman (*session->session_close)(session);
1569a4346210SGuillaume Nault
1570309795f4SJames Chapman l2tp_session_dec_refcount(session);
1571309795f4SJames Chapman }
1572309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete);
1573309795f4SJames Chapman
1574f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or
157562e7b6a5SLorenzo Bianconi * l2specific_type parameters are set.
1576f7faffa3SJames Chapman */
l2tp_session_set_header_len(struct l2tp_session * session,int version)1577bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1578f7faffa3SJames Chapman {
1579f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) {
1580f7faffa3SJames Chapman session->hdr_len = 6;
1581f7faffa3SJames Chapman if (session->send_seq)
1582f7faffa3SJames Chapman session->hdr_len += 4;
1583f7faffa3SJames Chapman } else {
158462e7b6a5SLorenzo Bianconi session->hdr_len = 4 + session->cookie_len;
158562e7b6a5SLorenzo Bianconi session->hdr_len += l2tp_get_l2specific_len(session);
15860d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
15870d76751fSJames Chapman session->hdr_len += 4;
1588f7faffa3SJames Chapman }
1589f7faffa3SJames Chapman }
1590bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1591f7faffa3SJames Chapman
l2tp_session_create(int priv_size,struct l2tp_tunnel * tunnel,u32 session_id,u32 peer_session_id,struct l2tp_session_cfg * cfg)1592c0235fb3STom Parkin struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1593c0235fb3STom Parkin u32 peer_session_id, struct l2tp_session_cfg *cfg)
1594fd558d18SJames Chapman {
1595fd558d18SJames Chapman struct l2tp_session *session;
1596fd558d18SJames Chapman
159770c05bfaSTom Parkin session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
15980febc7b3STom Parkin if (session) {
1599fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC;
1600fd558d18SJames Chapman session->tunnel = tunnel;
1601fd558d18SJames Chapman
1602fd558d18SJames Chapman session->session_id = session_id;
1603fd558d18SJames Chapman session->peer_session_id = peer_session_id;
1604d301e325SJames Chapman session->nr = 0;
16058a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2)
16068a1631d5SJames Chapman session->nr_max = 0xffff;
16078a1631d5SJames Chapman else
16088a1631d5SJames Chapman session->nr_max = 0xffffff;
16098a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2;
1610a0dbd822SJames Chapman session->nr_oos_count_max = 4;
1611a0dbd822SJames Chapman
1612a0dbd822SJames Chapman /* Use NR of first received packet */
1613a0dbd822SJames Chapman session->reorder_skip = 1;
1614fd558d18SJames Chapman
1615fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u",
1616fd558d18SJames Chapman tunnel->tunnel_id, session->session_id);
1617fd558d18SJames Chapman
1618fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q);
1619fd558d18SJames Chapman
1620fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist);
1621f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist);
1622fd558d18SJames Chapman
1623fd558d18SJames Chapman if (cfg) {
1624f7faffa3SJames Chapman session->pwtype = cfg->pw_type;
1625fd558d18SJames Chapman session->send_seq = cfg->send_seq;
1626fd558d18SJames Chapman session->recv_seq = cfg->recv_seq;
1627fd558d18SJames Chapman session->lns_mode = cfg->lns_mode;
1628f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout;
1629f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type;
1630f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len;
1631f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1632f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len;
1633f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1634fd558d18SJames Chapman }
1635fd558d18SJames Chapman
1636f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version);
1637f7faffa3SJames Chapman
16389ee369a4SGuillaume Nault refcount_set(&session->ref_count, 1);
16399ee369a4SGuillaume Nault
1640fd558d18SJames Chapman return session;
1641fd558d18SJames Chapman }
1642dbdbc73bSGuillaume Nault
1643dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM);
1644dbdbc73bSGuillaume Nault }
1645fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create);
1646fd558d18SJames Chapman
1647fd558d18SJames Chapman /*****************************************************************************
1648fd558d18SJames Chapman * Init and cleanup
1649fd558d18SJames Chapman *****************************************************************************/
1650fd558d18SJames Chapman
l2tp_init_net(struct net * net)1651fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net)
1652fd558d18SJames Chapman {
1653e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1654f7faffa3SJames Chapman int hash;
1655fd558d18SJames Chapman
1656c4d48a58SCong Wang idr_init(&pn->l2tp_tunnel_idr);
1657c4d48a58SCong Wang spin_lock_init(&pn->l2tp_tunnel_idr_lock);
1658fd558d18SJames Chapman
1659f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1660f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1661f7faffa3SJames Chapman
1662e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock);
1663f7faffa3SJames Chapman
1664fd558d18SJames Chapman return 0;
1665fd558d18SJames Chapman }
1666fd558d18SJames Chapman
l2tp_exit_net(struct net * net)1667167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net)
1668167eb17eSTom Parkin {
1669167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net);
1670167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL;
1671c4d48a58SCong Wang unsigned long tunnel_id, tmp;
16721e7af3b2SVasily Averin int hash;
1673167eb17eSTom Parkin
1674167eb17eSTom Parkin rcu_read_lock_bh();
1675c4d48a58SCong Wang idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
1676c4d48a58SCong Wang if (tunnel)
16774dc12ffeSJiri Slaby l2tp_tunnel_delete(tunnel);
1678167eb17eSTom Parkin }
1679167eb17eSTom Parkin rcu_read_unlock_bh();
16802f86953eSSabrina Dubroca
1681638a3a1eSYueHaibing if (l2tp_wq)
16822f86953eSSabrina Dubroca flush_workqueue(l2tp_wq);
16832f86953eSSabrina Dubroca rcu_barrier();
16841e7af3b2SVasily Averin
16851e7af3b2SVasily Averin for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
16861e7af3b2SVasily Averin WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
1687c4d48a58SCong Wang idr_destroy(&pn->l2tp_tunnel_idr);
1688167eb17eSTom Parkin }
1689167eb17eSTom Parkin
1690fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = {
1691fd558d18SJames Chapman .init = l2tp_init_net,
1692167eb17eSTom Parkin .exit = l2tp_exit_net,
1693fd558d18SJames Chapman .id = &l2tp_net_id,
1694fd558d18SJames Chapman .size = sizeof(struct l2tp_net),
1695fd558d18SJames Chapman };
1696fd558d18SJames Chapman
l2tp_init(void)1697fd558d18SJames Chapman static int __init l2tp_init(void)
1698fd558d18SJames Chapman {
1699fd558d18SJames Chapman int rc = 0;
1700fd558d18SJames Chapman
1701fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops);
1702fd558d18SJames Chapman if (rc)
1703fd558d18SJames Chapman goto out;
1704fd558d18SJames Chapman
170559ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1706f8ccac0eSTom Parkin if (!l2tp_wq) {
1707f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n");
170867e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops);
1709f8ccac0eSTom Parkin rc = -ENOMEM;
1710f8ccac0eSTom Parkin goto out;
1711f8ccac0eSTom Parkin }
1712f8ccac0eSTom Parkin
1713a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1714fd558d18SJames Chapman
1715fd558d18SJames Chapman out:
1716fd558d18SJames Chapman return rc;
1717fd558d18SJames Chapman }
1718fd558d18SJames Chapman
l2tp_exit(void)1719fd558d18SJames Chapman static void __exit l2tp_exit(void)
1720fd558d18SJames Chapman {
1721fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops);
1722f8ccac0eSTom Parkin if (l2tp_wq) {
1723f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq);
1724f8ccac0eSTom Parkin l2tp_wq = NULL;
1725f8ccac0eSTom Parkin }
1726fd558d18SJames Chapman }
1727fd558d18SJames Chapman
1728fd558d18SJames Chapman module_init(l2tp_init);
1729fd558d18SJames Chapman module_exit(l2tp_exit);
1730fd558d18SJames Chapman
1731fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1732fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core");
1733fd558d18SJames Chapman MODULE_LICENSE("GPL");
1734fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION);
1735