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> 56fd558d18SJames Chapman 57fd558d18SJames Chapman #include <asm/byteorder.h> 58fd558d18SJames Chapman #include <asm/atomic.h> 59fd558d18SJames Chapman 60fd558d18SJames Chapman #include "l2tp_core.h" 61fd558d18SJames Chapman 62fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 63fd558d18SJames Chapman 64fd558d18SJames Chapman /* L2TP header constants */ 65fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 66fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 67fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 68fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 69fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 70fd558d18SJames Chapman 71fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 72fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 73f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 74fd558d18SJames Chapman 75fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 76fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 77fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 78fd558d18SJames Chapman 79fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ 10 80fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ 6 81fd558d18SJames Chapman 82fd558d18SJames Chapman /* Default trace flags */ 83fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 84fd558d18SJames Chapman 85fd558d18SJames Chapman #define PRINTK(_mask, _type, _lvl, _fmt, args...) \ 86fd558d18SJames Chapman do { \ 87fd558d18SJames Chapman if ((_mask) & (_type)) \ 88fd558d18SJames Chapman printk(_lvl "L2TP: " _fmt, ##args); \ 89fd558d18SJames Chapman } while (0) 90fd558d18SJames Chapman 91fd558d18SJames Chapman /* Private data stored for received packets in the skb. 92fd558d18SJames Chapman */ 93fd558d18SJames Chapman struct l2tp_skb_cb { 94f7faffa3SJames Chapman u32 ns; 95fd558d18SJames Chapman u16 has_seq; 96fd558d18SJames Chapman u16 length; 97fd558d18SJames Chapman unsigned long expires; 98fd558d18SJames Chapman }; 99fd558d18SJames Chapman 100fd558d18SJames Chapman #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 101fd558d18SJames Chapman 102fd558d18SJames Chapman static atomic_t l2tp_tunnel_count; 103fd558d18SJames Chapman static atomic_t l2tp_session_count; 104fd558d18SJames Chapman 105fd558d18SJames Chapman /* per-net private data for this module */ 106fd558d18SJames Chapman static unsigned int l2tp_net_id; 107fd558d18SJames Chapman struct l2tp_net { 108fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 109e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 110f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 111e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 112fd558d18SJames Chapman }; 113fd558d18SJames Chapman 114fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(struct net *net) 115fd558d18SJames Chapman { 116fd558d18SJames Chapman BUG_ON(!net); 117fd558d18SJames Chapman 118fd558d18SJames Chapman return net_generic(net, l2tp_net_id); 119fd558d18SJames Chapman } 120fd558d18SJames Chapman 121f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 122f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 123f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 124f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 125f7faffa3SJames Chapman */ 126f7faffa3SJames Chapman static inline struct hlist_head * 127f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 128f7faffa3SJames Chapman { 129f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 130f7faffa3SJames Chapman 131f7faffa3SJames Chapman } 132f7faffa3SJames Chapman 133f7faffa3SJames Chapman /* Lookup a session by id in the global session list 134f7faffa3SJames Chapman */ 135f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id) 136f7faffa3SJames Chapman { 137f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 138f7faffa3SJames Chapman struct hlist_head *session_list = 139f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id); 140f7faffa3SJames Chapman struct l2tp_session *session; 141f7faffa3SJames Chapman struct hlist_node *walk; 142f7faffa3SJames Chapman 143e02d494dSJames Chapman rcu_read_lock_bh(); 144e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) { 145f7faffa3SJames Chapman if (session->session_id == session_id) { 146e02d494dSJames Chapman rcu_read_unlock_bh(); 147f7faffa3SJames Chapman return session; 148f7faffa3SJames Chapman } 149f7faffa3SJames Chapman } 150e02d494dSJames Chapman rcu_read_unlock_bh(); 151f7faffa3SJames Chapman 152f7faffa3SJames Chapman return NULL; 153f7faffa3SJames Chapman } 154f7faffa3SJames Chapman 155fd558d18SJames Chapman /* Session hash list. 156fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 157fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 158fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 159fd558d18SJames Chapman * simple bitmask. 160fd558d18SJames Chapman */ 161fd558d18SJames Chapman static inline struct hlist_head * 162fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 163fd558d18SJames Chapman { 164fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 165fd558d18SJames Chapman } 166fd558d18SJames Chapman 167fd558d18SJames Chapman /* Lookup a session by id 168fd558d18SJames Chapman */ 169f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id) 170fd558d18SJames Chapman { 171f7faffa3SJames Chapman struct hlist_head *session_list; 172fd558d18SJames Chapman struct l2tp_session *session; 173fd558d18SJames Chapman struct hlist_node *walk; 174fd558d18SJames Chapman 175f7faffa3SJames Chapman /* In L2TPv3, session_ids are unique over all tunnels and we 176f7faffa3SJames Chapman * sometimes need to look them up before we know the 177f7faffa3SJames Chapman * tunnel. 178f7faffa3SJames Chapman */ 179f7faffa3SJames Chapman if (tunnel == NULL) 180f7faffa3SJames Chapman return l2tp_session_find_2(net, session_id); 181f7faffa3SJames Chapman 182f7faffa3SJames Chapman session_list = l2tp_session_id_hash(tunnel, session_id); 183fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 184fd558d18SJames Chapman hlist_for_each_entry(session, walk, session_list, hlist) { 185fd558d18SJames Chapman if (session->session_id == session_id) { 186fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 187fd558d18SJames Chapman return session; 188fd558d18SJames Chapman } 189fd558d18SJames Chapman } 190fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 191fd558d18SJames Chapman 192fd558d18SJames Chapman return NULL; 193fd558d18SJames Chapman } 194fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find); 195fd558d18SJames Chapman 196fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth) 197fd558d18SJames Chapman { 198fd558d18SJames Chapman int hash; 199fd558d18SJames Chapman struct hlist_node *walk; 200fd558d18SJames Chapman struct l2tp_session *session; 201fd558d18SJames Chapman int count = 0; 202fd558d18SJames Chapman 203fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 204fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 205fd558d18SJames Chapman hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) { 206fd558d18SJames Chapman if (++count > nth) { 207fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 208fd558d18SJames Chapman return session; 209fd558d18SJames Chapman } 210fd558d18SJames Chapman } 211fd558d18SJames Chapman } 212fd558d18SJames Chapman 213fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 214fd558d18SJames Chapman 215fd558d18SJames Chapman return NULL; 216fd558d18SJames Chapman } 217fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth); 218fd558d18SJames Chapman 219309795f4SJames Chapman /* Lookup a session by interface name. 220309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 221309795f4SJames Chapman */ 222309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) 223309795f4SJames Chapman { 224309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 225309795f4SJames Chapman int hash; 226309795f4SJames Chapman struct hlist_node *walk; 227309795f4SJames Chapman struct l2tp_session *session; 228309795f4SJames Chapman 229e02d494dSJames Chapman rcu_read_lock_bh(); 230309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 231e02d494dSJames Chapman hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) { 232309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 233e02d494dSJames Chapman rcu_read_unlock_bh(); 234309795f4SJames Chapman return session; 235309795f4SJames Chapman } 236309795f4SJames Chapman } 237309795f4SJames Chapman } 238309795f4SJames Chapman 239e02d494dSJames Chapman rcu_read_unlock_bh(); 240309795f4SJames Chapman 241309795f4SJames Chapman return NULL; 242309795f4SJames Chapman } 243309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname); 244309795f4SJames Chapman 245fd558d18SJames Chapman /* Lookup a tunnel by id 246fd558d18SJames Chapman */ 247fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id) 248fd558d18SJames Chapman { 249fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 250fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 251fd558d18SJames Chapman 252e02d494dSJames Chapman rcu_read_lock_bh(); 253e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 254fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 255e02d494dSJames Chapman rcu_read_unlock_bh(); 256fd558d18SJames Chapman return tunnel; 257fd558d18SJames Chapman } 258fd558d18SJames Chapman } 259e02d494dSJames Chapman rcu_read_unlock_bh(); 260fd558d18SJames Chapman 261fd558d18SJames Chapman return NULL; 262fd558d18SJames Chapman } 263fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 264fd558d18SJames Chapman 265fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth) 266fd558d18SJames Chapman { 267fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 268fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 269fd558d18SJames Chapman int count = 0; 270fd558d18SJames Chapman 271e02d494dSJames Chapman rcu_read_lock_bh(); 272e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 273fd558d18SJames Chapman if (++count > nth) { 274e02d494dSJames Chapman rcu_read_unlock_bh(); 275fd558d18SJames Chapman return tunnel; 276fd558d18SJames Chapman } 277fd558d18SJames Chapman } 278fd558d18SJames Chapman 279e02d494dSJames Chapman rcu_read_unlock_bh(); 280fd558d18SJames Chapman 281fd558d18SJames Chapman return NULL; 282fd558d18SJames Chapman } 283fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 284fd558d18SJames Chapman 285fd558d18SJames Chapman /***************************************************************************** 286fd558d18SJames Chapman * Receive data handling 287fd558d18SJames Chapman *****************************************************************************/ 288fd558d18SJames Chapman 289fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 290fd558d18SJames Chapman * number. 291fd558d18SJames Chapman */ 292fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 293fd558d18SJames Chapman { 294fd558d18SJames Chapman struct sk_buff *skbp; 295fd558d18SJames Chapman struct sk_buff *tmp; 296f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 297fd558d18SJames Chapman 298fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 299fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 300fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 301fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 302fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 303fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 304fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 305fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 306fd558d18SJames Chapman session->stats.rx_oos_packets++; 307fd558d18SJames Chapman goto out; 308fd558d18SJames Chapman } 309fd558d18SJames Chapman } 310fd558d18SJames Chapman 311fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 312fd558d18SJames Chapman 313fd558d18SJames Chapman out: 314fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 315fd558d18SJames Chapman } 316fd558d18SJames Chapman 317fd558d18SJames Chapman /* Dequeue a single skb. 318fd558d18SJames Chapman */ 319fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 320fd558d18SJames Chapman { 321fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 322fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 323fd558d18SJames Chapman 324fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 325fd558d18SJames Chapman * to its current owner (a socket receive buffer). 326fd558d18SJames Chapman */ 327fd558d18SJames Chapman skb_orphan(skb); 328fd558d18SJames Chapman 329fd558d18SJames Chapman tunnel->stats.rx_packets++; 330fd558d18SJames Chapman tunnel->stats.rx_bytes += length; 331fd558d18SJames Chapman session->stats.rx_packets++; 332fd558d18SJames Chapman session->stats.rx_bytes += length; 333fd558d18SJames Chapman 334fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 335fd558d18SJames Chapman /* Bump our Nr */ 336fd558d18SJames Chapman session->nr++; 337f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 338f7faffa3SJames Chapman session->nr &= 0xffff; 339f7faffa3SJames Chapman else 340f7faffa3SJames Chapman session->nr &= 0xffffff; 341f7faffa3SJames Chapman 342fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 343fd558d18SJames Chapman "%s: updated nr to %hu\n", session->name, session->nr); 344fd558d18SJames Chapman } 345fd558d18SJames Chapman 346fd558d18SJames Chapman /* call private receive handler */ 347fd558d18SJames Chapman if (session->recv_skb != NULL) 348fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 349fd558d18SJames Chapman else 350fd558d18SJames Chapman kfree_skb(skb); 351fd558d18SJames Chapman 352fd558d18SJames Chapman if (session->deref) 353fd558d18SJames Chapman (*session->deref)(session); 354fd558d18SJames Chapman } 355fd558d18SJames Chapman 356fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 357fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 358fd558d18SJames Chapman */ 359fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 360fd558d18SJames Chapman { 361fd558d18SJames Chapman struct sk_buff *skb; 362fd558d18SJames Chapman struct sk_buff *tmp; 363fd558d18SJames Chapman 364fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 365fd558d18SJames Chapman * expect to send up next, dequeue it and any other 366fd558d18SJames Chapman * in-sequence packets behind it. 367fd558d18SJames Chapman */ 368fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 369fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 370fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 371fd558d18SJames Chapman session->stats.rx_seq_discards++; 372fd558d18SJames Chapman session->stats.rx_errors++; 373fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 374f7faffa3SJames Chapman "%s: oos pkt %u len %d discarded (too old), " 375f7faffa3SJames Chapman "waiting for %u, reorder_q_len=%d\n", 376fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 377fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 378fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 379fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 380fd558d18SJames Chapman kfree_skb(skb); 381fd558d18SJames Chapman if (session->deref) 382fd558d18SJames Chapman (*session->deref)(session); 383fd558d18SJames Chapman continue; 384fd558d18SJames Chapman } 385fd558d18SJames Chapman 386fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 387fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 388fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 389f7faffa3SJames Chapman "%s: holding oos pkt %u len %d, " 390f7faffa3SJames Chapman "waiting for %u, reorder_q_len=%d\n", 391fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 392fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 393fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 394fd558d18SJames Chapman goto out; 395fd558d18SJames Chapman } 396fd558d18SJames Chapman } 397fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 398fd558d18SJames Chapman 399fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 400fd558d18SJames Chapman * do so to let other contexts process the queue. 401fd558d18SJames Chapman */ 402fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 403fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 404fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 405fd558d18SJames Chapman } 406fd558d18SJames Chapman 407fd558d18SJames Chapman out: 408fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 409fd558d18SJames Chapman } 410fd558d18SJames Chapman 411fd558d18SJames Chapman static inline int l2tp_verify_udp_checksum(struct sock *sk, 412fd558d18SJames Chapman struct sk_buff *skb) 413fd558d18SJames Chapman { 414fd558d18SJames Chapman struct udphdr *uh = udp_hdr(skb); 415fd558d18SJames Chapman u16 ulen = ntohs(uh->len); 416fd558d18SJames Chapman struct inet_sock *inet; 417fd558d18SJames Chapman __wsum psum; 418fd558d18SJames Chapman 419fd558d18SJames Chapman if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check) 420fd558d18SJames Chapman return 0; 421fd558d18SJames Chapman 422fd558d18SJames Chapman inet = inet_sk(sk); 423fd558d18SJames Chapman psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen, 424fd558d18SJames Chapman IPPROTO_UDP, 0); 425fd558d18SJames Chapman 426fd558d18SJames Chapman if ((skb->ip_summed == CHECKSUM_COMPLETE) && 427fd558d18SJames Chapman !csum_fold(csum_add(psum, skb->csum))) 428fd558d18SJames Chapman return 0; 429fd558d18SJames Chapman 430fd558d18SJames Chapman skb->csum = psum; 431fd558d18SJames Chapman 432fd558d18SJames Chapman return __skb_checksum_complete(skb); 433fd558d18SJames Chapman } 434fd558d18SJames Chapman 435f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 436f7faffa3SJames Chapman * and L2TPv3 data frames here. 437f7faffa3SJames Chapman * 438f7faffa3SJames Chapman * L2TPv2 Data Message Header 439f7faffa3SJames Chapman * 440f7faffa3SJames Chapman * 0 1 2 3 441f7faffa3SJames 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 442f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 443f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 444f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 445f7faffa3SJames Chapman * | Tunnel ID | Session ID | 446f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 447f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 448f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 449f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 450f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 451f7faffa3SJames Chapman * 452f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 453f7faffa3SJames Chapman * those in L2TP control frames. 454f7faffa3SJames Chapman * 455f7faffa3SJames Chapman * L2TPv3 Data Message Header 456f7faffa3SJames Chapman * 457f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 458f7faffa3SJames Chapman * | L2TP Session Header | 459f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 460f7faffa3SJames Chapman * | L2-Specific Sublayer | 461f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 462f7faffa3SJames Chapman * | Tunnel Payload ... 463f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 464f7faffa3SJames Chapman * 465f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 466f7faffa3SJames Chapman * 467f7faffa3SJames Chapman * 0 1 2 3 468f7faffa3SJames 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 469f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 470f7faffa3SJames Chapman * | Session ID | 471f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 472f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 473f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 474f7faffa3SJames Chapman * | 475f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 476f7faffa3SJames Chapman * 477f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 478f7faffa3SJames Chapman * 479f7faffa3SJames Chapman * 0 1 2 3 480f7faffa3SJames 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 481f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 482f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 483f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 484f7faffa3SJames Chapman * 485f7faffa3SJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with 486f7faffa3SJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need 487f7faffa3SJames Chapman * to parse the packet header to determine if optional fields are 488f7faffa3SJames Chapman * present. 489f7faffa3SJames Chapman * 490f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 491f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 492f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 493f7faffa3SJames Chapman * after the session-id. 494f7faffa3SJames Chapman */ 495f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 496f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 497f7faffa3SJames Chapman int length, int (*payload_hook)(struct sk_buff *skb)) 498f7faffa3SJames Chapman { 499f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 500f7faffa3SJames Chapman int offset; 501f7faffa3SJames Chapman u32 ns, nr; 502f7faffa3SJames Chapman 503f7faffa3SJames Chapman /* The ref count is increased since we now hold a pointer to 504f7faffa3SJames Chapman * the session. Take care to decrement the refcnt when exiting 505f7faffa3SJames Chapman * this function from now on... 506f7faffa3SJames Chapman */ 507f7faffa3SJames Chapman l2tp_session_inc_refcount(session); 508f7faffa3SJames Chapman if (session->ref) 509f7faffa3SJames Chapman (*session->ref)(session); 510f7faffa3SJames Chapman 511f7faffa3SJames Chapman /* Parse and check optional cookie */ 512f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 513f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 514f7faffa3SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 515f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 516f7faffa3SJames Chapman tunnel->name, tunnel->tunnel_id, session->session_id); 517f7faffa3SJames Chapman session->stats.rx_cookie_discards++; 518f7faffa3SJames Chapman goto discard; 519f7faffa3SJames Chapman } 520f7faffa3SJames Chapman ptr += session->peer_cookie_len; 521f7faffa3SJames Chapman } 522f7faffa3SJames Chapman 523f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 524f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 525f7faffa3SJames Chapman * 526f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 527f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 528f7faffa3SJames Chapman * we were expecting them, discard frame. 529f7faffa3SJames Chapman */ 530f7faffa3SJames Chapman ns = nr = 0; 531f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 532f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 533f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 534f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 535f7faffa3SJames Chapman ptr += 2; 536f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 537f7faffa3SJames Chapman ptr += 2; 538f7faffa3SJames Chapman 539f7faffa3SJames Chapman /* Store L2TP info in the skb */ 540f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 541f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 542f7faffa3SJames Chapman 543f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 544f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 545f7faffa3SJames Chapman session->name, ns, nr, session->nr); 546f7faffa3SJames Chapman } 547f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 548f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 549f7faffa3SJames Chapman 550f7faffa3SJames Chapman if (l2h & 0x40000000) { 551f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 552f7faffa3SJames Chapman 553f7faffa3SJames Chapman /* Store L2TP info in the skb */ 554f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 555f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 556f7faffa3SJames Chapman 557f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 558f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 559f7faffa3SJames Chapman session->name, ns, session->nr); 560f7faffa3SJames Chapman } 561f7faffa3SJames Chapman } 562f7faffa3SJames Chapman 563f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 564f7faffa3SJames Chapman ptr += session->l2specific_len; 565f7faffa3SJames Chapman 566f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 567f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 568f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 569f7faffa3SJames Chapman * configure it so. 570f7faffa3SJames Chapman */ 571f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 572f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO, 573f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 574f7faffa3SJames Chapman session->name); 575f7faffa3SJames Chapman session->send_seq = -1; 576f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 577f7faffa3SJames Chapman } 578f7faffa3SJames Chapman } else { 579f7faffa3SJames Chapman /* No sequence numbers. 580f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 581f7faffa3SJames Chapman */ 582f7faffa3SJames Chapman if (session->recv_seq) { 583f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING, 584f7faffa3SJames Chapman "%s: recv data has no seq numbers when required. " 585f7faffa3SJames Chapman "Discarding\n", session->name); 586f7faffa3SJames Chapman session->stats.rx_seq_discards++; 587f7faffa3SJames Chapman goto discard; 588f7faffa3SJames Chapman } 589f7faffa3SJames Chapman 590f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 591f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 592f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 593f7faffa3SJames Chapman * LAC is broken. Discard the frame. 594f7faffa3SJames Chapman */ 595f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 596f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO, 597f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 598f7faffa3SJames Chapman session->name); 599f7faffa3SJames Chapman session->send_seq = 0; 600f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 601f7faffa3SJames Chapman } else if (session->send_seq) { 602f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING, 603f7faffa3SJames Chapman "%s: recv data has no seq numbers when required. " 604f7faffa3SJames Chapman "Discarding\n", session->name); 605f7faffa3SJames Chapman session->stats.rx_seq_discards++; 606f7faffa3SJames Chapman goto discard; 607f7faffa3SJames Chapman } 608f7faffa3SJames Chapman } 609f7faffa3SJames Chapman 610f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 611f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 612f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 613f7faffa3SJames Chapman * in the session setup control protocol. 614f7faffa3SJames Chapman */ 615f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 616f7faffa3SJames Chapman /* If offset bit set, skip it. */ 617f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 618f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 619f7faffa3SJames Chapman ptr += 2 + offset; 620f7faffa3SJames Chapman } 621f7faffa3SJames Chapman } else 622f7faffa3SJames Chapman ptr += session->offset; 623f7faffa3SJames Chapman 624f7faffa3SJames Chapman offset = ptr - optr; 625f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 626f7faffa3SJames Chapman goto discard; 627f7faffa3SJames Chapman 628f7faffa3SJames Chapman __skb_pull(skb, offset); 629f7faffa3SJames Chapman 630f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 631f7faffa3SJames Chapman * packet, do so now. 632f7faffa3SJames Chapman */ 633f7faffa3SJames Chapman if (payload_hook) 634f7faffa3SJames Chapman if ((*payload_hook)(skb)) 635f7faffa3SJames Chapman goto discard; 636f7faffa3SJames Chapman 637f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 638f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 639f7faffa3SJames Chapman * reordering. 640f7faffa3SJames Chapman */ 641f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 642f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 643f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 644f7faffa3SJames Chapman 645f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 646f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 647f7faffa3SJames Chapman */ 648f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 649f7faffa3SJames Chapman if (session->reorder_timeout != 0) { 650f7faffa3SJames Chapman /* Packet reordering enabled. Add skb to session's 651f7faffa3SJames Chapman * reorder queue, in order of ns. 652f7faffa3SJames Chapman */ 653f7faffa3SJames Chapman l2tp_recv_queue_skb(session, skb); 654f7faffa3SJames Chapman } else { 655f7faffa3SJames Chapman /* Packet reordering disabled. Discard out-of-sequence 656f7faffa3SJames Chapman * packets 657f7faffa3SJames Chapman */ 658f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 659f7faffa3SJames Chapman session->stats.rx_seq_discards++; 660f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 661f7faffa3SJames Chapman "%s: oos pkt %u len %d discarded, " 662f7faffa3SJames Chapman "waiting for %u, reorder_q_len=%d\n", 663f7faffa3SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 664f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 665f7faffa3SJames Chapman skb_queue_len(&session->reorder_q)); 666f7faffa3SJames Chapman goto discard; 667f7faffa3SJames Chapman } 668f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 669f7faffa3SJames Chapman } 670f7faffa3SJames Chapman } else { 671f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 672f7faffa3SJames Chapman * reorder queue. This ensures that it will be 673f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 674f7faffa3SJames Chapman */ 675f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 676f7faffa3SJames Chapman } 677f7faffa3SJames Chapman 678f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 679f7faffa3SJames Chapman l2tp_recv_dequeue(session); 680f7faffa3SJames Chapman 681f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 682f7faffa3SJames Chapman 683f7faffa3SJames Chapman return; 684f7faffa3SJames Chapman 685f7faffa3SJames Chapman discard: 686f7faffa3SJames Chapman session->stats.rx_errors++; 687f7faffa3SJames Chapman kfree_skb(skb); 688f7faffa3SJames Chapman 689f7faffa3SJames Chapman if (session->deref) 690f7faffa3SJames Chapman (*session->deref)(session); 691f7faffa3SJames Chapman 692f7faffa3SJames Chapman l2tp_session_dec_refcount(session); 693f7faffa3SJames Chapman } 694f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common); 695f7faffa3SJames Chapman 696fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 697fd558d18SJames Chapman * here. The skb is not on a list when we get here. 698fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 699fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 700fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 701fd558d18SJames Chapman */ 702fd558d18SJames Chapman int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 703fd558d18SJames Chapman int (*payload_hook)(struct sk_buff *skb)) 704fd558d18SJames Chapman { 705fd558d18SJames Chapman struct l2tp_session *session = NULL; 706fd558d18SJames Chapman unsigned char *ptr, *optr; 707fd558d18SJames Chapman u16 hdrflags; 708fd558d18SJames Chapman u32 tunnel_id, session_id; 709fd558d18SJames Chapman int offset; 710fd558d18SJames Chapman u16 version; 711f7faffa3SJames Chapman int length; 712fd558d18SJames Chapman 713fd558d18SJames Chapman if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb)) 714fd558d18SJames Chapman goto discard_bad_csum; 715fd558d18SJames Chapman 716fd558d18SJames Chapman /* UDP always verifies the packet length. */ 717fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 718fd558d18SJames Chapman 719fd558d18SJames Chapman /* Short packet? */ 720fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 721fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 722fd558d18SJames Chapman "%s: recv short packet (len=%d)\n", tunnel->name, skb->len); 723fd558d18SJames Chapman goto error; 724fd558d18SJames Chapman } 725fd558d18SJames Chapman 726fd558d18SJames Chapman /* Point to L2TP header */ 727fd558d18SJames Chapman optr = ptr = skb->data; 728fd558d18SJames Chapman 729fd558d18SJames Chapman /* Trace packet contents, if enabled */ 730fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 731fd558d18SJames Chapman length = min(32u, skb->len); 732fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 733fd558d18SJames Chapman goto error; 734fd558d18SJames Chapman 735fd558d18SJames Chapman printk(KERN_DEBUG "%s: recv: ", tunnel->name); 736fd558d18SJames Chapman 737fd558d18SJames Chapman offset = 0; 738fd558d18SJames Chapman do { 739fd558d18SJames Chapman printk(" %02X", ptr[offset]); 740fd558d18SJames Chapman } while (++offset < length); 741fd558d18SJames Chapman 742fd558d18SJames Chapman printk("\n"); 743fd558d18SJames Chapman } 744fd558d18SJames Chapman 745fd558d18SJames Chapman /* Get L2TP header flags */ 746fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 747fd558d18SJames Chapman 748fd558d18SJames Chapman /* Check protocol version */ 749fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 750fd558d18SJames Chapman if (version != tunnel->version) { 751fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 752fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 753fd558d18SJames Chapman tunnel->name, version, tunnel->version); 754fd558d18SJames Chapman goto error; 755fd558d18SJames Chapman } 756fd558d18SJames Chapman 757fd558d18SJames Chapman /* Get length of L2TP packet */ 758fd558d18SJames Chapman length = skb->len; 759fd558d18SJames Chapman 760fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 761fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 762fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG, 763fd558d18SJames Chapman "%s: recv control packet, len=%d\n", tunnel->name, length); 764fd558d18SJames Chapman goto error; 765fd558d18SJames Chapman } 766fd558d18SJames Chapman 767fd558d18SJames Chapman /* Skip flags */ 768fd558d18SJames Chapman ptr += 2; 769fd558d18SJames Chapman 770f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 771fd558d18SJames Chapman /* If length is present, skip it */ 772fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 773fd558d18SJames Chapman ptr += 2; 774fd558d18SJames Chapman 775fd558d18SJames Chapman /* Extract tunnel and session ID */ 776fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 777fd558d18SJames Chapman ptr += 2; 778fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 779fd558d18SJames Chapman ptr += 2; 780f7faffa3SJames Chapman } else { 781f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 782f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 783f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 784f7faffa3SJames Chapman ptr += 4; 785f7faffa3SJames Chapman } 786fd558d18SJames Chapman 787fd558d18SJames Chapman /* Find the session context */ 788f7faffa3SJames Chapman session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); 789309795f4SJames Chapman if (!session || !session->recv_skb) { 790fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 791fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO, 792f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 793fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 794fd558d18SJames Chapman goto error; 795fd558d18SJames Chapman } 796fd558d18SJames Chapman 797f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 798fd558d18SJames Chapman 799fd558d18SJames Chapman return 0; 800fd558d18SJames Chapman 801fd558d18SJames Chapman discard_bad_csum: 802fd558d18SJames Chapman LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name); 803fd558d18SJames Chapman UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0); 804fd558d18SJames Chapman tunnel->stats.rx_errors++; 805fd558d18SJames Chapman kfree_skb(skb); 806fd558d18SJames Chapman 807fd558d18SJames Chapman return 0; 808fd558d18SJames Chapman 809fd558d18SJames Chapman error: 810fd558d18SJames Chapman /* Put UDP header back */ 811fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 812fd558d18SJames Chapman 813fd558d18SJames Chapman return 1; 814fd558d18SJames Chapman } 815fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_recv_core); 816fd558d18SJames Chapman 817fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 818fd558d18SJames Chapman * Return codes: 819fd558d18SJames Chapman * 0 : success. 820fd558d18SJames Chapman * <0: error 821fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 822fd558d18SJames Chapman */ 823fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 824fd558d18SJames Chapman { 825fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 826fd558d18SJames Chapman 827fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 828fd558d18SJames Chapman if (tunnel == NULL) 829fd558d18SJames Chapman goto pass_up; 830fd558d18SJames Chapman 831fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG, 832fd558d18SJames Chapman "%s: received %d bytes\n", tunnel->name, skb->len); 833fd558d18SJames Chapman 834fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 835fd558d18SJames Chapman goto pass_up_put; 836fd558d18SJames Chapman 837fd558d18SJames Chapman sock_put(sk); 838fd558d18SJames Chapman return 0; 839fd558d18SJames Chapman 840fd558d18SJames Chapman pass_up_put: 841fd558d18SJames Chapman sock_put(sk); 842fd558d18SJames Chapman pass_up: 843fd558d18SJames Chapman return 1; 844fd558d18SJames Chapman } 845fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 846fd558d18SJames Chapman 847fd558d18SJames Chapman /************************************************************************ 848fd558d18SJames Chapman * Transmit handling 849fd558d18SJames Chapman ***********************************************************************/ 850fd558d18SJames Chapman 851fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 852fd558d18SJames Chapman */ 853f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 854fd558d18SJames Chapman { 855f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 856fd558d18SJames Chapman __be16 *bufp = buf; 857f7faffa3SJames Chapman __be16 *optr = buf; 858fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 859fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 860fd558d18SJames Chapman u32 session_id = session->peer_session_id; 861fd558d18SJames Chapman 862fd558d18SJames Chapman if (session->send_seq) 863fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 864fd558d18SJames Chapman 865fd558d18SJames Chapman /* Setup L2TP header. */ 866fd558d18SJames Chapman *bufp++ = htons(flags); 867fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 868fd558d18SJames Chapman *bufp++ = htons(session_id); 869fd558d18SJames Chapman if (session->send_seq) { 870fd558d18SJames Chapman *bufp++ = htons(session->ns); 871fd558d18SJames Chapman *bufp++ = 0; 872fd558d18SJames Chapman session->ns++; 873f7faffa3SJames Chapman session->ns &= 0xffff; 874fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 875f7faffa3SJames Chapman "%s: updated ns to %u\n", session->name, session->ns); 876fd558d18SJames Chapman } 877fd558d18SJames Chapman 878f7faffa3SJames Chapman return bufp - optr; 879f7faffa3SJames Chapman } 880f7faffa3SJames Chapman 881f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 882fd558d18SJames Chapman { 8830d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 884f7faffa3SJames Chapman char *bufp = buf; 885f7faffa3SJames Chapman char *optr = bufp; 886fd558d18SJames Chapman 8870d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 8880d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 8890d76751fSJames Chapman */ 8900d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 8910d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 892f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 893f7faffa3SJames Chapman bufp += 2; 894f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 895f7faffa3SJames Chapman bufp += 2; 8960d76751fSJames Chapman } 8970d76751fSJames Chapman 898f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 899f7faffa3SJames Chapman bufp += 4; 900f7faffa3SJames Chapman if (session->cookie_len) { 901f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 902f7faffa3SJames Chapman bufp += session->cookie_len; 903fd558d18SJames Chapman } 904f7faffa3SJames Chapman if (session->l2specific_len) { 905f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 906f7faffa3SJames Chapman u32 l2h = 0; 907f7faffa3SJames Chapman if (session->send_seq) { 908f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 909f7faffa3SJames Chapman session->ns++; 910f7faffa3SJames Chapman session->ns &= 0xffffff; 911f7faffa3SJames Chapman PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG, 912f7faffa3SJames Chapman "%s: updated ns to %u\n", session->name, session->ns); 913f7faffa3SJames Chapman } 914f7faffa3SJames Chapman 915f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 916f7faffa3SJames Chapman } 917f7faffa3SJames Chapman bufp += session->l2specific_len; 918f7faffa3SJames Chapman } 919f7faffa3SJames Chapman if (session->offset) 920f7faffa3SJames Chapman bufp += session->offset; 921f7faffa3SJames Chapman 922f7faffa3SJames Chapman return bufp - optr; 923f7faffa3SJames Chapman } 924fd558d18SJames Chapman 925fd558d18SJames Chapman int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len) 926fd558d18SJames Chapman { 927fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 928fd558d18SJames Chapman unsigned int len = skb->len; 929fd558d18SJames Chapman int error; 930fd558d18SJames Chapman 931fd558d18SJames Chapman /* Debug */ 932fd558d18SJames Chapman if (session->send_seq) 933fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG, 934f7faffa3SJames Chapman "%s: send %Zd bytes, ns=%u\n", session->name, 935fd558d18SJames Chapman data_len, session->ns - 1); 936fd558d18SJames Chapman else 937fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG, 938fd558d18SJames Chapman "%s: send %Zd bytes\n", session->name, data_len); 939fd558d18SJames Chapman 940fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 941fd558d18SJames Chapman int i; 9420d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 9430d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 944fd558d18SJames Chapman 945fd558d18SJames Chapman printk(KERN_DEBUG "%s: xmit:", session->name); 9460d76751fSJames Chapman for (i = 0; i < (len - uhlen); i++) { 947fd558d18SJames Chapman printk(" %02X", *datap++); 948fd558d18SJames Chapman if (i == 31) { 949fd558d18SJames Chapman printk(" ..."); 950fd558d18SJames Chapman break; 951fd558d18SJames Chapman } 952fd558d18SJames Chapman } 953fd558d18SJames Chapman printk("\n"); 954fd558d18SJames Chapman } 955fd558d18SJames Chapman 956fd558d18SJames Chapman /* Queue the packet to IP for output */ 957fd558d18SJames Chapman error = ip_queue_xmit(skb, 1); 958fd558d18SJames Chapman 959fd558d18SJames Chapman /* Update stats */ 960fd558d18SJames Chapman if (error >= 0) { 961fd558d18SJames Chapman tunnel->stats.tx_packets++; 962fd558d18SJames Chapman tunnel->stats.tx_bytes += len; 963fd558d18SJames Chapman session->stats.tx_packets++; 964fd558d18SJames Chapman session->stats.tx_bytes += len; 965fd558d18SJames Chapman } else { 966fd558d18SJames Chapman tunnel->stats.tx_errors++; 967fd558d18SJames Chapman session->stats.tx_errors++; 968fd558d18SJames Chapman } 969fd558d18SJames Chapman 970fd558d18SJames Chapman return 0; 971fd558d18SJames Chapman } 972fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_core); 973fd558d18SJames Chapman 974fd558d18SJames Chapman /* Automatically called when the skb is freed. 975fd558d18SJames Chapman */ 976fd558d18SJames Chapman static void l2tp_sock_wfree(struct sk_buff *skb) 977fd558d18SJames Chapman { 978fd558d18SJames Chapman sock_put(skb->sk); 979fd558d18SJames Chapman } 980fd558d18SJames Chapman 981fd558d18SJames Chapman /* For data skbs that we transmit, we associate with the tunnel socket 982fd558d18SJames Chapman * but don't do accounting. 983fd558d18SJames Chapman */ 984fd558d18SJames Chapman static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk) 985fd558d18SJames Chapman { 986fd558d18SJames Chapman sock_hold(sk); 987fd558d18SJames Chapman skb->sk = sk; 988fd558d18SJames Chapman skb->destructor = l2tp_sock_wfree; 989fd558d18SJames Chapman } 990fd558d18SJames Chapman 991fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 992fd558d18SJames Chapman * inserted in the skb data before calling this function. 993fd558d18SJames Chapman */ 994fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 995fd558d18SJames Chapman { 996fd558d18SJames Chapman int data_len = skb->len; 9970d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 9980d76751fSJames Chapman struct sock *sk = tunnel->sock; 999fd558d18SJames Chapman struct udphdr *uh; 1000fd558d18SJames Chapman struct inet_sock *inet; 1001fd558d18SJames Chapman __wsum csum; 1002fd558d18SJames Chapman int old_headroom; 1003fd558d18SJames Chapman int new_headroom; 1004fd558d18SJames Chapman int headroom; 10050d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 10060d76751fSJames Chapman int udp_len; 1007fd558d18SJames Chapman 1008fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1009fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1010fd558d18SJames Chapman * make room. Adjust truesize. 1011fd558d18SJames Chapman */ 1012fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 10130d76751fSJames Chapman uhlen + hdr_len; 1014fd558d18SJames Chapman old_headroom = skb_headroom(skb); 1015fd558d18SJames Chapman if (skb_cow_head(skb, headroom)) 1016fd558d18SJames Chapman goto abort; 1017fd558d18SJames Chapman 1018fd558d18SJames Chapman new_headroom = skb_headroom(skb); 1019fd558d18SJames Chapman skb_orphan(skb); 1020fd558d18SJames Chapman skb->truesize += new_headroom - old_headroom; 1021fd558d18SJames Chapman 1022fd558d18SJames Chapman /* Setup L2TP header */ 1023f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1024fd558d18SJames Chapman 10250d76751fSJames Chapman /* Reset skb netfilter state */ 1026fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1027fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1028fd558d18SJames Chapman IPSKB_REROUTED); 1029fd558d18SJames Chapman nf_reset(skb); 1030fd558d18SJames Chapman 1031fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1032fd558d18SJames Chapman skb_dst_drop(skb); 1033fd558d18SJames Chapman skb_dst_set(skb, dst_clone(__sk_dst_get(sk))); 10340d76751fSJames Chapman 10350d76751fSJames Chapman switch (tunnel->encap) { 10360d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 10370d76751fSJames Chapman /* Setup UDP header */ 10380d76751fSJames Chapman inet = inet_sk(sk); 10390d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 10400d76751fSJames Chapman skb_reset_transport_header(skb); 10410d76751fSJames Chapman uh = udp_hdr(skb); 10420d76751fSJames Chapman uh->source = inet->inet_sport; 10430d76751fSJames Chapman uh->dest = inet->inet_dport; 10440d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 10450d76751fSJames Chapman uh->len = htons(udp_len); 10460d76751fSJames Chapman uh->check = 0; 1047fd558d18SJames Chapman 1048fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1049fd558d18SJames Chapman if (sk->sk_no_check == UDP_CSUM_NOXMIT) 1050fd558d18SJames Chapman skb->ip_summed = CHECKSUM_NONE; 1051fd558d18SJames Chapman else if ((skb_dst(skb) && skb_dst(skb)->dev) && 1052fd558d18SJames Chapman (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) { 1053fd558d18SJames Chapman skb->ip_summed = CHECKSUM_COMPLETE; 1054fd558d18SJames Chapman csum = skb_checksum(skb, 0, udp_len, 0); 1055fd558d18SJames Chapman uh->check = csum_tcpudp_magic(inet->inet_saddr, 1056fd558d18SJames Chapman inet->inet_daddr, 1057fd558d18SJames Chapman udp_len, IPPROTO_UDP, csum); 1058fd558d18SJames Chapman if (uh->check == 0) 1059fd558d18SJames Chapman uh->check = CSUM_MANGLED_0; 1060fd558d18SJames Chapman } else { 1061fd558d18SJames Chapman skb->ip_summed = CHECKSUM_PARTIAL; 1062fd558d18SJames Chapman skb->csum_start = skb_transport_header(skb) - skb->head; 1063fd558d18SJames Chapman skb->csum_offset = offsetof(struct udphdr, check); 1064fd558d18SJames Chapman uh->check = ~csum_tcpudp_magic(inet->inet_saddr, 1065fd558d18SJames Chapman inet->inet_daddr, 1066fd558d18SJames Chapman udp_len, IPPROTO_UDP, 0); 1067fd558d18SJames Chapman } 10680d76751fSJames Chapman break; 10690d76751fSJames Chapman 10700d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 10710d76751fSJames Chapman break; 10720d76751fSJames Chapman } 10730d76751fSJames Chapman 10740d76751fSJames Chapman l2tp_skb_set_owner_w(skb, sk); 1075fd558d18SJames Chapman 1076fd558d18SJames Chapman l2tp_xmit_core(session, skb, data_len); 1077fd558d18SJames Chapman 1078fd558d18SJames Chapman abort: 1079fd558d18SJames Chapman return 0; 1080fd558d18SJames Chapman } 1081fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1082fd558d18SJames Chapman 1083fd558d18SJames Chapman /***************************************************************************** 1084fd558d18SJames Chapman * Tinnel and session create/destroy. 1085fd558d18SJames Chapman *****************************************************************************/ 1086fd558d18SJames Chapman 1087fd558d18SJames Chapman /* Tunnel socket destruct hook. 1088fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1089fd558d18SJames Chapman * closed. 1090fd558d18SJames Chapman */ 1091fd558d18SJames Chapman void l2tp_tunnel_destruct(struct sock *sk) 1092fd558d18SJames Chapman { 1093fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1094fd558d18SJames Chapman 1095fd558d18SJames Chapman tunnel = sk->sk_user_data; 1096fd558d18SJames Chapman if (tunnel == NULL) 1097fd558d18SJames Chapman goto end; 1098fd558d18SJames Chapman 1099fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1100fd558d18SJames Chapman "%s: closing...\n", tunnel->name); 1101fd558d18SJames Chapman 1102fd558d18SJames Chapman /* Close all sessions */ 1103fd558d18SJames Chapman l2tp_tunnel_closeall(tunnel); 1104fd558d18SJames Chapman 11050d76751fSJames Chapman switch (tunnel->encap) { 11060d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1107fd558d18SJames Chapman /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1108fd558d18SJames Chapman (udp_sk(sk))->encap_type = 0; 1109fd558d18SJames Chapman (udp_sk(sk))->encap_rcv = NULL; 11100d76751fSJames Chapman break; 11110d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 11120d76751fSJames Chapman break; 11130d76751fSJames Chapman } 1114fd558d18SJames Chapman 1115fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1116fd558d18SJames Chapman tunnel->sock = NULL; 1117fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1118fd558d18SJames Chapman sk->sk_user_data = NULL; 1119fd558d18SJames Chapman 1120fd558d18SJames Chapman /* Call the original destructor */ 1121fd558d18SJames Chapman if (sk->sk_destruct) 1122fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1123fd558d18SJames Chapman 1124fd558d18SJames Chapman /* We're finished with the socket */ 1125fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1126fd558d18SJames Chapman 1127fd558d18SJames Chapman end: 1128fd558d18SJames Chapman return; 1129fd558d18SJames Chapman } 1130fd558d18SJames Chapman EXPORT_SYMBOL(l2tp_tunnel_destruct); 1131fd558d18SJames Chapman 1132fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1133fd558d18SJames Chapman */ 1134fd558d18SJames Chapman void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1135fd558d18SJames Chapman { 1136fd558d18SJames Chapman int hash; 1137fd558d18SJames Chapman struct hlist_node *walk; 1138fd558d18SJames Chapman struct hlist_node *tmp; 1139fd558d18SJames Chapman struct l2tp_session *session; 1140fd558d18SJames Chapman 1141fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1142fd558d18SJames Chapman 1143fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1144fd558d18SJames Chapman "%s: closing all sessions...\n", tunnel->name); 1145fd558d18SJames Chapman 1146fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1147fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1148fd558d18SJames Chapman again: 1149fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1150fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1151fd558d18SJames Chapman 1152fd558d18SJames Chapman PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO, 1153fd558d18SJames Chapman "%s: closing session\n", session->name); 1154fd558d18SJames Chapman 1155fd558d18SJames Chapman hlist_del_init(&session->hlist); 1156fd558d18SJames Chapman 1157fd558d18SJames Chapman /* Since we should hold the sock lock while 1158fd558d18SJames Chapman * doing any unbinding, we need to release the 1159fd558d18SJames Chapman * lock we're holding before taking that lock. 1160fd558d18SJames Chapman * Hold a reference to the sock so it doesn't 1161fd558d18SJames Chapman * disappear as we're jumping between locks. 1162fd558d18SJames Chapman */ 1163fd558d18SJames Chapman if (session->ref != NULL) 1164fd558d18SJames Chapman (*session->ref)(session); 1165fd558d18SJames Chapman 1166fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1167fd558d18SJames Chapman 1168f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1169f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1170f7faffa3SJames Chapman 1171e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1172e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1173e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1174e02d494dSJames Chapman synchronize_rcu(); 1175f7faffa3SJames Chapman } 1176f7faffa3SJames Chapman 1177fd558d18SJames Chapman if (session->session_close != NULL) 1178fd558d18SJames Chapman (*session->session_close)(session); 1179fd558d18SJames Chapman 1180fd558d18SJames Chapman if (session->deref != NULL) 1181fd558d18SJames Chapman (*session->deref)(session); 1182fd558d18SJames Chapman 1183fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1184fd558d18SJames Chapman 1185fd558d18SJames Chapman /* Now restart from the beginning of this hash 1186fd558d18SJames Chapman * chain. We always remove a session from the 1187fd558d18SJames Chapman * list so we are guaranteed to make forward 1188fd558d18SJames Chapman * progress. 1189fd558d18SJames Chapman */ 1190fd558d18SJames Chapman goto again; 1191fd558d18SJames Chapman } 1192fd558d18SJames Chapman } 1193fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1194fd558d18SJames Chapman } 1195fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); 1196fd558d18SJames Chapman 1197fd558d18SJames Chapman /* Really kill the tunnel. 1198fd558d18SJames Chapman * Come here only when all sessions have been cleared from the tunnel. 1199fd558d18SJames Chapman */ 1200fd558d18SJames Chapman void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) 1201fd558d18SJames Chapman { 1202fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1203fd558d18SJames Chapman 1204fd558d18SJames Chapman BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1205fd558d18SJames Chapman BUG_ON(tunnel->sock != NULL); 1206fd558d18SJames Chapman 1207fd558d18SJames Chapman PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO, 1208fd558d18SJames Chapman "%s: free...\n", tunnel->name); 1209fd558d18SJames Chapman 1210fd558d18SJames Chapman /* Remove from tunnel list */ 1211e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1212e02d494dSJames Chapman list_del_rcu(&tunnel->list); 1213e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1214e02d494dSJames Chapman synchronize_rcu(); 1215fd558d18SJames Chapman 1216fd558d18SJames Chapman atomic_dec(&l2tp_tunnel_count); 1217fd558d18SJames Chapman kfree(tunnel); 1218fd558d18SJames Chapman } 1219fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_free); 1220fd558d18SJames Chapman 1221789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1222789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1223789a4a2cSJames Chapman * managing L2TP daemon. 1224789a4a2cSJames Chapman */ 1225789a4a2cSJames Chapman static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp) 1226789a4a2cSJames Chapman { 1227789a4a2cSJames Chapman int err = -EINVAL; 1228789a4a2cSJames Chapman struct sockaddr_in udp_addr; 1229789a4a2cSJames Chapman struct sockaddr_l2tpip ip_addr; 12307bddd0dbSEric Dumazet struct socket *sock = NULL; 1231789a4a2cSJames Chapman 1232789a4a2cSJames Chapman switch (cfg->encap) { 1233789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 1234789a4a2cSJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp); 1235789a4a2cSJames Chapman if (err < 0) 1236789a4a2cSJames Chapman goto out; 1237789a4a2cSJames Chapman 1238789a4a2cSJames Chapman sock = *sockp; 1239789a4a2cSJames Chapman 1240789a4a2cSJames Chapman memset(&udp_addr, 0, sizeof(udp_addr)); 1241789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1242789a4a2cSJames Chapman udp_addr.sin_addr = cfg->local_ip; 1243789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->local_udp_port); 1244789a4a2cSJames Chapman err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr)); 1245789a4a2cSJames Chapman if (err < 0) 1246789a4a2cSJames Chapman goto out; 1247789a4a2cSJames Chapman 1248789a4a2cSJames Chapman udp_addr.sin_family = AF_INET; 1249789a4a2cSJames Chapman udp_addr.sin_addr = cfg->peer_ip; 1250789a4a2cSJames Chapman udp_addr.sin_port = htons(cfg->peer_udp_port); 1251789a4a2cSJames Chapman err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0); 1252789a4a2cSJames Chapman if (err < 0) 1253789a4a2cSJames Chapman goto out; 1254789a4a2cSJames Chapman 1255789a4a2cSJames Chapman if (!cfg->use_udp_checksums) 1256789a4a2cSJames Chapman sock->sk->sk_no_check = UDP_CSUM_NOXMIT; 1257789a4a2cSJames Chapman 1258789a4a2cSJames Chapman break; 1259789a4a2cSJames Chapman 1260789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1261789a4a2cSJames Chapman err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp); 1262789a4a2cSJames Chapman if (err < 0) 1263789a4a2cSJames Chapman goto out; 1264789a4a2cSJames Chapman 1265789a4a2cSJames Chapman sock = *sockp; 1266789a4a2cSJames Chapman 1267789a4a2cSJames Chapman memset(&ip_addr, 0, sizeof(ip_addr)); 1268789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1269789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1270789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 1271789a4a2cSJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr)); 1272789a4a2cSJames Chapman if (err < 0) 1273789a4a2cSJames Chapman goto out; 1274789a4a2cSJames Chapman 1275789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1276789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1277789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 1278789a4a2cSJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0); 1279789a4a2cSJames Chapman if (err < 0) 1280789a4a2cSJames Chapman goto out; 1281789a4a2cSJames Chapman 1282789a4a2cSJames Chapman break; 1283789a4a2cSJames Chapman 1284789a4a2cSJames Chapman default: 1285789a4a2cSJames Chapman goto out; 1286789a4a2cSJames Chapman } 1287789a4a2cSJames Chapman 1288789a4a2cSJames Chapman out: 1289789a4a2cSJames Chapman if ((err < 0) && sock) { 1290789a4a2cSJames Chapman sock_release(sock); 1291789a4a2cSJames Chapman *sockp = NULL; 1292789a4a2cSJames Chapman } 1293789a4a2cSJames Chapman 1294789a4a2cSJames Chapman return err; 1295789a4a2cSJames Chapman } 1296789a4a2cSJames Chapman 1297fd558d18SJames 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) 1298fd558d18SJames Chapman { 1299fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1300fd558d18SJames Chapman int err; 1301fd558d18SJames Chapman struct socket *sock = NULL; 1302fd558d18SJames Chapman struct sock *sk = NULL; 1303fd558d18SJames Chapman struct l2tp_net *pn; 13040d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1305fd558d18SJames Chapman 1306fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1307789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1308789a4a2cSJames Chapman * kernel socket. 1309fd558d18SJames Chapman */ 1310789a4a2cSJames Chapman if (fd < 0) { 1311789a4a2cSJames Chapman err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock); 1312789a4a2cSJames Chapman if (err < 0) 1313789a4a2cSJames Chapman goto err; 1314789a4a2cSJames Chapman } else { 1315fd558d18SJames Chapman err = -EBADF; 1316fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1317fd558d18SJames Chapman if (!sock) { 1318fd558d18SJames Chapman printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n", 1319fd558d18SJames Chapman tunnel_id, fd, err); 1320fd558d18SJames Chapman goto err; 1321fd558d18SJames Chapman } 1322789a4a2cSJames Chapman } 1323fd558d18SJames Chapman 1324fd558d18SJames Chapman sk = sock->sk; 1325fd558d18SJames Chapman 13260d76751fSJames Chapman if (cfg != NULL) 13270d76751fSJames Chapman encap = cfg->encap; 13280d76751fSJames Chapman 1329fd558d18SJames Chapman /* Quick sanity checks */ 13300d76751fSJames Chapman switch (encap) { 13310d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1332fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1333fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1334fd558d18SJames Chapman printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1335fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1336fd558d18SJames Chapman goto err; 1337fd558d18SJames Chapman } 13380d76751fSJames Chapman break; 13390d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 13400d76751fSJames Chapman err = -EPROTONOSUPPORT; 13410d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 13420d76751fSJames Chapman printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 13430d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1344fd558d18SJames Chapman goto err; 1345fd558d18SJames Chapman } 13460d76751fSJames Chapman break; 13470d76751fSJames Chapman } 1348fd558d18SJames Chapman 1349fd558d18SJames Chapman /* Check if this socket has already been prepped */ 1350fd558d18SJames Chapman tunnel = (struct l2tp_tunnel *)sk->sk_user_data; 1351fd558d18SJames Chapman if (tunnel != NULL) { 1352fd558d18SJames Chapman /* This socket has already been prepped */ 1353fd558d18SJames Chapman err = -EBUSY; 1354fd558d18SJames Chapman goto err; 1355fd558d18SJames Chapman } 1356fd558d18SJames Chapman 1357fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1358fd558d18SJames Chapman if (tunnel == NULL) { 1359fd558d18SJames Chapman err = -ENOMEM; 1360fd558d18SJames Chapman goto err; 1361fd558d18SJames Chapman } 1362fd558d18SJames Chapman 1363fd558d18SJames Chapman tunnel->version = version; 1364fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1365fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1366fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1367fd558d18SJames Chapman 1368fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1369fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1370fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1371fd558d18SJames Chapman 1372fd558d18SJames Chapman /* The net we belong to */ 1373fd558d18SJames Chapman tunnel->l2tp_net = net; 1374fd558d18SJames Chapman pn = l2tp_pernet(net); 1375fd558d18SJames Chapman 13760d76751fSJames Chapman if (cfg != NULL) 1377fd558d18SJames Chapman tunnel->debug = cfg->debug; 1378fd558d18SJames Chapman 1379fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 13800d76751fSJames Chapman tunnel->encap = encap; 13810d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 13820d76751fSJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1383fd558d18SJames Chapman udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP; 1384fd558d18SJames Chapman udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv; 13850d76751fSJames Chapman } 1386fd558d18SJames Chapman 1387fd558d18SJames Chapman sk->sk_user_data = tunnel; 1388fd558d18SJames Chapman 1389fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1390fd558d18SJames Chapman * if the tunnel socket goes away. 1391fd558d18SJames Chapman */ 1392fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1393fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1394fd558d18SJames Chapman tunnel->sock = sk; 1395fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1396fd558d18SJames Chapman 1397fd558d18SJames Chapman /* Add tunnel to our list */ 1398fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1399e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1400e02d494dSJames Chapman list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 1401e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1402e02d494dSJames Chapman synchronize_rcu(); 1403fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1404fd558d18SJames Chapman 1405fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 1406fd558d18SJames Chapman * only when this drops to zero. 1407fd558d18SJames Chapman */ 1408fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1409fd558d18SJames Chapman 1410fd558d18SJames Chapman err = 0; 1411fd558d18SJames Chapman err: 1412fd558d18SJames Chapman if (tunnelp) 1413fd558d18SJames Chapman *tunnelp = tunnel; 1414fd558d18SJames Chapman 1415789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1416789a4a2cSJames Chapman * have a file. 1417789a4a2cSJames Chapman */ 1418789a4a2cSJames Chapman if (sock && sock->file) 1419fd558d18SJames Chapman sockfd_put(sock); 1420fd558d18SJames Chapman 1421fd558d18SJames Chapman return err; 1422fd558d18SJames Chapman } 1423fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1424fd558d18SJames Chapman 1425309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1426309795f4SJames Chapman */ 1427309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1428309795f4SJames Chapman { 1429309795f4SJames Chapman int err = 0; 1430789a4a2cSJames Chapman struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL; 1431309795f4SJames Chapman 1432309795f4SJames Chapman /* Force the tunnel socket to close. This will eventually 1433309795f4SJames Chapman * cause the tunnel to be deleted via the normal socket close 1434309795f4SJames Chapman * mechanisms when userspace closes the tunnel socket. 1435309795f4SJames Chapman */ 1436789a4a2cSJames Chapman if (sock != NULL) { 1437789a4a2cSJames Chapman err = inet_shutdown(sock, 2); 1438789a4a2cSJames Chapman 1439789a4a2cSJames Chapman /* If the tunnel's socket was created by the kernel, 1440789a4a2cSJames Chapman * close the socket here since the socket was not 1441789a4a2cSJames Chapman * created by userspace. 1442789a4a2cSJames Chapman */ 1443789a4a2cSJames Chapman if (sock->file == NULL) 1444789a4a2cSJames Chapman err = inet_release(sock); 1445789a4a2cSJames Chapman } 1446309795f4SJames Chapman 1447309795f4SJames Chapman return err; 1448309795f4SJames Chapman } 1449309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1450309795f4SJames Chapman 1451fd558d18SJames Chapman /* Really kill the session. 1452fd558d18SJames Chapman */ 1453fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1454fd558d18SJames Chapman { 1455fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1456fd558d18SJames Chapman 1457fd558d18SJames Chapman BUG_ON(atomic_read(&session->ref_count) != 0); 1458fd558d18SJames Chapman 1459fd558d18SJames Chapman tunnel = session->tunnel; 1460fd558d18SJames Chapman if (tunnel != NULL) { 1461fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1462fd558d18SJames Chapman 1463fd558d18SJames Chapman /* Delete the session from the hash */ 1464fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1465fd558d18SJames Chapman hlist_del_init(&session->hlist); 1466fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1467fd558d18SJames Chapman 1468f7faffa3SJames Chapman /* Unlink from the global hash if not L2TPv2 */ 1469f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1470f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1471f7faffa3SJames Chapman 1472e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1473e02d494dSJames Chapman hlist_del_init_rcu(&session->global_hlist); 1474e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1475e02d494dSJames Chapman synchronize_rcu(); 1476f7faffa3SJames Chapman } 1477f7faffa3SJames Chapman 1478fd558d18SJames Chapman if (session->session_id != 0) 1479fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1480fd558d18SJames Chapman 1481fd558d18SJames Chapman sock_put(tunnel->sock); 1482fd558d18SJames Chapman 1483fd558d18SJames Chapman /* This will delete the tunnel context if this 1484fd558d18SJames Chapman * is the last session on the tunnel. 1485fd558d18SJames Chapman */ 1486fd558d18SJames Chapman session->tunnel = NULL; 1487fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1488fd558d18SJames Chapman } 1489fd558d18SJames Chapman 1490fd558d18SJames Chapman kfree(session); 1491fd558d18SJames Chapman 1492fd558d18SJames Chapman return; 1493fd558d18SJames Chapman } 1494fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1495fd558d18SJames Chapman 1496309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1497309795f4SJames Chapman pseudowire modules. 1498309795f4SJames Chapman */ 1499309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1500309795f4SJames Chapman { 1501309795f4SJames Chapman if (session->session_close != NULL) 1502309795f4SJames Chapman (*session->session_close)(session); 1503309795f4SJames Chapman 1504309795f4SJames Chapman l2tp_session_dec_refcount(session); 1505309795f4SJames Chapman 1506309795f4SJames Chapman return 0; 1507309795f4SJames Chapman } 1508309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1509309795f4SJames Chapman 1510309795f4SJames Chapman 1511f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1512f7faffa3SJames Chapman * l2specific_len parameters are set. 1513f7faffa3SJames Chapman */ 1514f7faffa3SJames Chapman void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1515f7faffa3SJames Chapman { 1516f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1517f7faffa3SJames Chapman session->hdr_len = 6; 1518f7faffa3SJames Chapman if (session->send_seq) 1519f7faffa3SJames Chapman session->hdr_len += 4; 1520f7faffa3SJames Chapman } else { 15210d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 15220d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 15230d76751fSJames Chapman session->hdr_len += 4; 1524f7faffa3SJames Chapman } 1525f7faffa3SJames Chapman 1526f7faffa3SJames Chapman } 1527f7faffa3SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1528f7faffa3SJames Chapman 1529fd558d18SJames 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) 1530fd558d18SJames Chapman { 1531fd558d18SJames Chapman struct l2tp_session *session; 1532fd558d18SJames Chapman 1533fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1534fd558d18SJames Chapman if (session != NULL) { 1535fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1536fd558d18SJames Chapman session->tunnel = tunnel; 1537fd558d18SJames Chapman 1538fd558d18SJames Chapman session->session_id = session_id; 1539fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1540f7faffa3SJames Chapman session->nr = 1; 1541fd558d18SJames Chapman 1542fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1543fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1544fd558d18SJames Chapman 1545fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1546fd558d18SJames Chapman 1547fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1548f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1549fd558d18SJames Chapman 1550fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1551fd558d18SJames Chapman session->debug = tunnel->debug; 1552fd558d18SJames Chapman 1553fd558d18SJames Chapman if (cfg) { 1554f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1555fd558d18SJames Chapman session->debug = cfg->debug; 1556fd558d18SJames Chapman session->mtu = cfg->mtu; 1557fd558d18SJames Chapman session->mru = cfg->mru; 1558fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1559fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1560fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1561f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1562f7faffa3SJames Chapman session->offset = cfg->offset; 1563f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1564f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1565f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1566f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1567f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1568f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1569fd558d18SJames Chapman } 1570fd558d18SJames Chapman 1571f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1572f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1573f7faffa3SJames Chapman else 1574f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1575f7faffa3SJames Chapman 1576f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1577f7faffa3SJames Chapman 1578fd558d18SJames Chapman /* Bump the reference count. The session context is deleted 1579fd558d18SJames Chapman * only when this drops to zero. 1580fd558d18SJames Chapman */ 1581fd558d18SJames Chapman l2tp_session_inc_refcount(session); 1582fd558d18SJames Chapman l2tp_tunnel_inc_refcount(tunnel); 1583fd558d18SJames Chapman 1584fd558d18SJames Chapman /* Ensure tunnel socket isn't deleted */ 1585fd558d18SJames Chapman sock_hold(tunnel->sock); 1586fd558d18SJames Chapman 1587fd558d18SJames Chapman /* Add session to the tunnel's hash list */ 1588fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1589fd558d18SJames Chapman hlist_add_head(&session->hlist, 1590fd558d18SJames Chapman l2tp_session_id_hash(tunnel, session_id)); 1591fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1592fd558d18SJames Chapman 1593f7faffa3SJames Chapman /* And to the global session list if L2TPv3 */ 1594f7faffa3SJames Chapman if (tunnel->version != L2TP_HDR_VER_2) { 1595f7faffa3SJames Chapman struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1596f7faffa3SJames Chapman 1597e02d494dSJames Chapman spin_lock_bh(&pn->l2tp_session_hlist_lock); 1598e02d494dSJames Chapman hlist_add_head_rcu(&session->global_hlist, 1599f7faffa3SJames Chapman l2tp_session_id_hash_2(pn, session_id)); 1600e02d494dSJames Chapman spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1601e02d494dSJames Chapman synchronize_rcu(); 1602f7faffa3SJames Chapman } 1603f7faffa3SJames Chapman 1604fd558d18SJames Chapman /* Ignore management session in session count value */ 1605fd558d18SJames Chapman if (session->session_id != 0) 1606fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1607fd558d18SJames Chapman } 1608fd558d18SJames Chapman 1609fd558d18SJames Chapman return session; 1610fd558d18SJames Chapman } 1611fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1612fd558d18SJames Chapman 1613fd558d18SJames Chapman /***************************************************************************** 1614fd558d18SJames Chapman * Init and cleanup 1615fd558d18SJames Chapman *****************************************************************************/ 1616fd558d18SJames Chapman 1617fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1618fd558d18SJames Chapman { 1619fd558d18SJames Chapman struct l2tp_net *pn; 1620fd558d18SJames Chapman int err; 1621f7faffa3SJames Chapman int hash; 1622fd558d18SJames Chapman 1623fd558d18SJames Chapman pn = kzalloc(sizeof(*pn), GFP_KERNEL); 1624fd558d18SJames Chapman if (!pn) 1625fd558d18SJames Chapman return -ENOMEM; 1626fd558d18SJames Chapman 1627fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1628e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1629fd558d18SJames Chapman 1630f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1631f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1632f7faffa3SJames Chapman 1633e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1634f7faffa3SJames Chapman 1635fd558d18SJames Chapman err = net_assign_generic(net, l2tp_net_id, pn); 1636fd558d18SJames Chapman if (err) 1637fd558d18SJames Chapman goto out; 1638fd558d18SJames Chapman 1639fd558d18SJames Chapman return 0; 1640fd558d18SJames Chapman 1641fd558d18SJames Chapman out: 1642fd558d18SJames Chapman kfree(pn); 1643fd558d18SJames Chapman return err; 1644fd558d18SJames Chapman } 1645fd558d18SJames Chapman 1646fd558d18SJames Chapman static __net_exit void l2tp_exit_net(struct net *net) 1647fd558d18SJames Chapman { 1648fd558d18SJames Chapman struct l2tp_net *pn; 1649fd558d18SJames Chapman 1650fd558d18SJames Chapman pn = net_generic(net, l2tp_net_id); 1651fd558d18SJames Chapman /* 1652fd558d18SJames Chapman * if someone has cached our net then 1653fd558d18SJames Chapman * further net_generic call will return NULL 1654fd558d18SJames Chapman */ 1655fd558d18SJames Chapman net_assign_generic(net, l2tp_net_id, NULL); 1656fd558d18SJames Chapman kfree(pn); 1657fd558d18SJames Chapman } 1658fd558d18SJames Chapman 1659fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1660fd558d18SJames Chapman .init = l2tp_init_net, 1661fd558d18SJames Chapman .exit = l2tp_exit_net, 1662fd558d18SJames Chapman .id = &l2tp_net_id, 1663fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1664fd558d18SJames Chapman }; 1665fd558d18SJames Chapman 1666fd558d18SJames Chapman static int __init l2tp_init(void) 1667fd558d18SJames Chapman { 1668fd558d18SJames Chapman int rc = 0; 1669fd558d18SJames Chapman 1670fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1671fd558d18SJames Chapman if (rc) 1672fd558d18SJames Chapman goto out; 1673fd558d18SJames Chapman 1674fd558d18SJames Chapman printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION); 1675fd558d18SJames Chapman 1676fd558d18SJames Chapman out: 1677fd558d18SJames Chapman return rc; 1678fd558d18SJames Chapman } 1679fd558d18SJames Chapman 1680fd558d18SJames Chapman static void __exit l2tp_exit(void) 1681fd558d18SJames Chapman { 1682fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1683fd558d18SJames Chapman } 1684fd558d18SJames Chapman 1685fd558d18SJames Chapman module_init(l2tp_init); 1686fd558d18SJames Chapman module_exit(l2tp_exit); 1687fd558d18SJames Chapman 1688fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1689fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1690fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1691fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1692fd558d18SJames Chapman 1693