1fd558d18SJames Chapman /* 2fd558d18SJames Chapman * 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 * This program is free software; you can redistribute it and/or modify 17fd558d18SJames Chapman * it under the terms of the GNU General Public License version 2 as 18fd558d18SJames Chapman * published by the Free Software Foundation. 19fd558d18SJames Chapman */ 20fd558d18SJames Chapman 21a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22a4ca44faSJoe Perches 23fd558d18SJames Chapman #include <linux/module.h> 24fd558d18SJames Chapman #include <linux/string.h> 25fd558d18SJames Chapman #include <linux/list.h> 26e02d494dSJames Chapman #include <linux/rculist.h> 27fd558d18SJames Chapman #include <linux/uaccess.h> 28fd558d18SJames Chapman 29fd558d18SJames Chapman #include <linux/kernel.h> 30fd558d18SJames Chapman #include <linux/spinlock.h> 31fd558d18SJames Chapman #include <linux/kthread.h> 32fd558d18SJames Chapman #include <linux/sched.h> 33fd558d18SJames Chapman #include <linux/slab.h> 34fd558d18SJames Chapman #include <linux/errno.h> 35fd558d18SJames Chapman #include <linux/jiffies.h> 36fd558d18SJames Chapman 37fd558d18SJames Chapman #include <linux/netdevice.h> 38fd558d18SJames Chapman #include <linux/net.h> 39fd558d18SJames Chapman #include <linux/inetdevice.h> 40fd558d18SJames Chapman #include <linux/skbuff.h> 41fd558d18SJames Chapman #include <linux/init.h> 420d76751fSJames Chapman #include <linux/in.h> 43fd558d18SJames Chapman #include <linux/ip.h> 44fd558d18SJames Chapman #include <linux/udp.h> 450d76751fSJames Chapman #include <linux/l2tp.h> 46fd558d18SJames Chapman #include <linux/hash.h> 47fd558d18SJames Chapman #include <linux/sort.h> 48fd558d18SJames Chapman #include <linux/file.h> 49fd558d18SJames Chapman #include <linux/nsproxy.h> 50fd558d18SJames Chapman #include <net/net_namespace.h> 51fd558d18SJames Chapman #include <net/netns/generic.h> 52fd558d18SJames Chapman #include <net/dst.h> 53fd558d18SJames Chapman #include <net/ip.h> 54fd558d18SJames Chapman #include <net/udp.h> 5585644b4dSTom Herbert #include <net/udp_tunnel.h> 56309795f4SJames Chapman #include <net/inet_common.h> 57fd558d18SJames Chapman #include <net/xfrm.h> 580d76751fSJames Chapman #include <net/protocol.h> 59d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h> 60d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h> 61d2cf3361SBenjamin LaHaise #include <net/ip6_route.h> 62d499bd2eSDavid S. Miller #include <net/ip6_checksum.h> 63fd558d18SJames Chapman 64fd558d18SJames Chapman #include <asm/byteorder.h> 6560063497SArun Sharma #include <linux/atomic.h> 66fd558d18SJames Chapman 67fd558d18SJames Chapman #include "l2tp_core.h" 68fd558d18SJames Chapman 69fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 70fd558d18SJames Chapman 71fd558d18SJames Chapman /* L2TP header constants */ 72fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 73fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 74fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 75fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 76fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 77fd558d18SJames Chapman 78fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 79fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 80f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 81fd558d18SJames Chapman 82fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 83fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 84fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 85fd558d18SJames Chapman 86fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ 10 87fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ 6 88fd558d18SJames Chapman 89fd558d18SJames Chapman /* Default trace flags */ 90fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 91fd558d18SJames Chapman 92fd558d18SJames Chapman /* Private data stored for received packets in the skb. 93fd558d18SJames Chapman */ 94fd558d18SJames Chapman struct l2tp_skb_cb { 95f7faffa3SJames Chapman u32 ns; 96fd558d18SJames Chapman u16 has_seq; 97fd558d18SJames Chapman u16 length; 98fd558d18SJames Chapman unsigned long expires; 99fd558d18SJames Chapman }; 100fd558d18SJames Chapman 101fd558d18SJames Chapman #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 102fd558d18SJames Chapman 103fd558d18SJames Chapman static atomic_t l2tp_tunnel_count; 104fd558d18SJames Chapman static atomic_t l2tp_session_count; 105f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq; 106fd558d18SJames Chapman 107fd558d18SJames Chapman /* per-net private data for this module */ 108fd558d18SJames Chapman static unsigned int l2tp_net_id; 109fd558d18SJames Chapman struct l2tp_net { 110fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 111e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 112f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 113e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 114fd558d18SJames Chapman }; 115fd558d18SJames Chapman 116fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); 117fc130840Sstephen hemminger 1188d8a51e2SDavid S. Miller static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk) 1198d8a51e2SDavid S. Miller { 1208d8a51e2SDavid S. Miller return sk->sk_user_data; 1218d8a51e2SDavid S. Miller } 1228d8a51e2SDavid S. Miller 1239aaef50cSGuillaume Nault static inline struct l2tp_net *l2tp_pernet(const struct net *net) 124fd558d18SJames Chapman { 125fd558d18SJames Chapman BUG_ON(!net); 126fd558d18SJames Chapman 127fd558d18SJames Chapman return net_generic(net, l2tp_net_id); 128fd558d18SJames Chapman } 129fd558d18SJames Chapman 130fc130840Sstephen hemminger /* Tunnel reference counts. Incremented per session that is added to 131fc130840Sstephen hemminger * the tunnel. 132fc130840Sstephen hemminger */ 133fc130840Sstephen hemminger static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel) 134fc130840Sstephen hemminger { 135fc130840Sstephen hemminger atomic_inc(&tunnel->ref_count); 136fc130840Sstephen hemminger } 137fc130840Sstephen hemminger 138fc130840Sstephen hemminger static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel) 139fc130840Sstephen hemminger { 140fc130840Sstephen hemminger if (atomic_dec_and_test(&tunnel->ref_count)) 141fc130840Sstephen hemminger l2tp_tunnel_free(tunnel); 142fc130840Sstephen hemminger } 143fc130840Sstephen hemminger #ifdef L2TP_REFCNT_DEBUG 144a4ca44faSJoe Perches #define l2tp_tunnel_inc_refcount(_t) \ 145a4ca44faSJoe Perches do { \ 146a4ca44faSJoe Perches pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \ 147a4ca44faSJoe Perches __func__, __LINE__, (_t)->name, \ 148a4ca44faSJoe Perches atomic_read(&_t->ref_count)); \ 149fc130840Sstephen hemminger l2tp_tunnel_inc_refcount_1(_t); \ 150fc130840Sstephen hemminger } while (0) 15129abe2fdSAndy Zhou #define l2tp_tunnel_dec_refcount(_t) \ 152a4ca44faSJoe Perches do { \ 153a4ca44faSJoe Perches pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \ 154a4ca44faSJoe Perches __func__, __LINE__, (_t)->name, \ 155a4ca44faSJoe Perches atomic_read(&_t->ref_count)); \ 156fc130840Sstephen hemminger l2tp_tunnel_dec_refcount_1(_t); \ 157fc130840Sstephen hemminger } while (0) 158fc130840Sstephen hemminger #else 159fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t) 160fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t) 161fc130840Sstephen hemminger #endif 162fc130840Sstephen hemminger 163f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 164f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 165f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 166f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 167f7faffa3SJames Chapman */ 168f7faffa3SJames Chapman static inline struct hlist_head * 169f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 170f7faffa3SJames Chapman { 171f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 172f7faffa3SJames Chapman 173f7faffa3SJames Chapman } 174f7faffa3SJames Chapman 17580d84ef3STom Parkin /* Lookup the tunnel socket, possibly involving the fs code if the socket is 17680d84ef3STom Parkin * owned by userspace. A struct sock returned from this function must be 17780d84ef3STom Parkin * released using l2tp_tunnel_sock_put once you're done with it. 17880d84ef3STom Parkin */ 179b5d2b285Sstephen hemminger static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel) 18080d84ef3STom Parkin { 18180d84ef3STom Parkin int err = 0; 18280d84ef3STom Parkin struct socket *sock = NULL; 18380d84ef3STom Parkin struct sock *sk = NULL; 18480d84ef3STom Parkin 18580d84ef3STom Parkin if (!tunnel) 18680d84ef3STom Parkin goto out; 18780d84ef3STom Parkin 18880d84ef3STom Parkin if (tunnel->fd >= 0) { 18980d84ef3STom Parkin /* Socket is owned by userspace, who might be in the process 19080d84ef3STom Parkin * of closing it. Look the socket up using the fd to ensure 19180d84ef3STom Parkin * consistency. 19280d84ef3STom Parkin */ 19380d84ef3STom Parkin sock = sockfd_lookup(tunnel->fd, &err); 19480d84ef3STom Parkin if (sock) 19580d84ef3STom Parkin sk = sock->sk; 19680d84ef3STom Parkin } else { 19780d84ef3STom Parkin /* Socket is owned by kernelspace */ 19880d84ef3STom Parkin sk = tunnel->sock; 1998abbbe8fSTom Parkin sock_hold(sk); 20080d84ef3STom Parkin } 20180d84ef3STom Parkin 20280d84ef3STom Parkin out: 20380d84ef3STom Parkin return sk; 20480d84ef3STom Parkin } 20580d84ef3STom Parkin 20680d84ef3STom Parkin /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */ 207b5d2b285Sstephen hemminger static void l2tp_tunnel_sock_put(struct sock *sk) 20880d84ef3STom Parkin { 20980d84ef3STom Parkin struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 21080d84ef3STom Parkin if (tunnel) { 21180d84ef3STom Parkin if (tunnel->fd >= 0) { 21280d84ef3STom Parkin /* Socket is owned by userspace */ 21380d84ef3STom Parkin sockfd_put(sk->sk_socket); 21480d84ef3STom Parkin } 21580d84ef3STom Parkin sock_put(sk); 21680d84ef3STom Parkin } 2178abbbe8fSTom Parkin sock_put(sk); 21880d84ef3STom Parkin } 21980d84ef3STom Parkin 220fd558d18SJames Chapman /* Session hash list. 221fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 222fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 223fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 224fd558d18SJames Chapman * simple bitmask. 225fd558d18SJames Chapman */ 226fd558d18SJames Chapman static inline struct hlist_head * 227fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 228fd558d18SJames Chapman { 229fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 230fd558d18SJames Chapman } 231fd558d18SJames Chapman 23255a3ce3bSGuillaume Nault /* Lookup a session. A new reference is held on the returned session. 23361b9a047SGuillaume Nault * Optionally calls session->ref() too if do_ref is true. 23461b9a047SGuillaume Nault */ 2359aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, 23661b9a047SGuillaume Nault struct l2tp_tunnel *tunnel, 23761b9a047SGuillaume Nault u32 session_id, bool do_ref) 23861b9a047SGuillaume Nault { 23961b9a047SGuillaume Nault struct hlist_head *session_list; 24061b9a047SGuillaume Nault struct l2tp_session *session; 24161b9a047SGuillaume Nault 24261b9a047SGuillaume Nault if (!tunnel) { 24361b9a047SGuillaume Nault struct l2tp_net *pn = l2tp_pernet(net); 24461b9a047SGuillaume Nault 24561b9a047SGuillaume Nault session_list = l2tp_session_id_hash_2(pn, session_id); 24661b9a047SGuillaume Nault 24761b9a047SGuillaume Nault rcu_read_lock_bh(); 24861b9a047SGuillaume Nault hlist_for_each_entry_rcu(session, session_list, global_hlist) { 24961b9a047SGuillaume Nault if (session->session_id == session_id) { 25061b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 25161b9a047SGuillaume Nault if (do_ref && session->ref) 25261b9a047SGuillaume Nault session->ref(session); 25361b9a047SGuillaume Nault rcu_read_unlock_bh(); 25461b9a047SGuillaume Nault 25561b9a047SGuillaume Nault return session; 25661b9a047SGuillaume Nault } 25761b9a047SGuillaume Nault } 25861b9a047SGuillaume Nault rcu_read_unlock_bh(); 25961b9a047SGuillaume Nault 26061b9a047SGuillaume Nault return NULL; 26161b9a047SGuillaume Nault } 26261b9a047SGuillaume Nault 26361b9a047SGuillaume Nault session_list = l2tp_session_id_hash(tunnel, session_id); 26461b9a047SGuillaume Nault read_lock_bh(&tunnel->hlist_lock); 26561b9a047SGuillaume Nault hlist_for_each_entry(session, session_list, hlist) { 26661b9a047SGuillaume Nault if (session->session_id == session_id) { 26761b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 26861b9a047SGuillaume Nault if (do_ref && session->ref) 26961b9a047SGuillaume Nault session->ref(session); 27061b9a047SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 27161b9a047SGuillaume Nault 27261b9a047SGuillaume Nault return session; 27361b9a047SGuillaume Nault } 27461b9a047SGuillaume Nault } 27561b9a047SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 27661b9a047SGuillaume Nault 27761b9a047SGuillaume Nault return NULL; 27861b9a047SGuillaume Nault } 27961b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get); 28061b9a047SGuillaume Nault 281e08293a4SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth, 282e08293a4SGuillaume Nault bool do_ref) 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); 293e08293a4SGuillaume Nault if (do_ref && session->ref) 294e08293a4SGuillaume Nault session->ref(session); 295fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 296fd558d18SJames Chapman return session; 297fd558d18SJames Chapman } 298fd558d18SJames Chapman } 299fd558d18SJames Chapman } 300fd558d18SJames Chapman 301fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 302fd558d18SJames Chapman 303fd558d18SJames Chapman return NULL; 304fd558d18SJames Chapman } 305e08293a4SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_nth); 306fd558d18SJames Chapman 307309795f4SJames Chapman /* Lookup a session by interface name. 308309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 309309795f4SJames Chapman */ 3109aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 3119aaef50cSGuillaume Nault const char *ifname, 3122777e2abSGuillaume Nault bool do_ref) 313309795f4SJames Chapman { 314309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 315309795f4SJames Chapman int hash; 316309795f4SJames Chapman struct l2tp_session *session; 317309795f4SJames Chapman 318e02d494dSJames Chapman rcu_read_lock_bh(); 319309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 320b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { 321309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 3222777e2abSGuillaume Nault l2tp_session_inc_refcount(session); 3232777e2abSGuillaume Nault if (do_ref && session->ref) 3242777e2abSGuillaume Nault session->ref(session); 325e02d494dSJames Chapman rcu_read_unlock_bh(); 3262777e2abSGuillaume Nault 327309795f4SJames Chapman return session; 328309795f4SJames Chapman } 329309795f4SJames Chapman } 330309795f4SJames Chapman } 331309795f4SJames Chapman 332e02d494dSJames Chapman rcu_read_unlock_bh(); 333309795f4SJames Chapman 334309795f4SJames Chapman return NULL; 335309795f4SJames Chapman } 3362777e2abSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); 337309795f4SJames Chapman 338dbdbc73bSGuillaume Nault static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel, 339dbdbc73bSGuillaume Nault struct l2tp_session *session) 340dbdbc73bSGuillaume Nault { 341dbdbc73bSGuillaume Nault struct l2tp_session *session_walk; 342dbdbc73bSGuillaume Nault struct hlist_head *g_head; 343dbdbc73bSGuillaume Nault struct hlist_head *head; 344dbdbc73bSGuillaume Nault struct l2tp_net *pn; 345dbdbc73bSGuillaume Nault 346dbdbc73bSGuillaume Nault head = l2tp_session_id_hash(tunnel, session->session_id); 347dbdbc73bSGuillaume Nault 348dbdbc73bSGuillaume Nault write_lock_bh(&tunnel->hlist_lock); 349dbdbc73bSGuillaume Nault hlist_for_each_entry(session_walk, head, hlist) 350dbdbc73bSGuillaume Nault if (session_walk->session_id == session->session_id) 351dbdbc73bSGuillaume Nault goto exist; 352dbdbc73bSGuillaume Nault 353dbdbc73bSGuillaume Nault if (tunnel->version == L2TP_HDR_VER_3) { 354dbdbc73bSGuillaume Nault pn = l2tp_pernet(tunnel->l2tp_net); 355dbdbc73bSGuillaume Nault g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net), 356dbdbc73bSGuillaume Nault session->session_id); 357dbdbc73bSGuillaume Nault 358dbdbc73bSGuillaume Nault spin_lock_bh(&pn->l2tp_session_hlist_lock); 359dbdbc73bSGuillaume Nault hlist_for_each_entry(session_walk, g_head, global_hlist) 360dbdbc73bSGuillaume Nault if (session_walk->session_id == session->session_id) 361dbdbc73bSGuillaume Nault goto exist_glob; 362dbdbc73bSGuillaume Nault 363dbdbc73bSGuillaume Nault hlist_add_head_rcu(&session->global_hlist, g_head); 364dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 365dbdbc73bSGuillaume Nault } 366dbdbc73bSGuillaume Nault 367dbdbc73bSGuillaume Nault hlist_add_head(&session->hlist, head); 368dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 369dbdbc73bSGuillaume Nault 370dbdbc73bSGuillaume Nault return 0; 371dbdbc73bSGuillaume Nault 372dbdbc73bSGuillaume Nault exist_glob: 373dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 374dbdbc73bSGuillaume Nault exist: 375dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 376dbdbc73bSGuillaume Nault 377dbdbc73bSGuillaume Nault return -EEXIST; 378dbdbc73bSGuillaume Nault } 379dbdbc73bSGuillaume Nault 380fd558d18SJames Chapman /* Lookup a tunnel by id 381fd558d18SJames Chapman */ 3822f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id) 383fd558d18SJames Chapman { 384fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 385fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 386fd558d18SJames Chapman 387e02d494dSJames Chapman rcu_read_lock_bh(); 388e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 389fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 390e02d494dSJames Chapman rcu_read_unlock_bh(); 391fd558d18SJames Chapman return tunnel; 392fd558d18SJames Chapman } 393fd558d18SJames Chapman } 394e02d494dSJames Chapman rcu_read_unlock_bh(); 395fd558d18SJames Chapman 396fd558d18SJames Chapman return NULL; 397fd558d18SJames Chapman } 398fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 399fd558d18SJames Chapman 4002f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth) 401fd558d18SJames Chapman { 402fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 403fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 404fd558d18SJames Chapman int count = 0; 405fd558d18SJames Chapman 406e02d494dSJames Chapman rcu_read_lock_bh(); 407e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 408fd558d18SJames Chapman if (++count > nth) { 409e02d494dSJames Chapman rcu_read_unlock_bh(); 410fd558d18SJames Chapman return tunnel; 411fd558d18SJames Chapman } 412fd558d18SJames Chapman } 413fd558d18SJames Chapman 414e02d494dSJames Chapman rcu_read_unlock_bh(); 415fd558d18SJames Chapman 416fd558d18SJames Chapman return NULL; 417fd558d18SJames Chapman } 418fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 419fd558d18SJames Chapman 420fd558d18SJames Chapman /***************************************************************************** 421fd558d18SJames Chapman * Receive data handling 422fd558d18SJames Chapman *****************************************************************************/ 423fd558d18SJames Chapman 424fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 425fd558d18SJames Chapman * number. 426fd558d18SJames Chapman */ 427fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 428fd558d18SJames Chapman { 429fd558d18SJames Chapman struct sk_buff *skbp; 430fd558d18SJames Chapman struct sk_buff *tmp; 431f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 432fd558d18SJames Chapman 433fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 434fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 435fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 436fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 437a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 438fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 439fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 440fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 4417b7c0719STom Parkin atomic_long_inc(&session->stats.rx_oos_packets); 442fd558d18SJames Chapman goto out; 443fd558d18SJames Chapman } 444fd558d18SJames Chapman } 445fd558d18SJames Chapman 446fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 447fd558d18SJames Chapman 448fd558d18SJames Chapman out: 449fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 450fd558d18SJames Chapman } 451fd558d18SJames Chapman 452fd558d18SJames Chapman /* Dequeue a single skb. 453fd558d18SJames Chapman */ 454fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 455fd558d18SJames Chapman { 456fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 457fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 458fd558d18SJames Chapman 459fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 460fd558d18SJames Chapman * to its current owner (a socket receive buffer). 461fd558d18SJames Chapman */ 462fd558d18SJames Chapman skb_orphan(skb); 463fd558d18SJames Chapman 4647b7c0719STom Parkin atomic_long_inc(&tunnel->stats.rx_packets); 4657b7c0719STom Parkin atomic_long_add(length, &tunnel->stats.rx_bytes); 4667b7c0719STom Parkin atomic_long_inc(&session->stats.rx_packets); 4677b7c0719STom Parkin atomic_long_add(length, &session->stats.rx_bytes); 468fd558d18SJames Chapman 469fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 470fd558d18SJames Chapman /* Bump our Nr */ 471fd558d18SJames Chapman session->nr++; 4728a1631d5SJames Chapman session->nr &= session->nr_max; 473f7faffa3SJames Chapman 474a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 475a4ca44faSJoe Perches session->name, session->nr); 476fd558d18SJames Chapman } 477fd558d18SJames Chapman 478fd558d18SJames Chapman /* call private receive handler */ 479fd558d18SJames Chapman if (session->recv_skb != NULL) 480fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 481fd558d18SJames Chapman else 482fd558d18SJames Chapman kfree_skb(skb); 483fd558d18SJames Chapman 484fd558d18SJames Chapman if (session->deref) 485fd558d18SJames Chapman (*session->deref)(session); 486fd558d18SJames Chapman } 487fd558d18SJames Chapman 488fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 489fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 490fd558d18SJames Chapman */ 491fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 492fd558d18SJames Chapman { 493fd558d18SJames Chapman struct sk_buff *skb; 494fd558d18SJames Chapman struct sk_buff *tmp; 495fd558d18SJames Chapman 496fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 497fd558d18SJames Chapman * expect to send up next, dequeue it and any other 498fd558d18SJames Chapman * in-sequence packets behind it. 499fd558d18SJames Chapman */ 500e2e210c0SEric Dumazet start: 501fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 502fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 503fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 5047b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 5057b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 506a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 507a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 508fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 509fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 510fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 51138d40b3fSJames Chapman session->reorder_skip = 1; 512fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 513fd558d18SJames Chapman kfree_skb(skb); 514fd558d18SJames Chapman if (session->deref) 515fd558d18SJames Chapman (*session->deref)(session); 516fd558d18SJames Chapman continue; 517fd558d18SJames Chapman } 518fd558d18SJames Chapman 519fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 52038d40b3fSJames Chapman if (session->reorder_skip) { 521a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 52238d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 52338d40b3fSJames Chapman session->name, session->nr, 52438d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 52538d40b3fSJames Chapman session->reorder_skip = 0; 52638d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 52738d40b3fSJames Chapman } 528fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 529a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 530a4ca44faSJoe Perches "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 531fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 532fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 533fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 534fd558d18SJames Chapman goto out; 535fd558d18SJames Chapman } 536fd558d18SJames Chapman } 537fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 538fd558d18SJames Chapman 539fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 540fd558d18SJames Chapman * do so to let other contexts process the queue. 541fd558d18SJames Chapman */ 542fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 543fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 544e2e210c0SEric Dumazet goto start; 545fd558d18SJames Chapman } 546fd558d18SJames Chapman 547fd558d18SJames Chapman out: 548fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 549fd558d18SJames Chapman } 550fd558d18SJames Chapman 5518a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) 5528a1631d5SJames Chapman { 5538a1631d5SJames Chapman u32 nws; 5548a1631d5SJames Chapman 5558a1631d5SJames Chapman if (nr >= session->nr) 5568a1631d5SJames Chapman nws = nr - session->nr; 5578a1631d5SJames Chapman else 5588a1631d5SJames Chapman nws = (session->nr_max + 1) - (session->nr - nr); 5598a1631d5SJames Chapman 5608a1631d5SJames Chapman return nws < session->nr_window_size; 5618a1631d5SJames Chapman } 5628a1631d5SJames Chapman 563b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if 564b6dc01a4SJames Chapman * acceptable, else non-zero. 565b6dc01a4SJames Chapman */ 566b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) 567b6dc01a4SJames Chapman { 5688a1631d5SJames Chapman if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { 5698a1631d5SJames Chapman /* Packet sequence number is outside allowed window. 5708a1631d5SJames Chapman * Discard it. 5718a1631d5SJames Chapman */ 5728a1631d5SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 5738a1631d5SJames Chapman "%s: pkt %u len %d discarded, outside window, nr=%u\n", 5748a1631d5SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 5758a1631d5SJames Chapman L2TP_SKB_CB(skb)->length, session->nr); 5768a1631d5SJames Chapman goto discard; 5778a1631d5SJames Chapman } 5788a1631d5SJames Chapman 579b6dc01a4SJames Chapman if (session->reorder_timeout != 0) { 580b6dc01a4SJames Chapman /* Packet reordering enabled. Add skb to session's 581b6dc01a4SJames Chapman * reorder queue, in order of ns. 582b6dc01a4SJames Chapman */ 583b6dc01a4SJames Chapman l2tp_recv_queue_skb(session, skb); 584a0dbd822SJames Chapman goto out; 585a0dbd822SJames Chapman } 586a0dbd822SJames Chapman 587a0dbd822SJames Chapman /* Packet reordering disabled. Discard out-of-sequence packets, while 588a0dbd822SJames Chapman * tracking the number if in-sequence packets after the first OOS packet 589a0dbd822SJames Chapman * is seen. After nr_oos_count_max in-sequence packets, reset the 590a0dbd822SJames Chapman * sequence number to re-enable packet reception. 591b6dc01a4SJames Chapman */ 592a0dbd822SJames Chapman if (L2TP_SKB_CB(skb)->ns == session->nr) { 593a0dbd822SJames Chapman skb_queue_tail(&session->reorder_q, skb); 594a0dbd822SJames Chapman } else { 595a0dbd822SJames Chapman u32 nr_oos = L2TP_SKB_CB(skb)->ns; 596a0dbd822SJames Chapman u32 nr_next = (session->nr_oos + 1) & session->nr_max; 597a0dbd822SJames Chapman 598a0dbd822SJames Chapman if (nr_oos == nr_next) 599a0dbd822SJames Chapman session->nr_oos_count++; 600a0dbd822SJames Chapman else 601a0dbd822SJames Chapman session->nr_oos_count = 0; 602a0dbd822SJames Chapman 603a0dbd822SJames Chapman session->nr_oos = nr_oos; 604a0dbd822SJames Chapman if (session->nr_oos_count > session->nr_oos_count_max) { 605a0dbd822SJames Chapman session->reorder_skip = 1; 606a0dbd822SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 607a0dbd822SJames Chapman "%s: %d oos packets received. Resetting sequence numbers\n", 608a0dbd822SJames Chapman session->name, session->nr_oos_count); 609a0dbd822SJames Chapman } 610a0dbd822SJames Chapman if (!session->reorder_skip) { 611b6dc01a4SJames Chapman atomic_long_inc(&session->stats.rx_seq_discards); 612b6dc01a4SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 613b6dc01a4SJames Chapman "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 614b6dc01a4SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 615b6dc01a4SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 616b6dc01a4SJames Chapman skb_queue_len(&session->reorder_q)); 617b6dc01a4SJames Chapman goto discard; 618b6dc01a4SJames Chapman } 619b6dc01a4SJames Chapman skb_queue_tail(&session->reorder_q, skb); 620b6dc01a4SJames Chapman } 621b6dc01a4SJames Chapman 622a0dbd822SJames Chapman out: 623b6dc01a4SJames Chapman return 0; 624b6dc01a4SJames Chapman 625b6dc01a4SJames Chapman discard: 626b6dc01a4SJames Chapman return 1; 627b6dc01a4SJames Chapman } 628b6dc01a4SJames Chapman 629f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 630f7faffa3SJames Chapman * and L2TPv3 data frames here. 631f7faffa3SJames Chapman * 632f7faffa3SJames Chapman * L2TPv2 Data Message Header 633f7faffa3SJames Chapman * 634f7faffa3SJames Chapman * 0 1 2 3 635f7faffa3SJames 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 636f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 637f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 638f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 639f7faffa3SJames Chapman * | Tunnel ID | Session ID | 640f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 641f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 642f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 643f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 644f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 645f7faffa3SJames Chapman * 646f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 647f7faffa3SJames Chapman * those in L2TP control frames. 648f7faffa3SJames Chapman * 649f7faffa3SJames Chapman * L2TPv3 Data Message Header 650f7faffa3SJames Chapman * 651f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 652f7faffa3SJames Chapman * | L2TP Session Header | 653f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 654f7faffa3SJames Chapman * | L2-Specific Sublayer | 655f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 656f7faffa3SJames Chapman * | Tunnel Payload ... 657f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 658f7faffa3SJames Chapman * 659f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 660f7faffa3SJames Chapman * 661f7faffa3SJames Chapman * 0 1 2 3 662f7faffa3SJames 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 663f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 664f7faffa3SJames Chapman * | Session ID | 665f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 666f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 667f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 668f7faffa3SJames Chapman * | 669f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 670f7faffa3SJames Chapman * 671f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 672f7faffa3SJames Chapman * 673f7faffa3SJames Chapman * 0 1 2 3 674f7faffa3SJames 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 675f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 676f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 677f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 678f7faffa3SJames Chapman * 679f7faffa3SJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with 680f7faffa3SJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need 681f7faffa3SJames Chapman * to parse the packet header to determine if optional fields are 682f7faffa3SJames Chapman * present. 683f7faffa3SJames Chapman * 684f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 685f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 686f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 687f7faffa3SJames Chapman * after the session-id. 68861b9a047SGuillaume Nault * 68961b9a047SGuillaume Nault * session->ref() must have been called prior to l2tp_recv_common(). 69061b9a047SGuillaume Nault * session->deref() will be called automatically after skb is processed. 691f7faffa3SJames Chapman */ 692f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 693f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 694f7faffa3SJames Chapman int length, int (*payload_hook)(struct sk_buff *skb)) 695f7faffa3SJames Chapman { 696f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 697f7faffa3SJames Chapman int offset; 698f7faffa3SJames Chapman u32 ns, nr; 699f7faffa3SJames Chapman 700f7faffa3SJames Chapman /* Parse and check optional cookie */ 701f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 702f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 703a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 704f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 705a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 706a4ca44faSJoe Perches session->session_id); 7077b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards); 708f7faffa3SJames Chapman goto discard; 709f7faffa3SJames Chapman } 710f7faffa3SJames Chapman ptr += session->peer_cookie_len; 711f7faffa3SJames Chapman } 712f7faffa3SJames Chapman 713f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 714f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 715f7faffa3SJames Chapman * 716f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 717f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 718f7faffa3SJames Chapman * we were expecting them, discard frame. 719f7faffa3SJames Chapman */ 720f7faffa3SJames Chapman ns = nr = 0; 721f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 722f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 723f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 724f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 725f7faffa3SJames Chapman ptr += 2; 726f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 727f7faffa3SJames Chapman ptr += 2; 728f7faffa3SJames Chapman 729f7faffa3SJames Chapman /* Store L2TP info in the skb */ 730f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 731f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 732f7faffa3SJames Chapman 733a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 734f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 735f7faffa3SJames Chapman session->name, ns, nr, session->nr); 736f7faffa3SJames Chapman } 737f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 738f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 739f7faffa3SJames Chapman 740f7faffa3SJames Chapman if (l2h & 0x40000000) { 741f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 742f7faffa3SJames Chapman 743f7faffa3SJames Chapman /* Store L2TP info in the skb */ 744f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 745f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 746f7faffa3SJames Chapman 747a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 748f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 749f7faffa3SJames Chapman session->name, ns, session->nr); 750f7faffa3SJames Chapman } 751f7faffa3SJames Chapman } 752f7faffa3SJames Chapman 753f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 754f7faffa3SJames Chapman ptr += session->l2specific_len; 755f7faffa3SJames Chapman 756f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 757f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 758f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 759f7faffa3SJames Chapman * configure it so. 760f7faffa3SJames Chapman */ 761f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 762a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 763f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 764f7faffa3SJames Chapman session->name); 7653f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1; 766f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 767f7faffa3SJames Chapman } 768f7faffa3SJames Chapman } else { 769f7faffa3SJames Chapman /* No sequence numbers. 770f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 771f7faffa3SJames Chapman */ 772f7faffa3SJames Chapman if (session->recv_seq) { 773a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 774a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 775a4ca44faSJoe Perches session->name); 7767b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 777f7faffa3SJames Chapman goto discard; 778f7faffa3SJames Chapman } 779f7faffa3SJames Chapman 780f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 781f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 782f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 783f7faffa3SJames Chapman * LAC is broken. Discard the frame. 784f7faffa3SJames Chapman */ 785f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 786a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 787f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 788f7faffa3SJames Chapman session->name); 789f7faffa3SJames Chapman session->send_seq = 0; 790f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 791f7faffa3SJames Chapman } else if (session->send_seq) { 792a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 793a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 794a4ca44faSJoe Perches session->name); 7957b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 796f7faffa3SJames Chapman goto discard; 797f7faffa3SJames Chapman } 798f7faffa3SJames Chapman } 799f7faffa3SJames Chapman 800f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 801f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 802f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 803f7faffa3SJames Chapman * in the session setup control protocol. 804f7faffa3SJames Chapman */ 805f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 806f7faffa3SJames Chapman /* If offset bit set, skip it. */ 807f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 808f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 809f7faffa3SJames Chapman ptr += 2 + offset; 810f7faffa3SJames Chapman } 811f7faffa3SJames Chapman } else 812f7faffa3SJames Chapman ptr += session->offset; 813f7faffa3SJames Chapman 814f7faffa3SJames Chapman offset = ptr - optr; 815f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 816f7faffa3SJames Chapman goto discard; 817f7faffa3SJames Chapman 818f7faffa3SJames Chapman __skb_pull(skb, offset); 819f7faffa3SJames Chapman 820f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 821f7faffa3SJames Chapman * packet, do so now. 822f7faffa3SJames Chapman */ 823f7faffa3SJames Chapman if (payload_hook) 824f7faffa3SJames Chapman if ((*payload_hook)(skb)) 825f7faffa3SJames Chapman goto discard; 826f7faffa3SJames Chapman 827f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 828f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 829f7faffa3SJames Chapman * reordering. 830f7faffa3SJames Chapman */ 831f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 832f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 833f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 834f7faffa3SJames Chapman 835f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 836f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 837f7faffa3SJames Chapman */ 838f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 839b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb)) 840f7faffa3SJames Chapman goto discard; 841f7faffa3SJames Chapman } else { 842f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 843f7faffa3SJames Chapman * reorder queue. This ensures that it will be 844f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 845f7faffa3SJames Chapman */ 846f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 847f7faffa3SJames Chapman } 848f7faffa3SJames Chapman 849f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 850f7faffa3SJames Chapman l2tp_recv_dequeue(session); 851f7faffa3SJames Chapman 852f7faffa3SJames Chapman return; 853f7faffa3SJames Chapman 854f7faffa3SJames Chapman discard: 8557b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 856f7faffa3SJames Chapman kfree_skb(skb); 857f7faffa3SJames Chapman 858f7faffa3SJames Chapman if (session->deref) 859f7faffa3SJames Chapman (*session->deref)(session); 860f7faffa3SJames Chapman } 861f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common); 862f7faffa3SJames Chapman 86348f72f92STom Parkin /* Drop skbs from the session's reorder_q 86448f72f92STom Parkin */ 86548f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session) 86648f72f92STom Parkin { 86748f72f92STom Parkin struct sk_buff *skb = NULL; 86848f72f92STom Parkin BUG_ON(!session); 86948f72f92STom Parkin BUG_ON(session->magic != L2TP_SESSION_MAGIC); 87048f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) { 87148f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors); 87248f72f92STom Parkin kfree_skb(skb); 87348f72f92STom Parkin if (session->deref) 87448f72f92STom Parkin (*session->deref)(session); 87548f72f92STom Parkin } 87648f72f92STom Parkin return 0; 87748f72f92STom Parkin } 87848f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); 87948f72f92STom Parkin 880fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 881fd558d18SJames Chapman * here. The skb is not on a list when we get here. 882fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 883fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 884fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 885fd558d18SJames Chapman */ 886fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 887fd558d18SJames Chapman int (*payload_hook)(struct sk_buff *skb)) 888fd558d18SJames Chapman { 889fd558d18SJames Chapman struct l2tp_session *session = NULL; 890fd558d18SJames Chapman unsigned char *ptr, *optr; 891fd558d18SJames Chapman u16 hdrflags; 892fd558d18SJames Chapman u32 tunnel_id, session_id; 893fd558d18SJames Chapman u16 version; 894f7faffa3SJames Chapman int length; 895fd558d18SJames Chapman 89658d6085cSTom Herbert /* UDP has verifed checksum */ 897fd558d18SJames Chapman 898fd558d18SJames Chapman /* UDP always verifies the packet length. */ 899fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 900fd558d18SJames Chapman 901fd558d18SJames Chapman /* Short packet? */ 902fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 903a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 904a4ca44faSJoe Perches "%s: recv short packet (len=%d)\n", 905a4ca44faSJoe Perches tunnel->name, skb->len); 906fd558d18SJames Chapman goto error; 907fd558d18SJames Chapman } 908fd558d18SJames Chapman 909fd558d18SJames Chapman /* Trace packet contents, if enabled */ 910fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 911fd558d18SJames Chapman length = min(32u, skb->len); 912fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 913fd558d18SJames Chapman goto error; 914fd558d18SJames Chapman 915a4ca44faSJoe Perches pr_debug("%s: recv\n", tunnel->name); 916a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 917fd558d18SJames Chapman } 918fd558d18SJames Chapman 919e50e705cSEric Dumazet /* Point to L2TP header */ 920e50e705cSEric Dumazet optr = ptr = skb->data; 921e50e705cSEric Dumazet 922fd558d18SJames Chapman /* Get L2TP header flags */ 923fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 924fd558d18SJames Chapman 925fd558d18SJames Chapman /* Check protocol version */ 926fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 927fd558d18SJames Chapman if (version != tunnel->version) { 928a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 929fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 930fd558d18SJames Chapman tunnel->name, version, tunnel->version); 931fd558d18SJames Chapman goto error; 932fd558d18SJames Chapman } 933fd558d18SJames Chapman 934fd558d18SJames Chapman /* Get length of L2TP packet */ 935fd558d18SJames Chapman length = skb->len; 936fd558d18SJames Chapman 937fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 938fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 939a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, 940a4ca44faSJoe Perches "%s: recv control packet, len=%d\n", 941a4ca44faSJoe Perches tunnel->name, length); 942fd558d18SJames Chapman goto error; 943fd558d18SJames Chapman } 944fd558d18SJames Chapman 945fd558d18SJames Chapman /* Skip flags */ 946fd558d18SJames Chapman ptr += 2; 947fd558d18SJames Chapman 948f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 949fd558d18SJames Chapman /* If length is present, skip it */ 950fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 951fd558d18SJames Chapman ptr += 2; 952fd558d18SJames Chapman 953fd558d18SJames Chapman /* Extract tunnel and session ID */ 954fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 955fd558d18SJames Chapman ptr += 2; 956fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 957fd558d18SJames Chapman ptr += 2; 958f7faffa3SJames Chapman } else { 959f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 960f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 961f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 962f7faffa3SJames Chapman ptr += 4; 963f7faffa3SJames Chapman } 964fd558d18SJames Chapman 965fd558d18SJames Chapman /* Find the session context */ 96661b9a047SGuillaume Nault session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true); 967309795f4SJames Chapman if (!session || !session->recv_skb) { 96861b9a047SGuillaume Nault if (session) { 96961b9a047SGuillaume Nault if (session->deref) 97061b9a047SGuillaume Nault session->deref(session); 97161b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 97261b9a047SGuillaume Nault } 97361b9a047SGuillaume Nault 974fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 975a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 976f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 977fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 978fd558d18SJames Chapman goto error; 979fd558d18SJames Chapman } 980fd558d18SJames Chapman 981f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 98261b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 983fd558d18SJames Chapman 984fd558d18SJames Chapman return 0; 985fd558d18SJames Chapman 986fd558d18SJames Chapman error: 987fd558d18SJames Chapman /* Put UDP header back */ 988fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 989fd558d18SJames Chapman 990fd558d18SJames Chapman return 1; 991fd558d18SJames Chapman } 992fd558d18SJames Chapman 993fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 994fd558d18SJames Chapman * Return codes: 995fd558d18SJames Chapman * 0 : success. 996fd558d18SJames Chapman * <0: error 997fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 998fd558d18SJames Chapman */ 999fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 1000fd558d18SJames Chapman { 1001fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1002fd558d18SJames Chapman 1003fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 1004fd558d18SJames Chapman if (tunnel == NULL) 1005fd558d18SJames Chapman goto pass_up; 1006fd558d18SJames Chapman 1007a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 1008a4ca44faSJoe Perches tunnel->name, skb->len); 1009fd558d18SJames Chapman 1010fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 1011fd558d18SJames Chapman goto pass_up_put; 1012fd558d18SJames Chapman 1013fd558d18SJames Chapman sock_put(sk); 1014fd558d18SJames Chapman return 0; 1015fd558d18SJames Chapman 1016fd558d18SJames Chapman pass_up_put: 1017fd558d18SJames Chapman sock_put(sk); 1018fd558d18SJames Chapman pass_up: 1019fd558d18SJames Chapman return 1; 1020fd558d18SJames Chapman } 1021fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 1022fd558d18SJames Chapman 1023fd558d18SJames Chapman /************************************************************************ 1024fd558d18SJames Chapman * Transmit handling 1025fd558d18SJames Chapman ***********************************************************************/ 1026fd558d18SJames Chapman 1027fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 1028fd558d18SJames Chapman */ 1029f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 1030fd558d18SJames Chapman { 1031f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1032fd558d18SJames Chapman __be16 *bufp = buf; 1033f7faffa3SJames Chapman __be16 *optr = buf; 1034fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 1035fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 1036fd558d18SJames Chapman u32 session_id = session->peer_session_id; 1037fd558d18SJames Chapman 1038fd558d18SJames Chapman if (session->send_seq) 1039fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 1040fd558d18SJames Chapman 1041fd558d18SJames Chapman /* Setup L2TP header. */ 1042fd558d18SJames Chapman *bufp++ = htons(flags); 1043fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 1044fd558d18SJames Chapman *bufp++ = htons(session_id); 1045fd558d18SJames Chapman if (session->send_seq) { 1046fd558d18SJames Chapman *bufp++ = htons(session->ns); 1047fd558d18SJames Chapman *bufp++ = 0; 1048fd558d18SJames Chapman session->ns++; 1049f7faffa3SJames Chapman session->ns &= 0xffff; 1050a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 1051a4ca44faSJoe Perches session->name, session->ns); 1052fd558d18SJames Chapman } 1053fd558d18SJames Chapman 1054f7faffa3SJames Chapman return bufp - optr; 1055f7faffa3SJames Chapman } 1056f7faffa3SJames Chapman 1057f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 1058fd558d18SJames Chapman { 10590d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1060f7faffa3SJames Chapman char *bufp = buf; 1061f7faffa3SJames Chapman char *optr = bufp; 1062fd558d18SJames Chapman 10630d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 10640d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 10650d76751fSJames Chapman */ 10660d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 10670d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 1068f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 1069f7faffa3SJames Chapman bufp += 2; 1070f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 1071f7faffa3SJames Chapman bufp += 2; 10720d76751fSJames Chapman } 10730d76751fSJames Chapman 1074f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 1075f7faffa3SJames Chapman bufp += 4; 1076f7faffa3SJames Chapman if (session->cookie_len) { 1077f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 1078f7faffa3SJames Chapman bufp += session->cookie_len; 1079fd558d18SJames Chapman } 1080f7faffa3SJames Chapman if (session->l2specific_len) { 1081f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1082f7faffa3SJames Chapman u32 l2h = 0; 1083f7faffa3SJames Chapman if (session->send_seq) { 1084f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1085f7faffa3SJames Chapman session->ns++; 1086f7faffa3SJames Chapman session->ns &= 0xffffff; 1087a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 1088a4ca44faSJoe Perches "%s: updated ns to %u\n", 1089a4ca44faSJoe Perches session->name, session->ns); 1090f7faffa3SJames Chapman } 1091f7faffa3SJames Chapman 1092f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 1093f7faffa3SJames Chapman } 1094f7faffa3SJames Chapman bufp += session->l2specific_len; 1095f7faffa3SJames Chapman } 1096f7faffa3SJames Chapman if (session->offset) 1097f7faffa3SJames Chapman bufp += session->offset; 1098f7faffa3SJames Chapman 1099f7faffa3SJames Chapman return bufp - optr; 1100f7faffa3SJames Chapman } 1101fd558d18SJames Chapman 1102fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1103d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1104fd558d18SJames Chapman { 1105fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1106fd558d18SJames Chapman unsigned int len = skb->len; 1107fd558d18SJames Chapman int error; 1108fd558d18SJames Chapman 1109fd558d18SJames Chapman /* Debug */ 1110fd558d18SJames Chapman if (session->send_seq) 11115b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", 1112a4ca44faSJoe Perches session->name, data_len, session->ns - 1); 1113fd558d18SJames Chapman else 11145b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", 1115a4ca44faSJoe Perches session->name, data_len); 1116fd558d18SJames Chapman 1117fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 11180d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11190d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1120fd558d18SJames Chapman 1121a4ca44faSJoe Perches pr_debug("%s: xmit\n", session->name); 1122a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1123a4ca44faSJoe Perches datap, min_t(size_t, 32, len - uhlen)); 1124fd558d18SJames Chapman } 1125fd558d18SJames Chapman 1126fd558d18SJames Chapman /* Queue the packet to IP for output */ 112760ff7467SWANG Cong skb->ignore_df = 1; 1128d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1129746e3499SEric Dumazet if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped) 1130b0270e91SEric Dumazet error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1131d2cf3361SBenjamin LaHaise else 1132d2cf3361SBenjamin LaHaise #endif 1133b0270e91SEric Dumazet error = ip_queue_xmit(tunnel->sock, skb, fl); 1134fd558d18SJames Chapman 1135fd558d18SJames Chapman /* Update stats */ 1136fd558d18SJames Chapman if (error >= 0) { 11377b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_packets); 11387b7c0719STom Parkin atomic_long_add(len, &tunnel->stats.tx_bytes); 11397b7c0719STom Parkin atomic_long_inc(&session->stats.tx_packets); 11407b7c0719STom Parkin atomic_long_add(len, &session->stats.tx_bytes); 1141fd558d18SJames Chapman } else { 11427b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_errors); 11437b7c0719STom Parkin atomic_long_inc(&session->stats.tx_errors); 1144fd558d18SJames Chapman } 1145fd558d18SJames Chapman 1146fd558d18SJames Chapman return 0; 1147fd558d18SJames Chapman } 1148fd558d18SJames Chapman 1149fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1150fd558d18SJames Chapman * inserted in the skb data before calling this function. 1151fd558d18SJames Chapman */ 1152fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1153fd558d18SJames Chapman { 1154fd558d18SJames Chapman int data_len = skb->len; 11550d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 11560d76751fSJames Chapman struct sock *sk = tunnel->sock; 1157d9d8da80SDavid S. Miller struct flowi *fl; 1158fd558d18SJames Chapman struct udphdr *uh; 1159fd558d18SJames Chapman struct inet_sock *inet; 1160fd558d18SJames Chapman int headroom; 11610d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11620d76751fSJames Chapman int udp_len; 1163b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1164fd558d18SJames Chapman 1165fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1166fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1167fd558d18SJames Chapman * make room. Adjust truesize. 1168fd558d18SJames Chapman */ 1169fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11700d76751fSJames Chapman uhlen + hdr_len; 1171835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1172b8c84307SEric Dumazet kfree_skb(skb); 1173b8c84307SEric Dumazet return NET_XMIT_DROP; 1174835acf5dSEric Dumazet } 1175fd558d18SJames Chapman 1176fd558d18SJames Chapman /* Setup L2TP header */ 1177f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1178fd558d18SJames Chapman 11790d76751fSJames Chapman /* Reset skb netfilter state */ 1180fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1181fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1182fd558d18SJames Chapman IPSKB_REROUTED); 1183fd558d18SJames Chapman nf_reset(skb); 1184fd558d18SJames Chapman 11856af88da1SDavid S. Miller bh_lock_sock(sk); 11866af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1187b8c84307SEric Dumazet kfree_skb(skb); 1188b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11896af88da1SDavid S. Miller goto out_unlock; 11906af88da1SDavid S. Miller } 11916af88da1SDavid S. Miller 1192fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1193fd558d18SJames Chapman skb_dst_drop(skb); 119471b1391aSFlorian Westphal skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 11950d76751fSJames Chapman 1196d9d8da80SDavid S. Miller inet = inet_sk(sk); 1197d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11980d76751fSJames Chapman switch (tunnel->encap) { 11990d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 12000d76751fSJames Chapman /* Setup UDP header */ 12010d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 12020d76751fSJames Chapman skb_reset_transport_header(skb); 12030d76751fSJames Chapman uh = udp_hdr(skb); 12040d76751fSJames Chapman uh->source = inet->inet_sport; 12050d76751fSJames Chapman uh->dest = inet->inet_dport; 12060d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 12070d76751fSJames Chapman uh->len = htons(udp_len); 1208fd558d18SJames Chapman 1209fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1210d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1211e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) 121277157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 121377157e19STom Herbert skb, &inet6_sk(sk)->saddr, 121477157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1215d2cf3361SBenjamin LaHaise else 1216d2cf3361SBenjamin LaHaise #endif 121777157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 121877157e19STom Herbert inet->inet_daddr, udp_len); 12190d76751fSJames Chapman break; 12200d76751fSJames Chapman 12210d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12220d76751fSJames Chapman break; 12230d76751fSJames Chapman } 12240d76751fSJames Chapman 1225d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 12266af88da1SDavid S. Miller out_unlock: 12276af88da1SDavid S. Miller bh_unlock_sock(sk); 1228fd558d18SJames Chapman 1229b8c84307SEric Dumazet return ret; 1230fd558d18SJames Chapman } 1231fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1232fd558d18SJames Chapman 1233fd558d18SJames Chapman /***************************************************************************** 1234fd558d18SJames Chapman * Tinnel and session create/destroy. 1235fd558d18SJames Chapman *****************************************************************************/ 1236fd558d18SJames Chapman 1237fd558d18SJames Chapman /* Tunnel socket destruct hook. 1238fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1239fd558d18SJames Chapman * closed. 1240fd558d18SJames Chapman */ 1241fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1242fd558d18SJames Chapman { 12438d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1244f8ccac0eSTom Parkin struct l2tp_net *pn; 1245fd558d18SJames Chapman 1246fd558d18SJames Chapman if (tunnel == NULL) 1247fd558d18SJames Chapman goto end; 1248fd558d18SJames Chapman 1249a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1250fd558d18SJames Chapman 1251fd558d18SJames Chapman 1252f8ccac0eSTom Parkin /* Disable udp encapsulation */ 12530d76751fSJames Chapman switch (tunnel->encap) { 12540d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1255fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1256fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1257fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12589980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 12590d76751fSJames Chapman break; 12600d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12610d76751fSJames Chapman break; 12620d76751fSJames Chapman } 1263fd558d18SJames Chapman 1264fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1265fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1266fd558d18SJames Chapman sk->sk_user_data = NULL; 1267f8ccac0eSTom Parkin tunnel->sock = NULL; 1268f8ccac0eSTom Parkin 1269f8ccac0eSTom Parkin /* Remove the tunnel struct from the tunnel list */ 1270f8ccac0eSTom Parkin pn = l2tp_pernet(tunnel->l2tp_net); 1271f8ccac0eSTom Parkin spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1272f8ccac0eSTom Parkin list_del_rcu(&tunnel->list); 1273f8ccac0eSTom Parkin spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1274f8ccac0eSTom Parkin atomic_dec(&l2tp_tunnel_count); 1275f8ccac0eSTom Parkin 1276f8ccac0eSTom Parkin l2tp_tunnel_closeall(tunnel); 1277f8ccac0eSTom Parkin l2tp_tunnel_dec_refcount(tunnel); 1278fd558d18SJames Chapman 1279fd558d18SJames Chapman /* Call the original destructor */ 1280fd558d18SJames Chapman if (sk->sk_destruct) 1281fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1282fd558d18SJames Chapman end: 1283fd558d18SJames Chapman return; 1284fd558d18SJames Chapman } 1285fd558d18SJames Chapman 1286fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1287fd558d18SJames Chapman */ 1288e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1289fd558d18SJames Chapman { 1290fd558d18SJames Chapman int hash; 1291fd558d18SJames Chapman struct hlist_node *walk; 1292fd558d18SJames Chapman struct hlist_node *tmp; 1293fd558d18SJames Chapman struct l2tp_session *session; 1294fd558d18SJames Chapman 1295fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1296fd558d18SJames Chapman 1297a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1298a4ca44faSJoe Perches tunnel->name); 1299fd558d18SJames Chapman 1300fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1301fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1302fd558d18SJames Chapman again: 1303fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1304fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1305fd558d18SJames Chapman 1306a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1307fd558d18SJames Chapman "%s: closing session\n", session->name); 1308fd558d18SJames Chapman 1309fd558d18SJames Chapman hlist_del_init(&session->hlist); 1310fd558d18SJames Chapman 1311fd558d18SJames Chapman if (session->ref != NULL) 1312fd558d18SJames Chapman (*session->ref)(session); 1313fd558d18SJames Chapman 1314fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1315fd558d18SJames Chapman 1316f6e16b29STom Parkin __l2tp_session_unhash(session); 13174c6e2fd3STom Parkin l2tp_session_queue_purge(session); 13184c6e2fd3STom Parkin 1319fd558d18SJames Chapman if (session->session_close != NULL) 1320fd558d18SJames Chapman (*session->session_close)(session); 1321fd558d18SJames Chapman 1322fd558d18SJames Chapman if (session->deref != NULL) 1323fd558d18SJames Chapman (*session->deref)(session); 1324fd558d18SJames Chapman 13259980d001STom Parkin l2tp_session_dec_refcount(session); 13269980d001STom Parkin 1327fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1328fd558d18SJames Chapman 1329fd558d18SJames Chapman /* Now restart from the beginning of this hash 1330fd558d18SJames Chapman * chain. We always remove a session from the 1331fd558d18SJames Chapman * list so we are guaranteed to make forward 1332fd558d18SJames Chapman * progress. 1333fd558d18SJames Chapman */ 1334fd558d18SJames Chapman goto again; 1335fd558d18SJames Chapman } 1336fd558d18SJames Chapman } 1337fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1338fd558d18SJames Chapman } 1339e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); 1340fd558d18SJames Chapman 13419980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 13429980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 13439980d001STom Parkin { 13449980d001STom Parkin struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 13459980d001STom Parkin if (tunnel) { 13469980d001STom Parkin l2tp_tunnel_closeall(tunnel); 13479980d001STom Parkin sock_put(sk); 13489980d001STom Parkin } 13499980d001STom Parkin } 13509980d001STom Parkin 1351fd558d18SJames Chapman /* Really kill the tunnel. 1352fd558d18SJames Chapman * Come here only when all sessions have been cleared from the tunnel. 1353fd558d18SJames Chapman */ 1354fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 1355fd558d18SJames Chapman { 1356fd558d18SJames Chapman BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1357fd558d18SJames Chapman BUG_ON(tunnel->sock != NULL); 1358a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name); 135999469c32Sxeb@mail.ru kfree_rcu(tunnel, rcu); 1360f8ccac0eSTom Parkin } 1361fd558d18SJames Chapman 1362f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1363f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1364f8ccac0eSTom Parkin { 1365f8ccac0eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1366f8ccac0eSTom Parkin struct socket *sock = NULL; 1367f8ccac0eSTom Parkin struct sock *sk = NULL; 1368f8ccac0eSTom Parkin 1369f8ccac0eSTom Parkin tunnel = container_of(work, struct l2tp_tunnel, del_work); 137012d656afSRidge Kennedy 137112d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 137212d656afSRidge Kennedy 1373f8ccac0eSTom Parkin sk = l2tp_tunnel_sock_lookup(tunnel); 1374f8ccac0eSTom Parkin if (!sk) 137506a15f51SAlexander Couzens goto out; 1376f8ccac0eSTom Parkin 1377f8ccac0eSTom Parkin sock = sk->sk_socket; 1378f8ccac0eSTom Parkin 137902d13ed5STom Parkin /* If the tunnel socket was created by userspace, then go through the 138002d13ed5STom Parkin * inet layer to shut the socket down, and let userspace close it. 138102d13ed5STom Parkin * Otherwise, if we created the socket directly within the kernel, use 138202d13ed5STom Parkin * the sk API to release it here. 1383167eb17eSTom Parkin * In either case the tunnel resources are freed in the socket 1384167eb17eSTom Parkin * destructor when the tunnel socket goes away. 1385f8ccac0eSTom Parkin */ 138602d13ed5STom Parkin if (tunnel->fd >= 0) { 138702d13ed5STom Parkin if (sock) 138802d13ed5STom Parkin inet_shutdown(sock, 2); 138902d13ed5STom Parkin } else { 139026abe143SEric W. Biederman if (sock) { 1391167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 139226abe143SEric W. Biederman sock_release(sock); 139326abe143SEric W. Biederman } 1394167eb17eSTom Parkin } 1395f8ccac0eSTom Parkin 1396f8ccac0eSTom Parkin l2tp_tunnel_sock_put(sk); 139706a15f51SAlexander Couzens out: 139806a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1399fd558d18SJames Chapman } 1400fd558d18SJames Chapman 1401789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1402789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1403789a4a2cSJames Chapman * managing L2TP daemon. 1404167eb17eSTom Parkin * 1405167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1406167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1407167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1408167eb17eSTom Parkin * exit hook. 1409789a4a2cSJames Chapman */ 1410167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1411167eb17eSTom Parkin u32 tunnel_id, 1412167eb17eSTom Parkin u32 peer_tunnel_id, 1413167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1414167eb17eSTom Parkin struct socket **sockp) 1415789a4a2cSJames Chapman { 1416789a4a2cSJames Chapman int err = -EINVAL; 14177bddd0dbSEric Dumazet struct socket *sock = NULL; 141885644b4dSTom Herbert struct udp_port_cfg udp_conf; 1419789a4a2cSJames Chapman 1420789a4a2cSJames Chapman switch (cfg->encap) { 1421789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 142285644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 142385644b4dSTom Herbert 1424f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1425f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 142685644b4dSTom Herbert udp_conf.family = AF_INET6; 142785644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 142885644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 142985644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 143085644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 143185644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1432018f8258SWang Shanker ! cfg->udp6_zero_tx_checksums; 143385644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1434018f8258SWang Shanker ! cfg->udp6_zero_rx_checksums; 1435f9bac8dfSChris Elston } else 1436f9bac8dfSChris Elston #endif 1437f9bac8dfSChris Elston { 143885644b4dSTom Herbert udp_conf.family = AF_INET; 143985644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 144085644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 144185644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1442f9bac8dfSChris Elston } 1443789a4a2cSJames Chapman 144485644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 144585644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 144685644b4dSTom Herbert 144785644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 144885644b4dSTom Herbert if (err < 0) 144985644b4dSTom Herbert goto out; 1450789a4a2cSJames Chapman 1451789a4a2cSJames Chapman break; 1452789a4a2cSJames Chapman 1453789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1454f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1455f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 145685644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 145785644b4dSTom Herbert 145826abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1459167eb17eSTom Parkin IPPROTO_L2TP, &sock); 14605dac94e1SJames Chapman if (err < 0) 1461f9bac8dfSChris Elston goto out; 14625dac94e1SJames Chapman 14635dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14645dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14655dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14665dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14675dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, 14685dac94e1SJames Chapman sizeof(ip6_addr)); 14695dac94e1SJames Chapman if (err < 0) 14705dac94e1SJames Chapman goto out; 14715dac94e1SJames Chapman 14725dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14735dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14745dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14755dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14765dac94e1SJames Chapman err = kernel_connect(sock, 14775dac94e1SJames Chapman (struct sockaddr *) &ip6_addr, 14785dac94e1SJames Chapman sizeof(ip6_addr), 0); 14795dac94e1SJames Chapman if (err < 0) 14805dac94e1SJames Chapman goto out; 14815dac94e1SJames Chapman } else 1482f9bac8dfSChris Elston #endif 14835dac94e1SJames Chapman { 148485644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 148585644b4dSTom Herbert 148626abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1487167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1488789a4a2cSJames Chapman if (err < 0) 1489789a4a2cSJames Chapman goto out; 1490789a4a2cSJames Chapman 1491789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1492789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1493789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14945dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 14955dac94e1SJames Chapman sizeof(ip_addr)); 1496789a4a2cSJames Chapman if (err < 0) 1497789a4a2cSJames Chapman goto out; 1498789a4a2cSJames Chapman 1499789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1500789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1501789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 15025dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 15035dac94e1SJames Chapman sizeof(ip_addr), 0); 1504789a4a2cSJames Chapman if (err < 0) 1505789a4a2cSJames Chapman goto out; 15065dac94e1SJames Chapman } 1507789a4a2cSJames Chapman break; 1508789a4a2cSJames Chapman 1509789a4a2cSJames Chapman default: 1510789a4a2cSJames Chapman goto out; 1511789a4a2cSJames Chapman } 1512789a4a2cSJames Chapman 1513789a4a2cSJames Chapman out: 1514167eb17eSTom Parkin *sockp = sock; 1515789a4a2cSJames Chapman if ((err < 0) && sock) { 1516167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 151726abe143SEric W. Biederman sock_release(sock); 1518789a4a2cSJames Chapman *sockp = NULL; 1519789a4a2cSJames Chapman } 1520789a4a2cSJames Chapman 1521789a4a2cSJames Chapman return err; 1522789a4a2cSJames Chapman } 1523789a4a2cSJames Chapman 152437159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 152537159ef2SEric Dumazet 1526fd558d18SJames Chapman int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1527fd558d18SJames Chapman { 1528fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1529fd558d18SJames Chapman int err; 1530fd558d18SJames Chapman struct socket *sock = NULL; 1531fd558d18SJames Chapman struct sock *sk = NULL; 1532fd558d18SJames Chapman struct l2tp_net *pn; 15330d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1534fd558d18SJames Chapman 1535fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1536789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1537789a4a2cSJames Chapman * kernel socket. 1538fd558d18SJames Chapman */ 1539789a4a2cSJames Chapman if (fd < 0) { 1540167eb17eSTom Parkin err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, 1541167eb17eSTom Parkin cfg, &sock); 1542789a4a2cSJames Chapman if (err < 0) 1543789a4a2cSJames Chapman goto err; 1544789a4a2cSJames Chapman } else { 1545fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1546fd558d18SJames Chapman if (!sock) { 1547cbb95e0cSTom Parkin pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", 1548fd558d18SJames Chapman tunnel_id, fd, err); 1549cbb95e0cSTom Parkin err = -EBADF; 1550cbb95e0cSTom Parkin goto err; 1551cbb95e0cSTom Parkin } 1552cbb95e0cSTom Parkin 1553cbb95e0cSTom Parkin /* Reject namespace mismatches */ 1554cbb95e0cSTom Parkin if (!net_eq(sock_net(sock->sk), net)) { 1555cbb95e0cSTom Parkin pr_err("tunl %u: netns mismatch\n", tunnel_id); 1556cbb95e0cSTom Parkin err = -EINVAL; 1557fd558d18SJames Chapman goto err; 1558fd558d18SJames Chapman } 1559789a4a2cSJames Chapman } 1560fd558d18SJames Chapman 1561fd558d18SJames Chapman sk = sock->sk; 1562fd558d18SJames Chapman 15630d76751fSJames Chapman if (cfg != NULL) 15640d76751fSJames Chapman encap = cfg->encap; 15650d76751fSJames Chapman 1566fd558d18SJames Chapman /* Quick sanity checks */ 15670d76751fSJames Chapman switch (encap) { 15680d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1569fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1570fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1571a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1572fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1573fd558d18SJames Chapman goto err; 1574fd558d18SJames Chapman } 15750d76751fSJames Chapman break; 15760d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 15770d76751fSJames Chapman err = -EPROTONOSUPPORT; 15780d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 1579a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 15800d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1581fd558d18SJames Chapman goto err; 1582fd558d18SJames Chapman } 15830d76751fSJames Chapman break; 15840d76751fSJames Chapman } 1585fd558d18SJames Chapman 1586fd558d18SJames Chapman /* Check if this socket has already been prepped */ 15878d8a51e2SDavid S. Miller tunnel = l2tp_tunnel(sk); 1588fd558d18SJames Chapman if (tunnel != NULL) { 1589fd558d18SJames Chapman /* This socket has already been prepped */ 1590fd558d18SJames Chapman err = -EBUSY; 1591fd558d18SJames Chapman goto err; 1592fd558d18SJames Chapman } 1593fd558d18SJames Chapman 1594fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1595fd558d18SJames Chapman if (tunnel == NULL) { 1596fd558d18SJames Chapman err = -ENOMEM; 1597fd558d18SJames Chapman goto err; 1598fd558d18SJames Chapman } 1599fd558d18SJames Chapman 1600fd558d18SJames Chapman tunnel->version = version; 1601fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1602fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1603fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1604fd558d18SJames Chapman 1605fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1606fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1607fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1608fd558d18SJames Chapman 1609fd558d18SJames Chapman /* The net we belong to */ 1610fd558d18SJames Chapman tunnel->l2tp_net = net; 1611fd558d18SJames Chapman pn = l2tp_pernet(net); 1612fd558d18SJames Chapman 16130d76751fSJames Chapman if (cfg != NULL) 1614fd558d18SJames Chapman tunnel->debug = cfg->debug; 1615fd558d18SJames Chapman 1616e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6) 1617e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6) { 1618e18503f4SFrançois Cachereul struct ipv6_pinfo *np = inet6_sk(sk); 1619e18503f4SFrançois Cachereul 1620e18503f4SFrançois Cachereul if (ipv6_addr_v4mapped(&np->saddr) && 1621efe4208fSEric Dumazet ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 1622e18503f4SFrançois Cachereul struct inet_sock *inet = inet_sk(sk); 1623e18503f4SFrançois Cachereul 1624e18503f4SFrançois Cachereul tunnel->v4mapped = true; 1625e18503f4SFrançois Cachereul inet->inet_saddr = np->saddr.s6_addr32[3]; 1626efe4208fSEric Dumazet inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3]; 1627efe4208fSEric Dumazet inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3]; 1628e18503f4SFrançois Cachereul } else { 1629e18503f4SFrançois Cachereul tunnel->v4mapped = false; 1630e18503f4SFrançois Cachereul } 1631e18503f4SFrançois Cachereul } 1632e18503f4SFrançois Cachereul #endif 1633e18503f4SFrançois Cachereul 1634fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 16350d76751fSJames Chapman tunnel->encap = encap; 16360d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 1637a5c5e2daSGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { }; 1638fd558d18SJames Chapman 1639c8fffceaSAndy Zhou udp_cfg.sk_user_data = tunnel; 1640c8fffceaSAndy Zhou udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP; 1641c8fffceaSAndy Zhou udp_cfg.encap_rcv = l2tp_udp_encap_recv; 1642c8fffceaSAndy Zhou udp_cfg.encap_destroy = l2tp_udp_encap_destroy; 1643c8fffceaSAndy Zhou 1644c8fffceaSAndy Zhou setup_udp_tunnel_sock(net, sock, &udp_cfg); 1645c8fffceaSAndy Zhou } else { 1646fd558d18SJames Chapman sk->sk_user_data = tunnel; 1647c8fffceaSAndy Zhou } 1648fd558d18SJames Chapman 1649fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1650fd558d18SJames Chapman * if the tunnel socket goes away. 1651fd558d18SJames Chapman */ 1652fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1653fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1654fd558d18SJames Chapman tunnel->sock = sk; 165580d84ef3STom Parkin tunnel->fd = fd; 165637159ef2SEric Dumazet lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); 165737159ef2SEric Dumazet 1658fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1659fd558d18SJames Chapman 1660f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1661f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1662f8ccac0eSTom Parkin 1663fd558d18SJames Chapman /* Add tunnel to our list */ 1664fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1665fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1666fd558d18SJames Chapman 1667fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 16681769192aSEric Dumazet * only when this drops to zero. Must be done before list insertion 1669fd558d18SJames Chapman */ 1670fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 16711769192aSEric Dumazet spin_lock_bh(&pn->l2tp_tunnel_list_lock); 16721769192aSEric Dumazet list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 16731769192aSEric Dumazet spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1674fd558d18SJames Chapman 1675fd558d18SJames Chapman err = 0; 1676fd558d18SJames Chapman err: 1677fd558d18SJames Chapman if (tunnelp) 1678fd558d18SJames Chapman *tunnelp = tunnel; 1679fd558d18SJames Chapman 1680789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1681789a4a2cSJames Chapman * have a file. 1682789a4a2cSJames Chapman */ 1683789a4a2cSJames Chapman if (sock && sock->file) 1684fd558d18SJames Chapman sockfd_put(sock); 1685fd558d18SJames Chapman 1686fd558d18SJames Chapman return err; 1687fd558d18SJames Chapman } 1688fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1689fd558d18SJames Chapman 1690309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1691309795f4SJames Chapman */ 1692309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1693309795f4SJames Chapman { 169406a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 169506a15f51SAlexander Couzens if (false == queue_work(l2tp_wq, &tunnel->del_work)) { 169606a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 169706a15f51SAlexander Couzens return 1; 169806a15f51SAlexander Couzens } 169906a15f51SAlexander Couzens return 0; 1700309795f4SJames Chapman } 1701309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1702309795f4SJames Chapman 1703fd558d18SJames Chapman /* Really kill the session. 1704fd558d18SJames Chapman */ 1705fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1706fd558d18SJames Chapman { 1707f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1708fd558d18SJames Chapman 1709fd558d18SJames Chapman BUG_ON(atomic_read(&session->ref_count) != 0); 1710fd558d18SJames Chapman 1711f6e16b29STom Parkin if (tunnel) { 1712fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1713fd558d18SJames Chapman if (session->session_id != 0) 1714fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1715fd558d18SJames Chapman sock_put(tunnel->sock); 1716fd558d18SJames Chapman session->tunnel = NULL; 1717fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1718fd558d18SJames Chapman } 1719fd558d18SJames Chapman 1720fd558d18SJames Chapman kfree(session); 1721fd558d18SJames Chapman } 1722fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1723fd558d18SJames Chapman 1724f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. 1725f6e16b29STom Parkin * Provides a tidyup interface for pseudowire code which can't just route all 1726f6e16b29STom Parkin * shutdown via. l2tp_session_delete and a pseudowire-specific session_close 1727f6e16b29STom Parkin * callback. 1728f6e16b29STom Parkin */ 1729f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session) 1730f6e16b29STom Parkin { 1731f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1732f6e16b29STom Parkin 1733f6e16b29STom Parkin /* Remove the session from core hashes */ 1734f6e16b29STom Parkin if (tunnel) { 1735f6e16b29STom Parkin /* Remove from the per-tunnel hash */ 1736f6e16b29STom Parkin write_lock_bh(&tunnel->hlist_lock); 1737f6e16b29STom Parkin hlist_del_init(&session->hlist); 1738f6e16b29STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1739f6e16b29STom Parkin 1740f6e16b29STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1741f6e16b29STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1742f6e16b29STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1743f6e16b29STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1744f6e16b29STom Parkin hlist_del_init_rcu(&session->global_hlist); 1745f6e16b29STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1746f6e16b29STom Parkin synchronize_rcu(); 1747f6e16b29STom Parkin } 1748f6e16b29STom Parkin } 1749f6e16b29STom Parkin } 1750f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash); 1751f6e16b29STom Parkin 1752309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1753309795f4SJames Chapman pseudowire modules. 1754309795f4SJames Chapman */ 1755309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1756309795f4SJames Chapman { 1757f6e16b29STom Parkin if (session->ref) 1758f6e16b29STom Parkin (*session->ref)(session); 1759f6e16b29STom Parkin __l2tp_session_unhash(session); 17604c6e2fd3STom Parkin l2tp_session_queue_purge(session); 1761309795f4SJames Chapman if (session->session_close != NULL) 1762309795f4SJames Chapman (*session->session_close)(session); 1763f6e16b29STom Parkin if (session->deref) 17641b7c92b9SDan Carpenter (*session->deref)(session); 1765309795f4SJames Chapman l2tp_session_dec_refcount(session); 1766309795f4SJames Chapman return 0; 1767309795f4SJames Chapman } 1768309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1769309795f4SJames Chapman 1770f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1771f7faffa3SJames Chapman * l2specific_len parameters are set. 1772f7faffa3SJames Chapman */ 1773bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1774f7faffa3SJames Chapman { 1775f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1776f7faffa3SJames Chapman session->hdr_len = 6; 1777f7faffa3SJames Chapman if (session->send_seq) 1778f7faffa3SJames Chapman session->hdr_len += 4; 1779f7faffa3SJames Chapman } else { 17800d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 17810d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 17820d76751fSJames Chapman session->hdr_len += 4; 1783f7faffa3SJames Chapman } 1784f7faffa3SJames Chapman 1785f7faffa3SJames Chapman } 1786bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1787f7faffa3SJames Chapman 1788fd558d18SJames Chapman struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) 1789fd558d18SJames Chapman { 1790fd558d18SJames Chapman struct l2tp_session *session; 1791dbdbc73bSGuillaume Nault int err; 1792fd558d18SJames Chapman 1793fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1794fd558d18SJames Chapman if (session != NULL) { 1795fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1796fd558d18SJames Chapman session->tunnel = tunnel; 1797fd558d18SJames Chapman 1798fd558d18SJames Chapman session->session_id = session_id; 1799fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1800d301e325SJames Chapman session->nr = 0; 18018a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 18028a1631d5SJames Chapman session->nr_max = 0xffff; 18038a1631d5SJames Chapman else 18048a1631d5SJames Chapman session->nr_max = 0xffffff; 18058a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1806a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1807a0dbd822SJames Chapman 1808a0dbd822SJames Chapman /* Use NR of first received packet */ 1809a0dbd822SJames Chapman session->reorder_skip = 1; 1810fd558d18SJames Chapman 1811fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1812fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1813fd558d18SJames Chapman 1814fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1815fd558d18SJames Chapman 1816fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1817f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1818fd558d18SJames Chapman 1819fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1820fd558d18SJames Chapman session->debug = tunnel->debug; 1821fd558d18SJames Chapman 1822fd558d18SJames Chapman if (cfg) { 1823f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1824fd558d18SJames Chapman session->debug = cfg->debug; 1825fd558d18SJames Chapman session->mtu = cfg->mtu; 1826fd558d18SJames Chapman session->mru = cfg->mru; 1827fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1828fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1829fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1830f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1831f7faffa3SJames Chapman session->offset = cfg->offset; 1832f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1833f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1834f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1835f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1836f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1837f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1838fd558d18SJames Chapman } 1839fd558d18SJames Chapman 1840f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1841f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1842f7faffa3SJames Chapman else 1843f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1844f7faffa3SJames Chapman 1845f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1846f7faffa3SJames Chapman 1847dbdbc73bSGuillaume Nault err = l2tp_session_add_to_tunnel(tunnel, session); 1848dbdbc73bSGuillaume Nault if (err) { 1849dbdbc73bSGuillaume Nault kfree(session); 1850dbdbc73bSGuillaume Nault 1851dbdbc73bSGuillaume Nault return ERR_PTR(err); 1852dbdbc73bSGuillaume Nault } 1853dbdbc73bSGuillaume Nault 1854fd558d18SJames Chapman /* Bump the reference count. The session context is deleted 1855fd558d18SJames Chapman * only when this drops to zero. 1856fd558d18SJames Chapman */ 1857fd558d18SJames Chapman l2tp_session_inc_refcount(session); 1858fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1859fd558d18SJames Chapman 1860fd558d18SJames Chapman /* Ensure tunnel socket isn't deleted */ 1861fd558d18SJames Chapman sock_hold(tunnel->sock); 1862fd558d18SJames Chapman 1863fd558d18SJames Chapman /* Ignore management session in session count value */ 1864fd558d18SJames Chapman if (session->session_id != 0) 1865fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1866fd558d18SJames Chapman 1867fd558d18SJames Chapman return session; 1868fd558d18SJames Chapman } 1869dbdbc73bSGuillaume Nault 1870dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM); 1871dbdbc73bSGuillaume Nault } 1872fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1873fd558d18SJames Chapman 1874fd558d18SJames Chapman /***************************************************************************** 1875fd558d18SJames Chapman * Init and cleanup 1876fd558d18SJames Chapman *****************************************************************************/ 1877fd558d18SJames Chapman 1878fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1879fd558d18SJames Chapman { 1880e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1881f7faffa3SJames Chapman int hash; 1882fd558d18SJames Chapman 1883fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1884e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1885fd558d18SJames Chapman 1886f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1887f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1888f7faffa3SJames Chapman 1889e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1890f7faffa3SJames Chapman 1891fd558d18SJames Chapman return 0; 1892fd558d18SJames Chapman } 1893fd558d18SJames Chapman 1894167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1895167eb17eSTom Parkin { 1896167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1897167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1898167eb17eSTom Parkin 1899167eb17eSTom Parkin rcu_read_lock_bh(); 1900167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 1901167eb17eSTom Parkin (void)l2tp_tunnel_delete(tunnel); 1902167eb17eSTom Parkin } 1903167eb17eSTom Parkin rcu_read_unlock_bh(); 19042f86953eSSabrina Dubroca 19052f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 19062f86953eSSabrina Dubroca rcu_barrier(); 1907167eb17eSTom Parkin } 1908167eb17eSTom Parkin 1909fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1910fd558d18SJames Chapman .init = l2tp_init_net, 1911167eb17eSTom Parkin .exit = l2tp_exit_net, 1912fd558d18SJames Chapman .id = &l2tp_net_id, 1913fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1914fd558d18SJames Chapman }; 1915fd558d18SJames Chapman 1916fd558d18SJames Chapman static int __init l2tp_init(void) 1917fd558d18SJames Chapman { 1918fd558d18SJames Chapman int rc = 0; 1919fd558d18SJames Chapman 1920fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1921fd558d18SJames Chapman if (rc) 1922fd558d18SJames Chapman goto out; 1923fd558d18SJames Chapman 192459ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1925f8ccac0eSTom Parkin if (!l2tp_wq) { 1926f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 192767e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1928f8ccac0eSTom Parkin rc = -ENOMEM; 1929f8ccac0eSTom Parkin goto out; 1930f8ccac0eSTom Parkin } 1931f8ccac0eSTom Parkin 1932a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1933fd558d18SJames Chapman 1934fd558d18SJames Chapman out: 1935fd558d18SJames Chapman return rc; 1936fd558d18SJames Chapman } 1937fd558d18SJames Chapman 1938fd558d18SJames Chapman static void __exit l2tp_exit(void) 1939fd558d18SJames Chapman { 1940fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1941f8ccac0eSTom Parkin if (l2tp_wq) { 1942f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1943f8ccac0eSTom Parkin l2tp_wq = NULL; 1944f8ccac0eSTom Parkin } 1945fd558d18SJames Chapman } 1946fd558d18SJames Chapman 1947fd558d18SJames Chapman module_init(l2tp_init); 1948fd558d18SJames Chapman module_exit(l2tp_exit); 1949fd558d18SJames Chapman 1950fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1951fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1952fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1953fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1954fd558d18SJames Chapman 1955