21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/list.h>
29 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/errno.h>
37 #include <linux/netdevice.h>
38 #include <linux/net.h>
44 #include <linux/udp.h>
45 #include <linux/l2tp.h>
46 #include <linux/hash.h>
63 #include <asm/byteorder.h>
68 #define L2TP_DRV_VERSION "V2.0"
71 #define L2TP_HDRFLAG_T 0x8000
72 #define L2TP_HDRFLAG_L 0x4000
73 #define L2TP_HDRFLAG_S 0x0800
74 #define L2TP_HDRFLAG_O 0x0200
75 #define L2TP_HDRFLAG_P 0x0100
77 #define L2TP_HDR_VER_MASK 0x000F
78 #define L2TP_HDR_VER_2 0x0002
79 #define L2TP_HDR_VER_3 0x0003
82 #define L2TP_SLFLAG_S 0x40000000
83 #define L2TP_SL_SEQ_MASK 0x00ffffff
85 #define L2TP_HDR_SIZE_SEQ 10
86 #define L2TP_HDR_SIZE_NOSEQ 6
89 #define L2TP_DEFAULT_DEBUG_FLAGS 0
100 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
106 static unsigned int l2tp_net_id;
115 static void l2tp_tunnel_free(
struct l2tp_tunnel *tunnel);
116 static void l2tp_tunnel_closeall(
struct l2tp_tunnel *tunnel);
129 static inline void l2tp_tunnel_inc_refcount_1(
struct l2tp_tunnel *tunnel)
134 static inline void l2tp_tunnel_dec_refcount_1(
struct l2tp_tunnel *tunnel)
137 l2tp_tunnel_free(tunnel);
139 #ifdef L2TP_REFCNT_DEBUG
140 #define l2tp_tunnel_inc_refcount(_t) \
142 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
143 __func__, __LINE__, (_t)->name, \
144 atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
147 #define l2tp_tunnel_dec_refcount(_t)
149 pr_debug(
"l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
150 __func__, __LINE__, (_t)->
name, \
152 l2tp_tunnel_dec_refcount_1(_t); \
155 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
156 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
177 l2tp_session_id_hash_2(pn, session_id);
182 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
184 rcu_read_unlock_bh();
188 rcu_read_unlock_bh();
218 return l2tp_session_find_2(net, session_id);
220 session_list = l2tp_session_id_hash(tunnel, session_id);
262 struct l2tp_net *pn = l2tp_pernet(net);
271 rcu_read_unlock_bh();
277 rcu_read_unlock_bh();
288 struct l2tp_net *pn = l2tp_pernet(net);
293 rcu_read_unlock_bh();
297 rcu_read_unlock_bh();
305 struct l2tp_net *pn = l2tp_pernet(net);
312 rcu_read_unlock_bh();
317 rcu_read_unlock_bh();
338 sstats = &session->
stats;
339 skb_queue_walk_safe(&session->
reorder_q, skbp, tmp) {
341 __skb_queue_before(&session->
reorder_q, skbp, skb);
343 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
346 u64_stats_update_begin(&sstats->
syncp);
348 u64_stats_update_end(&sstats->
syncp);
353 __skb_queue_tail(&session->
reorder_q, skb);
356 spin_unlock_bh(&session->
reorder_q.lock);
372 tstats = &tunnel->
stats;
373 u64_stats_update_begin(&tstats->
syncp);
374 sstats = &session->
stats;
375 u64_stats_update_begin(&sstats->
syncp);
380 u64_stats_update_end(&tstats->
syncp);
381 u64_stats_update_end(&sstats->
syncp);
387 session->
nr &= 0xffff;
389 session->
nr &= 0xffffff;
392 session->
name, session->
nr);
402 (*session->
deref)(session);
408 static void l2tp_recv_dequeue(
struct l2tp_session *session)
420 sstats = &session->
stats;
421 skb_queue_walk_safe(&session->
reorder_q, skb, tmp) {
423 u64_stats_update_begin(&sstats->
syncp);
426 u64_stats_update_end(&sstats->
syncp);
428 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
436 (*session->
deref)(session);
443 "%s: advancing nr to next pkt: %u -> %u",
444 session->
name, session->
nr,
451 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
463 spin_unlock_bh(&session->
reorder_q.lock);
464 l2tp_recv_dequeue_skb(session, skb);
469 spin_unlock_bh(&session->
reorder_q.lock);
472 static inline int l2tp_verify_udp_checksum(
struct sock *
sk,
475 struct udphdr *uh = udp_hdr(skb);
482 #if IS_ENABLED(CONFIG_IPV6)
490 &ipv6_hdr(skb)->
daddr, ulen,
496 &ipv6_hdr(skb)->
daddr,
510 !csum_fold(csum_add(psum, skb->
csum)))
579 unsigned char *
ptr,
unsigned char *optr,
u16 hdrflags,
580 int length,
int (*payload_hook)(
struct sk_buff *skb))
593 (*session->
ref)(session);
599 "%s: cookie mismatch (%u/%u). Discarding.\n",
602 u64_stats_update_begin(&sstats->
syncp);
604 u64_stats_update_end(&sstats->
syncp);
631 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
632 session->
name, ns, nr, session->
nr);
637 if (l2h & 0x40000000) {
638 ns = l2h & 0x00ffffff;
645 "%s: recv data ns=%u, session nr=%u\n",
646 session->
name, ns, session->
nr);
660 "%s: requested to enable seq numbers by LNS\n",
663 l2tp_session_set_header_len(session, tunnel->
version);
671 "%s: recv data has no seq numbers when required. Discarding.\n",
673 u64_stats_update_begin(&sstats->
syncp);
675 u64_stats_update_end(&sstats->
syncp);
686 "%s: requested to disable seq numbers by LNS\n",
689 l2tp_session_set_header_len(session, tunnel->
version);
692 "%s: recv data has no seq numbers when required. Discarding.\n",
694 u64_stats_update_begin(&sstats->
syncp);
696 u64_stats_update_end(&sstats->
syncp);
716 if (!pskb_may_pull(skb, offset))
719 __skb_pull(skb, offset);
725 if ((*payload_hook)(skb))
744 l2tp_recv_queue_skb(session, skb);
750 u64_stats_update_begin(&sstats->
syncp);
752 u64_stats_update_end(&sstats->
syncp);
754 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
771 l2tp_recv_dequeue(session);
778 u64_stats_update_begin(&sstats->
syncp);
780 u64_stats_update_end(&sstats->
syncp);
784 (*session->
deref)(session);
797 int (*payload_hook)(
struct sk_buff *skb))
800 unsigned char *
ptr, *optr;
807 if (tunnel->
sock && l2tp_verify_udp_checksum(tunnel->
sock, skb))
808 goto discard_bad_csum;
811 __skb_pull(skb,
sizeof(
struct udphdr));
816 "%s: recv short packet (len=%d)\n",
824 if (!pskb_may_pull(skb, length))
832 optr = ptr = skb->
data;
839 if (version != tunnel->
version) {
841 "%s: recv protocol version mismatch: got %d expected %d\n",
852 "%s: recv control packet, len=%d\n",
853 tunnel->
name, length);
879 if (!session || !session->
recv_skb) {
882 "%s: no session found (%u/%u). Passing up.\n",
883 tunnel->
name, tunnel_id, session_id);
894 tstats = &tunnel->
stats;
895 u64_stats_update_begin(&tstats->
syncp);
897 u64_stats_update_end(&tstats->
syncp);
904 __skb_push(skb,
sizeof(
struct udphdr));
919 tunnel = l2tp_sock_to_tunnel(sk);
945 static int l2tp_build_l2tpv2_header(
struct l2tp_session *session,
void *
buf)
958 *bufp++ =
htons(flags);
959 *bufp++ =
htons(tunnel_id);
960 *bufp++ =
htons(session_id);
965 session->
ns &= 0xffff;
967 session->
name, session->
ns);
973 static int l2tp_build_l2tpv3_header(
struct l2tp_session *session,
void *buf)
1000 l2h = 0x40000000 | session->
ns;
1002 session->
ns &= 0xffffff;
1004 "%s: updated ns to %u\n",
1005 session->
name, session->
ns);
1022 unsigned int len = skb->
len;
1029 session->
name, data_len, session->
ns - 1);
1032 session->
name, data_len);
1036 unsigned char *datap = skb->
data + uhlen;
1040 datap,
min_t(
size_t, 32, len - uhlen));
1045 #if IS_ENABLED(CONFIG_IPV6)
1053 tstats = &tunnel->
stats;
1054 u64_stats_update_begin(&tstats->
syncp);
1055 sstats = &session->
stats;
1056 u64_stats_update_begin(&sstats->
syncp);
1066 u64_stats_update_end(&tstats->
syncp);
1067 u64_stats_update_end(&sstats->
syncp);
1074 static void l2tp_sock_wfree(
struct sk_buff *skb)
1082 static inline void l2tp_skb_set_owner_w(
struct sk_buff *skb,
struct sock *sk)
1089 #if IS_ENABLED(CONFIG_IPV6)
1090 static void l2tp_xmit_ipv6_csum(
struct sock *sk,
struct sk_buff *skb,
1094 struct udphdr *uh = udp_hdr(skb);
1096 if (!skb_dst(skb) || !skb_dst(skb)->
dev ||
1119 int data_len = skb->
len;
1139 old_headroom = skb_headroom(skb);
1140 if (skb_cow_head(skb, headroom)) {
1145 new_headroom = skb_headroom(skb);
1147 skb->
truesize += new_headroom - old_headroom;
1150 session->
build_header(session, __skb_push(skb, hdr_len));
1170 fl = &inet->
cork.fl;
1171 switch (tunnel->
encap) {
1174 __skb_push(skb,
sizeof(*uh));
1175 skb_reset_transport_header(skb);
1179 udp_len = uhlen + hdr_len +
data_len;
1184 #if IS_ENABLED(CONFIG_IPV6)
1186 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1191 else if ((skb_dst(skb) && skb_dst(skb)->
dev) &&
1214 l2tp_skb_set_owner_w(skb, sk);
1216 l2tp_xmit_core(session, skb, fl, data_len);
1232 static void l2tp_tunnel_destruct(
struct sock *sk)
1243 l2tp_tunnel_closeall(tunnel);
1245 switch (tunnel->
encap) {
1248 (udp_sk(sk))->encap_type = 0;
1249 (udp_sk(sk))->encap_rcv =
NULL;
1273 static void l2tp_tunnel_closeall(
struct l2tp_tunnel *tunnel)
1292 "%s: closing session\n", session->
name);
1294 hlist_del_init(&session->
hlist);
1303 (*session->
ref)(session);
1320 (*session->
deref)(session);
1338 static void l2tp_tunnel_free(
struct l2tp_tunnel *tunnel)
1349 list_del_rcu(&tunnel->
list);
1364 #if IS_ENABLED(CONFIG_IPV6)
1371 switch (cfg->
encap) {
1373 #if IS_ENABLED(CONFIG_IPV6)
1374 if (cfg->local_ip6 && cfg->peer_ip6) {
1381 memset(&udp6_addr, 0,
sizeof(udp6_addr));
1383 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1384 sizeof(udp6_addr.sin6_addr));
1392 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1393 sizeof(udp6_addr.sin6_addr));
1397 sizeof(udp6_addr), 0);
1409 memset(&udp_addr, 0,
sizeof(udp_addr));
1410 udp_addr.sin_family =
AF_INET;
1418 udp_addr.sin_family =
AF_INET;
1419 udp_addr.sin_addr = cfg->
peer_ip;
1423 sizeof(udp_addr), 0);
1434 #if IS_ENABLED(CONFIG_IPV6)
1435 if (cfg->local_ip6 && cfg->peer_ip6) {
1443 memset(&ip6_addr, 0,
sizeof(ip6_addr));
1445 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1446 sizeof(ip6_addr.l2tp_addr));
1447 ip6_addr.l2tp_conn_id = tunnel_id;
1454 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1455 sizeof(ip6_addr.l2tp_addr));
1456 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1459 sizeof(ip6_addr), 0);
1475 ip_addr.l2tp_conn_id = tunnel_id;
1483 ip_addr.l2tp_conn_id = peer_tunnel_id;
1496 if ((err < 0) && sock) {
1511 struct sock *sk =
NULL;
1520 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1527 pr_err(
"tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1528 tunnel_id, fd, err);
1543 pr_err(
"tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1551 pr_err(
"tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1567 if (tunnel ==
NULL) {
1583 pn = l2tp_pernet(net);
1594 #if IS_ENABLED(CONFIG_IPV6)
1615 INIT_LIST_HEAD(&tunnel->
list);
1634 if (sock && sock->
file)
1675 tunnel = session->
tunnel;
1676 if (tunnel !=
NULL) {
1681 hlist_del_init(&session->
hlist);
1697 sock_put(tunnel->
sock);
1730 static void l2tp_session_set_header_len(
struct l2tp_session *session,
int version)
1749 if (session !=
NULL) {
1760 skb_queue_head_init(&session->
reorder_q);
1762 INIT_HLIST_NODE(&session->
hlist);
1791 l2tp_session_set_header_len(session, tunnel->
version);
1800 sock_hold(tunnel->
sock);
1804 hlist_add_head(&session->
hlist,
1805 l2tp_session_id_hash(tunnel, session_id));
1814 l2tp_session_id_hash_2(pn, session_id));
1831 static __net_init int l2tp_init_net(
struct net *net)
1848 .init = l2tp_init_net,
1853 static int __init l2tp_init(
void)
1867 static void __exit l2tp_exit(
void)