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 123fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(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 220f7faffa3SJames Chapman /* Lookup a session by id in the global session list 221f7faffa3SJames Chapman */ 222f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id) 223f7faffa3SJames Chapman { 224f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 225f7faffa3SJames Chapman struct hlist_head *session_list = 226f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id); 227f7faffa3SJames Chapman struct l2tp_session *session; 228f7faffa3SJames Chapman 229e02d494dSJames Chapman rcu_read_lock_bh(); 230b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, session_list, global_hlist) { 231f7faffa3SJames Chapman if (session->session_id == session_id) { 232e02d494dSJames Chapman rcu_read_unlock_bh(); 233f7faffa3SJames Chapman return session; 234f7faffa3SJames Chapman } 235f7faffa3SJames Chapman } 236e02d494dSJames Chapman rcu_read_unlock_bh(); 237f7faffa3SJames Chapman 238f7faffa3SJames Chapman return NULL; 239f7faffa3SJames Chapman } 240f7faffa3SJames Chapman 241fd558d18SJames Chapman /* Session hash list. 242fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 243fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 244fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 245fd558d18SJames Chapman * simple bitmask. 246fd558d18SJames Chapman */ 247fd558d18SJames Chapman static inline struct hlist_head * 248fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 249fd558d18SJames Chapman { 250fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 251fd558d18SJames Chapman } 252fd558d18SJames Chapman 253fd558d18SJames Chapman /* Lookup a session by id 254fd558d18SJames Chapman */ 255f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id) 256fd558d18SJames Chapman { 257f7faffa3SJames Chapman struct hlist_head *session_list; 258fd558d18SJames Chapman struct l2tp_session *session; 259fd558d18SJames Chapman 260f7faffa3SJames Chapman /* In L2TPv3, session_ids are unique over all tunnels and we 261f7faffa3SJames Chapman * sometimes need to look them up before we know the 262f7faffa3SJames Chapman * tunnel. 263f7faffa3SJames Chapman */ 264f7faffa3SJames Chapman if (tunnel == NULL) 265f7faffa3SJames Chapman return l2tp_session_find_2(net, session_id); 266f7faffa3SJames Chapman 267f7faffa3SJames Chapman session_list = l2tp_session_id_hash(tunnel, session_id); 268fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 269b67bfe0dSSasha Levin hlist_for_each_entry(session, session_list, hlist) { 270fd558d18SJames Chapman if (session->session_id == session_id) { 271fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 272fd558d18SJames Chapman return session; 273fd558d18SJames Chapman } 274fd558d18SJames Chapman } 275fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 276fd558d18SJames Chapman 277fd558d18SJames Chapman return NULL; 278fd558d18SJames Chapman } 279fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find); 280fd558d18SJames Chapman 281fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth) 282fd558d18SJames Chapman { 283fd558d18SJames Chapman int hash; 284fd558d18SJames Chapman struct l2tp_session *session; 285fd558d18SJames Chapman int count = 0; 286fd558d18SJames Chapman 287fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 288fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 289b67bfe0dSSasha Levin hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { 290fd558d18SJames Chapman if (++count > nth) { 291fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 292fd558d18SJames Chapman return session; 293fd558d18SJames Chapman } 294fd558d18SJames Chapman } 295fd558d18SJames Chapman } 296fd558d18SJames Chapman 297fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 298fd558d18SJames Chapman 299fd558d18SJames Chapman return NULL; 300fd558d18SJames Chapman } 301fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth); 302fd558d18SJames Chapman 303309795f4SJames Chapman /* Lookup a session by interface name. 304309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 305309795f4SJames Chapman */ 306309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) 307309795f4SJames Chapman { 308309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 309309795f4SJames Chapman int hash; 310309795f4SJames Chapman struct l2tp_session *session; 311309795f4SJames Chapman 312e02d494dSJames Chapman rcu_read_lock_bh(); 313309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 314b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { 315309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 316e02d494dSJames Chapman rcu_read_unlock_bh(); 317309795f4SJames Chapman return session; 318309795f4SJames Chapman } 319309795f4SJames Chapman } 320309795f4SJames Chapman } 321309795f4SJames Chapman 322e02d494dSJames Chapman rcu_read_unlock_bh(); 323309795f4SJames Chapman 324309795f4SJames Chapman return NULL; 325309795f4SJames Chapman } 326309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname); 327309795f4SJames Chapman 328fd558d18SJames Chapman /* Lookup a tunnel by id 329fd558d18SJames Chapman */ 330fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id) 331fd558d18SJames Chapman { 332fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 333fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 334fd558d18SJames Chapman 335e02d494dSJames Chapman rcu_read_lock_bh(); 336e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 337fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 338e02d494dSJames Chapman rcu_read_unlock_bh(); 339fd558d18SJames Chapman return tunnel; 340fd558d18SJames Chapman } 341fd558d18SJames Chapman } 342e02d494dSJames Chapman rcu_read_unlock_bh(); 343fd558d18SJames Chapman 344fd558d18SJames Chapman return NULL; 345fd558d18SJames Chapman } 346fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 347fd558d18SJames Chapman 348fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth) 349fd558d18SJames Chapman { 350fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 351fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 352fd558d18SJames Chapman int count = 0; 353fd558d18SJames Chapman 354e02d494dSJames Chapman rcu_read_lock_bh(); 355e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 356fd558d18SJames Chapman if (++count > nth) { 357e02d494dSJames Chapman rcu_read_unlock_bh(); 358fd558d18SJames Chapman return tunnel; 359fd558d18SJames Chapman } 360fd558d18SJames Chapman } 361fd558d18SJames Chapman 362e02d494dSJames Chapman rcu_read_unlock_bh(); 363fd558d18SJames Chapman 364fd558d18SJames Chapman return NULL; 365fd558d18SJames Chapman } 366fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 367fd558d18SJames Chapman 368fd558d18SJames Chapman /***************************************************************************** 369fd558d18SJames Chapman * Receive data handling 370fd558d18SJames Chapman *****************************************************************************/ 371fd558d18SJames Chapman 372fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 373fd558d18SJames Chapman * number. 374fd558d18SJames Chapman */ 375fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 376fd558d18SJames Chapman { 377fd558d18SJames Chapman struct sk_buff *skbp; 378fd558d18SJames Chapman struct sk_buff *tmp; 379f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 380fd558d18SJames Chapman 381fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 382fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 383fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 384fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 385a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 386fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 387fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 388fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 3897b7c0719STom Parkin atomic_long_inc(&session->stats.rx_oos_packets); 390fd558d18SJames Chapman goto out; 391fd558d18SJames Chapman } 392fd558d18SJames Chapman } 393fd558d18SJames Chapman 394fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 395fd558d18SJames Chapman 396fd558d18SJames Chapman out: 397fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 398fd558d18SJames Chapman } 399fd558d18SJames Chapman 400fd558d18SJames Chapman /* Dequeue a single skb. 401fd558d18SJames Chapman */ 402fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 403fd558d18SJames Chapman { 404fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 405fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 406fd558d18SJames Chapman 407fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 408fd558d18SJames Chapman * to its current owner (a socket receive buffer). 409fd558d18SJames Chapman */ 410fd558d18SJames Chapman skb_orphan(skb); 411fd558d18SJames Chapman 4127b7c0719STom Parkin atomic_long_inc(&tunnel->stats.rx_packets); 4137b7c0719STom Parkin atomic_long_add(length, &tunnel->stats.rx_bytes); 4147b7c0719STom Parkin atomic_long_inc(&session->stats.rx_packets); 4157b7c0719STom Parkin atomic_long_add(length, &session->stats.rx_bytes); 416fd558d18SJames Chapman 417fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 418fd558d18SJames Chapman /* Bump our Nr */ 419fd558d18SJames Chapman session->nr++; 4208a1631d5SJames Chapman session->nr &= session->nr_max; 421f7faffa3SJames Chapman 422a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 423a4ca44faSJoe Perches session->name, session->nr); 424fd558d18SJames Chapman } 425fd558d18SJames Chapman 426fd558d18SJames Chapman /* call private receive handler */ 427fd558d18SJames Chapman if (session->recv_skb != NULL) 428fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 429fd558d18SJames Chapman else 430fd558d18SJames Chapman kfree_skb(skb); 431fd558d18SJames Chapman 432fd558d18SJames Chapman if (session->deref) 433fd558d18SJames Chapman (*session->deref)(session); 434fd558d18SJames Chapman } 435fd558d18SJames Chapman 436fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 437fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 438fd558d18SJames Chapman */ 439fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 440fd558d18SJames Chapman { 441fd558d18SJames Chapman struct sk_buff *skb; 442fd558d18SJames Chapman struct sk_buff *tmp; 443fd558d18SJames Chapman 444fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 445fd558d18SJames Chapman * expect to send up next, dequeue it and any other 446fd558d18SJames Chapman * in-sequence packets behind it. 447fd558d18SJames Chapman */ 448e2e210c0SEric Dumazet start: 449fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 450fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 451fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 4527b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 4537b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 454a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 455a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 456fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 457fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 458fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 45938d40b3fSJames Chapman session->reorder_skip = 1; 460fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 461fd558d18SJames Chapman kfree_skb(skb); 462fd558d18SJames Chapman if (session->deref) 463fd558d18SJames Chapman (*session->deref)(session); 464fd558d18SJames Chapman continue; 465fd558d18SJames Chapman } 466fd558d18SJames Chapman 467fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 46838d40b3fSJames Chapman if (session->reorder_skip) { 469a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 47038d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 47138d40b3fSJames Chapman session->name, session->nr, 47238d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 47338d40b3fSJames Chapman session->reorder_skip = 0; 47438d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 47538d40b3fSJames Chapman } 476fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 477a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 478a4ca44faSJoe Perches "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 479fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 480fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 481fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 482fd558d18SJames Chapman goto out; 483fd558d18SJames Chapman } 484fd558d18SJames Chapman } 485fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 486fd558d18SJames Chapman 487fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 488fd558d18SJames Chapman * do so to let other contexts process the queue. 489fd558d18SJames Chapman */ 490fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 491fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 492e2e210c0SEric Dumazet goto start; 493fd558d18SJames Chapman } 494fd558d18SJames Chapman 495fd558d18SJames Chapman out: 496fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 497fd558d18SJames Chapman } 498fd558d18SJames Chapman 4998a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) 5008a1631d5SJames Chapman { 5018a1631d5SJames Chapman u32 nws; 5028a1631d5SJames Chapman 5038a1631d5SJames Chapman if (nr >= session->nr) 5048a1631d5SJames Chapman nws = nr - session->nr; 5058a1631d5SJames Chapman else 5068a1631d5SJames Chapman nws = (session->nr_max + 1) - (session->nr - nr); 5078a1631d5SJames Chapman 5088a1631d5SJames Chapman return nws < session->nr_window_size; 5098a1631d5SJames Chapman } 5108a1631d5SJames Chapman 511b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if 512b6dc01a4SJames Chapman * acceptable, else non-zero. 513b6dc01a4SJames Chapman */ 514b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) 515b6dc01a4SJames Chapman { 5168a1631d5SJames Chapman if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { 5178a1631d5SJames Chapman /* Packet sequence number is outside allowed window. 5188a1631d5SJames Chapman * Discard it. 5198a1631d5SJames Chapman */ 5208a1631d5SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 5218a1631d5SJames Chapman "%s: pkt %u len %d discarded, outside window, nr=%u\n", 5228a1631d5SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 5238a1631d5SJames Chapman L2TP_SKB_CB(skb)->length, session->nr); 5248a1631d5SJames Chapman goto discard; 5258a1631d5SJames Chapman } 5268a1631d5SJames Chapman 527b6dc01a4SJames Chapman if (session->reorder_timeout != 0) { 528b6dc01a4SJames Chapman /* Packet reordering enabled. Add skb to session's 529b6dc01a4SJames Chapman * reorder queue, in order of ns. 530b6dc01a4SJames Chapman */ 531b6dc01a4SJames Chapman l2tp_recv_queue_skb(session, skb); 532a0dbd822SJames Chapman goto out; 533a0dbd822SJames Chapman } 534a0dbd822SJames Chapman 535a0dbd822SJames Chapman /* Packet reordering disabled. Discard out-of-sequence packets, while 536a0dbd822SJames Chapman * tracking the number if in-sequence packets after the first OOS packet 537a0dbd822SJames Chapman * is seen. After nr_oos_count_max in-sequence packets, reset the 538a0dbd822SJames Chapman * sequence number to re-enable packet reception. 539b6dc01a4SJames Chapman */ 540a0dbd822SJames Chapman if (L2TP_SKB_CB(skb)->ns == session->nr) { 541a0dbd822SJames Chapman skb_queue_tail(&session->reorder_q, skb); 542a0dbd822SJames Chapman } else { 543a0dbd822SJames Chapman u32 nr_oos = L2TP_SKB_CB(skb)->ns; 544a0dbd822SJames Chapman u32 nr_next = (session->nr_oos + 1) & session->nr_max; 545a0dbd822SJames Chapman 546a0dbd822SJames Chapman if (nr_oos == nr_next) 547a0dbd822SJames Chapman session->nr_oos_count++; 548a0dbd822SJames Chapman else 549a0dbd822SJames Chapman session->nr_oos_count = 0; 550a0dbd822SJames Chapman 551a0dbd822SJames Chapman session->nr_oos = nr_oos; 552a0dbd822SJames Chapman if (session->nr_oos_count > session->nr_oos_count_max) { 553a0dbd822SJames Chapman session->reorder_skip = 1; 554a0dbd822SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 555a0dbd822SJames Chapman "%s: %d oos packets received. Resetting sequence numbers\n", 556a0dbd822SJames Chapman session->name, session->nr_oos_count); 557a0dbd822SJames Chapman } 558a0dbd822SJames Chapman if (!session->reorder_skip) { 559b6dc01a4SJames Chapman atomic_long_inc(&session->stats.rx_seq_discards); 560b6dc01a4SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 561b6dc01a4SJames Chapman "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 562b6dc01a4SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 563b6dc01a4SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 564b6dc01a4SJames Chapman skb_queue_len(&session->reorder_q)); 565b6dc01a4SJames Chapman goto discard; 566b6dc01a4SJames Chapman } 567b6dc01a4SJames Chapman skb_queue_tail(&session->reorder_q, skb); 568b6dc01a4SJames Chapman } 569b6dc01a4SJames Chapman 570a0dbd822SJames Chapman out: 571b6dc01a4SJames Chapman return 0; 572b6dc01a4SJames Chapman 573b6dc01a4SJames Chapman discard: 574b6dc01a4SJames Chapman return 1; 575b6dc01a4SJames Chapman } 576b6dc01a4SJames Chapman 577f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 578f7faffa3SJames Chapman * and L2TPv3 data frames here. 579f7faffa3SJames Chapman * 580f7faffa3SJames Chapman * L2TPv2 Data Message Header 581f7faffa3SJames Chapman * 582f7faffa3SJames Chapman * 0 1 2 3 583f7faffa3SJames 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 584f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 585f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 586f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 587f7faffa3SJames Chapman * | Tunnel ID | Session ID | 588f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 589f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 590f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 591f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 592f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 593f7faffa3SJames Chapman * 594f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 595f7faffa3SJames Chapman * those in L2TP control frames. 596f7faffa3SJames Chapman * 597f7faffa3SJames Chapman * L2TPv3 Data Message Header 598f7faffa3SJames Chapman * 599f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 600f7faffa3SJames Chapman * | L2TP Session Header | 601f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 602f7faffa3SJames Chapman * | L2-Specific Sublayer | 603f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 604f7faffa3SJames Chapman * | Tunnel Payload ... 605f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 606f7faffa3SJames Chapman * 607f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 608f7faffa3SJames Chapman * 609f7faffa3SJames Chapman * 0 1 2 3 610f7faffa3SJames 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 611f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 612f7faffa3SJames Chapman * | Session ID | 613f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 614f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 615f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 616f7faffa3SJames Chapman * | 617f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 618f7faffa3SJames Chapman * 619f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 620f7faffa3SJames Chapman * 621f7faffa3SJames Chapman * 0 1 2 3 622f7faffa3SJames 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 623f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 624f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 625f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 626f7faffa3SJames Chapman * 627f7faffa3SJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with 628f7faffa3SJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need 629f7faffa3SJames Chapman * to parse the packet header to determine if optional fields are 630f7faffa3SJames Chapman * present. 631f7faffa3SJames Chapman * 632f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 633f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 634f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 635f7faffa3SJames Chapman * after the session-id. 636f7faffa3SJames Chapman */ 637f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 638f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 639f7faffa3SJames Chapman int length, int (*payload_hook)(struct sk_buff *skb)) 640f7faffa3SJames Chapman { 641f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 642f7faffa3SJames Chapman int offset; 643f7faffa3SJames Chapman u32 ns, nr; 644f7faffa3SJames Chapman 645f7faffa3SJames Chapman /* The ref count is increased since we now hold a pointer to 646f7faffa3SJames Chapman * the session. Take care to decrement the refcnt when exiting 647f7faffa3SJames Chapman * this function from now on... 648f7faffa3SJames Chapman */ 649f7faffa3SJames Chapman l2tp_session_inc_refcount(session); 650f7faffa3SJames Chapman if (session->ref) 651f7faffa3SJames Chapman (*session->ref)(session); 652f7faffa3SJames Chapman 653f7faffa3SJames Chapman /* Parse and check optional cookie */ 654f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 655f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 656a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 657f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 658a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 659a4ca44faSJoe Perches session->session_id); 6607b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards); 661f7faffa3SJames Chapman goto discard; 662f7faffa3SJames Chapman } 663f7faffa3SJames Chapman ptr += session->peer_cookie_len; 664f7faffa3SJames Chapman } 665f7faffa3SJames Chapman 666f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 667f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 668f7faffa3SJames Chapman * 669f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 670f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 671f7faffa3SJames Chapman * we were expecting them, discard frame. 672f7faffa3SJames Chapman */ 673f7faffa3SJames Chapman ns = nr = 0; 674f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 675f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 676f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 677f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 678f7faffa3SJames Chapman ptr += 2; 679f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 680f7faffa3SJames Chapman ptr += 2; 681f7faffa3SJames Chapman 682f7faffa3SJames Chapman /* Store L2TP info in the skb */ 683f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 684f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 685f7faffa3SJames Chapman 686a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 687f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 688f7faffa3SJames Chapman session->name, ns, nr, session->nr); 689f7faffa3SJames Chapman } 690f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 691f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 692f7faffa3SJames Chapman 693f7faffa3SJames Chapman if (l2h & 0x40000000) { 694f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 695f7faffa3SJames Chapman 696f7faffa3SJames Chapman /* Store L2TP info in the skb */ 697f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 698f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 699f7faffa3SJames Chapman 700a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 701f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 702f7faffa3SJames Chapman session->name, ns, session->nr); 703f7faffa3SJames Chapman } 704f7faffa3SJames Chapman } 705f7faffa3SJames Chapman 706f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 707f7faffa3SJames Chapman ptr += session->l2specific_len; 708f7faffa3SJames Chapman 709f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 710f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 711f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 712f7faffa3SJames Chapman * configure it so. 713f7faffa3SJames Chapman */ 714f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 715a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 716f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 717f7faffa3SJames Chapman session->name); 7183f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1; 719f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 720f7faffa3SJames Chapman } 721f7faffa3SJames Chapman } else { 722f7faffa3SJames Chapman /* No sequence numbers. 723f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 724f7faffa3SJames Chapman */ 725f7faffa3SJames Chapman if (session->recv_seq) { 726a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 727a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 728a4ca44faSJoe Perches session->name); 7297b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 730f7faffa3SJames Chapman goto discard; 731f7faffa3SJames Chapman } 732f7faffa3SJames Chapman 733f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 734f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 735f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 736f7faffa3SJames Chapman * LAC is broken. Discard the frame. 737f7faffa3SJames Chapman */ 738f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 739a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 740f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 741f7faffa3SJames Chapman session->name); 742f7faffa3SJames Chapman session->send_seq = 0; 743f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 744f7faffa3SJames Chapman } else if (session->send_seq) { 745a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 746a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 747a4ca44faSJoe Perches session->name); 7487b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 749f7faffa3SJames Chapman goto discard; 750f7faffa3SJames Chapman } 751f7faffa3SJames Chapman } 752f7faffa3SJames Chapman 753f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 754f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 755f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 756f7faffa3SJames Chapman * in the session setup control protocol. 757f7faffa3SJames Chapman */ 758f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 759f7faffa3SJames Chapman /* If offset bit set, skip it. */ 760f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 761f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 762f7faffa3SJames Chapman ptr += 2 + offset; 763f7faffa3SJames Chapman } 764f7faffa3SJames Chapman } else 765f7faffa3SJames Chapman ptr += session->offset; 766f7faffa3SJames Chapman 767f7faffa3SJames Chapman offset = ptr - optr; 768f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 769f7faffa3SJames Chapman goto discard; 770f7faffa3SJames Chapman 771f7faffa3SJames Chapman __skb_pull(skb, offset); 772f7faffa3SJames Chapman 773f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 774f7faffa3SJames Chapman * packet, do so now. 775f7faffa3SJames Chapman */ 776f7faffa3SJames Chapman if (payload_hook) 777f7faffa3SJames Chapman if ((*payload_hook)(skb)) 778f7faffa3SJames Chapman goto discard; 779f7faffa3SJames Chapman 780f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 781f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 782f7faffa3SJames Chapman * reordering. 783f7faffa3SJames Chapman */ 784f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 785f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 786f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 787f7faffa3SJames Chapman 788f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 789f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 790f7faffa3SJames Chapman */ 791f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 792b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb)) 793f7faffa3SJames Chapman goto discard; 794f7faffa3SJames Chapman } else { 795f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 796f7faffa3SJames Chapman * reorder queue. This ensures that it will be 797f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 798f7faffa3SJames Chapman */ 799f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 800f7faffa3SJames Chapman } 801f7faffa3SJames Chapman 802f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 803f7faffa3SJames Chapman l2tp_recv_dequeue(session); 804f7faffa3SJames Chapman 805f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 806f7faffa3SJames Chapman 807f7faffa3SJames Chapman return; 808f7faffa3SJames Chapman 809f7faffa3SJames Chapman discard: 8107b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 811f7faffa3SJames Chapman kfree_skb(skb); 812f7faffa3SJames Chapman 813f7faffa3SJames Chapman if (session->deref) 814f7faffa3SJames Chapman (*session->deref)(session); 815f7faffa3SJames Chapman 816f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 817f7faffa3SJames Chapman } 818f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common); 819f7faffa3SJames Chapman 82048f72f92STom Parkin /* Drop skbs from the session's reorder_q 82148f72f92STom Parkin */ 82248f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session) 82348f72f92STom Parkin { 82448f72f92STom Parkin struct sk_buff *skb = NULL; 82548f72f92STom Parkin BUG_ON(!session); 82648f72f92STom Parkin BUG_ON(session->magic != L2TP_SESSION_MAGIC); 82748f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) { 82848f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors); 82948f72f92STom Parkin kfree_skb(skb); 83048f72f92STom Parkin if (session->deref) 83148f72f92STom Parkin (*session->deref)(session); 83248f72f92STom Parkin } 83348f72f92STom Parkin return 0; 83448f72f92STom Parkin } 83548f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); 83648f72f92STom Parkin 837fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 838fd558d18SJames Chapman * here. The skb is not on a list when we get here. 839fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 840fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 841fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 842fd558d18SJames Chapman */ 843fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 844fd558d18SJames Chapman int (*payload_hook)(struct sk_buff *skb)) 845fd558d18SJames Chapman { 846fd558d18SJames Chapman struct l2tp_session *session = NULL; 847fd558d18SJames Chapman unsigned char *ptr, *optr; 848fd558d18SJames Chapman u16 hdrflags; 849fd558d18SJames Chapman u32 tunnel_id, session_id; 850fd558d18SJames Chapman u16 version; 851f7faffa3SJames Chapman int length; 852fd558d18SJames Chapman 85358d6085cSTom Herbert /* UDP has verifed checksum */ 854fd558d18SJames Chapman 855fd558d18SJames Chapman /* UDP always verifies the packet length. */ 856fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 857fd558d18SJames Chapman 858fd558d18SJames Chapman /* Short packet? */ 859fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 860a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 861a4ca44faSJoe Perches "%s: recv short packet (len=%d)\n", 862a4ca44faSJoe Perches tunnel->name, skb->len); 863fd558d18SJames Chapman goto error; 864fd558d18SJames Chapman } 865fd558d18SJames Chapman 866fd558d18SJames Chapman /* Trace packet contents, if enabled */ 867fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 868fd558d18SJames Chapman length = min(32u, skb->len); 869fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 870fd558d18SJames Chapman goto error; 871fd558d18SJames Chapman 872a4ca44faSJoe Perches pr_debug("%s: recv\n", tunnel->name); 873a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 874fd558d18SJames Chapman } 875fd558d18SJames Chapman 876e50e705cSEric Dumazet /* Point to L2TP header */ 877e50e705cSEric Dumazet optr = ptr = skb->data; 878e50e705cSEric Dumazet 879fd558d18SJames Chapman /* Get L2TP header flags */ 880fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 881fd558d18SJames Chapman 882fd558d18SJames Chapman /* Check protocol version */ 883fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 884fd558d18SJames Chapman if (version != tunnel->version) { 885a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 886fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 887fd558d18SJames Chapman tunnel->name, version, tunnel->version); 888fd558d18SJames Chapman goto error; 889fd558d18SJames Chapman } 890fd558d18SJames Chapman 891fd558d18SJames Chapman /* Get length of L2TP packet */ 892fd558d18SJames Chapman length = skb->len; 893fd558d18SJames Chapman 894fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 895fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 896a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, 897a4ca44faSJoe Perches "%s: recv control packet, len=%d\n", 898a4ca44faSJoe Perches tunnel->name, length); 899fd558d18SJames Chapman goto error; 900fd558d18SJames Chapman } 901fd558d18SJames Chapman 902fd558d18SJames Chapman /* Skip flags */ 903fd558d18SJames Chapman ptr += 2; 904fd558d18SJames Chapman 905f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 906fd558d18SJames Chapman /* If length is present, skip it */ 907fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 908fd558d18SJames Chapman ptr += 2; 909fd558d18SJames Chapman 910fd558d18SJames Chapman /* Extract tunnel and session ID */ 911fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 912fd558d18SJames Chapman ptr += 2; 913fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 914fd558d18SJames Chapman ptr += 2; 915f7faffa3SJames Chapman } else { 916f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 917f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 918f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 919f7faffa3SJames Chapman ptr += 4; 920f7faffa3SJames Chapman } 921fd558d18SJames Chapman 922fd558d18SJames Chapman /* Find the session context */ 923f7faffa3SJames Chapman session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); 924309795f4SJames Chapman if (!session || !session->recv_skb) { 925fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 926a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 927f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 928fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 929fd558d18SJames Chapman goto error; 930fd558d18SJames Chapman } 931fd558d18SJames Chapman 932f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 933fd558d18SJames Chapman 934fd558d18SJames Chapman return 0; 935fd558d18SJames Chapman 936fd558d18SJames Chapman error: 937fd558d18SJames Chapman /* Put UDP header back */ 938fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 939fd558d18SJames Chapman 940fd558d18SJames Chapman return 1; 941fd558d18SJames Chapman } 942fd558d18SJames Chapman 943fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 944fd558d18SJames Chapman * Return codes: 945fd558d18SJames Chapman * 0 : success. 946fd558d18SJames Chapman * <0: error 947fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 948fd558d18SJames Chapman */ 949fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 950fd558d18SJames Chapman { 951fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 952fd558d18SJames Chapman 953fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 954fd558d18SJames Chapman if (tunnel == NULL) 955fd558d18SJames Chapman goto pass_up; 956fd558d18SJames Chapman 957a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 958a4ca44faSJoe Perches tunnel->name, skb->len); 959fd558d18SJames Chapman 960fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 961fd558d18SJames Chapman goto pass_up_put; 962fd558d18SJames Chapman 963fd558d18SJames Chapman sock_put(sk); 964fd558d18SJames Chapman return 0; 965fd558d18SJames Chapman 966fd558d18SJames Chapman pass_up_put: 967fd558d18SJames Chapman sock_put(sk); 968fd558d18SJames Chapman pass_up: 969fd558d18SJames Chapman return 1; 970fd558d18SJames Chapman } 971fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 972fd558d18SJames Chapman 973fd558d18SJames Chapman /************************************************************************ 974fd558d18SJames Chapman * Transmit handling 975fd558d18SJames Chapman ***********************************************************************/ 976fd558d18SJames Chapman 977fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 978fd558d18SJames Chapman */ 979f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 980fd558d18SJames Chapman { 981f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 982fd558d18SJames Chapman __be16 *bufp = buf; 983f7faffa3SJames Chapman __be16 *optr = buf; 984fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 985fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 986fd558d18SJames Chapman u32 session_id = session->peer_session_id; 987fd558d18SJames Chapman 988fd558d18SJames Chapman if (session->send_seq) 989fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 990fd558d18SJames Chapman 991fd558d18SJames Chapman /* Setup L2TP header. */ 992fd558d18SJames Chapman *bufp++ = htons(flags); 993fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 994fd558d18SJames Chapman *bufp++ = htons(session_id); 995fd558d18SJames Chapman if (session->send_seq) { 996fd558d18SJames Chapman *bufp++ = htons(session->ns); 997fd558d18SJames Chapman *bufp++ = 0; 998fd558d18SJames Chapman session->ns++; 999f7faffa3SJames Chapman session->ns &= 0xffff; 1000a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 1001a4ca44faSJoe Perches session->name, session->ns); 1002fd558d18SJames Chapman } 1003fd558d18SJames Chapman 1004f7faffa3SJames Chapman return bufp - optr; 1005f7faffa3SJames Chapman } 1006f7faffa3SJames Chapman 1007f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 1008fd558d18SJames Chapman { 10090d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1010f7faffa3SJames Chapman char *bufp = buf; 1011f7faffa3SJames Chapman char *optr = bufp; 1012fd558d18SJames Chapman 10130d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 10140d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 10150d76751fSJames Chapman */ 10160d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 10170d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 1018f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 1019f7faffa3SJames Chapman bufp += 2; 1020f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 1021f7faffa3SJames Chapman bufp += 2; 10220d76751fSJames Chapman } 10230d76751fSJames Chapman 1024f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 1025f7faffa3SJames Chapman bufp += 4; 1026f7faffa3SJames Chapman if (session->cookie_len) { 1027f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 1028f7faffa3SJames Chapman bufp += session->cookie_len; 1029fd558d18SJames Chapman } 1030f7faffa3SJames Chapman if (session->l2specific_len) { 1031f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1032f7faffa3SJames Chapman u32 l2h = 0; 1033f7faffa3SJames Chapman if (session->send_seq) { 1034f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1035f7faffa3SJames Chapman session->ns++; 1036f7faffa3SJames Chapman session->ns &= 0xffffff; 1037a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 1038a4ca44faSJoe Perches "%s: updated ns to %u\n", 1039a4ca44faSJoe Perches session->name, session->ns); 1040f7faffa3SJames Chapman } 1041f7faffa3SJames Chapman 1042f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 1043f7faffa3SJames Chapman } 1044f7faffa3SJames Chapman bufp += session->l2specific_len; 1045f7faffa3SJames Chapman } 1046f7faffa3SJames Chapman if (session->offset) 1047f7faffa3SJames Chapman bufp += session->offset; 1048f7faffa3SJames Chapman 1049f7faffa3SJames Chapman return bufp - optr; 1050f7faffa3SJames Chapman } 1051fd558d18SJames Chapman 1052fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1053d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1054fd558d18SJames Chapman { 1055fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1056fd558d18SJames Chapman unsigned int len = skb->len; 1057fd558d18SJames Chapman int error; 1058fd558d18SJames Chapman 1059fd558d18SJames Chapman /* Debug */ 1060fd558d18SJames Chapman if (session->send_seq) 10615b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", 1062a4ca44faSJoe Perches session->name, data_len, session->ns - 1); 1063fd558d18SJames Chapman else 10645b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", 1065a4ca44faSJoe Perches session->name, data_len); 1066fd558d18SJames Chapman 1067fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 10680d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10690d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1070fd558d18SJames Chapman 1071a4ca44faSJoe Perches pr_debug("%s: xmit\n", session->name); 1072a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1073a4ca44faSJoe Perches datap, min_t(size_t, 32, len - uhlen)); 1074fd558d18SJames Chapman } 1075fd558d18SJames Chapman 1076fd558d18SJames Chapman /* Queue the packet to IP for output */ 107760ff7467SWANG Cong skb->ignore_df = 1; 1078d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1079746e3499SEric Dumazet if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped) 1080b0270e91SEric Dumazet error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1081d2cf3361SBenjamin LaHaise else 1082d2cf3361SBenjamin LaHaise #endif 1083b0270e91SEric Dumazet error = ip_queue_xmit(tunnel->sock, skb, fl); 1084fd558d18SJames Chapman 1085fd558d18SJames Chapman /* Update stats */ 1086fd558d18SJames Chapman if (error >= 0) { 10877b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_packets); 10887b7c0719STom Parkin atomic_long_add(len, &tunnel->stats.tx_bytes); 10897b7c0719STom Parkin atomic_long_inc(&session->stats.tx_packets); 10907b7c0719STom Parkin atomic_long_add(len, &session->stats.tx_bytes); 1091fd558d18SJames Chapman } else { 10927b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_errors); 10937b7c0719STom Parkin atomic_long_inc(&session->stats.tx_errors); 1094fd558d18SJames Chapman } 1095fd558d18SJames Chapman 1096fd558d18SJames Chapman return 0; 1097fd558d18SJames Chapman } 1098fd558d18SJames Chapman 1099fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1100fd558d18SJames Chapman * inserted in the skb data before calling this function. 1101fd558d18SJames Chapman */ 1102fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1103fd558d18SJames Chapman { 1104fd558d18SJames Chapman int data_len = skb->len; 11050d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 11060d76751fSJames Chapman struct sock *sk = tunnel->sock; 1107d9d8da80SDavid S. Miller struct flowi *fl; 1108fd558d18SJames Chapman struct udphdr *uh; 1109fd558d18SJames Chapman struct inet_sock *inet; 1110fd558d18SJames Chapman int headroom; 11110d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11120d76751fSJames Chapman int udp_len; 1113b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1114fd558d18SJames Chapman 1115fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1116fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1117fd558d18SJames Chapman * make room. Adjust truesize. 1118fd558d18SJames Chapman */ 1119fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11200d76751fSJames Chapman uhlen + hdr_len; 1121835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1122b8c84307SEric Dumazet kfree_skb(skb); 1123b8c84307SEric Dumazet return NET_XMIT_DROP; 1124835acf5dSEric Dumazet } 1125fd558d18SJames Chapman 1126fd558d18SJames Chapman /* Setup L2TP header */ 1127f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1128fd558d18SJames Chapman 11290d76751fSJames Chapman /* Reset skb netfilter state */ 1130fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1131fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1132fd558d18SJames Chapman IPSKB_REROUTED); 1133fd558d18SJames Chapman nf_reset(skb); 1134fd558d18SJames Chapman 11356af88da1SDavid S. Miller bh_lock_sock(sk); 11366af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1137b8c84307SEric Dumazet kfree_skb(skb); 1138b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11396af88da1SDavid S. Miller goto out_unlock; 11406af88da1SDavid S. Miller } 11416af88da1SDavid S. Miller 1142fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1143fd558d18SJames Chapman skb_dst_drop(skb); 114471b1391aSFlorian Westphal skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 11450d76751fSJames Chapman 1146d9d8da80SDavid S. Miller inet = inet_sk(sk); 1147d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11480d76751fSJames Chapman switch (tunnel->encap) { 11490d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 11500d76751fSJames Chapman /* Setup UDP header */ 11510d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11520d76751fSJames Chapman skb_reset_transport_header(skb); 11530d76751fSJames Chapman uh = udp_hdr(skb); 11540d76751fSJames Chapman uh->source = inet->inet_sport; 11550d76751fSJames Chapman uh->dest = inet->inet_dport; 11560d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11570d76751fSJames Chapman uh->len = htons(udp_len); 1158fd558d18SJames Chapman 1159fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1160d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1161e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) 116277157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 116377157e19STom Herbert skb, &inet6_sk(sk)->saddr, 116477157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1165d2cf3361SBenjamin LaHaise else 1166d2cf3361SBenjamin LaHaise #endif 116777157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 116877157e19STom Herbert inet->inet_daddr, udp_len); 11690d76751fSJames Chapman break; 11700d76751fSJames Chapman 11710d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11720d76751fSJames Chapman break; 11730d76751fSJames Chapman } 11740d76751fSJames Chapman 1175d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 11766af88da1SDavid S. Miller out_unlock: 11776af88da1SDavid S. Miller bh_unlock_sock(sk); 1178fd558d18SJames Chapman 1179b8c84307SEric Dumazet return ret; 1180fd558d18SJames Chapman } 1181fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1182fd558d18SJames Chapman 1183fd558d18SJames Chapman /***************************************************************************** 1184fd558d18SJames Chapman * Tinnel and session create/destroy. 1185fd558d18SJames Chapman *****************************************************************************/ 1186fd558d18SJames Chapman 1187fd558d18SJames Chapman /* Tunnel socket destruct hook. 1188fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1189fd558d18SJames Chapman * closed. 1190fd558d18SJames Chapman */ 1191fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1192fd558d18SJames Chapman { 11938d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1194f8ccac0eSTom Parkin struct l2tp_net *pn; 1195fd558d18SJames Chapman 1196fd558d18SJames Chapman if (tunnel == NULL) 1197fd558d18SJames Chapman goto end; 1198fd558d18SJames Chapman 1199a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1200fd558d18SJames Chapman 1201fd558d18SJames Chapman 1202f8ccac0eSTom Parkin /* Disable udp encapsulation */ 12030d76751fSJames Chapman switch (tunnel->encap) { 12040d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1205fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1206fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1207fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12089980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 12090d76751fSJames Chapman break; 12100d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12110d76751fSJames Chapman break; 12120d76751fSJames Chapman } 1213fd558d18SJames Chapman 1214fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1215fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1216fd558d18SJames Chapman sk->sk_user_data = NULL; 1217f8ccac0eSTom Parkin tunnel->sock = NULL; 1218f8ccac0eSTom Parkin 1219f8ccac0eSTom Parkin /* Remove the tunnel struct from the tunnel list */ 1220f8ccac0eSTom Parkin pn = l2tp_pernet(tunnel->l2tp_net); 1221f8ccac0eSTom Parkin spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1222f8ccac0eSTom Parkin list_del_rcu(&tunnel->list); 1223f8ccac0eSTom Parkin spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1224f8ccac0eSTom Parkin atomic_dec(&l2tp_tunnel_count); 1225f8ccac0eSTom Parkin 1226f8ccac0eSTom Parkin l2tp_tunnel_closeall(tunnel); 1227f8ccac0eSTom Parkin l2tp_tunnel_dec_refcount(tunnel); 1228fd558d18SJames Chapman 1229fd558d18SJames Chapman /* Call the original destructor */ 1230fd558d18SJames Chapman if (sk->sk_destruct) 1231fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1232fd558d18SJames Chapman end: 1233fd558d18SJames Chapman return; 1234fd558d18SJames Chapman } 1235fd558d18SJames Chapman 1236fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1237fd558d18SJames Chapman */ 1238e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1239fd558d18SJames Chapman { 1240fd558d18SJames Chapman int hash; 1241fd558d18SJames Chapman struct hlist_node *walk; 1242fd558d18SJames Chapman struct hlist_node *tmp; 1243fd558d18SJames Chapman struct l2tp_session *session; 1244fd558d18SJames Chapman 1245fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1246fd558d18SJames Chapman 1247a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1248a4ca44faSJoe Perches tunnel->name); 1249fd558d18SJames Chapman 1250fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1251fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1252fd558d18SJames Chapman again: 1253fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1254fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1255fd558d18SJames Chapman 1256a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1257fd558d18SJames Chapman "%s: closing session\n", session->name); 1258fd558d18SJames Chapman 1259fd558d18SJames Chapman hlist_del_init(&session->hlist); 1260fd558d18SJames Chapman 1261fd558d18SJames Chapman if (session->ref != NULL) 1262fd558d18SJames Chapman (*session->ref)(session); 1263fd558d18SJames Chapman 1264fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1265fd558d18SJames Chapman 1266f6e16b29STom Parkin __l2tp_session_unhash(session); 12674c6e2fd3STom Parkin l2tp_session_queue_purge(session); 12684c6e2fd3STom Parkin 1269fd558d18SJames Chapman if (session->session_close != NULL) 1270fd558d18SJames Chapman (*session->session_close)(session); 1271fd558d18SJames Chapman 1272fd558d18SJames Chapman if (session->deref != NULL) 1273fd558d18SJames Chapman (*session->deref)(session); 1274fd558d18SJames Chapman 12759980d001STom Parkin l2tp_session_dec_refcount(session); 12769980d001STom Parkin 1277fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1278fd558d18SJames Chapman 1279fd558d18SJames Chapman /* Now restart from the beginning of this hash 1280fd558d18SJames Chapman * chain. We always remove a session from the 1281fd558d18SJames Chapman * list so we are guaranteed to make forward 1282fd558d18SJames Chapman * progress. 1283fd558d18SJames Chapman */ 1284fd558d18SJames Chapman goto again; 1285fd558d18SJames Chapman } 1286fd558d18SJames Chapman } 1287fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1288fd558d18SJames Chapman } 1289e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); 1290fd558d18SJames Chapman 12919980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 12929980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 12939980d001STom Parkin { 12949980d001STom Parkin struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 12959980d001STom Parkin if (tunnel) { 12969980d001STom Parkin l2tp_tunnel_closeall(tunnel); 12979980d001STom Parkin sock_put(sk); 12989980d001STom Parkin } 12999980d001STom Parkin } 13009980d001STom Parkin 1301fd558d18SJames Chapman /* Really kill the tunnel. 1302fd558d18SJames Chapman * Come here only when all sessions have been cleared from the tunnel. 1303fd558d18SJames Chapman */ 1304fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 1305fd558d18SJames Chapman { 1306fd558d18SJames Chapman BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1307fd558d18SJames Chapman BUG_ON(tunnel->sock != NULL); 1308a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name); 130999469c32Sxeb@mail.ru kfree_rcu(tunnel, rcu); 1310f8ccac0eSTom Parkin } 1311fd558d18SJames Chapman 1312f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1313f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1314f8ccac0eSTom Parkin { 1315f8ccac0eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1316f8ccac0eSTom Parkin struct socket *sock = NULL; 1317f8ccac0eSTom Parkin struct sock *sk = NULL; 1318f8ccac0eSTom Parkin 1319f8ccac0eSTom Parkin tunnel = container_of(work, struct l2tp_tunnel, del_work); 132012d656afSRidge Kennedy 132112d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 132212d656afSRidge Kennedy 1323f8ccac0eSTom Parkin sk = l2tp_tunnel_sock_lookup(tunnel); 1324f8ccac0eSTom Parkin if (!sk) 132506a15f51SAlexander Couzens goto out; 1326f8ccac0eSTom Parkin 1327f8ccac0eSTom Parkin sock = sk->sk_socket; 1328f8ccac0eSTom Parkin 132902d13ed5STom Parkin /* If the tunnel socket was created by userspace, then go through the 133002d13ed5STom Parkin * inet layer to shut the socket down, and let userspace close it. 133102d13ed5STom Parkin * Otherwise, if we created the socket directly within the kernel, use 133202d13ed5STom Parkin * the sk API to release it here. 1333167eb17eSTom Parkin * In either case the tunnel resources are freed in the socket 1334167eb17eSTom Parkin * destructor when the tunnel socket goes away. 1335f8ccac0eSTom Parkin */ 133602d13ed5STom Parkin if (tunnel->fd >= 0) { 133702d13ed5STom Parkin if (sock) 133802d13ed5STom Parkin inet_shutdown(sock, 2); 133902d13ed5STom Parkin } else { 134026abe143SEric W. Biederman if (sock) { 1341167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 134226abe143SEric W. Biederman sock_release(sock); 134326abe143SEric W. Biederman } 1344167eb17eSTom Parkin } 1345f8ccac0eSTom Parkin 1346f8ccac0eSTom Parkin l2tp_tunnel_sock_put(sk); 134706a15f51SAlexander Couzens out: 134806a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1349fd558d18SJames Chapman } 1350fd558d18SJames Chapman 1351789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1352789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1353789a4a2cSJames Chapman * managing L2TP daemon. 1354167eb17eSTom Parkin * 1355167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1356167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1357167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1358167eb17eSTom Parkin * exit hook. 1359789a4a2cSJames Chapman */ 1360167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1361167eb17eSTom Parkin u32 tunnel_id, 1362167eb17eSTom Parkin u32 peer_tunnel_id, 1363167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1364167eb17eSTom Parkin struct socket **sockp) 1365789a4a2cSJames Chapman { 1366789a4a2cSJames Chapman int err = -EINVAL; 13677bddd0dbSEric Dumazet struct socket *sock = NULL; 136885644b4dSTom Herbert struct udp_port_cfg udp_conf; 1369789a4a2cSJames Chapman 1370789a4a2cSJames Chapman switch (cfg->encap) { 1371789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 137285644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 137385644b4dSTom Herbert 1374f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1375f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 137685644b4dSTom Herbert udp_conf.family = AF_INET6; 137785644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 137885644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 137985644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 138085644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 138185644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1382018f8258SWang Shanker ! cfg->udp6_zero_tx_checksums; 138385644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1384018f8258SWang Shanker ! cfg->udp6_zero_rx_checksums; 1385f9bac8dfSChris Elston } else 1386f9bac8dfSChris Elston #endif 1387f9bac8dfSChris Elston { 138885644b4dSTom Herbert udp_conf.family = AF_INET; 138985644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 139085644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 139185644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1392f9bac8dfSChris Elston } 1393789a4a2cSJames Chapman 139485644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 139585644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 139685644b4dSTom Herbert 139785644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 139885644b4dSTom Herbert if (err < 0) 139985644b4dSTom Herbert goto out; 1400789a4a2cSJames Chapman 1401789a4a2cSJames Chapman break; 1402789a4a2cSJames Chapman 1403789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1404f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1405f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 140685644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 140785644b4dSTom Herbert 140826abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1409167eb17eSTom Parkin IPPROTO_L2TP, &sock); 14105dac94e1SJames Chapman if (err < 0) 1411f9bac8dfSChris Elston goto out; 14125dac94e1SJames Chapman 14135dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14145dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14155dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14165dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14175dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, 14185dac94e1SJames Chapman sizeof(ip6_addr)); 14195dac94e1SJames Chapman if (err < 0) 14205dac94e1SJames Chapman goto out; 14215dac94e1SJames Chapman 14225dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14235dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14245dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14255dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14265dac94e1SJames Chapman err = kernel_connect(sock, 14275dac94e1SJames Chapman (struct sockaddr *) &ip6_addr, 14285dac94e1SJames Chapman sizeof(ip6_addr), 0); 14295dac94e1SJames Chapman if (err < 0) 14305dac94e1SJames Chapman goto out; 14315dac94e1SJames Chapman } else 1432f9bac8dfSChris Elston #endif 14335dac94e1SJames Chapman { 143485644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 143585644b4dSTom Herbert 143626abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1437167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1438789a4a2cSJames Chapman if (err < 0) 1439789a4a2cSJames Chapman goto out; 1440789a4a2cSJames Chapman 1441789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1442789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1443789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14445dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 14455dac94e1SJames Chapman sizeof(ip_addr)); 1446789a4a2cSJames Chapman if (err < 0) 1447789a4a2cSJames Chapman goto out; 1448789a4a2cSJames Chapman 1449789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1450789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1451789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14525dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 14535dac94e1SJames Chapman sizeof(ip_addr), 0); 1454789a4a2cSJames Chapman if (err < 0) 1455789a4a2cSJames Chapman goto out; 14565dac94e1SJames Chapman } 1457789a4a2cSJames Chapman break; 1458789a4a2cSJames Chapman 1459789a4a2cSJames Chapman default: 1460789a4a2cSJames Chapman goto out; 1461789a4a2cSJames Chapman } 1462789a4a2cSJames Chapman 1463789a4a2cSJames Chapman out: 1464167eb17eSTom Parkin *sockp = sock; 1465789a4a2cSJames Chapman if ((err < 0) && sock) { 1466167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 146726abe143SEric W. Biederman sock_release(sock); 1468789a4a2cSJames Chapman *sockp = NULL; 1469789a4a2cSJames Chapman } 1470789a4a2cSJames Chapman 1471789a4a2cSJames Chapman return err; 1472789a4a2cSJames Chapman } 1473789a4a2cSJames Chapman 147437159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 147537159ef2SEric Dumazet 1476fd558d18SJames 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) 1477fd558d18SJames Chapman { 1478fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1479fd558d18SJames Chapman int err; 1480fd558d18SJames Chapman struct socket *sock = NULL; 1481fd558d18SJames Chapman struct sock *sk = NULL; 1482fd558d18SJames Chapman struct l2tp_net *pn; 14830d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1484fd558d18SJames Chapman 1485fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1486789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1487789a4a2cSJames Chapman * kernel socket. 1488fd558d18SJames Chapman */ 1489789a4a2cSJames Chapman if (fd < 0) { 1490167eb17eSTom Parkin err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, 1491167eb17eSTom Parkin cfg, &sock); 1492789a4a2cSJames Chapman if (err < 0) 1493789a4a2cSJames Chapman goto err; 1494789a4a2cSJames Chapman } else { 1495fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1496fd558d18SJames Chapman if (!sock) { 1497cbb95e0cSTom Parkin pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", 1498fd558d18SJames Chapman tunnel_id, fd, err); 1499cbb95e0cSTom Parkin err = -EBADF; 1500cbb95e0cSTom Parkin goto err; 1501cbb95e0cSTom Parkin } 1502cbb95e0cSTom Parkin 1503cbb95e0cSTom Parkin /* Reject namespace mismatches */ 1504cbb95e0cSTom Parkin if (!net_eq(sock_net(sock->sk), net)) { 1505cbb95e0cSTom Parkin pr_err("tunl %u: netns mismatch\n", tunnel_id); 1506cbb95e0cSTom Parkin err = -EINVAL; 1507fd558d18SJames Chapman goto err; 1508fd558d18SJames Chapman } 1509789a4a2cSJames Chapman } 1510fd558d18SJames Chapman 1511fd558d18SJames Chapman sk = sock->sk; 1512fd558d18SJames Chapman 15130d76751fSJames Chapman if (cfg != NULL) 15140d76751fSJames Chapman encap = cfg->encap; 15150d76751fSJames Chapman 1516fd558d18SJames Chapman /* Quick sanity checks */ 15170d76751fSJames Chapman switch (encap) { 15180d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1519fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1520fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1521a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1522fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1523fd558d18SJames Chapman goto err; 1524fd558d18SJames Chapman } 15250d76751fSJames Chapman break; 15260d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 15270d76751fSJames Chapman err = -EPROTONOSUPPORT; 15280d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 1529a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 15300d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1531fd558d18SJames Chapman goto err; 1532fd558d18SJames Chapman } 15330d76751fSJames Chapman break; 15340d76751fSJames Chapman } 1535fd558d18SJames Chapman 1536fd558d18SJames Chapman /* Check if this socket has already been prepped */ 15378d8a51e2SDavid S. Miller tunnel = l2tp_tunnel(sk); 1538fd558d18SJames Chapman if (tunnel != NULL) { 1539fd558d18SJames Chapman /* This socket has already been prepped */ 1540fd558d18SJames Chapman err = -EBUSY; 1541fd558d18SJames Chapman goto err; 1542fd558d18SJames Chapman } 1543fd558d18SJames Chapman 1544fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1545fd558d18SJames Chapman if (tunnel == NULL) { 1546fd558d18SJames Chapman err = -ENOMEM; 1547fd558d18SJames Chapman goto err; 1548fd558d18SJames Chapman } 1549fd558d18SJames Chapman 1550fd558d18SJames Chapman tunnel->version = version; 1551fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1552fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1553fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1554fd558d18SJames Chapman 1555fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1556fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1557fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1558fd558d18SJames Chapman 1559fd558d18SJames Chapman /* The net we belong to */ 1560fd558d18SJames Chapman tunnel->l2tp_net = net; 1561fd558d18SJames Chapman pn = l2tp_pernet(net); 1562fd558d18SJames Chapman 15630d76751fSJames Chapman if (cfg != NULL) 1564fd558d18SJames Chapman tunnel->debug = cfg->debug; 1565fd558d18SJames Chapman 1566e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6) 1567e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6) { 1568e18503f4SFrançois Cachereul struct ipv6_pinfo *np = inet6_sk(sk); 1569e18503f4SFrançois Cachereul 1570e18503f4SFrançois Cachereul if (ipv6_addr_v4mapped(&np->saddr) && 1571efe4208fSEric Dumazet ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 1572e18503f4SFrançois Cachereul struct inet_sock *inet = inet_sk(sk); 1573e18503f4SFrançois Cachereul 1574e18503f4SFrançois Cachereul tunnel->v4mapped = true; 1575e18503f4SFrançois Cachereul inet->inet_saddr = np->saddr.s6_addr32[3]; 1576efe4208fSEric Dumazet inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3]; 1577efe4208fSEric Dumazet inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3]; 1578e18503f4SFrançois Cachereul } else { 1579e18503f4SFrançois Cachereul tunnel->v4mapped = false; 1580e18503f4SFrançois Cachereul } 1581e18503f4SFrançois Cachereul } 1582e18503f4SFrançois Cachereul #endif 1583e18503f4SFrançois Cachereul 1584fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 15850d76751fSJames Chapman tunnel->encap = encap; 15860d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 1587a5c5e2daSGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { }; 1588fd558d18SJames Chapman 1589c8fffceaSAndy Zhou udp_cfg.sk_user_data = tunnel; 1590c8fffceaSAndy Zhou udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP; 1591c8fffceaSAndy Zhou udp_cfg.encap_rcv = l2tp_udp_encap_recv; 1592c8fffceaSAndy Zhou udp_cfg.encap_destroy = l2tp_udp_encap_destroy; 1593c8fffceaSAndy Zhou 1594c8fffceaSAndy Zhou setup_udp_tunnel_sock(net, sock, &udp_cfg); 1595c8fffceaSAndy Zhou } else { 1596fd558d18SJames Chapman sk->sk_user_data = tunnel; 1597c8fffceaSAndy Zhou } 1598fd558d18SJames Chapman 1599fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1600fd558d18SJames Chapman * if the tunnel socket goes away. 1601fd558d18SJames Chapman */ 1602fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1603fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1604fd558d18SJames Chapman tunnel->sock = sk; 160580d84ef3STom Parkin tunnel->fd = fd; 160637159ef2SEric Dumazet lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); 160737159ef2SEric Dumazet 1608fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1609fd558d18SJames Chapman 1610f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1611f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1612f8ccac0eSTom Parkin 1613fd558d18SJames Chapman /* Add tunnel to our list */ 1614fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1615fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1616fd558d18SJames Chapman 1617fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 16181769192aSEric Dumazet * only when this drops to zero. Must be done before list insertion 1619fd558d18SJames Chapman */ 1620fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 16211769192aSEric Dumazet spin_lock_bh(&pn->l2tp_tunnel_list_lock); 16221769192aSEric Dumazet list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 16231769192aSEric Dumazet spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1624fd558d18SJames Chapman 1625fd558d18SJames Chapman err = 0; 1626fd558d18SJames Chapman err: 1627fd558d18SJames Chapman if (tunnelp) 1628fd558d18SJames Chapman *tunnelp = tunnel; 1629fd558d18SJames Chapman 1630789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1631789a4a2cSJames Chapman * have a file. 1632789a4a2cSJames Chapman */ 1633789a4a2cSJames Chapman if (sock && sock->file) 1634fd558d18SJames Chapman sockfd_put(sock); 1635fd558d18SJames Chapman 1636fd558d18SJames Chapman return err; 1637fd558d18SJames Chapman } 1638fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1639fd558d18SJames Chapman 1640309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1641309795f4SJames Chapman */ 1642309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1643309795f4SJames Chapman { 164406a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 164506a15f51SAlexander Couzens if (false == queue_work(l2tp_wq, &tunnel->del_work)) { 164606a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 164706a15f51SAlexander Couzens return 1; 164806a15f51SAlexander Couzens } 164906a15f51SAlexander Couzens return 0; 1650309795f4SJames Chapman } 1651309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1652309795f4SJames Chapman 1653fd558d18SJames Chapman /* Really kill the session. 1654fd558d18SJames Chapman */ 1655fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1656fd558d18SJames Chapman { 1657f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1658fd558d18SJames Chapman 1659fd558d18SJames Chapman BUG_ON(atomic_read(&session->ref_count) != 0); 1660fd558d18SJames Chapman 1661f6e16b29STom Parkin if (tunnel) { 1662fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1663fd558d18SJames Chapman if (session->session_id != 0) 1664fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1665fd558d18SJames Chapman sock_put(tunnel->sock); 1666fd558d18SJames Chapman session->tunnel = NULL; 1667fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1668fd558d18SJames Chapman } 1669fd558d18SJames Chapman 1670fd558d18SJames Chapman kfree(session); 1671fd558d18SJames Chapman } 1672fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1673fd558d18SJames Chapman 1674f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. 1675f6e16b29STom Parkin * Provides a tidyup interface for pseudowire code which can't just route all 1676f6e16b29STom Parkin * shutdown via. l2tp_session_delete and a pseudowire-specific session_close 1677f6e16b29STom Parkin * callback. 1678f6e16b29STom Parkin */ 1679f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session) 1680f6e16b29STom Parkin { 1681f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1682f6e16b29STom Parkin 1683f6e16b29STom Parkin /* Remove the session from core hashes */ 1684f6e16b29STom Parkin if (tunnel) { 1685f6e16b29STom Parkin /* Remove from the per-tunnel hash */ 1686f6e16b29STom Parkin write_lock_bh(&tunnel->hlist_lock); 1687f6e16b29STom Parkin hlist_del_init(&session->hlist); 1688f6e16b29STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1689f6e16b29STom Parkin 1690f6e16b29STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1691f6e16b29STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1692f6e16b29STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1693f6e16b29STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1694f6e16b29STom Parkin hlist_del_init_rcu(&session->global_hlist); 1695f6e16b29STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1696f6e16b29STom Parkin synchronize_rcu(); 1697f6e16b29STom Parkin } 1698f6e16b29STom Parkin } 1699f6e16b29STom Parkin } 1700f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash); 1701f6e16b29STom Parkin 1702309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1703309795f4SJames Chapman pseudowire modules. 1704309795f4SJames Chapman */ 1705309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1706309795f4SJames Chapman { 1707f6e16b29STom Parkin if (session->ref) 1708f6e16b29STom Parkin (*session->ref)(session); 1709f6e16b29STom Parkin __l2tp_session_unhash(session); 17104c6e2fd3STom Parkin l2tp_session_queue_purge(session); 1711309795f4SJames Chapman if (session->session_close != NULL) 1712309795f4SJames Chapman (*session->session_close)(session); 1713f6e16b29STom Parkin if (session->deref) 17141b7c92b9SDan Carpenter (*session->deref)(session); 1715309795f4SJames Chapman l2tp_session_dec_refcount(session); 1716309795f4SJames Chapman return 0; 1717309795f4SJames Chapman } 1718309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1719309795f4SJames Chapman 1720f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1721f7faffa3SJames Chapman * l2specific_len parameters are set. 1722f7faffa3SJames Chapman */ 1723bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1724f7faffa3SJames Chapman { 1725f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1726f7faffa3SJames Chapman session->hdr_len = 6; 1727f7faffa3SJames Chapman if (session->send_seq) 1728f7faffa3SJames Chapman session->hdr_len += 4; 1729f7faffa3SJames Chapman } else { 17300d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 17310d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 17320d76751fSJames Chapman session->hdr_len += 4; 1733f7faffa3SJames Chapman } 1734f7faffa3SJames Chapman 1735f7faffa3SJames Chapman } 1736bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1737f7faffa3SJames Chapman 1738fd558d18SJames 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) 1739fd558d18SJames Chapman { 1740fd558d18SJames Chapman struct l2tp_session *session; 1741fd558d18SJames Chapman 1742fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1743fd558d18SJames Chapman if (session != NULL) { 1744fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1745fd558d18SJames Chapman session->tunnel = tunnel; 1746fd558d18SJames Chapman 1747fd558d18SJames Chapman session->session_id = session_id; 1748fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1749d301e325SJames Chapman session->nr = 0; 17508a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 17518a1631d5SJames Chapman session->nr_max = 0xffff; 17528a1631d5SJames Chapman else 17538a1631d5SJames Chapman session->nr_max = 0xffffff; 17548a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1755a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1756a0dbd822SJames Chapman 1757a0dbd822SJames Chapman /* Use NR of first received packet */ 1758a0dbd822SJames Chapman session->reorder_skip = 1; 1759fd558d18SJames Chapman 1760fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1761fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1762fd558d18SJames Chapman 1763fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1764fd558d18SJames Chapman 1765fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1766f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1767fd558d18SJames Chapman 1768fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1769fd558d18SJames Chapman session->debug = tunnel->debug; 1770fd558d18SJames Chapman 1771fd558d18SJames Chapman if (cfg) { 1772f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1773fd558d18SJames Chapman session->debug = cfg->debug; 1774fd558d18SJames Chapman session->mtu = cfg->mtu; 1775fd558d18SJames Chapman session->mru = cfg->mru; 1776fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1777fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1778fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1779f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1780f7faffa3SJames Chapman session->offset = cfg->offset; 1781f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1782f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1783f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1784f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1785f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1786f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1787fd558d18SJames Chapman } 1788fd558d18SJames Chapman 1789f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1790f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1791f7faffa3SJames Chapman else 1792f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1793f7faffa3SJames Chapman 1794f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1795f7faffa3SJames Chapman 1796fd558d18SJames Chapman /* Bump the reference count. The session context is deleted 1797fd558d18SJames Chapman * only when this drops to zero. 1798fd558d18SJames Chapman */ 1799fd558d18SJames Chapman l2tp_session_inc_refcount(session); 1800fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1801fd558d18SJames Chapman 1802fd558d18SJames Chapman /* Ensure tunnel socket isn't deleted */ 1803fd558d18SJames Chapman sock_hold(tunnel->sock); 1804fd558d18SJames Chapman 1805fd558d18SJames Chapman /* Add session to the tunnel's hash list */ 1806fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1807fd558d18SJames Chapman hlist_add_head(&session->hlist, 1808fd558d18SJames Chapman l2tp_session_id_hash(tunnel, session_id)); 1809fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1810fd558d18SJames Chapman 1811f7faffa3SJames Chapman /* And to the global session list if L2TPv3 */ 1812f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1813f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1814f7faffa3SJames Chapman 1815e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1816e02d494dSJames Chapman hlist_add_head_rcu(&session->global_hlist, 1817f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id)); 1818e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1819f7faffa3SJames Chapman } 1820f7faffa3SJames Chapman 1821fd558d18SJames Chapman /* Ignore management session in session count value */ 1822fd558d18SJames Chapman if (session->session_id != 0) 1823fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1824fd558d18SJames Chapman } 1825fd558d18SJames Chapman 1826fd558d18SJames Chapman return session; 1827fd558d18SJames Chapman } 1828fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1829fd558d18SJames Chapman 1830fd558d18SJames Chapman /***************************************************************************** 1831fd558d18SJames Chapman * Init and cleanup 1832fd558d18SJames Chapman *****************************************************************************/ 1833fd558d18SJames Chapman 1834fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1835fd558d18SJames Chapman { 1836e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1837f7faffa3SJames Chapman int hash; 1838fd558d18SJames Chapman 1839fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1840e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1841fd558d18SJames Chapman 1842f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1843f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1844f7faffa3SJames Chapman 1845e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1846f7faffa3SJames Chapman 1847fd558d18SJames Chapman return 0; 1848fd558d18SJames Chapman } 1849fd558d18SJames Chapman 1850167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1851167eb17eSTom Parkin { 1852167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1853167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1854167eb17eSTom Parkin 1855167eb17eSTom Parkin rcu_read_lock_bh(); 1856167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 1857167eb17eSTom Parkin (void)l2tp_tunnel_delete(tunnel); 1858167eb17eSTom Parkin } 1859167eb17eSTom Parkin rcu_read_unlock_bh(); 18602f86953eSSabrina Dubroca 18612f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 18622f86953eSSabrina Dubroca rcu_barrier(); 1863167eb17eSTom Parkin } 1864167eb17eSTom Parkin 1865fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1866fd558d18SJames Chapman .init = l2tp_init_net, 1867167eb17eSTom Parkin .exit = l2tp_exit_net, 1868fd558d18SJames Chapman .id = &l2tp_net_id, 1869fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1870fd558d18SJames Chapman }; 1871fd558d18SJames Chapman 1872fd558d18SJames Chapman static int __init l2tp_init(void) 1873fd558d18SJames Chapman { 1874fd558d18SJames Chapman int rc = 0; 1875fd558d18SJames Chapman 1876fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1877fd558d18SJames Chapman if (rc) 1878fd558d18SJames Chapman goto out; 1879fd558d18SJames Chapman 188059ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1881f8ccac0eSTom Parkin if (!l2tp_wq) { 1882f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 188367e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1884f8ccac0eSTom Parkin rc = -ENOMEM; 1885f8ccac0eSTom Parkin goto out; 1886f8ccac0eSTom Parkin } 1887f8ccac0eSTom Parkin 1888a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1889fd558d18SJames Chapman 1890fd558d18SJames Chapman out: 1891fd558d18SJames Chapman return rc; 1892fd558d18SJames Chapman } 1893fd558d18SJames Chapman 1894fd558d18SJames Chapman static void __exit l2tp_exit(void) 1895fd558d18SJames Chapman { 1896fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1897f8ccac0eSTom Parkin if (l2tp_wq) { 1898f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1899f8ccac0eSTom Parkin l2tp_wq = NULL; 1900f8ccac0eSTom Parkin } 1901fd558d18SJames Chapman } 1902fd558d18SJames Chapman 1903fd558d18SJames Chapman module_init(l2tp_init); 1904fd558d18SJames Chapman module_exit(l2tp_exit); 1905fd558d18SJames Chapman 1906fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1907fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1908fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1909fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1910fd558d18SJames Chapman 1911