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 } 811f7faffa3SJames Chapman EXPORT_SYMBOL(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 */ 1119f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1120fd558d18SJames Chapman 11210d76751fSJames Chapman /* Reset skb netfilter state */ 1122fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1123fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1124fd558d18SJames Chapman IPSKB_REROUTED); 1125895b5c9fSFlorian Westphal nf_reset_ct(skb); 1126fd558d18SJames Chapman 11276af88da1SDavid S. Miller bh_lock_sock(sk); 11286af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1129b8c84307SEric Dumazet kfree_skb(skb); 1130b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11316af88da1SDavid S. Miller goto out_unlock; 11326af88da1SDavid S. Miller } 11336af88da1SDavid S. Miller 1134b954f940SPaolo Abeni /* The user-space may change the connection status for the user-space 1135b954f940SPaolo Abeni * provided socket at run time: we must check it under the socket lock 1136b954f940SPaolo Abeni */ 1137b954f940SPaolo Abeni if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) { 1138b954f940SPaolo Abeni kfree_skb(skb); 1139b954f940SPaolo Abeni ret = NET_XMIT_DROP; 1140b954f940SPaolo Abeni goto out_unlock; 1141b954f940SPaolo Abeni } 1142b954f940SPaolo Abeni 1143d9d8da80SDavid S. Miller inet = inet_sk(sk); 1144d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11450d76751fSJames Chapman switch (tunnel->encap) { 11460d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 11470d76751fSJames Chapman /* Setup UDP header */ 11480d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11490d76751fSJames Chapman skb_reset_transport_header(skb); 11500d76751fSJames Chapman uh = udp_hdr(skb); 11510d76751fSJames Chapman uh->source = inet->inet_sport; 11520d76751fSJames Chapman uh->dest = inet->inet_dport; 11530d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11540d76751fSJames Chapman uh->len = htons(udp_len); 1155fd558d18SJames Chapman 1156fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1157d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1158b954f940SPaolo Abeni if (l2tp_sk_is_v6(sk)) 115977157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 116077157e19STom Herbert skb, &inet6_sk(sk)->saddr, 116177157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1162d2cf3361SBenjamin LaHaise else 1163d2cf3361SBenjamin LaHaise #endif 116477157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 116577157e19STom Herbert inet->inet_daddr, udp_len); 11660d76751fSJames Chapman break; 11670d76751fSJames Chapman 11680d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11690d76751fSJames Chapman break; 11700d76751fSJames Chapman } 11710d76751fSJames Chapman 1172d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 11736af88da1SDavid S. Miller out_unlock: 11746af88da1SDavid S. Miller bh_unlock_sock(sk); 1175fd558d18SJames Chapman 1176b8c84307SEric Dumazet return ret; 1177fd558d18SJames Chapman } 1178fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1179fd558d18SJames Chapman 1180fd558d18SJames Chapman /***************************************************************************** 1181fd558d18SJames Chapman * Tinnel and session create/destroy. 1182fd558d18SJames Chapman *****************************************************************************/ 1183fd558d18SJames Chapman 1184fd558d18SJames Chapman /* Tunnel socket destruct hook. 1185fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1186fd558d18SJames Chapman * closed. 1187fd558d18SJames Chapman */ 1188fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1189fd558d18SJames Chapman { 11908d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1191fd558d18SJames Chapman 11920febc7b3STom Parkin if (!tunnel) 1193fd558d18SJames Chapman goto end; 1194fd558d18SJames Chapman 1195a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1196fd558d18SJames Chapman 1197f8ccac0eSTom Parkin /* Disable udp encapsulation */ 11980d76751fSJames Chapman switch (tunnel->encap) { 11990d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1200fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1201fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1202fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12039980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 12040d76751fSJames Chapman break; 12050d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12060d76751fSJames Chapman break; 12070d76751fSJames Chapman } 1208fd558d18SJames Chapman 1209fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1210fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1211fd558d18SJames Chapman sk->sk_user_data = NULL; 1212f8ccac0eSTom Parkin 1213fd558d18SJames Chapman /* Call the original destructor */ 1214fd558d18SJames Chapman if (sk->sk_destruct) 1215fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1216d00fa9adSJames Chapman 1217d00fa9adSJames Chapman kfree_rcu(tunnel, rcu); 1218fd558d18SJames Chapman end: 1219fd558d18SJames Chapman return; 1220fd558d18SJames Chapman } 1221fd558d18SJames Chapman 1222b2aecfe8STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. */ 1223b2aecfe8STom Parkin static void l2tp_session_unhash(struct l2tp_session *session) 1224b2aecfe8STom Parkin { 1225b2aecfe8STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1226b2aecfe8STom Parkin 1227b2aecfe8STom Parkin /* Remove the session from core hashes */ 1228b2aecfe8STom Parkin if (tunnel) { 1229b2aecfe8STom Parkin /* Remove from the per-tunnel hash */ 1230b2aecfe8STom Parkin write_lock_bh(&tunnel->hlist_lock); 1231b2aecfe8STom Parkin hlist_del_init(&session->hlist); 1232b2aecfe8STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1233b2aecfe8STom Parkin 1234b2aecfe8STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1235b2aecfe8STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1236b2aecfe8STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1237b2aecfe8STom Parkin 1238b2aecfe8STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1239b2aecfe8STom Parkin hlist_del_init_rcu(&session->global_hlist); 1240b2aecfe8STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1241b2aecfe8STom Parkin synchronize_rcu(); 1242b2aecfe8STom Parkin } 1243b2aecfe8STom Parkin } 1244b2aecfe8STom Parkin } 1245b2aecfe8STom Parkin 1246fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1247fd558d18SJames Chapman */ 1248d08532bbSGuillaume Nault static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1249fd558d18SJames Chapman { 1250fd558d18SJames Chapman int hash; 1251fd558d18SJames Chapman struct hlist_node *walk; 1252fd558d18SJames Chapman struct hlist_node *tmp; 1253fd558d18SJames Chapman struct l2tp_session *session; 1254fd558d18SJames Chapman 1255a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1256a4ca44faSJoe Perches tunnel->name); 1257fd558d18SJames Chapman 1258fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1259f3c66d4eSGuillaume Nault tunnel->acpt_newsess = false; 1260fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1261fd558d18SJames Chapman again: 1262fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1263fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1264fd558d18SJames Chapman 1265a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1266fd558d18SJames Chapman "%s: closing session\n", session->name); 1267fd558d18SJames Chapman 1268fd558d18SJames Chapman hlist_del_init(&session->hlist); 1269fd558d18SJames Chapman 1270b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1271b228a940SGuillaume Nault goto again; 1272b228a940SGuillaume Nault 1273fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1274fd558d18SJames Chapman 1275b2aecfe8STom Parkin l2tp_session_unhash(session); 12764c6e2fd3STom Parkin l2tp_session_queue_purge(session); 12774c6e2fd3STom Parkin 12780febc7b3STom Parkin if (session->session_close) 1279fd558d18SJames Chapman (*session->session_close)(session); 1280fd558d18SJames Chapman 12819980d001STom Parkin l2tp_session_dec_refcount(session); 12829980d001STom Parkin 1283fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1284fd558d18SJames Chapman 1285fd558d18SJames Chapman /* Now restart from the beginning of this hash 1286fd558d18SJames Chapman * chain. We always remove a session from the 1287fd558d18SJames Chapman * list so we are guaranteed to make forward 1288fd558d18SJames Chapman * progress. 1289fd558d18SJames Chapman */ 1290fd558d18SJames Chapman goto again; 1291fd558d18SJames Chapman } 1292fd558d18SJames Chapman } 1293fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1294fd558d18SJames Chapman } 1295fd558d18SJames Chapman 12969980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 12979980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 12989980d001STom Parkin { 1299d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1300d00fa9adSJames Chapman 1301d00fa9adSJames Chapman if (tunnel) 1302d00fa9adSJames Chapman l2tp_tunnel_delete(tunnel); 13039980d001STom Parkin } 13049980d001STom Parkin 1305f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1306f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1307f8ccac0eSTom Parkin { 1308d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel, 1309d00fa9adSJames Chapman del_work); 1310d00fa9adSJames Chapman struct sock *sk = tunnel->sock; 1311d00fa9adSJames Chapman struct socket *sock = sk->sk_socket; 131228f5bfb8SJames Chapman struct l2tp_net *pn; 131312d656afSRidge Kennedy 131412d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 131512d656afSRidge Kennedy 131676a6abdbSJames Chapman /* If the tunnel socket was created within the kernel, use 131702d13ed5STom Parkin * the sk API to release it here. 1318f8ccac0eSTom Parkin */ 131976a6abdbSJames Chapman if (tunnel->fd < 0) { 132026abe143SEric W. Biederman if (sock) { 1321167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 132226abe143SEric W. Biederman sock_release(sock); 132326abe143SEric W. Biederman } 1324167eb17eSTom Parkin } 1325f8ccac0eSTom Parkin 132628f5bfb8SJames Chapman /* Remove the tunnel struct from the tunnel list */ 132728f5bfb8SJames Chapman pn = l2tp_pernet(tunnel->l2tp_net); 132828f5bfb8SJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 132928f5bfb8SJames Chapman list_del_rcu(&tunnel->list); 133028f5bfb8SJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 133128f5bfb8SJames Chapman 1332d00fa9adSJames Chapman /* drop initial ref */ 1333d00fa9adSJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1334d00fa9adSJames Chapman 1335d00fa9adSJames Chapman /* drop workqueue ref */ 133606a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1337fd558d18SJames Chapman } 1338fd558d18SJames Chapman 1339789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1340789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1341789a4a2cSJames Chapman * managing L2TP daemon. 1342167eb17eSTom Parkin * 1343167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1344167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1345167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1346167eb17eSTom Parkin * exit hook. 1347789a4a2cSJames Chapman */ 1348167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1349167eb17eSTom Parkin u32 tunnel_id, 1350167eb17eSTom Parkin u32 peer_tunnel_id, 1351167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1352167eb17eSTom Parkin struct socket **sockp) 1353789a4a2cSJames Chapman { 1354789a4a2cSJames Chapman int err = -EINVAL; 13557bddd0dbSEric Dumazet struct socket *sock = NULL; 135685644b4dSTom Herbert struct udp_port_cfg udp_conf; 1357789a4a2cSJames Chapman 1358789a4a2cSJames Chapman switch (cfg->encap) { 1359789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 136085644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 136185644b4dSTom Herbert 1362f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1363f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 136485644b4dSTom Herbert udp_conf.family = AF_INET6; 136585644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 136685644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 136785644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 136885644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 136985644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1370018f8258SWang Shanker !cfg->udp6_zero_tx_checksums; 137185644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1372018f8258SWang Shanker !cfg->udp6_zero_rx_checksums; 1373f9bac8dfSChris Elston } else 1374f9bac8dfSChris Elston #endif 1375f9bac8dfSChris Elston { 137685644b4dSTom Herbert udp_conf.family = AF_INET; 137785644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 137885644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 137985644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1380f9bac8dfSChris Elston } 1381789a4a2cSJames Chapman 138285644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 138385644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 138485644b4dSTom Herbert 138585644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 138685644b4dSTom Herbert if (err < 0) 138785644b4dSTom Herbert goto out; 1388789a4a2cSJames Chapman 1389789a4a2cSJames Chapman break; 1390789a4a2cSJames Chapman 1391789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1392f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1393f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 139485644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 139585644b4dSTom Herbert 139626abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1397167eb17eSTom Parkin IPPROTO_L2TP, &sock); 13985dac94e1SJames Chapman if (err < 0) 1399f9bac8dfSChris Elston goto out; 14005dac94e1SJames Chapman 14015dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14025dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14035dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14045dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14055dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip6_addr, 14065dac94e1SJames Chapman sizeof(ip6_addr)); 14075dac94e1SJames Chapman if (err < 0) 14085dac94e1SJames Chapman goto out; 14095dac94e1SJames Chapman 14105dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14115dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14125dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14135dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14145dac94e1SJames Chapman err = kernel_connect(sock, 14155dac94e1SJames Chapman (struct sockaddr *)&ip6_addr, 14165dac94e1SJames Chapman sizeof(ip6_addr), 0); 14175dac94e1SJames Chapman if (err < 0) 14185dac94e1SJames Chapman goto out; 14195dac94e1SJames Chapman } else 1420f9bac8dfSChris Elston #endif 14215dac94e1SJames Chapman { 142285644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 142385644b4dSTom Herbert 142426abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1425167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1426789a4a2cSJames Chapman if (err < 0) 1427789a4a2cSJames Chapman goto out; 1428789a4a2cSJames Chapman 1429789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1430789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1431789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14325dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip_addr, 14335dac94e1SJames Chapman sizeof(ip_addr)); 1434789a4a2cSJames Chapman if (err < 0) 1435789a4a2cSJames Chapman goto out; 1436789a4a2cSJames Chapman 1437789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1438789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1439789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14405dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *)&ip_addr, 14415dac94e1SJames Chapman sizeof(ip_addr), 0); 1442789a4a2cSJames Chapman if (err < 0) 1443789a4a2cSJames Chapman goto out; 14445dac94e1SJames Chapman } 1445789a4a2cSJames Chapman break; 1446789a4a2cSJames Chapman 1447789a4a2cSJames Chapman default: 1448789a4a2cSJames Chapman goto out; 1449789a4a2cSJames Chapman } 1450789a4a2cSJames Chapman 1451789a4a2cSJames Chapman out: 1452167eb17eSTom Parkin *sockp = sock; 14536c0ec37bSTom Parkin if (err < 0 && sock) { 1454167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 145526abe143SEric W. Biederman sock_release(sock); 1456789a4a2cSJames Chapman *sockp = NULL; 1457789a4a2cSJames Chapman } 1458789a4a2cSJames Chapman 1459789a4a2cSJames Chapman return err; 1460789a4a2cSJames Chapman } 1461789a4a2cSJames Chapman 146237159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 146337159ef2SEric Dumazet 1464c0235fb3STom Parkin int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, 1465c0235fb3STom Parkin struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1466fd558d18SJames Chapman { 1467fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1468fd558d18SJames Chapman int err; 14690d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1470fd558d18SJames Chapman 14710febc7b3STom Parkin if (cfg) 14720d76751fSJames Chapman encap = cfg->encap; 14730d76751fSJames Chapman 147470c05bfaSTom Parkin tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); 14750febc7b3STom Parkin if (!tunnel) { 1476fd558d18SJames Chapman err = -ENOMEM; 1477fd558d18SJames Chapman goto err; 1478fd558d18SJames Chapman } 1479fd558d18SJames Chapman 1480fd558d18SJames Chapman tunnel->version = version; 1481fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1482fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1483fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1484fd558d18SJames Chapman 1485fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1486fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1487fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1488f3c66d4eSGuillaume Nault tunnel->acpt_newsess = true; 1489fd558d18SJames Chapman 14900febc7b3STom Parkin if (cfg) 1491fd558d18SJames Chapman tunnel->debug = cfg->debug; 1492fd558d18SJames Chapman 14930d76751fSJames Chapman tunnel->encap = encap; 1494fd558d18SJames Chapman 1495d00fa9adSJames Chapman refcount_set(&tunnel->ref_count, 1); 1496d00fa9adSJames Chapman tunnel->fd = fd; 1497d00fa9adSJames Chapman 1498f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1499f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1500f8ccac0eSTom Parkin 1501fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1502fd558d18SJames Chapman 1503fd558d18SJames Chapman err = 0; 1504fd558d18SJames Chapman err: 1505fd558d18SJames Chapman if (tunnelp) 1506fd558d18SJames Chapman *tunnelp = tunnel; 1507fd558d18SJames Chapman 1508fd558d18SJames Chapman return err; 1509fd558d18SJames Chapman } 1510fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1511fd558d18SJames Chapman 15126b9f3423SGuillaume Nault static int l2tp_validate_socket(const struct sock *sk, const struct net *net, 15136b9f3423SGuillaume Nault enum l2tp_encap_type encap) 15146b9f3423SGuillaume Nault { 15156b9f3423SGuillaume Nault if (!net_eq(sock_net(sk), net)) 15166b9f3423SGuillaume Nault return -EINVAL; 15176b9f3423SGuillaume Nault 15186b9f3423SGuillaume Nault if (sk->sk_type != SOCK_DGRAM) 15196b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 15206b9f3423SGuillaume Nault 1521d9a81a22SEric Dumazet if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) 1522d9a81a22SEric Dumazet return -EPROTONOSUPPORT; 1523d9a81a22SEric Dumazet 15246b9f3423SGuillaume Nault if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) || 15256b9f3423SGuillaume Nault (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) 15266b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 15276b9f3423SGuillaume Nault 15286b9f3423SGuillaume Nault if (sk->sk_user_data) 15296b9f3423SGuillaume Nault return -EBUSY; 15306b9f3423SGuillaume Nault 15316b9f3423SGuillaume Nault return 0; 15326b9f3423SGuillaume Nault } 15336b9f3423SGuillaume Nault 15346b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, 15356b9f3423SGuillaume Nault struct l2tp_tunnel_cfg *cfg) 15366b9f3423SGuillaume Nault { 1537f6cd651bSGuillaume Nault struct l2tp_tunnel *tunnel_walk; 15386b9f3423SGuillaume Nault struct l2tp_net *pn; 15396b9f3423SGuillaume Nault struct socket *sock; 15406b9f3423SGuillaume Nault struct sock *sk; 15416b9f3423SGuillaume Nault int ret; 15426b9f3423SGuillaume Nault 15436b9f3423SGuillaume Nault if (tunnel->fd < 0) { 15446b9f3423SGuillaume Nault ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id, 15456b9f3423SGuillaume Nault tunnel->peer_tunnel_id, cfg, 15466b9f3423SGuillaume Nault &sock); 15476b9f3423SGuillaume Nault if (ret < 0) 15486b9f3423SGuillaume Nault goto err; 15496b9f3423SGuillaume Nault } else { 15506b9f3423SGuillaume Nault sock = sockfd_lookup(tunnel->fd, &ret); 15516b9f3423SGuillaume Nault if (!sock) 15526b9f3423SGuillaume Nault goto err; 15536b9f3423SGuillaume Nault 15546b9f3423SGuillaume Nault ret = l2tp_validate_socket(sock->sk, net, tunnel->encap); 15556b9f3423SGuillaume Nault if (ret < 0) 15566b9f3423SGuillaume Nault goto err_sock; 15576b9f3423SGuillaume Nault } 15586b9f3423SGuillaume Nault 15596b9f3423SGuillaume Nault tunnel->l2tp_net = net; 15606b9f3423SGuillaume Nault pn = l2tp_pernet(net); 1561f6cd651bSGuillaume Nault 15626b9f3423SGuillaume Nault spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1563f6cd651bSGuillaume Nault list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) { 1564f6cd651bSGuillaume Nault if (tunnel_walk->tunnel_id == tunnel->tunnel_id) { 1565f6cd651bSGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1566f6cd651bSGuillaume Nault 1567f6cd651bSGuillaume Nault ret = -EEXIST; 1568f6cd651bSGuillaume Nault goto err_sock; 1569f6cd651bSGuillaume Nault } 1570f6cd651bSGuillaume Nault } 15716b9f3423SGuillaume Nault list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 15726b9f3423SGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 15736b9f3423SGuillaume Nault 1574f8504f4cSXin Long sk = sock->sk; 1575f8504f4cSXin Long sock_hold(sk); 1576f8504f4cSXin Long tunnel->sock = sk; 1577f8504f4cSXin Long 15786b9f3423SGuillaume Nault if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 15796b9f3423SGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { 15806b9f3423SGuillaume Nault .sk_user_data = tunnel, 15816b9f3423SGuillaume Nault .encap_type = UDP_ENCAP_L2TPINUDP, 15826b9f3423SGuillaume Nault .encap_rcv = l2tp_udp_encap_recv, 15836b9f3423SGuillaume Nault .encap_destroy = l2tp_udp_encap_destroy, 15846b9f3423SGuillaume Nault }; 15856b9f3423SGuillaume Nault 15866b9f3423SGuillaume Nault setup_udp_tunnel_sock(net, sock, &udp_cfg); 15876b9f3423SGuillaume Nault } else { 15886b9f3423SGuillaume Nault sk->sk_user_data = tunnel; 15896b9f3423SGuillaume Nault } 15906b9f3423SGuillaume Nault 15916b9f3423SGuillaume Nault tunnel->old_sk_destruct = sk->sk_destruct; 15926b9f3423SGuillaume Nault sk->sk_destruct = &l2tp_tunnel_destruct; 15936b9f3423SGuillaume Nault lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, 15946b9f3423SGuillaume Nault "l2tp_sock"); 15956b9f3423SGuillaume Nault sk->sk_allocation = GFP_ATOMIC; 15966b9f3423SGuillaume Nault 15976b9f3423SGuillaume Nault if (tunnel->fd >= 0) 15986b9f3423SGuillaume Nault sockfd_put(sock); 15996b9f3423SGuillaume Nault 16006b9f3423SGuillaume Nault return 0; 16016b9f3423SGuillaume Nault 16026b9f3423SGuillaume Nault err_sock: 1603f6cd651bSGuillaume Nault if (tunnel->fd < 0) 1604f6cd651bSGuillaume Nault sock_release(sock); 1605f6cd651bSGuillaume Nault else 16066b9f3423SGuillaume Nault sockfd_put(sock); 16076b9f3423SGuillaume Nault err: 16086b9f3423SGuillaume Nault return ret; 16096b9f3423SGuillaume Nault } 16106b9f3423SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_register); 16116b9f3423SGuillaume Nault 1612309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1613309795f4SJames Chapman */ 161462b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1615309795f4SJames Chapman { 161662b982eeSSabrina Dubroca if (!test_and_set_bit(0, &tunnel->dead)) { 161706a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 161862b982eeSSabrina Dubroca queue_work(l2tp_wq, &tunnel->del_work); 161906a15f51SAlexander Couzens } 1620309795f4SJames Chapman } 1621309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1622309795f4SJames Chapman 1623628703f5STom Parkin void l2tp_session_delete(struct l2tp_session *session) 1624309795f4SJames Chapman { 1625b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1626628703f5STom Parkin return; 1627b228a940SGuillaume Nault 1628b2aecfe8STom Parkin l2tp_session_unhash(session); 16294c6e2fd3STom Parkin l2tp_session_queue_purge(session); 16300febc7b3STom Parkin if (session->session_close) 1631309795f4SJames Chapman (*session->session_close)(session); 1632a4346210SGuillaume Nault 1633309795f4SJames Chapman l2tp_session_dec_refcount(session); 1634309795f4SJames Chapman } 1635309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1636309795f4SJames Chapman 1637f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 163862e7b6a5SLorenzo Bianconi * l2specific_type parameters are set. 1639f7faffa3SJames Chapman */ 1640bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1641f7faffa3SJames Chapman { 1642f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1643f7faffa3SJames Chapman session->hdr_len = 6; 1644f7faffa3SJames Chapman if (session->send_seq) 1645f7faffa3SJames Chapman session->hdr_len += 4; 1646f7faffa3SJames Chapman } else { 164762e7b6a5SLorenzo Bianconi session->hdr_len = 4 + session->cookie_len; 164862e7b6a5SLorenzo Bianconi session->hdr_len += l2tp_get_l2specific_len(session); 16490d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 16500d76751fSJames Chapman session->hdr_len += 4; 1651f7faffa3SJames Chapman } 1652f7faffa3SJames Chapman } 1653bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1654f7faffa3SJames Chapman 1655c0235fb3STom Parkin struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, 1656c0235fb3STom Parkin u32 peer_session_id, struct l2tp_session_cfg *cfg) 1657fd558d18SJames Chapman { 1658fd558d18SJames Chapman struct l2tp_session *session; 1659fd558d18SJames Chapman 166070c05bfaSTom Parkin session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL); 16610febc7b3STom Parkin if (session) { 1662fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1663fd558d18SJames Chapman session->tunnel = tunnel; 1664fd558d18SJames Chapman 1665fd558d18SJames Chapman session->session_id = session_id; 1666fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1667d301e325SJames Chapman session->nr = 0; 16688a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 16698a1631d5SJames Chapman session->nr_max = 0xffff; 16708a1631d5SJames Chapman else 16718a1631d5SJames Chapman session->nr_max = 0xffffff; 16728a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1673a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1674a0dbd822SJames Chapman 1675a0dbd822SJames Chapman /* Use NR of first received packet */ 1676a0dbd822SJames Chapman session->reorder_skip = 1; 1677fd558d18SJames Chapman 1678fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1679fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1680fd558d18SJames Chapman 1681fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1682fd558d18SJames Chapman 1683fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1684f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1685fd558d18SJames Chapman 1686fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1687fd558d18SJames Chapman session->debug = tunnel->debug; 1688fd558d18SJames Chapman 1689fd558d18SJames Chapman if (cfg) { 1690f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1691fd558d18SJames Chapman session->debug = cfg->debug; 1692fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1693fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1694fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1695f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1696f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1697f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1698f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1699f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1700f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1701fd558d18SJames Chapman } 1702fd558d18SJames Chapman 1703f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1704f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1705f7faffa3SJames Chapman else 1706f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1707f7faffa3SJames Chapman 1708f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1709f7faffa3SJames Chapman 17109ee369a4SGuillaume Nault refcount_set(&session->ref_count, 1); 17119ee369a4SGuillaume Nault 1712fd558d18SJames Chapman return session; 1713fd558d18SJames Chapman } 1714dbdbc73bSGuillaume Nault 1715dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM); 1716dbdbc73bSGuillaume Nault } 1717fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1718fd558d18SJames Chapman 1719fd558d18SJames Chapman /***************************************************************************** 1720fd558d18SJames Chapman * Init and cleanup 1721fd558d18SJames Chapman *****************************************************************************/ 1722fd558d18SJames Chapman 1723fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1724fd558d18SJames Chapman { 1725e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1726f7faffa3SJames Chapman int hash; 1727fd558d18SJames Chapman 1728fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1729e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1730fd558d18SJames Chapman 1731f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1732f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1733f7faffa3SJames Chapman 1734e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1735f7faffa3SJames Chapman 1736fd558d18SJames Chapman return 0; 1737fd558d18SJames Chapman } 1738fd558d18SJames Chapman 1739167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1740167eb17eSTom Parkin { 1741167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1742167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 17431e7af3b2SVasily Averin int hash; 1744167eb17eSTom Parkin 1745167eb17eSTom Parkin rcu_read_lock_bh(); 1746167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 17474dc12ffeSJiri Slaby l2tp_tunnel_delete(tunnel); 1748167eb17eSTom Parkin } 1749167eb17eSTom Parkin rcu_read_unlock_bh(); 17502f86953eSSabrina Dubroca 1751638a3a1eSYueHaibing if (l2tp_wq) 17522f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 17532f86953eSSabrina Dubroca rcu_barrier(); 17541e7af3b2SVasily Averin 17551e7af3b2SVasily Averin for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 17561e7af3b2SVasily Averin WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); 1757167eb17eSTom Parkin } 1758167eb17eSTom Parkin 1759fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1760fd558d18SJames Chapman .init = l2tp_init_net, 1761167eb17eSTom Parkin .exit = l2tp_exit_net, 1762fd558d18SJames Chapman .id = &l2tp_net_id, 1763fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1764fd558d18SJames Chapman }; 1765fd558d18SJames Chapman 1766fd558d18SJames Chapman static int __init l2tp_init(void) 1767fd558d18SJames Chapman { 1768fd558d18SJames Chapman int rc = 0; 1769fd558d18SJames Chapman 1770fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1771fd558d18SJames Chapman if (rc) 1772fd558d18SJames Chapman goto out; 1773fd558d18SJames Chapman 177459ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1775f8ccac0eSTom Parkin if (!l2tp_wq) { 1776f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 177767e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1778f8ccac0eSTom Parkin rc = -ENOMEM; 1779f8ccac0eSTom Parkin goto out; 1780f8ccac0eSTom Parkin } 1781f8ccac0eSTom Parkin 1782a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1783fd558d18SJames Chapman 1784fd558d18SJames Chapman out: 1785fd558d18SJames Chapman return rc; 1786fd558d18SJames Chapman } 1787fd558d18SJames Chapman 1788fd558d18SJames Chapman static void __exit l2tp_exit(void) 1789fd558d18SJames Chapman { 1790fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1791f8ccac0eSTom Parkin if (l2tp_wq) { 1792f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1793f8ccac0eSTom Parkin l2tp_wq = NULL; 1794f8ccac0eSTom Parkin } 1795fd558d18SJames Chapman } 1796fd558d18SJames Chapman 1797fd558d18SJames Chapman module_init(l2tp_init); 1798fd558d18SJames Chapman module_exit(l2tp_exit); 1799fd558d18SJames Chapman 1800fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1801fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1802fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1803fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1804