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" 64fd558d18SJames Chapman 65fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 66fd558d18SJames Chapman 67fd558d18SJames Chapman /* L2TP header constants */ 68fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 69fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 70fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 71fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 72fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 73fd558d18SJames Chapman 74fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 75fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 76f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 77fd558d18SJames Chapman 78fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 79fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 80fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 81fd558d18SJames Chapman 8291c52470SJacob Wen #define L2TP_HDR_SIZE_MAX 14 83fd558d18SJames Chapman 84fd558d18SJames Chapman /* Default trace flags */ 85fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 86fd558d18SJames Chapman 87fd558d18SJames Chapman /* Private data stored for received packets in the skb. 88fd558d18SJames Chapman */ 89fd558d18SJames Chapman struct l2tp_skb_cb { 90f7faffa3SJames Chapman u32 ns; 91fd558d18SJames Chapman u16 has_seq; 92fd558d18SJames Chapman u16 length; 93fd558d18SJames Chapman unsigned long expires; 94fd558d18SJames Chapman }; 95fd558d18SJames Chapman 96efcd8c85STom Parkin #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)]) 97fd558d18SJames Chapman 98f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq; 99fd558d18SJames Chapman 100fd558d18SJames Chapman /* per-net private data for this module */ 101fd558d18SJames Chapman static unsigned int l2tp_net_id; 102fd558d18SJames Chapman struct l2tp_net { 103fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 10420dcb110STom Parkin /* Lock for write access to l2tp_tunnel_list */ 105e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 106f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 10720dcb110STom Parkin /* Lock for write access to l2tp_session_hlist */ 108e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 109fd558d18SJames Chapman }; 110fd558d18SJames Chapman 111b954f940SPaolo Abeni #if IS_ENABLED(CONFIG_IPV6) 112b954f940SPaolo Abeni static bool l2tp_sk_is_v6(struct sock *sk) 113b954f940SPaolo Abeni { 114b954f940SPaolo Abeni return sk->sk_family == PF_INET6 && 115b954f940SPaolo Abeni !ipv6_addr_v4mapped(&sk->sk_v6_daddr); 116b954f940SPaolo Abeni } 117b954f940SPaolo Abeni #endif 118fc130840Sstephen hemminger 1198d8a51e2SDavid S. Miller static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk) 1208d8a51e2SDavid S. Miller { 1218d8a51e2SDavid S. Miller return sk->sk_user_data; 1228d8a51e2SDavid S. Miller } 1238d8a51e2SDavid S. Miller 1249aaef50cSGuillaume Nault static inline struct l2tp_net *l2tp_pernet(const struct net *net) 125fd558d18SJames Chapman { 126fd558d18SJames Chapman return net_generic(net, l2tp_net_id); 127fd558d18SJames Chapman } 128fd558d18SJames Chapman 129f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 130f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 131f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 132f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 133f7faffa3SJames Chapman */ 134f7faffa3SJames Chapman static inline struct hlist_head * 135f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 136f7faffa3SJames Chapman { 137f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 138f7faffa3SJames Chapman } 139f7faffa3SJames Chapman 140fd558d18SJames Chapman /* Session hash list. 141fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 142fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 143fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 144fd558d18SJames Chapman * simple bitmask. 145fd558d18SJames Chapman */ 146fd558d18SJames Chapman static inline struct hlist_head * 147fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 148fd558d18SJames Chapman { 149fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 150fd558d18SJames Chapman } 151fd558d18SJames Chapman 15252016e25STom Parkin static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 153d00fa9adSJames Chapman { 154d00fa9adSJames Chapman sock_put(tunnel->sock); 155d00fa9adSJames Chapman /* the tunnel is freed in the socket destructor */ 156d00fa9adSJames Chapman } 15752016e25STom Parkin 15852016e25STom Parkin static void l2tp_session_free(struct l2tp_session *session) 15952016e25STom Parkin { 16052016e25STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 16152016e25STom Parkin 16252016e25STom Parkin if (tunnel) { 16352016e25STom Parkin if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC)) 16452016e25STom Parkin goto out; 16552016e25STom Parkin l2tp_tunnel_dec_refcount(tunnel); 16652016e25STom Parkin } 16752016e25STom Parkin 16852016e25STom Parkin out: 16952016e25STom Parkin kfree(session); 17052016e25STom Parkin } 17152016e25STom Parkin 17252016e25STom Parkin void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel) 17352016e25STom Parkin { 17452016e25STom Parkin refcount_inc(&tunnel->ref_count); 17552016e25STom Parkin } 17652016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount); 17752016e25STom Parkin 17852016e25STom Parkin void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel) 17952016e25STom Parkin { 18052016e25STom Parkin if (refcount_dec_and_test(&tunnel->ref_count)) 18152016e25STom Parkin l2tp_tunnel_free(tunnel); 18252016e25STom Parkin } 18352016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount); 18452016e25STom Parkin 18552016e25STom Parkin void l2tp_session_inc_refcount(struct l2tp_session *session) 18652016e25STom Parkin { 18752016e25STom Parkin refcount_inc(&session->ref_count); 18852016e25STom Parkin } 18952016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount); 19052016e25STom Parkin 19152016e25STom Parkin void l2tp_session_dec_refcount(struct l2tp_session *session) 19252016e25STom Parkin { 19352016e25STom Parkin if (refcount_dec_and_test(&session->ref_count)) 19452016e25STom Parkin l2tp_session_free(session); 19552016e25STom Parkin } 19652016e25STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount); 197d00fa9adSJames Chapman 19854652eb1SGuillaume Nault /* Lookup a tunnel. A new reference is held on the returned tunnel. */ 19954652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id) 20054652eb1SGuillaume Nault { 20154652eb1SGuillaume Nault const struct l2tp_net *pn = l2tp_pernet(net); 20254652eb1SGuillaume Nault struct l2tp_tunnel *tunnel; 20354652eb1SGuillaume Nault 20454652eb1SGuillaume Nault rcu_read_lock_bh(); 20554652eb1SGuillaume Nault list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 206a622b400SEric Dumazet if (tunnel->tunnel_id == tunnel_id && 207a622b400SEric Dumazet refcount_inc_not_zero(&tunnel->ref_count)) { 20854652eb1SGuillaume Nault rcu_read_unlock_bh(); 20954652eb1SGuillaume Nault 21054652eb1SGuillaume Nault return tunnel; 21154652eb1SGuillaume Nault } 21254652eb1SGuillaume Nault } 21354652eb1SGuillaume Nault rcu_read_unlock_bh(); 21454652eb1SGuillaume Nault 21554652eb1SGuillaume Nault return NULL; 21654652eb1SGuillaume Nault } 21754652eb1SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get); 21854652eb1SGuillaume Nault 2195846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth) 2205846c131SGuillaume Nault { 2215846c131SGuillaume Nault const struct l2tp_net *pn = l2tp_pernet(net); 2225846c131SGuillaume Nault struct l2tp_tunnel *tunnel; 2235846c131SGuillaume Nault int count = 0; 2245846c131SGuillaume Nault 2255846c131SGuillaume Nault rcu_read_lock_bh(); 2265846c131SGuillaume Nault list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 227a622b400SEric Dumazet if (++count > nth && 228a622b400SEric Dumazet refcount_inc_not_zero(&tunnel->ref_count)) { 2295846c131SGuillaume Nault rcu_read_unlock_bh(); 2305846c131SGuillaume Nault return tunnel; 2315846c131SGuillaume Nault } 2325846c131SGuillaume Nault } 2335846c131SGuillaume Nault rcu_read_unlock_bh(); 2345846c131SGuillaume Nault 2355846c131SGuillaume Nault return NULL; 2365846c131SGuillaume Nault } 2375846c131SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth); 2385846c131SGuillaume Nault 23901e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel, 240a4346210SGuillaume Nault u32 session_id) 24161b9a047SGuillaume Nault { 24261b9a047SGuillaume Nault struct hlist_head *session_list; 24361b9a047SGuillaume Nault struct l2tp_session *session; 24461b9a047SGuillaume Nault 24501e28b92SGuillaume Nault session_list = l2tp_session_id_hash(tunnel, session_id); 24661b9a047SGuillaume Nault 24701e28b92SGuillaume Nault read_lock_bh(&tunnel->hlist_lock); 24801e28b92SGuillaume Nault hlist_for_each_entry(session, session_list, hlist) 24961b9a047SGuillaume Nault if (session->session_id == session_id) { 25061b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 25101e28b92SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 25261b9a047SGuillaume Nault 25361b9a047SGuillaume Nault return session; 25461b9a047SGuillaume Nault } 25501e28b92SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 25661b9a047SGuillaume Nault 25761b9a047SGuillaume Nault return NULL; 25861b9a047SGuillaume Nault } 25901e28b92SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session); 26061b9a047SGuillaume Nault 26101e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id) 26201e28b92SGuillaume Nault { 26301e28b92SGuillaume Nault struct hlist_head *session_list; 26401e28b92SGuillaume Nault struct l2tp_session *session; 26501e28b92SGuillaume Nault 26601e28b92SGuillaume Nault session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id); 26701e28b92SGuillaume Nault 26801e28b92SGuillaume Nault rcu_read_lock_bh(); 26901e28b92SGuillaume Nault hlist_for_each_entry_rcu(session, session_list, global_hlist) 27061b9a047SGuillaume Nault if (session->session_id == session_id) { 27161b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 27201e28b92SGuillaume Nault rcu_read_unlock_bh(); 27361b9a047SGuillaume Nault 27461b9a047SGuillaume Nault return session; 27561b9a047SGuillaume Nault } 27601e28b92SGuillaume Nault rcu_read_unlock_bh(); 27761b9a047SGuillaume Nault 27861b9a047SGuillaume Nault return NULL; 27961b9a047SGuillaume Nault } 28061b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get); 28161b9a047SGuillaume Nault 282a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth) 283fd558d18SJames Chapman { 284fd558d18SJames Chapman int hash; 285fd558d18SJames Chapman struct l2tp_session *session; 286fd558d18SJames Chapman int count = 0; 287fd558d18SJames Chapman 288fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 289fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 290b67bfe0dSSasha Levin hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { 291fd558d18SJames Chapman if (++count > nth) { 292e08293a4SGuillaume Nault l2tp_session_inc_refcount(session); 293fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 294fd558d18SJames Chapman return session; 295fd558d18SJames Chapman } 296fd558d18SJames Chapman } 297fd558d18SJames Chapman } 298fd558d18SJames Chapman 299fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 300fd558d18SJames Chapman 301fd558d18SJames Chapman return NULL; 302fd558d18SJames Chapman } 303e08293a4SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_nth); 304fd558d18SJames Chapman 305309795f4SJames Chapman /* Lookup a session by interface name. 306309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 307309795f4SJames Chapman */ 3089aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 309a4346210SGuillaume Nault const char *ifname) 310309795f4SJames Chapman { 311309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 312309795f4SJames Chapman int hash; 313309795f4SJames Chapman struct l2tp_session *session; 314309795f4SJames Chapman 315e02d494dSJames Chapman rcu_read_lock_bh(); 316309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 317b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { 318309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 3192777e2abSGuillaume Nault l2tp_session_inc_refcount(session); 320e02d494dSJames Chapman rcu_read_unlock_bh(); 3212777e2abSGuillaume Nault 322309795f4SJames Chapman return session; 323309795f4SJames Chapman } 324309795f4SJames Chapman } 325309795f4SJames Chapman } 326309795f4SJames Chapman 327e02d494dSJames Chapman rcu_read_unlock_bh(); 328309795f4SJames Chapman 329309795f4SJames Chapman return NULL; 330309795f4SJames Chapman } 3312777e2abSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); 332309795f4SJames Chapman 3333953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session, 3343953ae7bSGuillaume Nault struct l2tp_tunnel *tunnel) 335dbdbc73bSGuillaume Nault { 336dbdbc73bSGuillaume Nault struct l2tp_session *session_walk; 337dbdbc73bSGuillaume Nault struct hlist_head *g_head; 338dbdbc73bSGuillaume Nault struct hlist_head *head; 339dbdbc73bSGuillaume Nault struct l2tp_net *pn; 340f3c66d4eSGuillaume Nault int err; 341dbdbc73bSGuillaume Nault 342dbdbc73bSGuillaume Nault head = l2tp_session_id_hash(tunnel, session->session_id); 343dbdbc73bSGuillaume Nault 344dbdbc73bSGuillaume Nault write_lock_bh(&tunnel->hlist_lock); 345f3c66d4eSGuillaume Nault if (!tunnel->acpt_newsess) { 346f3c66d4eSGuillaume Nault err = -ENODEV; 347f3c66d4eSGuillaume Nault goto err_tlock; 348f3c66d4eSGuillaume Nault } 349f3c66d4eSGuillaume Nault 350dbdbc73bSGuillaume Nault hlist_for_each_entry(session_walk, head, hlist) 351f3c66d4eSGuillaume Nault if (session_walk->session_id == session->session_id) { 352f3c66d4eSGuillaume Nault err = -EEXIST; 353f3c66d4eSGuillaume Nault goto err_tlock; 354f3c66d4eSGuillaume Nault } 355dbdbc73bSGuillaume Nault 356dbdbc73bSGuillaume Nault if (tunnel->version == L2TP_HDR_VER_3) { 357dbdbc73bSGuillaume Nault pn = l2tp_pernet(tunnel->l2tp_net); 358363a341dSGuillaume Nault g_head = l2tp_session_id_hash_2(pn, session->session_id); 359dbdbc73bSGuillaume Nault 360dbdbc73bSGuillaume Nault spin_lock_bh(&pn->l2tp_session_hlist_lock); 361dbdbc73bSGuillaume Nault 3620d0d9a38SRidge Kennedy /* IP encap expects session IDs to be globally unique, while 3630d0d9a38SRidge Kennedy * UDP encap doesn't. 3640d0d9a38SRidge Kennedy */ 365f3c66d4eSGuillaume Nault hlist_for_each_entry(session_walk, g_head, global_hlist) 3660d0d9a38SRidge Kennedy if (session_walk->session_id == session->session_id && 3670d0d9a38SRidge Kennedy (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP || 3680d0d9a38SRidge Kennedy tunnel->encap == L2TP_ENCAPTYPE_IP)) { 369f3c66d4eSGuillaume Nault err = -EEXIST; 370f3c66d4eSGuillaume Nault goto err_tlock_pnlock; 371f3c66d4eSGuillaume Nault } 372f3c66d4eSGuillaume Nault 373f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel); 374dbdbc73bSGuillaume Nault hlist_add_head_rcu(&session->global_hlist, g_head); 375f3c66d4eSGuillaume Nault 376dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 377f3c66d4eSGuillaume Nault } else { 378f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel); 379dbdbc73bSGuillaume Nault } 380dbdbc73bSGuillaume Nault 381dbdbc73bSGuillaume Nault hlist_add_head(&session->hlist, head); 382dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 383dbdbc73bSGuillaume Nault 384dbdbc73bSGuillaume Nault return 0; 385dbdbc73bSGuillaume Nault 386f3c66d4eSGuillaume Nault err_tlock_pnlock: 387dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 388f3c66d4eSGuillaume Nault err_tlock: 389dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 390dbdbc73bSGuillaume Nault 391f3c66d4eSGuillaume Nault return err; 392dbdbc73bSGuillaume Nault } 3933953ae7bSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_register); 394dbdbc73bSGuillaume Nault 395fd558d18SJames Chapman /***************************************************************************** 396fd558d18SJames Chapman * Receive data handling 397fd558d18SJames Chapman *****************************************************************************/ 398fd558d18SJames Chapman 399fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 400fd558d18SJames Chapman * number. 401fd558d18SJames Chapman */ 402fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 403fd558d18SJames Chapman { 404fd558d18SJames Chapman struct sk_buff *skbp; 405fd558d18SJames Chapman struct sk_buff *tmp; 406f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 407fd558d18SJames Chapman 408fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 409fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 410fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 411fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 412a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 413fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 414fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 415fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 4167b7c0719STom Parkin atomic_long_inc(&session->stats.rx_oos_packets); 417fd558d18SJames Chapman goto out; 418fd558d18SJames Chapman } 419fd558d18SJames Chapman } 420fd558d18SJames Chapman 421fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 422fd558d18SJames Chapman 423fd558d18SJames Chapman out: 424fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 425fd558d18SJames Chapman } 426fd558d18SJames Chapman 427fd558d18SJames Chapman /* Dequeue a single skb. 428fd558d18SJames Chapman */ 429fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 430fd558d18SJames Chapman { 431fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 432fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 433fd558d18SJames Chapman 434fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 435fd558d18SJames Chapman * to its current owner (a socket receive buffer). 436fd558d18SJames Chapman */ 437fd558d18SJames Chapman skb_orphan(skb); 438fd558d18SJames Chapman 4397b7c0719STom Parkin atomic_long_inc(&tunnel->stats.rx_packets); 4407b7c0719STom Parkin atomic_long_add(length, &tunnel->stats.rx_bytes); 4417b7c0719STom Parkin atomic_long_inc(&session->stats.rx_packets); 4427b7c0719STom Parkin atomic_long_add(length, &session->stats.rx_bytes); 443fd558d18SJames Chapman 444fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 445fd558d18SJames Chapman /* Bump our Nr */ 446fd558d18SJames Chapman session->nr++; 4478a1631d5SJames Chapman session->nr &= session->nr_max; 448f7faffa3SJames Chapman 449a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 450a4ca44faSJoe Perches session->name, session->nr); 451fd558d18SJames Chapman } 452fd558d18SJames Chapman 453fd558d18SJames Chapman /* call private receive handler */ 4540febc7b3STom Parkin if (session->recv_skb) 455fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 456fd558d18SJames Chapman else 457fd558d18SJames Chapman kfree_skb(skb); 458fd558d18SJames Chapman } 459fd558d18SJames Chapman 460fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 461fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 462fd558d18SJames Chapman */ 463fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 464fd558d18SJames Chapman { 465fd558d18SJames Chapman struct sk_buff *skb; 466fd558d18SJames Chapman struct sk_buff *tmp; 467fd558d18SJames Chapman 468fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 469fd558d18SJames Chapman * expect to send up next, dequeue it and any other 470fd558d18SJames Chapman * in-sequence packets behind it. 471fd558d18SJames Chapman */ 472e2e210c0SEric Dumazet start: 473fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 474fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 475fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 4767b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 4777b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 478a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 479a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 480fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 481fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 482fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 48338d40b3fSJames Chapman session->reorder_skip = 1; 484fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 485fd558d18SJames Chapman kfree_skb(skb); 486fd558d18SJames Chapman continue; 487fd558d18SJames Chapman } 488fd558d18SJames Chapman 489fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 49038d40b3fSJames Chapman if (session->reorder_skip) { 491a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 49238d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 49338d40b3fSJames Chapman session->name, session->nr, 49438d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 49538d40b3fSJames Chapman session->reorder_skip = 0; 49638d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 49738d40b3fSJames Chapman } 498fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 499a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 500a4ca44faSJoe Perches "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 501fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 502fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 503fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 504fd558d18SJames Chapman goto out; 505fd558d18SJames Chapman } 506fd558d18SJames Chapman } 507fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 508fd558d18SJames Chapman 509fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 510fd558d18SJames Chapman * do so to let other contexts process the queue. 511fd558d18SJames Chapman */ 512fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 513fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 514e2e210c0SEric Dumazet goto start; 515fd558d18SJames Chapman } 516fd558d18SJames Chapman 517fd558d18SJames Chapman out: 518fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 519fd558d18SJames Chapman } 520fd558d18SJames Chapman 5218a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) 5228a1631d5SJames Chapman { 5238a1631d5SJames Chapman u32 nws; 5248a1631d5SJames Chapman 5258a1631d5SJames Chapman if (nr >= session->nr) 5268a1631d5SJames Chapman nws = nr - session->nr; 5278a1631d5SJames Chapman else 5288a1631d5SJames Chapman nws = (session->nr_max + 1) - (session->nr - nr); 5298a1631d5SJames Chapman 5308a1631d5SJames Chapman return nws < session->nr_window_size; 5318a1631d5SJames Chapman } 5328a1631d5SJames Chapman 533b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if 534b6dc01a4SJames Chapman * acceptable, else non-zero. 535b6dc01a4SJames Chapman */ 536b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) 537b6dc01a4SJames Chapman { 5388a1631d5SJames Chapman if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { 5398a1631d5SJames Chapman /* Packet sequence number is outside allowed window. 5408a1631d5SJames Chapman * Discard it. 5418a1631d5SJames Chapman */ 5428a1631d5SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 5438a1631d5SJames Chapman "%s: pkt %u len %d discarded, outside window, nr=%u\n", 5448a1631d5SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 5458a1631d5SJames Chapman L2TP_SKB_CB(skb)->length, session->nr); 5468a1631d5SJames Chapman goto discard; 5478a1631d5SJames Chapman } 5488a1631d5SJames Chapman 549b6dc01a4SJames Chapman if (session->reorder_timeout != 0) { 550b6dc01a4SJames Chapman /* Packet reordering enabled. Add skb to session's 551b6dc01a4SJames Chapman * reorder queue, in order of ns. 552b6dc01a4SJames Chapman */ 553b6dc01a4SJames Chapman l2tp_recv_queue_skb(session, skb); 554a0dbd822SJames Chapman goto out; 555a0dbd822SJames Chapman } 556a0dbd822SJames Chapman 557a0dbd822SJames Chapman /* Packet reordering disabled. Discard out-of-sequence packets, while 558a0dbd822SJames Chapman * tracking the number if in-sequence packets after the first OOS packet 559a0dbd822SJames Chapman * is seen. After nr_oos_count_max in-sequence packets, reset the 560a0dbd822SJames Chapman * sequence number to re-enable packet reception. 561b6dc01a4SJames Chapman */ 562a0dbd822SJames Chapman if (L2TP_SKB_CB(skb)->ns == session->nr) { 563a0dbd822SJames Chapman skb_queue_tail(&session->reorder_q, skb); 564a0dbd822SJames Chapman } else { 565a0dbd822SJames Chapman u32 nr_oos = L2TP_SKB_CB(skb)->ns; 566a0dbd822SJames Chapman u32 nr_next = (session->nr_oos + 1) & session->nr_max; 567a0dbd822SJames Chapman 568a0dbd822SJames Chapman if (nr_oos == nr_next) 569a0dbd822SJames Chapman session->nr_oos_count++; 570a0dbd822SJames Chapman else 571a0dbd822SJames Chapman session->nr_oos_count = 0; 572a0dbd822SJames Chapman 573a0dbd822SJames Chapman session->nr_oos = nr_oos; 574a0dbd822SJames Chapman if (session->nr_oos_count > session->nr_oos_count_max) { 575a0dbd822SJames Chapman session->reorder_skip = 1; 576a0dbd822SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 577a0dbd822SJames Chapman "%s: %d oos packets received. Resetting sequence numbers\n", 578a0dbd822SJames Chapman session->name, session->nr_oos_count); 579a0dbd822SJames Chapman } 580a0dbd822SJames Chapman if (!session->reorder_skip) { 581b6dc01a4SJames Chapman atomic_long_inc(&session->stats.rx_seq_discards); 582b6dc01a4SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 583b6dc01a4SJames Chapman "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 584b6dc01a4SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 585b6dc01a4SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 586b6dc01a4SJames Chapman skb_queue_len(&session->reorder_q)); 587b6dc01a4SJames Chapman goto discard; 588b6dc01a4SJames Chapman } 589b6dc01a4SJames Chapman skb_queue_tail(&session->reorder_q, skb); 590b6dc01a4SJames Chapman } 591b6dc01a4SJames Chapman 592a0dbd822SJames Chapman out: 593b6dc01a4SJames Chapman return 0; 594b6dc01a4SJames Chapman 595b6dc01a4SJames Chapman discard: 596b6dc01a4SJames Chapman return 1; 597b6dc01a4SJames Chapman } 598b6dc01a4SJames Chapman 599f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 600f7faffa3SJames Chapman * and L2TPv3 data frames here. 601f7faffa3SJames Chapman * 602f7faffa3SJames Chapman * L2TPv2 Data Message Header 603f7faffa3SJames Chapman * 604f7faffa3SJames Chapman * 0 1 2 3 605f7faffa3SJames 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 606f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 607f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 608f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 609f7faffa3SJames Chapman * | Tunnel ID | Session ID | 610f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 611f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 612f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 613f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 614f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 615f7faffa3SJames Chapman * 616f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 617f7faffa3SJames Chapman * those in L2TP control frames. 618f7faffa3SJames Chapman * 619f7faffa3SJames Chapman * L2TPv3 Data Message Header 620f7faffa3SJames Chapman * 621f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 622f7faffa3SJames Chapman * | L2TP Session Header | 623f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 624f7faffa3SJames Chapman * | L2-Specific Sublayer | 625f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 626f7faffa3SJames Chapman * | Tunnel Payload ... 627f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 628f7faffa3SJames Chapman * 629f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 630f7faffa3SJames Chapman * 631f7faffa3SJames Chapman * 0 1 2 3 632f7faffa3SJames 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 633f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 634f7faffa3SJames Chapman * | Session ID | 635f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 636f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 637f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 638f7faffa3SJames Chapman * | 639f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 640f7faffa3SJames Chapman * 641f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 642f7faffa3SJames Chapman * 643f7faffa3SJames Chapman * 0 1 2 3 644f7faffa3SJames 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 645f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 646f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 647f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 648f7faffa3SJames Chapman * 64923fe846fSGuillaume Nault * Cookie value and sublayer format are negotiated with the peer when 65023fe846fSGuillaume Nault * the session is set up. Unlike L2TPv2, we do not need to parse the 65123fe846fSGuillaume Nault * packet header to determine if optional fields are present. 652f7faffa3SJames Chapman * 653f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 654f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 655f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 656f7faffa3SJames Chapman * after the session-id. 657f7faffa3SJames Chapman */ 658f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 659f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 6602b139e6bSGuillaume Nault int length) 661f7faffa3SJames Chapman { 662f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 66395075150STom Parkin u32 ns = 0, nr = 0; 664f7faffa3SJames Chapman int offset; 665f7faffa3SJames Chapman 666f7faffa3SJames Chapman /* Parse and check optional cookie */ 667f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 668f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 669a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 670f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 671a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 672a4ca44faSJoe Perches session->session_id); 6737b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards); 674f7faffa3SJames Chapman goto discard; 675f7faffa3SJames Chapman } 676f7faffa3SJames Chapman ptr += session->peer_cookie_len; 677f7faffa3SJames Chapman } 678f7faffa3SJames Chapman 679f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 680f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 681f7faffa3SJames Chapman * 682f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 683f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 684f7faffa3SJames Chapman * we were expecting them, discard frame. 685f7faffa3SJames Chapman */ 686f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 687f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 688f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 689f7faffa3SJames Chapman ns = ntohs(*(__be16 *)ptr); 690f7faffa3SJames Chapman ptr += 2; 691f7faffa3SJames Chapman nr = ntohs(*(__be16 *)ptr); 692f7faffa3SJames Chapman ptr += 2; 693f7faffa3SJames Chapman 694f7faffa3SJames Chapman /* Store L2TP info in the skb */ 695f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 696f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 697f7faffa3SJames Chapman 698a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 699f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 700f7faffa3SJames Chapman session->name, ns, nr, session->nr); 701f7faffa3SJames Chapman } 702f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 703f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *)ptr); 704f7faffa3SJames Chapman 705f7faffa3SJames Chapman if (l2h & 0x40000000) { 706f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 707f7faffa3SJames Chapman 708f7faffa3SJames Chapman /* Store L2TP info in the skb */ 709f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 710f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 711f7faffa3SJames Chapman 712a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 713f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 714f7faffa3SJames Chapman session->name, ns, session->nr); 715f7faffa3SJames Chapman } 71662e7b6a5SLorenzo Bianconi ptr += 4; 717f7faffa3SJames Chapman } 718f7faffa3SJames Chapman 719f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 72020dcb110STom Parkin /* Received a packet with sequence numbers. If we're the LAC, 721f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 722f7faffa3SJames Chapman * configure it so. 723f7faffa3SJames Chapman */ 7246c0ec37bSTom Parkin if (!session->lns_mode && !session->send_seq) { 725a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 726f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 727f7faffa3SJames Chapman session->name); 7283f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1; 729f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 730f7faffa3SJames Chapman } 731f7faffa3SJames Chapman } else { 732f7faffa3SJames Chapman /* No sequence numbers. 733f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 734f7faffa3SJames Chapman */ 735f7faffa3SJames Chapman if (session->recv_seq) { 736a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 737a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 738a4ca44faSJoe Perches session->name); 7397b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 740f7faffa3SJames Chapman goto discard; 741f7faffa3SJames Chapman } 742f7faffa3SJames Chapman 743f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 744f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 745f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 746f7faffa3SJames Chapman * LAC is broken. Discard the frame. 747f7faffa3SJames Chapman */ 7486c0ec37bSTom Parkin if (!session->lns_mode && session->send_seq) { 749a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 750f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 751f7faffa3SJames Chapman session->name); 752f7faffa3SJames Chapman session->send_seq = 0; 753f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 754f7faffa3SJames Chapman } else if (session->send_seq) { 755a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 756a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 757a4ca44faSJoe Perches session->name); 7587b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 759f7faffa3SJames Chapman goto discard; 760f7faffa3SJames Chapman } 761f7faffa3SJames Chapman } 762f7faffa3SJames Chapman 763900631eeSJames Chapman /* Session data offset is defined only for L2TPv2 and is 764900631eeSJames Chapman * indicated by an optional 16-bit value in the header. 765f7faffa3SJames Chapman */ 766f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 767f7faffa3SJames Chapman /* If offset bit set, skip it. */ 768f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 769f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 770f7faffa3SJames Chapman ptr += 2 + offset; 771f7faffa3SJames Chapman } 772900631eeSJames Chapman } 773f7faffa3SJames Chapman 774f7faffa3SJames Chapman offset = ptr - optr; 775f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 776f7faffa3SJames Chapman goto discard; 777f7faffa3SJames Chapman 778f7faffa3SJames Chapman __skb_pull(skb, offset); 779f7faffa3SJames Chapman 780f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 781f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 782f7faffa3SJames Chapman * reordering. 783f7faffa3SJames Chapman */ 784f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 785f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 786f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 787f7faffa3SJames Chapman 788f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 789f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 790f7faffa3SJames Chapman */ 791f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 792b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb)) 793f7faffa3SJames Chapman goto discard; 794f7faffa3SJames Chapman } else { 795f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 796f7faffa3SJames Chapman * reorder queue. This ensures that it will be 797f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 798f7faffa3SJames Chapman */ 799f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 800f7faffa3SJames Chapman } 801f7faffa3SJames Chapman 802f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 803f7faffa3SJames Chapman l2tp_recv_dequeue(session); 804f7faffa3SJames Chapman 805f7faffa3SJames Chapman return; 806f7faffa3SJames Chapman 807f7faffa3SJames Chapman discard: 8087b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 809f7faffa3SJames Chapman kfree_skb(skb); 810f7faffa3SJames Chapman } 811ca7885dbSTom Parkin EXPORT_SYMBOL_GPL(l2tp_recv_common); 812f7faffa3SJames Chapman 81348f72f92STom Parkin /* Drop skbs from the session's reorder_q 81448f72f92STom Parkin */ 815493048f5STom Parkin static void l2tp_session_queue_purge(struct l2tp_session *session) 81648f72f92STom Parkin { 81748f72f92STom Parkin struct sk_buff *skb = NULL; 818b71a61ccSTom Parkin 819493048f5STom Parkin if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) 820493048f5STom Parkin return; 821493048f5STom Parkin 82248f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) { 82348f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors); 82448f72f92STom Parkin kfree_skb(skb); 82548f72f92STom Parkin } 82648f72f92STom Parkin } 82748f72f92STom Parkin 828fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 829fd558d18SJames Chapman * here. The skb is not on a list when we get here. 830fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 831fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 832fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 833fd558d18SJames Chapman */ 8342b139e6bSGuillaume Nault static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb) 835fd558d18SJames Chapman { 836fd558d18SJames Chapman struct l2tp_session *session = NULL; 837fd558d18SJames Chapman unsigned char *ptr, *optr; 838fd558d18SJames Chapman u16 hdrflags; 839fd558d18SJames Chapman u32 tunnel_id, session_id; 840fd558d18SJames Chapman u16 version; 841f7faffa3SJames Chapman int length; 842fd558d18SJames Chapman 84358d6085cSTom Herbert /* UDP has verifed checksum */ 844fd558d18SJames Chapman 845fd558d18SJames Chapman /* UDP always verifies the packet length. */ 846fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 847fd558d18SJames Chapman 848fd558d18SJames Chapman /* Short packet? */ 84991c52470SJacob Wen if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) { 850a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 851a4ca44faSJoe Perches "%s: recv short packet (len=%d)\n", 852a4ca44faSJoe Perches tunnel->name, skb->len); 853fd558d18SJames Chapman goto error; 854fd558d18SJames Chapman } 855fd558d18SJames Chapman 856fd558d18SJames Chapman /* Trace packet contents, if enabled */ 857fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 858fd558d18SJames Chapman length = min(32u, skb->len); 859fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 860fd558d18SJames Chapman goto error; 861fd558d18SJames Chapman 862a4ca44faSJoe Perches pr_debug("%s: recv\n", tunnel->name); 863a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 864fd558d18SJames Chapman } 865fd558d18SJames Chapman 866e50e705cSEric Dumazet /* Point to L2TP header */ 86795075150STom Parkin optr = skb->data; 86895075150STom Parkin ptr = skb->data; 869e50e705cSEric Dumazet 870fd558d18SJames Chapman /* Get L2TP header flags */ 871fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *)ptr); 872fd558d18SJames Chapman 873fd558d18SJames Chapman /* Check protocol version */ 874fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 875fd558d18SJames Chapman if (version != tunnel->version) { 876a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 877fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 878fd558d18SJames Chapman tunnel->name, version, tunnel->version); 879fd558d18SJames Chapman goto error; 880fd558d18SJames Chapman } 881fd558d18SJames Chapman 882fd558d18SJames Chapman /* Get length of L2TP packet */ 883fd558d18SJames Chapman length = skb->len; 884fd558d18SJames Chapman 885fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 886fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 887a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, 888a4ca44faSJoe Perches "%s: recv control packet, len=%d\n", 889a4ca44faSJoe Perches tunnel->name, length); 890fd558d18SJames Chapman goto error; 891fd558d18SJames Chapman } 892fd558d18SJames Chapman 893fd558d18SJames Chapman /* Skip flags */ 894fd558d18SJames Chapman ptr += 2; 895fd558d18SJames Chapman 896f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 897fd558d18SJames Chapman /* If length is present, skip it */ 898fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 899fd558d18SJames Chapman ptr += 2; 900fd558d18SJames Chapman 901fd558d18SJames Chapman /* Extract tunnel and session ID */ 902fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *)ptr); 903fd558d18SJames Chapman ptr += 2; 904fd558d18SJames Chapman session_id = ntohs(*(__be16 *)ptr); 905fd558d18SJames Chapman ptr += 2; 906f7faffa3SJames Chapman } else { 907f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 908f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 909f7faffa3SJames Chapman session_id = ntohl(*(__be32 *)ptr); 910f7faffa3SJames Chapman ptr += 4; 911f7faffa3SJames Chapman } 912fd558d18SJames Chapman 913fd558d18SJames Chapman /* Find the session context */ 91401e28b92SGuillaume Nault session = l2tp_tunnel_get_session(tunnel, session_id); 915309795f4SJames Chapman if (!session || !session->recv_skb) { 916a4346210SGuillaume Nault if (session) 91761b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 91861b9a047SGuillaume Nault 919fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 920a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 921f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 922fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 923fd558d18SJames Chapman goto error; 924fd558d18SJames Chapman } 925fd558d18SJames Chapman 9264522a70dSJacob Wen if (tunnel->version == L2TP_HDR_VER_3 && 9274522a70dSJacob Wen l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) 9284522a70dSJacob Wen goto error; 9294522a70dSJacob Wen 9302b139e6bSGuillaume Nault l2tp_recv_common(session, skb, ptr, optr, hdrflags, length); 93161b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 932fd558d18SJames Chapman 933fd558d18SJames Chapman return 0; 934fd558d18SJames Chapman 935fd558d18SJames Chapman error: 936fd558d18SJames Chapman /* Put UDP header back */ 937fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 938fd558d18SJames Chapman 939fd558d18SJames Chapman return 1; 940fd558d18SJames Chapman } 941fd558d18SJames Chapman 942fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 943fd558d18SJames Chapman * Return codes: 944fd558d18SJames Chapman * 0 : success. 945fd558d18SJames Chapman * <0: error 946fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 947fd558d18SJames Chapman */ 948fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 949fd558d18SJames Chapman { 950fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 951fd558d18SJames Chapman 952c1c47721SEric Dumazet tunnel = rcu_dereference_sk_user_data(sk); 9530febc7b3STom Parkin if (!tunnel) 954fd558d18SJames Chapman goto pass_up; 955fd558d18SJames Chapman 956a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 957a4ca44faSJoe Perches tunnel->name, skb->len); 958fd558d18SJames Chapman 9592b139e6bSGuillaume Nault if (l2tp_udp_recv_core(tunnel, skb)) 960d00fa9adSJames Chapman goto pass_up; 961fd558d18SJames Chapman 962fd558d18SJames Chapman return 0; 963fd558d18SJames Chapman 964fd558d18SJames Chapman pass_up: 965fd558d18SJames Chapman return 1; 966fd558d18SJames Chapman } 967fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 968fd558d18SJames Chapman 969fd558d18SJames Chapman /************************************************************************ 970fd558d18SJames Chapman * Transmit handling 971fd558d18SJames Chapman ***********************************************************************/ 972fd558d18SJames Chapman 973fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 974fd558d18SJames Chapman */ 975f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 976fd558d18SJames Chapman { 977f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 978fd558d18SJames Chapman __be16 *bufp = buf; 979f7faffa3SJames Chapman __be16 *optr = buf; 980fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 981fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 982fd558d18SJames Chapman u32 session_id = session->peer_session_id; 983fd558d18SJames Chapman 984fd558d18SJames Chapman if (session->send_seq) 985fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 986fd558d18SJames Chapman 987fd558d18SJames Chapman /* Setup L2TP header. */ 988fd558d18SJames Chapman *bufp++ = htons(flags); 989fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 990fd558d18SJames Chapman *bufp++ = htons(session_id); 991fd558d18SJames Chapman if (session->send_seq) { 992fd558d18SJames Chapman *bufp++ = htons(session->ns); 993fd558d18SJames Chapman *bufp++ = 0; 994fd558d18SJames Chapman session->ns++; 995f7faffa3SJames Chapman session->ns &= 0xffff; 996a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 997a4ca44faSJoe Perches session->name, session->ns); 998fd558d18SJames Chapman } 999fd558d18SJames Chapman 1000f7faffa3SJames Chapman return bufp - optr; 1001f7faffa3SJames Chapman } 1002f7faffa3SJames Chapman 1003f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 1004fd558d18SJames Chapman { 10050d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1006f7faffa3SJames Chapman char *bufp = buf; 1007f7faffa3SJames Chapman char *optr = bufp; 1008fd558d18SJames Chapman 10090d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 10100d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 10110d76751fSJames Chapman */ 10120d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 10130d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 1014f7faffa3SJames Chapman *((__be16 *)bufp) = htons(flags); 1015f7faffa3SJames Chapman bufp += 2; 1016f7faffa3SJames Chapman *((__be16 *)bufp) = 0; 1017f7faffa3SJames Chapman bufp += 2; 10180d76751fSJames Chapman } 10190d76751fSJames Chapman 1020f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(session->peer_session_id); 1021f7faffa3SJames Chapman bufp += 4; 1022f7faffa3SJames Chapman if (session->cookie_len) { 1023f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 1024f7faffa3SJames Chapman bufp += session->cookie_len; 1025fd558d18SJames Chapman } 1026f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1027f7faffa3SJames Chapman u32 l2h = 0; 102862e7b6a5SLorenzo Bianconi 1029f7faffa3SJames Chapman if (session->send_seq) { 1030f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1031f7faffa3SJames Chapman session->ns++; 1032f7faffa3SJames Chapman session->ns &= 0xffffff; 1033a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 1034a4ca44faSJoe Perches "%s: updated ns to %u\n", 1035a4ca44faSJoe Perches session->name, session->ns); 1036f7faffa3SJames Chapman } 1037f7faffa3SJames Chapman 1038f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(l2h); 103962e7b6a5SLorenzo Bianconi bufp += 4; 1040f7faffa3SJames Chapman } 1041f7faffa3SJames Chapman 1042f7faffa3SJames Chapman return bufp - optr; 1043f7faffa3SJames Chapman } 1044fd558d18SJames Chapman 10452685fbb8SGuillaume Nault static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1046d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1047fd558d18SJames Chapman { 1048fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1049fd558d18SJames Chapman unsigned int len = skb->len; 1050fd558d18SJames Chapman int error; 1051fd558d18SJames Chapman 1052fd558d18SJames Chapman /* Debug */ 1053fd558d18SJames Chapman if (session->send_seq) 10545b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", 1055a4ca44faSJoe Perches session->name, data_len, session->ns - 1); 1056fd558d18SJames Chapman else 10575b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", 1058a4ca44faSJoe Perches session->name, data_len); 1059fd558d18SJames Chapman 1060fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 10610d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10620d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1063fd558d18SJames Chapman 1064a4ca44faSJoe Perches pr_debug("%s: xmit\n", session->name); 1065a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1066a4ca44faSJoe Perches datap, min_t(size_t, 32, len - uhlen)); 1067fd558d18SJames Chapman } 1068fd558d18SJames Chapman 1069fd558d18SJames Chapman /* Queue the packet to IP for output */ 107060ff7467SWANG Cong skb->ignore_df = 1; 107127d53323SXin Long skb_dst_drop(skb); 1072d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1073b954f940SPaolo Abeni if (l2tp_sk_is_v6(tunnel->sock)) 1074b0270e91SEric Dumazet error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1075d2cf3361SBenjamin LaHaise else 1076d2cf3361SBenjamin LaHaise #endif 1077b0270e91SEric Dumazet error = ip_queue_xmit(tunnel->sock, skb, fl); 1078fd558d18SJames Chapman 1079fd558d18SJames Chapman /* Update stats */ 1080fd558d18SJames Chapman if (error >= 0) { 10817b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_packets); 10827b7c0719STom Parkin atomic_long_add(len, &tunnel->stats.tx_bytes); 10837b7c0719STom Parkin atomic_long_inc(&session->stats.tx_packets); 10847b7c0719STom Parkin atomic_long_add(len, &session->stats.tx_bytes); 1085fd558d18SJames Chapman } else { 10867b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_errors); 10877b7c0719STom Parkin atomic_long_inc(&session->stats.tx_errors); 1088fd558d18SJames Chapman } 1089fd558d18SJames Chapman } 1090fd558d18SJames Chapman 1091fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1092fd558d18SJames Chapman * inserted in the skb data before calling this function. 1093fd558d18SJames Chapman */ 1094fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1095fd558d18SJames Chapman { 1096fd558d18SJames Chapman int data_len = skb->len; 10970d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 10980d76751fSJames Chapman struct sock *sk = tunnel->sock; 1099d9d8da80SDavid S. Miller struct flowi *fl; 1100fd558d18SJames Chapman struct udphdr *uh; 1101fd558d18SJames Chapman struct inet_sock *inet; 1102fd558d18SJames Chapman int headroom; 11030d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11040d76751fSJames Chapman int udp_len; 1105b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1106fd558d18SJames Chapman 1107fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1108fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1109fd558d18SJames Chapman * make room. Adjust truesize. 1110fd558d18SJames Chapman */ 1111fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11120d76751fSJames Chapman uhlen + hdr_len; 1113835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1114b8c84307SEric Dumazet kfree_skb(skb); 1115b8c84307SEric Dumazet return NET_XMIT_DROP; 1116835acf5dSEric Dumazet } 1117fd558d18SJames Chapman 1118fd558d18SJames Chapman /* Setup L2TP header */ 11192dedab6fSTom Parkin if (tunnel->version == L2TP_HDR_VER_2) 11202dedab6fSTom Parkin l2tp_build_l2tpv2_header(session, __skb_push(skb, hdr_len)); 11212dedab6fSTom Parkin else 11222dedab6fSTom Parkin l2tp_build_l2tpv3_header(session, __skb_push(skb, hdr_len)); 1123fd558d18SJames Chapman 11240d76751fSJames Chapman /* Reset skb netfilter state */ 1125fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1126fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1127fd558d18SJames Chapman IPSKB_REROUTED); 1128895b5c9fSFlorian Westphal nf_reset_ct(skb); 1129fd558d18SJames Chapman 11306af88da1SDavid S. Miller bh_lock_sock(sk); 11316af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1132b8c84307SEric Dumazet kfree_skb(skb); 1133b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11346af88da1SDavid S. Miller goto out_unlock; 11356af88da1SDavid S. Miller } 11366af88da1SDavid S. Miller 1137b954f940SPaolo Abeni /* The user-space may change the connection status for the user-space 1138b954f940SPaolo Abeni * provided socket at run time: we must check it under the socket lock 1139b954f940SPaolo Abeni */ 1140b954f940SPaolo Abeni if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) { 1141b954f940SPaolo Abeni kfree_skb(skb); 1142b954f940SPaolo Abeni ret = NET_XMIT_DROP; 1143b954f940SPaolo Abeni goto out_unlock; 1144b954f940SPaolo Abeni } 1145b954f940SPaolo Abeni 1146d9d8da80SDavid S. Miller inet = inet_sk(sk); 1147d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11480d76751fSJames Chapman switch (tunnel->encap) { 11490d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 11500d76751fSJames Chapman /* Setup UDP header */ 11510d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11520d76751fSJames Chapman skb_reset_transport_header(skb); 11530d76751fSJames Chapman uh = udp_hdr(skb); 11540d76751fSJames Chapman uh->source = inet->inet_sport; 11550d76751fSJames Chapman uh->dest = inet->inet_dport; 11560d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11570d76751fSJames Chapman uh->len = htons(udp_len); 1158fd558d18SJames Chapman 1159fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1160d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1161b954f940SPaolo Abeni if (l2tp_sk_is_v6(sk)) 116277157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 116377157e19STom Herbert skb, &inet6_sk(sk)->saddr, 116477157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1165d2cf3361SBenjamin LaHaise else 1166d2cf3361SBenjamin LaHaise #endif 116777157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 116877157e19STom Herbert inet->inet_daddr, udp_len); 11690d76751fSJames Chapman break; 11700d76751fSJames Chapman 11710d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11720d76751fSJames Chapman break; 11730d76751fSJames Chapman } 11740d76751fSJames Chapman 1175d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 11766af88da1SDavid S. Miller out_unlock: 11776af88da1SDavid S. Miller bh_unlock_sock(sk); 1178fd558d18SJames Chapman 1179b8c84307SEric Dumazet return ret; 1180fd558d18SJames Chapman } 1181fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1182fd558d18SJames Chapman 1183fd558d18SJames Chapman /***************************************************************************** 1184fd558d18SJames Chapman * Tinnel and session create/destroy. 1185fd558d18SJames Chapman *****************************************************************************/ 1186fd558d18SJames Chapman 1187fd558d18SJames Chapman /* Tunnel socket destruct hook. 1188fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1189fd558d18SJames Chapman * closed. 1190fd558d18SJames Chapman */ 1191fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1192fd558d18SJames Chapman { 11938d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1194fd558d18SJames Chapman 11950febc7b3STom Parkin if (!tunnel) 1196fd558d18SJames Chapman goto end; 1197fd558d18SJames Chapman 1198a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1199fd558d18SJames Chapman 1200f8ccac0eSTom Parkin /* Disable udp encapsulation */ 12010d76751fSJames Chapman switch (tunnel->encap) { 12020d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1203fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1204fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1205fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12069980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 12070d76751fSJames Chapman break; 12080d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12090d76751fSJames Chapman break; 12100d76751fSJames Chapman } 1211fd558d18SJames Chapman 1212fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1213fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1214fd558d18SJames Chapman sk->sk_user_data = NULL; 1215f8ccac0eSTom Parkin 1216fd558d18SJames Chapman /* Call the original destructor */ 1217fd558d18SJames Chapman if (sk->sk_destruct) 1218fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1219d00fa9adSJames Chapman 1220d00fa9adSJames Chapman kfree_rcu(tunnel, rcu); 1221fd558d18SJames Chapman end: 1222fd558d18SJames Chapman return; 1223fd558d18SJames Chapman } 1224fd558d18SJames Chapman 1225b2aecfe8STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. */ 1226b2aecfe8STom Parkin static void l2tp_session_unhash(struct l2tp_session *session) 1227b2aecfe8STom Parkin { 1228b2aecfe8STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1229b2aecfe8STom Parkin 1230b2aecfe8STom Parkin /* Remove the session from core hashes */ 1231b2aecfe8STom Parkin if (tunnel) { 1232b2aecfe8STom Parkin /* Remove from the per-tunnel hash */ 1233b2aecfe8STom Parkin write_lock_bh(&tunnel->hlist_lock); 1234b2aecfe8STom Parkin hlist_del_init(&session->hlist); 1235b2aecfe8STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1236b2aecfe8STom Parkin 1237b2aecfe8STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1238b2aecfe8STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1239b2aecfe8STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1240b2aecfe8STom Parkin 1241b2aecfe8STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1242b2aecfe8STom Parkin hlist_del_init_rcu(&session->global_hlist); 1243b2aecfe8STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1244b2aecfe8STom Parkin synchronize_rcu(); 1245b2aecfe8STom Parkin } 1246b2aecfe8STom Parkin } 1247b2aecfe8STom Parkin } 1248b2aecfe8STom Parkin 1249fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1250fd558d18SJames Chapman */ 1251d08532bbSGuillaume Nault static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1252fd558d18SJames Chapman { 1253fd558d18SJames Chapman int hash; 1254fd558d18SJames Chapman struct hlist_node *walk; 1255fd558d18SJames Chapman struct hlist_node *tmp; 1256fd558d18SJames Chapman struct l2tp_session *session; 1257fd558d18SJames Chapman 1258a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1259a4ca44faSJoe Perches tunnel->name); 1260fd558d18SJames Chapman 1261fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1262f3c66d4eSGuillaume Nault tunnel->acpt_newsess = false; 1263fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1264fd558d18SJames Chapman again: 1265fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1266fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1267fd558d18SJames Chapman 1268a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1269fd558d18SJames Chapman "%s: closing session\n", session->name); 1270fd558d18SJames Chapman 1271fd558d18SJames Chapman hlist_del_init(&session->hlist); 1272fd558d18SJames Chapman 1273b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1274b228a940SGuillaume Nault goto again; 1275b228a940SGuillaume Nault 1276fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1277fd558d18SJames Chapman 1278b2aecfe8STom Parkin l2tp_session_unhash(session); 12794c6e2fd3STom Parkin l2tp_session_queue_purge(session); 12804c6e2fd3STom Parkin 12810febc7b3STom Parkin if (session->session_close) 1282fd558d18SJames Chapman (*session->session_close)(session); 1283fd558d18SJames Chapman 12849980d001STom Parkin l2tp_session_dec_refcount(session); 12859980d001STom Parkin 1286fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1287fd558d18SJames Chapman 1288fd558d18SJames Chapman /* Now restart from the beginning of this hash 1289fd558d18SJames Chapman * chain. We always remove a session from the 1290fd558d18SJames Chapman * list so we are guaranteed to make forward 1291fd558d18SJames Chapman * progress. 1292fd558d18SJames Chapman */ 1293fd558d18SJames Chapman goto again; 1294fd558d18SJames Chapman } 1295fd558d18SJames Chapman } 1296fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1297fd558d18SJames Chapman } 1298fd558d18SJames Chapman 12999980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 13009980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 13019980d001STom Parkin { 1302d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1303d00fa9adSJames Chapman 1304d00fa9adSJames Chapman if (tunnel) 1305d00fa9adSJames Chapman l2tp_tunnel_delete(tunnel); 13069980d001STom Parkin } 13079980d001STom Parkin 1308f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1309f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1310f8ccac0eSTom Parkin { 1311d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel, 1312d00fa9adSJames Chapman del_work); 1313d00fa9adSJames Chapman struct sock *sk = tunnel->sock; 1314d00fa9adSJames Chapman struct socket *sock = sk->sk_socket; 131528f5bfb8SJames Chapman struct l2tp_net *pn; 131612d656afSRidge Kennedy 131712d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 131812d656afSRidge Kennedy 131976a6abdbSJames Chapman /* If the tunnel socket was created within the kernel, use 132002d13ed5STom Parkin * the sk API to release it here. 1321f8ccac0eSTom Parkin */ 132276a6abdbSJames Chapman if (tunnel->fd < 0) { 132326abe143SEric W. Biederman if (sock) { 1324167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 132526abe143SEric W. Biederman sock_release(sock); 132626abe143SEric W. Biederman } 1327167eb17eSTom Parkin } 1328f8ccac0eSTom Parkin 132928f5bfb8SJames Chapman /* Remove the tunnel struct from the tunnel list */ 133028f5bfb8SJames Chapman pn = l2tp_pernet(tunnel->l2tp_net); 133128f5bfb8SJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 133228f5bfb8SJames Chapman list_del_rcu(&tunnel->list); 133328f5bfb8SJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 133428f5bfb8SJames Chapman 1335d00fa9adSJames Chapman /* drop initial ref */ 1336d00fa9adSJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1337d00fa9adSJames Chapman 1338d00fa9adSJames Chapman /* drop workqueue ref */ 133906a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1340fd558d18SJames Chapman } 1341fd558d18SJames Chapman 1342789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1343789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1344789a4a2cSJames Chapman * managing L2TP daemon. 1345167eb17eSTom Parkin * 1346167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1347167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1348167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1349167eb17eSTom Parkin * exit hook. 1350789a4a2cSJames Chapman */ 1351167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1352167eb17eSTom Parkin u32 tunnel_id, 1353167eb17eSTom Parkin u32 peer_tunnel_id, 1354167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1355167eb17eSTom Parkin struct socket **sockp) 1356789a4a2cSJames Chapman { 1357789a4a2cSJames Chapman int err = -EINVAL; 13587bddd0dbSEric Dumazet struct socket *sock = NULL; 135985644b4dSTom Herbert struct udp_port_cfg udp_conf; 1360789a4a2cSJames Chapman 1361789a4a2cSJames Chapman switch (cfg->encap) { 1362789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 136385644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 136485644b4dSTom Herbert 1365f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1366f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 136785644b4dSTom Herbert udp_conf.family = AF_INET6; 136885644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 136985644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 137085644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 137185644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 137285644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1373018f8258SWang Shanker !cfg->udp6_zero_tx_checksums; 137485644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1375018f8258SWang Shanker !cfg->udp6_zero_rx_checksums; 1376f9bac8dfSChris Elston } else 1377f9bac8dfSChris Elston #endif 1378f9bac8dfSChris Elston { 137985644b4dSTom Herbert udp_conf.family = AF_INET; 138085644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 138185644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 138285644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1383f9bac8dfSChris Elston } 1384789a4a2cSJames Chapman 138585644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 138685644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 138785644b4dSTom Herbert 138885644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 138985644b4dSTom Herbert if (err < 0) 139085644b4dSTom Herbert goto out; 1391789a4a2cSJames Chapman 1392789a4a2cSJames Chapman break; 1393789a4a2cSJames Chapman 1394789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1395f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1396f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 139785644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 139885644b4dSTom Herbert 139926abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1400167eb17eSTom Parkin IPPROTO_L2TP, &sock); 14015dac94e1SJames Chapman if (err < 0) 1402f9bac8dfSChris Elston goto out; 14035dac94e1SJames Chapman 14045dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14055dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14065dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14075dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14085dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip6_addr, 14095dac94e1SJames Chapman sizeof(ip6_addr)); 14105dac94e1SJames Chapman if (err < 0) 14115dac94e1SJames Chapman goto out; 14125dac94e1SJames Chapman 14135dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14145dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14155dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14165dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14175dac94e1SJames Chapman err = kernel_connect(sock, 14185dac94e1SJames Chapman (struct sockaddr *)&ip6_addr, 14195dac94e1SJames Chapman sizeof(ip6_addr), 0); 14205dac94e1SJames Chapman if (err < 0) 14215dac94e1SJames Chapman goto out; 14225dac94e1SJames Chapman } else 1423f9bac8dfSChris Elston #endif 14245dac94e1SJames Chapman { 142585644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 142685644b4dSTom Herbert 142726abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1428167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1429789a4a2cSJames Chapman if (err < 0) 1430789a4a2cSJames Chapman goto out; 1431789a4a2cSJames Chapman 1432789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1433789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1434789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14355dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip_addr, 14365dac94e1SJames Chapman sizeof(ip_addr)); 1437789a4a2cSJames Chapman if (err < 0) 1438789a4a2cSJames Chapman goto out; 1439789a4a2cSJames Chapman 1440789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1441789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1442789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14435dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *)&ip_addr, 14445dac94e1SJames Chapman sizeof(ip_addr), 0); 1445789a4a2cSJames Chapman if (err < 0) 1446789a4a2cSJames Chapman goto out; 14475dac94e1SJames Chapman } 1448789a4a2cSJames Chapman break; 1449789a4a2cSJames Chapman 1450789a4a2cSJames Chapman default: 1451789a4a2cSJames Chapman goto out; 1452789a4a2cSJames Chapman } 1453789a4a2cSJames Chapman 1454789a4a2cSJames Chapman out: 1455167eb17eSTom Parkin *sockp = sock; 14566c0ec37bSTom Parkin if (err < 0 && sock) { 1457167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 145826abe143SEric W. Biederman sock_release(sock); 1459789a4a2cSJames Chapman *sockp = NULL; 1460789a4a2cSJames Chapman } 1461789a4a2cSJames Chapman 1462789a4a2cSJames Chapman return err; 1463789a4a2cSJames Chapman } 1464789a4a2cSJames Chapman 146537159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 146637159ef2SEric Dumazet 1467c0235fb3STom Parkin int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, 1468c0235fb3STom Parkin struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1469fd558d18SJames Chapman { 1470fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1471fd558d18SJames Chapman int err; 14720d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1473fd558d18SJames Chapman 14740febc7b3STom Parkin if (cfg) 14750d76751fSJames Chapman encap = cfg->encap; 14760d76751fSJames Chapman 147770c05bfaSTom Parkin tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); 14780febc7b3STom Parkin if (!tunnel) { 1479fd558d18SJames Chapman err = -ENOMEM; 1480fd558d18SJames Chapman goto err; 1481fd558d18SJames Chapman } 1482fd558d18SJames Chapman 1483fd558d18SJames Chapman tunnel->version = version; 1484fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1485fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1486fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1487fd558d18SJames Chapman 1488fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1489fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1490fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1491f3c66d4eSGuillaume Nault tunnel->acpt_newsess = true; 1492fd558d18SJames Chapman 14930febc7b3STom Parkin if (cfg) 1494fd558d18SJames Chapman tunnel->debug = cfg->debug; 1495fd558d18SJames Chapman 14960d76751fSJames Chapman tunnel->encap = encap; 1497fd558d18SJames Chapman 1498d00fa9adSJames Chapman refcount_set(&tunnel->ref_count, 1); 1499d00fa9adSJames Chapman tunnel->fd = fd; 1500d00fa9adSJames Chapman 1501f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1502f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1503f8ccac0eSTom Parkin 1504fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1505fd558d18SJames Chapman 1506fd558d18SJames Chapman err = 0; 1507fd558d18SJames Chapman err: 1508fd558d18SJames Chapman if (tunnelp) 1509fd558d18SJames Chapman *tunnelp = tunnel; 1510fd558d18SJames Chapman 1511fd558d18SJames Chapman return err; 1512fd558d18SJames Chapman } 1513fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1514fd558d18SJames Chapman 15156b9f3423SGuillaume Nault static int l2tp_validate_socket(const struct sock *sk, const struct net *net, 15166b9f3423SGuillaume Nault enum l2tp_encap_type encap) 15176b9f3423SGuillaume Nault { 15186b9f3423SGuillaume Nault if (!net_eq(sock_net(sk), net)) 15196b9f3423SGuillaume Nault return -EINVAL; 15206b9f3423SGuillaume Nault 15216b9f3423SGuillaume Nault if (sk->sk_type != SOCK_DGRAM) 15226b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 15236b9f3423SGuillaume Nault 1524d9a81a22SEric Dumazet if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) 1525d9a81a22SEric Dumazet return -EPROTONOSUPPORT; 1526d9a81a22SEric Dumazet 15276b9f3423SGuillaume Nault if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) || 15286b9f3423SGuillaume Nault (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) 15296b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 15306b9f3423SGuillaume Nault 15316b9f3423SGuillaume Nault if (sk->sk_user_data) 15326b9f3423SGuillaume Nault return -EBUSY; 15336b9f3423SGuillaume Nault 15346b9f3423SGuillaume Nault return 0; 15356b9f3423SGuillaume Nault } 15366b9f3423SGuillaume Nault 15376b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, 15386b9f3423SGuillaume Nault struct l2tp_tunnel_cfg *cfg) 15396b9f3423SGuillaume Nault { 1540f6cd651bSGuillaume Nault struct l2tp_tunnel *tunnel_walk; 15416b9f3423SGuillaume Nault struct l2tp_net *pn; 15426b9f3423SGuillaume Nault struct socket *sock; 15436b9f3423SGuillaume Nault struct sock *sk; 15446b9f3423SGuillaume Nault int ret; 15456b9f3423SGuillaume Nault 15466b9f3423SGuillaume Nault if (tunnel->fd < 0) { 15476b9f3423SGuillaume Nault ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id, 15486b9f3423SGuillaume Nault tunnel->peer_tunnel_id, cfg, 15496b9f3423SGuillaume Nault &sock); 15506b9f3423SGuillaume Nault if (ret < 0) 15516b9f3423SGuillaume Nault goto err; 15526b9f3423SGuillaume Nault } else { 15536b9f3423SGuillaume Nault sock = sockfd_lookup(tunnel->fd, &ret); 15546b9f3423SGuillaume Nault if (!sock) 15556b9f3423SGuillaume Nault goto err; 15566b9f3423SGuillaume Nault 15576b9f3423SGuillaume Nault ret = l2tp_validate_socket(sock->sk, net, tunnel->encap); 15586b9f3423SGuillaume Nault if (ret < 0) 15596b9f3423SGuillaume Nault goto err_sock; 15606b9f3423SGuillaume Nault } 15616b9f3423SGuillaume Nault 15626b9f3423SGuillaume Nault tunnel->l2tp_net = net; 15636b9f3423SGuillaume Nault pn = l2tp_pernet(net); 1564f6cd651bSGuillaume Nault 15656b9f3423SGuillaume Nault spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1566f6cd651bSGuillaume Nault list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) { 1567f6cd651bSGuillaume Nault if (tunnel_walk->tunnel_id == tunnel->tunnel_id) { 1568f6cd651bSGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1569f6cd651bSGuillaume Nault 1570f6cd651bSGuillaume Nault ret = -EEXIST; 1571f6cd651bSGuillaume Nault goto err_sock; 1572f6cd651bSGuillaume Nault } 1573f6cd651bSGuillaume Nault } 15746b9f3423SGuillaume Nault list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 15756b9f3423SGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 15766b9f3423SGuillaume Nault 1577f8504f4cSXin Long sk = sock->sk; 1578f8504f4cSXin Long sock_hold(sk); 1579f8504f4cSXin Long tunnel->sock = sk; 1580f8504f4cSXin Long 15816b9f3423SGuillaume Nault if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 15826b9f3423SGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { 15836b9f3423SGuillaume Nault .sk_user_data = tunnel, 15846b9f3423SGuillaume Nault .encap_type = UDP_ENCAP_L2TPINUDP, 15856b9f3423SGuillaume Nault .encap_rcv = l2tp_udp_encap_recv, 15866b9f3423SGuillaume Nault .encap_destroy = l2tp_udp_encap_destroy, 15876b9f3423SGuillaume Nault }; 15886b9f3423SGuillaume Nault 15896b9f3423SGuillaume Nault setup_udp_tunnel_sock(net, sock, &udp_cfg); 15906b9f3423SGuillaume Nault } else { 15916b9f3423SGuillaume Nault sk->sk_user_data = tunnel; 15926b9f3423SGuillaume Nault } 15936b9f3423SGuillaume Nault 15946b9f3423SGuillaume Nault tunnel->old_sk_destruct = sk->sk_destruct; 15956b9f3423SGuillaume Nault sk->sk_destruct = &l2tp_tunnel_destruct; 15966b9f3423SGuillaume Nault lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, 15976b9f3423SGuillaume Nault "l2tp_sock"); 15986b9f3423SGuillaume Nault sk->sk_allocation = GFP_ATOMIC; 15996b9f3423SGuillaume Nault 16006b9f3423SGuillaume Nault if (tunnel->fd >= 0) 16016b9f3423SGuillaume Nault sockfd_put(sock); 16026b9f3423SGuillaume Nault 16036b9f3423SGuillaume Nault return 0; 16046b9f3423SGuillaume Nault 16056b9f3423SGuillaume Nault err_sock: 1606f6cd651bSGuillaume Nault if (tunnel->fd < 0) 1607f6cd651bSGuillaume Nault sock_release(sock); 1608f6cd651bSGuillaume Nault else 16096b9f3423SGuillaume Nault sockfd_put(sock); 16106b9f3423SGuillaume Nault err: 16116b9f3423SGuillaume Nault return ret; 16126b9f3423SGuillaume Nault } 16136b9f3423SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_register); 16146b9f3423SGuillaume Nault 1615309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1616309795f4SJames Chapman */ 161762b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1618309795f4SJames Chapman { 161962b982eeSSabrina Dubroca if (!test_and_set_bit(0, &tunnel->dead)) { 162006a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 162162b982eeSSabrina Dubroca queue_work(l2tp_wq, &tunnel->del_work); 162206a15f51SAlexander Couzens } 1623309795f4SJames Chapman } 1624309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1625309795f4SJames Chapman 1626628703f5STom Parkin void l2tp_session_delete(struct l2tp_session *session) 1627309795f4SJames Chapman { 1628b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1629628703f5STom Parkin return; 1630b228a940SGuillaume Nault 1631b2aecfe8STom Parkin l2tp_session_unhash(session); 16324c6e2fd3STom Parkin l2tp_session_queue_purge(session); 16330febc7b3STom Parkin if (session->session_close) 1634309795f4SJames Chapman (*session->session_close)(session); 1635a4346210SGuillaume Nault 1636309795f4SJames Chapman l2tp_session_dec_refcount(session); 1637309795f4SJames Chapman } 1638309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1639309795f4SJames Chapman 1640f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 164162e7b6a5SLorenzo Bianconi * l2specific_type parameters are set. 1642f7faffa3SJames Chapman */ 1643bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1644f7faffa3SJames Chapman { 1645f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1646f7faffa3SJames Chapman session->hdr_len = 6; 1647f7faffa3SJames Chapman if (session->send_seq) 1648f7faffa3SJames Chapman session->hdr_len += 4; 1649f7faffa3SJames Chapman } else { 165062e7b6a5SLorenzo Bianconi session->hdr_len = 4 + session->cookie_len; 165162e7b6a5SLorenzo Bianconi session->hdr_len += l2tp_get_l2specific_len(session); 16520d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 16530d76751fSJames Chapman session->hdr_len += 4; 1654f7faffa3SJames Chapman } 1655f7faffa3SJames Chapman } 1656bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1657f7faffa3SJames Chapman 1658c0235fb3STom Parkin struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, 1659c0235fb3STom Parkin u32 peer_session_id, struct l2tp_session_cfg *cfg) 1660fd558d18SJames Chapman { 1661fd558d18SJames Chapman struct l2tp_session *session; 1662fd558d18SJames Chapman 166370c05bfaSTom Parkin session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL); 16640febc7b3STom Parkin if (session) { 1665fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1666fd558d18SJames Chapman session->tunnel = tunnel; 1667fd558d18SJames Chapman 1668fd558d18SJames Chapman session->session_id = session_id; 1669fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1670d301e325SJames Chapman session->nr = 0; 16718a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 16728a1631d5SJames Chapman session->nr_max = 0xffff; 16738a1631d5SJames Chapman else 16748a1631d5SJames Chapman session->nr_max = 0xffffff; 16758a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1676a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1677a0dbd822SJames Chapman 1678a0dbd822SJames Chapman /* Use NR of first received packet */ 1679a0dbd822SJames Chapman session->reorder_skip = 1; 1680fd558d18SJames Chapman 1681fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1682fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1683fd558d18SJames Chapman 1684fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1685fd558d18SJames Chapman 1686fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1687f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1688fd558d18SJames Chapman 1689fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1690fd558d18SJames Chapman session->debug = tunnel->debug; 1691fd558d18SJames Chapman 1692fd558d18SJames Chapman if (cfg) { 1693f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1694fd558d18SJames Chapman session->debug = cfg->debug; 1695fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1696fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1697fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1698f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1699f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1700f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1701f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1702f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1703f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1704fd558d18SJames Chapman } 1705fd558d18SJames Chapman 1706f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1707f7faffa3SJames Chapman 17089ee369a4SGuillaume Nault refcount_set(&session->ref_count, 1); 17099ee369a4SGuillaume Nault 1710fd558d18SJames Chapman return session; 1711fd558d18SJames Chapman } 1712dbdbc73bSGuillaume Nault 1713dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM); 1714dbdbc73bSGuillaume Nault } 1715fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1716fd558d18SJames Chapman 1717fd558d18SJames Chapman /***************************************************************************** 1718fd558d18SJames Chapman * Init and cleanup 1719fd558d18SJames Chapman *****************************************************************************/ 1720fd558d18SJames Chapman 1721fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1722fd558d18SJames Chapman { 1723e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1724f7faffa3SJames Chapman int hash; 1725fd558d18SJames Chapman 1726fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1727e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1728fd558d18SJames Chapman 1729f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1730f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1731f7faffa3SJames Chapman 1732e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1733f7faffa3SJames Chapman 1734fd558d18SJames Chapman return 0; 1735fd558d18SJames Chapman } 1736fd558d18SJames Chapman 1737167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1738167eb17eSTom Parkin { 1739167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1740167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 17411e7af3b2SVasily Averin int hash; 1742167eb17eSTom Parkin 1743167eb17eSTom Parkin rcu_read_lock_bh(); 1744167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 17454dc12ffeSJiri Slaby l2tp_tunnel_delete(tunnel); 1746167eb17eSTom Parkin } 1747167eb17eSTom Parkin rcu_read_unlock_bh(); 17482f86953eSSabrina Dubroca 1749638a3a1eSYueHaibing if (l2tp_wq) 17502f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 17512f86953eSSabrina Dubroca rcu_barrier(); 17521e7af3b2SVasily Averin 17531e7af3b2SVasily Averin for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 17541e7af3b2SVasily Averin WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); 1755167eb17eSTom Parkin } 1756167eb17eSTom Parkin 1757fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1758fd558d18SJames Chapman .init = l2tp_init_net, 1759167eb17eSTom Parkin .exit = l2tp_exit_net, 1760fd558d18SJames Chapman .id = &l2tp_net_id, 1761fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1762fd558d18SJames Chapman }; 1763fd558d18SJames Chapman 1764fd558d18SJames Chapman static int __init l2tp_init(void) 1765fd558d18SJames Chapman { 1766fd558d18SJames Chapman int rc = 0; 1767fd558d18SJames Chapman 1768fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1769fd558d18SJames Chapman if (rc) 1770fd558d18SJames Chapman goto out; 1771fd558d18SJames Chapman 177259ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1773f8ccac0eSTom Parkin if (!l2tp_wq) { 1774f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 177567e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1776f8ccac0eSTom Parkin rc = -ENOMEM; 1777f8ccac0eSTom Parkin goto out; 1778f8ccac0eSTom Parkin } 1779f8ccac0eSTom Parkin 1780a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1781fd558d18SJames Chapman 1782fd558d18SJames Chapman out: 1783fd558d18SJames Chapman return rc; 1784fd558d18SJames Chapman } 1785fd558d18SJames Chapman 1786fd558d18SJames Chapman static void __exit l2tp_exit(void) 1787fd558d18SJames Chapman { 1788fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1789f8ccac0eSTom Parkin if (l2tp_wq) { 1790f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1791f8ccac0eSTom Parkin l2tp_wq = NULL; 1792f8ccac0eSTom Parkin } 1793fd558d18SJames Chapman } 1794fd558d18SJames Chapman 1795fd558d18SJames Chapman module_init(l2tp_init); 1796fd558d18SJames Chapman module_exit(l2tp_exit); 1797fd558d18SJames Chapman 1798fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1799fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1800fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1801fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1802