1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * L2TPv3 IP encapsulation support
5 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <asm/ioctls.h>
11 #include <linux/icmp.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/random.h>
15 #include <linux/socket.h>
16 #include <linux/l2tp.h>
22 #include <net/inet_common.h>
23 #include <net/tcp_states.h>
24 #include <net/protocol.h>
27 #include "l2tp_core.h"
30 /* inet_sock has to be the first member of l2tp_ip_sock */
31 struct inet_sock inet;
37 static DEFINE_RWLOCK(l2tp_ip_lock);
38 static struct hlist_head l2tp_ip_table;
39 static struct hlist_head l2tp_ip_bind_table;
41 static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
43 return (struct l2tp_ip_sock *)sk;
46 static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr,
47 __be32 raddr, int dif, u32 tunnel_id)
51 sk_for_each_bound(sk, &l2tp_ip_bind_table) {
52 const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
53 const struct inet_sock *inet = inet_sk(sk);
55 if (!net_eq(sock_net(sk), net))
58 if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif)
61 if (inet->inet_rcv_saddr && laddr &&
62 inet->inet_rcv_saddr != laddr)
65 if (inet->inet_daddr && raddr && inet->inet_daddr != raddr)
68 if (l2tp->conn_id != tunnel_id)
79 /* When processing receive frames, there are two cases to
80 * consider. Data frames consist of a non-zero session-id and an
81 * optional cookie. Control frames consist of a regular L2TP header
82 * preceded by 32-bits of zeros.
84 * L2TPv3 Session Header Over IP
87 * 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
88 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | Cookie (optional, maximum 64 bits)...
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 * L2TPv3 Control Message Header Over IP
99 * 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
100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * | (32 bits of zeros) |
102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 * |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Control Connection ID |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 * All control frames are passed to userspace.
112 static int l2tp_ip_recv(struct sk_buff *skb)
114 struct net *net = dev_net(skb->dev);
118 unsigned char *ptr, *optr;
119 struct l2tp_session *session;
120 struct l2tp_tunnel *tunnel = NULL;
124 if (!pskb_may_pull(skb, 4))
127 /* Point to L2TP header */
128 optr = ptr = skb->data;
129 session_id = ntohl(*((__be32 *) ptr));
132 /* RFC3931: L2TP/IP packets have the first 4 bytes containing
133 * the session_id. If it is 0, the packet is a L2TP control
134 * frame and the session_id value can be discarded.
136 if (session_id == 0) {
141 /* Ok, this is a data packet. Lookup the session. */
142 session = l2tp_session_get(net, session_id);
146 tunnel = session->tunnel;
150 /* Trace packet contents, if enabled */
151 if (tunnel->debug & L2TP_MSG_DATA) {
152 length = min(32u, skb->len);
153 if (!pskb_may_pull(skb, length))
156 /* Point to L2TP header */
157 optr = ptr = skb->data;
159 pr_debug("%s: ip recv\n", tunnel->name);
160 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
163 if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
166 l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
167 l2tp_session_dec_refcount(session);
172 /* Get the tunnel_id from the L2TP header */
173 if (!pskb_may_pull(skb, 12))
176 if ((skb->data[0] & 0xc0) != 0xc0)
179 tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
180 iph = (struct iphdr *)skb_network_header(skb);
182 read_lock_bh(&l2tp_ip_lock);
183 sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb),
186 read_unlock_bh(&l2tp_ip_lock);
190 read_unlock_bh(&l2tp_ip_lock);
192 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
197 return sk_receive_skb(sk, skb, 1);
200 l2tp_session_dec_refcount(session);
211 static int l2tp_ip_hash(struct sock *sk)
213 if (sk_unhashed(sk)) {
214 write_lock_bh(&l2tp_ip_lock);
215 sk_add_node(sk, &l2tp_ip_table);
216 write_unlock_bh(&l2tp_ip_lock);
221 static void l2tp_ip_unhash(struct sock *sk)
225 write_lock_bh(&l2tp_ip_lock);
226 sk_del_node_init(sk);
227 write_unlock_bh(&l2tp_ip_lock);
230 static int l2tp_ip_open(struct sock *sk)
232 /* Prevent autobind. We don't have ports. */
233 inet_sk(sk)->inet_num = IPPROTO_L2TP;
239 static void l2tp_ip_close(struct sock *sk, long timeout)
241 write_lock_bh(&l2tp_ip_lock);
242 hlist_del_init(&sk->sk_bind_node);
243 sk_del_node_init(sk);
244 write_unlock_bh(&l2tp_ip_lock);
245 sk_common_release(sk);
248 static void l2tp_ip_destroy_sock(struct sock *sk)
251 struct l2tp_tunnel *tunnel = sk->sk_user_data;
253 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
257 l2tp_tunnel_delete(tunnel);
260 static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
262 struct inet_sock *inet = inet_sk(sk);
263 struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
264 struct net *net = sock_net(sk);
268 if (addr_len < sizeof(struct sockaddr_l2tpip))
270 if (addr->l2tp_family != AF_INET)
276 if (!sock_flag(sk, SOCK_ZAPPED))
279 if (sk->sk_state != TCP_CLOSE)
282 chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
283 ret = -EADDRNOTAVAIL;
284 if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
285 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
288 if (addr->l2tp_addr.s_addr)
289 inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
290 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
291 inet->inet_saddr = 0; /* Use device */
293 write_lock_bh(&l2tp_ip_lock);
294 if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0,
295 sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
296 write_unlock_bh(&l2tp_ip_lock);
302 l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
304 sk_add_bind_node(sk, &l2tp_ip_bind_table);
305 sk_del_node_init(sk);
306 write_unlock_bh(&l2tp_ip_lock);
309 sock_reset_flag(sk, SOCK_ZAPPED);
317 static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
319 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
322 if (addr_len < sizeof(*lsa))
325 if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
330 /* Must bind first - autobinding does not work */
331 if (sock_flag(sk, SOCK_ZAPPED)) {
336 rc = __ip4_datagram_connect(sk, uaddr, addr_len);
340 l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
342 write_lock_bh(&l2tp_ip_lock);
343 hlist_del_init(&sk->sk_bind_node);
344 sk_add_bind_node(sk, &l2tp_ip_bind_table);
345 write_unlock_bh(&l2tp_ip_lock);
353 static int l2tp_ip_disconnect(struct sock *sk, int flags)
355 if (sock_flag(sk, SOCK_ZAPPED))
358 return __udp_disconnect(sk, flags);
361 static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
364 struct sock *sk = sock->sk;
365 struct inet_sock *inet = inet_sk(sk);
366 struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
367 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
369 memset(lsa, 0, sizeof(*lsa));
370 lsa->l2tp_family = AF_INET;
372 if (!inet->inet_dport)
374 lsa->l2tp_conn_id = lsk->peer_conn_id;
375 lsa->l2tp_addr.s_addr = inet->inet_daddr;
377 __be32 addr = inet->inet_rcv_saddr;
379 addr = inet->inet_saddr;
380 lsa->l2tp_conn_id = lsk->conn_id;
381 lsa->l2tp_addr.s_addr = addr;
386 static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
390 /* Charge it to the socket, dropping if the queue is full. */
391 rc = sock_queue_rcv_skb(sk, skb);
398 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
403 /* Userspace will call sendmsg() on the tunnel socket to send L2TP
406 static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
410 struct inet_sock *inet = inet_sk(sk);
411 struct rtable *rt = NULL;
419 if (sock_flag(sk, SOCK_DEAD))
422 /* Get and verify the address. */
424 DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name);
426 if (msg->msg_namelen < sizeof(*lip))
429 if (lip->l2tp_family != AF_INET) {
431 if (lip->l2tp_family != AF_UNSPEC)
435 daddr = lip->l2tp_addr.s_addr;
438 if (sk->sk_state != TCP_ESTABLISHED)
441 daddr = inet->inet_daddr;
445 /* Allocate a socket buffer */
447 skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
448 4 + len, 0, GFP_KERNEL);
452 /* Reserve space for headers, putting IP header on 4-byte boundary. */
453 skb_reserve(skb, 2 + NET_SKB_PAD);
454 skb_reset_network_header(skb);
455 skb_reserve(skb, sizeof(struct iphdr));
456 skb_reset_transport_header(skb);
458 /* Insert 0 session_id */
459 *((__be32 *) skb_put(skb, 4)) = 0;
461 /* Copy user data into skb */
462 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
468 fl4 = &inet->cork.fl.u.ip4;
470 rt = (struct rtable *) __sk_dst_check(sk, 0);
474 const struct ip_options_rcu *inet_opt;
476 inet_opt = rcu_dereference(inet->inet_opt);
478 /* Use correct destination address if we have options. */
479 if (inet_opt && inet_opt->opt.srr)
480 daddr = inet_opt->opt.faddr;
482 /* If this fails, retransmit mechanism of transport layer will
483 * keep trying until route appears or the connection times
486 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
487 daddr, inet->inet_saddr,
488 inet->inet_dport, inet->inet_sport,
489 sk->sk_protocol, RT_CONN_FLAGS(sk),
490 sk->sk_bound_dev_if);
494 sk_setup_caps(sk, &rt->dst);
496 skb_dst_set(skb, &rt->dst);
501 /* We dont need to clone dst here, it is guaranteed to not disappear.
502 * __dev_xmit_skb() might force a refcount if needed.
504 skb_dst_set_noref(skb, &rt->dst);
507 /* Queue the packet to IP for output */
508 rc = ip_queue_xmit(sk, skb, &inet->cork.fl);
521 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
527 static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
528 size_t len, int noblock, int flags, int *addr_len)
530 struct inet_sock *inet = inet_sk(sk);
532 int err = -EOPNOTSUPP;
533 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
539 skb = skb_recv_datagram(sk, flags, noblock, &err);
545 msg->msg_flags |= MSG_TRUNC;
549 err = skb_copy_datagram_msg(skb, 0, msg, copied);
553 sock_recv_timestamp(msg, sk, skb);
555 /* Copy the address. */
557 sin->sin_family = AF_INET;
558 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
560 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
561 *addr_len = sizeof(*sin);
563 if (inet->cmsg_flags)
564 ip_cmsg_recv(msg, skb);
565 if (flags & MSG_TRUNC)
568 skb_free_datagram(sk, skb);
570 return err ? err : copied;
573 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
580 amount = sk_wmem_alloc_get(sk);
583 spin_lock_bh(&sk->sk_receive_queue.lock);
584 skb = skb_peek(&sk->sk_receive_queue);
585 amount = skb ? skb->len : 0;
586 spin_unlock_bh(&sk->sk_receive_queue.lock);
593 return put_user(amount, (int __user *)arg);
595 EXPORT_SYMBOL(l2tp_ioctl);
597 static struct proto l2tp_ip_prot = {
599 .owner = THIS_MODULE,
600 .init = l2tp_ip_open,
601 .close = l2tp_ip_close,
602 .bind = l2tp_ip_bind,
603 .connect = l2tp_ip_connect,
604 .disconnect = l2tp_ip_disconnect,
606 .destroy = l2tp_ip_destroy_sock,
607 .setsockopt = ip_setsockopt,
608 .getsockopt = ip_getsockopt,
609 .sendmsg = l2tp_ip_sendmsg,
610 .recvmsg = l2tp_ip_recvmsg,
611 .backlog_rcv = l2tp_ip_backlog_recv,
612 .hash = l2tp_ip_hash,
613 .unhash = l2tp_ip_unhash,
614 .obj_size = sizeof(struct l2tp_ip_sock),
616 .compat_setsockopt = compat_ip_setsockopt,
617 .compat_getsockopt = compat_ip_getsockopt,
621 static const struct proto_ops l2tp_ip_ops = {
623 .owner = THIS_MODULE,
624 .release = inet_release,
626 .connect = inet_dgram_connect,
627 .socketpair = sock_no_socketpair,
628 .accept = sock_no_accept,
629 .getname = l2tp_ip_getname,
630 .poll = datagram_poll,
632 .gettstamp = sock_gettstamp,
633 .listen = sock_no_listen,
634 .shutdown = inet_shutdown,
635 .setsockopt = sock_common_setsockopt,
636 .getsockopt = sock_common_getsockopt,
637 .sendmsg = inet_sendmsg,
638 .recvmsg = sock_common_recvmsg,
639 .mmap = sock_no_mmap,
640 .sendpage = sock_no_sendpage,
642 .compat_setsockopt = compat_sock_common_setsockopt,
643 .compat_getsockopt = compat_sock_common_getsockopt,
647 static struct inet_protosw l2tp_ip_protosw = {
649 .protocol = IPPROTO_L2TP,
650 .prot = &l2tp_ip_prot,
654 static struct net_protocol l2tp_ip_protocol __read_mostly = {
655 .handler = l2tp_ip_recv,
659 static int __init l2tp_ip_init(void)
663 pr_info("L2TP IP encapsulation support (L2TPv3)\n");
665 err = proto_register(&l2tp_ip_prot, 1);
669 err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
673 inet_register_protosw(&l2tp_ip_protosw);
677 proto_unregister(&l2tp_ip_prot);
682 static void __exit l2tp_ip_exit(void)
684 inet_unregister_protosw(&l2tp_ip_protosw);
685 inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
686 proto_unregister(&l2tp_ip_prot);
689 module_init(l2tp_ip_init);
690 module_exit(l2tp_ip_exit);
692 MODULE_LICENSE("GPL");
693 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
694 MODULE_DESCRIPTION("L2TP over IP");
695 MODULE_VERSION("1.0");
697 /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
700 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);
701 MODULE_ALIAS_NET_PF_PROTO(PF_INET, IPPROTO_L2TP);