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> 55309795f4SJames Chapman #include <net/inet_common.h> 56fd558d18SJames Chapman #include <net/xfrm.h> 570d76751fSJames Chapman #include <net/protocol.h> 58d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h> 59d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h> 60d2cf3361SBenjamin LaHaise #include <net/ip6_route.h> 61d499bd2eSDavid S. Miller #include <net/ip6_checksum.h> 62fd558d18SJames Chapman 63fd558d18SJames Chapman #include <asm/byteorder.h> 6460063497SArun Sharma #include <linux/atomic.h> 65fd558d18SJames Chapman 66fd558d18SJames Chapman #include "l2tp_core.h" 67fd558d18SJames Chapman 68fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 69fd558d18SJames Chapman 70fd558d18SJames Chapman /* L2TP header constants */ 71fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 72fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 73fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 74fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 75fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 76fd558d18SJames Chapman 77fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 78fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 79f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 80fd558d18SJames Chapman 81fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 82fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 83fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 84fd558d18SJames Chapman 85fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ 10 86fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ 6 87fd558d18SJames Chapman 88fd558d18SJames Chapman /* Default trace flags */ 89fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 90fd558d18SJames Chapman 91fd558d18SJames Chapman /* Private data stored for received packets in the skb. 92fd558d18SJames Chapman */ 93fd558d18SJames Chapman struct l2tp_skb_cb { 94f7faffa3SJames Chapman u32 ns; 95fd558d18SJames Chapman u16 has_seq; 96fd558d18SJames Chapman u16 length; 97fd558d18SJames Chapman unsigned long expires; 98fd558d18SJames Chapman }; 99fd558d18SJames Chapman 100fd558d18SJames Chapman #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 101fd558d18SJames Chapman 102fd558d18SJames Chapman static atomic_t l2tp_tunnel_count; 103fd558d18SJames Chapman static atomic_t l2tp_session_count; 104fd558d18SJames Chapman 105fd558d18SJames Chapman /* per-net private data for this module */ 106fd558d18SJames Chapman static unsigned int l2tp_net_id; 107fd558d18SJames Chapman struct l2tp_net { 108fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 109e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 110f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 111e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 112fd558d18SJames Chapman }; 113fd558d18SJames Chapman 114fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version); 115fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); 116fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel); 117fc130840Sstephen hemminger 118fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(struct net *net) 119fd558d18SJames Chapman { 120fd558d18SJames Chapman BUG_ON(!net); 121fd558d18SJames Chapman 122fd558d18SJames Chapman return net_generic(net, l2tp_net_id); 123fd558d18SJames Chapman } 124fd558d18SJames Chapman 125fc130840Sstephen hemminger 126fc130840Sstephen hemminger /* Tunnel reference counts. Incremented per session that is added to 127fc130840Sstephen hemminger * the tunnel. 128fc130840Sstephen hemminger */ 129fc130840Sstephen hemminger static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel) 130fc130840Sstephen hemminger { 131fc130840Sstephen hemminger atomic_inc(&tunnel->ref_count); 132fc130840Sstephen hemminger } 133fc130840Sstephen hemminger 134fc130840Sstephen hemminger static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel) 135fc130840Sstephen hemminger { 136fc130840Sstephen hemminger if (atomic_dec_and_test(&tunnel->ref_count)) 137fc130840Sstephen hemminger l2tp_tunnel_free(tunnel); 138fc130840Sstephen hemminger } 139fc130840Sstephen hemminger #ifdef L2TP_REFCNT_DEBUG 140a4ca44faSJoe Perches #define l2tp_tunnel_inc_refcount(_t) \ 141a4ca44faSJoe Perches do { \ 142a4ca44faSJoe Perches pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \ 143a4ca44faSJoe Perches __func__, __LINE__, (_t)->name, \ 144a4ca44faSJoe Perches atomic_read(&_t->ref_count)); \ 145fc130840Sstephen hemminger l2tp_tunnel_inc_refcount_1(_t); \ 146fc130840Sstephen hemminger } while (0) 147a4ca44faSJoe Perches #define l2tp_tunnel_dec_refcount(_t) 148a4ca44faSJoe Perches do { \ 149a4ca44faSJoe Perches pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \ 150a4ca44faSJoe Perches __func__, __LINE__, (_t)->name, \ 151a4ca44faSJoe Perches atomic_read(&_t->ref_count)); \ 152fc130840Sstephen hemminger l2tp_tunnel_dec_refcount_1(_t); \ 153fc130840Sstephen hemminger } while (0) 154fc130840Sstephen hemminger #else 155fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t) 156fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t) 157fc130840Sstephen hemminger #endif 158fc130840Sstephen hemminger 159f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 160f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 161f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 162f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 163f7faffa3SJames Chapman */ 164f7faffa3SJames Chapman static inline struct hlist_head * 165f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 166f7faffa3SJames Chapman { 167f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 168f7faffa3SJames Chapman 169f7faffa3SJames Chapman } 170f7faffa3SJames Chapman 171f7faffa3SJames Chapman /* Lookup a session by id in the global session list 172f7faffa3SJames Chapman */ 173f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id) 174f7faffa3SJames Chapman { 175f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 176f7faffa3SJames Chapman struct hlist_head *session_list = 177f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id); 178f7faffa3SJames Chapman struct l2tp_session *session; 179f7faffa3SJames Chapman struct hlist_node *walk; 180f7faffa3SJames Chapman 181e02d494dSJames Chapman rcu_read_lock_bh(); 182e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) { 183f7faffa3SJames Chapman if (session->session_id == session_id) { 184e02d494dSJames Chapman rcu_read_unlock_bh(); 185f7faffa3SJames Chapman return session; 186f7faffa3SJames Chapman } 187f7faffa3SJames Chapman } 188e02d494dSJames Chapman rcu_read_unlock_bh(); 189f7faffa3SJames Chapman 190f7faffa3SJames Chapman return NULL; 191f7faffa3SJames Chapman } 192f7faffa3SJames Chapman 193fd558d18SJames Chapman /* Session hash list. 194fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 195fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 196fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 197fd558d18SJames Chapman * simple bitmask. 198fd558d18SJames Chapman */ 199fd558d18SJames Chapman static inline struct hlist_head * 200fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 201fd558d18SJames Chapman { 202fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 203fd558d18SJames Chapman } 204fd558d18SJames Chapman 205fd558d18SJames Chapman /* Lookup a session by id 206fd558d18SJames Chapman */ 207f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id) 208fd558d18SJames Chapman { 209f7faffa3SJames Chapman struct hlist_head *session_list; 210fd558d18SJames Chapman struct l2tp_session *session; 211fd558d18SJames Chapman struct hlist_node *walk; 212fd558d18SJames Chapman 213f7faffa3SJames Chapman /* In L2TPv3, session_ids are unique over all tunnels and we 214f7faffa3SJames Chapman * sometimes need to look them up before we know the 215f7faffa3SJames Chapman * tunnel. 216f7faffa3SJames Chapman */ 217f7faffa3SJames Chapman if (tunnel == NULL) 218f7faffa3SJames Chapman return l2tp_session_find_2(net, session_id); 219f7faffa3SJames Chapman 220f7faffa3SJames Chapman session_list = l2tp_session_id_hash(tunnel, session_id); 221fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 222fd558d18SJames Chapman hlist_for_each_entry(session, walk, session_list, hlist) { 223fd558d18SJames Chapman if (session->session_id == session_id) { 224fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 225fd558d18SJames Chapman return session; 226fd558d18SJames Chapman } 227fd558d18SJames Chapman } 228fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 229fd558d18SJames Chapman 230fd558d18SJames Chapman return NULL; 231fd558d18SJames Chapman } 232fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find); 233fd558d18SJames Chapman 234fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth) 235fd558d18SJames Chapman { 236fd558d18SJames Chapman int hash; 237fd558d18SJames Chapman struct hlist_node *walk; 238fd558d18SJames Chapman struct l2tp_session *session; 239fd558d18SJames Chapman int count = 0; 240fd558d18SJames Chapman 241fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 242fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 243fd558d18SJames Chapman hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) { 244fd558d18SJames Chapman if (++count > nth) { 245fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 246fd558d18SJames Chapman return session; 247fd558d18SJames Chapman } 248fd558d18SJames Chapman } 249fd558d18SJames Chapman } 250fd558d18SJames Chapman 251fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 252fd558d18SJames Chapman 253fd558d18SJames Chapman return NULL; 254fd558d18SJames Chapman } 255fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth); 256fd558d18SJames Chapman 257309795f4SJames Chapman /* Lookup a session by interface name. 258309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 259309795f4SJames Chapman */ 260309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) 261309795f4SJames Chapman { 262309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 263309795f4SJames Chapman int hash; 264309795f4SJames Chapman struct hlist_node *walk; 265309795f4SJames Chapman struct l2tp_session *session; 266309795f4SJames Chapman 267e02d494dSJames Chapman rcu_read_lock_bh(); 268309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 269e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) { 270309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 271e02d494dSJames Chapman rcu_read_unlock_bh(); 272309795f4SJames Chapman return session; 273309795f4SJames Chapman } 274309795f4SJames Chapman } 275309795f4SJames Chapman } 276309795f4SJames Chapman 277e02d494dSJames Chapman rcu_read_unlock_bh(); 278309795f4SJames Chapman 279309795f4SJames Chapman return NULL; 280309795f4SJames Chapman } 281309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname); 282309795f4SJames Chapman 283fd558d18SJames Chapman /* Lookup a tunnel by id 284fd558d18SJames Chapman */ 285fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id) 286fd558d18SJames Chapman { 287fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 288fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 289fd558d18SJames Chapman 290e02d494dSJames Chapman rcu_read_lock_bh(); 291e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 292fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 293e02d494dSJames Chapman rcu_read_unlock_bh(); 294fd558d18SJames Chapman return tunnel; 295fd558d18SJames Chapman } 296fd558d18SJames Chapman } 297e02d494dSJames Chapman rcu_read_unlock_bh(); 298fd558d18SJames Chapman 299fd558d18SJames Chapman return NULL; 300fd558d18SJames Chapman } 301fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 302fd558d18SJames Chapman 303fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth) 304fd558d18SJames Chapman { 305fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 306fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 307fd558d18SJames Chapman int count = 0; 308fd558d18SJames Chapman 309e02d494dSJames Chapman rcu_read_lock_bh(); 310e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 311fd558d18SJames Chapman if (++count > nth) { 312e02d494dSJames Chapman rcu_read_unlock_bh(); 313fd558d18SJames Chapman return tunnel; 314fd558d18SJames Chapman } 315fd558d18SJames Chapman } 316fd558d18SJames Chapman 317e02d494dSJames Chapman rcu_read_unlock_bh(); 318fd558d18SJames Chapman 319fd558d18SJames Chapman return NULL; 320fd558d18SJames Chapman } 321fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 322fd558d18SJames Chapman 323fd558d18SJames Chapman /***************************************************************************** 324fd558d18SJames Chapman * Receive data handling 325fd558d18SJames Chapman *****************************************************************************/ 326fd558d18SJames Chapman 327fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 328fd558d18SJames Chapman * number. 329fd558d18SJames Chapman */ 330fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 331fd558d18SJames Chapman { 332fd558d18SJames Chapman struct sk_buff *skbp; 333fd558d18SJames Chapman struct sk_buff *tmp; 334f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 3355de7aee5SJames Chapman struct l2tp_stats *sstats; 336fd558d18SJames Chapman 337fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 3385de7aee5SJames Chapman sstats = &session->stats; 339fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 340fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 341fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 342a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 343fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 344fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 345fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 3465de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 3475de7aee5SJames Chapman sstats->rx_oos_packets++; 3485de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 349fd558d18SJames Chapman goto out; 350fd558d18SJames Chapman } 351fd558d18SJames Chapman } 352fd558d18SJames Chapman 353fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 354fd558d18SJames Chapman 355fd558d18SJames Chapman out: 356fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 357fd558d18SJames Chapman } 358fd558d18SJames Chapman 359fd558d18SJames Chapman /* Dequeue a single skb. 360fd558d18SJames Chapman */ 361fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 362fd558d18SJames Chapman { 363fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 364fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 3655de7aee5SJames Chapman struct l2tp_stats *tstats, *sstats; 366fd558d18SJames Chapman 367fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 368fd558d18SJames Chapman * to its current owner (a socket receive buffer). 369fd558d18SJames Chapman */ 370fd558d18SJames Chapman skb_orphan(skb); 371fd558d18SJames Chapman 3725de7aee5SJames Chapman tstats = &tunnel->stats; 3735de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 3745de7aee5SJames Chapman sstats = &session->stats; 3755de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 3765de7aee5SJames Chapman tstats->rx_packets++; 3775de7aee5SJames Chapman tstats->rx_bytes += length; 3785de7aee5SJames Chapman sstats->rx_packets++; 3795de7aee5SJames Chapman sstats->rx_bytes += length; 3805de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 3815de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 382fd558d18SJames Chapman 383fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 384fd558d18SJames Chapman /* Bump our Nr */ 385fd558d18SJames Chapman session->nr++; 386f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 387f7faffa3SJames Chapman session->nr &= 0xffff; 388f7faffa3SJames Chapman else 389f7faffa3SJames Chapman session->nr &= 0xffffff; 390f7faffa3SJames Chapman 391a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 392a4ca44faSJoe Perches session->name, session->nr); 393fd558d18SJames Chapman } 394fd558d18SJames Chapman 395fd558d18SJames Chapman /* call private receive handler */ 396fd558d18SJames Chapman if (session->recv_skb != NULL) 397fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 398fd558d18SJames Chapman else 399fd558d18SJames Chapman kfree_skb(skb); 400fd558d18SJames Chapman 401fd558d18SJames Chapman if (session->deref) 402fd558d18SJames Chapman (*session->deref)(session); 403fd558d18SJames Chapman } 404fd558d18SJames Chapman 405fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 406fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 407fd558d18SJames Chapman */ 408fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 409fd558d18SJames Chapman { 410fd558d18SJames Chapman struct sk_buff *skb; 411fd558d18SJames Chapman struct sk_buff *tmp; 4125de7aee5SJames Chapman struct l2tp_stats *sstats; 413fd558d18SJames Chapman 414fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 415fd558d18SJames Chapman * expect to send up next, dequeue it and any other 416fd558d18SJames Chapman * in-sequence packets behind it. 417fd558d18SJames Chapman */ 418e2e210c0SEric Dumazet start: 419fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 4205de7aee5SJames Chapman sstats = &session->stats; 421fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 422fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 4235de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 4245de7aee5SJames Chapman sstats->rx_seq_discards++; 4255de7aee5SJames Chapman sstats->rx_errors++; 4265de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 427a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 428a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 429fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 430fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 431fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 43238d40b3fSJames Chapman session->reorder_skip = 1; 433fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 434fd558d18SJames Chapman kfree_skb(skb); 435fd558d18SJames Chapman if (session->deref) 436fd558d18SJames Chapman (*session->deref)(session); 437fd558d18SJames Chapman continue; 438fd558d18SJames Chapman } 439fd558d18SJames Chapman 440fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 44138d40b3fSJames Chapman if (session->reorder_skip) { 442a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 44338d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 44438d40b3fSJames Chapman session->name, session->nr, 44538d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 44638d40b3fSJames Chapman session->reorder_skip = 0; 44738d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 44838d40b3fSJames Chapman } 449fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 450a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 451a4ca44faSJoe Perches "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 452fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 453fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 454fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 455fd558d18SJames Chapman goto out; 456fd558d18SJames Chapman } 457fd558d18SJames Chapman } 458fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 459fd558d18SJames Chapman 460fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 461fd558d18SJames Chapman * do so to let other contexts process the queue. 462fd558d18SJames Chapman */ 463fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 464fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 465e2e210c0SEric Dumazet goto start; 466fd558d18SJames Chapman } 467fd558d18SJames Chapman 468fd558d18SJames Chapman out: 469fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 470fd558d18SJames Chapman } 471fd558d18SJames Chapman 472fd558d18SJames Chapman static inline int l2tp_verify_udp_checksum(struct sock *sk, 473fd558d18SJames Chapman struct sk_buff *skb) 474fd558d18SJames Chapman { 475fd558d18SJames Chapman struct udphdr *uh = udp_hdr(skb); 476fd558d18SJames Chapman u16 ulen = ntohs(uh->len); 477fd558d18SJames Chapman __wsum psum; 478fd558d18SJames Chapman 479d2cf3361SBenjamin LaHaise if (sk->sk_no_check || skb_csum_unnecessary(skb)) 480fd558d18SJames Chapman return 0; 481fd558d18SJames Chapman 482d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 483d2cf3361SBenjamin LaHaise if (sk->sk_family == PF_INET6) { 484d2cf3361SBenjamin LaHaise if (!uh->check) { 485d2cf3361SBenjamin LaHaise LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n"); 486d2cf3361SBenjamin LaHaise return 1; 487d2cf3361SBenjamin LaHaise } 488d2cf3361SBenjamin LaHaise if ((skb->ip_summed == CHECKSUM_COMPLETE) && 489d2cf3361SBenjamin LaHaise !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 490d2cf3361SBenjamin LaHaise &ipv6_hdr(skb)->daddr, ulen, 491d2cf3361SBenjamin LaHaise IPPROTO_UDP, skb->csum)) { 492d2cf3361SBenjamin LaHaise skb->ip_summed = CHECKSUM_UNNECESSARY; 493d2cf3361SBenjamin LaHaise return 0; 494d2cf3361SBenjamin LaHaise } 495d2cf3361SBenjamin LaHaise skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 496d2cf3361SBenjamin LaHaise &ipv6_hdr(skb)->daddr, 497d2cf3361SBenjamin LaHaise skb->len, IPPROTO_UDP, 498d2cf3361SBenjamin LaHaise 0)); 499d2cf3361SBenjamin LaHaise } else 500d2cf3361SBenjamin LaHaise #endif 501d2cf3361SBenjamin LaHaise { 502d2cf3361SBenjamin LaHaise struct inet_sock *inet; 503d2cf3361SBenjamin LaHaise if (!uh->check) 504d2cf3361SBenjamin LaHaise return 0; 505fd558d18SJames Chapman inet = inet_sk(sk); 506d2cf3361SBenjamin LaHaise psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, 507d2cf3361SBenjamin LaHaise ulen, IPPROTO_UDP, 0); 508fd558d18SJames Chapman 509fd558d18SJames Chapman if ((skb->ip_summed == CHECKSUM_COMPLETE) && 510fd558d18SJames Chapman !csum_fold(csum_add(psum, skb->csum))) 511fd558d18SJames Chapman return 0; 512fd558d18SJames Chapman skb->csum = psum; 513d2cf3361SBenjamin LaHaise } 514fd558d18SJames Chapman 515fd558d18SJames Chapman return __skb_checksum_complete(skb); 516fd558d18SJames Chapman } 517fd558d18SJames Chapman 518f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 519f7faffa3SJames Chapman * and L2TPv3 data frames here. 520f7faffa3SJames Chapman * 521f7faffa3SJames Chapman * L2TPv2 Data Message Header 522f7faffa3SJames Chapman * 523f7faffa3SJames Chapman * 0 1 2 3 524f7faffa3SJames 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 525f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 526f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 527f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 528f7faffa3SJames Chapman * | Tunnel ID | Session ID | 529f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 530f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 531f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 532f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 533f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 534f7faffa3SJames Chapman * 535f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 536f7faffa3SJames Chapman * those in L2TP control frames. 537f7faffa3SJames Chapman * 538f7faffa3SJames Chapman * L2TPv3 Data Message Header 539f7faffa3SJames Chapman * 540f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 541f7faffa3SJames Chapman * | L2TP Session Header | 542f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 543f7faffa3SJames Chapman * | L2-Specific Sublayer | 544f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 545f7faffa3SJames Chapman * | Tunnel Payload ... 546f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 547f7faffa3SJames Chapman * 548f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 549f7faffa3SJames Chapman * 550f7faffa3SJames Chapman * 0 1 2 3 551f7faffa3SJames 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 552f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 553f7faffa3SJames Chapman * | Session ID | 554f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 555f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 556f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 557f7faffa3SJames Chapman * | 558f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 559f7faffa3SJames Chapman * 560f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 561f7faffa3SJames Chapman * 562f7faffa3SJames Chapman * 0 1 2 3 563f7faffa3SJames 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 564f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 565f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 566f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 567f7faffa3SJames Chapman * 568f7faffa3SJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with 569f7faffa3SJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need 570f7faffa3SJames Chapman * to parse the packet header to determine if optional fields are 571f7faffa3SJames Chapman * present. 572f7faffa3SJames Chapman * 573f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 574f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 575f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 576f7faffa3SJames Chapman * after the session-id. 577f7faffa3SJames Chapman */ 578f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 579f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 580f7faffa3SJames Chapman int length, int (*payload_hook)(struct sk_buff *skb)) 581f7faffa3SJames Chapman { 582f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 583f7faffa3SJames Chapman int offset; 584f7faffa3SJames Chapman u32 ns, nr; 5855de7aee5SJames Chapman struct l2tp_stats *sstats = &session->stats; 586f7faffa3SJames Chapman 587f7faffa3SJames Chapman /* The ref count is increased since we now hold a pointer to 588f7faffa3SJames Chapman * the session. Take care to decrement the refcnt when exiting 589f7faffa3SJames Chapman * this function from now on... 590f7faffa3SJames Chapman */ 591f7faffa3SJames Chapman l2tp_session_inc_refcount(session); 592f7faffa3SJames Chapman if (session->ref) 593f7faffa3SJames Chapman (*session->ref)(session); 594f7faffa3SJames Chapman 595f7faffa3SJames Chapman /* Parse and check optional cookie */ 596f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 597f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 598a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 599f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 600a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 601a4ca44faSJoe Perches session->session_id); 6025de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6035de7aee5SJames Chapman sstats->rx_cookie_discards++; 6045de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 605f7faffa3SJames Chapman goto discard; 606f7faffa3SJames Chapman } 607f7faffa3SJames Chapman ptr += session->peer_cookie_len; 608f7faffa3SJames Chapman } 609f7faffa3SJames Chapman 610f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 611f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 612f7faffa3SJames Chapman * 613f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 614f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 615f7faffa3SJames Chapman * we were expecting them, discard frame. 616f7faffa3SJames Chapman */ 617f7faffa3SJames Chapman ns = nr = 0; 618f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 619f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 620f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 621f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 622f7faffa3SJames Chapman ptr += 2; 623f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 624f7faffa3SJames Chapman ptr += 2; 625f7faffa3SJames Chapman 626f7faffa3SJames Chapman /* Store L2TP info in the skb */ 627f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 628f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 629f7faffa3SJames Chapman 630a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 631f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 632f7faffa3SJames Chapman session->name, ns, nr, session->nr); 633f7faffa3SJames Chapman } 634f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 635f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 636f7faffa3SJames Chapman 637f7faffa3SJames Chapman if (l2h & 0x40000000) { 638f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 639f7faffa3SJames Chapman 640f7faffa3SJames Chapman /* Store L2TP info in the skb */ 641f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 642f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 643f7faffa3SJames Chapman 644a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 645f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 646f7faffa3SJames Chapman session->name, ns, session->nr); 647f7faffa3SJames Chapman } 648f7faffa3SJames Chapman } 649f7faffa3SJames Chapman 650f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 651f7faffa3SJames Chapman ptr += session->l2specific_len; 652f7faffa3SJames Chapman 653f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 654f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 655f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 656f7faffa3SJames Chapman * configure it so. 657f7faffa3SJames Chapman */ 658f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 659a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 660f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 661f7faffa3SJames Chapman session->name); 662f7faffa3SJames Chapman session->send_seq = -1; 663f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 664f7faffa3SJames Chapman } 665f7faffa3SJames Chapman } else { 666f7faffa3SJames Chapman /* No sequence numbers. 667f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 668f7faffa3SJames Chapman */ 669f7faffa3SJames Chapman if (session->recv_seq) { 670a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 671a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 672a4ca44faSJoe Perches session->name); 6735de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6745de7aee5SJames Chapman sstats->rx_seq_discards++; 6755de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 676f7faffa3SJames Chapman goto discard; 677f7faffa3SJames Chapman } 678f7faffa3SJames Chapman 679f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 680f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 681f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 682f7faffa3SJames Chapman * LAC is broken. Discard the frame. 683f7faffa3SJames Chapman */ 684f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 685a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 686f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 687f7faffa3SJames Chapman session->name); 688f7faffa3SJames Chapman session->send_seq = 0; 689f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 690f7faffa3SJames Chapman } else if (session->send_seq) { 691a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 692a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 693a4ca44faSJoe Perches session->name); 6945de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6955de7aee5SJames Chapman sstats->rx_seq_discards++; 6965de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 697f7faffa3SJames Chapman goto discard; 698f7faffa3SJames Chapman } 699f7faffa3SJames Chapman } 700f7faffa3SJames Chapman 701f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 702f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 703f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 704f7faffa3SJames Chapman * in the session setup control protocol. 705f7faffa3SJames Chapman */ 706f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 707f7faffa3SJames Chapman /* If offset bit set, skip it. */ 708f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 709f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 710f7faffa3SJames Chapman ptr += 2 + offset; 711f7faffa3SJames Chapman } 712f7faffa3SJames Chapman } else 713f7faffa3SJames Chapman ptr += session->offset; 714f7faffa3SJames Chapman 715f7faffa3SJames Chapman offset = ptr - optr; 716f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 717f7faffa3SJames Chapman goto discard; 718f7faffa3SJames Chapman 719f7faffa3SJames Chapman __skb_pull(skb, offset); 720f7faffa3SJames Chapman 721f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 722f7faffa3SJames Chapman * packet, do so now. 723f7faffa3SJames Chapman */ 724f7faffa3SJames Chapman if (payload_hook) 725f7faffa3SJames Chapman if ((*payload_hook)(skb)) 726f7faffa3SJames Chapman goto discard; 727f7faffa3SJames Chapman 728f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 729f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 730f7faffa3SJames Chapman * reordering. 731f7faffa3SJames Chapman */ 732f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 733f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 734f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 735f7faffa3SJames Chapman 736f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 737f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 738f7faffa3SJames Chapman */ 739f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 740f7faffa3SJames Chapman if (session->reorder_timeout != 0) { 741f7faffa3SJames Chapman /* Packet reordering enabled. Add skb to session's 742f7faffa3SJames Chapman * reorder queue, in order of ns. 743f7faffa3SJames Chapman */ 744f7faffa3SJames Chapman l2tp_recv_queue_skb(session, skb); 745f7faffa3SJames Chapman } else { 746f7faffa3SJames Chapman /* Packet reordering disabled. Discard out-of-sequence 747f7faffa3SJames Chapman * packets 748f7faffa3SJames Chapman */ 749f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 7505de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 7515de7aee5SJames Chapman sstats->rx_seq_discards++; 7525de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 753a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 754a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 755f7faffa3SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 756f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 757f7faffa3SJames Chapman skb_queue_len(&session->reorder_q)); 758f7faffa3SJames Chapman goto discard; 759f7faffa3SJames Chapman } 760f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 761f7faffa3SJames Chapman } 762f7faffa3SJames Chapman } else { 763f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 764f7faffa3SJames Chapman * reorder queue. This ensures that it will be 765f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 766f7faffa3SJames Chapman */ 767f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 768f7faffa3SJames Chapman } 769f7faffa3SJames Chapman 770f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 771f7faffa3SJames Chapman l2tp_recv_dequeue(session); 772f7faffa3SJames Chapman 773f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 774f7faffa3SJames Chapman 775f7faffa3SJames Chapman return; 776f7faffa3SJames Chapman 777f7faffa3SJames Chapman discard: 7785de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 7795de7aee5SJames Chapman sstats->rx_errors++; 7805de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 781f7faffa3SJames Chapman kfree_skb(skb); 782f7faffa3SJames Chapman 783f7faffa3SJames Chapman if (session->deref) 784f7faffa3SJames Chapman (*session->deref)(session); 785f7faffa3SJames Chapman 786f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 787f7faffa3SJames Chapman } 788f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common); 789f7faffa3SJames Chapman 790fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 791fd558d18SJames Chapman * here. The skb is not on a list when we get here. 792fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 793fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 794fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 795fd558d18SJames Chapman */ 796fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 797fd558d18SJames Chapman int (*payload_hook)(struct sk_buff *skb)) 798fd558d18SJames Chapman { 799fd558d18SJames Chapman struct l2tp_session *session = NULL; 800fd558d18SJames Chapman unsigned char *ptr, *optr; 801fd558d18SJames Chapman u16 hdrflags; 802fd558d18SJames Chapman u32 tunnel_id, session_id; 803fd558d18SJames Chapman u16 version; 804f7faffa3SJames Chapman int length; 8055de7aee5SJames Chapman struct l2tp_stats *tstats; 806fd558d18SJames Chapman 807fd558d18SJames Chapman if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb)) 808fd558d18SJames Chapman goto discard_bad_csum; 809fd558d18SJames Chapman 810fd558d18SJames Chapman /* UDP always verifies the packet length. */ 811fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 812fd558d18SJames Chapman 813fd558d18SJames Chapman /* Short packet? */ 814fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 815a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 816a4ca44faSJoe Perches "%s: recv short packet (len=%d)\n", 817a4ca44faSJoe Perches tunnel->name, skb->len); 818fd558d18SJames Chapman goto error; 819fd558d18SJames Chapman } 820fd558d18SJames Chapman 821fd558d18SJames Chapman /* Trace packet contents, if enabled */ 822fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 823fd558d18SJames Chapman length = min(32u, skb->len); 824fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 825fd558d18SJames Chapman goto error; 826fd558d18SJames Chapman 827a4ca44faSJoe Perches pr_debug("%s: recv\n", tunnel->name); 828a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 829fd558d18SJames Chapman } 830fd558d18SJames Chapman 831e50e705cSEric Dumazet /* Point to L2TP header */ 832e50e705cSEric Dumazet optr = ptr = skb->data; 833e50e705cSEric Dumazet 834fd558d18SJames Chapman /* Get L2TP header flags */ 835fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 836fd558d18SJames Chapman 837fd558d18SJames Chapman /* Check protocol version */ 838fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 839fd558d18SJames Chapman if (version != tunnel->version) { 840a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 841fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 842fd558d18SJames Chapman tunnel->name, version, tunnel->version); 843fd558d18SJames Chapman goto error; 844fd558d18SJames Chapman } 845fd558d18SJames Chapman 846fd558d18SJames Chapman /* Get length of L2TP packet */ 847fd558d18SJames Chapman length = skb->len; 848fd558d18SJames Chapman 849fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 850fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 851a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, 852a4ca44faSJoe Perches "%s: recv control packet, len=%d\n", 853a4ca44faSJoe Perches tunnel->name, length); 854fd558d18SJames Chapman goto error; 855fd558d18SJames Chapman } 856fd558d18SJames Chapman 857fd558d18SJames Chapman /* Skip flags */ 858fd558d18SJames Chapman ptr += 2; 859fd558d18SJames Chapman 860f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 861fd558d18SJames Chapman /* If length is present, skip it */ 862fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 863fd558d18SJames Chapman ptr += 2; 864fd558d18SJames Chapman 865fd558d18SJames Chapman /* Extract tunnel and session ID */ 866fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 867fd558d18SJames Chapman ptr += 2; 868fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 869fd558d18SJames Chapman ptr += 2; 870f7faffa3SJames Chapman } else { 871f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 872f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 873f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 874f7faffa3SJames Chapman ptr += 4; 875f7faffa3SJames Chapman } 876fd558d18SJames Chapman 877fd558d18SJames Chapman /* Find the session context */ 878f7faffa3SJames Chapman session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); 879309795f4SJames Chapman if (!session || !session->recv_skb) { 880fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 881a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 882f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 883fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 884fd558d18SJames Chapman goto error; 885fd558d18SJames Chapman } 886fd558d18SJames Chapman 887f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 888fd558d18SJames Chapman 889fd558d18SJames Chapman return 0; 890fd558d18SJames Chapman 891fd558d18SJames Chapman discard_bad_csum: 892fd558d18SJames Chapman LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name); 893fd558d18SJames Chapman UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0); 8945de7aee5SJames Chapman tstats = &tunnel->stats; 8955de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 8965de7aee5SJames Chapman tstats->rx_errors++; 8975de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 898fd558d18SJames Chapman kfree_skb(skb); 899fd558d18SJames Chapman 900fd558d18SJames Chapman return 0; 901fd558d18SJames Chapman 902fd558d18SJames Chapman error: 903fd558d18SJames Chapman /* Put UDP header back */ 904fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 905fd558d18SJames Chapman 906fd558d18SJames Chapman return 1; 907fd558d18SJames Chapman } 908fd558d18SJames Chapman 909fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 910fd558d18SJames Chapman * Return codes: 911fd558d18SJames Chapman * 0 : success. 912fd558d18SJames Chapman * <0: error 913fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 914fd558d18SJames Chapman */ 915fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 916fd558d18SJames Chapman { 917fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 918fd558d18SJames Chapman 919fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 920fd558d18SJames Chapman if (tunnel == NULL) 921fd558d18SJames Chapman goto pass_up; 922fd558d18SJames Chapman 923a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 924a4ca44faSJoe Perches tunnel->name, skb->len); 925fd558d18SJames Chapman 926fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 927fd558d18SJames Chapman goto pass_up_put; 928fd558d18SJames Chapman 929fd558d18SJames Chapman sock_put(sk); 930fd558d18SJames Chapman return 0; 931fd558d18SJames Chapman 932fd558d18SJames Chapman pass_up_put: 933fd558d18SJames Chapman sock_put(sk); 934fd558d18SJames Chapman pass_up: 935fd558d18SJames Chapman return 1; 936fd558d18SJames Chapman } 937fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 938fd558d18SJames Chapman 939fd558d18SJames Chapman /************************************************************************ 940fd558d18SJames Chapman * Transmit handling 941fd558d18SJames Chapman ***********************************************************************/ 942fd558d18SJames Chapman 943fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 944fd558d18SJames Chapman */ 945f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 946fd558d18SJames Chapman { 947f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 948fd558d18SJames Chapman __be16 *bufp = buf; 949f7faffa3SJames Chapman __be16 *optr = buf; 950fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 951fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 952fd558d18SJames Chapman u32 session_id = session->peer_session_id; 953fd558d18SJames Chapman 954fd558d18SJames Chapman if (session->send_seq) 955fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 956fd558d18SJames Chapman 957fd558d18SJames Chapman /* Setup L2TP header. */ 958fd558d18SJames Chapman *bufp++ = htons(flags); 959fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 960fd558d18SJames Chapman *bufp++ = htons(session_id); 961fd558d18SJames Chapman if (session->send_seq) { 962fd558d18SJames Chapman *bufp++ = htons(session->ns); 963fd558d18SJames Chapman *bufp++ = 0; 964fd558d18SJames Chapman session->ns++; 965f7faffa3SJames Chapman session->ns &= 0xffff; 966a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 967a4ca44faSJoe Perches session->name, session->ns); 968fd558d18SJames Chapman } 969fd558d18SJames Chapman 970f7faffa3SJames Chapman return bufp - optr; 971f7faffa3SJames Chapman } 972f7faffa3SJames Chapman 973f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 974fd558d18SJames Chapman { 9750d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 976f7faffa3SJames Chapman char *bufp = buf; 977f7faffa3SJames Chapman char *optr = bufp; 978fd558d18SJames Chapman 9790d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 9800d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 9810d76751fSJames Chapman */ 9820d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 9830d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 984f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 985f7faffa3SJames Chapman bufp += 2; 986f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 987f7faffa3SJames Chapman bufp += 2; 9880d76751fSJames Chapman } 9890d76751fSJames Chapman 990f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 991f7faffa3SJames Chapman bufp += 4; 992f7faffa3SJames Chapman if (session->cookie_len) { 993f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 994f7faffa3SJames Chapman bufp += session->cookie_len; 995fd558d18SJames Chapman } 996f7faffa3SJames Chapman if (session->l2specific_len) { 997f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 998f7faffa3SJames Chapman u32 l2h = 0; 999f7faffa3SJames Chapman if (session->send_seq) { 1000f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1001f7faffa3SJames Chapman session->ns++; 1002f7faffa3SJames Chapman session->ns &= 0xffffff; 1003a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 1004a4ca44faSJoe Perches "%s: updated ns to %u\n", 1005a4ca44faSJoe Perches session->name, session->ns); 1006f7faffa3SJames Chapman } 1007f7faffa3SJames Chapman 1008f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 1009f7faffa3SJames Chapman } 1010f7faffa3SJames Chapman bufp += session->l2specific_len; 1011f7faffa3SJames Chapman } 1012f7faffa3SJames Chapman if (session->offset) 1013f7faffa3SJames Chapman bufp += session->offset; 1014f7faffa3SJames Chapman 1015f7faffa3SJames Chapman return bufp - optr; 1016f7faffa3SJames Chapman } 1017fd558d18SJames Chapman 1018fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1019d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1020fd558d18SJames Chapman { 1021fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1022fd558d18SJames Chapman unsigned int len = skb->len; 1023fd558d18SJames Chapman int error; 10245de7aee5SJames Chapman struct l2tp_stats *tstats, *sstats; 1025fd558d18SJames Chapman 1026fd558d18SJames Chapman /* Debug */ 1027fd558d18SJames Chapman if (session->send_seq) 1028a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n", 1029a4ca44faSJoe Perches session->name, data_len, session->ns - 1); 1030fd558d18SJames Chapman else 1031a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n", 1032a4ca44faSJoe Perches session->name, data_len); 1033fd558d18SJames Chapman 1034fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 10350d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10360d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1037fd558d18SJames Chapman 1038a4ca44faSJoe Perches pr_debug("%s: xmit\n", session->name); 1039a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1040a4ca44faSJoe Perches datap, min_t(size_t, 32, len - uhlen)); 1041fd558d18SJames Chapman } 1042fd558d18SJames Chapman 1043fd558d18SJames Chapman /* Queue the packet to IP for output */ 10444e15ed4dSShan Wei skb->local_df = 1; 1045d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1046d2cf3361SBenjamin LaHaise if (skb->sk->sk_family == PF_INET6) 1047d2cf3361SBenjamin LaHaise error = inet6_csk_xmit(skb, NULL); 1048d2cf3361SBenjamin LaHaise else 1049d2cf3361SBenjamin LaHaise #endif 1050d9d8da80SDavid S. Miller error = ip_queue_xmit(skb, fl); 1051fd558d18SJames Chapman 1052fd558d18SJames Chapman /* Update stats */ 10535de7aee5SJames Chapman tstats = &tunnel->stats; 10545de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 10555de7aee5SJames Chapman sstats = &session->stats; 10565de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 1057fd558d18SJames Chapman if (error >= 0) { 10585de7aee5SJames Chapman tstats->tx_packets++; 10595de7aee5SJames Chapman tstats->tx_bytes += len; 10605de7aee5SJames Chapman sstats->tx_packets++; 10615de7aee5SJames Chapman sstats->tx_bytes += len; 1062fd558d18SJames Chapman } else { 10635de7aee5SJames Chapman tstats->tx_errors++; 10645de7aee5SJames Chapman sstats->tx_errors++; 1065fd558d18SJames Chapman } 10665de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 10675de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 1068fd558d18SJames Chapman 1069fd558d18SJames Chapman return 0; 1070fd558d18SJames Chapman } 1071fd558d18SJames Chapman 1072fd558d18SJames Chapman /* Automatically called when the skb is freed. 1073fd558d18SJames Chapman */ 1074fd558d18SJames Chapman static void l2tp_sock_wfree(struct sk_buff *skb) 1075fd558d18SJames Chapman { 1076fd558d18SJames Chapman sock_put(skb->sk); 1077fd558d18SJames Chapman } 1078fd558d18SJames Chapman 1079fd558d18SJames Chapman /* For data skbs that we transmit, we associate with the tunnel socket 1080fd558d18SJames Chapman * but don't do accounting. 1081fd558d18SJames Chapman */ 1082fd558d18SJames Chapman static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk) 1083fd558d18SJames Chapman { 1084fd558d18SJames Chapman sock_hold(sk); 1085fd558d18SJames Chapman skb->sk = sk; 1086fd558d18SJames Chapman skb->destructor = l2tp_sock_wfree; 1087fd558d18SJames Chapman } 1088fd558d18SJames Chapman 1089d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1090d2cf3361SBenjamin LaHaise static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb, 1091d2cf3361SBenjamin LaHaise int udp_len) 1092d2cf3361SBenjamin LaHaise { 1093d2cf3361SBenjamin LaHaise struct ipv6_pinfo *np = inet6_sk(sk); 1094d2cf3361SBenjamin LaHaise struct udphdr *uh = udp_hdr(skb); 1095d2cf3361SBenjamin LaHaise 1096d2cf3361SBenjamin LaHaise if (!skb_dst(skb) || !skb_dst(skb)->dev || 1097d2cf3361SBenjamin LaHaise !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) { 1098d2cf3361SBenjamin LaHaise __wsum csum = skb_checksum(skb, 0, udp_len, 0); 1099d2cf3361SBenjamin LaHaise skb->ip_summed = CHECKSUM_UNNECESSARY; 1100d2cf3361SBenjamin LaHaise uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len, 1101d2cf3361SBenjamin LaHaise IPPROTO_UDP, csum); 1102d2cf3361SBenjamin LaHaise if (uh->check == 0) 1103d2cf3361SBenjamin LaHaise uh->check = CSUM_MANGLED_0; 1104d2cf3361SBenjamin LaHaise } else { 1105d2cf3361SBenjamin LaHaise skb->ip_summed = CHECKSUM_PARTIAL; 1106d2cf3361SBenjamin LaHaise skb->csum_start = skb_transport_header(skb) - skb->head; 1107d2cf3361SBenjamin LaHaise skb->csum_offset = offsetof(struct udphdr, check); 1108d2cf3361SBenjamin LaHaise uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr, 1109d2cf3361SBenjamin LaHaise udp_len, IPPROTO_UDP, 0); 1110d2cf3361SBenjamin LaHaise } 1111d2cf3361SBenjamin LaHaise } 1112d2cf3361SBenjamin LaHaise #endif 1113d2cf3361SBenjamin LaHaise 1114fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1115fd558d18SJames Chapman * inserted in the skb data before calling this function. 1116fd558d18SJames Chapman */ 1117fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1118fd558d18SJames Chapman { 1119fd558d18SJames Chapman int data_len = skb->len; 11200d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 11210d76751fSJames Chapman struct sock *sk = tunnel->sock; 1122d9d8da80SDavid S. Miller struct flowi *fl; 1123fd558d18SJames Chapman struct udphdr *uh; 1124fd558d18SJames Chapman struct inet_sock *inet; 1125fd558d18SJames Chapman __wsum csum; 1126fd558d18SJames Chapman int old_headroom; 1127fd558d18SJames Chapman int new_headroom; 1128fd558d18SJames Chapman int headroom; 11290d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11300d76751fSJames Chapman int udp_len; 1131b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1132fd558d18SJames Chapman 1133fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1134fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1135fd558d18SJames Chapman * make room. Adjust truesize. 1136fd558d18SJames Chapman */ 1137fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11380d76751fSJames Chapman uhlen + hdr_len; 1139fd558d18SJames Chapman old_headroom = skb_headroom(skb); 1140835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1141b8c84307SEric Dumazet kfree_skb(skb); 1142b8c84307SEric Dumazet return NET_XMIT_DROP; 1143835acf5dSEric Dumazet } 1144fd558d18SJames Chapman 1145fd558d18SJames Chapman new_headroom = skb_headroom(skb); 1146fd558d18SJames Chapman skb_orphan(skb); 1147fd558d18SJames Chapman skb->truesize += new_headroom - old_headroom; 1148fd558d18SJames Chapman 1149fd558d18SJames Chapman /* Setup L2TP header */ 1150f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1151fd558d18SJames Chapman 11520d76751fSJames Chapman /* Reset skb netfilter state */ 1153fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1154fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1155fd558d18SJames Chapman IPSKB_REROUTED); 1156fd558d18SJames Chapman nf_reset(skb); 1157fd558d18SJames Chapman 11586af88da1SDavid S. Miller bh_lock_sock(sk); 11596af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1160b8c84307SEric Dumazet kfree_skb(skb); 1161b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11626af88da1SDavid S. Miller goto out_unlock; 11636af88da1SDavid S. Miller } 11646af88da1SDavid S. Miller 1165fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1166fd558d18SJames Chapman skb_dst_drop(skb); 116771b1391aSFlorian Westphal skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 11680d76751fSJames Chapman 1169d9d8da80SDavid S. Miller inet = inet_sk(sk); 1170d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11710d76751fSJames Chapman switch (tunnel->encap) { 11720d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 11730d76751fSJames Chapman /* Setup UDP header */ 11740d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11750d76751fSJames Chapman skb_reset_transport_header(skb); 11760d76751fSJames Chapman uh = udp_hdr(skb); 11770d76751fSJames Chapman uh->source = inet->inet_sport; 11780d76751fSJames Chapman uh->dest = inet->inet_dport; 11790d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11800d76751fSJames Chapman uh->len = htons(udp_len); 11810d76751fSJames Chapman uh->check = 0; 1182fd558d18SJames Chapman 1183fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1184d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1185d2cf3361SBenjamin LaHaise if (sk->sk_family == PF_INET6) 1186d2cf3361SBenjamin LaHaise l2tp_xmit_ipv6_csum(sk, skb, udp_len); 1187d2cf3361SBenjamin LaHaise else 1188d2cf3361SBenjamin LaHaise #endif 1189fd558d18SJames Chapman if (sk->sk_no_check == UDP_CSUM_NOXMIT) 1190fd558d18SJames Chapman skb->ip_summed = CHECKSUM_NONE; 1191fd558d18SJames Chapman else if ((skb_dst(skb) && skb_dst(skb)->dev) && 1192fd558d18SJames Chapman (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) { 1193fd558d18SJames Chapman skb->ip_summed = CHECKSUM_COMPLETE; 1194fd558d18SJames Chapman csum = skb_checksum(skb, 0, udp_len, 0); 1195fd558d18SJames Chapman uh->check = csum_tcpudp_magic(inet->inet_saddr, 1196fd558d18SJames Chapman inet->inet_daddr, 1197fd558d18SJames Chapman udp_len, IPPROTO_UDP, csum); 1198fd558d18SJames Chapman if (uh->check == 0) 1199fd558d18SJames Chapman uh->check = CSUM_MANGLED_0; 1200fd558d18SJames Chapman } else { 1201fd558d18SJames Chapman skb->ip_summed = CHECKSUM_PARTIAL; 1202fd558d18SJames Chapman skb->csum_start = skb_transport_header(skb) - skb->head; 1203fd558d18SJames Chapman skb->csum_offset = offsetof(struct udphdr, check); 1204fd558d18SJames Chapman uh->check = ~csum_tcpudp_magic(inet->inet_saddr, 1205fd558d18SJames Chapman inet->inet_daddr, 1206fd558d18SJames Chapman udp_len, IPPROTO_UDP, 0); 1207fd558d18SJames Chapman } 12080d76751fSJames Chapman break; 12090d76751fSJames Chapman 12100d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12110d76751fSJames Chapman break; 12120d76751fSJames Chapman } 12130d76751fSJames Chapman 12140d76751fSJames Chapman l2tp_skb_set_owner_w(skb, sk); 1215fd558d18SJames Chapman 1216d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 12176af88da1SDavid S. Miller out_unlock: 12186af88da1SDavid S. Miller bh_unlock_sock(sk); 1219fd558d18SJames Chapman 1220b8c84307SEric Dumazet return ret; 1221fd558d18SJames Chapman } 1222fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1223fd558d18SJames Chapman 1224fd558d18SJames Chapman /***************************************************************************** 1225fd558d18SJames Chapman * Tinnel and session create/destroy. 1226fd558d18SJames Chapman *****************************************************************************/ 1227fd558d18SJames Chapman 1228fd558d18SJames Chapman /* Tunnel socket destruct hook. 1229fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1230fd558d18SJames Chapman * closed. 1231fd558d18SJames Chapman */ 1232fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1233fd558d18SJames Chapman { 1234fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1235fd558d18SJames Chapman 1236fd558d18SJames Chapman tunnel = sk->sk_user_data; 1237fd558d18SJames Chapman if (tunnel == NULL) 1238fd558d18SJames Chapman goto end; 1239fd558d18SJames Chapman 1240a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1241fd558d18SJames Chapman 1242fd558d18SJames Chapman /* Close all sessions */ 1243fd558d18SJames Chapman l2tp_tunnel_closeall(tunnel); 1244fd558d18SJames Chapman 12450d76751fSJames Chapman switch (tunnel->encap) { 12460d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1247fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1248fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1249fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12500d76751fSJames Chapman break; 12510d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12520d76751fSJames Chapman break; 12530d76751fSJames Chapman } 1254fd558d18SJames Chapman 1255fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1256fd558d18SJames Chapman tunnel->sock = NULL; 1257fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1258fd558d18SJames Chapman sk->sk_user_data = NULL; 1259fd558d18SJames Chapman 1260fd558d18SJames Chapman /* Call the original destructor */ 1261fd558d18SJames Chapman if (sk->sk_destruct) 1262fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1263fd558d18SJames Chapman 1264fd558d18SJames Chapman /* We're finished with the socket */ 1265fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1266fd558d18SJames Chapman 1267fd558d18SJames Chapman end: 1268fd558d18SJames Chapman return; 1269fd558d18SJames Chapman } 1270fd558d18SJames Chapman 1271fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1272fd558d18SJames Chapman */ 1273fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1274fd558d18SJames Chapman { 1275fd558d18SJames Chapman int hash; 1276fd558d18SJames Chapman struct hlist_node *walk; 1277fd558d18SJames Chapman struct hlist_node *tmp; 1278fd558d18SJames Chapman struct l2tp_session *session; 1279fd558d18SJames Chapman 1280fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1281fd558d18SJames Chapman 1282a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1283a4ca44faSJoe Perches tunnel->name); 1284fd558d18SJames Chapman 1285fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1286fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1287fd558d18SJames Chapman again: 1288fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1289fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1290fd558d18SJames Chapman 1291a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1292fd558d18SJames Chapman "%s: closing session\n", session->name); 1293fd558d18SJames Chapman 1294fd558d18SJames Chapman hlist_del_init(&session->hlist); 1295fd558d18SJames Chapman 1296fd558d18SJames Chapman /* Since we should hold the sock lock while 1297fd558d18SJames Chapman * doing any unbinding, we need to release the 1298fd558d18SJames Chapman * lock we're holding before taking that lock. 1299fd558d18SJames Chapman * Hold a reference to the sock so it doesn't 1300fd558d18SJames Chapman * disappear as we're jumping between locks. 1301fd558d18SJames Chapman */ 1302fd558d18SJames Chapman if (session->ref != NULL) 1303fd558d18SJames Chapman (*session->ref)(session); 1304fd558d18SJames Chapman 1305fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1306fd558d18SJames Chapman 1307f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1308f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1309f7faffa3SJames Chapman 1310e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1311e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1312e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1313e02d494dSJames Chapman synchronize_rcu(); 1314f7faffa3SJames Chapman } 1315f7faffa3SJames Chapman 1316fd558d18SJames Chapman if (session->session_close != NULL) 1317fd558d18SJames Chapman (*session->session_close)(session); 1318fd558d18SJames Chapman 1319fd558d18SJames Chapman if (session->deref != NULL) 1320fd558d18SJames Chapman (*session->deref)(session); 1321fd558d18SJames Chapman 1322fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1323fd558d18SJames Chapman 1324fd558d18SJames Chapman /* Now restart from the beginning of this hash 1325fd558d18SJames Chapman * chain. We always remove a session from the 1326fd558d18SJames Chapman * list so we are guaranteed to make forward 1327fd558d18SJames Chapman * progress. 1328fd558d18SJames Chapman */ 1329fd558d18SJames Chapman goto again; 1330fd558d18SJames Chapman } 1331fd558d18SJames Chapman } 1332fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1333fd558d18SJames Chapman } 1334fd558d18SJames Chapman 1335fd558d18SJames Chapman /* Really kill the tunnel. 1336fd558d18SJames Chapman * Come here only when all sessions have been cleared from the tunnel. 1337fd558d18SJames Chapman */ 1338fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 1339fd558d18SJames Chapman { 1340fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1341fd558d18SJames Chapman 1342fd558d18SJames Chapman BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1343fd558d18SJames Chapman BUG_ON(tunnel->sock != NULL); 1344fd558d18SJames Chapman 1345a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name); 1346fd558d18SJames Chapman 1347fd558d18SJames Chapman /* Remove from tunnel list */ 1348e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1349e02d494dSJames Chapman list_del_rcu(&tunnel->list); 1350e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1351e02d494dSJames Chapman synchronize_rcu(); 1352fd558d18SJames Chapman 1353fd558d18SJames Chapman atomic_dec(&l2tp_tunnel_count); 1354fd558d18SJames Chapman kfree(tunnel); 1355fd558d18SJames Chapman } 1356fd558d18SJames Chapman 1357789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1358789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1359789a4a2cSJames Chapman * managing L2TP daemon. 1360789a4a2cSJames Chapman */ 1361789a4a2cSJames Chapman static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp) 1362789a4a2cSJames Chapman { 1363789a4a2cSJames Chapman int err = -EINVAL; 1364789a4a2cSJames Chapman struct sockaddr_in udp_addr; 1365f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1366f9bac8dfSChris Elston struct sockaddr_in6 udp6_addr; 13675dac94e1SJames Chapman struct sockaddr_l2tpip6 ip6_addr; 1368f9bac8dfSChris Elston #endif 1369789a4a2cSJames Chapman struct sockaddr_l2tpip ip_addr; 13707bddd0dbSEric Dumazet struct socket *sock = NULL; 1371789a4a2cSJames Chapman 1372789a4a2cSJames Chapman switch (cfg->encap) { 1373789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 1374f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1375f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 1376f9bac8dfSChris Elston err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp); 1377f9bac8dfSChris Elston if (err < 0) 1378f9bac8dfSChris Elston goto out; 1379f9bac8dfSChris Elston 1380f9bac8dfSChris Elston sock = *sockp; 1381f9bac8dfSChris Elston 1382f9bac8dfSChris Elston memset(&udp6_addr, 0, sizeof(udp6_addr)); 1383f9bac8dfSChris Elston udp6_addr.sin6_family = AF_INET6; 1384f9bac8dfSChris Elston memcpy(&udp6_addr.sin6_addr, cfg->local_ip6, 1385f9bac8dfSChris Elston sizeof(udp6_addr.sin6_addr)); 1386f9bac8dfSChris Elston udp6_addr.sin6_port = htons(cfg->local_udp_port); 1387f9bac8dfSChris Elston err = kernel_bind(sock, (struct sockaddr *) &udp6_addr, 1388f9bac8dfSChris Elston sizeof(udp6_addr)); 1389f9bac8dfSChris Elston if (err < 0) 1390f9bac8dfSChris Elston goto out; 1391f9bac8dfSChris Elston 1392f9bac8dfSChris Elston udp6_addr.sin6_family = AF_INET6; 1393f9bac8dfSChris Elston memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6, 1394f9bac8dfSChris Elston sizeof(udp6_addr.sin6_addr)); 1395f9bac8dfSChris Elston udp6_addr.sin6_port = htons(cfg->peer_udp_port); 1396f9bac8dfSChris Elston err = kernel_connect(sock, 1397f9bac8dfSChris Elston (struct sockaddr *) &udp6_addr, 1398f9bac8dfSChris Elston sizeof(udp6_addr), 0); 1399f9bac8dfSChris Elston if (err < 0) 1400f9bac8dfSChris Elston goto out; 1401f9bac8dfSChris Elston } else 1402f9bac8dfSChris Elston #endif 1403f9bac8dfSChris Elston { 1404789a4a2cSJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp); 1405789a4a2cSJames Chapman if (err < 0) 1406789a4a2cSJames Chapman goto out; 1407789a4a2cSJames Chapman 1408789a4a2cSJames Chapman sock = *sockp; 1409789a4a2cSJames Chapman 1410789a4a2cSJames Chapman memset(&udp_addr, 0, sizeof(udp_addr)); 1411789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1412789a4a2cSJames Chapman udp_addr.sin_addr = cfg->local_ip; 1413789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->local_udp_port); 1414f9bac8dfSChris Elston err = kernel_bind(sock, (struct sockaddr *) &udp_addr, 1415f9bac8dfSChris Elston sizeof(udp_addr)); 1416789a4a2cSJames Chapman if (err < 0) 1417789a4a2cSJames Chapman goto out; 1418789a4a2cSJames Chapman 1419789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1420789a4a2cSJames Chapman udp_addr.sin_addr = cfg->peer_ip; 1421789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->peer_udp_port); 1422f9bac8dfSChris Elston err = kernel_connect(sock, 1423f9bac8dfSChris Elston (struct sockaddr *) &udp_addr, 1424f9bac8dfSChris Elston sizeof(udp_addr), 0); 1425789a4a2cSJames Chapman if (err < 0) 1426789a4a2cSJames Chapman goto out; 1427f9bac8dfSChris Elston } 1428789a4a2cSJames Chapman 1429789a4a2cSJames Chapman if (!cfg->use_udp_checksums) 1430789a4a2cSJames Chapman sock->sk->sk_no_check = UDP_CSUM_NOXMIT; 1431789a4a2cSJames Chapman 1432789a4a2cSJames Chapman break; 1433789a4a2cSJames Chapman 1434789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1435f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1436f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 14375dac94e1SJames Chapman err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP, 14385dac94e1SJames Chapman sockp); 14395dac94e1SJames Chapman if (err < 0) 1440f9bac8dfSChris Elston goto out; 14415dac94e1SJames Chapman 14425dac94e1SJames Chapman sock = *sockp; 14435dac94e1SJames Chapman 14445dac94e1SJames Chapman memset(&ip6_addr, 0, sizeof(ip6_addr)); 14455dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14465dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14475dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14485dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14495dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, 14505dac94e1SJames Chapman sizeof(ip6_addr)); 14515dac94e1SJames Chapman if (err < 0) 14525dac94e1SJames Chapman goto out; 14535dac94e1SJames Chapman 14545dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14555dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14565dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14575dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14585dac94e1SJames Chapman err = kernel_connect(sock, 14595dac94e1SJames Chapman (struct sockaddr *) &ip6_addr, 14605dac94e1SJames Chapman sizeof(ip6_addr), 0); 14615dac94e1SJames Chapman if (err < 0) 14625dac94e1SJames Chapman goto out; 14635dac94e1SJames Chapman } else 1464f9bac8dfSChris Elston #endif 14655dac94e1SJames Chapman { 14665dac94e1SJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, 14675dac94e1SJames Chapman sockp); 1468789a4a2cSJames Chapman if (err < 0) 1469789a4a2cSJames Chapman goto out; 1470789a4a2cSJames Chapman 1471789a4a2cSJames Chapman sock = *sockp; 1472789a4a2cSJames Chapman 1473789a4a2cSJames Chapman memset(&ip_addr, 0, sizeof(ip_addr)); 1474789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1475789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1476789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14775dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 14785dac94e1SJames Chapman sizeof(ip_addr)); 1479789a4a2cSJames Chapman if (err < 0) 1480789a4a2cSJames Chapman goto out; 1481789a4a2cSJames Chapman 1482789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1483789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1484789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14855dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 14865dac94e1SJames Chapman sizeof(ip_addr), 0); 1487789a4a2cSJames Chapman if (err < 0) 1488789a4a2cSJames Chapman goto out; 14895dac94e1SJames Chapman } 1490789a4a2cSJames Chapman break; 1491789a4a2cSJames Chapman 1492789a4a2cSJames Chapman default: 1493789a4a2cSJames Chapman goto out; 1494789a4a2cSJames Chapman } 1495789a4a2cSJames Chapman 1496789a4a2cSJames Chapman out: 1497789a4a2cSJames Chapman if ((err < 0) && sock) { 1498789a4a2cSJames Chapman sock_release(sock); 1499789a4a2cSJames Chapman *sockp = NULL; 1500789a4a2cSJames Chapman } 1501789a4a2cSJames Chapman 1502789a4a2cSJames Chapman return err; 1503789a4a2cSJames Chapman } 1504789a4a2cSJames Chapman 1505fd558d18SJames 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) 1506fd558d18SJames Chapman { 1507fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1508fd558d18SJames Chapman int err; 1509fd558d18SJames Chapman struct socket *sock = NULL; 1510fd558d18SJames Chapman struct sock *sk = NULL; 1511fd558d18SJames Chapman struct l2tp_net *pn; 15120d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1513fd558d18SJames Chapman 1514fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1515789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1516789a4a2cSJames Chapman * kernel socket. 1517fd558d18SJames Chapman */ 1518789a4a2cSJames Chapman if (fd < 0) { 1519789a4a2cSJames Chapman err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock); 1520789a4a2cSJames Chapman if (err < 0) 1521789a4a2cSJames Chapman goto err; 1522789a4a2cSJames Chapman } else { 1523fd558d18SJames Chapman err = -EBADF; 1524fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1525fd558d18SJames Chapman if (!sock) { 1526a4ca44faSJoe Perches pr_err("tunl %hu: sockfd_lookup(fd=%d) returned %d\n", 1527fd558d18SJames Chapman tunnel_id, fd, err); 1528fd558d18SJames Chapman goto err; 1529fd558d18SJames Chapman } 1530789a4a2cSJames Chapman } 1531fd558d18SJames Chapman 1532fd558d18SJames Chapman sk = sock->sk; 1533fd558d18SJames Chapman 15340d76751fSJames Chapman if (cfg != NULL) 15350d76751fSJames Chapman encap = cfg->encap; 15360d76751fSJames Chapman 1537fd558d18SJames Chapman /* Quick sanity checks */ 15380d76751fSJames Chapman switch (encap) { 15390d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1540fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1541fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1542a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1543fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1544fd558d18SJames Chapman goto err; 1545fd558d18SJames Chapman } 15460d76751fSJames Chapman break; 15470d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 15480d76751fSJames Chapman err = -EPROTONOSUPPORT; 15490d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 1550a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 15510d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1552fd558d18SJames Chapman goto err; 1553fd558d18SJames Chapman } 15540d76751fSJames Chapman break; 15550d76751fSJames Chapman } 1556fd558d18SJames Chapman 1557fd558d18SJames Chapman /* Check if this socket has already been prepped */ 1558fd558d18SJames Chapman tunnel = (struct l2tp_tunnel *)sk->sk_user_data; 1559fd558d18SJames Chapman if (tunnel != NULL) { 1560fd558d18SJames Chapman /* This socket has already been prepped */ 1561fd558d18SJames Chapman err = -EBUSY; 1562fd558d18SJames Chapman goto err; 1563fd558d18SJames Chapman } 1564fd558d18SJames Chapman 1565fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1566fd558d18SJames Chapman if (tunnel == NULL) { 1567fd558d18SJames Chapman err = -ENOMEM; 1568fd558d18SJames Chapman goto err; 1569fd558d18SJames Chapman } 1570fd558d18SJames Chapman 1571fd558d18SJames Chapman tunnel->version = version; 1572fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1573fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1574fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1575fd558d18SJames Chapman 1576fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1577fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1578fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1579fd558d18SJames Chapman 1580fd558d18SJames Chapman /* The net we belong to */ 1581fd558d18SJames Chapman tunnel->l2tp_net = net; 1582fd558d18SJames Chapman pn = l2tp_pernet(net); 1583fd558d18SJames Chapman 15840d76751fSJames Chapman if (cfg != NULL) 1585fd558d18SJames Chapman tunnel->debug = cfg->debug; 1586fd558d18SJames Chapman 1587fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 15880d76751fSJames Chapman tunnel->encap = encap; 15890d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 15900d76751fSJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1591fd558d18SJames Chapman udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP; 1592fd558d18SJames Chapman udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv; 1593d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1594d2cf3361SBenjamin LaHaise if (sk->sk_family == PF_INET6) 1595d2cf3361SBenjamin LaHaise udpv6_encap_enable(); 1596d2cf3361SBenjamin LaHaise else 1597d2cf3361SBenjamin LaHaise #endif 1598447167bfSEric Dumazet udp_encap_enable(); 15990d76751fSJames Chapman } 1600fd558d18SJames Chapman 1601fd558d18SJames Chapman sk->sk_user_data = tunnel; 1602fd558d18SJames Chapman 1603fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1604fd558d18SJames Chapman * if the tunnel socket goes away. 1605fd558d18SJames Chapman */ 1606fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1607fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1608fd558d18SJames Chapman tunnel->sock = sk; 1609fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1610fd558d18SJames Chapman 1611fd558d18SJames Chapman /* Add tunnel to our list */ 1612fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1613fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1614fd558d18SJames Chapman 1615fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 16161769192aSEric Dumazet * only when this drops to zero. Must be done before list insertion 1617fd558d18SJames Chapman */ 1618fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 16191769192aSEric Dumazet spin_lock_bh(&pn->l2tp_tunnel_list_lock); 16201769192aSEric Dumazet list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 16211769192aSEric Dumazet spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1622fd558d18SJames Chapman 1623fd558d18SJames Chapman err = 0; 1624fd558d18SJames Chapman err: 1625fd558d18SJames Chapman if (tunnelp) 1626fd558d18SJames Chapman *tunnelp = tunnel; 1627fd558d18SJames Chapman 1628789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1629789a4a2cSJames Chapman * have a file. 1630789a4a2cSJames Chapman */ 1631789a4a2cSJames Chapman if (sock && sock->file) 1632fd558d18SJames Chapman sockfd_put(sock); 1633fd558d18SJames Chapman 1634fd558d18SJames Chapman return err; 1635fd558d18SJames Chapman } 1636fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1637fd558d18SJames Chapman 1638309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1639309795f4SJames Chapman */ 1640309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1641309795f4SJames Chapman { 1642309795f4SJames Chapman int err = 0; 1643789a4a2cSJames Chapman struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL; 1644309795f4SJames Chapman 1645309795f4SJames Chapman /* Force the tunnel socket to close. This will eventually 1646309795f4SJames Chapman * cause the tunnel to be deleted via the normal socket close 1647309795f4SJames Chapman * mechanisms when userspace closes the tunnel socket. 1648309795f4SJames Chapman */ 1649789a4a2cSJames Chapman if (sock != NULL) { 1650789a4a2cSJames Chapman err = inet_shutdown(sock, 2); 1651789a4a2cSJames Chapman 1652789a4a2cSJames Chapman /* If the tunnel's socket was created by the kernel, 1653789a4a2cSJames Chapman * close the socket here since the socket was not 1654789a4a2cSJames Chapman * created by userspace. 1655789a4a2cSJames Chapman */ 1656789a4a2cSJames Chapman if (sock->file == NULL) 1657789a4a2cSJames Chapman err = inet_release(sock); 1658789a4a2cSJames Chapman } 1659309795f4SJames Chapman 1660309795f4SJames Chapman return err; 1661309795f4SJames Chapman } 1662309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1663309795f4SJames Chapman 1664fd558d18SJames Chapman /* Really kill the session. 1665fd558d18SJames Chapman */ 1666fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1667fd558d18SJames Chapman { 1668fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1669fd558d18SJames Chapman 1670fd558d18SJames Chapman BUG_ON(atomic_read(&session->ref_count) != 0); 1671fd558d18SJames Chapman 1672fd558d18SJames Chapman tunnel = session->tunnel; 1673fd558d18SJames Chapman if (tunnel != NULL) { 1674fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1675fd558d18SJames Chapman 1676fd558d18SJames Chapman /* Delete the session from the hash */ 1677fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1678fd558d18SJames Chapman hlist_del_init(&session->hlist); 1679fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1680fd558d18SJames Chapman 1681f7faffa3SJames Chapman /* Unlink from the global hash if not L2TPv2 */ 1682f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1683f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1684f7faffa3SJames Chapman 1685e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1686e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1687e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1688e02d494dSJames Chapman synchronize_rcu(); 1689f7faffa3SJames Chapman } 1690f7faffa3SJames Chapman 1691fd558d18SJames Chapman if (session->session_id != 0) 1692fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1693fd558d18SJames Chapman 1694fd558d18SJames Chapman sock_put(tunnel->sock); 1695fd558d18SJames Chapman 1696fd558d18SJames Chapman /* This will delete the tunnel context if this 1697fd558d18SJames Chapman * is the last session on the tunnel. 1698fd558d18SJames Chapman */ 1699fd558d18SJames Chapman session->tunnel = NULL; 1700fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1701fd558d18SJames Chapman } 1702fd558d18SJames Chapman 1703fd558d18SJames Chapman kfree(session); 1704fd558d18SJames Chapman 1705fd558d18SJames Chapman return; 1706fd558d18SJames Chapman } 1707fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1708fd558d18SJames Chapman 1709309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1710309795f4SJames Chapman pseudowire modules. 1711309795f4SJames Chapman */ 1712309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1713309795f4SJames Chapman { 1714309795f4SJames Chapman if (session->session_close != NULL) 1715309795f4SJames Chapman (*session->session_close)(session); 1716309795f4SJames Chapman 1717309795f4SJames Chapman l2tp_session_dec_refcount(session); 1718309795f4SJames Chapman 1719309795f4SJames Chapman return 0; 1720309795f4SJames Chapman } 1721309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1722309795f4SJames Chapman 1723309795f4SJames Chapman 1724f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1725f7faffa3SJames Chapman * l2specific_len parameters are set. 1726f7faffa3SJames Chapman */ 1727fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1728f7faffa3SJames Chapman { 1729f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1730f7faffa3SJames Chapman session->hdr_len = 6; 1731f7faffa3SJames Chapman if (session->send_seq) 1732f7faffa3SJames Chapman session->hdr_len += 4; 1733f7faffa3SJames Chapman } else { 17340d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 17350d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 17360d76751fSJames Chapman session->hdr_len += 4; 1737f7faffa3SJames Chapman } 1738f7faffa3SJames Chapman 1739f7faffa3SJames Chapman } 1740f7faffa3SJames Chapman 1741fd558d18SJames 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) 1742fd558d18SJames Chapman { 1743fd558d18SJames Chapman struct l2tp_session *session; 1744fd558d18SJames Chapman 1745fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1746fd558d18SJames Chapman if (session != NULL) { 1747fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1748fd558d18SJames Chapman session->tunnel = tunnel; 1749fd558d18SJames Chapman 1750fd558d18SJames Chapman session->session_id = session_id; 1751fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1752d301e325SJames Chapman session->nr = 0; 1753fd558d18SJames Chapman 1754fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1755fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1756fd558d18SJames Chapman 1757fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1758fd558d18SJames Chapman 1759fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1760f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1761fd558d18SJames Chapman 1762fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1763fd558d18SJames Chapman session->debug = tunnel->debug; 1764fd558d18SJames Chapman 1765fd558d18SJames Chapman if (cfg) { 1766f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1767fd558d18SJames Chapman session->debug = cfg->debug; 1768fd558d18SJames Chapman session->mtu = cfg->mtu; 1769fd558d18SJames Chapman session->mru = cfg->mru; 1770fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1771fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1772fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1773f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1774f7faffa3SJames Chapman session->offset = cfg->offset; 1775f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1776f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1777f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1778f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1779f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1780f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1781fd558d18SJames Chapman } 1782fd558d18SJames Chapman 1783f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1784f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1785f7faffa3SJames Chapman else 1786f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1787f7faffa3SJames Chapman 1788f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1789f7faffa3SJames Chapman 1790fd558d18SJames Chapman /* Bump the reference count. The session context is deleted 1791fd558d18SJames Chapman * only when this drops to zero. 1792fd558d18SJames Chapman */ 1793fd558d18SJames Chapman l2tp_session_inc_refcount(session); 1794fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1795fd558d18SJames Chapman 1796fd558d18SJames Chapman /* Ensure tunnel socket isn't deleted */ 1797fd558d18SJames Chapman sock_hold(tunnel->sock); 1798fd558d18SJames Chapman 1799fd558d18SJames Chapman /* Add session to the tunnel's hash list */ 1800fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1801fd558d18SJames Chapman hlist_add_head(&session->hlist, 1802fd558d18SJames Chapman l2tp_session_id_hash(tunnel, session_id)); 1803fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1804fd558d18SJames Chapman 1805f7faffa3SJames Chapman /* And to the global session list if L2TPv3 */ 1806f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1807f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1808f7faffa3SJames Chapman 1809e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1810e02d494dSJames Chapman hlist_add_head_rcu(&session->global_hlist, 1811f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id)); 1812e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1813f7faffa3SJames Chapman } 1814f7faffa3SJames Chapman 1815fd558d18SJames Chapman /* Ignore management session in session count value */ 1816fd558d18SJames Chapman if (session->session_id != 0) 1817fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1818fd558d18SJames Chapman } 1819fd558d18SJames Chapman 1820fd558d18SJames Chapman return session; 1821fd558d18SJames Chapman } 1822fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1823fd558d18SJames Chapman 1824fd558d18SJames Chapman /***************************************************************************** 1825fd558d18SJames Chapman * Init and cleanup 1826fd558d18SJames Chapman *****************************************************************************/ 1827fd558d18SJames Chapman 1828fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1829fd558d18SJames Chapman { 1830e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1831f7faffa3SJames Chapman int hash; 1832fd558d18SJames Chapman 1833fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1834e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1835fd558d18SJames Chapman 1836f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1837f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1838f7faffa3SJames Chapman 1839e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1840f7faffa3SJames Chapman 1841fd558d18SJames Chapman return 0; 1842fd558d18SJames Chapman } 1843fd558d18SJames Chapman 1844fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1845fd558d18SJames Chapman .init = l2tp_init_net, 1846fd558d18SJames Chapman .id = &l2tp_net_id, 1847fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1848fd558d18SJames Chapman }; 1849fd558d18SJames Chapman 1850fd558d18SJames Chapman static int __init l2tp_init(void) 1851fd558d18SJames Chapman { 1852fd558d18SJames Chapman int rc = 0; 1853fd558d18SJames Chapman 1854fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1855fd558d18SJames Chapman if (rc) 1856fd558d18SJames Chapman goto out; 1857fd558d18SJames Chapman 1858a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1859fd558d18SJames Chapman 1860fd558d18SJames Chapman out: 1861fd558d18SJames Chapman return rc; 1862fd558d18SJames Chapman } 1863fd558d18SJames Chapman 1864fd558d18SJames Chapman static void __exit l2tp_exit(void) 1865fd558d18SJames Chapman { 1866fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1867fd558d18SJames Chapman } 1868fd558d18SJames Chapman 1869fd558d18SJames Chapman module_init(l2tp_init); 1870fd558d18SJames Chapman module_exit(l2tp_exit); 1871fd558d18SJames Chapman 1872fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1873fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1874fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1875fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1876fd558d18SJames Chapman 1877