1fd558d18SJames Chapman /* 2fd558d18SJames Chapman * L2TP core. 3fd558d18SJames Chapman * 4fd558d18SJames Chapman * Copyright (c) 2008,2009,2010 Katalix Systems Ltd 5fd558d18SJames Chapman * 6fd558d18SJames Chapman * This file contains some code of the original L2TPv2 pppol2tp 7fd558d18SJames Chapman * driver, which has the following copyright: 8fd558d18SJames Chapman * 9fd558d18SJames Chapman * Authors: Martijn van Oosterhout <kleptog@svana.org> 10fd558d18SJames Chapman * James Chapman (jchapman@katalix.com) 11fd558d18SJames Chapman * Contributors: 12fd558d18SJames Chapman * Michal Ostrowski <mostrows@speakeasy.net> 13fd558d18SJames Chapman * Arnaldo Carvalho de Melo <acme@xconectiva.com.br> 14fd558d18SJames Chapman * David S. Miller (davem@redhat.com) 15fd558d18SJames Chapman * 16fd558d18SJames Chapman * This program is free software; you can redistribute it and/or modify 17fd558d18SJames Chapman * it under the terms of the GNU General Public License version 2 as 18fd558d18SJames Chapman * published by the Free Software Foundation. 19fd558d18SJames Chapman */ 20fd558d18SJames Chapman 21a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22a4ca44faSJoe Perches 23fd558d18SJames Chapman #include <linux/module.h> 24fd558d18SJames Chapman #include <linux/string.h> 25fd558d18SJames Chapman #include <linux/list.h> 26e02d494dSJames Chapman #include <linux/rculist.h> 27fd558d18SJames Chapman #include <linux/uaccess.h> 28fd558d18SJames Chapman 29fd558d18SJames Chapman #include <linux/kernel.h> 30fd558d18SJames Chapman #include <linux/spinlock.h> 31fd558d18SJames Chapman #include <linux/kthread.h> 32fd558d18SJames Chapman #include <linux/sched.h> 33fd558d18SJames Chapman #include <linux/slab.h> 34fd558d18SJames Chapman #include <linux/errno.h> 35fd558d18SJames Chapman #include <linux/jiffies.h> 36fd558d18SJames Chapman 37fd558d18SJames Chapman #include <linux/netdevice.h> 38fd558d18SJames Chapman #include <linux/net.h> 39fd558d18SJames Chapman #include <linux/inetdevice.h> 40fd558d18SJames Chapman #include <linux/skbuff.h> 41fd558d18SJames Chapman #include <linux/init.h> 420d76751fSJames Chapman #include <linux/in.h> 43fd558d18SJames Chapman #include <linux/ip.h> 44fd558d18SJames Chapman #include <linux/udp.h> 450d76751fSJames Chapman #include <linux/l2tp.h> 46fd558d18SJames Chapman #include <linux/hash.h> 47fd558d18SJames Chapman #include <linux/sort.h> 48fd558d18SJames Chapman #include <linux/file.h> 49fd558d18SJames Chapman #include <linux/nsproxy.h> 50fd558d18SJames Chapman #include <net/net_namespace.h> 51fd558d18SJames Chapman #include <net/netns/generic.h> 52fd558d18SJames Chapman #include <net/dst.h> 53fd558d18SJames Chapman #include <net/ip.h> 54fd558d18SJames Chapman #include <net/udp.h> 5585644b4dSTom Herbert #include <net/udp_tunnel.h> 56309795f4SJames Chapman #include <net/inet_common.h> 57fd558d18SJames Chapman #include <net/xfrm.h> 580d76751fSJames Chapman #include <net/protocol.h> 59d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h> 60d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h> 61d2cf3361SBenjamin LaHaise #include <net/ip6_route.h> 62d499bd2eSDavid S. Miller #include <net/ip6_checksum.h> 63fd558d18SJames Chapman 64fd558d18SJames Chapman #include <asm/byteorder.h> 6560063497SArun Sharma #include <linux/atomic.h> 66fd558d18SJames Chapman 67fd558d18SJames Chapman #include "l2tp_core.h" 68fd558d18SJames Chapman 69fd558d18SJames Chapman #define L2TP_DRV_VERSION "V2.0" 70fd558d18SJames Chapman 71fd558d18SJames Chapman /* L2TP header constants */ 72fd558d18SJames Chapman #define L2TP_HDRFLAG_T 0x8000 73fd558d18SJames Chapman #define L2TP_HDRFLAG_L 0x4000 74fd558d18SJames Chapman #define L2TP_HDRFLAG_S 0x0800 75fd558d18SJames Chapman #define L2TP_HDRFLAG_O 0x0200 76fd558d18SJames Chapman #define L2TP_HDRFLAG_P 0x0100 77fd558d18SJames Chapman 78fd558d18SJames Chapman #define L2TP_HDR_VER_MASK 0x000F 79fd558d18SJames Chapman #define L2TP_HDR_VER_2 0x0002 80f7faffa3SJames Chapman #define L2TP_HDR_VER_3 0x0003 81fd558d18SJames Chapman 82fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */ 83fd558d18SJames Chapman #define L2TP_SLFLAG_S 0x40000000 84fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK 0x00ffffff 85fd558d18SJames Chapman 86fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ 10 87fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ 6 88fd558d18SJames Chapman 89fd558d18SJames Chapman /* Default trace flags */ 90fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS 0 91fd558d18SJames Chapman 92fd558d18SJames Chapman /* Private data stored for received packets in the skb. 93fd558d18SJames Chapman */ 94fd558d18SJames Chapman struct l2tp_skb_cb { 95f7faffa3SJames Chapman u32 ns; 96fd558d18SJames Chapman u16 has_seq; 97fd558d18SJames Chapman u16 length; 98fd558d18SJames Chapman unsigned long expires; 99fd558d18SJames Chapman }; 100fd558d18SJames Chapman 101fd558d18SJames Chapman #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 102fd558d18SJames Chapman 103fd558d18SJames Chapman static atomic_t l2tp_tunnel_count; 104fd558d18SJames Chapman static atomic_t l2tp_session_count; 105f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq; 106fd558d18SJames Chapman 107fd558d18SJames Chapman /* per-net private data for this module */ 108fd558d18SJames Chapman static unsigned int l2tp_net_id; 109fd558d18SJames Chapman struct l2tp_net { 110fd558d18SJames Chapman struct list_head l2tp_tunnel_list; 111e02d494dSJames Chapman spinlock_t l2tp_tunnel_list_lock; 112f7faffa3SJames Chapman struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 113e02d494dSJames Chapman spinlock_t l2tp_session_hlist_lock; 114fd558d18SJames Chapman }; 115fd558d18SJames Chapman 116fc130840Sstephen hemminger 1178d8a51e2SDavid S. Miller static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk) 1188d8a51e2SDavid S. Miller { 1198d8a51e2SDavid S. Miller return sk->sk_user_data; 1208d8a51e2SDavid S. Miller } 1218d8a51e2SDavid S. Miller 1229aaef50cSGuillaume Nault static inline struct l2tp_net *l2tp_pernet(const 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 129f7faffa3SJames Chapman /* Session hash global list for L2TPv3. 130f7faffa3SJames Chapman * The session_id SHOULD be random according to RFC3931, but several 131f7faffa3SJames Chapman * L2TP implementations use incrementing session_ids. So we do a real 132f7faffa3SJames Chapman * hash on the session_id, rather than a simple bitmask. 133f7faffa3SJames Chapman */ 134f7faffa3SJames Chapman static inline struct hlist_head * 135f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 136f7faffa3SJames Chapman { 137f7faffa3SJames Chapman return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 138f7faffa3SJames Chapman 139f7faffa3SJames Chapman } 140f7faffa3SJames Chapman 14180d84ef3STom Parkin /* Lookup the tunnel socket, possibly involving the fs code if the socket is 14280d84ef3STom Parkin * owned by userspace. A struct sock returned from this function must be 14380d84ef3STom Parkin * released using l2tp_tunnel_sock_put once you're done with it. 14480d84ef3STom Parkin */ 145b5d2b285Sstephen hemminger static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel) 14680d84ef3STom Parkin { 14780d84ef3STom Parkin int err = 0; 14880d84ef3STom Parkin struct socket *sock = NULL; 14980d84ef3STom Parkin struct sock *sk = NULL; 15080d84ef3STom Parkin 15180d84ef3STom Parkin if (!tunnel) 15280d84ef3STom Parkin goto out; 15380d84ef3STom Parkin 15480d84ef3STom Parkin if (tunnel->fd >= 0) { 15580d84ef3STom Parkin /* Socket is owned by userspace, who might be in the process 15680d84ef3STom Parkin * of closing it. Look the socket up using the fd to ensure 15780d84ef3STom Parkin * consistency. 15880d84ef3STom Parkin */ 15980d84ef3STom Parkin sock = sockfd_lookup(tunnel->fd, &err); 16080d84ef3STom Parkin if (sock) 16180d84ef3STom Parkin sk = sock->sk; 16280d84ef3STom Parkin } else { 16380d84ef3STom Parkin /* Socket is owned by kernelspace */ 16480d84ef3STom Parkin sk = tunnel->sock; 1658abbbe8fSTom Parkin sock_hold(sk); 16680d84ef3STom Parkin } 16780d84ef3STom Parkin 16880d84ef3STom Parkin out: 16980d84ef3STom Parkin return sk; 17080d84ef3STom Parkin } 17180d84ef3STom Parkin 17280d84ef3STom Parkin /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */ 173b5d2b285Sstephen hemminger static void l2tp_tunnel_sock_put(struct sock *sk) 17480d84ef3STom Parkin { 17580d84ef3STom Parkin struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 17680d84ef3STom Parkin if (tunnel) { 17780d84ef3STom Parkin if (tunnel->fd >= 0) { 17880d84ef3STom Parkin /* Socket is owned by userspace */ 17980d84ef3STom Parkin sockfd_put(sk->sk_socket); 18080d84ef3STom Parkin } 18180d84ef3STom Parkin sock_put(sk); 18280d84ef3STom Parkin } 1838abbbe8fSTom Parkin sock_put(sk); 18480d84ef3STom Parkin } 18580d84ef3STom Parkin 186fd558d18SJames Chapman /* Session hash list. 187fd558d18SJames Chapman * The session_id SHOULD be random according to RFC2661, but several 188fd558d18SJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing 189fd558d18SJames Chapman * session_ids. So we do a real hash on the session_id, rather than a 190fd558d18SJames Chapman * simple bitmask. 191fd558d18SJames Chapman */ 192fd558d18SJames Chapman static inline struct hlist_head * 193fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 194fd558d18SJames Chapman { 195fd558d18SJames Chapman return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 196fd558d18SJames Chapman } 197fd558d18SJames Chapman 19854652eb1SGuillaume Nault /* Lookup a tunnel. A new reference is held on the returned tunnel. */ 19954652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id) 20054652eb1SGuillaume Nault { 20154652eb1SGuillaume Nault const struct l2tp_net *pn = l2tp_pernet(net); 20254652eb1SGuillaume Nault struct l2tp_tunnel *tunnel; 20354652eb1SGuillaume Nault 20454652eb1SGuillaume Nault rcu_read_lock_bh(); 20554652eb1SGuillaume Nault list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 20654652eb1SGuillaume Nault if (tunnel->tunnel_id == tunnel_id) { 20754652eb1SGuillaume Nault l2tp_tunnel_inc_refcount(tunnel); 20854652eb1SGuillaume Nault rcu_read_unlock_bh(); 20954652eb1SGuillaume Nault 21054652eb1SGuillaume Nault return tunnel; 21154652eb1SGuillaume Nault } 21254652eb1SGuillaume Nault } 21354652eb1SGuillaume Nault rcu_read_unlock_bh(); 21454652eb1SGuillaume Nault 21554652eb1SGuillaume Nault return NULL; 21654652eb1SGuillaume Nault } 21754652eb1SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get); 21854652eb1SGuillaume Nault 21955a3ce3bSGuillaume Nault /* Lookup a session. A new reference is held on the returned session. 22061b9a047SGuillaume Nault * Optionally calls session->ref() too if do_ref is true. 22161b9a047SGuillaume Nault */ 2229aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, 22361b9a047SGuillaume Nault struct l2tp_tunnel *tunnel, 22461b9a047SGuillaume Nault u32 session_id, bool do_ref) 22561b9a047SGuillaume Nault { 22661b9a047SGuillaume Nault struct hlist_head *session_list; 22761b9a047SGuillaume Nault struct l2tp_session *session; 22861b9a047SGuillaume Nault 22961b9a047SGuillaume Nault if (!tunnel) { 23061b9a047SGuillaume Nault struct l2tp_net *pn = l2tp_pernet(net); 23161b9a047SGuillaume Nault 23261b9a047SGuillaume Nault session_list = l2tp_session_id_hash_2(pn, session_id); 23361b9a047SGuillaume Nault 23461b9a047SGuillaume Nault rcu_read_lock_bh(); 23561b9a047SGuillaume Nault hlist_for_each_entry_rcu(session, session_list, global_hlist) { 23661b9a047SGuillaume Nault if (session->session_id == session_id) { 23761b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 23861b9a047SGuillaume Nault if (do_ref && session->ref) 23961b9a047SGuillaume Nault session->ref(session); 24061b9a047SGuillaume Nault rcu_read_unlock_bh(); 24161b9a047SGuillaume Nault 24261b9a047SGuillaume Nault return session; 24361b9a047SGuillaume Nault } 24461b9a047SGuillaume Nault } 24561b9a047SGuillaume Nault rcu_read_unlock_bh(); 24661b9a047SGuillaume Nault 24761b9a047SGuillaume Nault return NULL; 24861b9a047SGuillaume Nault } 24961b9a047SGuillaume Nault 25061b9a047SGuillaume Nault session_list = l2tp_session_id_hash(tunnel, session_id); 25161b9a047SGuillaume Nault read_lock_bh(&tunnel->hlist_lock); 25261b9a047SGuillaume Nault hlist_for_each_entry(session, session_list, hlist) { 25361b9a047SGuillaume Nault if (session->session_id == session_id) { 25461b9a047SGuillaume Nault l2tp_session_inc_refcount(session); 25561b9a047SGuillaume Nault if (do_ref && session->ref) 25661b9a047SGuillaume Nault session->ref(session); 25761b9a047SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 25861b9a047SGuillaume Nault 25961b9a047SGuillaume Nault return session; 26061b9a047SGuillaume Nault } 26161b9a047SGuillaume Nault } 26261b9a047SGuillaume Nault read_unlock_bh(&tunnel->hlist_lock); 26361b9a047SGuillaume Nault 26461b9a047SGuillaume Nault return NULL; 26561b9a047SGuillaume Nault } 26661b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get); 26761b9a047SGuillaume Nault 268e08293a4SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth, 269e08293a4SGuillaume Nault bool do_ref) 270fd558d18SJames Chapman { 271fd558d18SJames Chapman int hash; 272fd558d18SJames Chapman struct l2tp_session *session; 273fd558d18SJames Chapman int count = 0; 274fd558d18SJames Chapman 275fd558d18SJames Chapman read_lock_bh(&tunnel->hlist_lock); 276fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 277b67bfe0dSSasha Levin hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { 278fd558d18SJames Chapman if (++count > nth) { 279e08293a4SGuillaume Nault l2tp_session_inc_refcount(session); 280e08293a4SGuillaume Nault if (do_ref && session->ref) 281e08293a4SGuillaume Nault session->ref(session); 282fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 283fd558d18SJames Chapman return session; 284fd558d18SJames Chapman } 285fd558d18SJames Chapman } 286fd558d18SJames Chapman } 287fd558d18SJames Chapman 288fd558d18SJames Chapman read_unlock_bh(&tunnel->hlist_lock); 289fd558d18SJames Chapman 290fd558d18SJames Chapman return NULL; 291fd558d18SJames Chapman } 292e08293a4SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_nth); 293fd558d18SJames Chapman 294309795f4SJames Chapman /* Lookup a session by interface name. 295309795f4SJames Chapman * This is very inefficient but is only used by management interfaces. 296309795f4SJames Chapman */ 2979aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 2989aaef50cSGuillaume Nault const char *ifname, 2992777e2abSGuillaume Nault bool do_ref) 300309795f4SJames Chapman { 301309795f4SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 302309795f4SJames Chapman int hash; 303309795f4SJames Chapman struct l2tp_session *session; 304309795f4SJames Chapman 305e02d494dSJames Chapman rcu_read_lock_bh(); 306309795f4SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 307b67bfe0dSSasha Levin hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { 308309795f4SJames Chapman if (!strcmp(session->ifname, ifname)) { 3092777e2abSGuillaume Nault l2tp_session_inc_refcount(session); 3102777e2abSGuillaume Nault if (do_ref && session->ref) 3112777e2abSGuillaume Nault session->ref(session); 312e02d494dSJames Chapman rcu_read_unlock_bh(); 3132777e2abSGuillaume Nault 314309795f4SJames Chapman return session; 315309795f4SJames Chapman } 316309795f4SJames Chapman } 317309795f4SJames Chapman } 318309795f4SJames Chapman 319e02d494dSJames Chapman rcu_read_unlock_bh(); 320309795f4SJames Chapman 321309795f4SJames Chapman return NULL; 322309795f4SJames Chapman } 3232777e2abSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); 324309795f4SJames Chapman 325dbdbc73bSGuillaume Nault static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel, 326dbdbc73bSGuillaume Nault struct l2tp_session *session) 327dbdbc73bSGuillaume Nault { 328dbdbc73bSGuillaume Nault struct l2tp_session *session_walk; 329dbdbc73bSGuillaume Nault struct hlist_head *g_head; 330dbdbc73bSGuillaume Nault struct hlist_head *head; 331dbdbc73bSGuillaume Nault struct l2tp_net *pn; 332f3c66d4eSGuillaume Nault int err; 333dbdbc73bSGuillaume Nault 334dbdbc73bSGuillaume Nault head = l2tp_session_id_hash(tunnel, session->session_id); 335dbdbc73bSGuillaume Nault 336dbdbc73bSGuillaume Nault write_lock_bh(&tunnel->hlist_lock); 337f3c66d4eSGuillaume Nault if (!tunnel->acpt_newsess) { 338f3c66d4eSGuillaume Nault err = -ENODEV; 339f3c66d4eSGuillaume Nault goto err_tlock; 340f3c66d4eSGuillaume Nault } 341f3c66d4eSGuillaume Nault 342dbdbc73bSGuillaume Nault hlist_for_each_entry(session_walk, head, hlist) 343f3c66d4eSGuillaume Nault if (session_walk->session_id == session->session_id) { 344f3c66d4eSGuillaume Nault err = -EEXIST; 345f3c66d4eSGuillaume Nault goto err_tlock; 346f3c66d4eSGuillaume Nault } 347dbdbc73bSGuillaume Nault 348dbdbc73bSGuillaume Nault if (tunnel->version == L2TP_HDR_VER_3) { 349dbdbc73bSGuillaume Nault pn = l2tp_pernet(tunnel->l2tp_net); 350dbdbc73bSGuillaume Nault g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net), 351dbdbc73bSGuillaume Nault session->session_id); 352dbdbc73bSGuillaume Nault 353dbdbc73bSGuillaume Nault spin_lock_bh(&pn->l2tp_session_hlist_lock); 354dbdbc73bSGuillaume Nault 355f3c66d4eSGuillaume Nault hlist_for_each_entry(session_walk, g_head, global_hlist) 356f3c66d4eSGuillaume Nault if (session_walk->session_id == session->session_id) { 357f3c66d4eSGuillaume Nault err = -EEXIST; 358f3c66d4eSGuillaume Nault goto err_tlock_pnlock; 359f3c66d4eSGuillaume Nault } 360f3c66d4eSGuillaume Nault 361f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel); 362f3c66d4eSGuillaume Nault sock_hold(tunnel->sock); 363dbdbc73bSGuillaume Nault hlist_add_head_rcu(&session->global_hlist, g_head); 364f3c66d4eSGuillaume Nault 365dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 366f3c66d4eSGuillaume Nault } else { 367f3c66d4eSGuillaume Nault l2tp_tunnel_inc_refcount(tunnel); 368f3c66d4eSGuillaume Nault sock_hold(tunnel->sock); 369dbdbc73bSGuillaume Nault } 370dbdbc73bSGuillaume Nault 371dbdbc73bSGuillaume Nault hlist_add_head(&session->hlist, head); 372dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 373dbdbc73bSGuillaume Nault 374dbdbc73bSGuillaume Nault return 0; 375dbdbc73bSGuillaume Nault 376f3c66d4eSGuillaume Nault err_tlock_pnlock: 377dbdbc73bSGuillaume Nault spin_unlock_bh(&pn->l2tp_session_hlist_lock); 378f3c66d4eSGuillaume Nault err_tlock: 379dbdbc73bSGuillaume Nault write_unlock_bh(&tunnel->hlist_lock); 380dbdbc73bSGuillaume Nault 381f3c66d4eSGuillaume Nault return err; 382dbdbc73bSGuillaume Nault } 383dbdbc73bSGuillaume Nault 384fd558d18SJames Chapman /* Lookup a tunnel by id 385fd558d18SJames Chapman */ 3862f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id) 387fd558d18SJames Chapman { 388fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 389fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 390fd558d18SJames Chapman 391e02d494dSJames Chapman rcu_read_lock_bh(); 392e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 393fd558d18SJames Chapman if (tunnel->tunnel_id == tunnel_id) { 394e02d494dSJames Chapman rcu_read_unlock_bh(); 395fd558d18SJames Chapman return tunnel; 396fd558d18SJames Chapman } 397fd558d18SJames Chapman } 398e02d494dSJames Chapman rcu_read_unlock_bh(); 399fd558d18SJames Chapman 400fd558d18SJames Chapman return NULL; 401fd558d18SJames Chapman } 402fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 403fd558d18SJames Chapman 4042f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth) 405fd558d18SJames Chapman { 406fd558d18SJames Chapman struct l2tp_net *pn = l2tp_pernet(net); 407fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 408fd558d18SJames Chapman int count = 0; 409fd558d18SJames Chapman 410e02d494dSJames Chapman rcu_read_lock_bh(); 411e02d494dSJames Chapman list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 412fd558d18SJames Chapman if (++count > nth) { 413e02d494dSJames Chapman rcu_read_unlock_bh(); 414fd558d18SJames Chapman return tunnel; 415fd558d18SJames Chapman } 416fd558d18SJames Chapman } 417fd558d18SJames Chapman 418e02d494dSJames Chapman rcu_read_unlock_bh(); 419fd558d18SJames Chapman 420fd558d18SJames Chapman return NULL; 421fd558d18SJames Chapman } 422fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 423fd558d18SJames Chapman 424fd558d18SJames Chapman /***************************************************************************** 425fd558d18SJames Chapman * Receive data handling 426fd558d18SJames Chapman *****************************************************************************/ 427fd558d18SJames Chapman 428fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence 429fd558d18SJames Chapman * number. 430fd558d18SJames Chapman */ 431fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 432fd558d18SJames Chapman { 433fd558d18SJames Chapman struct sk_buff *skbp; 434fd558d18SJames Chapman struct sk_buff *tmp; 435f7faffa3SJames Chapman u32 ns = L2TP_SKB_CB(skb)->ns; 436fd558d18SJames Chapman 437fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 438fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 439fd558d18SJames Chapman if (L2TP_SKB_CB(skbp)->ns > ns) { 440fd558d18SJames Chapman __skb_queue_before(&session->reorder_q, skbp, skb); 441a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 442fd558d18SJames Chapman "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 443fd558d18SJames Chapman session->name, ns, L2TP_SKB_CB(skbp)->ns, 444fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 4457b7c0719STom Parkin atomic_long_inc(&session->stats.rx_oos_packets); 446fd558d18SJames Chapman goto out; 447fd558d18SJames Chapman } 448fd558d18SJames Chapman } 449fd558d18SJames Chapman 450fd558d18SJames Chapman __skb_queue_tail(&session->reorder_q, skb); 451fd558d18SJames Chapman 452fd558d18SJames Chapman out: 453fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 454fd558d18SJames Chapman } 455fd558d18SJames Chapman 456fd558d18SJames Chapman /* Dequeue a single skb. 457fd558d18SJames Chapman */ 458fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 459fd558d18SJames Chapman { 460fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 461fd558d18SJames Chapman int length = L2TP_SKB_CB(skb)->length; 462fd558d18SJames Chapman 463fd558d18SJames Chapman /* We're about to requeue the skb, so return resources 464fd558d18SJames Chapman * to its current owner (a socket receive buffer). 465fd558d18SJames Chapman */ 466fd558d18SJames Chapman skb_orphan(skb); 467fd558d18SJames Chapman 4687b7c0719STom Parkin atomic_long_inc(&tunnel->stats.rx_packets); 4697b7c0719STom Parkin atomic_long_add(length, &tunnel->stats.rx_bytes); 4707b7c0719STom Parkin atomic_long_inc(&session->stats.rx_packets); 4717b7c0719STom Parkin atomic_long_add(length, &session->stats.rx_bytes); 472fd558d18SJames Chapman 473fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 474fd558d18SJames Chapman /* Bump our Nr */ 475fd558d18SJames Chapman session->nr++; 4768a1631d5SJames Chapman session->nr &= session->nr_max; 477f7faffa3SJames Chapman 478a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 479a4ca44faSJoe Perches session->name, session->nr); 480fd558d18SJames Chapman } 481fd558d18SJames Chapman 482fd558d18SJames Chapman /* call private receive handler */ 483fd558d18SJames Chapman if (session->recv_skb != NULL) 484fd558d18SJames Chapman (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 485fd558d18SJames Chapman else 486fd558d18SJames Chapman kfree_skb(skb); 487fd558d18SJames Chapman 488fd558d18SJames Chapman if (session->deref) 489fd558d18SJames Chapman (*session->deref)(session); 490fd558d18SJames Chapman } 491fd558d18SJames Chapman 492fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order. 493fd558d18SJames Chapman * Skbs that have been in the queue for too long are simply discarded. 494fd558d18SJames Chapman */ 495fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session) 496fd558d18SJames Chapman { 497fd558d18SJames Chapman struct sk_buff *skb; 498fd558d18SJames Chapman struct sk_buff *tmp; 499fd558d18SJames Chapman 500fd558d18SJames Chapman /* If the pkt at the head of the queue has the nr that we 501fd558d18SJames Chapman * expect to send up next, dequeue it and any other 502fd558d18SJames Chapman * in-sequence packets behind it. 503fd558d18SJames Chapman */ 504e2e210c0SEric Dumazet start: 505fd558d18SJames Chapman spin_lock_bh(&session->reorder_q.lock); 506fd558d18SJames Chapman skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 507fd558d18SJames Chapman if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 5087b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 5097b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 510a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 511a4ca44faSJoe Perches "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 512fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 513fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 514fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 51538d40b3fSJames Chapman session->reorder_skip = 1; 516fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 517fd558d18SJames Chapman kfree_skb(skb); 518fd558d18SJames Chapman if (session->deref) 519fd558d18SJames Chapman (*session->deref)(session); 520fd558d18SJames Chapman continue; 521fd558d18SJames Chapman } 522fd558d18SJames Chapman 523fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 52438d40b3fSJames Chapman if (session->reorder_skip) { 525a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 52638d40b3fSJames Chapman "%s: advancing nr to next pkt: %u -> %u", 52738d40b3fSJames Chapman session->name, session->nr, 52838d40b3fSJames Chapman L2TP_SKB_CB(skb)->ns); 52938d40b3fSJames Chapman session->reorder_skip = 0; 53038d40b3fSJames Chapman session->nr = L2TP_SKB_CB(skb)->ns; 53138d40b3fSJames Chapman } 532fd558d18SJames Chapman if (L2TP_SKB_CB(skb)->ns != session->nr) { 533a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 534a4ca44faSJoe Perches "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 535fd558d18SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 536fd558d18SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 537fd558d18SJames Chapman skb_queue_len(&session->reorder_q)); 538fd558d18SJames Chapman goto out; 539fd558d18SJames Chapman } 540fd558d18SJames Chapman } 541fd558d18SJames Chapman __skb_unlink(skb, &session->reorder_q); 542fd558d18SJames Chapman 543fd558d18SJames Chapman /* Process the skb. We release the queue lock while we 544fd558d18SJames Chapman * do so to let other contexts process the queue. 545fd558d18SJames Chapman */ 546fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 547fd558d18SJames Chapman l2tp_recv_dequeue_skb(session, skb); 548e2e210c0SEric Dumazet goto start; 549fd558d18SJames Chapman } 550fd558d18SJames Chapman 551fd558d18SJames Chapman out: 552fd558d18SJames Chapman spin_unlock_bh(&session->reorder_q.lock); 553fd558d18SJames Chapman } 554fd558d18SJames Chapman 5558a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) 5568a1631d5SJames Chapman { 5578a1631d5SJames Chapman u32 nws; 5588a1631d5SJames Chapman 5598a1631d5SJames Chapman if (nr >= session->nr) 5608a1631d5SJames Chapman nws = nr - session->nr; 5618a1631d5SJames Chapman else 5628a1631d5SJames Chapman nws = (session->nr_max + 1) - (session->nr - nr); 5638a1631d5SJames Chapman 5648a1631d5SJames Chapman return nws < session->nr_window_size; 5658a1631d5SJames Chapman } 5668a1631d5SJames Chapman 567b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if 568b6dc01a4SJames Chapman * acceptable, else non-zero. 569b6dc01a4SJames Chapman */ 570b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) 571b6dc01a4SJames Chapman { 5728a1631d5SJames Chapman if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { 5738a1631d5SJames Chapman /* Packet sequence number is outside allowed window. 5748a1631d5SJames Chapman * Discard it. 5758a1631d5SJames Chapman */ 5768a1631d5SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 5778a1631d5SJames Chapman "%s: pkt %u len %d discarded, outside window, nr=%u\n", 5788a1631d5SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 5798a1631d5SJames Chapman L2TP_SKB_CB(skb)->length, session->nr); 5808a1631d5SJames Chapman goto discard; 5818a1631d5SJames Chapman } 5828a1631d5SJames Chapman 583b6dc01a4SJames Chapman if (session->reorder_timeout != 0) { 584b6dc01a4SJames Chapman /* Packet reordering enabled. Add skb to session's 585b6dc01a4SJames Chapman * reorder queue, in order of ns. 586b6dc01a4SJames Chapman */ 587b6dc01a4SJames Chapman l2tp_recv_queue_skb(session, skb); 588a0dbd822SJames Chapman goto out; 589a0dbd822SJames Chapman } 590a0dbd822SJames Chapman 591a0dbd822SJames Chapman /* Packet reordering disabled. Discard out-of-sequence packets, while 592a0dbd822SJames Chapman * tracking the number if in-sequence packets after the first OOS packet 593a0dbd822SJames Chapman * is seen. After nr_oos_count_max in-sequence packets, reset the 594a0dbd822SJames Chapman * sequence number to re-enable packet reception. 595b6dc01a4SJames Chapman */ 596a0dbd822SJames Chapman if (L2TP_SKB_CB(skb)->ns == session->nr) { 597a0dbd822SJames Chapman skb_queue_tail(&session->reorder_q, skb); 598a0dbd822SJames Chapman } else { 599a0dbd822SJames Chapman u32 nr_oos = L2TP_SKB_CB(skb)->ns; 600a0dbd822SJames Chapman u32 nr_next = (session->nr_oos + 1) & session->nr_max; 601a0dbd822SJames Chapman 602a0dbd822SJames Chapman if (nr_oos == nr_next) 603a0dbd822SJames Chapman session->nr_oos_count++; 604a0dbd822SJames Chapman else 605a0dbd822SJames Chapman session->nr_oos_count = 0; 606a0dbd822SJames Chapman 607a0dbd822SJames Chapman session->nr_oos = nr_oos; 608a0dbd822SJames Chapman if (session->nr_oos_count > session->nr_oos_count_max) { 609a0dbd822SJames Chapman session->reorder_skip = 1; 610a0dbd822SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 611a0dbd822SJames Chapman "%s: %d oos packets received. Resetting sequence numbers\n", 612a0dbd822SJames Chapman session->name, session->nr_oos_count); 613a0dbd822SJames Chapman } 614a0dbd822SJames Chapman if (!session->reorder_skip) { 615b6dc01a4SJames Chapman atomic_long_inc(&session->stats.rx_seq_discards); 616b6dc01a4SJames Chapman l2tp_dbg(session, L2TP_MSG_SEQ, 617b6dc01a4SJames Chapman "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 618b6dc01a4SJames Chapman session->name, L2TP_SKB_CB(skb)->ns, 619b6dc01a4SJames Chapman L2TP_SKB_CB(skb)->length, session->nr, 620b6dc01a4SJames Chapman skb_queue_len(&session->reorder_q)); 621b6dc01a4SJames Chapman goto discard; 622b6dc01a4SJames Chapman } 623b6dc01a4SJames Chapman skb_queue_tail(&session->reorder_q, skb); 624b6dc01a4SJames Chapman } 625b6dc01a4SJames Chapman 626a0dbd822SJames Chapman out: 627b6dc01a4SJames Chapman return 0; 628b6dc01a4SJames Chapman 629b6dc01a4SJames Chapman discard: 630b6dc01a4SJames Chapman return 1; 631b6dc01a4SJames Chapman } 632b6dc01a4SJames Chapman 633f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2 634f7faffa3SJames Chapman * and L2TPv3 data frames here. 635f7faffa3SJames Chapman * 636f7faffa3SJames Chapman * L2TPv2 Data Message Header 637f7faffa3SJames Chapman * 638f7faffa3SJames Chapman * 0 1 2 3 639f7faffa3SJames 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 640f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 641f7faffa3SJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 642f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 643f7faffa3SJames Chapman * | Tunnel ID | Session ID | 644f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 645f7faffa3SJames Chapman * | Ns (opt) | Nr (opt) | 646f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 647f7faffa3SJames Chapman * | Offset Size (opt) | Offset pad... (opt) 648f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 649f7faffa3SJames Chapman * 650f7faffa3SJames Chapman * Data frames are marked by T=0. All other fields are the same as 651f7faffa3SJames Chapman * those in L2TP control frames. 652f7faffa3SJames Chapman * 653f7faffa3SJames Chapman * L2TPv3 Data Message Header 654f7faffa3SJames Chapman * 655f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 656f7faffa3SJames Chapman * | L2TP Session Header | 657f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 658f7faffa3SJames Chapman * | L2-Specific Sublayer | 659f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 660f7faffa3SJames Chapman * | Tunnel Payload ... 661f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 662f7faffa3SJames Chapman * 663f7faffa3SJames Chapman * L2TPv3 Session Header Over IP 664f7faffa3SJames Chapman * 665f7faffa3SJames Chapman * 0 1 2 3 666f7faffa3SJames 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 667f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 668f7faffa3SJames Chapman * | Session ID | 669f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 670f7faffa3SJames Chapman * | Cookie (optional, maximum 64 bits)... 671f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 672f7faffa3SJames Chapman * | 673f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 674f7faffa3SJames Chapman * 675f7faffa3SJames Chapman * L2TPv3 L2-Specific Sublayer Format 676f7faffa3SJames Chapman * 677f7faffa3SJames Chapman * 0 1 2 3 678f7faffa3SJames 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 679f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 680f7faffa3SJames Chapman * |x|S|x|x|x|x|x|x| Sequence Number | 681f7faffa3SJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 682f7faffa3SJames Chapman * 683f7faffa3SJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with 684f7faffa3SJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need 685f7faffa3SJames Chapman * to parse the packet header to determine if optional fields are 686f7faffa3SJames Chapman * present. 687f7faffa3SJames Chapman * 688f7faffa3SJames Chapman * Caller must already have parsed the frame and determined that it is 689f7faffa3SJames Chapman * a data (not control) frame before coming here. Fields up to the 690f7faffa3SJames Chapman * session-id have already been parsed and ptr points to the data 691f7faffa3SJames Chapman * after the session-id. 69261b9a047SGuillaume Nault * 69361b9a047SGuillaume Nault * session->ref() must have been called prior to l2tp_recv_common(). 69461b9a047SGuillaume Nault * session->deref() will be called automatically after skb is processed. 695f7faffa3SJames Chapman */ 696f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 697f7faffa3SJames Chapman unsigned char *ptr, unsigned char *optr, u16 hdrflags, 698f7faffa3SJames Chapman int length, int (*payload_hook)(struct sk_buff *skb)) 699f7faffa3SJames Chapman { 700f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 701f7faffa3SJames Chapman int offset; 702f7faffa3SJames Chapman u32 ns, nr; 703f7faffa3SJames Chapman 704f7faffa3SJames Chapman /* Parse and check optional cookie */ 705f7faffa3SJames Chapman if (session->peer_cookie_len > 0) { 706f7faffa3SJames Chapman if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 707a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 708f7faffa3SJames Chapman "%s: cookie mismatch (%u/%u). Discarding.\n", 709a4ca44faSJoe Perches tunnel->name, tunnel->tunnel_id, 710a4ca44faSJoe Perches session->session_id); 7117b7c0719STom Parkin atomic_long_inc(&session->stats.rx_cookie_discards); 712f7faffa3SJames Chapman goto discard; 713f7faffa3SJames Chapman } 714f7faffa3SJames Chapman ptr += session->peer_cookie_len; 715f7faffa3SJames Chapman } 716f7faffa3SJames Chapman 717f7faffa3SJames Chapman /* Handle the optional sequence numbers. Sequence numbers are 718f7faffa3SJames Chapman * in different places for L2TPv2 and L2TPv3. 719f7faffa3SJames Chapman * 720f7faffa3SJames Chapman * If we are the LAC, enable/disable sequence numbers under 721f7faffa3SJames Chapman * the control of the LNS. If no sequence numbers present but 722f7faffa3SJames Chapman * we were expecting them, discard frame. 723f7faffa3SJames Chapman */ 724f7faffa3SJames Chapman ns = nr = 0; 725f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 0; 726f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 727f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_S) { 728f7faffa3SJames Chapman ns = ntohs(*(__be16 *) ptr); 729f7faffa3SJames Chapman ptr += 2; 730f7faffa3SJames Chapman nr = ntohs(*(__be16 *) ptr); 731f7faffa3SJames Chapman ptr += 2; 732f7faffa3SJames Chapman 733f7faffa3SJames Chapman /* Store L2TP info in the skb */ 734f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 735f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 736f7faffa3SJames Chapman 737a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 738f7faffa3SJames Chapman "%s: recv data ns=%u, nr=%u, session nr=%u\n", 739f7faffa3SJames Chapman session->name, ns, nr, session->nr); 740f7faffa3SJames Chapman } 741f7faffa3SJames Chapman } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 742f7faffa3SJames Chapman u32 l2h = ntohl(*(__be32 *) ptr); 743f7faffa3SJames Chapman 744f7faffa3SJames Chapman if (l2h & 0x40000000) { 745f7faffa3SJames Chapman ns = l2h & 0x00ffffff; 746f7faffa3SJames Chapman 747f7faffa3SJames Chapman /* Store L2TP info in the skb */ 748f7faffa3SJames Chapman L2TP_SKB_CB(skb)->ns = ns; 749f7faffa3SJames Chapman L2TP_SKB_CB(skb)->has_seq = 1; 750f7faffa3SJames Chapman 751a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 752f7faffa3SJames Chapman "%s: recv data ns=%u, session nr=%u\n", 753f7faffa3SJames Chapman session->name, ns, session->nr); 754f7faffa3SJames Chapman } 755f7faffa3SJames Chapman } 756f7faffa3SJames Chapman 757f7faffa3SJames Chapman /* Advance past L2-specific header, if present */ 758f7faffa3SJames Chapman ptr += session->l2specific_len; 759f7faffa3SJames Chapman 760f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 761f7faffa3SJames Chapman /* Received a packet with sequence numbers. If we're the LNS, 762f7faffa3SJames Chapman * check if we sre sending sequence numbers and if not, 763f7faffa3SJames Chapman * configure it so. 764f7faffa3SJames Chapman */ 765f7faffa3SJames Chapman if ((!session->lns_mode) && (!session->send_seq)) { 766a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 767f7faffa3SJames Chapman "%s: requested to enable seq numbers by LNS\n", 768f7faffa3SJames Chapman session->name); 7693f9b9770SAsbjørn Sloth Tønnesen session->send_seq = 1; 770f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 771f7faffa3SJames Chapman } 772f7faffa3SJames Chapman } else { 773f7faffa3SJames Chapman /* No sequence numbers. 774f7faffa3SJames Chapman * If user has configured mandatory sequence numbers, discard. 775f7faffa3SJames Chapman */ 776f7faffa3SJames Chapman if (session->recv_seq) { 777a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 778a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 779a4ca44faSJoe Perches session->name); 7807b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 781f7faffa3SJames Chapman goto discard; 782f7faffa3SJames Chapman } 783f7faffa3SJames Chapman 784f7faffa3SJames Chapman /* If we're the LAC and we're sending sequence numbers, the 785f7faffa3SJames Chapman * LNS has requested that we no longer send sequence numbers. 786f7faffa3SJames Chapman * If we're the LNS and we're sending sequence numbers, the 787f7faffa3SJames Chapman * LAC is broken. Discard the frame. 788f7faffa3SJames Chapman */ 789f7faffa3SJames Chapman if ((!session->lns_mode) && (session->send_seq)) { 790a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_SEQ, 791f7faffa3SJames Chapman "%s: requested to disable seq numbers by LNS\n", 792f7faffa3SJames Chapman session->name); 793f7faffa3SJames Chapman session->send_seq = 0; 794f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 795f7faffa3SJames Chapman } else if (session->send_seq) { 796a4ca44faSJoe Perches l2tp_warn(session, L2TP_MSG_SEQ, 797a4ca44faSJoe Perches "%s: recv data has no seq numbers when required. Discarding.\n", 798a4ca44faSJoe Perches session->name); 7997b7c0719STom Parkin atomic_long_inc(&session->stats.rx_seq_discards); 800f7faffa3SJames Chapman goto discard; 801f7faffa3SJames Chapman } 802f7faffa3SJames Chapman } 803f7faffa3SJames Chapman 804f7faffa3SJames Chapman /* Session data offset is handled differently for L2TPv2 and 805f7faffa3SJames Chapman * L2TPv3. For L2TPv2, there is an optional 16-bit value in 806f7faffa3SJames Chapman * the header. For L2TPv3, the offset is negotiated using AVPs 807f7faffa3SJames Chapman * in the session setup control protocol. 808f7faffa3SJames Chapman */ 809f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 810f7faffa3SJames Chapman /* If offset bit set, skip it. */ 811f7faffa3SJames Chapman if (hdrflags & L2TP_HDRFLAG_O) { 812f7faffa3SJames Chapman offset = ntohs(*(__be16 *)ptr); 813f7faffa3SJames Chapman ptr += 2 + offset; 814f7faffa3SJames Chapman } 815f7faffa3SJames Chapman } else 816f7faffa3SJames Chapman ptr += session->offset; 817f7faffa3SJames Chapman 818f7faffa3SJames Chapman offset = ptr - optr; 819f7faffa3SJames Chapman if (!pskb_may_pull(skb, offset)) 820f7faffa3SJames Chapman goto discard; 821f7faffa3SJames Chapman 822f7faffa3SJames Chapman __skb_pull(skb, offset); 823f7faffa3SJames Chapman 824f7faffa3SJames Chapman /* If caller wants to process the payload before we queue the 825f7faffa3SJames Chapman * packet, do so now. 826f7faffa3SJames Chapman */ 827f7faffa3SJames Chapman if (payload_hook) 828f7faffa3SJames Chapman if ((*payload_hook)(skb)) 829f7faffa3SJames Chapman goto discard; 830f7faffa3SJames Chapman 831f7faffa3SJames Chapman /* Prepare skb for adding to the session's reorder_q. Hold 832f7faffa3SJames Chapman * packets for max reorder_timeout or 1 second if not 833f7faffa3SJames Chapman * reordering. 834f7faffa3SJames Chapman */ 835f7faffa3SJames Chapman L2TP_SKB_CB(skb)->length = length; 836f7faffa3SJames Chapman L2TP_SKB_CB(skb)->expires = jiffies + 837f7faffa3SJames Chapman (session->reorder_timeout ? session->reorder_timeout : HZ); 838f7faffa3SJames Chapman 839f7faffa3SJames Chapman /* Add packet to the session's receive queue. Reordering is done here, if 840f7faffa3SJames Chapman * enabled. Saved L2TP protocol info is stored in skb->sb[]. 841f7faffa3SJames Chapman */ 842f7faffa3SJames Chapman if (L2TP_SKB_CB(skb)->has_seq) { 843b6dc01a4SJames Chapman if (l2tp_recv_data_seq(session, skb)) 844f7faffa3SJames Chapman goto discard; 845f7faffa3SJames Chapman } else { 846f7faffa3SJames Chapman /* No sequence numbers. Add the skb to the tail of the 847f7faffa3SJames Chapman * reorder queue. This ensures that it will be 848f7faffa3SJames Chapman * delivered after all previous sequenced skbs. 849f7faffa3SJames Chapman */ 850f7faffa3SJames Chapman skb_queue_tail(&session->reorder_q, skb); 851f7faffa3SJames Chapman } 852f7faffa3SJames Chapman 853f7faffa3SJames Chapman /* Try to dequeue as many skbs from reorder_q as we can. */ 854f7faffa3SJames Chapman l2tp_recv_dequeue(session); 855f7faffa3SJames Chapman 856f7faffa3SJames Chapman return; 857f7faffa3SJames Chapman 858f7faffa3SJames Chapman discard: 8597b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 860f7faffa3SJames Chapman kfree_skb(skb); 861f7faffa3SJames Chapman 862f7faffa3SJames Chapman if (session->deref) 863f7faffa3SJames Chapman (*session->deref)(session); 864f7faffa3SJames Chapman } 865f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common); 866f7faffa3SJames Chapman 86748f72f92STom Parkin /* Drop skbs from the session's reorder_q 86848f72f92STom Parkin */ 86948f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session) 87048f72f92STom Parkin { 87148f72f92STom Parkin struct sk_buff *skb = NULL; 87248f72f92STom Parkin BUG_ON(!session); 87348f72f92STom Parkin BUG_ON(session->magic != L2TP_SESSION_MAGIC); 87448f72f92STom Parkin while ((skb = skb_dequeue(&session->reorder_q))) { 87548f72f92STom Parkin atomic_long_inc(&session->stats.rx_errors); 87648f72f92STom Parkin kfree_skb(skb); 87748f72f92STom Parkin if (session->deref) 87848f72f92STom Parkin (*session->deref)(session); 87948f72f92STom Parkin } 88048f72f92STom Parkin return 0; 88148f72f92STom Parkin } 88248f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); 88348f72f92STom Parkin 884fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 885fd558d18SJames Chapman * here. The skb is not on a list when we get here. 886fd558d18SJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on. 887fd558d18SJames Chapman * Returns 1 if the packet was not a good data packet and could not be 888fd558d18SJames Chapman * forwarded. All such packets are passed up to userspace to deal with. 889fd558d18SJames Chapman */ 890fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 891fd558d18SJames Chapman int (*payload_hook)(struct sk_buff *skb)) 892fd558d18SJames Chapman { 893fd558d18SJames Chapman struct l2tp_session *session = NULL; 894fd558d18SJames Chapman unsigned char *ptr, *optr; 895fd558d18SJames Chapman u16 hdrflags; 896fd558d18SJames Chapman u32 tunnel_id, session_id; 897fd558d18SJames Chapman u16 version; 898f7faffa3SJames Chapman int length; 899fd558d18SJames Chapman 90058d6085cSTom Herbert /* UDP has verifed checksum */ 901fd558d18SJames Chapman 902fd558d18SJames Chapman /* UDP always verifies the packet length. */ 903fd558d18SJames Chapman __skb_pull(skb, sizeof(struct udphdr)); 904fd558d18SJames Chapman 905fd558d18SJames Chapman /* Short packet? */ 906fd558d18SJames Chapman if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 907a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 908a4ca44faSJoe Perches "%s: recv short packet (len=%d)\n", 909a4ca44faSJoe Perches tunnel->name, skb->len); 910fd558d18SJames Chapman goto error; 911fd558d18SJames Chapman } 912fd558d18SJames Chapman 913fd558d18SJames Chapman /* Trace packet contents, if enabled */ 914fd558d18SJames Chapman if (tunnel->debug & L2TP_MSG_DATA) { 915fd558d18SJames Chapman length = min(32u, skb->len); 916fd558d18SJames Chapman if (!pskb_may_pull(skb, length)) 917fd558d18SJames Chapman goto error; 918fd558d18SJames Chapman 919a4ca44faSJoe Perches pr_debug("%s: recv\n", tunnel->name); 920a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 921fd558d18SJames Chapman } 922fd558d18SJames Chapman 923e50e705cSEric Dumazet /* Point to L2TP header */ 924e50e705cSEric Dumazet optr = ptr = skb->data; 925e50e705cSEric Dumazet 926fd558d18SJames Chapman /* Get L2TP header flags */ 927fd558d18SJames Chapman hdrflags = ntohs(*(__be16 *) ptr); 928fd558d18SJames Chapman 929fd558d18SJames Chapman /* Check protocol version */ 930fd558d18SJames Chapman version = hdrflags & L2TP_HDR_VER_MASK; 931fd558d18SJames Chapman if (version != tunnel->version) { 932a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 933fd558d18SJames Chapman "%s: recv protocol version mismatch: got %d expected %d\n", 934fd558d18SJames Chapman tunnel->name, version, tunnel->version); 935fd558d18SJames Chapman goto error; 936fd558d18SJames Chapman } 937fd558d18SJames Chapman 938fd558d18SJames Chapman /* Get length of L2TP packet */ 939fd558d18SJames Chapman length = skb->len; 940fd558d18SJames Chapman 941fd558d18SJames Chapman /* If type is control packet, it is handled by userspace. */ 942fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_T) { 943a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, 944a4ca44faSJoe Perches "%s: recv control packet, len=%d\n", 945a4ca44faSJoe Perches tunnel->name, length); 946fd558d18SJames Chapman goto error; 947fd558d18SJames Chapman } 948fd558d18SJames Chapman 949fd558d18SJames Chapman /* Skip flags */ 950fd558d18SJames Chapman ptr += 2; 951fd558d18SJames Chapman 952f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) { 953fd558d18SJames Chapman /* If length is present, skip it */ 954fd558d18SJames Chapman if (hdrflags & L2TP_HDRFLAG_L) 955fd558d18SJames Chapman ptr += 2; 956fd558d18SJames Chapman 957fd558d18SJames Chapman /* Extract tunnel and session ID */ 958fd558d18SJames Chapman tunnel_id = ntohs(*(__be16 *) ptr); 959fd558d18SJames Chapman ptr += 2; 960fd558d18SJames Chapman session_id = ntohs(*(__be16 *) ptr); 961fd558d18SJames Chapman ptr += 2; 962f7faffa3SJames Chapman } else { 963f7faffa3SJames Chapman ptr += 2; /* skip reserved bits */ 964f7faffa3SJames Chapman tunnel_id = tunnel->tunnel_id; 965f7faffa3SJames Chapman session_id = ntohl(*(__be32 *) ptr); 966f7faffa3SJames Chapman ptr += 4; 967f7faffa3SJames Chapman } 968fd558d18SJames Chapman 969fd558d18SJames Chapman /* Find the session context */ 97061b9a047SGuillaume Nault session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true); 971309795f4SJames Chapman if (!session || !session->recv_skb) { 97261b9a047SGuillaume Nault if (session) { 97361b9a047SGuillaume Nault if (session->deref) 97461b9a047SGuillaume Nault session->deref(session); 97561b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 97661b9a047SGuillaume Nault } 97761b9a047SGuillaume Nault 978fd558d18SJames Chapman /* Not found? Pass to userspace to deal with */ 979a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_DATA, 980f7faffa3SJames Chapman "%s: no session found (%u/%u). Passing up.\n", 981fd558d18SJames Chapman tunnel->name, tunnel_id, session_id); 982fd558d18SJames Chapman goto error; 983fd558d18SJames Chapman } 984fd558d18SJames Chapman 985f7faffa3SJames Chapman l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 98661b9a047SGuillaume Nault l2tp_session_dec_refcount(session); 987fd558d18SJames Chapman 988fd558d18SJames Chapman return 0; 989fd558d18SJames Chapman 990fd558d18SJames Chapman error: 991fd558d18SJames Chapman /* Put UDP header back */ 992fd558d18SJames Chapman __skb_push(skb, sizeof(struct udphdr)); 993fd558d18SJames Chapman 994fd558d18SJames Chapman return 1; 995fd558d18SJames Chapman } 996fd558d18SJames Chapman 997fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c. 998fd558d18SJames Chapman * Return codes: 999fd558d18SJames Chapman * 0 : success. 1000fd558d18SJames Chapman * <0: error 1001fd558d18SJames Chapman * >0: skb should be passed up to userspace as UDP. 1002fd558d18SJames Chapman */ 1003fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 1004fd558d18SJames Chapman { 1005fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1006fd558d18SJames Chapman 1007fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk); 1008fd558d18SJames Chapman if (tunnel == NULL) 1009fd558d18SJames Chapman goto pass_up; 1010fd558d18SJames Chapman 1011a4ca44faSJoe Perches l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 1012a4ca44faSJoe Perches tunnel->name, skb->len); 1013fd558d18SJames Chapman 1014fd558d18SJames Chapman if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 1015fd558d18SJames Chapman goto pass_up_put; 1016fd558d18SJames Chapman 1017fd558d18SJames Chapman sock_put(sk); 1018fd558d18SJames Chapman return 0; 1019fd558d18SJames Chapman 1020fd558d18SJames Chapman pass_up_put: 1021fd558d18SJames Chapman sock_put(sk); 1022fd558d18SJames Chapman pass_up: 1023fd558d18SJames Chapman return 1; 1024fd558d18SJames Chapman } 1025fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 1026fd558d18SJames Chapman 1027fd558d18SJames Chapman /************************************************************************ 1028fd558d18SJames Chapman * Transmit handling 1029fd558d18SJames Chapman ***********************************************************************/ 1030fd558d18SJames Chapman 1031fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided. 1032fd558d18SJames Chapman */ 1033f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 1034fd558d18SJames Chapman { 1035f7faffa3SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1036fd558d18SJames Chapman __be16 *bufp = buf; 1037f7faffa3SJames Chapman __be16 *optr = buf; 1038fd558d18SJames Chapman u16 flags = L2TP_HDR_VER_2; 1039fd558d18SJames Chapman u32 tunnel_id = tunnel->peer_tunnel_id; 1040fd558d18SJames Chapman u32 session_id = session->peer_session_id; 1041fd558d18SJames Chapman 1042fd558d18SJames Chapman if (session->send_seq) 1043fd558d18SJames Chapman flags |= L2TP_HDRFLAG_S; 1044fd558d18SJames Chapman 1045fd558d18SJames Chapman /* Setup L2TP header. */ 1046fd558d18SJames Chapman *bufp++ = htons(flags); 1047fd558d18SJames Chapman *bufp++ = htons(tunnel_id); 1048fd558d18SJames Chapman *bufp++ = htons(session_id); 1049fd558d18SJames Chapman if (session->send_seq) { 1050fd558d18SJames Chapman *bufp++ = htons(session->ns); 1051fd558d18SJames Chapman *bufp++ = 0; 1052fd558d18SJames Chapman session->ns++; 1053f7faffa3SJames Chapman session->ns &= 0xffff; 1054a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 1055a4ca44faSJoe Perches session->name, session->ns); 1056fd558d18SJames Chapman } 1057fd558d18SJames Chapman 1058f7faffa3SJames Chapman return bufp - optr; 1059f7faffa3SJames Chapman } 1060f7faffa3SJames Chapman 1061f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 1062fd558d18SJames Chapman { 10630d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1064f7faffa3SJames Chapman char *bufp = buf; 1065f7faffa3SJames Chapman char *optr = bufp; 1066fd558d18SJames Chapman 10670d76751fSJames Chapman /* Setup L2TP header. The header differs slightly for UDP and 10680d76751fSJames Chapman * IP encapsulations. For UDP, there is 4 bytes of flags. 10690d76751fSJames Chapman */ 10700d76751fSJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 10710d76751fSJames Chapman u16 flags = L2TP_HDR_VER_3; 1072f7faffa3SJames Chapman *((__be16 *) bufp) = htons(flags); 1073f7faffa3SJames Chapman bufp += 2; 1074f7faffa3SJames Chapman *((__be16 *) bufp) = 0; 1075f7faffa3SJames Chapman bufp += 2; 10760d76751fSJames Chapman } 10770d76751fSJames Chapman 1078f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(session->peer_session_id); 1079f7faffa3SJames Chapman bufp += 4; 1080f7faffa3SJames Chapman if (session->cookie_len) { 1081f7faffa3SJames Chapman memcpy(bufp, &session->cookie[0], session->cookie_len); 1082f7faffa3SJames Chapman bufp += session->cookie_len; 1083fd558d18SJames Chapman } 1084f7faffa3SJames Chapman if (session->l2specific_len) { 1085f7faffa3SJames Chapman if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1086f7faffa3SJames Chapman u32 l2h = 0; 1087f7faffa3SJames Chapman if (session->send_seq) { 1088f7faffa3SJames Chapman l2h = 0x40000000 | session->ns; 1089f7faffa3SJames Chapman session->ns++; 1090f7faffa3SJames Chapman session->ns &= 0xffffff; 1091a4ca44faSJoe Perches l2tp_dbg(session, L2TP_MSG_SEQ, 1092a4ca44faSJoe Perches "%s: updated ns to %u\n", 1093a4ca44faSJoe Perches session->name, session->ns); 1094f7faffa3SJames Chapman } 1095f7faffa3SJames Chapman 1096f7faffa3SJames Chapman *((__be32 *) bufp) = htonl(l2h); 1097f7faffa3SJames Chapman } 1098f7faffa3SJames Chapman bufp += session->l2specific_len; 1099f7faffa3SJames Chapman } 1100f7faffa3SJames Chapman if (session->offset) 1101f7faffa3SJames Chapman bufp += session->offset; 1102f7faffa3SJames Chapman 1103f7faffa3SJames Chapman return bufp - optr; 1104f7faffa3SJames Chapman } 1105fd558d18SJames Chapman 1106fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1107d9d8da80SDavid S. Miller struct flowi *fl, size_t data_len) 1108fd558d18SJames Chapman { 1109fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1110fd558d18SJames Chapman unsigned int len = skb->len; 1111fd558d18SJames Chapman int error; 1112fd558d18SJames Chapman 1113fd558d18SJames Chapman /* Debug */ 1114fd558d18SJames Chapman if (session->send_seq) 11155b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", 1116a4ca44faSJoe Perches session->name, data_len, session->ns - 1); 1117fd558d18SJames Chapman else 11185b5e0928SAlexey Dobriyan l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", 1119a4ca44faSJoe Perches session->name, data_len); 1120fd558d18SJames Chapman 1121fd558d18SJames Chapman if (session->debug & L2TP_MSG_DATA) { 11220d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11230d76751fSJames Chapman unsigned char *datap = skb->data + uhlen; 1124fd558d18SJames Chapman 1125a4ca44faSJoe Perches pr_debug("%s: xmit\n", session->name); 1126a4ca44faSJoe Perches print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1127a4ca44faSJoe Perches datap, min_t(size_t, 32, len - uhlen)); 1128fd558d18SJames Chapman } 1129fd558d18SJames Chapman 1130fd558d18SJames Chapman /* Queue the packet to IP for output */ 113160ff7467SWANG Cong skb->ignore_df = 1; 1132d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1133746e3499SEric Dumazet if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped) 1134b0270e91SEric Dumazet error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1135d2cf3361SBenjamin LaHaise else 1136d2cf3361SBenjamin LaHaise #endif 1137b0270e91SEric Dumazet error = ip_queue_xmit(tunnel->sock, skb, fl); 1138fd558d18SJames Chapman 1139fd558d18SJames Chapman /* Update stats */ 1140fd558d18SJames Chapman if (error >= 0) { 11417b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_packets); 11427b7c0719STom Parkin atomic_long_add(len, &tunnel->stats.tx_bytes); 11437b7c0719STom Parkin atomic_long_inc(&session->stats.tx_packets); 11447b7c0719STom Parkin atomic_long_add(len, &session->stats.tx_bytes); 1145fd558d18SJames Chapman } else { 11467b7c0719STom Parkin atomic_long_inc(&tunnel->stats.tx_errors); 11477b7c0719STom Parkin atomic_long_inc(&session->stats.tx_errors); 1148fd558d18SJames Chapman } 1149fd558d18SJames Chapman 1150fd558d18SJames Chapman return 0; 1151fd558d18SJames Chapman } 1152fd558d18SJames Chapman 1153fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be 1154fd558d18SJames Chapman * inserted in the skb data before calling this function. 1155fd558d18SJames Chapman */ 1156fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1157fd558d18SJames Chapman { 1158fd558d18SJames Chapman int data_len = skb->len; 11590d76751fSJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 11600d76751fSJames Chapman struct sock *sk = tunnel->sock; 1161d9d8da80SDavid S. Miller struct flowi *fl; 1162fd558d18SJames Chapman struct udphdr *uh; 1163fd558d18SJames Chapman struct inet_sock *inet; 1164fd558d18SJames Chapman int headroom; 11650d76751fSJames Chapman int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 11660d76751fSJames Chapman int udp_len; 1167b8c84307SEric Dumazet int ret = NET_XMIT_SUCCESS; 1168fd558d18SJames Chapman 1169fd558d18SJames Chapman /* Check that there's enough headroom in the skb to insert IP, 1170fd558d18SJames Chapman * UDP and L2TP headers. If not enough, expand it to 1171fd558d18SJames Chapman * make room. Adjust truesize. 1172fd558d18SJames Chapman */ 1173fd558d18SJames Chapman headroom = NET_SKB_PAD + sizeof(struct iphdr) + 11740d76751fSJames Chapman uhlen + hdr_len; 1175835acf5dSEric Dumazet if (skb_cow_head(skb, headroom)) { 1176b8c84307SEric Dumazet kfree_skb(skb); 1177b8c84307SEric Dumazet return NET_XMIT_DROP; 1178835acf5dSEric Dumazet } 1179fd558d18SJames Chapman 1180fd558d18SJames Chapman /* Setup L2TP header */ 1181f7faffa3SJames Chapman session->build_header(session, __skb_push(skb, hdr_len)); 1182fd558d18SJames Chapman 11830d76751fSJames Chapman /* Reset skb netfilter state */ 1184fd558d18SJames Chapman memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1185fd558d18SJames Chapman IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1186fd558d18SJames Chapman IPSKB_REROUTED); 1187fd558d18SJames Chapman nf_reset(skb); 1188fd558d18SJames Chapman 11896af88da1SDavid S. Miller bh_lock_sock(sk); 11906af88da1SDavid S. Miller if (sock_owned_by_user(sk)) { 1191b8c84307SEric Dumazet kfree_skb(skb); 1192b8c84307SEric Dumazet ret = NET_XMIT_DROP; 11936af88da1SDavid S. Miller goto out_unlock; 11946af88da1SDavid S. Miller } 11956af88da1SDavid S. Miller 1196fd558d18SJames Chapman /* Get routing info from the tunnel socket */ 1197fd558d18SJames Chapman skb_dst_drop(skb); 119871b1391aSFlorian Westphal skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 11990d76751fSJames Chapman 1200d9d8da80SDavid S. Miller inet = inet_sk(sk); 1201d9d8da80SDavid S. Miller fl = &inet->cork.fl; 12020d76751fSJames Chapman switch (tunnel->encap) { 12030d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 12040d76751fSJames Chapman /* Setup UDP header */ 12050d76751fSJames Chapman __skb_push(skb, sizeof(*uh)); 12060d76751fSJames Chapman skb_reset_transport_header(skb); 12070d76751fSJames Chapman uh = udp_hdr(skb); 12080d76751fSJames Chapman uh->source = inet->inet_sport; 12090d76751fSJames Chapman uh->dest = inet->inet_dport; 12100d76751fSJames Chapman udp_len = uhlen + hdr_len + data_len; 12110d76751fSJames Chapman uh->len = htons(udp_len); 1212fd558d18SJames Chapman 1213fd558d18SJames Chapman /* Calculate UDP checksum if configured to do so */ 1214d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 1215e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) 121677157e19STom Herbert udp6_set_csum(udp_get_no_check6_tx(sk), 121777157e19STom Herbert skb, &inet6_sk(sk)->saddr, 121877157e19STom Herbert &sk->sk_v6_daddr, udp_len); 1219d2cf3361SBenjamin LaHaise else 1220d2cf3361SBenjamin LaHaise #endif 122177157e19STom Herbert udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 122277157e19STom Herbert inet->inet_daddr, udp_len); 12230d76751fSJames Chapman break; 12240d76751fSJames Chapman 12250d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12260d76751fSJames Chapman break; 12270d76751fSJames Chapman } 12280d76751fSJames Chapman 1229d9d8da80SDavid S. Miller l2tp_xmit_core(session, skb, fl, data_len); 12306af88da1SDavid S. Miller out_unlock: 12316af88da1SDavid S. Miller bh_unlock_sock(sk); 1232fd558d18SJames Chapman 1233b8c84307SEric Dumazet return ret; 1234fd558d18SJames Chapman } 1235fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1236fd558d18SJames Chapman 1237fd558d18SJames Chapman /***************************************************************************** 1238fd558d18SJames Chapman * Tinnel and session create/destroy. 1239fd558d18SJames Chapman *****************************************************************************/ 1240fd558d18SJames Chapman 1241fd558d18SJames Chapman /* Tunnel socket destruct hook. 1242fd558d18SJames Chapman * The tunnel context is deleted only when all session sockets have been 1243fd558d18SJames Chapman * closed. 1244fd558d18SJames Chapman */ 1245fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk) 1246fd558d18SJames Chapman { 12478d8a51e2SDavid S. Miller struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1248f8ccac0eSTom Parkin struct l2tp_net *pn; 1249fd558d18SJames Chapman 1250fd558d18SJames Chapman if (tunnel == NULL) 1251fd558d18SJames Chapman goto end; 1252fd558d18SJames Chapman 1253a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1254fd558d18SJames Chapman 1255fd558d18SJames Chapman 1256f8ccac0eSTom Parkin /* Disable udp encapsulation */ 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; 12629980d001STom Parkin (udp_sk(sk))->encap_destroy = NULL; 12630d76751fSJames Chapman break; 12640d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 12650d76751fSJames Chapman break; 12660d76751fSJames Chapman } 1267fd558d18SJames Chapman 1268fd558d18SJames Chapman /* Remove hooks into tunnel socket */ 1269fd558d18SJames Chapman sk->sk_destruct = tunnel->old_sk_destruct; 1270fd558d18SJames Chapman sk->sk_user_data = NULL; 1271f8ccac0eSTom Parkin 1272f8ccac0eSTom Parkin /* Remove the tunnel struct from the tunnel list */ 1273f8ccac0eSTom Parkin pn = l2tp_pernet(tunnel->l2tp_net); 1274f8ccac0eSTom Parkin spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1275f8ccac0eSTom Parkin list_del_rcu(&tunnel->list); 1276f8ccac0eSTom Parkin spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1277f8ccac0eSTom Parkin atomic_dec(&l2tp_tunnel_count); 1278f8ccac0eSTom Parkin 1279f8ccac0eSTom Parkin l2tp_tunnel_closeall(tunnel); 1280f3c66d4eSGuillaume Nault 1281f3c66d4eSGuillaume Nault tunnel->sock = NULL; 1282f8ccac0eSTom Parkin l2tp_tunnel_dec_refcount(tunnel); 1283fd558d18SJames Chapman 1284fd558d18SJames Chapman /* Call the original destructor */ 1285fd558d18SJames Chapman if (sk->sk_destruct) 1286fd558d18SJames Chapman (*sk->sk_destruct)(sk); 1287fd558d18SJames Chapman end: 1288fd558d18SJames Chapman return; 1289fd558d18SJames Chapman } 1290fd558d18SJames Chapman 1291fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too. 1292fd558d18SJames Chapman */ 1293e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1294fd558d18SJames Chapman { 1295fd558d18SJames Chapman int hash; 1296fd558d18SJames Chapman struct hlist_node *walk; 1297fd558d18SJames Chapman struct hlist_node *tmp; 1298fd558d18SJames Chapman struct l2tp_session *session; 1299fd558d18SJames Chapman 1300fd558d18SJames Chapman BUG_ON(tunnel == NULL); 1301fd558d18SJames Chapman 1302a4ca44faSJoe Perches l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1303a4ca44faSJoe Perches tunnel->name); 1304fd558d18SJames Chapman 1305fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1306f3c66d4eSGuillaume Nault tunnel->acpt_newsess = false; 1307fd558d18SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1308fd558d18SJames Chapman again: 1309fd558d18SJames Chapman hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1310fd558d18SJames Chapman session = hlist_entry(walk, struct l2tp_session, hlist); 1311fd558d18SJames Chapman 1312a4ca44faSJoe Perches l2tp_info(session, L2TP_MSG_CONTROL, 1313fd558d18SJames Chapman "%s: closing session\n", session->name); 1314fd558d18SJames Chapman 1315fd558d18SJames Chapman hlist_del_init(&session->hlist); 1316fd558d18SJames Chapman 1317fd558d18SJames Chapman if (session->ref != NULL) 1318fd558d18SJames Chapman (*session->ref)(session); 1319fd558d18SJames Chapman 1320fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1321fd558d18SJames Chapman 1322f6e16b29STom Parkin __l2tp_session_unhash(session); 13234c6e2fd3STom Parkin l2tp_session_queue_purge(session); 13244c6e2fd3STom Parkin 1325fd558d18SJames Chapman if (session->session_close != NULL) 1326fd558d18SJames Chapman (*session->session_close)(session); 1327fd558d18SJames Chapman 1328fd558d18SJames Chapman if (session->deref != NULL) 1329fd558d18SJames Chapman (*session->deref)(session); 1330fd558d18SJames Chapman 13319980d001STom Parkin l2tp_session_dec_refcount(session); 13329980d001STom Parkin 1333fd558d18SJames Chapman write_lock_bh(&tunnel->hlist_lock); 1334fd558d18SJames Chapman 1335fd558d18SJames Chapman /* Now restart from the beginning of this hash 1336fd558d18SJames Chapman * chain. We always remove a session from the 1337fd558d18SJames Chapman * list so we are guaranteed to make forward 1338fd558d18SJames Chapman * progress. 1339fd558d18SJames Chapman */ 1340fd558d18SJames Chapman goto again; 1341fd558d18SJames Chapman } 1342fd558d18SJames Chapman } 1343fd558d18SJames Chapman write_unlock_bh(&tunnel->hlist_lock); 1344fd558d18SJames Chapman } 1345e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); 1346fd558d18SJames Chapman 13479980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */ 13489980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk) 13499980d001STom Parkin { 13509980d001STom Parkin struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 13519980d001STom Parkin if (tunnel) { 13529980d001STom Parkin l2tp_tunnel_closeall(tunnel); 13539980d001STom Parkin sock_put(sk); 13549980d001STom Parkin } 13559980d001STom Parkin } 13569980d001STom Parkin 1357f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */ 1358f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work) 1359f8ccac0eSTom Parkin { 1360f8ccac0eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1361f8ccac0eSTom Parkin struct socket *sock = NULL; 1362f8ccac0eSTom Parkin struct sock *sk = NULL; 1363f8ccac0eSTom Parkin 1364f8ccac0eSTom Parkin tunnel = container_of(work, struct l2tp_tunnel, del_work); 136512d656afSRidge Kennedy 136612d656afSRidge Kennedy l2tp_tunnel_closeall(tunnel); 136712d656afSRidge Kennedy 1368f8ccac0eSTom Parkin sk = l2tp_tunnel_sock_lookup(tunnel); 1369f8ccac0eSTom Parkin if (!sk) 137006a15f51SAlexander Couzens goto out; 1371f8ccac0eSTom Parkin 1372f8ccac0eSTom Parkin sock = sk->sk_socket; 1373f8ccac0eSTom Parkin 137402d13ed5STom Parkin /* If the tunnel socket was created by userspace, then go through the 137502d13ed5STom Parkin * inet layer to shut the socket down, and let userspace close it. 137602d13ed5STom Parkin * Otherwise, if we created the socket directly within the kernel, use 137702d13ed5STom Parkin * the sk API to release it here. 1378167eb17eSTom Parkin * In either case the tunnel resources are freed in the socket 1379167eb17eSTom Parkin * destructor when the tunnel socket goes away. 1380f8ccac0eSTom Parkin */ 138102d13ed5STom Parkin if (tunnel->fd >= 0) { 138202d13ed5STom Parkin if (sock) 138302d13ed5STom Parkin inet_shutdown(sock, 2); 138402d13ed5STom Parkin } else { 138526abe143SEric W. Biederman if (sock) { 1386167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 138726abe143SEric W. Biederman sock_release(sock); 138826abe143SEric W. Biederman } 1389167eb17eSTom Parkin } 1390f8ccac0eSTom Parkin 1391f8ccac0eSTom Parkin l2tp_tunnel_sock_put(sk); 139206a15f51SAlexander Couzens out: 139306a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 1394fd558d18SJames Chapman } 1395fd558d18SJames Chapman 1396789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by 1397789a4a2cSJames Chapman * userspace. This is used for static tunnels where there is no 1398789a4a2cSJames Chapman * managing L2TP daemon. 1399167eb17eSTom Parkin * 1400167eb17eSTom Parkin * Since we don't want these sockets to keep a namespace alive by 1401167eb17eSTom Parkin * themselves, we drop the socket's namespace refcount after creation. 1402167eb17eSTom Parkin * These sockets are freed when the namespace exits using the pernet 1403167eb17eSTom Parkin * exit hook. 1404789a4a2cSJames Chapman */ 1405167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net, 1406167eb17eSTom Parkin u32 tunnel_id, 1407167eb17eSTom Parkin u32 peer_tunnel_id, 1408167eb17eSTom Parkin struct l2tp_tunnel_cfg *cfg, 1409167eb17eSTom Parkin struct socket **sockp) 1410789a4a2cSJames Chapman { 1411789a4a2cSJames Chapman int err = -EINVAL; 14127bddd0dbSEric Dumazet struct socket *sock = NULL; 141385644b4dSTom Herbert struct udp_port_cfg udp_conf; 1414789a4a2cSJames Chapman 1415789a4a2cSJames Chapman switch (cfg->encap) { 1416789a4a2cSJames Chapman case L2TP_ENCAPTYPE_UDP: 141785644b4dSTom Herbert memset(&udp_conf, 0, sizeof(udp_conf)); 141885644b4dSTom Herbert 1419f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1420f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 142185644b4dSTom Herbert udp_conf.family = AF_INET6; 142285644b4dSTom Herbert memcpy(&udp_conf.local_ip6, cfg->local_ip6, 142385644b4dSTom Herbert sizeof(udp_conf.local_ip6)); 142485644b4dSTom Herbert memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 142585644b4dSTom Herbert sizeof(udp_conf.peer_ip6)); 142685644b4dSTom Herbert udp_conf.use_udp6_tx_checksums = 1427018f8258SWang Shanker ! cfg->udp6_zero_tx_checksums; 142885644b4dSTom Herbert udp_conf.use_udp6_rx_checksums = 1429018f8258SWang Shanker ! cfg->udp6_zero_rx_checksums; 1430f9bac8dfSChris Elston } else 1431f9bac8dfSChris Elston #endif 1432f9bac8dfSChris Elston { 143385644b4dSTom Herbert udp_conf.family = AF_INET; 143485644b4dSTom Herbert udp_conf.local_ip = cfg->local_ip; 143585644b4dSTom Herbert udp_conf.peer_ip = cfg->peer_ip; 143685644b4dSTom Herbert udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1437f9bac8dfSChris Elston } 1438789a4a2cSJames Chapman 143985644b4dSTom Herbert udp_conf.local_udp_port = htons(cfg->local_udp_port); 144085644b4dSTom Herbert udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 144185644b4dSTom Herbert 144285644b4dSTom Herbert err = udp_sock_create(net, &udp_conf, &sock); 144385644b4dSTom Herbert if (err < 0) 144485644b4dSTom Herbert goto out; 1445789a4a2cSJames Chapman 1446789a4a2cSJames Chapman break; 1447789a4a2cSJames Chapman 1448789a4a2cSJames Chapman case L2TP_ENCAPTYPE_IP: 1449f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 1450f9bac8dfSChris Elston if (cfg->local_ip6 && cfg->peer_ip6) { 145185644b4dSTom Herbert struct sockaddr_l2tpip6 ip6_addr = {0}; 145285644b4dSTom Herbert 145326abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1454167eb17eSTom Parkin IPPROTO_L2TP, &sock); 14555dac94e1SJames Chapman if (err < 0) 1456f9bac8dfSChris Elston goto out; 14575dac94e1SJames Chapman 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 { 147985644b4dSTom Herbert struct sockaddr_l2tpip ip_addr = {0}; 148085644b4dSTom Herbert 148126abe143SEric W. Biederman err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1482167eb17eSTom Parkin IPPROTO_L2TP, &sock); 1483789a4a2cSJames Chapman if (err < 0) 1484789a4a2cSJames Chapman goto out; 1485789a4a2cSJames Chapman 1486789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1487789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->local_ip; 1488789a4a2cSJames Chapman ip_addr.l2tp_conn_id = tunnel_id; 14895dac94e1SJames Chapman err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 14905dac94e1SJames Chapman sizeof(ip_addr)); 1491789a4a2cSJames Chapman if (err < 0) 1492789a4a2cSJames Chapman goto out; 1493789a4a2cSJames Chapman 1494789a4a2cSJames Chapman ip_addr.l2tp_family = AF_INET; 1495789a4a2cSJames Chapman ip_addr.l2tp_addr = cfg->peer_ip; 1496789a4a2cSJames Chapman ip_addr.l2tp_conn_id = peer_tunnel_id; 14975dac94e1SJames Chapman err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 14985dac94e1SJames Chapman sizeof(ip_addr), 0); 1499789a4a2cSJames Chapman if (err < 0) 1500789a4a2cSJames Chapman goto out; 15015dac94e1SJames Chapman } 1502789a4a2cSJames Chapman break; 1503789a4a2cSJames Chapman 1504789a4a2cSJames Chapman default: 1505789a4a2cSJames Chapman goto out; 1506789a4a2cSJames Chapman } 1507789a4a2cSJames Chapman 1508789a4a2cSJames Chapman out: 1509167eb17eSTom Parkin *sockp = sock; 1510789a4a2cSJames Chapman if ((err < 0) && sock) { 1511167eb17eSTom Parkin kernel_sock_shutdown(sock, SHUT_RDWR); 151226abe143SEric W. Biederman sock_release(sock); 1513789a4a2cSJames Chapman *sockp = NULL; 1514789a4a2cSJames Chapman } 1515789a4a2cSJames Chapman 1516789a4a2cSJames Chapman return err; 1517789a4a2cSJames Chapman } 1518789a4a2cSJames Chapman 151937159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class; 152037159ef2SEric Dumazet 1521fd558d18SJames 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) 1522fd558d18SJames Chapman { 1523fd558d18SJames Chapman struct l2tp_tunnel *tunnel = NULL; 1524fd558d18SJames Chapman int err; 1525fd558d18SJames Chapman struct socket *sock = NULL; 1526fd558d18SJames Chapman struct sock *sk = NULL; 1527fd558d18SJames Chapman struct l2tp_net *pn; 15280d76751fSJames Chapman enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1529fd558d18SJames Chapman 1530fd558d18SJames Chapman /* Get the tunnel socket from the fd, which was opened by 1531789a4a2cSJames Chapman * the userspace L2TP daemon. If not specified, create a 1532789a4a2cSJames Chapman * kernel socket. 1533fd558d18SJames Chapman */ 1534789a4a2cSJames Chapman if (fd < 0) { 1535167eb17eSTom Parkin err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, 1536167eb17eSTom Parkin cfg, &sock); 1537789a4a2cSJames Chapman if (err < 0) 1538789a4a2cSJames Chapman goto err; 1539789a4a2cSJames Chapman } else { 1540fd558d18SJames Chapman sock = sockfd_lookup(fd, &err); 1541fd558d18SJames Chapman if (!sock) { 1542cbb95e0cSTom Parkin pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", 1543fd558d18SJames Chapman tunnel_id, fd, err); 1544cbb95e0cSTom Parkin err = -EBADF; 1545cbb95e0cSTom Parkin goto err; 1546cbb95e0cSTom Parkin } 1547cbb95e0cSTom Parkin 1548cbb95e0cSTom Parkin /* Reject namespace mismatches */ 1549cbb95e0cSTom Parkin if (!net_eq(sock_net(sock->sk), net)) { 1550cbb95e0cSTom Parkin pr_err("tunl %u: netns mismatch\n", tunnel_id); 1551cbb95e0cSTom Parkin err = -EINVAL; 1552fd558d18SJames Chapman goto err; 1553fd558d18SJames Chapman } 1554789a4a2cSJames Chapman } 1555fd558d18SJames Chapman 1556fd558d18SJames Chapman sk = sock->sk; 1557fd558d18SJames Chapman 15580d76751fSJames Chapman if (cfg != NULL) 15590d76751fSJames Chapman encap = cfg->encap; 15600d76751fSJames Chapman 1561fd558d18SJames Chapman /* Quick sanity checks */ 15620d76751fSJames Chapman switch (encap) { 15630d76751fSJames Chapman case L2TP_ENCAPTYPE_UDP: 1564fd558d18SJames Chapman err = -EPROTONOSUPPORT; 1565fd558d18SJames Chapman if (sk->sk_protocol != IPPROTO_UDP) { 1566a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1567fd558d18SJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1568fd558d18SJames Chapman goto err; 1569fd558d18SJames Chapman } 15700d76751fSJames Chapman break; 15710d76751fSJames Chapman case L2TP_ENCAPTYPE_IP: 15720d76751fSJames Chapman err = -EPROTONOSUPPORT; 15730d76751fSJames Chapman if (sk->sk_protocol != IPPROTO_L2TP) { 1574a4ca44faSJoe Perches pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 15750d76751fSJames Chapman tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1576fd558d18SJames Chapman goto err; 1577fd558d18SJames Chapman } 15780d76751fSJames Chapman break; 15790d76751fSJames Chapman } 1580fd558d18SJames Chapman 1581fd558d18SJames Chapman /* Check if this socket has already been prepped */ 15828d8a51e2SDavid S. Miller tunnel = l2tp_tunnel(sk); 1583fd558d18SJames Chapman if (tunnel != NULL) { 1584fd558d18SJames Chapman /* This socket has already been prepped */ 1585fd558d18SJames Chapman err = -EBUSY; 1586fd558d18SJames Chapman goto err; 1587fd558d18SJames Chapman } 1588fd558d18SJames Chapman 1589fd558d18SJames Chapman tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1590fd558d18SJames Chapman if (tunnel == NULL) { 1591fd558d18SJames Chapman err = -ENOMEM; 1592fd558d18SJames Chapman goto err; 1593fd558d18SJames Chapman } 1594fd558d18SJames Chapman 1595fd558d18SJames Chapman tunnel->version = version; 1596fd558d18SJames Chapman tunnel->tunnel_id = tunnel_id; 1597fd558d18SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 1598fd558d18SJames Chapman tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1599fd558d18SJames Chapman 1600fd558d18SJames Chapman tunnel->magic = L2TP_TUNNEL_MAGIC; 1601fd558d18SJames Chapman sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1602fd558d18SJames Chapman rwlock_init(&tunnel->hlist_lock); 1603f3c66d4eSGuillaume Nault tunnel->acpt_newsess = true; 1604fd558d18SJames Chapman 1605fd558d18SJames Chapman /* The net we belong to */ 1606fd558d18SJames Chapman tunnel->l2tp_net = net; 1607fd558d18SJames Chapman pn = l2tp_pernet(net); 1608fd558d18SJames Chapman 16090d76751fSJames Chapman if (cfg != NULL) 1610fd558d18SJames Chapman tunnel->debug = cfg->debug; 1611fd558d18SJames Chapman 1612e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6) 1613e18503f4SFrançois Cachereul if (sk->sk_family == PF_INET6) { 1614e18503f4SFrançois Cachereul struct ipv6_pinfo *np = inet6_sk(sk); 1615e18503f4SFrançois Cachereul 1616e18503f4SFrançois Cachereul if (ipv6_addr_v4mapped(&np->saddr) && 1617efe4208fSEric Dumazet ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 1618e18503f4SFrançois Cachereul struct inet_sock *inet = inet_sk(sk); 1619e18503f4SFrançois Cachereul 1620e18503f4SFrançois Cachereul tunnel->v4mapped = true; 1621e18503f4SFrançois Cachereul inet->inet_saddr = np->saddr.s6_addr32[3]; 1622efe4208fSEric Dumazet inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3]; 1623efe4208fSEric Dumazet inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3]; 1624e18503f4SFrançois Cachereul } else { 1625e18503f4SFrançois Cachereul tunnel->v4mapped = false; 1626e18503f4SFrançois Cachereul } 1627e18503f4SFrançois Cachereul } 1628e18503f4SFrançois Cachereul #endif 1629e18503f4SFrançois Cachereul 1630fd558d18SJames Chapman /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 16310d76751fSJames Chapman tunnel->encap = encap; 16320d76751fSJames Chapman if (encap == L2TP_ENCAPTYPE_UDP) { 1633a5c5e2daSGuillaume Nault struct udp_tunnel_sock_cfg udp_cfg = { }; 1634fd558d18SJames Chapman 1635c8fffceaSAndy Zhou udp_cfg.sk_user_data = tunnel; 1636c8fffceaSAndy Zhou udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP; 1637c8fffceaSAndy Zhou udp_cfg.encap_rcv = l2tp_udp_encap_recv; 1638c8fffceaSAndy Zhou udp_cfg.encap_destroy = l2tp_udp_encap_destroy; 1639c8fffceaSAndy Zhou 1640c8fffceaSAndy Zhou setup_udp_tunnel_sock(net, sock, &udp_cfg); 1641c8fffceaSAndy Zhou } else { 1642fd558d18SJames Chapman sk->sk_user_data = tunnel; 1643c8fffceaSAndy Zhou } 1644fd558d18SJames Chapman 1645fd558d18SJames Chapman /* Hook on the tunnel socket destructor so that we can cleanup 1646fd558d18SJames Chapman * if the tunnel socket goes away. 1647fd558d18SJames Chapman */ 1648fd558d18SJames Chapman tunnel->old_sk_destruct = sk->sk_destruct; 1649fd558d18SJames Chapman sk->sk_destruct = &l2tp_tunnel_destruct; 1650fd558d18SJames Chapman tunnel->sock = sk; 165180d84ef3STom Parkin tunnel->fd = fd; 165237159ef2SEric Dumazet lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); 165337159ef2SEric Dumazet 1654fd558d18SJames Chapman sk->sk_allocation = GFP_ATOMIC; 1655fd558d18SJames Chapman 1656f8ccac0eSTom Parkin /* Init delete workqueue struct */ 1657f8ccac0eSTom Parkin INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1658f8ccac0eSTom Parkin 1659fd558d18SJames Chapman /* Add tunnel to our list */ 1660fd558d18SJames Chapman INIT_LIST_HEAD(&tunnel->list); 1661fd558d18SJames Chapman atomic_inc(&l2tp_tunnel_count); 1662fd558d18SJames Chapman 1663fd558d18SJames Chapman /* Bump the reference count. The tunnel context is deleted 16641769192aSEric Dumazet * only when this drops to zero. Must be done before list insertion 1665fd558d18SJames Chapman */ 1666fbea9e07SReshetova, Elena refcount_set(&tunnel->ref_count, 1); 16671769192aSEric Dumazet spin_lock_bh(&pn->l2tp_tunnel_list_lock); 16681769192aSEric Dumazet list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 16691769192aSEric Dumazet spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1670fd558d18SJames Chapman 1671fd558d18SJames Chapman err = 0; 1672fd558d18SJames Chapman err: 1673fd558d18SJames Chapman if (tunnelp) 1674fd558d18SJames Chapman *tunnelp = tunnel; 1675fd558d18SJames Chapman 1676789a4a2cSJames Chapman /* If tunnel's socket was created by the kernel, it doesn't 1677789a4a2cSJames Chapman * have a file. 1678789a4a2cSJames Chapman */ 1679789a4a2cSJames Chapman if (sock && sock->file) 1680fd558d18SJames Chapman sockfd_put(sock); 1681fd558d18SJames Chapman 1682fd558d18SJames Chapman return err; 1683fd558d18SJames Chapman } 1684fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1685fd558d18SJames Chapman 1686309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command. 1687309795f4SJames Chapman */ 1688309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1689309795f4SJames Chapman { 169006a15f51SAlexander Couzens l2tp_tunnel_inc_refcount(tunnel); 169106a15f51SAlexander Couzens if (false == queue_work(l2tp_wq, &tunnel->del_work)) { 169206a15f51SAlexander Couzens l2tp_tunnel_dec_refcount(tunnel); 169306a15f51SAlexander Couzens return 1; 169406a15f51SAlexander Couzens } 169506a15f51SAlexander Couzens return 0; 1696309795f4SJames Chapman } 1697309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1698309795f4SJames Chapman 1699fd558d18SJames Chapman /* Really kill the session. 1700fd558d18SJames Chapman */ 1701fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session) 1702fd558d18SJames Chapman { 1703f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1704fd558d18SJames Chapman 1705fbea9e07SReshetova, Elena BUG_ON(refcount_read(&session->ref_count) != 0); 1706fd558d18SJames Chapman 1707f6e16b29STom Parkin if (tunnel) { 1708fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1709fd558d18SJames Chapman if (session->session_id != 0) 1710fd558d18SJames Chapman atomic_dec(&l2tp_session_count); 1711fd558d18SJames Chapman sock_put(tunnel->sock); 1712fd558d18SJames Chapman session->tunnel = NULL; 1713fd558d18SJames Chapman l2tp_tunnel_dec_refcount(tunnel); 1714fd558d18SJames Chapman } 1715fd558d18SJames Chapman 1716fd558d18SJames Chapman kfree(session); 1717fd558d18SJames Chapman } 1718fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free); 1719fd558d18SJames Chapman 1720f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists. 1721f6e16b29STom Parkin * Provides a tidyup interface for pseudowire code which can't just route all 1722f6e16b29STom Parkin * shutdown via. l2tp_session_delete and a pseudowire-specific session_close 1723f6e16b29STom Parkin * callback. 1724f6e16b29STom Parkin */ 1725f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session) 1726f6e16b29STom Parkin { 1727f6e16b29STom Parkin struct l2tp_tunnel *tunnel = session->tunnel; 1728f6e16b29STom Parkin 1729f6e16b29STom Parkin /* Remove the session from core hashes */ 1730f6e16b29STom Parkin if (tunnel) { 1731f6e16b29STom Parkin /* Remove from the per-tunnel hash */ 1732f6e16b29STom Parkin write_lock_bh(&tunnel->hlist_lock); 1733f6e16b29STom Parkin hlist_del_init(&session->hlist); 1734f6e16b29STom Parkin write_unlock_bh(&tunnel->hlist_lock); 1735f6e16b29STom Parkin 1736f6e16b29STom Parkin /* For L2TPv3 we have a per-net hash: remove from there, too */ 1737f6e16b29STom Parkin if (tunnel->version != L2TP_HDR_VER_2) { 1738f6e16b29STom Parkin struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1739f6e16b29STom Parkin spin_lock_bh(&pn->l2tp_session_hlist_lock); 1740f6e16b29STom Parkin hlist_del_init_rcu(&session->global_hlist); 1741f6e16b29STom Parkin spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1742f6e16b29STom Parkin synchronize_rcu(); 1743f6e16b29STom Parkin } 1744f6e16b29STom Parkin } 1745f6e16b29STom Parkin } 1746f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash); 1747f6e16b29STom Parkin 1748309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by 1749309795f4SJames Chapman pseudowire modules. 1750309795f4SJames Chapman */ 1751309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session) 1752309795f4SJames Chapman { 1753f6e16b29STom Parkin if (session->ref) 1754f6e16b29STom Parkin (*session->ref)(session); 1755f6e16b29STom Parkin __l2tp_session_unhash(session); 17564c6e2fd3STom Parkin l2tp_session_queue_purge(session); 1757309795f4SJames Chapman if (session->session_close != NULL) 1758309795f4SJames Chapman (*session->session_close)(session); 1759f6e16b29STom Parkin if (session->deref) 17601b7c92b9SDan Carpenter (*session->deref)(session); 1761309795f4SJames Chapman l2tp_session_dec_refcount(session); 1762309795f4SJames Chapman return 0; 1763309795f4SJames Chapman } 1764309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete); 1765309795f4SJames Chapman 1766f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or 1767f7faffa3SJames Chapman * l2specific_len parameters are set. 1768f7faffa3SJames Chapman */ 1769bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1770f7faffa3SJames Chapman { 1771f7faffa3SJames Chapman if (version == L2TP_HDR_VER_2) { 1772f7faffa3SJames Chapman session->hdr_len = 6; 1773f7faffa3SJames Chapman if (session->send_seq) 1774f7faffa3SJames Chapman session->hdr_len += 4; 1775f7faffa3SJames Chapman } else { 17760d76751fSJames Chapman session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 17770d76751fSJames Chapman if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 17780d76751fSJames Chapman session->hdr_len += 4; 1779f7faffa3SJames Chapman } 1780f7faffa3SJames Chapman 1781f7faffa3SJames Chapman } 1782bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1783f7faffa3SJames Chapman 1784fd558d18SJames 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) 1785fd558d18SJames Chapman { 1786fd558d18SJames Chapman struct l2tp_session *session; 1787dbdbc73bSGuillaume Nault int err; 1788fd558d18SJames Chapman 1789fd558d18SJames Chapman session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1790fd558d18SJames Chapman if (session != NULL) { 1791fd558d18SJames Chapman session->magic = L2TP_SESSION_MAGIC; 1792fd558d18SJames Chapman session->tunnel = tunnel; 1793fd558d18SJames Chapman 1794fd558d18SJames Chapman session->session_id = session_id; 1795fd558d18SJames Chapman session->peer_session_id = peer_session_id; 1796d301e325SJames Chapman session->nr = 0; 17978a1631d5SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 17988a1631d5SJames Chapman session->nr_max = 0xffff; 17998a1631d5SJames Chapman else 18008a1631d5SJames Chapman session->nr_max = 0xffffff; 18018a1631d5SJames Chapman session->nr_window_size = session->nr_max / 2; 1802a0dbd822SJames Chapman session->nr_oos_count_max = 4; 1803a0dbd822SJames Chapman 1804a0dbd822SJames Chapman /* Use NR of first received packet */ 1805a0dbd822SJames Chapman session->reorder_skip = 1; 1806fd558d18SJames Chapman 1807fd558d18SJames Chapman sprintf(&session->name[0], "sess %u/%u", 1808fd558d18SJames Chapman tunnel->tunnel_id, session->session_id); 1809fd558d18SJames Chapman 1810fd558d18SJames Chapman skb_queue_head_init(&session->reorder_q); 1811fd558d18SJames Chapman 1812fd558d18SJames Chapman INIT_HLIST_NODE(&session->hlist); 1813f7faffa3SJames Chapman INIT_HLIST_NODE(&session->global_hlist); 1814fd558d18SJames Chapman 1815fd558d18SJames Chapman /* Inherit debug options from tunnel */ 1816fd558d18SJames Chapman session->debug = tunnel->debug; 1817fd558d18SJames Chapman 1818fd558d18SJames Chapman if (cfg) { 1819f7faffa3SJames Chapman session->pwtype = cfg->pw_type; 1820fd558d18SJames Chapman session->debug = cfg->debug; 1821fd558d18SJames Chapman session->mtu = cfg->mtu; 1822fd558d18SJames Chapman session->mru = cfg->mru; 1823fd558d18SJames Chapman session->send_seq = cfg->send_seq; 1824fd558d18SJames Chapman session->recv_seq = cfg->recv_seq; 1825fd558d18SJames Chapman session->lns_mode = cfg->lns_mode; 1826f7faffa3SJames Chapman session->reorder_timeout = cfg->reorder_timeout; 1827f7faffa3SJames Chapman session->offset = cfg->offset; 1828f7faffa3SJames Chapman session->l2specific_type = cfg->l2specific_type; 1829f7faffa3SJames Chapman session->l2specific_len = cfg->l2specific_len; 1830f7faffa3SJames Chapman session->cookie_len = cfg->cookie_len; 1831f7faffa3SJames Chapman memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1832f7faffa3SJames Chapman session->peer_cookie_len = cfg->peer_cookie_len; 1833f7faffa3SJames Chapman memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1834fd558d18SJames Chapman } 1835fd558d18SJames Chapman 1836f7faffa3SJames Chapman if (tunnel->version == L2TP_HDR_VER_2) 1837f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv2_header; 1838f7faffa3SJames Chapman else 1839f7faffa3SJames Chapman session->build_header = l2tp_build_l2tpv3_header; 1840f7faffa3SJames Chapman 1841f7faffa3SJames Chapman l2tp_session_set_header_len(session, tunnel->version); 1842f7faffa3SJames Chapman 18439ee369a4SGuillaume Nault refcount_set(&session->ref_count, 1); 18449ee369a4SGuillaume Nault 1845dbdbc73bSGuillaume Nault err = l2tp_session_add_to_tunnel(tunnel, session); 1846dbdbc73bSGuillaume Nault if (err) { 1847dbdbc73bSGuillaume Nault kfree(session); 1848dbdbc73bSGuillaume Nault 1849dbdbc73bSGuillaume Nault return ERR_PTR(err); 1850dbdbc73bSGuillaume Nault } 1851dbdbc73bSGuillaume Nault 1852fd558d18SJames Chapman /* Ignore management session in session count value */ 1853fd558d18SJames Chapman if (session->session_id != 0) 1854fd558d18SJames Chapman atomic_inc(&l2tp_session_count); 1855fd558d18SJames Chapman 1856fd558d18SJames Chapman return session; 1857fd558d18SJames Chapman } 1858dbdbc73bSGuillaume Nault 1859dbdbc73bSGuillaume Nault return ERR_PTR(-ENOMEM); 1860dbdbc73bSGuillaume Nault } 1861fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create); 1862fd558d18SJames Chapman 1863fd558d18SJames Chapman /***************************************************************************** 1864fd558d18SJames Chapman * Init and cleanup 1865fd558d18SJames Chapman *****************************************************************************/ 1866fd558d18SJames Chapman 1867fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net) 1868fd558d18SJames Chapman { 1869e773aaffSJiri Pirko struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1870f7faffa3SJames Chapman int hash; 1871fd558d18SJames Chapman 1872fd558d18SJames Chapman INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1873e02d494dSJames Chapman spin_lock_init(&pn->l2tp_tunnel_list_lock); 1874fd558d18SJames Chapman 1875f7faffa3SJames Chapman for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1876f7faffa3SJames Chapman INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1877f7faffa3SJames Chapman 1878e02d494dSJames Chapman spin_lock_init(&pn->l2tp_session_hlist_lock); 1879f7faffa3SJames Chapman 1880fd558d18SJames Chapman return 0; 1881fd558d18SJames Chapman } 1882fd558d18SJames Chapman 1883167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net) 1884167eb17eSTom Parkin { 1885167eb17eSTom Parkin struct l2tp_net *pn = l2tp_pernet(net); 1886167eb17eSTom Parkin struct l2tp_tunnel *tunnel = NULL; 1887167eb17eSTom Parkin 1888167eb17eSTom Parkin rcu_read_lock_bh(); 1889167eb17eSTom Parkin list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 1890167eb17eSTom Parkin (void)l2tp_tunnel_delete(tunnel); 1891167eb17eSTom Parkin } 1892167eb17eSTom Parkin rcu_read_unlock_bh(); 18932f86953eSSabrina Dubroca 18942f86953eSSabrina Dubroca flush_workqueue(l2tp_wq); 18952f86953eSSabrina Dubroca rcu_barrier(); 1896167eb17eSTom Parkin } 1897167eb17eSTom Parkin 1898fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = { 1899fd558d18SJames Chapman .init = l2tp_init_net, 1900167eb17eSTom Parkin .exit = l2tp_exit_net, 1901fd558d18SJames Chapman .id = &l2tp_net_id, 1902fd558d18SJames Chapman .size = sizeof(struct l2tp_net), 1903fd558d18SJames Chapman }; 1904fd558d18SJames Chapman 1905fd558d18SJames Chapman static int __init l2tp_init(void) 1906fd558d18SJames Chapman { 1907fd558d18SJames Chapman int rc = 0; 1908fd558d18SJames Chapman 1909fd558d18SJames Chapman rc = register_pernet_device(&l2tp_net_ops); 1910fd558d18SJames Chapman if (rc) 1911fd558d18SJames Chapman goto out; 1912fd558d18SJames Chapman 191359ff3eb6SZhangZhen l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1914f8ccac0eSTom Parkin if (!l2tp_wq) { 1915f8ccac0eSTom Parkin pr_err("alloc_workqueue failed\n"); 191667e04c29SWANG Cong unregister_pernet_device(&l2tp_net_ops); 1917f8ccac0eSTom Parkin rc = -ENOMEM; 1918f8ccac0eSTom Parkin goto out; 1919f8ccac0eSTom Parkin } 1920f8ccac0eSTom Parkin 1921a4ca44faSJoe Perches pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1922fd558d18SJames Chapman 1923fd558d18SJames Chapman out: 1924fd558d18SJames Chapman return rc; 1925fd558d18SJames Chapman } 1926fd558d18SJames Chapman 1927fd558d18SJames Chapman static void __exit l2tp_exit(void) 1928fd558d18SJames Chapman { 1929fd558d18SJames Chapman unregister_pernet_device(&l2tp_net_ops); 1930f8ccac0eSTom Parkin if (l2tp_wq) { 1931f8ccac0eSTom Parkin destroy_workqueue(l2tp_wq); 1932f8ccac0eSTom Parkin l2tp_wq = NULL; 1933f8ccac0eSTom Parkin } 1934fd558d18SJames Chapman } 1935fd558d18SJames Chapman 1936fd558d18SJames Chapman module_init(l2tp_init); 1937fd558d18SJames Chapman module_exit(l2tp_exit); 1938fd558d18SJames Chapman 1939fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1940fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core"); 1941fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1942fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION); 1943fd558d18SJames Chapman 1944