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; 663f7faffa3SJames Chapman int offset; 664f7faffa3SJames Chapman 665f7faffa3SJames Chapman /* Parse and check optional cookie */ 666f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 667f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 6685ee759cdSTom Parkin pr_warn_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n", 669a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 670a4ca44faSJoe Perches session->session_id); 6717b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards); 672f7faffa3SJames Chapman goto discard; 673f7faffa3SJames Chapman } 674f7faffa3SJames Chapman ptr += session->peer_cookie_len; 675f7faffa3SJames Chapman } 676f7faffa3SJames Chapman 677f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 678f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 679f7faffa3SJames Chapman * 680f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 681f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 682f7faffa3SJames Chapman * we were expecting them, discard frame. 683f7faffa3SJames Chapman */ 684f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 685f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 686f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 687f7faffa3SJames Chapman /* Store L2TP info in the skb */ 68812923365STom Parkin L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr); 689f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 69012923365STom Parkin ptr += 2; 69112923365STom Parkin /* Skip past nr in the header */ 69212923365STom Parkin ptr += 2; 693f7faffa3SJames Chapman 694f7faffa3SJames Chapman } 695f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 696f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *)ptr); 697f7faffa3SJames Chapman 698f7faffa3SJames Chapman if (l2h & 0x40000000) { 699f7faffa3SJames Chapman /* Store L2TP info in the skb */ 70012923365STom Parkin L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff; 701f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 702f7faffa3SJames Chapman } 70362e7b6a5SLorenzo Bianconi ptr += 4; 704f7faffa3SJames Chapman } 705f7faffa3SJames Chapman 706f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 70720dcb110STom Parkin /* Received a packet with sequence numbers. If we're the LAC, 708f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 709f7faffa3SJames Chapman * configure it so. 710f7faffa3SJames Chapman */ 7116c0ec37bSTom Parkin if (!session->lns_mode && !session->send_seq) { 712a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 713f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 714f7faffa3SJames Chapman session->name); 7153f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1; 716f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 717f7faffa3SJames Chapman } 718f7faffa3SJames Chapman } else { 719f7faffa3SJames Chapman /* No sequence numbers. 720f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 721f7faffa3SJames Chapman */ 722f7faffa3SJames Chapman if (session->recv_seq) { 7235ee759cdSTom Parkin pr_warn_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n", 724a4ca44faSJoe Perches session->name); 7257b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 726f7faffa3SJames Chapman goto discard; 727f7faffa3SJames Chapman } 728f7faffa3SJames Chapman 729f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 730f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 731f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 732f7faffa3SJames Chapman * LAC is broken. Discard the frame. 733f7faffa3SJames Chapman */ 7346c0ec37bSTom Parkin if (!session->lns_mode && session->send_seq) { 735a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 736f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 737f7faffa3SJames Chapman session->name); 738f7faffa3SJames Chapman session->send_seq = 0; 739f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 740f7faffa3SJames Chapman } else if (session->send_seq) { 7415ee759cdSTom Parkin pr_warn_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n", 742a4ca44faSJoe Perches session->name); 7437b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 744f7faffa3SJames Chapman goto discard; 745f7faffa3SJames Chapman } 746f7faffa3SJames Chapman } 747f7faffa3SJames Chapman 748900631eeSJames Chapman /* Session data offset is defined only for L2TPv2 and is 749900631eeSJames Chapman * indicated by an optional 16-bit value in the header. 750f7faffa3SJames Chapman */ 751f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 752f7faffa3SJames Chapman /* If offset bit set, skip it. */ 753f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 754f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 755f7faffa3SJames Chapman ptr += 2 + offset; 756f7faffa3SJames Chapman } 757900631eeSJames Chapman } 758f7faffa3SJames Chapman 759f7faffa3SJames Chapman offset = ptr - optr; 760f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 761f7faffa3SJames Chapman goto discard; 762f7faffa3SJames Chapman 763f7faffa3SJames Chapman __skb_pull(skb, offset); 764f7faffa3SJames Chapman 765f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 766f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 767f7faffa3SJames Chapman * reordering. 768f7faffa3SJames Chapman */ 769f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 770f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 771f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 772f7faffa3SJames Chapman 773f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 774f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 775f7faffa3SJames Chapman */ 776f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 777b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb)) 778f7faffa3SJames Chapman goto discard; 779f7faffa3SJames Chapman } else { 780f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 781f7faffa3SJames Chapman * reorder queue. This ensures that it will be 782f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 783f7faffa3SJames Chapman */ 784f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 785f7faffa3SJames Chapman } 786f7faffa3SJames Chapman 787f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 788f7faffa3SJames Chapman l2tp_recv_dequeue(session); 789f7faffa3SJames Chapman 790f7faffa3SJames Chapman return; 791f7faffa3SJames Chapman 792f7faffa3SJames Chapman discard: 7937b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 794f7faffa3SJames Chapman kfree_skb(skb); 795f7faffa3SJames Chapman } 796ca7885dbSTom Parkin EXPORT_SYMBOL_GPL(l2tp_recv_common); 797f7faffa3SJames Chapman 79848f72f92STom Parkin /* Drop skbs from the session's reorder_q 79948f72f92STom Parkin */ 800493048f5STom Parkin static void l2tp_session_queue_purge(struct l2tp_session *session) 80148f72f92STom Parkin { 80248f72f92STom Parkin struct sk_buff *skb = NULL; 803b71a61ccSTom Parkin 804493048f5STom Parkin if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) 805493048f5STom Parkin return; 806493048f5STom Parkin 80748f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) { 80848f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors); 80948f72f92STom Parkin kfree_skb(skb); 81048f72f92STom Parkin } 81148f72f92STom Parkin } 81248f72f92STom Parkin 813fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 814fd558d18SJames Chapman * here. The skb is not on a list when we get here. 815fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 816fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 817fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 818fd558d18SJames Chapman */ 8192b139e6bSGuillaume Nault static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb) 820fd558d18SJames Chapman { 821fd558d18SJames Chapman struct l2tp_session *session = NULL; 822fd558d18SJames Chapman unsigned char *ptr, *optr; 823fd558d18SJames Chapman u16 hdrflags; 824fd558d18SJames Chapman u32 tunnel_id, session_id; 825fd558d18SJames Chapman u16 version; 826f7faffa3SJames Chapman int length; 827fd558d18SJames Chapman 82858d6085cSTom Herbert /* UDP has verifed checksum */ 829fd558d18SJames Chapman 830fd558d18SJames Chapman /* UDP always verifies the packet length. */ 831fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 832fd558d18SJames Chapman 833fd558d18SJames Chapman /* Short packet? */ 83491c52470SJacob Wen if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) { 8355ee759cdSTom Parkin pr_warn_ratelimited("%s: recv short packet (len=%d)\n", 836a4ca44faSJoe Perches tunnel->name, skb->len); 837fd558d18SJames Chapman goto error; 838fd558d18SJames Chapman } 839fd558d18SJames Chapman 840e50e705cSEric Dumazet /* Point to L2TP header */ 84195075150STom Parkin optr = skb->data; 84295075150STom Parkin ptr = skb->data; 843e50e705cSEric Dumazet 844fd558d18SJames Chapman /* Get L2TP header flags */ 845fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *)ptr); 846fd558d18SJames Chapman 847fd558d18SJames Chapman /* Check protocol version */ 848fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 849fd558d18SJames Chapman if (version != tunnel->version) { 8505ee759cdSTom Parkin pr_warn_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n", 851fd558d18SJames Chapman tunnel->name, version, tunnel->version); 852fd558d18SJames Chapman goto error; 853fd558d18SJames Chapman } 854fd558d18SJames Chapman 855fd558d18SJames Chapman /* Get length of L2TP packet */ 856fd558d18SJames Chapman length = skb->len; 857fd558d18SJames Chapman 858fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 85912923365STom Parkin if (hdrflags & L2TP_HDRFLAG_T) 860fd558d18SJames Chapman goto error; 861fd558d18SJames Chapman 862fd558d18SJames Chapman /* Skip flags */ 863fd558d18SJames Chapman ptr += 2; 864fd558d18SJames Chapman 865f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 866fd558d18SJames Chapman /* If length is present, skip it */ 867fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 868fd558d18SJames Chapman ptr += 2; 869fd558d18SJames Chapman 870fd558d18SJames Chapman /* Extract tunnel and session ID */ 871fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *)ptr); 872fd558d18SJames Chapman ptr += 2; 873fd558d18SJames Chapman session_id = ntohs(*(__be16 *)ptr); 874fd558d18SJames Chapman ptr += 2; 875f7faffa3SJames Chapman } else { 876f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 877f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 878f7faffa3SJames Chapman session_id = ntohl(*(__be32 *)ptr); 879f7faffa3SJames Chapman ptr += 4; 880f7faffa3SJames Chapman } 881fd558d18SJames Chapman 882fd558d18SJames Chapman /* Find the session context */ 88301e28b92SGuillaume Nault session = l2tp_tunnel_get_session(tunnel, session_id); 884309795f4SJames Chapman if (!session || !session->recv_skb) { 885a4346210SGuillaume Nault if (session) 88661b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 88761b9a047SGuillaume Nault 888fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 8895ee759cdSTom Parkin pr_warn_ratelimited("%s: no session found (%u/%u). Passing up.\n", 890fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 891fd558d18SJames Chapman goto error; 892fd558d18SJames Chapman } 893fd558d18SJames Chapman 8944522a70dSJacob Wen if (tunnel->version == L2TP_HDR_VER_3 && 8954522a70dSJacob Wen l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) 8964522a70dSJacob Wen goto error; 8974522a70dSJacob Wen 8982b139e6bSGuillaume Nault l2tp_recv_common(session, skb, ptr, optr, hdrflags, length); 89961b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 900fd558d18SJames Chapman 901fd558d18SJames Chapman return 0; 902fd558d18SJames Chapman 903fd558d18SJames Chapman error: 904fd558d18SJames Chapman /* Put UDP header back */ 905fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 906fd558d18SJames Chapman 907fd558d18SJames Chapman return 1; 908fd558d18SJames Chapman } 909fd558d18SJames Chapman 910fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 911fd558d18SJames Chapman * Return codes: 912fd558d18SJames Chapman * 0 : success. 913fd558d18SJames Chapman * <0: error 914fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 915fd558d18SJames Chapman */ 916fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 917fd558d18SJames Chapman { 918fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 919fd558d18SJames Chapman 920c1c47721SEric Dumazet tunnel = rcu_dereference_sk_user_data(sk); 9210febc7b3STom Parkin if (!tunnel) 922fd558d18SJames Chapman goto pass_up; 923fd558d18SJames Chapman 9242b139e6bSGuillaume Nault if (l2tp_udp_recv_core(tunnel, skb)) 925d00fa9adSJames Chapman goto pass_up; 926fd558d18SJames Chapman 927fd558d18SJames Chapman return 0; 928fd558d18SJames Chapman 929fd558d18SJames Chapman pass_up: 930fd558d18SJames Chapman return 1; 931fd558d18SJames Chapman } 932fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 933fd558d18SJames Chapman 934fd558d18SJames Chapman /************************************************************************ 935fd558d18SJames Chapman * Transmit handling 936fd558d18SJames Chapman ***********************************************************************/ 937fd558d18SJames Chapman 938fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 939fd558d18SJames Chapman */ 940f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 941fd558d18SJames Chapman { 942f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 943fd558d18SJames Chapman __be16 *bufp = buf; 944f7faffa3SJames Chapman __be16 *optr = buf; 945fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 946fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 947fd558d18SJames Chapman u32 session_id = session->peer_session_id; 948fd558d18SJames Chapman 949fd558d18SJames Chapman if (session->send_seq) 950fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 951fd558d18SJames Chapman 952fd558d18SJames Chapman /* Setup L2TP header. */ 953fd558d18SJames Chapman *bufp++ = htons(flags); 954fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 955fd558d18SJames Chapman *bufp++ = htons(session_id); 956fd558d18SJames Chapman if (session->send_seq) { 957fd558d18SJames Chapman *bufp++ = htons(session->ns); 958fd558d18SJames Chapman *bufp++ = 0; 959fd558d18SJames Chapman session->ns++; 960f7faffa3SJames Chapman session->ns &= 0xffff; 961a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 962a4ca44faSJoe Perches session->name, session->ns); 963fd558d18SJames Chapman } 964fd558d18SJames Chapman 965f7faffa3SJames Chapman return bufp - optr; 966f7faffa3SJames Chapman } 967f7faffa3SJames Chapman 968f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 969fd558d18SJames Chapman { 9700d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 971f7faffa3SJames Chapman char *bufp = buf; 972f7faffa3SJames Chapman char *optr = bufp; 973fd558d18SJames Chapman 9740d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 9750d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 9760d76751fSJames Chapman */ 9770d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 9780d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 979f7faffa3SJames Chapman *((__be16 *)bufp) = htons(flags); 980f7faffa3SJames Chapman bufp += 2; 981f7faffa3SJames Chapman *((__be16 *)bufp) = 0; 982f7faffa3SJames Chapman bufp += 2; 9830d76751fSJames Chapman } 9840d76751fSJames Chapman 985f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(session->peer_session_id); 986f7faffa3SJames Chapman bufp += 4; 987f7faffa3SJames Chapman if (session->cookie_len) { 988f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 989f7faffa3SJames Chapman bufp += session->cookie_len; 990fd558d18SJames Chapman } 991f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 992f7faffa3SJames Chapman u32 l2h = 0; 99362e7b6a5SLorenzo Bianconi 994f7faffa3SJames Chapman if (session->send_seq) { 995f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 996f7faffa3SJames Chapman session->ns++; 997f7faffa3SJames Chapman session->ns &= 0xffffff; 998a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 999a4ca44faSJoe Perches "%s: updated ns to %u\n", 1000a4ca44faSJoe Perches session->name, session->ns); 1001f7faffa3SJames Chapman } 1002f7faffa3SJames Chapman 1003f7faffa3SJames Chapman *((__be32 *)bufp) = htonl(l2h); 100462e7b6a5SLorenzo Bianconi bufp += 4; 1005f7faffa3SJames Chapman } 1006f7faffa3SJames Chapman 1007f7faffa3SJames Chapman return bufp - optr; 1008f7faffa3SJames Chapman } 1009fd558d18SJames Chapman 10102685fbb8SGuillaume Nault static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1011d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1012fd558d18SJames Chapman { 1013fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1014fd558d18SJames Chapman unsigned int len = skb->len; 1015fd558d18SJames Chapman int error; 1016fd558d18SJames Chapman 1017fd558d18SJames Chapman /* Queue the packet to IP for output */ 101860ff7467SWANG Cong skb->ignore_df = 1; 101927d53323SXin Long skb_dst_drop(skb); 1020d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1021b954f940SPaolo Abeni if (l2tp_sk_is_v6(tunnel->sock)) 1022b0270e91SEric Dumazet error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1023d2cf3361SBenjamin LaHaise else 1024d2cf3361SBenjamin LaHaise #endif 1025b0270e91SEric Dumazet error = ip_queue_xmit(tunnel->sock, skb, fl); 1026fd558d18SJames Chapman 1027fd558d18SJames Chapman /* Update stats */ 1028fd558d18SJames Chapman if (error >= 0) { 10297b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_packets); 10307b7c0719STom Parkin atomic_long_add(len, &tunnel->stats.tx_bytes); 10317b7c0719STom Parkin atomic_long_inc(&session->stats.tx_packets); 10327b7c0719STom Parkin atomic_long_add(len, &session->stats.tx_bytes); 1033fd558d18SJames Chapman } else { 10347b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_errors); 10357b7c0719STom Parkin atomic_long_inc(&session->stats.tx_errors); 1036fd558d18SJames Chapman } 1037fd558d18SJames Chapman } 1038fd558d18SJames Chapman 1039fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1040fd558d18SJames Chapman * inserted in the skb data before calling this function. 1041fd558d18SJames Chapman */ 1042fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1043fd558d18SJames Chapman { 1044fd558d18SJames Chapman int data_len = skb->len; 10450d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 10460d76751fSJames Chapman struct sock *sk = tunnel->sock; 1047d9d8da80SDavid S. Miller struct flowi *fl; 1048fd558d18SJames Chapman struct udphdr *uh; 1049fd558d18SJames Chapman struct inet_sock *inet; 1050fd558d18SJames Chapman int headroom; 10510d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10520d76751fSJames Chapman int udp_len; 1053b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1054fd558d18SJames Chapman 1055fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1056fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1057fd558d18SJames Chapman * make room. Adjust truesize. 1058fd558d18SJames Chapman */ 1059fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 10600d76751fSJames Chapman uhlen + hdr_len; 1061835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1062b8c84307SEric Dumazet kfree_skb(skb); 1063b8c84307SEric Dumazet return NET_XMIT_DROP; 1064835acf5dSEric Dumazet } 1065fd558d18SJames Chapman 1066fd558d18SJames Chapman /* Setup L2TP header */ 10672dedab6fSTom Parkin if (tunnel->version == L2TP_HDR_VER_2) 10682dedab6fSTom Parkin l2tp_build_l2tpv2_header(session, __skb_push(skb, hdr_len)); 10692dedab6fSTom Parkin else 10702dedab6fSTom Parkin l2tp_build_l2tpv3_header(session, __skb_push(skb, hdr_len)); 1071fd558d18SJames Chapman 10720d76751fSJames Chapman /* Reset skb netfilter state */ 1073fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1074fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1075fd558d18SJames Chapman IPSKB_REROUTED); 1076895b5c9fSFlorian Westphal nf_reset_ct(skb); 1077fd558d18SJames Chapman 10786af88da1SDavid S. Miller bh_lock_sock(sk); 10796af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1080b8c84307SEric Dumazet kfree_skb(skb); 1081b8c84307SEric Dumazet ret = NET_XMIT_DROP; 10826af88da1SDavid S. Miller goto out_unlock; 10836af88da1SDavid S. Miller } 10846af88da1SDavid S. Miller 1085b954f940SPaolo Abeni /* The user-space may change the connection status for the user-space 1086b954f940SPaolo Abeni * provided socket at run time: we must check it under the socket lock 1087b954f940SPaolo Abeni */ 1088b954f940SPaolo Abeni if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) { 1089b954f940SPaolo Abeni kfree_skb(skb); 1090b954f940SPaolo Abeni ret = NET_XMIT_DROP; 1091b954f940SPaolo Abeni goto out_unlock; 1092b954f940SPaolo Abeni } 1093b954f940SPaolo Abeni 1094d9d8da80SDavid S. Miller inet = inet_sk(sk); 1095d9d8da80SDavid S. Miller fl = &inet->cork.fl; 10960d76751fSJames Chapman switch (tunnel->encap) { 10970d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 10980d76751fSJames Chapman /* Setup UDP header */ 10990d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11000d76751fSJames Chapman skb_reset_transport_header(skb); 11010d76751fSJames Chapman uh = udp_hdr(skb); 11020d76751fSJames Chapman uh->source = inet->inet_sport; 11030d76751fSJames Chapman uh->dest = inet->inet_dport; 11040d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11050d76751fSJames Chapman uh->len = htons(udp_len); 1106fd558d18SJames Chapman 1107fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1108d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1109b954f940SPaolo Abeni if (l2tp_sk_is_v6(sk)) 111077157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 111177157e19STom Herbert skb, &inet6_sk(sk)->saddr, 111277157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1113d2cf3361SBenjamin LaHaise else 1114d2cf3361SBenjamin LaHaise #endif 111577157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 111677157e19STom Herbert inet->inet_daddr, udp_len); 11170d76751fSJames Chapman break; 11180d76751fSJames Chapman 11190d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11200d76751fSJames Chapman break; 11210d76751fSJames Chapman } 11220d76751fSJames Chapman 1123d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 11246af88da1SDavid S. Miller out_unlock: 11256af88da1SDavid S. Miller bh_unlock_sock(sk); 1126fd558d18SJames Chapman 1127b8c84307SEric Dumazet return ret; 1128fd558d18SJames Chapman } 1129fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1130fd558d18SJames Chapman 1131fd558d18SJames Chapman /***************************************************************************** 1132fd558d18SJames Chapman * Tinnel and session create/destroy. 1133fd558d18SJames Chapman *****************************************************************************/ 1134fd558d18SJames Chapman 1135fd558d18SJames Chapman /* Tunnel socket destruct hook. 1136fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1137fd558d18SJames Chapman * closed. 1138fd558d18SJames Chapman */ 1139fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1140fd558d18SJames Chapman { 11418d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1142fd558d18SJames Chapman 11430febc7b3STom Parkin if (!tunnel) 1144fd558d18SJames Chapman goto end; 1145fd558d18SJames Chapman 1146f8ccac0eSTom Parkin /* Disable udp encapsulation */ 11470d76751fSJames Chapman switch (tunnel->encap) { 11480d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1149fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1150fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1151fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 11529980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 11530d76751fSJames Chapman break; 11540d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11550d76751fSJames Chapman break; 11560d76751fSJames Chapman } 1157fd558d18SJames Chapman 1158fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1159fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1160fd558d18SJames Chapman sk->sk_user_data = NULL; 1161f8ccac0eSTom Parkin 1162fd558d18SJames Chapman /* Call the original destructor */ 1163fd558d18SJames Chapman if (sk->sk_destruct) 1164fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1165d00fa9adSJames Chapman 1166d00fa9adSJames Chapman kfree_rcu(tunnel, rcu); 1167fd558d18SJames Chapman end: 1168fd558d18SJames Chapman return; 1169fd558d18SJames Chapman } 1170fd558d18SJames Chapman 1171b2aecfe8STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. */ 1172b2aecfe8STom Parkin static void l2tp_session_unhash(struct l2tp_session *session) 1173b2aecfe8STom Parkin { 1174b2aecfe8STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1175b2aecfe8STom Parkin 1176b2aecfe8STom Parkin /* Remove the session from core hashes */ 1177b2aecfe8STom Parkin if (tunnel) { 1178b2aecfe8STom Parkin /* Remove from the per-tunnel hash */ 1179b2aecfe8STom Parkin write_lock_bh(&tunnel->hlist_lock); 1180b2aecfe8STom Parkin hlist_del_init(&session->hlist); 1181b2aecfe8STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1182b2aecfe8STom Parkin 1183b2aecfe8STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1184b2aecfe8STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1185b2aecfe8STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1186b2aecfe8STom Parkin 1187b2aecfe8STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1188b2aecfe8STom Parkin hlist_del_init_rcu(&session->global_hlist); 1189b2aecfe8STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1190b2aecfe8STom Parkin synchronize_rcu(); 1191b2aecfe8STom Parkin } 1192b2aecfe8STom Parkin } 1193b2aecfe8STom Parkin } 1194b2aecfe8STom Parkin 1195fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1196fd558d18SJames Chapman */ 1197d08532bbSGuillaume Nault static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1198fd558d18SJames Chapman { 1199fd558d18SJames Chapman int hash; 1200fd558d18SJames Chapman struct hlist_node *walk; 1201fd558d18SJames Chapman struct hlist_node *tmp; 1202fd558d18SJames Chapman struct l2tp_session *session; 1203fd558d18SJames Chapman 1204fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1205f3c66d4eSGuillaume Nault tunnel->acpt_newsess = false; 1206fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1207fd558d18SJames Chapman again: 1208fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1209fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1210fd558d18SJames Chapman 1211fd558d18SJames Chapman hlist_del_init(&session->hlist); 1212fd558d18SJames Chapman 1213b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1214b228a940SGuillaume Nault goto again; 1215b228a940SGuillaume Nault 1216fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1217fd558d18SJames Chapman 1218b2aecfe8STom Parkin l2tp_session_unhash(session); 12194c6e2fd3STom Parkin l2tp_session_queue_purge(session); 12204c6e2fd3STom Parkin 12210febc7b3STom Parkin if (session->session_close) 1222fd558d18SJames Chapman (*session->session_close)(session); 1223fd558d18SJames Chapman 12249980d001STom Parkin l2tp_session_dec_refcount(session); 12259980d001STom Parkin 1226fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1227fd558d18SJames Chapman 1228fd558d18SJames Chapman /* Now restart from the beginning of this hash 1229fd558d18SJames Chapman * chain. We always remove a session from the 1230fd558d18SJames Chapman * list so we are guaranteed to make forward 1231fd558d18SJames Chapman * progress. 1232fd558d18SJames Chapman */ 1233fd558d18SJames Chapman goto again; 1234fd558d18SJames Chapman } 1235fd558d18SJames Chapman } 1236fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1237fd558d18SJames Chapman } 1238fd558d18SJames Chapman 12399980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 12409980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 12419980d001STom Parkin { 1242d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1243d00fa9adSJames Chapman 1244d00fa9adSJames Chapman if (tunnel) 1245d00fa9adSJames Chapman l2tp_tunnel_delete(tunnel); 12469980d001STom Parkin } 12479980d001STom Parkin 1248f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1249f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1250f8ccac0eSTom Parkin { 1251d00fa9adSJames Chapman struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel, 1252d00fa9adSJames Chapman del_work); 1253d00fa9adSJames Chapman struct sock *sk = tunnel->sock; 1254d00fa9adSJames Chapman struct socket *sock = sk->sk_socket; 125528f5bfb8SJames Chapman struct l2tp_net *pn; 125612d656afSRidge Kennedy 125712d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 125812d656afSRidge Kennedy 125976a6abdbSJames Chapman /* If the tunnel socket was created within the kernel, use 126002d13ed5STom Parkin * the sk API to release it here. 1261f8ccac0eSTom Parkin */ 126276a6abdbSJames Chapman if (tunnel->fd < 0) { 126326abe143SEric W. Biederman if (sock) { 1264167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 126526abe143SEric W. Biederman sock_release(sock); 126626abe143SEric W. Biederman } 1267167eb17eSTom Parkin } 1268f8ccac0eSTom Parkin 126928f5bfb8SJames Chapman /* Remove the tunnel struct from the tunnel list */ 127028f5bfb8SJames Chapman pn = l2tp_pernet(tunnel->l2tp_net); 127128f5bfb8SJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 127228f5bfb8SJames Chapman list_del_rcu(&tunnel->list); 127328f5bfb8SJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 127428f5bfb8SJames Chapman 1275d00fa9adSJames Chapman /* drop initial ref */ 1276d00fa9adSJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1277d00fa9adSJames Chapman 1278d00fa9adSJames Chapman /* drop workqueue ref */ 127906a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1280fd558d18SJames Chapman } 1281fd558d18SJames Chapman 1282789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1283789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1284789a4a2cSJames Chapman * managing L2TP daemon. 1285167eb17eSTom Parkin * 1286167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1287167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1288167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1289167eb17eSTom Parkin * exit hook. 1290789a4a2cSJames Chapman */ 1291167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1292167eb17eSTom Parkin u32 tunnel_id, 1293167eb17eSTom Parkin u32 peer_tunnel_id, 1294167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1295167eb17eSTom Parkin struct socket **sockp) 1296789a4a2cSJames Chapman { 1297789a4a2cSJames Chapman int err = -EINVAL; 12987bddd0dbSEric Dumazet struct socket *sock = NULL; 129985644b4dSTom Herbert struct udp_port_cfg udp_conf; 1300789a4a2cSJames Chapman 1301789a4a2cSJames Chapman switch (cfg->encap) { 1302789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 130385644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 130485644b4dSTom Herbert 1305f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1306f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 130785644b4dSTom Herbert udp_conf.family = AF_INET6; 130885644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 130985644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 131085644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 131185644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 131285644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1313018f8258SWang Shanker !cfg->udp6_zero_tx_checksums; 131485644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1315018f8258SWang Shanker !cfg->udp6_zero_rx_checksums; 1316f9bac8dfSChris Elston } else 1317f9bac8dfSChris Elston #endif 1318f9bac8dfSChris Elston { 131985644b4dSTom Herbert udp_conf.family = AF_INET; 132085644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 132185644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 132285644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1323f9bac8dfSChris Elston } 1324789a4a2cSJames Chapman 132585644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 132685644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 132785644b4dSTom Herbert 132885644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 132985644b4dSTom Herbert if (err < 0) 133085644b4dSTom Herbert goto out; 1331789a4a2cSJames Chapman 1332789a4a2cSJames Chapman break; 1333789a4a2cSJames Chapman 1334789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1335f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1336f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 133785644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 133885644b4dSTom Herbert 133926abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1340167eb17eSTom Parkin IPPROTO_L2TP, &sock); 13415dac94e1SJames Chapman if (err < 0) 1342f9bac8dfSChris Elston goto out; 13435dac94e1SJames Chapman 13445dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 13455dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 13465dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 13475dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 13485dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip6_addr, 13495dac94e1SJames Chapman sizeof(ip6_addr)); 13505dac94e1SJames Chapman if (err < 0) 13515dac94e1SJames Chapman goto out; 13525dac94e1SJames Chapman 13535dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 13545dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 13555dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 13565dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 13575dac94e1SJames Chapman err = kernel_connect(sock, 13585dac94e1SJames Chapman (struct sockaddr *)&ip6_addr, 13595dac94e1SJames Chapman sizeof(ip6_addr), 0); 13605dac94e1SJames Chapman if (err < 0) 13615dac94e1SJames Chapman goto out; 13625dac94e1SJames Chapman } else 1363f9bac8dfSChris Elston #endif 13645dac94e1SJames Chapman { 136585644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 136685644b4dSTom Herbert 136726abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1368167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1369789a4a2cSJames Chapman if (err < 0) 1370789a4a2cSJames Chapman goto out; 1371789a4a2cSJames Chapman 1372789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1373789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1374789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 13755dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *)&ip_addr, 13765dac94e1SJames Chapman sizeof(ip_addr)); 1377789a4a2cSJames Chapman if (err < 0) 1378789a4a2cSJames Chapman goto out; 1379789a4a2cSJames Chapman 1380789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1381789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1382789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 13835dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *)&ip_addr, 13845dac94e1SJames Chapman sizeof(ip_addr), 0); 1385789a4a2cSJames Chapman if (err < 0) 1386789a4a2cSJames Chapman goto out; 13875dac94e1SJames Chapman } 1388789a4a2cSJames Chapman break; 1389789a4a2cSJames Chapman 1390789a4a2cSJames Chapman default: 1391789a4a2cSJames Chapman goto out; 1392789a4a2cSJames Chapman } 1393789a4a2cSJames Chapman 1394789a4a2cSJames Chapman out: 1395167eb17eSTom Parkin *sockp = sock; 13966c0ec37bSTom Parkin if (err < 0 && sock) { 1397167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 139826abe143SEric W. Biederman sock_release(sock); 1399789a4a2cSJames Chapman *sockp = NULL; 1400789a4a2cSJames Chapman } 1401789a4a2cSJames Chapman 1402789a4a2cSJames Chapman return err; 1403789a4a2cSJames Chapman } 1404789a4a2cSJames Chapman 140537159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 140637159ef2SEric Dumazet 1407c0235fb3STom Parkin int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, 1408c0235fb3STom Parkin struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1409fd558d18SJames Chapman { 1410fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1411fd558d18SJames Chapman int err; 14120d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1413fd558d18SJames Chapman 14140febc7b3STom Parkin if (cfg) 14150d76751fSJames Chapman encap = cfg->encap; 14160d76751fSJames Chapman 141770c05bfaSTom Parkin tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); 14180febc7b3STom Parkin if (!tunnel) { 1419fd558d18SJames Chapman err = -ENOMEM; 1420fd558d18SJames Chapman goto err; 1421fd558d18SJames Chapman } 1422fd558d18SJames Chapman 1423fd558d18SJames Chapman tunnel->version = version; 1424fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1425fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1426fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1427fd558d18SJames Chapman 1428fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1429fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1430fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1431f3c66d4eSGuillaume Nault tunnel->acpt_newsess = true; 1432fd558d18SJames Chapman 14330febc7b3STom Parkin if (cfg) 1434fd558d18SJames Chapman tunnel->debug = cfg->debug; 1435fd558d18SJames Chapman 14360d76751fSJames Chapman tunnel->encap = encap; 1437fd558d18SJames Chapman 1438d00fa9adSJames Chapman refcount_set(&tunnel->ref_count, 1); 1439d00fa9adSJames Chapman tunnel->fd = fd; 1440d00fa9adSJames Chapman 1441f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1442f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1443f8ccac0eSTom Parkin 1444fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1445fd558d18SJames Chapman 1446fd558d18SJames Chapman err = 0; 1447fd558d18SJames Chapman err: 1448fd558d18SJames Chapman if (tunnelp) 1449fd558d18SJames Chapman *tunnelp = tunnel; 1450fd558d18SJames Chapman 1451fd558d18SJames Chapman return err; 1452fd558d18SJames Chapman } 1453fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1454fd558d18SJames Chapman 14556b9f3423SGuillaume Nault static int l2tp_validate_socket(const struct sock *sk, const struct net *net, 14566b9f3423SGuillaume Nault enum l2tp_encap_type encap) 14576b9f3423SGuillaume Nault { 14586b9f3423SGuillaume Nault if (!net_eq(sock_net(sk), net)) 14596b9f3423SGuillaume Nault return -EINVAL; 14606b9f3423SGuillaume Nault 14616b9f3423SGuillaume Nault if (sk->sk_type != SOCK_DGRAM) 14626b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 14636b9f3423SGuillaume Nault 1464d9a81a22SEric Dumazet if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) 1465d9a81a22SEric Dumazet return -EPROTONOSUPPORT; 1466d9a81a22SEric Dumazet 14676b9f3423SGuillaume Nault if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) || 14686b9f3423SGuillaume Nault (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) 14696b9f3423SGuillaume Nault return -EPROTONOSUPPORT; 14706b9f3423SGuillaume Nault 14716b9f3423SGuillaume Nault if (sk->sk_user_data) 14726b9f3423SGuillaume Nault return -EBUSY; 14736b9f3423SGuillaume Nault 14746b9f3423SGuillaume Nault return 0; 14756b9f3423SGuillaume Nault } 14766b9f3423SGuillaume Nault 14776b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, 14786b9f3423SGuillaume Nault struct l2tp_tunnel_cfg *cfg) 14796b9f3423SGuillaume Nault { 1480f6cd651bSGuillaume Nault struct l2tp_tunnel *tunnel_walk; 14816b9f3423SGuillaume Nault struct l2tp_net *pn; 14826b9f3423SGuillaume Nault struct socket *sock; 14836b9f3423SGuillaume Nault struct sock *sk; 14846b9f3423SGuillaume Nault int ret; 14856b9f3423SGuillaume Nault 14866b9f3423SGuillaume Nault if (tunnel->fd < 0) { 14876b9f3423SGuillaume Nault ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id, 14886b9f3423SGuillaume Nault tunnel->peer_tunnel_id, cfg, 14896b9f3423SGuillaume Nault &sock); 14906b9f3423SGuillaume Nault if (ret < 0) 14916b9f3423SGuillaume Nault goto err; 14926b9f3423SGuillaume Nault } else { 14936b9f3423SGuillaume Nault sock = sockfd_lookup(tunnel->fd, &ret); 14946b9f3423SGuillaume Nault if (!sock) 14956b9f3423SGuillaume Nault goto err; 14966b9f3423SGuillaume Nault 14976b9f3423SGuillaume Nault ret = l2tp_validate_socket(sock->sk, net, tunnel->encap); 14986b9f3423SGuillaume Nault if (ret < 0) 14996b9f3423SGuillaume Nault goto err_sock; 15006b9f3423SGuillaume Nault } 15016b9f3423SGuillaume Nault 15026b9f3423SGuillaume Nault tunnel->l2tp_net = net; 15036b9f3423SGuillaume Nault pn = l2tp_pernet(net); 1504f6cd651bSGuillaume Nault 15056b9f3423SGuillaume Nault spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1506f6cd651bSGuillaume Nault list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) { 1507f6cd651bSGuillaume Nault if (tunnel_walk->tunnel_id == tunnel->tunnel_id) { 1508f6cd651bSGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1509f6cd651bSGuillaume Nault 1510f6cd651bSGuillaume Nault ret = -EEXIST; 1511f6cd651bSGuillaume Nault goto err_sock; 1512f6cd651bSGuillaume Nault } 1513f6cd651bSGuillaume Nault } 15146b9f3423SGuillaume Nault list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 15156b9f3423SGuillaume Nault spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 15166b9f3423SGuillaume Nault 1517f8504f4cSXin Long sk = sock->sk; 1518f8504f4cSXin Long sock_hold(sk); 1519f8504f4cSXin Long tunnel->sock = sk; 1520f8504f4cSXin Long 15216b9f3423SGuillaume Nault if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 15226b9f3423SGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { 15236b9f3423SGuillaume Nault .sk_user_data = tunnel, 15246b9f3423SGuillaume Nault .encap_type = UDP_ENCAP_L2TPINUDP, 15256b9f3423SGuillaume Nault .encap_rcv = l2tp_udp_encap_recv, 15266b9f3423SGuillaume Nault .encap_destroy = l2tp_udp_encap_destroy, 15276b9f3423SGuillaume Nault }; 15286b9f3423SGuillaume Nault 15296b9f3423SGuillaume Nault setup_udp_tunnel_sock(net, sock, &udp_cfg); 15306b9f3423SGuillaume Nault } else { 15316b9f3423SGuillaume Nault sk->sk_user_data = tunnel; 15326b9f3423SGuillaume Nault } 15336b9f3423SGuillaume Nault 15346b9f3423SGuillaume Nault tunnel->old_sk_destruct = sk->sk_destruct; 15356b9f3423SGuillaume Nault sk->sk_destruct = &l2tp_tunnel_destruct; 15366b9f3423SGuillaume Nault lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, 15376b9f3423SGuillaume Nault "l2tp_sock"); 15386b9f3423SGuillaume Nault sk->sk_allocation = GFP_ATOMIC; 15396b9f3423SGuillaume Nault 15406b9f3423SGuillaume Nault if (tunnel->fd >= 0) 15416b9f3423SGuillaume Nault sockfd_put(sock); 15426b9f3423SGuillaume Nault 15436b9f3423SGuillaume Nault return 0; 15446b9f3423SGuillaume Nault 15456b9f3423SGuillaume Nault err_sock: 1546f6cd651bSGuillaume Nault if (tunnel->fd < 0) 1547f6cd651bSGuillaume Nault sock_release(sock); 1548f6cd651bSGuillaume Nault else 15496b9f3423SGuillaume Nault sockfd_put(sock); 15506b9f3423SGuillaume Nault err: 15516b9f3423SGuillaume Nault return ret; 15526b9f3423SGuillaume Nault } 15536b9f3423SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_register); 15546b9f3423SGuillaume Nault 1555309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1556309795f4SJames Chapman */ 155762b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1558309795f4SJames Chapman { 155962b982eeSSabrina Dubroca if (!test_and_set_bit(0, &tunnel->dead)) { 156006a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 156162b982eeSSabrina Dubroca queue_work(l2tp_wq, &tunnel->del_work); 156206a15f51SAlexander Couzens } 1563309795f4SJames Chapman } 1564309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1565309795f4SJames Chapman 1566628703f5STom Parkin void l2tp_session_delete(struct l2tp_session *session) 1567309795f4SJames Chapman { 1568b228a940SGuillaume Nault if (test_and_set_bit(0, &session->dead)) 1569628703f5STom Parkin return; 1570b228a940SGuillaume Nault 1571b2aecfe8STom Parkin l2tp_session_unhash(session); 15724c6e2fd3STom Parkin l2tp_session_queue_purge(session); 15730febc7b3STom Parkin if (session->session_close) 1574309795f4SJames Chapman (*session->session_close)(session); 1575a4346210SGuillaume Nault 1576309795f4SJames Chapman l2tp_session_dec_refcount(session); 1577309795f4SJames Chapman } 1578309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1579309795f4SJames Chapman 1580f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 158162e7b6a5SLorenzo Bianconi * l2specific_type parameters are set. 1582f7faffa3SJames Chapman */ 1583bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1584f7faffa3SJames Chapman { 1585f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1586f7faffa3SJames Chapman session->hdr_len = 6; 1587f7faffa3SJames Chapman if (session->send_seq) 1588f7faffa3SJames Chapman session->hdr_len += 4; 1589f7faffa3SJames Chapman } else { 159062e7b6a5SLorenzo Bianconi session->hdr_len = 4 + session->cookie_len; 159162e7b6a5SLorenzo Bianconi session->hdr_len += l2tp_get_l2specific_len(session); 15920d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 15930d76751fSJames Chapman session->hdr_len += 4; 1594f7faffa3SJames Chapman } 1595f7faffa3SJames Chapman } 1596bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1597f7faffa3SJames Chapman 1598c0235fb3STom Parkin struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, 1599c0235fb3STom Parkin u32 peer_session_id, struct l2tp_session_cfg *cfg) 1600fd558d18SJames Chapman { 1601fd558d18SJames Chapman struct l2tp_session *session; 1602fd558d18SJames Chapman 160370c05bfaSTom Parkin session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL); 16040febc7b3STom Parkin if (session) { 1605fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1606fd558d18SJames Chapman session->tunnel = tunnel; 1607fd558d18SJames Chapman 1608fd558d18SJames Chapman session->session_id = session_id; 1609fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1610d301e325SJames Chapman session->nr = 0; 16118a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 16128a1631d5SJames Chapman session->nr_max = 0xffff; 16138a1631d5SJames Chapman else 16148a1631d5SJames Chapman session->nr_max = 0xffffff; 16158a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1616a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1617a0dbd822SJames Chapman 1618a0dbd822SJames Chapman /* Use NR of first received packet */ 1619a0dbd822SJames Chapman session->reorder_skip = 1; 1620fd558d18SJames Chapman 1621fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1622fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1623fd558d18SJames Chapman 1624fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1625fd558d18SJames Chapman 1626fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1627f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1628fd558d18SJames Chapman 1629fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1630fd558d18SJames Chapman session->debug = tunnel->debug; 1631fd558d18SJames Chapman 1632fd558d18SJames Chapman if (cfg) { 1633f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1634fd558d18SJames Chapman session->debug = cfg->debug; 1635fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1636fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1637fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1638f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1639f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1640f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1641f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1642f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1643f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1644fd558d18SJames Chapman } 1645fd558d18SJames Chapman 1646f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1647f7faffa3SJames Chapman 16489ee369a4SGuillaume Nault refcount_set(&session->ref_count, 1); 16499ee369a4SGuillaume Nault 1650fd558d18SJames Chapman return session; 1651fd558d18SJames Chapman } 1652dbdbc73bSGuillaume Nault 1653dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM); 1654dbdbc73bSGuillaume Nault } 1655fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1656fd558d18SJames Chapman 1657fd558d18SJames Chapman /***************************************************************************** 1658fd558d18SJames Chapman * Init and cleanup 1659fd558d18SJames Chapman *****************************************************************************/ 1660fd558d18SJames Chapman 1661fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1662fd558d18SJames Chapman { 1663e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1664f7faffa3SJames Chapman int hash; 1665fd558d18SJames Chapman 1666fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1667e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1668fd558d18SJames Chapman 1669f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1670f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1671f7faffa3SJames Chapman 1672e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1673f7faffa3SJames Chapman 1674fd558d18SJames Chapman return 0; 1675fd558d18SJames Chapman } 1676fd558d18SJames Chapman 1677167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1678167eb17eSTom Parkin { 1679167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1680167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 16811e7af3b2SVasily Averin int hash; 1682167eb17eSTom Parkin 1683167eb17eSTom Parkin rcu_read_lock_bh(); 1684167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 16854dc12ffeSJiri Slaby l2tp_tunnel_delete(tunnel); 1686167eb17eSTom Parkin } 1687167eb17eSTom Parkin rcu_read_unlock_bh(); 16882f86953eSSabrina Dubroca 1689638a3a1eSYueHaibing if (l2tp_wq) 16902f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 16912f86953eSSabrina Dubroca rcu_barrier(); 16921e7af3b2SVasily Averin 16931e7af3b2SVasily Averin for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 16941e7af3b2SVasily Averin WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash])); 1695167eb17eSTom Parkin } 1696167eb17eSTom Parkin 1697fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1698fd558d18SJames Chapman .init = l2tp_init_net, 1699167eb17eSTom Parkin .exit = l2tp_exit_net, 1700fd558d18SJames Chapman .id = &l2tp_net_id, 1701fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1702fd558d18SJames Chapman }; 1703fd558d18SJames Chapman 1704fd558d18SJames Chapman static int __init l2tp_init(void) 1705fd558d18SJames Chapman { 1706fd558d18SJames Chapman int rc = 0; 1707fd558d18SJames Chapman 1708fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1709fd558d18SJames Chapman if (rc) 1710fd558d18SJames Chapman goto out; 1711fd558d18SJames Chapman 171259ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1713f8ccac0eSTom Parkin if (!l2tp_wq) { 1714f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 171567e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1716f8ccac0eSTom Parkin rc = -ENOMEM; 1717f8ccac0eSTom Parkin goto out; 1718f8ccac0eSTom Parkin } 1719f8ccac0eSTom Parkin 1720a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1721fd558d18SJames Chapman 1722fd558d18SJames Chapman out: 1723fd558d18SJames Chapman return rc; 1724fd558d18SJames Chapman } 1725fd558d18SJames Chapman 1726fd558d18SJames Chapman static void __exit l2tp_exit(void) 1727fd558d18SJames Chapman { 1728fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1729f8ccac0eSTom Parkin if (l2tp_wq) { 1730f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1731f8ccac0eSTom Parkin l2tp_wq = NULL; 1732f8ccac0eSTom Parkin } 1733fd558d18SJames Chapman } 1734fd558d18SJames Chapman 1735fd558d18SJames Chapman module_init(l2tp_init); 1736fd558d18SJames Chapman module_exit(l2tp_exit); 1737fd558d18SJames Chapman 1738fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1739fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1740fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1741fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1742