xref: /linux/net/l2tp/l2tp_ip6.c (revision 9f2347842b526cbc2655068591fb0166362d2999)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
220dcb110STom Parkin /* L2TPv3 IP encapsulation support for IPv6
3a32e0eecSChris Elston  *
4a32e0eecSChris Elston  * Copyright (c) 2012 Katalix Systems Ltd
5a32e0eecSChris Elston  */
6a32e0eecSChris Elston 
7a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8a4ca44faSJoe Perches 
9a32e0eecSChris Elston #include <linux/icmp.h>
10a32e0eecSChris Elston #include <linux/module.h>
11a32e0eecSChris Elston #include <linux/skbuff.h>
12a32e0eecSChris Elston #include <linux/random.h>
13a32e0eecSChris Elston #include <linux/socket.h>
14a32e0eecSChris Elston #include <linux/l2tp.h>
15a32e0eecSChris Elston #include <linux/in.h>
16a32e0eecSChris Elston #include <linux/in6.h>
17a32e0eecSChris Elston #include <net/sock.h>
18a32e0eecSChris Elston #include <net/ip.h>
19a32e0eecSChris Elston #include <net/icmp.h>
20a32e0eecSChris Elston #include <net/udp.h>
21a32e0eecSChris Elston #include <net/inet_common.h>
22a32e0eecSChris Elston #include <net/tcp_states.h>
23a32e0eecSChris Elston #include <net/protocol.h>
24a32e0eecSChris Elston #include <net/xfrm.h>
25a32e0eecSChris Elston #include <net/net_namespace.h>
26a32e0eecSChris Elston #include <net/netns/generic.h>
27a32e0eecSChris Elston 
28a32e0eecSChris Elston #include <net/transp_v6.h>
29a32e0eecSChris Elston #include <net/addrconf.h>
30a32e0eecSChris Elston #include <net/ip6_route.h>
31a32e0eecSChris Elston 
32a32e0eecSChris Elston #include "l2tp_core.h"
33a32e0eecSChris Elston 
34a32e0eecSChris Elston /* per-net private data for this module */
35a32e0eecSChris Elston static unsigned int l2tp_ip6_net_id;
36a32e0eecSChris Elston struct l2tp_ip6_net {
37a32e0eecSChris Elston 	rwlock_t l2tp_ip6_lock;
38a32e0eecSChris Elston 	struct hlist_head l2tp_ip6_table;
39a32e0eecSChris Elston 	struct hlist_head l2tp_ip6_bind_table;
40a32e0eecSChris Elston };
41a32e0eecSChris Elston 
42a32e0eecSChris Elston struct l2tp_ip6_sock {
43a32e0eecSChris Elston 	/* inet_sock has to be the first member of l2tp_ip6_sock */
44a32e0eecSChris Elston 	struct inet_sock	inet;
45a32e0eecSChris Elston 
46a32e0eecSChris Elston 	u32			conn_id;
47a32e0eecSChris Elston 	u32			peer_conn_id;
48a32e0eecSChris Elston 
49a32e0eecSChris Elston 	struct ipv6_pinfo	inet6;
50a32e0eecSChris Elston };
51bb39b0bdSGuillaume Nault 
l2tp_ip6_sk(const struct sock * sk)52bb39b0bdSGuillaume Nault static struct l2tp_ip6_sock *l2tp_ip6_sk(const struct sock *sk)
53a9b2dff8SGuillaume Nault {
54a32e0eecSChris Elston 	return (struct l2tp_ip6_sock *)sk;
55a32e0eecSChris Elston }
56a32e0eecSChris Elston 
l2tp_ip6_pernet(const struct net * net)57a32e0eecSChris Elston static struct l2tp_ip6_net *l2tp_ip6_pernet(const struct net *net)
58b67bfe0dSSasha Levin {
5997b84fd6SGuillaume Nault 	return net_generic(net, l2tp_ip6_net_id);
60a9b2dff8SGuillaume Nault }
61bb39b0bdSGuillaume Nault 
__l2tp_ip6_bind_lookup(const struct net * net,const struct in6_addr * laddr,const struct in6_addr * raddr,int dif,u32 tunnel_id)62ff009403SEric Dumazet static struct sock *__l2tp_ip6_bind_lookup(const struct net *net,
63a32e0eecSChris Elston 					   const struct in6_addr *laddr,
64c5fdae04SGuillaume Nault 					   const struct in6_addr *raddr,
65c5fdae04SGuillaume Nault 					   int dif, u32 tunnel_id)
66c5fdae04SGuillaume Nault {
67ff009403SEric Dumazet 	struct l2tp_ip6_net *pn = l2tp_ip6_pernet(net);
68ff009403SEric Dumazet 	struct sock *sk;
69c5fdae04SGuillaume Nault 
70c5fdae04SGuillaume Nault 	sk_for_each_bound(sk, &pn->l2tp_ip6_bind_table) {
71c5fdae04SGuillaume Nault 		const struct in6_addr *sk_laddr = inet6_rcv_saddr(sk);
72c5fdae04SGuillaume Nault 		const struct in6_addr *sk_raddr = &sk->sk_v6_daddr;
73c5fdae04SGuillaume Nault 		const struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk);
74c5fdae04SGuillaume Nault 		int bound_dev_if;
75c5fdae04SGuillaume Nault 
76c5fdae04SGuillaume Nault 		if (!net_eq(sock_net(sk), net))
77c5fdae04SGuillaume Nault 			continue;
78c5fdae04SGuillaume Nault 
79c5fdae04SGuillaume Nault 		bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
80c5fdae04SGuillaume Nault 		if (bound_dev_if && dif && bound_dev_if != dif)
81c5fdae04SGuillaume Nault 			continue;
82a32e0eecSChris Elston 
83a32e0eecSChris Elston 		if (sk_laddr && !ipv6_addr_any(sk_laddr) &&
84a32e0eecSChris Elston 		    !ipv6_addr_any(laddr) && !ipv6_addr_equal(sk_laddr, laddr))
85a32e0eecSChris Elston 			continue;
86a32e0eecSChris Elston 
87a32e0eecSChris Elston 		if (!ipv6_addr_any(sk_raddr) && raddr &&
88a32e0eecSChris Elston 		    !ipv6_addr_any(raddr) && !ipv6_addr_equal(sk_raddr, raddr))
89a32e0eecSChris Elston 			continue;
90a32e0eecSChris Elston 
91a32e0eecSChris Elston 		if (l2tp->conn_id != tunnel_id)
92a32e0eecSChris Elston 			continue;
93a32e0eecSChris Elston 
94a32e0eecSChris Elston 		goto found;
95a32e0eecSChris Elston 	}
96a32e0eecSChris Elston 
97a32e0eecSChris Elston 	sk = NULL;
98a32e0eecSChris Elston found:
99a32e0eecSChris Elston 	return sk;
100a32e0eecSChris Elston }
101a32e0eecSChris Elston 
102a32e0eecSChris Elston /* When processing receive frames, there are two cases to
103a32e0eecSChris Elston  * consider. Data frames consist of a non-zero session-id and an
104a32e0eecSChris Elston  * optional cookie. Control frames consist of a regular L2TP header
105a32e0eecSChris Elston  * preceded by 32-bits of zeros.
106a32e0eecSChris Elston  *
107a32e0eecSChris Elston  * L2TPv3 Session Header Over IP
108a32e0eecSChris Elston  *
109a32e0eecSChris Elston  *  0                   1                   2                   3
110a32e0eecSChris Elston  *  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
111a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112a32e0eecSChris Elston  * |                           Session ID                          |
113a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114a32e0eecSChris Elston  * |               Cookie (optional, maximum 64 bits)...
115a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116a32e0eecSChris Elston  *                                                                 |
117a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118a32e0eecSChris Elston  *
119a32e0eecSChris Elston  * L2TPv3 Control Message Header Over IP
120a32e0eecSChris Elston  *
121a32e0eecSChris Elston  *  0                   1                   2                   3
122a32e0eecSChris Elston  *  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
123a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124a32e0eecSChris Elston  * |                      (32 bits of zeros)                       |
1250e6b5259SShmulik Ladkani  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126a32e0eecSChris Elston  * |T|L|x|x|S|x|x|x|x|x|x|x|  Ver  |             Length            |
127a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128a32e0eecSChris Elston  * |                     Control Connection ID                     |
129a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130a32e0eecSChris Elston  * |               Ns              |               Nr              |
131a32e0eecSChris Elston  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1328f7dc9aeSGuillaume Nault  *
133a32e0eecSChris Elston  * All control frames are passed to userspace.
134a32e0eecSChris Elston  */
l2tp_ip6_recv(struct sk_buff * skb)135a32e0eecSChris Elston static int l2tp_ip6_recv(struct sk_buff *skb)
136a32e0eecSChris Elston {
137be447f30SHaishuang Yan 	struct net *net = dev_net(skb->dev);
13895075150STom Parkin 	struct l2tp_ip6_net *pn;
13995075150STom Parkin 	struct sock *sk;
140a32e0eecSChris Elston 	u32 session_id;
141a32e0eecSChris Elston 	u32 tunnel_id;
142a32e0eecSChris Elston 	unsigned char *ptr, *optr;
143a32e0eecSChris Elston 	struct l2tp_session *session;
144a32e0eecSChris Elston 	struct l2tp_tunnel *tunnel = NULL;
145a32e0eecSChris Elston 	struct ipv6hdr *iph;
146a32e0eecSChris Elston 
147a32e0eecSChris Elston 	pn = l2tp_ip6_pernet(net);
148a32e0eecSChris Elston 
149a32e0eecSChris Elston 	if (!pskb_may_pull(skb, 4))
150a32e0eecSChris Elston 		goto discard;
151a32e0eecSChris Elston 
152a32e0eecSChris Elston 	/* Point to L2TP header */
15301e28b92SGuillaume Nault 	optr = skb->data;
15461b9a047SGuillaume Nault 	ptr = skb->data;
155a32e0eecSChris Elston 	session_id = ntohl(*((__be32 *)ptr));
156a32e0eecSChris Elston 	ptr += 4;
157a32e0eecSChris Elston 
15861b9a047SGuillaume Nault 	/* RFC3931: L2TP/IP packets have the first 4 bytes containing
15961b9a047SGuillaume Nault 	 * the session_id. If it is 0, the packet is a L2TP control
160a32e0eecSChris Elston 	 * frame and the session_id value can be discarded.
1614522a70dSJacob Wen 	 */
1624522a70dSJacob Wen 	if (session_id == 0) {
1634522a70dSJacob Wen 		__skb_pull(skb, 4);
1642b139e6bSGuillaume Nault 		goto pass_up;
16561b9a047SGuillaume Nault 	}
16661b9a047SGuillaume Nault 
167a32e0eecSChris Elston 	/* Ok, this is a data packet. Lookup the session. */
168a32e0eecSChris Elston 	session = l2tp_v3_session_get(net, NULL, session_id);
169a32e0eecSChris Elston 	if (!session)
170a32e0eecSChris Elston 		goto discard;
171a32e0eecSChris Elston 
172a32e0eecSChris Elston 	tunnel = session->tunnel;
173a32e0eecSChris Elston 	if (!tunnel)
174a32e0eecSChris Elston 		goto discard_sess;
175a32e0eecSChris Elston 
176a32e0eecSChris Elston 	if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
177a32e0eecSChris Elston 		goto discard_sess;
1788f7dc9aeSGuillaume Nault 
179a32e0eecSChris Elston 	l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
180a32e0eecSChris Elston 	l2tp_session_put(session);
181a9b2dff8SGuillaume Nault 
182a9b2dff8SGuillaume Nault 	return 0;
183a3c18422SGuillaume Nault 
184a32e0eecSChris Elston pass_up:
185a3c18422SGuillaume Nault 	/* Get the tunnel_id from the L2TP header */
186a32e0eecSChris Elston 	if (!pskb_may_pull(skb, 12))
187a32e0eecSChris Elston 		goto discard;
188a3c18422SGuillaume Nault 
189a32e0eecSChris Elston 	if ((skb->data[0] & 0xc0) != 0xc0)
190a32e0eecSChris Elston 		goto discard;
191a32e0eecSChris Elston 
192a32e0eecSChris Elston 	tunnel_id = ntohl(*(__be32 *)&skb->data[4]);
193895b5c9fSFlorian Westphal 	iph = ipv6_hdr(skb);
194a32e0eecSChris Elston 
195a32e0eecSChris Elston 	read_lock_bh(&pn->l2tp_ip6_lock);
196a32e0eecSChris Elston 	sk = __l2tp_ip6_bind_lookup(net, &iph->daddr, &iph->saddr,
19761b9a047SGuillaume Nault 				    inet6_iif(skb), tunnel_id);
19861b9a047SGuillaume Nault 	if (!sk) {
19961b9a047SGuillaume Nault 		read_unlock_bh(&pn->l2tp_ip6_lock);
20061b9a047SGuillaume Nault 		goto discard;
201a32e0eecSChris Elston 	}
202a32e0eecSChris Elston 	sock_hold(sk);
203a32e0eecSChris Elston 	read_unlock_bh(&pn->l2tp_ip6_lock);
204a32e0eecSChris Elston 
205a32e0eecSChris Elston 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
206a32e0eecSChris Elston 		goto discard_put;
207a32e0eecSChris Elston 
208a32e0eecSChris Elston 	nf_reset_ct(skb);
20902c71b14SEric Dumazet 
21002c71b14SEric Dumazet 	return sk_receive_skb(sk, skb, 1);
21102c71b14SEric Dumazet 
21202c71b14SEric Dumazet discard_sess:
21302c71b14SEric Dumazet 	l2tp_session_put(session);
21402c71b14SEric Dumazet 	goto discard;
21502c71b14SEric Dumazet 
21602c71b14SEric Dumazet discard_put:
21702c71b14SEric Dumazet 	sock_put(sk);
21802c71b14SEric Dumazet 
21902c71b14SEric Dumazet discard:
22002c71b14SEric Dumazet 	kfree_skb(skb);
22102c71b14SEric Dumazet 	return 0;
22202c71b14SEric Dumazet }
22302c71b14SEric Dumazet 
l2tp_ip6_hash(struct sock * sk)22402c71b14SEric Dumazet static int l2tp_ip6_hash(struct sock *sk)
22502c71b14SEric Dumazet {
22602c71b14SEric Dumazet 	struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
22702c71b14SEric Dumazet 
228a32e0eecSChris Elston 	if (sk_unhashed(sk)) {
229a32e0eecSChris Elston 		write_lock_bh(&pn->l2tp_ip6_lock);
230a32e0eecSChris Elston 		sk_add_node(sk, &pn->l2tp_ip6_table);
231a32e0eecSChris Elston 		write_unlock_bh(&pn->l2tp_ip6_lock);
232a32e0eecSChris Elston 	}
23302c71b14SEric Dumazet 	return 0;
234a32e0eecSChris Elston }
235a32e0eecSChris Elston 
l2tp_ip6_unhash(struct sock * sk)236a32e0eecSChris Elston static void l2tp_ip6_unhash(struct sock *sk)
237a32e0eecSChris Elston {
238a32e0eecSChris Elston 	struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
239a32e0eecSChris Elston 
240a32e0eecSChris Elston 	if (sk_unhashed(sk))
241a32e0eecSChris Elston 		return;
242a32e0eecSChris Elston 	write_lock_bh(&pn->l2tp_ip6_lock);
243a32e0eecSChris Elston 	sk_del_node_init(sk);
244a32e0eecSChris Elston 	write_unlock_bh(&pn->l2tp_ip6_lock);
245a32e0eecSChris Elston }
246a32e0eecSChris Elston 
l2tp_ip6_open(struct sock * sk)247a32e0eecSChris Elston static int l2tp_ip6_open(struct sock *sk)
248a32e0eecSChris Elston {
24945faeff1STom Parkin 	/* Prevent autobind. We don't have ports. */
25093606317STom Parkin 	inet_sk(sk)->inet_num = IPPROTO_L2TP;
251a32e0eecSChris Elston 
252a32e0eecSChris Elston 	l2tp_ip6_hash(sk);
253a32e0eecSChris Elston 	return 0;
254a32e0eecSChris Elston }
255d00fa9adSJames Chapman 
l2tp_ip6_close(struct sock * sk,long timeout)256d00fa9adSJames Chapman static void l2tp_ip6_close(struct sock *sk, long timeout)
257a32e0eecSChris Elston {
258a32e0eecSChris Elston 	struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
259a32e0eecSChris Elston 
260a32e0eecSChris Elston 	write_lock_bh(&pn->l2tp_ip6_lock);
261a32e0eecSChris Elston 	hlist_del_init(&sk->sk_bind_node);
262a32e0eecSChris Elston 	sk_del_node_init(sk);
263a32e0eecSChris Elston 	write_unlock_bh(&pn->l2tp_ip6_lock);
2640e6b5259SShmulik Ladkani 
265a32e0eecSChris Elston 	sk_common_release(sk);
266d5e3a190SGuillaume Nault }
267a32e0eecSChris Elston 
l2tp_ip6_destroy_sock(struct sock * sk)268a32e0eecSChris Elston static void l2tp_ip6_destroy_sock(struct sock *sk)
269a32e0eecSChris Elston {
270c51ce497SJames Chapman 	struct l2tp_tunnel *tunnel;
271c51ce497SJames Chapman 
272a32e0eecSChris Elston 	lock_sock(sk);
273a32e0eecSChris Elston 	ip6_flush_pending_frames(sk);
274a32e0eecSChris Elston 	release_sock(sk);
275a32e0eecSChris Elston 
276a32e0eecSChris Elston 	tunnel = l2tp_sk_to_tunnel(sk);
277a32e0eecSChris Elston 	if (tunnel) {
278a32e0eecSChris Elston 		l2tp_tunnel_delete(tunnel);
279a32e0eecSChris Elston 		l2tp_tunnel_put(tunnel);
280a32e0eecSChris Elston 	}
281a32e0eecSChris Elston }
282a32e0eecSChris Elston 
l2tp_ip6_bind(struct sock * sk,struct sockaddr * uaddr,int addr_len)283a32e0eecSChris Elston static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
284a32e0eecSChris Elston {
285a32e0eecSChris Elston 	struct inet_sock *inet = inet_sk(sk);
286a32e0eecSChris Elston 	struct ipv6_pinfo *np = inet6_sk(sk);
287a32e0eecSChris Elston 	struct sockaddr_l2tpip6 *addr = (struct sockaddr_l2tpip6 *)uaddr;
28832c23116SGuillaume Nault 	struct net *net = sock_net(sk);
28932c23116SGuillaume Nault 	struct l2tp_ip6_net *pn;
29032c23116SGuillaume Nault 	__be32 v4addr = 0;
291a32e0eecSChris Elston 	int bound_dev_if;
292a32e0eecSChris Elston 	int addr_type;
293a32e0eecSChris Elston 	int err;
294d5e3a190SGuillaume Nault 
295d5e3a190SGuillaume Nault 	pn = l2tp_ip6_pernet(net);
296a32e0eecSChris Elston 
297a32e0eecSChris Elston 	if (addr->l2tp_family != AF_INET6)
298a32e0eecSChris Elston 		return -EINVAL;
299a32e0eecSChris Elston 	if (addr_len < sizeof(*addr))
300a32e0eecSChris Elston 		return -EINVAL;
301a32e0eecSChris Elston 
302d5e3a190SGuillaume Nault 	addr_type = ipv6_addr_type(&addr->l2tp_addr);
303d5e3a190SGuillaume Nault 
304a32e0eecSChris Elston 	/* l2tp_ip6 sockets are IPv6 only */
305a32e0eecSChris Elston 	if (addr_type == IPV6_ADDR_MAPPED)
306d5e3a190SGuillaume Nault 		return -EADDRNOTAVAIL;
307d5e3a190SGuillaume Nault 
308d5e3a190SGuillaume Nault 	/* L2TP is point-point, not multicast */
309a32e0eecSChris Elston 	if (addr_type & IPV6_ADDR_MULTICAST)
310a32e0eecSChris Elston 		return -EADDRNOTAVAIL;
311a32e0eecSChris Elston 
312d5e3a190SGuillaume Nault 	lock_sock(sk);
313a32e0eecSChris Elston 
314a32e0eecSChris Elston 	err = -EINVAL;
315a32e0eecSChris Elston 	if (!sock_flag(sk, SOCK_ZAPPED))
316a32e0eecSChris Elston 		goto out_unlock;
317a32e0eecSChris Elston 
318a32e0eecSChris Elston 	if (sk->sk_state != TCP_CLOSE)
319a32e0eecSChris Elston 		goto out_unlock;
320a32e0eecSChris Elston 
321a32e0eecSChris Elston 	bound_dev_if = sk->sk_bound_dev_if;
322a32e0eecSChris Elston 
323a32e0eecSChris Elston 	/* Check if the address belongs to the host. */
324a32e0eecSChris Elston 	rcu_read_lock();
325a32e0eecSChris Elston 	if (addr_type != IPV6_ADDR_ANY) {
326a32e0eecSChris Elston 		struct net_device *dev = NULL;
327d5e3a190SGuillaume Nault 
328a9b2dff8SGuillaume Nault 		if (addr_type & IPV6_ADDR_LINKLOCAL) {
329d5e3a190SGuillaume Nault 			if (addr->l2tp_scope_id)
330d5e3a190SGuillaume Nault 				bound_dev_if = addr->l2tp_scope_id;
331d5e3a190SGuillaume Nault 
332d5e3a190SGuillaume Nault 			/* Binding to link-local address requires an
333d5e3a190SGuillaume Nault 			 * interface.
334d5e3a190SGuillaume Nault 			 */
335d5e3a190SGuillaume Nault 			if (!bound_dev_if)
336d5e3a190SGuillaume Nault 				goto out_unlock_rcu;
337d5e3a190SGuillaume Nault 
338efe4208fSEric Dumazet 			err = -ENODEV;
339a32e0eecSChris Elston 			dev = dev_get_by_index_rcu(sock_net(sk), bound_dev_if);
340a32e0eecSChris Elston 			if (!dev)
341a32e0eecSChris Elston 				goto out_unlock_rcu;
342a32e0eecSChris Elston 		}
343a32e0eecSChris Elston 
344a32e0eecSChris Elston 		/* ipv4 addr of the socket is invalid.  Only the
345a32e0eecSChris Elston 		 * unspecified and mapped address have a v4 equivalent.
346a32e0eecSChris Elston 		 */
347c51ce497SJames Chapman 		v4addr = LOOPBACK4_IPV6;
348a32e0eecSChris Elston 		err = -EADDRNOTAVAIL;
349a32e0eecSChris Elston 		if (!ipv6_chk_addr(sock_net(sk), &addr->l2tp_addr, dev, 0))
350a32e0eecSChris Elston 			goto out_unlock_rcu;
351a32e0eecSChris Elston 	}
352a32e0eecSChris Elston 	rcu_read_unlock();
353a32e0eecSChris Elston 
354a32e0eecSChris Elston 	write_lock_bh(&pn->l2tp_ip6_lock);
355a32e0eecSChris Elston 	if (__l2tp_ip6_bind_lookup(net, &addr->l2tp_addr, NULL, bound_dev_if,
356a32e0eecSChris Elston 				   addr->l2tp_conn_id)) {
357a32e0eecSChris Elston 		write_unlock_bh(&pn->l2tp_ip6_lock);
358a32e0eecSChris Elston 		err = -EADDRINUSE;
359a32e0eecSChris Elston 		goto out_unlock;
360a32e0eecSChris Elston 	}
361a32e0eecSChris Elston 
362a32e0eecSChris Elston 	inet->inet_saddr = v4addr;
363a32e0eecSChris Elston 	inet->inet_rcv_saddr = v4addr;
364a32e0eecSChris Elston 	sk->sk_bound_dev_if = bound_dev_if;
365a32e0eecSChris Elston 	sk->sk_v6_rcv_saddr = addr->l2tp_addr;
366a32e0eecSChris Elston 	np->saddr = addr->l2tp_addr;
367a32e0eecSChris Elston 
368a32e0eecSChris Elston 	l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
369a32e0eecSChris Elston 
370a32e0eecSChris Elston 	sk_add_bind_node(sk, &pn->l2tp_ip6_bind_table);
37182b276cdSHannes Frederic Sowa 	sk_del_node_init(sk);
37282b276cdSHannes Frederic Sowa 	write_unlock_bh(&pn->l2tp_ip6_lock);
37382b276cdSHannes Frederic Sowa 
374a32e0eecSChris Elston 	sock_reset_flag(sk, SOCK_ZAPPED);
375a32e0eecSChris Elston 	release_sock(sk);
376a32e0eecSChris Elston 	return 0;
377a32e0eecSChris Elston 
378a32e0eecSChris Elston out_unlock_rcu:
379a32e0eecSChris Elston 	rcu_read_unlock();
380a32e0eecSChris Elston out_unlock:
381a32e0eecSChris Elston 	release_sock(sk);
382a32e0eecSChris Elston 
383a32e0eecSChris Elston 	return err;
384a32e0eecSChris Elston }
385a32e0eecSChris Elston 
l2tp_ip6_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)3860382a25aSGuillaume Nault static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,
3870382a25aSGuillaume Nault 			    int addr_len)
3880382a25aSGuillaume Nault {
3890382a25aSGuillaume Nault 	struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
3900382a25aSGuillaume Nault 	struct sockaddr_in6	*usin = (struct sockaddr_in6 *)uaddr;
3910382a25aSGuillaume Nault 	struct in6_addr	*daddr;
3920382a25aSGuillaume Nault 	int	addr_type;
3930382a25aSGuillaume Nault 	int rc;
3940382a25aSGuillaume Nault 	struct l2tp_ip6_net *pn;
3950382a25aSGuillaume Nault 
396a32e0eecSChris Elston 	if (addr_len < sizeof(*lsa))
397a32e0eecSChris Elston 		return -EINVAL;
398a32e0eecSChris Elston 
399a32e0eecSChris Elston 	if (usin->sin6_family != AF_INET6)
400a32e0eecSChris Elston 		return -EINVAL;
401a32e0eecSChris Elston 
402a32e0eecSChris Elston 	addr_type = ipv6_addr_type(&usin->sin6_addr);
4030382a25aSGuillaume Nault 	if (addr_type & IPV6_ADDR_MULTICAST)
404a32e0eecSChris Elston 		return -EINVAL;
405a32e0eecSChris Elston 
406a32e0eecSChris Elston 	if (addr_type & IPV6_ADDR_MAPPED) {
407a32e0eecSChris Elston 		daddr = &usin->sin6_addr;
408a32e0eecSChris Elston 		if (ipv4_is_multicast(daddr->s6_addr32[3]))
409c51ce497SJames Chapman 			return -EINVAL;
410c51ce497SJames Chapman 	}
411c51ce497SJames Chapman 
412c51ce497SJames Chapman 	lock_sock(sk);
413c51ce497SJames Chapman 
414286c72deSEric Dumazet 	 /* Must bind first - autobinding does not work */
415c51ce497SJames Chapman 	if (sock_flag(sk, SOCK_ZAPPED)) {
416c51ce497SJames Chapman 		rc = -EINVAL;
417a32e0eecSChris Elston 		goto out_sk;
4189b2c45d4SDenys Vlasenko 	}
419a32e0eecSChris Elston 
420a32e0eecSChris Elston 	rc = __ip6_datagram_connect(sk, uaddr, addr_len);
421a32e0eecSChris Elston 	if (rc < 0)
422a32e0eecSChris Elston 		goto out_sk;
423a32e0eecSChris Elston 
424a32e0eecSChris Elston 	l2tp_ip6_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
425a32e0eecSChris Elston 
426a32e0eecSChris Elston 	pn = l2tp_ip6_pernet(sock_net(sk));
427a32e0eecSChris Elston 	write_lock_bh(&pn->l2tp_ip6_lock);
42804d4fbcaSMathias Krause 	hlist_del_init(&sk->sk_bind_node);
429a32e0eecSChris Elston 	sk_add_bind_node(sk, &pn->l2tp_ip6_bind_table);
430a32e0eecSChris Elston 	write_unlock_bh(&pn->l2tp_ip6_lock);
431a32e0eecSChris Elston 
432a32e0eecSChris Elston out_sk:
433efe4208fSEric Dumazet 	release_sock(sk);
434859f8b26SEric Dumazet 
435a32e0eecSChris Elston 	return rc;
436a32e0eecSChris Elston }
437efe4208fSEric Dumazet 
l2tp_ip6_disconnect(struct sock * sk,int flags)438a32e0eecSChris Elston static int l2tp_ip6_disconnect(struct sock *sk, int flags)
439a32e0eecSChris Elston {
440efe4208fSEric Dumazet 	if (sock_flag(sk, SOCK_ZAPPED))
441a32e0eecSChris Elston 		return 0;
442a32e0eecSChris Elston 
443a32e0eecSChris Elston 	return __udp_disconnect(sk, flags);
444a32e0eecSChris Elston }
445ff009403SEric Dumazet 
l2tp_ip6_getname(struct socket * sock,struct sockaddr * uaddr,int peer)4469b2c45d4SDenys Vlasenko static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
447a32e0eecSChris Elston 			    int peer)
448a32e0eecSChris Elston {
449a32e0eecSChris Elston 	struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
450a32e0eecSChris Elston 	struct sock *sk = sock->sk;
451a32e0eecSChris Elston 	struct ipv6_pinfo *np = inet6_sk(sk);
452a32e0eecSChris Elston 	struct l2tp_ip6_sock *lsk = l2tp_ip6_sk(sk);
453a32e0eecSChris Elston 
454a32e0eecSChris Elston 	lsa->l2tp_family = AF_INET6;
455a32e0eecSChris Elston 	lsa->l2tp_flowinfo = 0;
456a32e0eecSChris Elston 	lsa->l2tp_scope_id = 0;
457a32e0eecSChris Elston 	lsa->l2tp_unused = 0;
458a32e0eecSChris Elston 	if (peer) {
459a32e0eecSChris Elston 		if (!lsk->peer_conn_id)
460a32e0eecSChris Elston 			return -ENOTCONN;
4610e6b5259SShmulik Ladkani 		lsa->l2tp_conn_id = lsk->peer_conn_id;
462a32e0eecSChris Elston 		lsa->l2tp_addr = sk->sk_v6_daddr;
463a32e0eecSChris Elston 		if (inet6_test_bit(SNDFLOW, sk))
464a32e0eecSChris Elston 			lsa->l2tp_flowinfo = np->flow_label;
465a32e0eecSChris Elston 	} else {
466a32e0eecSChris Elston 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
467a32e0eecSChris Elston 			lsa->l2tp_addr = np->saddr;
468a32e0eecSChris Elston 		else
469a32e0eecSChris Elston 			lsa->l2tp_addr = sk->sk_v6_rcv_saddr;
470a32e0eecSChris Elston 
471a32e0eecSChris Elston 		lsa->l2tp_conn_id = lsk->conn_id;
472a32e0eecSChris Elston 	}
4730febc7b3STom Parkin 	if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
474a32e0eecSChris Elston 		lsa->l2tp_scope_id = READ_ONCE(sk->sk_bound_dev_if);
475a32e0eecSChris Elston 	return sizeof(*lsa);
476a32e0eecSChris Elston }
477a32e0eecSChris Elston 
l2tp_ip6_backlog_recv(struct sock * sk,struct sk_buff * skb)478a32e0eecSChris Elston static int l2tp_ip6_backlog_recv(struct sock *sk, struct sk_buff *skb)
479a32e0eecSChris Elston {
480a32e0eecSChris Elston 	int rc;
481a32e0eecSChris Elston 
482a32e0eecSChris Elston 	/* Charge it to the socket, dropping if the queue is full. */
483a32e0eecSChris Elston 	rc = sock_queue_rcv_skb(sk, skb);
484a32e0eecSChris Elston 	if (rc < 0)
485a32e0eecSChris Elston 		goto drop;
486a32e0eecSChris Elston 
487a32e0eecSChris Elston 	return 0;
4881b784140SYing Xue 
489a32e0eecSChris Elston drop:
490a32e0eecSChris Elston 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
491342dfc30SSteffen Hurrle 	kfree_skb(skb);
492a32e0eecSChris Elston 	return -1;
493a32e0eecSChris Elston }
49445f6fad8SEric Dumazet 
l2tp_ip6_push_pending_frames(struct sock * sk)495a32e0eecSChris Elston static int l2tp_ip6_push_pending_frames(struct sock *sk)
496a32e0eecSChris Elston {
497a32e0eecSChris Elston 	struct sk_buff *skb;
498a32e0eecSChris Elston 	__be32 *transhdr = NULL;
49926879da5SWei Wang 	int err = 0;
500a32e0eecSChris Elston 
501a32e0eecSChris Elston 	skb = skb_peek(&sk->sk_write_queue);
502f638a84aSWang Yufen 	if (!skb)
503a32e0eecSChris Elston 		goto out;
504a32e0eecSChris Elston 
505a32e0eecSChris Elston 	transhdr = (__be32 *)skb_transport_header(skb);
50620dcb110STom Parkin 	*transhdr = 0;
507a32e0eecSChris Elston 
508f638a84aSWang Yufen 	err = ip6_push_pending_frames(sk);
509a32e0eecSChris Elston 
510a32e0eecSChris Elston out:
511a32e0eecSChris Elston 	return err;
512a32e0eecSChris Elston }
513a32e0eecSChris Elston 
514a32e0eecSChris Elston /* Userspace will call sendmsg() on the tunnel socket to send L2TP
51520dcb110STom Parkin  * control frames.
516a32e0eecSChris Elston  */
l2tp_ip6_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)517a32e0eecSChris Elston static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
5183c5b4d69SEric Dumazet {
519e2d118a1SLorenzo Colitti 	struct ipv6_txoptions opt_space;
520a32e0eecSChris Elston 	DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
521b515430aSWillem de Bruijn 	struct in6_addr *daddr, *final_p, final;
52226879da5SWei Wang 	struct ipv6_pinfo *np = inet6_sk(sk);
523a32e0eecSChris Elston 	struct ipv6_txoptions *opt_to_free = NULL;
524a32e0eecSChris Elston 	struct ipv6_txoptions *opt = NULL;
525a32e0eecSChris Elston 	struct ip6_flowlabel *flowlabel = NULL;
526a32e0eecSChris Elston 	struct dst_entry *dst = NULL;
527a32e0eecSChris Elston 	struct flowi6 fl6;
528a32e0eecSChris Elston 	struct ipcm6_cookie ipc6;
529a32e0eecSChris Elston 	int addr_len = msg->msg_namelen;
530a32e0eecSChris Elston 	int transhdrlen = 4; /* zero session-id */
531859f8b26SEric Dumazet 	int ulen;
532a32e0eecSChris Elston 	int err;
533a32e0eecSChris Elston 
534a32e0eecSChris Elston 	/* Rough check on arithmetic overflow,
53559c820b2SWillem de Bruijn 	 * better check is made in ip6_append_data().
536a32e0eecSChris Elston 	 */
537a32e0eecSChris Elston 	if (len > INT_MAX - transhdrlen)
538a32e0eecSChris Elston 		return -EMSGSIZE;
539a32e0eecSChris Elston 
54020dcb110STom Parkin 	/* Mirror BSD error message compatibility */
541a32e0eecSChris Elston 	if (msg->msg_flags & MSG_OOB)
542a32e0eecSChris Elston 		return -EOPNOTSUPP;
543a32e0eecSChris Elston 
544efe4208fSEric Dumazet 	/* Get and verify the address */
545efe4208fSEric Dumazet 	memset(&fl6, 0, sizeof(fl6));
546a32e0eecSChris Elston 
547a32e0eecSChris Elston 	fl6.flowi6_mark = READ_ONCE(sk->sk_mark);
548a32e0eecSChris Elston 	fl6.flowi6_uid = sk_uid(sk);
549a32e0eecSChris Elston 
550a32e0eecSChris Elston 	ipcm6_init_sk(&ipc6, sk);
551a32e0eecSChris Elston 
552a32e0eecSChris Elston 	if (lsa) {
553a32e0eecSChris Elston 		if (addr_len < SIN6_LEN_RFC2133)
554a32e0eecSChris Elston 			return -EINVAL;
555efe4208fSEric Dumazet 
556a32e0eecSChris Elston 		if (lsa->l2tp_family && lsa->l2tp_family != AF_INET6)
557a32e0eecSChris Elston 			return -EAFNOSUPPORT;
558a32e0eecSChris Elston 
559a32e0eecSChris Elston 		daddr = &lsa->l2tp_addr;
560ff009403SEric Dumazet 		if (inet6_test_bit(SNDFLOW, sk)) {
561a32e0eecSChris Elston 			fl6.flowlabel = lsa->l2tp_flowinfo & IPV6_FLOWINFO_MASK;
562a32e0eecSChris Elston 			if (fl6.flowlabel & IPV6_FLOWLABEL_MASK) {
563a32e0eecSChris Elston 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
564a32e0eecSChris Elston 				if (IS_ERR(flowlabel))
565a32e0eecSChris Elston 					return -EINVAL;
56626879da5SWei Wang 			}
567a32e0eecSChris Elston 		}
5685fdaa88dSWillem de Bruijn 
569a32e0eecSChris Elston 		/* Otherwise it will be difficult to maintain
570a32e0eecSChris Elston 		 * sk->sk_dst_cache.
571a32e0eecSChris Elston 		 */
572a32e0eecSChris Elston 		if (sk->sk_state == TCP_ESTABLISHED &&
573a32e0eecSChris Elston 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
574a32e0eecSChris Elston 			daddr = &sk->sk_v6_daddr;
57559c820b2SWillem de Bruijn 
576a32e0eecSChris Elston 		if (addr_len >= sizeof(struct sockaddr_in6) &&
577a32e0eecSChris Elston 		    lsa->l2tp_scope_id &&
578a32e0eecSChris Elston 		    ipv6_addr_type(daddr) & IPV6_ADDR_LINKLOCAL)
579a32e0eecSChris Elston 			fl6.flowi6_oif = lsa->l2tp_scope_id;
580a32e0eecSChris Elston 	} else {
581a32e0eecSChris Elston 		if (sk->sk_state != TCP_ESTABLISHED)
58245f6fad8SEric Dumazet 			return -EDESTADDRREQ;
58345f6fad8SEric Dumazet 
58445f6fad8SEric Dumazet 		daddr = &sk->sk_v6_daddr;
58545f6fad8SEric Dumazet 		fl6.flowlabel = np->flow_label;
586a32e0eecSChris Elston 	}
587a32e0eecSChris Elston 
588a32e0eecSChris Elston 	if (fl6.flowi6_oif == 0)
58926879da5SWei Wang 		fl6.flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
590a32e0eecSChris Elston 
591a32e0eecSChris Elston 	if (msg->msg_controllen) {
592a32e0eecSChris Elston 		opt = &opt_space;
593a32e0eecSChris Elston 		memset(opt, 0, sizeof(struct ipv6_txoptions));
594a32e0eecSChris Elston 		opt->tot_len = sizeof(struct ipv6_txoptions);
595a32e0eecSChris Elston 		ipc6.opt = opt;
596a32e0eecSChris Elston 
597a32e0eecSChris Elston 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
598a32e0eecSChris Elston 		if (err < 0) {
599a32e0eecSChris Elston 			fl6_sock_release(flowlabel);
600a32e0eecSChris Elston 			return err;
601a32e0eecSChris Elston 		}
602d2f011a0SEric Dumazet 		if ((fl6.flowlabel & IPV6_FLOWLABEL_MASK) && !flowlabel) {
603a32e0eecSChris Elston 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
6041ac13efdSEric Dumazet 			if (IS_ERR(flowlabel))
605a32e0eecSChris Elston 				return -EINVAL;
6063df98d79SPaul Moore 		}
607a32e0eecSChris Elston 		if (!(opt->opt_nflen | opt->opt_flen))
60838b7097bSHannes Frederic Sowa 			opt = NULL;
60938b7097bSHannes Frederic Sowa 	}
61038b7097bSHannes Frederic Sowa 
61138b7097bSHannes Frederic Sowa 	if (!opt) {
61238b7097bSHannes Frederic Sowa 		opt = txopt_get(np);
613c4e85f73SSabrina Dubroca 		opt_to_free = opt;
614a32e0eecSChris Elston 	}
615a32e0eecSChris Elston 	if (flowlabel)
616a32e0eecSChris Elston 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
617a32e0eecSChris Elston 	opt = ipv6_fixup_options(&opt_space, opt);
618a32e0eecSChris Elston 	ipc6.opt = opt;
61926879da5SWei Wang 
62026879da5SWei Wang 	fl6.flowi6_proto = sk->sk_protocol;
621a32e0eecSChris Elston 	if (!ipv6_addr_any(daddr))
62226879da5SWei Wang 		fl6.daddr = *daddr;
6231086ca7cSEric Dumazet 	else
624a32e0eecSChris Elston 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
625a32e0eecSChris Elston 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
626a32e0eecSChris Elston 		fl6.saddr = np->saddr;
627a32e0eecSChris Elston 
628a32e0eecSChris Elston 	final_p = fl6_update_dst(&fl6, opt, &final);
629a32e0eecSChris Elston 
630*359e54a9STom Parkin 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
631f69e6d13SAl Viro 		fl6.flowi6_oif = READ_ONCE(np->mcast_oif);
63226879da5SWei Wang 	else if (!fl6.flowi6_oif)
633a32e0eecSChris Elston 		fl6.flowi6_oif = READ_ONCE(np->ucast_oif);
6345fdaa88dSWillem de Bruijn 
635a32e0eecSChris Elston 	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
636a32e0eecSChris Elston 
637a32e0eecSChris Elston 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
638a32e0eecSChris Elston 
639a32e0eecSChris Elston 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
640a32e0eecSChris Elston 	if (IS_ERR(dst)) {
641a32e0eecSChris Elston 		err = PTR_ERR(dst);
642a32e0eecSChris Elston 		goto out;
643a32e0eecSChris Elston 	}
64445f6fad8SEric Dumazet 
645a32e0eecSChris Elston 	if (ipc6.hlimit < 0)
646a32e0eecSChris Elston 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
647a32e0eecSChris Elston 
648a32e0eecSChris Elston 	if (msg->msg_flags & MSG_CONFIRM)
6490dec879fSJulian Anastasov 		goto do_confirm;
6500dec879fSJulian Anastasov 
651a32e0eecSChris Elston back_from_confirm:
652a32e0eecSChris Elston 	lock_sock(sk);
653a32e0eecSChris Elston 	ulen = len + (skb_queue_empty(&sk->sk_write_queue) ? transhdrlen : 0);
654a32e0eecSChris Elston 	err = ip6_append_data(sk, ip_generic_getfrag, msg,
655a32e0eecSChris Elston 			      ulen, transhdrlen, &ipc6,
656a32e0eecSChris Elston 			      &fl6, dst_rt6_info(dst),
6571b784140SYing Xue 			      msg->msg_flags);
658ec095263SOliver Hartkopp 	if (err)
659a32e0eecSChris Elston 		ip6_flush_pending_frames(sk);
660700163dbSTom Parkin 	else if (!(msg->msg_flags & MSG_MORE))
661342dfc30SSteffen Hurrle 		err = l2tp_ip6_push_pending_frames(sk);
662a32e0eecSChris Elston 	release_sock(sk);
663a32e0eecSChris Elston done:
664a32e0eecSChris Elston 	dst_release(dst);
665a32e0eecSChris Elston out:
666a32e0eecSChris Elston 	fl6_sock_release(flowlabel);
667a32e0eecSChris Elston 	txopt_put(opt_to_free);
668a32e0eecSChris Elston 
669a32e0eecSChris Elston 	return err < 0 ? err : len;
67085fbaa75SHannes Frederic Sowa 
671a32e0eecSChris Elston do_confirm:
672f4b41f06SOliver Hartkopp 	if (msg->msg_flags & MSG_PROBE)
673a32e0eecSChris Elston 		dst_confirm_neigh(dst, &fl6.daddr);
674a32e0eecSChris Elston 	if (!(msg->msg_flags & MSG_PROBE) || len)
675a32e0eecSChris Elston 		goto back_from_confirm;
676a32e0eecSChris Elston 	err = 0;
677a32e0eecSChris Elston 	goto done;
678a32e0eecSChris Elston }
679a32e0eecSChris Elston 
l2tp_ip6_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)680a32e0eecSChris Elston static int l2tp_ip6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
681a32e0eecSChris Elston 			    int flags, int *addr_len)
68251f3d02bSDavid S. Miller {
683a32e0eecSChris Elston 	struct ipv6_pinfo *np = inet6_sk(sk);
684a32e0eecSChris Elston 	DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
685a32e0eecSChris Elston 	size_t copied = 0;
686a32e0eecSChris Elston 	int err = -EOPNOTSUPP;
687a32e0eecSChris Elston 	struct sk_buff *skb;
688a32e0eecSChris Elston 
689a32e0eecSChris Elston 	if (flags & MSG_OOB)
690a32e0eecSChris Elston 		goto out;
691a32e0eecSChris Elston 
692a32e0eecSChris Elston 	if (flags & MSG_ERRQUEUE)
693a32e0eecSChris Elston 		return ipv6_recv_error(sk, msg, len, addr_len);
694a32e0eecSChris Elston 
695b860d3ccSMathias Krause 	skb = skb_recv_datagram(sk, flags, &err);
696a32e0eecSChris Elston 	if (!skb)
6974330487aSDuan Jiong 		goto out;
698163d1c3dSEric Dumazet 
699a32e0eecSChris Elston 	copied = skb->len;
700a32e0eecSChris Elston 	if (len < copied) {
701700163dbSTom Parkin 		msg->msg_flags |= MSG_TRUNC;
702700163dbSTom Parkin 		copied = len;
703a32e0eecSChris Elston 	}
704a32e0eecSChris Elston 
705a32e0eecSChris Elston 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
706a32e0eecSChris Elston 	if (err)
707a32e0eecSChris Elston 		goto done;
708a32e0eecSChris Elston 
709a32e0eecSChris Elston 	sock_recv_timestamp(msg, sk, skb);
710a32e0eecSChris Elston 
711a32e0eecSChris Elston 	/* Copy the address. */
712a32e0eecSChris Elston 	if (lsa) {
713a32e0eecSChris Elston 		lsa->l2tp_family = AF_INET6;
714a32e0eecSChris Elston 		lsa->l2tp_unused = 0;
715a32e0eecSChris Elston 		lsa->l2tp_addr = ipv6_hdr(skb)->saddr;
716a32e0eecSChris Elston 		lsa->l2tp_flowinfo = 0;
717a32e0eecSChris Elston 		lsa->l2tp_scope_id = 0;
718a32e0eecSChris Elston 		lsa->l2tp_conn_id = 0;
719c51ce497SJames Chapman 		if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
72072fb96e7SEric Dumazet 			lsa->l2tp_scope_id = inet6_iif(skb);
721a32e0eecSChris Elston 		*addr_len = sizeof(*lsa);
722a32e0eecSChris Elston 	}
723a32e0eecSChris Elston 
724a32e0eecSChris Elston 	if (np->rxopt.all)
725a32e0eecSChris Elston 		ip6_datagram_recv_ctl(sk, msg, skb);
726a32e0eecSChris Elston 
72702c71b14SEric Dumazet 	if (flags & MSG_TRUNC)
72802c71b14SEric Dumazet 		copied = skb->len;
729a32e0eecSChris Elston done:
730f5f80e32SEric Dumazet 	skb_free_datagram(sk, skb);
731a32e0eecSChris Elston out:
732a32e0eecSChris Elston 	return err ? err : copied;
733a32e0eecSChris Elston }
734a32e0eecSChris Elston 
735a32e0eecSChris Elston static struct proto l2tp_ip6_prot = {
736a32e0eecSChris Elston 	.name		   = "L2TP/IPv6",
737a32e0eecSChris Elston 	.owner		   = THIS_MODULE,
738a32e0eecSChris Elston 	.init		   = l2tp_ip6_open,
739a32e0eecSChris Elston 	.close		   = l2tp_ip6_close,
740a32e0eecSChris Elston 	.bind		   = l2tp_ip6_bind,
741a32e0eecSChris Elston 	.connect	   = l2tp_ip6_connect,
742a11e1d43SLinus Torvalds 	.disconnect	   = l2tp_ip6_disconnect,
743a32e0eecSChris Elston 	.ioctl		   = l2tp_ioctl,
744c7cbdbf2SArnd Bergmann 	.destroy	   = l2tp_ip6_destroy_sock,
745a32e0eecSChris Elston 	.setsockopt	   = ipv6_setsockopt,
746a32e0eecSChris Elston 	.getsockopt	   = ipv6_getsockopt,
747a32e0eecSChris Elston 	.sendmsg	   = l2tp_ip6_sendmsg,
748a32e0eecSChris Elston 	.recvmsg	   = l2tp_ip6_recvmsg,
749a32e0eecSChris Elston 	.backlog_rcv	   = l2tp_ip6_backlog_recv,
750a32e0eecSChris Elston 	.hash		   = l2tp_ip6_hash,
751a32e0eecSChris Elston 	.unhash		   = l2tp_ip6_unhash,
752a32e0eecSChris Elston 	.obj_size	   = sizeof(struct l2tp_ip6_sock),
7533986912fSChristoph Hellwig 	.ipv6_pinfo_offset = offsetof(struct l2tp_ip6_sock, inet6),
754a32e0eecSChris Elston };
755a32e0eecSChris Elston 
756a32e0eecSChris Elston static const struct proto_ops l2tp_ip6_ops = {
757a32e0eecSChris Elston 	.family		   = PF_INET6,
758a32e0eecSChris Elston 	.owner		   = THIS_MODULE,
759a32e0eecSChris Elston 	.release	   = inet6_release,
760a32e0eecSChris Elston 	.bind		   = inet6_bind,
761a32e0eecSChris Elston 	.connect	   = inet_dgram_connect,
762a32e0eecSChris Elston 	.socketpair	   = sock_no_socketpair,
763a32e0eecSChris Elston 	.accept		   = sock_no_accept,
764a32e0eecSChris Elston 	.getname	   = l2tp_ip6_getname,
765a32e0eecSChris Elston 	.poll		   = datagram_poll,
766a32e0eecSChris Elston 	.ioctl		   = inet6_ioctl,
767a32e0eecSChris Elston 	.gettstamp	   = sock_gettstamp,
768a32e0eecSChris Elston 	.listen		   = sock_no_listen,
769a32e0eecSChris Elston 	.shutdown	   = inet_shutdown,
770a32e0eecSChris Elston 	.setsockopt	   = sock_common_setsockopt,
771a32e0eecSChris Elston 	.getsockopt	   = sock_common_getsockopt,
772a4ca44faSJoe Perches 	.sendmsg	   = inet_sendmsg,
773a32e0eecSChris Elston 	.recvmsg	   = sock_common_recvmsg,
774a32e0eecSChris Elston 	.mmap		   = sock_no_mmap,
775a32e0eecSChris Elston #ifdef CONFIG_COMPAT
776a32e0eecSChris Elston 	.compat_ioctl	   = inet6_compat_ioctl,
777a32e0eecSChris Elston #endif
778a32e0eecSChris Elston };
779a32e0eecSChris Elston 
780a32e0eecSChris Elston static struct inet_protosw l2tp_ip6_protosw = {
781a32e0eecSChris Elston 	.type		= SOCK_DGRAM,
782a32e0eecSChris Elston 	.protocol	= IPPROTO_L2TP,
783a32e0eecSChris Elston 	.prot		= &l2tp_ip6_prot,
784a32e0eecSChris Elston 	.ops		= &l2tp_ip6_ops,
785a32e0eecSChris Elston };
786a32e0eecSChris Elston 
787a32e0eecSChris Elston static struct inet6_protocol l2tp_ip6_protocol __read_mostly = {
788a32e0eecSChris Elston 	.handler	= l2tp_ip6_recv,
789a32e0eecSChris Elston };
790a32e0eecSChris Elston 
l2tp_ip6_init_net(struct net * net)791a32e0eecSChris Elston static __net_init int l2tp_ip6_init_net(struct net *net)
792a32e0eecSChris Elston {
793a32e0eecSChris Elston 	struct l2tp_ip6_net *pn = net_generic(net, l2tp_ip6_net_id);
794a32e0eecSChris Elston 
795a32e0eecSChris Elston 	rwlock_init(&pn->l2tp_ip6_lock);
796a32e0eecSChris Elston 	INIT_HLIST_HEAD(&pn->l2tp_ip6_table);
797a32e0eecSChris Elston 	INIT_HLIST_HEAD(&pn->l2tp_ip6_bind_table);
798a32e0eecSChris Elston 	return 0;
799a32e0eecSChris Elston }
800a32e0eecSChris Elston 
l2tp_ip6_exit_net(struct net * net)801a32e0eecSChris Elston static __net_exit void l2tp_ip6_exit_net(struct net *net)
802a32e0eecSChris Elston {
803a32e0eecSChris Elston 	struct l2tp_ip6_net *pn = l2tp_ip6_pernet(net);
804a32e0eecSChris Elston 
805a32e0eecSChris Elston 	write_lock_bh(&pn->l2tp_ip6_lock);
806154e07c1SAndrea Righi 	WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip6_table) != 0);
807154e07c1SAndrea Righi 	WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip6_bind_table) != 0);
808a32e0eecSChris Elston 	write_unlock_bh(&pn->l2tp_ip6_lock);
809154e07c1SAndrea Righi }
810154e07c1SAndrea Righi 
811 static struct pernet_operations l2tp_ip6_net_ops = {
812 	.init = l2tp_ip6_init_net,
813 	.exit = l2tp_ip6_exit_net,
814 	.id   = &l2tp_ip6_net_id,
815 	.size = sizeof(struct l2tp_ip6_net),
816 };
817 
l2tp_ip6_init(void)818 static int __init l2tp_ip6_init(void)
819 {
820 	int err;
821 
822 	pr_info("L2TP IP encapsulation support for IPv6 (L2TPv3)\n");
823 
824 	err = register_pernet_device(&l2tp_ip6_net_ops);
825 	if (err)
826 		goto out;
827 
828 	err = proto_register(&l2tp_ip6_prot, 1);
829 	if (err != 0)
830 		goto out1;
831 
832 	err = inet6_add_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
833 	if (err)
834 		goto out2;
835 
836 	inet6_register_protosw(&l2tp_ip6_protosw);
837 	return 0;
838 
839 out2:
840 	proto_unregister(&l2tp_ip6_prot);
841 out1:
842 	unregister_pernet_device(&l2tp_ip6_net_ops);
843 out:
844 	return err;
845 }
846 
l2tp_ip6_exit(void)847 static void __exit l2tp_ip6_exit(void)
848 {
849 	inet6_unregister_protosw(&l2tp_ip6_protosw);
850 	inet6_del_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
851 	proto_unregister(&l2tp_ip6_prot);
852 	unregister_pernet_device(&l2tp_ip6_net_ops);
853 }
854 
855 module_init(l2tp_ip6_init);
856 module_exit(l2tp_ip6_exit);
857 
858 MODULE_LICENSE("GPL");
859 MODULE_AUTHOR("Chris Elston <celston@katalix.com>");
860 MODULE_DESCRIPTION("L2TP IP encapsulation for IPv6");
861 MODULE_VERSION("1.0");
862 
863 /* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol,
864  * because __stringify doesn't like enums
865  */
866 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 115, 2);
867 MODULE_ALIAS_NET_PF_PROTO(PF_INET6, 115);
868