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