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 21fd558d18SJames Chapman #include <linux/module.h> 22fd558d18SJames Chapman #include <linux/string.h> 23fd558d18SJames Chapman #include <linux/list.h> 24e02d494dSJames Chapman #include <linux/rculist.h> 25fd558d18SJames Chapman #include <linux/uaccess.h> 26fd558d18SJames Chapman 27fd558d18SJames Chapman #include <linux/kernel.h> 28fd558d18SJames Chapman #include <linux/spinlock.h> 29fd558d18SJames Chapman #include <linux/kthread.h> 30fd558d18SJames Chapman #include <linux/sched.h> 31fd558d18SJames Chapman #include <linux/slab.h> 32fd558d18SJames Chapman #include <linux/errno.h> 33fd558d18SJames Chapman #include <linux/jiffies.h> 34fd558d18SJames Chapman 35fd558d18SJames Chapman #include <linux/netdevice.h> 36fd558d18SJames Chapman #include <linux/net.h> 37fd558d18SJames Chapman #include <linux/inetdevice.h> 38fd558d18SJames Chapman #include <linux/skbuff.h> 39fd558d18SJames Chapman #include <linux/init.h> 400d76751fSJames Chapman #include <linux/in.h> 41fd558d18SJames Chapman #include <linux/ip.h> 42fd558d18SJames Chapman #include <linux/udp.h> 430d76751fSJames Chapman #include <linux/l2tp.h> 44fd558d18SJames Chapman #include <linux/hash.h> 45fd558d18SJames Chapman #include <linux/sort.h> 46fd558d18SJames Chapman #include <linux/file.h> 47fd558d18SJames Chapman #include <linux/nsproxy.h> 48fd558d18SJames Chapman #include <net/net_namespace.h> 49fd558d18SJames Chapman #include <net/netns/generic.h> 50fd558d18SJames Chapman #include <net/dst.h> 51fd558d18SJames Chapman #include <net/ip.h> 52fd558d18SJames Chapman #include <net/udp.h> 53309795f4SJames Chapman #include <net/inet_common.h> 54fd558d18SJames Chapman #include <net/xfrm.h> 550d76751fSJames Chapman #include <net/protocol.h> 56d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h> 57d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h> 58d2cf3361SBenjamin LaHaise #include <net/ip6_route.h> 59d499bd2eSDavid S. Miller #include <net/ip6_checksum.h> 60fd558d18SJames Chapman 61fd558d18SJames Chapman #include <asm/byteorder.h> 6260063497SArun Sharma #include <linux/atomic.h> 63fd558d18SJames Chapman 64fd558d18SJames Chapman #include "l2tp_core.h" 65fd558d18SJames Chapman 66fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 67fd558d18SJames Chapman 68fd558d18SJames Chapman /* L2TP header constants */ 69fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 70fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 71fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 72fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 73fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 74fd558d18SJames Chapman 75fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 76fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 77f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 78fd558d18SJames Chapman 79fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 80fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 81fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 82fd558d18SJames Chapman 83fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ 10 84fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ 6 85fd558d18SJames Chapman 86fd558d18SJames Chapman /* Default trace flags */ 87fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 88fd558d18SJames Chapman 89fd558d18SJames Chapman #define PRINTK(_mask, _type, _lvl, _fmt, args...) \ 90fd558d18SJames Chapman do { \ 91fd558d18SJames Chapman if ((_mask) & (_type)) \ 92fd558d18SJames Chapman printk(_lvl "L2TP: " _fmt, ##args); \ 93fd558d18SJames Chapman } while (0) 94fd558d18SJames Chapman 95fd558d18SJames Chapman /* Private data stored for received packets in the skb. 96fd558d18SJames Chapman */ 97fd558d18SJames Chapman struct l2tp_skb_cb { 98f7faffa3SJames Chapman u32 ns; 99fd558d18SJames Chapman u16 has_seq; 100fd558d18SJames Chapman u16 length; 101fd558d18SJames Chapman unsigned long expires; 102fd558d18SJames Chapman }; 103fd558d18SJames Chapman 104fd558d18SJames Chapman #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 105fd558d18SJames Chapman 106fd558d18SJames Chapman static atomic_t l2tp_tunnel_count; 107fd558d18SJames Chapman static atomic_t l2tp_session_count; 108fd558d18SJames Chapman 109fd558d18SJames Chapman /* per-net private data for this module */ 110fd558d18SJames Chapman static unsigned int l2tp_net_id; 111fd558d18SJames Chapman struct l2tp_net { 112fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 113e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 114f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 115e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 116fd558d18SJames Chapman }; 117fd558d18SJames Chapman 118fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version); 119fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); 120fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel); 121fc130840Sstephen hemminger 122fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(struct net *net) 123fd558d18SJames Chapman { 124fd558d18SJames Chapman BUG_ON(!net); 125fd558d18SJames Chapman 126fd558d18SJames Chapman return net_generic(net, l2tp_net_id); 127fd558d18SJames Chapman } 128fd558d18SJames Chapman 129fc130840Sstephen hemminger 130fc130840Sstephen hemminger /* Tunnel reference counts. Incremented per session that is added to 131fc130840Sstephen hemminger * the tunnel. 132fc130840Sstephen hemminger */ 133fc130840Sstephen hemminger static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel) 134fc130840Sstephen hemminger { 135fc130840Sstephen hemminger atomic_inc(&tunnel->ref_count); 136fc130840Sstephen hemminger } 137fc130840Sstephen hemminger 138fc130840Sstephen hemminger static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel) 139fc130840Sstephen hemminger { 140fc130840Sstephen hemminger if (atomic_dec_and_test(&tunnel->ref_count)) 141fc130840Sstephen hemminger l2tp_tunnel_free(tunnel); 142fc130840Sstephen hemminger } 143fc130840Sstephen hemminger #ifdef L2TP_REFCNT_DEBUG 144fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(_t) do { \ 145fc130840Sstephen hemminger printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \ 146fc130840Sstephen hemminger l2tp_tunnel_inc_refcount_1(_t); \ 147fc130840Sstephen hemminger } while (0) 148fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(_t) do { \ 149fc130840Sstephen hemminger printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \ 150fc130840Sstephen hemminger l2tp_tunnel_dec_refcount_1(_t); \ 151fc130840Sstephen hemminger } while (0) 152fc130840Sstephen hemminger #else 153fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t) 154fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t) 155fc130840Sstephen hemminger #endif 156fc130840Sstephen hemminger 157f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 158f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 159f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 160f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 161f7faffa3SJames Chapman */ 162f7faffa3SJames Chapman static inline struct hlist_head * 163f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 164f7faffa3SJames Chapman { 165f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 166f7faffa3SJames Chapman 167f7faffa3SJames Chapman } 168f7faffa3SJames Chapman 169f7faffa3SJames Chapman /* Lookup a session by id in the global session list 170f7faffa3SJames Chapman */ 171f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id) 172f7faffa3SJames Chapman { 173f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 174f7faffa3SJames Chapman struct hlist_head *session_list = 175f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id); 176f7faffa3SJames Chapman struct l2tp_session *session; 177f7faffa3SJames Chapman struct hlist_node *walk; 178f7faffa3SJames Chapman 179e02d494dSJames Chapman rcu_read_lock_bh(); 180e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) { 181f7faffa3SJames Chapman if (session->session_id == session_id) { 182e02d494dSJames Chapman rcu_read_unlock_bh(); 183f7faffa3SJames Chapman return session; 184f7faffa3SJames Chapman } 185f7faffa3SJames Chapman } 186e02d494dSJames Chapman rcu_read_unlock_bh(); 187f7faffa3SJames Chapman 188f7faffa3SJames Chapman return NULL; 189f7faffa3SJames Chapman } 190f7faffa3SJames Chapman 191fd558d18SJames Chapman /* Session hash list. 192fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 193fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 194fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 195fd558d18SJames Chapman * simple bitmask. 196fd558d18SJames Chapman */ 197fd558d18SJames Chapman static inline struct hlist_head * 198fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 199fd558d18SJames Chapman { 200fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 201fd558d18SJames Chapman } 202fd558d18SJames Chapman 203fd558d18SJames Chapman /* Lookup a session by id 204fd558d18SJames Chapman */ 205f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id) 206fd558d18SJames Chapman { 207f7faffa3SJames Chapman struct hlist_head *session_list; 208fd558d18SJames Chapman struct l2tp_session *session; 209fd558d18SJames Chapman struct hlist_node *walk; 210fd558d18SJames Chapman 211f7faffa3SJames Chapman /* In L2TPv3, session_ids are unique over all tunnels and we 212f7faffa3SJames Chapman * sometimes need to look them up before we know the 213f7faffa3SJames Chapman * tunnel. 214f7faffa3SJames Chapman */ 215f7faffa3SJames Chapman if (tunnel == NULL) 216f7faffa3SJames Chapman return l2tp_session_find_2(net, session_id); 217f7faffa3SJames Chapman 218f7faffa3SJames Chapman session_list = l2tp_session_id_hash(tunnel, session_id); 219fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 220fd558d18SJames Chapman hlist_for_each_entry(session, walk, session_list, hlist) { 221fd558d18SJames Chapman if (session->session_id == session_id) { 222fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 223fd558d18SJames Chapman return session; 224fd558d18SJames Chapman } 225fd558d18SJames Chapman } 226fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 227fd558d18SJames Chapman 228fd558d18SJames Chapman return NULL; 229fd558d18SJames Chapman } 230fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find); 231fd558d18SJames Chapman 232fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth) 233fd558d18SJames Chapman { 234fd558d18SJames Chapman int hash; 235fd558d18SJames Chapman struct hlist_node *walk; 236fd558d18SJames Chapman struct l2tp_session *session; 237fd558d18SJames Chapman int count = 0; 238fd558d18SJames Chapman 239fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 240fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 241fd558d18SJames Chapman hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) { 242fd558d18SJames Chapman if (++count > nth) { 243fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 244fd558d18SJames Chapman return session; 245fd558d18SJames Chapman } 246fd558d18SJames Chapman } 247fd558d18SJames Chapman } 248fd558d18SJames Chapman 249fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 250fd558d18SJames Chapman 251fd558d18SJames Chapman return NULL; 252fd558d18SJames Chapman } 253fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth); 254fd558d18SJames Chapman 255309795f4SJames Chapman /* Lookup a session by interface name. 256309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 257309795f4SJames Chapman */ 258309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) 259309795f4SJames Chapman { 260309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 261309795f4SJames Chapman int hash; 262309795f4SJames Chapman struct hlist_node *walk; 263309795f4SJames Chapman struct l2tp_session *session; 264309795f4SJames Chapman 265e02d494dSJames Chapman rcu_read_lock_bh(); 266309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 267e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) { 268309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 269e02d494dSJames Chapman rcu_read_unlock_bh(); 270309795f4SJames Chapman return session; 271309795f4SJames Chapman } 272309795f4SJames Chapman } 273309795f4SJames Chapman } 274309795f4SJames Chapman 275e02d494dSJames Chapman rcu_read_unlock_bh(); 276309795f4SJames Chapman 277309795f4SJames Chapman return NULL; 278309795f4SJames Chapman } 279309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname); 280309795f4SJames Chapman 281fd558d18SJames Chapman /* Lookup a tunnel by id 282fd558d18SJames Chapman */ 283fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id) 284fd558d18SJames Chapman { 285fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 286fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 287fd558d18SJames Chapman 288e02d494dSJames Chapman rcu_read_lock_bh(); 289e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 290fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 291e02d494dSJames Chapman rcu_read_unlock_bh(); 292fd558d18SJames Chapman return tunnel; 293fd558d18SJames Chapman } 294fd558d18SJames Chapman } 295e02d494dSJames Chapman rcu_read_unlock_bh(); 296fd558d18SJames Chapman 297fd558d18SJames Chapman return NULL; 298fd558d18SJames Chapman } 299fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 300fd558d18SJames Chapman 301fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth) 302fd558d18SJames Chapman { 303fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 304fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 305fd558d18SJames Chapman int count = 0; 306fd558d18SJames Chapman 307e02d494dSJames Chapman rcu_read_lock_bh(); 308e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 309fd558d18SJames Chapman if (++count > nth) { 310e02d494dSJames Chapman rcu_read_unlock_bh(); 311fd558d18SJames Chapman return tunnel; 312fd558d18SJames Chapman } 313fd558d18SJames Chapman } 314fd558d18SJames Chapman 315e02d494dSJames Chapman rcu_read_unlock_bh(); 316fd558d18SJames Chapman 317fd558d18SJames Chapman return NULL; 318fd558d18SJames Chapman } 319fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 320fd558d18SJames Chapman 321fd558d18SJames Chapman /***************************************************************************** 322fd558d18SJames Chapman * Receive data handling 323fd558d18SJames Chapman *****************************************************************************/ 324fd558d18SJames Chapman 325fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 326fd558d18SJames Chapman * number. 327fd558d18SJames Chapman */ 328fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 329fd558d18SJames Chapman { 330fd558d18SJames Chapman struct sk_buff *skbp; 331fd558d18SJames Chapman struct sk_buff *tmp; 332f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 3335de7aee5SJames Chapman struct l2tp_stats *sstats; 334fd558d18SJames Chapman 335fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 3365de7aee5SJames Chapman sstats = &session->stats; 337fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 338fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 339fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 340fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 341fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 342fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 343fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 3445de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 3455de7aee5SJames Chapman sstats->rx_oos_packets++; 3465de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 347fd558d18SJames Chapman goto out; 348fd558d18SJames Chapman } 349fd558d18SJames Chapman } 350fd558d18SJames Chapman 351fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 352fd558d18SJames Chapman 353fd558d18SJames Chapman out: 354fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 355fd558d18SJames Chapman } 356fd558d18SJames Chapman 357fd558d18SJames Chapman /* Dequeue a single skb. 358fd558d18SJames Chapman */ 359fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 360fd558d18SJames Chapman { 361fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 362fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 3635de7aee5SJames Chapman struct l2tp_stats *tstats, *sstats; 364fd558d18SJames Chapman 365fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 366fd558d18SJames Chapman * to its current owner (a socket receive buffer). 367fd558d18SJames Chapman */ 368fd558d18SJames Chapman skb_orphan(skb); 369fd558d18SJames Chapman 3705de7aee5SJames Chapman tstats = &tunnel->stats; 3715de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 3725de7aee5SJames Chapman sstats = &session->stats; 3735de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 3745de7aee5SJames Chapman tstats->rx_packets++; 3755de7aee5SJames Chapman tstats->rx_bytes += length; 3765de7aee5SJames Chapman sstats->rx_packets++; 3775de7aee5SJames Chapman sstats->rx_bytes += length; 3785de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 3795de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 380fd558d18SJames Chapman 381fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 382fd558d18SJames Chapman /* Bump our Nr */ 383fd558d18SJames Chapman session->nr++; 384f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 385f7faffa3SJames Chapman session->nr &= 0xffff; 386f7faffa3SJames Chapman else 387f7faffa3SJames Chapman session->nr &= 0xffffff; 388f7faffa3SJames Chapman 389fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 390fd558d18SJames Chapman "%s: updated nr to %hu\n", session->name, session->nr); 391fd558d18SJames Chapman } 392fd558d18SJames Chapman 393fd558d18SJames Chapman /* call private receive handler */ 394fd558d18SJames Chapman if (session->recv_skb != NULL) 395fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 396fd558d18SJames Chapman else 397fd558d18SJames Chapman kfree_skb(skb); 398fd558d18SJames Chapman 399fd558d18SJames Chapman if (session->deref) 400fd558d18SJames Chapman (*session->deref)(session); 401fd558d18SJames Chapman } 402fd558d18SJames Chapman 403fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 404fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 405fd558d18SJames Chapman */ 406fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 407fd558d18SJames Chapman { 408fd558d18SJames Chapman struct sk_buff *skb; 409fd558d18SJames Chapman struct sk_buff *tmp; 4105de7aee5SJames Chapman struct l2tp_stats *sstats; 411fd558d18SJames Chapman 412fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 413fd558d18SJames Chapman * expect to send up next, dequeue it and any other 414fd558d18SJames Chapman * in-sequence packets behind it. 415fd558d18SJames Chapman */ 416e2e210c0SEric Dumazet start: 417fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 4185de7aee5SJames Chapman sstats = &session->stats; 419fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 420fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 4215de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 4225de7aee5SJames Chapman sstats->rx_seq_discards++; 4235de7aee5SJames Chapman sstats->rx_errors++; 4245de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 425fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 426f7faffa3SJames Chapman "%s: oos pkt %u len %d discarded (too old), " 427f7faffa3SJames Chapman "waiting for %u, reorder_q_len=%d\n", 428fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 429fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 430fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 43138d40b3fSJames Chapman session->reorder_skip = 1; 432fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 433fd558d18SJames Chapman kfree_skb(skb); 434fd558d18SJames Chapman if (session->deref) 435fd558d18SJames Chapman (*session->deref)(session); 436fd558d18SJames Chapman continue; 437fd558d18SJames Chapman } 438fd558d18SJames Chapman 439fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 44038d40b3fSJames Chapman if (session->reorder_skip) { 44138d40b3fSJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 44238d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 44338d40b3fSJames Chapman session->name, session->nr, 44438d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 44538d40b3fSJames Chapman session->reorder_skip = 0; 44638d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 44738d40b3fSJames Chapman } 448fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 449fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 450f7faffa3SJames Chapman "%s: holding oos pkt %u len %d, " 451f7faffa3SJames Chapman "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)) { 598f7faffa3SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 599f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 600f7faffa3SJames Chapman tunnel->name, tunnel->tunnel_id, session->session_id); 6015de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6025de7aee5SJames Chapman sstats->rx_cookie_discards++; 6035de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 604f7faffa3SJames Chapman goto discard; 605f7faffa3SJames Chapman } 606f7faffa3SJames Chapman ptr += session->peer_cookie_len; 607f7faffa3SJames Chapman } 608f7faffa3SJames Chapman 609f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 610f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 611f7faffa3SJames Chapman * 612f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 613f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 614f7faffa3SJames Chapman * we were expecting them, discard frame. 615f7faffa3SJames Chapman */ 616f7faffa3SJames Chapman ns = nr = 0; 617f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 618f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 619f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 620f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 621f7faffa3SJames Chapman ptr += 2; 622f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 623f7faffa3SJames Chapman ptr += 2; 624f7faffa3SJames Chapman 625f7faffa3SJames Chapman /* Store L2TP info in the skb */ 626f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 627f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 628f7faffa3SJames Chapman 629f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 630f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 631f7faffa3SJames Chapman session->name, ns, nr, session->nr); 632f7faffa3SJames Chapman } 633f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 634f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 635f7faffa3SJames Chapman 636f7faffa3SJames Chapman if (l2h & 0x40000000) { 637f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 638f7faffa3SJames Chapman 639f7faffa3SJames Chapman /* Store L2TP info in the skb */ 640f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 641f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 642f7faffa3SJames Chapman 643f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 644f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 645f7faffa3SJames Chapman session->name, ns, session->nr); 646f7faffa3SJames Chapman } 647f7faffa3SJames Chapman } 648f7faffa3SJames Chapman 649f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 650f7faffa3SJames Chapman ptr += session->l2specific_len; 651f7faffa3SJames Chapman 652f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 653f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 654f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 655f7faffa3SJames Chapman * configure it so. 656f7faffa3SJames Chapman */ 657f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 658f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO, 659f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 660f7faffa3SJames Chapman session->name); 661f7faffa3SJames Chapman session->send_seq = -1; 662f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 663f7faffa3SJames Chapman } 664f7faffa3SJames Chapman } else { 665f7faffa3SJames Chapman /* No sequence numbers. 666f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 667f7faffa3SJames Chapman */ 668f7faffa3SJames Chapman if (session->recv_seq) { 669f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING, 670f7faffa3SJames Chapman "%s: recv data has no seq numbers when required. " 671f7faffa3SJames Chapman "Discarding\n", session->name); 6725de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6735de7aee5SJames Chapman sstats->rx_seq_discards++; 6745de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 675f7faffa3SJames Chapman goto discard; 676f7faffa3SJames Chapman } 677f7faffa3SJames Chapman 678f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 679f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 680f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 681f7faffa3SJames Chapman * LAC is broken. Discard the frame. 682f7faffa3SJames Chapman */ 683f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 684f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO, 685f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 686f7faffa3SJames Chapman session->name); 687f7faffa3SJames Chapman session->send_seq = 0; 688f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 689f7faffa3SJames Chapman } else if (session->send_seq) { 690f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING, 691f7faffa3SJames Chapman "%s: recv data has no seq numbers when required. " 692f7faffa3SJames Chapman "Discarding\n", session->name); 6935de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 6945de7aee5SJames Chapman sstats->rx_seq_discards++; 6955de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 696f7faffa3SJames Chapman goto discard; 697f7faffa3SJames Chapman } 698f7faffa3SJames Chapman } 699f7faffa3SJames Chapman 700f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 701f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 702f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 703f7faffa3SJames Chapman * in the session setup control protocol. 704f7faffa3SJames Chapman */ 705f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 706f7faffa3SJames Chapman /* If offset bit set, skip it. */ 707f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 708f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 709f7faffa3SJames Chapman ptr += 2 + offset; 710f7faffa3SJames Chapman } 711f7faffa3SJames Chapman } else 712f7faffa3SJames Chapman ptr += session->offset; 713f7faffa3SJames Chapman 714f7faffa3SJames Chapman offset = ptr - optr; 715f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 716f7faffa3SJames Chapman goto discard; 717f7faffa3SJames Chapman 718f7faffa3SJames Chapman __skb_pull(skb, offset); 719f7faffa3SJames Chapman 720f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 721f7faffa3SJames Chapman * packet, do so now. 722f7faffa3SJames Chapman */ 723f7faffa3SJames Chapman if (payload_hook) 724f7faffa3SJames Chapman if ((*payload_hook)(skb)) 725f7faffa3SJames Chapman goto discard; 726f7faffa3SJames Chapman 727f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 728f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 729f7faffa3SJames Chapman * reordering. 730f7faffa3SJames Chapman */ 731f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 732f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 733f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 734f7faffa3SJames Chapman 735f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 736f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 737f7faffa3SJames Chapman */ 738f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 739f7faffa3SJames Chapman if (session->reorder_timeout != 0) { 740f7faffa3SJames Chapman /* Packet reordering enabled. Add skb to session's 741f7faffa3SJames Chapman * reorder queue, in order of ns. 742f7faffa3SJames Chapman */ 743f7faffa3SJames Chapman l2tp_recv_queue_skb(session, skb); 744f7faffa3SJames Chapman } else { 745f7faffa3SJames Chapman /* Packet reordering disabled. Discard out-of-sequence 746f7faffa3SJames Chapman * packets 747f7faffa3SJames Chapman */ 748f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 7495de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 7505de7aee5SJames Chapman sstats->rx_seq_discards++; 7515de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 752f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 753f7faffa3SJames Chapman "%s: oos pkt %u len %d discarded, " 754f7faffa3SJames Chapman "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 int offset; 804fd558d18SJames Chapman u16 version; 805f7faffa3SJames Chapman int length; 8065de7aee5SJames Chapman struct l2tp_stats *tstats; 807fd558d18SJames Chapman 808fd558d18SJames Chapman if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb)) 809fd558d18SJames Chapman goto discard_bad_csum; 810fd558d18SJames Chapman 811fd558d18SJames Chapman /* UDP always verifies the packet length. */ 812fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 813fd558d18SJames Chapman 814fd558d18SJames Chapman /* Short packet? */ 815fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 816fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 817fd558d18SJames Chapman "%s: recv short packet (len=%d)\n", 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 827fd558d18SJames Chapman printk(KERN_DEBUG "%s: recv: ", tunnel->name); 828fd558d18SJames Chapman 829fd558d18SJames Chapman offset = 0; 830fd558d18SJames Chapman do { 831e50e705cSEric Dumazet printk(" %02X", skb->data[offset]); 832fd558d18SJames Chapman } while (++offset < length); 833fd558d18SJames Chapman 834fd558d18SJames Chapman printk("\n"); 835fd558d18SJames Chapman } 836fd558d18SJames Chapman 837e50e705cSEric Dumazet /* Point to L2TP header */ 838e50e705cSEric Dumazet optr = ptr = skb->data; 839e50e705cSEric Dumazet 840fd558d18SJames Chapman /* Get L2TP header flags */ 841fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 842fd558d18SJames Chapman 843fd558d18SJames Chapman /* Check protocol version */ 844fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 845fd558d18SJames Chapman if (version != tunnel->version) { 846fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 847fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 848fd558d18SJames Chapman tunnel->name, version, tunnel->version); 849fd558d18SJames Chapman goto error; 850fd558d18SJames Chapman } 851fd558d18SJames Chapman 852fd558d18SJames Chapman /* Get length of L2TP packet */ 853fd558d18SJames Chapman length = skb->len; 854fd558d18SJames Chapman 855fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 856fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 857fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG, 858fd558d18SJames Chapman "%s: recv control packet, len=%d\n", tunnel->name, length); 859fd558d18SJames Chapman goto error; 860fd558d18SJames Chapman } 861fd558d18SJames Chapman 862fd558d18SJames Chapman /* Skip flags */ 863fd558d18SJames Chapman ptr += 2; 864fd558d18SJames Chapman 865f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 866fd558d18SJames Chapman /* If length is present, skip it */ 867fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 868fd558d18SJames Chapman ptr += 2; 869fd558d18SJames Chapman 870fd558d18SJames Chapman /* Extract tunnel and session ID */ 871fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 872fd558d18SJames Chapman ptr += 2; 873fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 874fd558d18SJames Chapman ptr += 2; 875f7faffa3SJames Chapman } else { 876f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 877f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 878f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 879f7faffa3SJames Chapman ptr += 4; 880f7faffa3SJames Chapman } 881fd558d18SJames Chapman 882fd558d18SJames Chapman /* Find the session context */ 883f7faffa3SJames Chapman session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); 884309795f4SJames Chapman if (!session || !session->recv_skb) { 885fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 886fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 887f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 888fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 889fd558d18SJames Chapman goto error; 890fd558d18SJames Chapman } 891fd558d18SJames Chapman 892f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 893fd558d18SJames Chapman 894fd558d18SJames Chapman return 0; 895fd558d18SJames Chapman 896fd558d18SJames Chapman discard_bad_csum: 897fd558d18SJames Chapman LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name); 898fd558d18SJames Chapman UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0); 8995de7aee5SJames Chapman tstats = &tunnel->stats; 9005de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 9015de7aee5SJames Chapman tstats->rx_errors++; 9025de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 903fd558d18SJames Chapman kfree_skb(skb); 904fd558d18SJames Chapman 905fd558d18SJames Chapman return 0; 906fd558d18SJames Chapman 907fd558d18SJames Chapman error: 908fd558d18SJames Chapman /* Put UDP header back */ 909fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 910fd558d18SJames Chapman 911fd558d18SJames Chapman return 1; 912fd558d18SJames Chapman } 913fd558d18SJames Chapman 914fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 915fd558d18SJames Chapman * Return codes: 916fd558d18SJames Chapman * 0 : success. 917fd558d18SJames Chapman * <0: error 918fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 919fd558d18SJames Chapman */ 920fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 921fd558d18SJames Chapman { 922fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 923fd558d18SJames Chapman 924fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 925fd558d18SJames Chapman if (tunnel == NULL) 926fd558d18SJames Chapman goto pass_up; 927fd558d18SJames Chapman 928fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG, 929fd558d18SJames Chapman "%s: received %d bytes\n", tunnel->name, skb->len); 930fd558d18SJames Chapman 931fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 932fd558d18SJames Chapman goto pass_up_put; 933fd558d18SJames Chapman 934fd558d18SJames Chapman sock_put(sk); 935fd558d18SJames Chapman return 0; 936fd558d18SJames Chapman 937fd558d18SJames Chapman pass_up_put: 938fd558d18SJames Chapman sock_put(sk); 939fd558d18SJames Chapman pass_up: 940fd558d18SJames Chapman return 1; 941fd558d18SJames Chapman } 942fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 943fd558d18SJames Chapman 944fd558d18SJames Chapman /************************************************************************ 945fd558d18SJames Chapman * Transmit handling 946fd558d18SJames Chapman ***********************************************************************/ 947fd558d18SJames Chapman 948fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 949fd558d18SJames Chapman */ 950f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 951fd558d18SJames Chapman { 952f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 953fd558d18SJames Chapman __be16 *bufp = buf; 954f7faffa3SJames Chapman __be16 *optr = buf; 955fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 956fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 957fd558d18SJames Chapman u32 session_id = session->peer_session_id; 958fd558d18SJames Chapman 959fd558d18SJames Chapman if (session->send_seq) 960fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 961fd558d18SJames Chapman 962fd558d18SJames Chapman /* Setup L2TP header. */ 963fd558d18SJames Chapman *bufp++ = htons(flags); 964fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 965fd558d18SJames Chapman *bufp++ = htons(session_id); 966fd558d18SJames Chapman if (session->send_seq) { 967fd558d18SJames Chapman *bufp++ = htons(session->ns); 968fd558d18SJames Chapman *bufp++ = 0; 969fd558d18SJames Chapman session->ns++; 970f7faffa3SJames Chapman session->ns &= 0xffff; 971fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 972f7faffa3SJames Chapman "%s: updated ns to %u\n", session->name, session->ns); 973fd558d18SJames Chapman } 974fd558d18SJames Chapman 975f7faffa3SJames Chapman return bufp - optr; 976f7faffa3SJames Chapman } 977f7faffa3SJames Chapman 978f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 979fd558d18SJames Chapman { 9800d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 981f7faffa3SJames Chapman char *bufp = buf; 982f7faffa3SJames Chapman char *optr = bufp; 983fd558d18SJames Chapman 9840d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 9850d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 9860d76751fSJames Chapman */ 9870d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 9880d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 989f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 990f7faffa3SJames Chapman bufp += 2; 991f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 992f7faffa3SJames Chapman bufp += 2; 9930d76751fSJames Chapman } 9940d76751fSJames Chapman 995f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 996f7faffa3SJames Chapman bufp += 4; 997f7faffa3SJames Chapman if (session->cookie_len) { 998f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 999f7faffa3SJames Chapman bufp += session->cookie_len; 1000fd558d18SJames Chapman } 1001f7faffa3SJames Chapman if (session->l2specific_len) { 1002f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1003f7faffa3SJames Chapman u32 l2h = 0; 1004f7faffa3SJames Chapman if (session->send_seq) { 1005f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1006f7faffa3SJames Chapman session->ns++; 1007f7faffa3SJames Chapman session->ns &= 0xffffff; 1008f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 1009f7faffa3SJames Chapman "%s: updated ns to %u\n", session->name, session->ns); 1010f7faffa3SJames Chapman } 1011f7faffa3SJames Chapman 1012f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 1013f7faffa3SJames Chapman } 1014f7faffa3SJames Chapman bufp += session->l2specific_len; 1015f7faffa3SJames Chapman } 1016f7faffa3SJames Chapman if (session->offset) 1017f7faffa3SJames Chapman bufp += session->offset; 1018f7faffa3SJames Chapman 1019f7faffa3SJames Chapman return bufp - optr; 1020f7faffa3SJames Chapman } 1021fd558d18SJames Chapman 1022fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1023d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1024fd558d18SJames Chapman { 1025fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1026fd558d18SJames Chapman unsigned int len = skb->len; 1027fd558d18SJames Chapman int error; 10285de7aee5SJames Chapman struct l2tp_stats *tstats, *sstats; 1029fd558d18SJames Chapman 1030fd558d18SJames Chapman /* Debug */ 1031fd558d18SJames Chapman if (session->send_seq) 1032fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG, 1033f7faffa3SJames Chapman "%s: send %Zd bytes, ns=%u\n", session->name, 1034fd558d18SJames Chapman data_len, session->ns - 1); 1035fd558d18SJames Chapman else 1036fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG, 1037fd558d18SJames Chapman "%s: send %Zd bytes\n", session->name, data_len); 1038fd558d18SJames Chapman 1039fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 1040fd558d18SJames Chapman int i; 10410d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10420d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1043fd558d18SJames Chapman 1044fd558d18SJames Chapman printk(KERN_DEBUG "%s: xmit:", session->name); 10450d76751fSJames Chapman for (i = 0; i < (len - uhlen); i++) { 1046fd558d18SJames Chapman printk(" %02X", *datap++); 1047fd558d18SJames Chapman if (i == 31) { 1048fd558d18SJames Chapman printk(" ..."); 1049fd558d18SJames Chapman break; 1050fd558d18SJames Chapman } 1051fd558d18SJames Chapman } 1052fd558d18SJames Chapman printk("\n"); 1053fd558d18SJames Chapman } 1054fd558d18SJames Chapman 1055fd558d18SJames Chapman /* Queue the packet to IP for output */ 10564e15ed4dSShan Wei skb->local_df = 1; 1057d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1058d2cf3361SBenjamin LaHaise if (skb->sk->sk_family == PF_INET6) 1059d2cf3361SBenjamin LaHaise error = inet6_csk_xmit(skb, NULL); 1060d2cf3361SBenjamin LaHaise else 1061d2cf3361SBenjamin LaHaise #endif 1062d9d8da80SDavid S. Miller error = ip_queue_xmit(skb, fl); 1063fd558d18SJames Chapman 1064fd558d18SJames Chapman /* Update stats */ 10655de7aee5SJames Chapman tstats = &tunnel->stats; 10665de7aee5SJames Chapman u64_stats_update_begin(&tstats->syncp); 10675de7aee5SJames Chapman sstats = &session->stats; 10685de7aee5SJames Chapman u64_stats_update_begin(&sstats->syncp); 1069fd558d18SJames Chapman if (error >= 0) { 10705de7aee5SJames Chapman tstats->tx_packets++; 10715de7aee5SJames Chapman tstats->tx_bytes += len; 10725de7aee5SJames Chapman sstats->tx_packets++; 10735de7aee5SJames Chapman sstats->tx_bytes += len; 1074fd558d18SJames Chapman } else { 10755de7aee5SJames Chapman tstats->tx_errors++; 10765de7aee5SJames Chapman sstats->tx_errors++; 1077fd558d18SJames Chapman } 10785de7aee5SJames Chapman u64_stats_update_end(&tstats->syncp); 10795de7aee5SJames Chapman u64_stats_update_end(&sstats->syncp); 1080fd558d18SJames Chapman 1081fd558d18SJames Chapman return 0; 1082fd558d18SJames Chapman } 1083fd558d18SJames Chapman 1084fd558d18SJames Chapman /* Automatically called when the skb is freed. 1085fd558d18SJames Chapman */ 1086fd558d18SJames Chapman static void l2tp_sock_wfree(struct sk_buff *skb) 1087fd558d18SJames Chapman { 1088fd558d18SJames Chapman sock_put(skb->sk); 1089fd558d18SJames Chapman } 1090fd558d18SJames Chapman 1091fd558d18SJames Chapman /* For data skbs that we transmit, we associate with the tunnel socket 1092fd558d18SJames Chapman * but don't do accounting. 1093fd558d18SJames Chapman */ 1094fd558d18SJames Chapman static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk) 1095fd558d18SJames Chapman { 1096fd558d18SJames Chapman sock_hold(sk); 1097fd558d18SJames Chapman skb->sk = sk; 1098fd558d18SJames Chapman skb->destructor = l2tp_sock_wfree; 1099fd558d18SJames Chapman } 1100fd558d18SJames Chapman 1101d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1102d2cf3361SBenjamin LaHaise static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb, 1103d2cf3361SBenjamin LaHaise int udp_len) 1104d2cf3361SBenjamin LaHaise { 1105d2cf3361SBenjamin LaHaise struct ipv6_pinfo *np = inet6_sk(sk); 1106d2cf3361SBenjamin LaHaise struct udphdr *uh = udp_hdr(skb); 1107d2cf3361SBenjamin LaHaise 1108d2cf3361SBenjamin LaHaise if (!skb_dst(skb) || !skb_dst(skb)->dev || 1109d2cf3361SBenjamin LaHaise !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) { 1110d2cf3361SBenjamin LaHaise __wsum csum = skb_checksum(skb, 0, udp_len, 0); 1111d2cf3361SBenjamin LaHaise skb->ip_summed = CHECKSUM_UNNECESSARY; 1112d2cf3361SBenjamin LaHaise uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len, 1113d2cf3361SBenjamin LaHaise IPPROTO_UDP, csum); 1114d2cf3361SBenjamin LaHaise if (uh->check == 0) 1115d2cf3361SBenjamin LaHaise uh->check = CSUM_MANGLED_0; 1116d2cf3361SBenjamin LaHaise } else { 1117d2cf3361SBenjamin LaHaise skb->ip_summed = CHECKSUM_PARTIAL; 1118d2cf3361SBenjamin LaHaise skb->csum_start = skb_transport_header(skb) - skb->head; 1119d2cf3361SBenjamin LaHaise skb->csum_offset = offsetof(struct udphdr, check); 1120d2cf3361SBenjamin LaHaise uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr, 1121d2cf3361SBenjamin LaHaise udp_len, IPPROTO_UDP, 0); 1122d2cf3361SBenjamin LaHaise } 1123d2cf3361SBenjamin LaHaise } 1124d2cf3361SBenjamin LaHaise #endif 1125d2cf3361SBenjamin LaHaise 1126fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1127fd558d18SJames Chapman * inserted in the skb data before calling this function. 1128fd558d18SJames Chapman */ 1129fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1130fd558d18SJames Chapman { 1131fd558d18SJames Chapman int data_len = skb->len; 11320d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 11330d76751fSJames Chapman struct sock *sk = tunnel->sock; 1134d9d8da80SDavid S. Miller struct flowi *fl; 1135fd558d18SJames Chapman struct udphdr *uh; 1136fd558d18SJames Chapman struct inet_sock *inet; 1137fd558d18SJames Chapman __wsum csum; 1138fd558d18SJames Chapman int old_headroom; 1139fd558d18SJames Chapman int new_headroom; 1140fd558d18SJames Chapman int headroom; 11410d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11420d76751fSJames Chapman int udp_len; 1143fd558d18SJames Chapman 1144fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1145fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1146fd558d18SJames Chapman * make room. Adjust truesize. 1147fd558d18SJames Chapman */ 1148fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11490d76751fSJames Chapman uhlen + hdr_len; 1150fd558d18SJames Chapman old_headroom = skb_headroom(skb); 1151835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1152835acf5dSEric Dumazet dev_kfree_skb(skb); 1153fd558d18SJames Chapman goto abort; 1154835acf5dSEric Dumazet } 1155fd558d18SJames Chapman 1156fd558d18SJames Chapman new_headroom = skb_headroom(skb); 1157fd558d18SJames Chapman skb_orphan(skb); 1158fd558d18SJames Chapman skb->truesize += new_headroom - old_headroom; 1159fd558d18SJames Chapman 1160fd558d18SJames Chapman /* Setup L2TP header */ 1161f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1162fd558d18SJames Chapman 11630d76751fSJames Chapman /* Reset skb netfilter state */ 1164fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1165fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1166fd558d18SJames Chapman IPSKB_REROUTED); 1167fd558d18SJames Chapman nf_reset(skb); 1168fd558d18SJames Chapman 11696af88da1SDavid S. Miller bh_lock_sock(sk); 11706af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 11716af88da1SDavid S. Miller dev_kfree_skb(skb); 11726af88da1SDavid S. Miller goto out_unlock; 11736af88da1SDavid S. Miller } 11746af88da1SDavid S. Miller 1175fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1176fd558d18SJames Chapman skb_dst_drop(skb); 117771b1391aSFlorian Westphal skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 11780d76751fSJames Chapman 1179d9d8da80SDavid S. Miller inet = inet_sk(sk); 1180d9d8da80SDavid S. Miller fl = &inet->cork.fl; 11810d76751fSJames Chapman switch (tunnel->encap) { 11820d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 11830d76751fSJames Chapman /* Setup UDP header */ 11840d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 11850d76751fSJames Chapman skb_reset_transport_header(skb); 11860d76751fSJames Chapman uh = udp_hdr(skb); 11870d76751fSJames Chapman uh->source = inet->inet_sport; 11880d76751fSJames Chapman uh->dest = inet->inet_dport; 11890d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 11900d76751fSJames Chapman uh->len = htons(udp_len); 11910d76751fSJames Chapman uh->check = 0; 1192fd558d18SJames Chapman 1193fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1194d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1195d2cf3361SBenjamin LaHaise if (sk->sk_family == PF_INET6) 1196d2cf3361SBenjamin LaHaise l2tp_xmit_ipv6_csum(sk, skb, udp_len); 1197d2cf3361SBenjamin LaHaise else 1198d2cf3361SBenjamin LaHaise #endif 1199fd558d18SJames Chapman if (sk->sk_no_check == UDP_CSUM_NOXMIT) 1200fd558d18SJames Chapman skb->ip_summed = CHECKSUM_NONE; 1201fd558d18SJames Chapman else if ((skb_dst(skb) && skb_dst(skb)->dev) && 1202fd558d18SJames Chapman (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) { 1203fd558d18SJames Chapman skb->ip_summed = CHECKSUM_COMPLETE; 1204fd558d18SJames Chapman csum = skb_checksum(skb, 0, udp_len, 0); 1205fd558d18SJames Chapman uh->check = csum_tcpudp_magic(inet->inet_saddr, 1206fd558d18SJames Chapman inet->inet_daddr, 1207fd558d18SJames Chapman udp_len, IPPROTO_UDP, csum); 1208fd558d18SJames Chapman if (uh->check == 0) 1209fd558d18SJames Chapman uh->check = CSUM_MANGLED_0; 1210fd558d18SJames Chapman } else { 1211fd558d18SJames Chapman skb->ip_summed = CHECKSUM_PARTIAL; 1212fd558d18SJames Chapman skb->csum_start = skb_transport_header(skb) - skb->head; 1213fd558d18SJames Chapman skb->csum_offset = offsetof(struct udphdr, check); 1214fd558d18SJames Chapman uh->check = ~csum_tcpudp_magic(inet->inet_saddr, 1215fd558d18SJames Chapman inet->inet_daddr, 1216fd558d18SJames Chapman udp_len, IPPROTO_UDP, 0); 1217fd558d18SJames Chapman } 12180d76751fSJames Chapman break; 12190d76751fSJames Chapman 12200d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12210d76751fSJames Chapman break; 12220d76751fSJames Chapman } 12230d76751fSJames Chapman 12240d76751fSJames Chapman l2tp_skb_set_owner_w(skb, sk); 1225fd558d18SJames Chapman 1226d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 12276af88da1SDavid S. Miller out_unlock: 12286af88da1SDavid S. Miller bh_unlock_sock(sk); 1229fd558d18SJames Chapman 1230fd558d18SJames Chapman abort: 1231fd558d18SJames Chapman return 0; 1232fd558d18SJames Chapman } 1233fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1234fd558d18SJames Chapman 1235fd558d18SJames Chapman /***************************************************************************** 1236fd558d18SJames Chapman * Tinnel and session create/destroy. 1237fd558d18SJames Chapman *****************************************************************************/ 1238fd558d18SJames Chapman 1239fd558d18SJames Chapman /* Tunnel socket destruct hook. 1240fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1241fd558d18SJames Chapman * closed. 1242fd558d18SJames Chapman */ 1243fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1244fd558d18SJames Chapman { 1245fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1246fd558d18SJames Chapman 1247fd558d18SJames Chapman tunnel = sk->sk_user_data; 1248fd558d18SJames Chapman if (tunnel == NULL) 1249fd558d18SJames Chapman goto end; 1250fd558d18SJames Chapman 1251fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1252fd558d18SJames Chapman "%s: closing...\n", tunnel->name); 1253fd558d18SJames Chapman 1254fd558d18SJames Chapman /* Close all sessions */ 1255fd558d18SJames Chapman l2tp_tunnel_closeall(tunnel); 1256fd558d18SJames Chapman 12570d76751fSJames Chapman switch (tunnel->encap) { 12580d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1259fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1260fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1261fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 12620d76751fSJames Chapman break; 12630d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12640d76751fSJames Chapman break; 12650d76751fSJames Chapman } 1266fd558d18SJames Chapman 1267fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1268fd558d18SJames Chapman tunnel->sock = NULL; 1269fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1270fd558d18SJames Chapman sk->sk_user_data = NULL; 1271fd558d18SJames Chapman 1272fd558d18SJames Chapman /* Call the original destructor */ 1273fd558d18SJames Chapman if (sk->sk_destruct) 1274fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1275fd558d18SJames Chapman 1276fd558d18SJames Chapman /* We're finished with the socket */ 1277fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1278fd558d18SJames Chapman 1279fd558d18SJames Chapman end: 1280fd558d18SJames Chapman return; 1281fd558d18SJames Chapman } 1282fd558d18SJames Chapman 1283fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1284fd558d18SJames Chapman */ 1285fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1286fd558d18SJames Chapman { 1287fd558d18SJames Chapman int hash; 1288fd558d18SJames Chapman struct hlist_node *walk; 1289fd558d18SJames Chapman struct hlist_node *tmp; 1290fd558d18SJames Chapman struct l2tp_session *session; 1291fd558d18SJames Chapman 1292fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1293fd558d18SJames Chapman 1294fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1295fd558d18SJames Chapman "%s: closing all sessions...\n", tunnel->name); 1296fd558d18SJames Chapman 1297fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1298fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1299fd558d18SJames Chapman again: 1300fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1301fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1302fd558d18SJames Chapman 1303fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO, 1304fd558d18SJames Chapman "%s: closing session\n", session->name); 1305fd558d18SJames Chapman 1306fd558d18SJames Chapman hlist_del_init(&session->hlist); 1307fd558d18SJames Chapman 1308fd558d18SJames Chapman /* Since we should hold the sock lock while 1309fd558d18SJames Chapman * doing any unbinding, we need to release the 1310fd558d18SJames Chapman * lock we're holding before taking that lock. 1311fd558d18SJames Chapman * Hold a reference to the sock so it doesn't 1312fd558d18SJames Chapman * disappear as we're jumping between locks. 1313fd558d18SJames Chapman */ 1314fd558d18SJames Chapman if (session->ref != NULL) 1315fd558d18SJames Chapman (*session->ref)(session); 1316fd558d18SJames Chapman 1317fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1318fd558d18SJames Chapman 1319f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1320f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1321f7faffa3SJames Chapman 1322e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1323e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1324e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1325e02d494dSJames Chapman synchronize_rcu(); 1326f7faffa3SJames Chapman } 1327f7faffa3SJames Chapman 1328fd558d18SJames Chapman if (session->session_close != NULL) 1329fd558d18SJames Chapman (*session->session_close)(session); 1330fd558d18SJames Chapman 1331fd558d18SJames Chapman if (session->deref != NULL) 1332fd558d18SJames Chapman (*session->deref)(session); 1333fd558d18SJames Chapman 1334fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1335fd558d18SJames Chapman 1336fd558d18SJames Chapman /* Now restart from the beginning of this hash 1337fd558d18SJames Chapman * chain. We always remove a session from the 1338fd558d18SJames Chapman * list so we are guaranteed to make forward 1339fd558d18SJames Chapman * progress. 1340fd558d18SJames Chapman */ 1341fd558d18SJames Chapman goto again; 1342fd558d18SJames Chapman } 1343fd558d18SJames Chapman } 1344fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1345fd558d18SJames Chapman } 1346fd558d18SJames Chapman 1347fd558d18SJames Chapman /* Really kill the tunnel. 1348fd558d18SJames Chapman * Come here only when all sessions have been cleared from the tunnel. 1349fd558d18SJames Chapman */ 1350fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 1351fd558d18SJames Chapman { 1352fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1353fd558d18SJames Chapman 1354fd558d18SJames Chapman BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1355fd558d18SJames Chapman BUG_ON(tunnel->sock != NULL); 1356fd558d18SJames Chapman 1357fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1358fd558d18SJames Chapman "%s: free...\n", tunnel->name); 1359fd558d18SJames Chapman 1360fd558d18SJames Chapman /* Remove from tunnel list */ 1361e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1362e02d494dSJames Chapman list_del_rcu(&tunnel->list); 1363e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1364e02d494dSJames Chapman synchronize_rcu(); 1365fd558d18SJames Chapman 1366fd558d18SJames Chapman atomic_dec(&l2tp_tunnel_count); 1367fd558d18SJames Chapman kfree(tunnel); 1368fd558d18SJames Chapman } 1369fd558d18SJames Chapman 1370789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1371789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1372789a4a2cSJames Chapman * managing L2TP daemon. 1373789a4a2cSJames Chapman */ 1374789a4a2cSJames Chapman static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp) 1375789a4a2cSJames Chapman { 1376789a4a2cSJames Chapman int err = -EINVAL; 1377789a4a2cSJames Chapman struct sockaddr_in udp_addr; 1378f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1379f9bac8dfSChris Elston struct sockaddr_in6 udp6_addr; 13805dac94e1SJames Chapman struct sockaddr_l2tpip6 ip6_addr; 1381f9bac8dfSChris Elston #endif 1382789a4a2cSJames Chapman struct sockaddr_l2tpip ip_addr; 13837bddd0dbSEric Dumazet struct socket *sock = NULL; 1384789a4a2cSJames Chapman 1385789a4a2cSJames Chapman switch (cfg->encap) { 1386789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 1387f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1388f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 1389f9bac8dfSChris Elston err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp); 1390f9bac8dfSChris Elston if (err < 0) 1391f9bac8dfSChris Elston goto out; 1392f9bac8dfSChris Elston 1393f9bac8dfSChris Elston sock = *sockp; 1394f9bac8dfSChris Elston 1395f9bac8dfSChris Elston memset(&udp6_addr, 0, sizeof(udp6_addr)); 1396f9bac8dfSChris Elston udp6_addr.sin6_family = AF_INET6; 1397f9bac8dfSChris Elston memcpy(&udp6_addr.sin6_addr, cfg->local_ip6, 1398f9bac8dfSChris Elston sizeof(udp6_addr.sin6_addr)); 1399f9bac8dfSChris Elston udp6_addr.sin6_port = htons(cfg->local_udp_port); 1400f9bac8dfSChris Elston err = kernel_bind(sock, (struct sockaddr *) &udp6_addr, 1401f9bac8dfSChris Elston sizeof(udp6_addr)); 1402f9bac8dfSChris Elston if (err < 0) 1403f9bac8dfSChris Elston goto out; 1404f9bac8dfSChris Elston 1405f9bac8dfSChris Elston udp6_addr.sin6_family = AF_INET6; 1406f9bac8dfSChris Elston memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6, 1407f9bac8dfSChris Elston sizeof(udp6_addr.sin6_addr)); 1408f9bac8dfSChris Elston udp6_addr.sin6_port = htons(cfg->peer_udp_port); 1409f9bac8dfSChris Elston err = kernel_connect(sock, 1410f9bac8dfSChris Elston (struct sockaddr *) &udp6_addr, 1411f9bac8dfSChris Elston sizeof(udp6_addr), 0); 1412f9bac8dfSChris Elston if (err < 0) 1413f9bac8dfSChris Elston goto out; 1414f9bac8dfSChris Elston } else 1415f9bac8dfSChris Elston #endif 1416f9bac8dfSChris Elston { 1417789a4a2cSJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp); 1418789a4a2cSJames Chapman if (err < 0) 1419789a4a2cSJames Chapman goto out; 1420789a4a2cSJames Chapman 1421789a4a2cSJames Chapman sock = *sockp; 1422789a4a2cSJames Chapman 1423789a4a2cSJames Chapman memset(&udp_addr, 0, sizeof(udp_addr)); 1424789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1425789a4a2cSJames Chapman udp_addr.sin_addr = cfg->local_ip; 1426789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->local_udp_port); 1427f9bac8dfSChris Elston err = kernel_bind(sock, (struct sockaddr *) &udp_addr, 1428f9bac8dfSChris Elston sizeof(udp_addr)); 1429789a4a2cSJames Chapman if (err < 0) 1430789a4a2cSJames Chapman goto out; 1431789a4a2cSJames Chapman 1432789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1433789a4a2cSJames Chapman udp_addr.sin_addr = cfg->peer_ip; 1434789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->peer_udp_port); 1435f9bac8dfSChris Elston err = kernel_connect(sock, 1436f9bac8dfSChris Elston (struct sockaddr *) &udp_addr, 1437f9bac8dfSChris Elston sizeof(udp_addr), 0); 1438789a4a2cSJames Chapman if (err < 0) 1439789a4a2cSJames Chapman goto out; 1440f9bac8dfSChris Elston } 1441789a4a2cSJames Chapman 1442789a4a2cSJames Chapman if (!cfg->use_udp_checksums) 1443789a4a2cSJames Chapman sock->sk->sk_no_check = UDP_CSUM_NOXMIT; 1444789a4a2cSJames Chapman 1445789a4a2cSJames Chapman break; 1446789a4a2cSJames Chapman 1447789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1448f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1449f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 14505dac94e1SJames Chapman err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP, 14515dac94e1SJames Chapman sockp); 14525dac94e1SJames Chapman if (err < 0) 1453f9bac8dfSChris Elston goto out; 14545dac94e1SJames Chapman 14555dac94e1SJames Chapman sock = *sockp; 14565dac94e1SJames Chapman 14575dac94e1SJames Chapman memset(&ip6_addr, 0, sizeof(ip6_addr)); 14585dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14595dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 14605dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14615dac94e1SJames Chapman ip6_addr.l2tp_conn_id = tunnel_id; 14625dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, 14635dac94e1SJames Chapman sizeof(ip6_addr)); 14645dac94e1SJames Chapman if (err < 0) 14655dac94e1SJames Chapman goto out; 14665dac94e1SJames Chapman 14675dac94e1SJames Chapman ip6_addr.l2tp_family = AF_INET6; 14685dac94e1SJames Chapman memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 14695dac94e1SJames Chapman sizeof(ip6_addr.l2tp_addr)); 14705dac94e1SJames Chapman ip6_addr.l2tp_conn_id = peer_tunnel_id; 14715dac94e1SJames Chapman err = kernel_connect(sock, 14725dac94e1SJames Chapman (struct sockaddr *) &ip6_addr, 14735dac94e1SJames Chapman sizeof(ip6_addr), 0); 14745dac94e1SJames Chapman if (err < 0) 14755dac94e1SJames Chapman goto out; 14765dac94e1SJames Chapman } else 1477f9bac8dfSChris Elston #endif 14785dac94e1SJames Chapman { 14795dac94e1SJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, 14805dac94e1SJames Chapman sockp); 1481789a4a2cSJames Chapman if (err < 0) 1482789a4a2cSJames Chapman goto out; 1483789a4a2cSJames Chapman 1484789a4a2cSJames Chapman sock = *sockp; 1485789a4a2cSJames Chapman 1486789a4a2cSJames Chapman memset(&ip_addr, 0, sizeof(ip_addr)); 1487789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1488789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1489789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14905dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 14915dac94e1SJames Chapman sizeof(ip_addr)); 1492789a4a2cSJames Chapman if (err < 0) 1493789a4a2cSJames Chapman goto out; 1494789a4a2cSJames Chapman 1495789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1496789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1497789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14985dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 14995dac94e1SJames Chapman sizeof(ip_addr), 0); 1500789a4a2cSJames Chapman if (err < 0) 1501789a4a2cSJames Chapman goto out; 15025dac94e1SJames Chapman } 1503789a4a2cSJames Chapman break; 1504789a4a2cSJames Chapman 1505789a4a2cSJames Chapman default: 1506789a4a2cSJames Chapman goto out; 1507789a4a2cSJames Chapman } 1508789a4a2cSJames Chapman 1509789a4a2cSJames Chapman out: 1510789a4a2cSJames Chapman if ((err < 0) && sock) { 1511789a4a2cSJames Chapman sock_release(sock); 1512789a4a2cSJames Chapman *sockp = NULL; 1513789a4a2cSJames Chapman } 1514789a4a2cSJames Chapman 1515789a4a2cSJames Chapman return err; 1516789a4a2cSJames Chapman } 1517789a4a2cSJames Chapman 1518fd558d18SJames 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) 1519fd558d18SJames Chapman { 1520fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1521fd558d18SJames Chapman int err; 1522fd558d18SJames Chapman struct socket *sock = NULL; 1523fd558d18SJames Chapman struct sock *sk = NULL; 1524fd558d18SJames Chapman struct l2tp_net *pn; 15250d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1526fd558d18SJames Chapman 1527fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1528789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1529789a4a2cSJames Chapman * kernel socket. 1530fd558d18SJames Chapman */ 1531789a4a2cSJames Chapman if (fd < 0) { 1532789a4a2cSJames Chapman err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock); 1533789a4a2cSJames Chapman if (err < 0) 1534789a4a2cSJames Chapman goto err; 1535789a4a2cSJames Chapman } else { 1536fd558d18SJames Chapman err = -EBADF; 1537fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1538fd558d18SJames Chapman if (!sock) { 1539fd558d18SJames Chapman printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n", 1540fd558d18SJames Chapman tunnel_id, fd, err); 1541fd558d18SJames Chapman goto err; 1542fd558d18SJames Chapman } 1543789a4a2cSJames Chapman } 1544fd558d18SJames Chapman 1545fd558d18SJames Chapman sk = sock->sk; 1546fd558d18SJames Chapman 15470d76751fSJames Chapman if (cfg != NULL) 15480d76751fSJames Chapman encap = cfg->encap; 15490d76751fSJames Chapman 1550fd558d18SJames Chapman /* Quick sanity checks */ 15510d76751fSJames Chapman switch (encap) { 15520d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1553fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1554fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1555fd558d18SJames Chapman printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1556fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1557fd558d18SJames Chapman goto err; 1558fd558d18SJames Chapman } 15590d76751fSJames Chapman break; 15600d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 15610d76751fSJames Chapman err = -EPROTONOSUPPORT; 15620d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 15630d76751fSJames Chapman printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 15640d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1565fd558d18SJames Chapman goto err; 1566fd558d18SJames Chapman } 15670d76751fSJames Chapman break; 15680d76751fSJames Chapman } 1569fd558d18SJames Chapman 1570fd558d18SJames Chapman /* Check if this socket has already been prepped */ 1571fd558d18SJames Chapman tunnel = (struct l2tp_tunnel *)sk->sk_user_data; 1572fd558d18SJames Chapman if (tunnel != NULL) { 1573fd558d18SJames Chapman /* This socket has already been prepped */ 1574fd558d18SJames Chapman err = -EBUSY; 1575fd558d18SJames Chapman goto err; 1576fd558d18SJames Chapman } 1577fd558d18SJames Chapman 1578fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1579fd558d18SJames Chapman if (tunnel == NULL) { 1580fd558d18SJames Chapman err = -ENOMEM; 1581fd558d18SJames Chapman goto err; 1582fd558d18SJames Chapman } 1583fd558d18SJames Chapman 1584fd558d18SJames Chapman tunnel->version = version; 1585fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1586fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1587fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1588fd558d18SJames Chapman 1589fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1590fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1591fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1592fd558d18SJames Chapman 1593fd558d18SJames Chapman /* The net we belong to */ 1594fd558d18SJames Chapman tunnel->l2tp_net = net; 1595fd558d18SJames Chapman pn = l2tp_pernet(net); 1596fd558d18SJames Chapman 15970d76751fSJames Chapman if (cfg != NULL) 1598fd558d18SJames Chapman tunnel->debug = cfg->debug; 1599fd558d18SJames Chapman 1600fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 16010d76751fSJames Chapman tunnel->encap = encap; 16020d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 16030d76751fSJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1604fd558d18SJames Chapman udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP; 1605fd558d18SJames Chapman udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv; 1606d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1607d2cf3361SBenjamin LaHaise if (sk->sk_family == PF_INET6) 1608d2cf3361SBenjamin LaHaise udpv6_encap_enable(); 1609d2cf3361SBenjamin LaHaise else 1610d2cf3361SBenjamin LaHaise #endif 1611447167bfSEric Dumazet udp_encap_enable(); 16120d76751fSJames Chapman } 1613fd558d18SJames Chapman 1614fd558d18SJames Chapman sk->sk_user_data = tunnel; 1615fd558d18SJames Chapman 1616fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1617fd558d18SJames Chapman * if the tunnel socket goes away. 1618fd558d18SJames Chapman */ 1619fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1620fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1621fd558d18SJames Chapman tunnel->sock = sk; 1622fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1623fd558d18SJames Chapman 1624fd558d18SJames Chapman /* Add tunnel to our list */ 1625fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1626fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1627fd558d18SJames Chapman 1628fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 16291769192aSEric Dumazet * only when this drops to zero. Must be done before list insertion 1630fd558d18SJames Chapman */ 1631fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 16321769192aSEric Dumazet spin_lock_bh(&pn->l2tp_tunnel_list_lock); 16331769192aSEric Dumazet list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 16341769192aSEric Dumazet spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1635fd558d18SJames Chapman 1636fd558d18SJames Chapman err = 0; 1637fd558d18SJames Chapman err: 1638fd558d18SJames Chapman if (tunnelp) 1639fd558d18SJames Chapman *tunnelp = tunnel; 1640fd558d18SJames Chapman 1641789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1642789a4a2cSJames Chapman * have a file. 1643789a4a2cSJames Chapman */ 1644789a4a2cSJames Chapman if (sock && sock->file) 1645fd558d18SJames Chapman sockfd_put(sock); 1646fd558d18SJames Chapman 1647fd558d18SJames Chapman return err; 1648fd558d18SJames Chapman } 1649fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1650fd558d18SJames Chapman 1651309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1652309795f4SJames Chapman */ 1653309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1654309795f4SJames Chapman { 1655309795f4SJames Chapman int err = 0; 1656789a4a2cSJames Chapman struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL; 1657309795f4SJames Chapman 1658309795f4SJames Chapman /* Force the tunnel socket to close. This will eventually 1659309795f4SJames Chapman * cause the tunnel to be deleted via the normal socket close 1660309795f4SJames Chapman * mechanisms when userspace closes the tunnel socket. 1661309795f4SJames Chapman */ 1662789a4a2cSJames Chapman if (sock != NULL) { 1663789a4a2cSJames Chapman err = inet_shutdown(sock, 2); 1664789a4a2cSJames Chapman 1665789a4a2cSJames Chapman /* If the tunnel's socket was created by the kernel, 1666789a4a2cSJames Chapman * close the socket here since the socket was not 1667789a4a2cSJames Chapman * created by userspace. 1668789a4a2cSJames Chapman */ 1669789a4a2cSJames Chapman if (sock->file == NULL) 1670789a4a2cSJames Chapman err = inet_release(sock); 1671789a4a2cSJames Chapman } 1672309795f4SJames Chapman 1673309795f4SJames Chapman return err; 1674309795f4SJames Chapman } 1675309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1676309795f4SJames Chapman 1677fd558d18SJames Chapman /* Really kill the session. 1678fd558d18SJames Chapman */ 1679fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1680fd558d18SJames Chapman { 1681fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1682fd558d18SJames Chapman 1683fd558d18SJames Chapman BUG_ON(atomic_read(&session->ref_count) != 0); 1684fd558d18SJames Chapman 1685fd558d18SJames Chapman tunnel = session->tunnel; 1686fd558d18SJames Chapman if (tunnel != NULL) { 1687fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1688fd558d18SJames Chapman 1689fd558d18SJames Chapman /* Delete the session from the hash */ 1690fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1691fd558d18SJames Chapman hlist_del_init(&session->hlist); 1692fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1693fd558d18SJames Chapman 1694f7faffa3SJames Chapman /* Unlink from the global hash if not L2TPv2 */ 1695f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1696f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1697f7faffa3SJames Chapman 1698e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1699e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1700e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1701e02d494dSJames Chapman synchronize_rcu(); 1702f7faffa3SJames Chapman } 1703f7faffa3SJames Chapman 1704fd558d18SJames Chapman if (session->session_id != 0) 1705fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1706fd558d18SJames Chapman 1707fd558d18SJames Chapman sock_put(tunnel->sock); 1708fd558d18SJames Chapman 1709fd558d18SJames Chapman /* This will delete the tunnel context if this 1710fd558d18SJames Chapman * is the last session on the tunnel. 1711fd558d18SJames Chapman */ 1712fd558d18SJames Chapman session->tunnel = NULL; 1713fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1714fd558d18SJames Chapman } 1715fd558d18SJames Chapman 1716fd558d18SJames Chapman kfree(session); 1717fd558d18SJames Chapman 1718fd558d18SJames Chapman return; 1719fd558d18SJames Chapman } 1720fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1721fd558d18SJames Chapman 1722309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1723309795f4SJames Chapman pseudowire modules. 1724309795f4SJames Chapman */ 1725309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1726309795f4SJames Chapman { 1727309795f4SJames Chapman if (session->session_close != NULL) 1728309795f4SJames Chapman (*session->session_close)(session); 1729309795f4SJames Chapman 1730309795f4SJames Chapman l2tp_session_dec_refcount(session); 1731309795f4SJames Chapman 1732309795f4SJames Chapman return 0; 1733309795f4SJames Chapman } 1734309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1735309795f4SJames Chapman 1736309795f4SJames Chapman 1737f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1738f7faffa3SJames Chapman * l2specific_len parameters are set. 1739f7faffa3SJames Chapman */ 1740fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1741f7faffa3SJames Chapman { 1742f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1743f7faffa3SJames Chapman session->hdr_len = 6; 1744f7faffa3SJames Chapman if (session->send_seq) 1745f7faffa3SJames Chapman session->hdr_len += 4; 1746f7faffa3SJames Chapman } else { 17470d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 17480d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 17490d76751fSJames Chapman session->hdr_len += 4; 1750f7faffa3SJames Chapman } 1751f7faffa3SJames Chapman 1752f7faffa3SJames Chapman } 1753f7faffa3SJames Chapman 1754fd558d18SJames 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) 1755fd558d18SJames Chapman { 1756fd558d18SJames Chapman struct l2tp_session *session; 1757fd558d18SJames Chapman 1758fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1759fd558d18SJames Chapman if (session != NULL) { 1760fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1761fd558d18SJames Chapman session->tunnel = tunnel; 1762fd558d18SJames Chapman 1763fd558d18SJames Chapman session->session_id = session_id; 1764fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1765d301e325SJames Chapman session->nr = 0; 1766fd558d18SJames Chapman 1767fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1768fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1769fd558d18SJames Chapman 1770fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1771fd558d18SJames Chapman 1772fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1773f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1774fd558d18SJames Chapman 1775fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1776fd558d18SJames Chapman session->debug = tunnel->debug; 1777fd558d18SJames Chapman 1778fd558d18SJames Chapman if (cfg) { 1779f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1780fd558d18SJames Chapman session->debug = cfg->debug; 1781fd558d18SJames Chapman session->mtu = cfg->mtu; 1782fd558d18SJames Chapman session->mru = cfg->mru; 1783fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1784fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1785fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1786f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1787f7faffa3SJames Chapman session->offset = cfg->offset; 1788f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1789f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1790f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1791f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1792f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1793f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1794fd558d18SJames Chapman } 1795fd558d18SJames Chapman 1796f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1797f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1798f7faffa3SJames Chapman else 1799f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1800f7faffa3SJames Chapman 1801f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1802f7faffa3SJames Chapman 1803fd558d18SJames Chapman /* Bump the reference count. The session context is deleted 1804fd558d18SJames Chapman * only when this drops to zero. 1805fd558d18SJames Chapman */ 1806fd558d18SJames Chapman l2tp_session_inc_refcount(session); 1807fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1808fd558d18SJames Chapman 1809fd558d18SJames Chapman /* Ensure tunnel socket isn't deleted */ 1810fd558d18SJames Chapman sock_hold(tunnel->sock); 1811fd558d18SJames Chapman 1812fd558d18SJames Chapman /* Add session to the tunnel's hash list */ 1813fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1814fd558d18SJames Chapman hlist_add_head(&session->hlist, 1815fd558d18SJames Chapman l2tp_session_id_hash(tunnel, session_id)); 1816fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1817fd558d18SJames Chapman 1818f7faffa3SJames Chapman /* And to the global session list if L2TPv3 */ 1819f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1820f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1821f7faffa3SJames Chapman 1822e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1823e02d494dSJames Chapman hlist_add_head_rcu(&session->global_hlist, 1824f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id)); 1825e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1826f7faffa3SJames Chapman } 1827f7faffa3SJames Chapman 1828fd558d18SJames Chapman /* Ignore management session in session count value */ 1829fd558d18SJames Chapman if (session->session_id != 0) 1830fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1831fd558d18SJames Chapman } 1832fd558d18SJames Chapman 1833fd558d18SJames Chapman return session; 1834fd558d18SJames Chapman } 1835fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1836fd558d18SJames Chapman 1837fd558d18SJames Chapman /***************************************************************************** 1838fd558d18SJames Chapman * Init and cleanup 1839fd558d18SJames Chapman *****************************************************************************/ 1840fd558d18SJames Chapman 1841fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1842fd558d18SJames Chapman { 1843e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1844f7faffa3SJames Chapman int hash; 1845fd558d18SJames Chapman 1846fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1847e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1848fd558d18SJames Chapman 1849f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1850f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1851f7faffa3SJames Chapman 1852e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1853f7faffa3SJames Chapman 1854fd558d18SJames Chapman return 0; 1855fd558d18SJames Chapman } 1856fd558d18SJames Chapman 1857fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1858fd558d18SJames Chapman .init = l2tp_init_net, 1859fd558d18SJames Chapman .id = &l2tp_net_id, 1860fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1861fd558d18SJames Chapman }; 1862fd558d18SJames Chapman 1863fd558d18SJames Chapman static int __init l2tp_init(void) 1864fd558d18SJames Chapman { 1865fd558d18SJames Chapman int rc = 0; 1866fd558d18SJames Chapman 1867fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1868fd558d18SJames Chapman if (rc) 1869fd558d18SJames Chapman goto out; 1870fd558d18SJames Chapman 1871fd558d18SJames Chapman printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION); 1872fd558d18SJames Chapman 1873fd558d18SJames Chapman out: 1874fd558d18SJames Chapman return rc; 1875fd558d18SJames Chapman } 1876fd558d18SJames Chapman 1877fd558d18SJames Chapman static void __exit l2tp_exit(void) 1878fd558d18SJames Chapman { 1879fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1880fd558d18SJames Chapman } 1881fd558d18SJames Chapman 1882fd558d18SJames Chapman module_init(l2tp_init); 1883fd558d18SJames Chapman module_exit(l2tp_exit); 1884fd558d18SJames Chapman 1885fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1886fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1887fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1888fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1889fd558d18SJames Chapman 1890