1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* L2TPv3 IP encapsulation support for IPv6
4 * Copyright (c) 2012 Katalix Systems Ltd
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/icmp.h>
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/random.h>
13 #include <linux/socket.h>
14 #include <linux/l2tp.h>
16 #include <linux/in6.h>
21 #include <net/inet_common.h>
22 #include <net/tcp_states.h>
23 #include <net/protocol.h>
26 #include <net/transp_v6.h>
27 #include <net/addrconf.h>
28 #include <net/ip6_route.h>
30 #include "l2tp_core.h"
32 struct l2tp_ip6_sock {
33 /* inet_sock has to be the first member of l2tp_ip6_sock */
34 struct inet_sock inet;
39 struct ipv6_pinfo inet6;
42 static DEFINE_RWLOCK(l2tp_ip6_lock);
43 static struct hlist_head l2tp_ip6_table;
44 static struct hlist_head l2tp_ip6_bind_table;
46 static inline struct l2tp_ip6_sock *l2tp_ip6_sk(const struct sock *sk)
48 return (struct l2tp_ip6_sock *)sk;
51 static struct sock *__l2tp_ip6_bind_lookup(const struct net *net,
52 const struct in6_addr *laddr,
53 const struct in6_addr *raddr,
54 int dif, u32 tunnel_id)
58 sk_for_each_bound(sk, &l2tp_ip6_bind_table) {
59 const struct in6_addr *sk_laddr = inet6_rcv_saddr(sk);
60 const struct in6_addr *sk_raddr = &sk->sk_v6_daddr;
61 const struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk);
64 if (!net_eq(sock_net(sk), net))
67 bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
68 if (bound_dev_if && dif && bound_dev_if != dif)
71 if (sk_laddr && !ipv6_addr_any(sk_laddr) &&
72 !ipv6_addr_any(laddr) && !ipv6_addr_equal(sk_laddr, laddr))
75 if (!ipv6_addr_any(sk_raddr) && raddr &&
76 !ipv6_addr_any(raddr) && !ipv6_addr_equal(sk_raddr, raddr))
79 if (l2tp->conn_id != tunnel_id)
90 /* When processing receive frames, there are two cases to
91 * consider. Data frames consist of a non-zero session-id and an
92 * optional cookie. Control frames consist of a regular L2TP header
93 * preceded by 32-bits of zeros.
95 * L2TPv3 Session Header Over IP
98 * 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
99 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 * | Cookie (optional, maximum 64 bits)...
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * L2TPv3 Control Message Header Over IP
110 * 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
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | (32 bits of zeros) |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 * |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 * | Control Connection ID |
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 * All control frames are passed to userspace.
123 static int l2tp_ip6_recv(struct sk_buff *skb)
125 struct net *net = dev_net(skb->dev);
129 unsigned char *ptr, *optr;
130 struct l2tp_session *session;
131 struct l2tp_tunnel *tunnel = NULL;
134 if (!pskb_may_pull(skb, 4))
137 /* Point to L2TP header */
140 session_id = ntohl(*((__be32 *)ptr));
143 /* RFC3931: L2TP/IP packets have the first 4 bytes containing
144 * the session_id. If it is 0, the packet is a L2TP control
145 * frame and the session_id value can be discarded.
147 if (session_id == 0) {
152 /* Ok, this is a data packet. Lookup the session. */
153 session = l2tp_session_get(net, session_id);
157 tunnel = session->tunnel;
161 if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
164 l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
165 l2tp_session_dec_refcount(session);
170 /* Get the tunnel_id from the L2TP header */
171 if (!pskb_may_pull(skb, 12))
174 if ((skb->data[0] & 0xc0) != 0xc0)
177 tunnel_id = ntohl(*(__be32 *)&skb->data[4]);
180 read_lock_bh(&l2tp_ip6_lock);
181 sk = __l2tp_ip6_bind_lookup(net, &iph->daddr, &iph->saddr,
182 inet6_iif(skb), tunnel_id);
184 read_unlock_bh(&l2tp_ip6_lock);
188 read_unlock_bh(&l2tp_ip6_lock);
190 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
195 return sk_receive_skb(sk, skb, 1);
198 l2tp_session_dec_refcount(session);
209 static int l2tp_ip6_hash(struct sock *sk)
211 if (sk_unhashed(sk)) {
212 write_lock_bh(&l2tp_ip6_lock);
213 sk_add_node(sk, &l2tp_ip6_table);
214 write_unlock_bh(&l2tp_ip6_lock);
219 static void l2tp_ip6_unhash(struct sock *sk)
223 write_lock_bh(&l2tp_ip6_lock);
224 sk_del_node_init(sk);
225 write_unlock_bh(&l2tp_ip6_lock);
228 static int l2tp_ip6_open(struct sock *sk)
230 /* Prevent autobind. We don't have ports. */
231 inet_sk(sk)->inet_num = IPPROTO_L2TP;
237 static void l2tp_ip6_close(struct sock *sk, long timeout)
239 write_lock_bh(&l2tp_ip6_lock);
240 hlist_del_init(&sk->sk_bind_node);
241 sk_del_node_init(sk);
242 write_unlock_bh(&l2tp_ip6_lock);
244 sk_common_release(sk);
247 static void l2tp_ip6_destroy_sock(struct sock *sk)
249 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
252 ip6_flush_pending_frames(sk);
256 l2tp_tunnel_delete(tunnel);
259 static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
261 struct inet_sock *inet = inet_sk(sk);
262 struct ipv6_pinfo *np = inet6_sk(sk);
263 struct sockaddr_l2tpip6 *addr = (struct sockaddr_l2tpip6 *)uaddr;
264 struct net *net = sock_net(sk);
270 if (addr->l2tp_family != AF_INET6)
272 if (addr_len < sizeof(*addr))
275 addr_type = ipv6_addr_type(&addr->l2tp_addr);
277 /* l2tp_ip6 sockets are IPv6 only */
278 if (addr_type == IPV6_ADDR_MAPPED)
279 return -EADDRNOTAVAIL;
281 /* L2TP is point-point, not multicast */
282 if (addr_type & IPV6_ADDR_MULTICAST)
283 return -EADDRNOTAVAIL;
288 if (!sock_flag(sk, SOCK_ZAPPED))
291 if (sk->sk_state != TCP_CLOSE)
294 bound_dev_if = sk->sk_bound_dev_if;
296 /* Check if the address belongs to the host. */
298 if (addr_type != IPV6_ADDR_ANY) {
299 struct net_device *dev = NULL;
301 if (addr_type & IPV6_ADDR_LINKLOCAL) {
302 if (addr->l2tp_scope_id)
303 bound_dev_if = addr->l2tp_scope_id;
305 /* Binding to link-local address requires an
312 dev = dev_get_by_index_rcu(sock_net(sk), bound_dev_if);
317 /* ipv4 addr of the socket is invalid. Only the
318 * unspecified and mapped address have a v4 equivalent.
320 v4addr = LOOPBACK4_IPV6;
321 err = -EADDRNOTAVAIL;
322 if (!ipv6_chk_addr(sock_net(sk), &addr->l2tp_addr, dev, 0))
327 write_lock_bh(&l2tp_ip6_lock);
328 if (__l2tp_ip6_bind_lookup(net, &addr->l2tp_addr, NULL, bound_dev_if,
329 addr->l2tp_conn_id)) {
330 write_unlock_bh(&l2tp_ip6_lock);
335 inet->inet_saddr = v4addr;
336 inet->inet_rcv_saddr = v4addr;
337 sk->sk_bound_dev_if = bound_dev_if;
338 sk->sk_v6_rcv_saddr = addr->l2tp_addr;
339 np->saddr = addr->l2tp_addr;
341 l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
343 sk_add_bind_node(sk, &l2tp_ip6_bind_table);
344 sk_del_node_init(sk);
345 write_unlock_bh(&l2tp_ip6_lock);
347 sock_reset_flag(sk, SOCK_ZAPPED);
359 static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,
362 struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
363 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
364 struct in6_addr *daddr;
368 if (addr_len < sizeof(*lsa))
371 if (usin->sin6_family != AF_INET6)
374 addr_type = ipv6_addr_type(&usin->sin6_addr);
375 if (addr_type & IPV6_ADDR_MULTICAST)
378 if (addr_type & IPV6_ADDR_MAPPED) {
379 daddr = &usin->sin6_addr;
380 if (ipv4_is_multicast(daddr->s6_addr32[3]))
386 /* Must bind first - autobinding does not work */
387 if (sock_flag(sk, SOCK_ZAPPED)) {
392 rc = __ip6_datagram_connect(sk, uaddr, addr_len);
396 l2tp_ip6_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
398 write_lock_bh(&l2tp_ip6_lock);
399 hlist_del_init(&sk->sk_bind_node);
400 sk_add_bind_node(sk, &l2tp_ip6_bind_table);
401 write_unlock_bh(&l2tp_ip6_lock);
409 static int l2tp_ip6_disconnect(struct sock *sk, int flags)
411 if (sock_flag(sk, SOCK_ZAPPED))
414 return __udp_disconnect(sk, flags);
417 static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
420 struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
421 struct sock *sk = sock->sk;
422 struct ipv6_pinfo *np = inet6_sk(sk);
423 struct l2tp_ip6_sock *lsk = l2tp_ip6_sk(sk);
425 lsa->l2tp_family = AF_INET6;
426 lsa->l2tp_flowinfo = 0;
427 lsa->l2tp_scope_id = 0;
428 lsa->l2tp_unused = 0;
430 if (!lsk->peer_conn_id)
432 lsa->l2tp_conn_id = lsk->peer_conn_id;
433 lsa->l2tp_addr = sk->sk_v6_daddr;
435 lsa->l2tp_flowinfo = np->flow_label;
437 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
438 lsa->l2tp_addr = np->saddr;
440 lsa->l2tp_addr = sk->sk_v6_rcv_saddr;
442 lsa->l2tp_conn_id = lsk->conn_id;
444 if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
445 lsa->l2tp_scope_id = READ_ONCE(sk->sk_bound_dev_if);
449 static int l2tp_ip6_backlog_recv(struct sock *sk, struct sk_buff *skb)
453 /* Charge it to the socket, dropping if the queue is full. */
454 rc = sock_queue_rcv_skb(sk, skb);
461 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
466 static int l2tp_ip6_push_pending_frames(struct sock *sk)
469 __be32 *transhdr = NULL;
472 skb = skb_peek(&sk->sk_write_queue);
476 transhdr = (__be32 *)skb_transport_header(skb);
479 err = ip6_push_pending_frames(sk);
485 /* Userspace will call sendmsg() on the tunnel socket to send L2TP
488 static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
490 struct ipv6_txoptions opt_space;
491 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
492 struct in6_addr *daddr, *final_p, final;
493 struct ipv6_pinfo *np = inet6_sk(sk);
494 struct ipv6_txoptions *opt_to_free = NULL;
495 struct ipv6_txoptions *opt = NULL;
496 struct ip6_flowlabel *flowlabel = NULL;
497 struct dst_entry *dst = NULL;
499 struct ipcm6_cookie ipc6;
500 int addr_len = msg->msg_namelen;
501 int transhdrlen = 4; /* zero session-id */
505 /* Rough check on arithmetic overflow,
506 * better check is made in ip6_append_data().
508 if (len > INT_MAX - transhdrlen)
511 /* Mirror BSD error message compatibility */
512 if (msg->msg_flags & MSG_OOB)
515 /* Get and verify the address */
516 memset(&fl6, 0, sizeof(fl6));
518 fl6.flowi6_mark = READ_ONCE(sk->sk_mark);
519 fl6.flowi6_uid = sk->sk_uid;
524 if (addr_len < SIN6_LEN_RFC2133)
527 if (lsa->l2tp_family && lsa->l2tp_family != AF_INET6)
528 return -EAFNOSUPPORT;
530 daddr = &lsa->l2tp_addr;
532 fl6.flowlabel = lsa->l2tp_flowinfo & IPV6_FLOWINFO_MASK;
533 if (fl6.flowlabel & IPV6_FLOWLABEL_MASK) {
534 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
535 if (IS_ERR(flowlabel))
540 /* Otherwise it will be difficult to maintain
543 if (sk->sk_state == TCP_ESTABLISHED &&
544 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
545 daddr = &sk->sk_v6_daddr;
547 if (addr_len >= sizeof(struct sockaddr_in6) &&
548 lsa->l2tp_scope_id &&
549 ipv6_addr_type(daddr) & IPV6_ADDR_LINKLOCAL)
550 fl6.flowi6_oif = lsa->l2tp_scope_id;
552 if (sk->sk_state != TCP_ESTABLISHED)
553 return -EDESTADDRREQ;
555 daddr = &sk->sk_v6_daddr;
556 fl6.flowlabel = np->flow_label;
559 if (fl6.flowi6_oif == 0)
560 fl6.flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
562 if (msg->msg_controllen) {
564 memset(opt, 0, sizeof(struct ipv6_txoptions));
565 opt->tot_len = sizeof(struct ipv6_txoptions);
568 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
570 fl6_sock_release(flowlabel);
573 if ((fl6.flowlabel & IPV6_FLOWLABEL_MASK) && !flowlabel) {
574 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
575 if (IS_ERR(flowlabel))
578 if (!(opt->opt_nflen | opt->opt_flen))
587 opt = fl6_merge_options(&opt_space, flowlabel, opt);
588 opt = ipv6_fixup_options(&opt_space, opt);
591 fl6.flowi6_proto = sk->sk_protocol;
592 if (!ipv6_addr_any(daddr))
595 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
596 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
597 fl6.saddr = np->saddr;
599 final_p = fl6_update_dst(&fl6, opt, &final);
601 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
602 fl6.flowi6_oif = np->mcast_oif;
603 else if (!fl6.flowi6_oif)
604 fl6.flowi6_oif = np->ucast_oif;
606 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
609 ipc6.tclass = np->tclass;
611 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
613 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
620 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
622 if (ipc6.dontfrag < 0)
623 ipc6.dontfrag = np->dontfrag;
625 if (msg->msg_flags & MSG_CONFIRM)
630 ulen = len + skb_queue_empty(&sk->sk_write_queue) ? transhdrlen : 0;
631 err = ip6_append_data(sk, ip_generic_getfrag, msg,
632 ulen, transhdrlen, &ipc6,
633 &fl6, (struct rt6_info *)dst,
636 ip6_flush_pending_frames(sk);
637 else if (!(msg->msg_flags & MSG_MORE))
638 err = l2tp_ip6_push_pending_frames(sk);
643 fl6_sock_release(flowlabel);
644 txopt_put(opt_to_free);
646 return err < 0 ? err : len;
649 if (msg->msg_flags & MSG_PROBE)
650 dst_confirm_neigh(dst, &fl6.daddr);
651 if (!(msg->msg_flags & MSG_PROBE) || len)
652 goto back_from_confirm;
657 static int l2tp_ip6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
658 int flags, int *addr_len)
660 struct ipv6_pinfo *np = inet6_sk(sk);
661 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
663 int err = -EOPNOTSUPP;
669 if (flags & MSG_ERRQUEUE)
670 return ipv6_recv_error(sk, msg, len, addr_len);
672 skb = skb_recv_datagram(sk, flags, &err);
678 msg->msg_flags |= MSG_TRUNC;
682 err = skb_copy_datagram_msg(skb, 0, msg, copied);
686 sock_recv_timestamp(msg, sk, skb);
688 /* Copy the address. */
690 lsa->l2tp_family = AF_INET6;
691 lsa->l2tp_unused = 0;
692 lsa->l2tp_addr = ipv6_hdr(skb)->saddr;
693 lsa->l2tp_flowinfo = 0;
694 lsa->l2tp_scope_id = 0;
695 lsa->l2tp_conn_id = 0;
696 if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
697 lsa->l2tp_scope_id = inet6_iif(skb);
698 *addr_len = sizeof(*lsa);
702 ip6_datagram_recv_ctl(sk, msg, skb);
704 if (flags & MSG_TRUNC)
707 skb_free_datagram(sk, skb);
709 return err ? err : copied;
712 static struct proto l2tp_ip6_prot = {
714 .owner = THIS_MODULE,
715 .init = l2tp_ip6_open,
716 .close = l2tp_ip6_close,
717 .bind = l2tp_ip6_bind,
718 .connect = l2tp_ip6_connect,
719 .disconnect = l2tp_ip6_disconnect,
721 .destroy = l2tp_ip6_destroy_sock,
722 .setsockopt = ipv6_setsockopt,
723 .getsockopt = ipv6_getsockopt,
724 .sendmsg = l2tp_ip6_sendmsg,
725 .recvmsg = l2tp_ip6_recvmsg,
726 .backlog_rcv = l2tp_ip6_backlog_recv,
727 .hash = l2tp_ip6_hash,
728 .unhash = l2tp_ip6_unhash,
729 .obj_size = sizeof(struct l2tp_ip6_sock),
730 .ipv6_pinfo_offset = offsetof(struct l2tp_ip6_sock, inet6),
733 static const struct proto_ops l2tp_ip6_ops = {
735 .owner = THIS_MODULE,
736 .release = inet6_release,
738 .connect = inet_dgram_connect,
739 .socketpair = sock_no_socketpair,
740 .accept = sock_no_accept,
741 .getname = l2tp_ip6_getname,
742 .poll = datagram_poll,
743 .ioctl = inet6_ioctl,
744 .gettstamp = sock_gettstamp,
745 .listen = sock_no_listen,
746 .shutdown = inet_shutdown,
747 .setsockopt = sock_common_setsockopt,
748 .getsockopt = sock_common_getsockopt,
749 .sendmsg = inet_sendmsg,
750 .recvmsg = sock_common_recvmsg,
751 .mmap = sock_no_mmap,
753 .compat_ioctl = inet6_compat_ioctl,
757 static struct inet_protosw l2tp_ip6_protosw = {
759 .protocol = IPPROTO_L2TP,
760 .prot = &l2tp_ip6_prot,
761 .ops = &l2tp_ip6_ops,
764 static struct inet6_protocol l2tp_ip6_protocol __read_mostly = {
765 .handler = l2tp_ip6_recv,
768 static int __init l2tp_ip6_init(void)
772 pr_info("L2TP IP encapsulation support for IPv6 (L2TPv3)\n");
774 err = proto_register(&l2tp_ip6_prot, 1);
778 err = inet6_add_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
782 inet6_register_protosw(&l2tp_ip6_protosw);
786 proto_unregister(&l2tp_ip6_prot);
791 static void __exit l2tp_ip6_exit(void)
793 inet6_unregister_protosw(&l2tp_ip6_protosw);
794 inet6_del_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
795 proto_unregister(&l2tp_ip6_prot);
798 module_init(l2tp_ip6_init);
799 module_exit(l2tp_ip6_exit);
801 MODULE_LICENSE("GPL");
802 MODULE_AUTHOR("Chris Elston <celston@katalix.com>");
803 MODULE_DESCRIPTION("L2TP IP encapsulation for IPv6");
804 MODULE_VERSION("1.0");
806 /* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol,
807 * because __stringify doesn't like enums
809 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 115, 2);
810 MODULE_ALIAS_NET_PF_PROTO(PF_INET6, 115);