xref: /linux/net/l2tp/l2tp_ip.c (revision 5fe164fb0e6e31dbcbb4b706fd76bc578e5af4c6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* L2TPv3 IP encapsulation support
3  *
4  * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <asm/ioctls.h>
10 #include <linux/icmp.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/random.h>
14 #include <linux/socket.h>
15 #include <linux/l2tp.h>
16 #include <linux/in.h>
17 #include <net/sock.h>
18 #include <net/ip.h>
19 #include <net/icmp.h>
20 #include <net/udp.h>
21 #include <net/inet_common.h>
22 #include <net/tcp_states.h>
23 #include <net/protocol.h>
24 #include <net/xfrm.h>
25 
26 #include "l2tp_core.h"
27 
28 struct l2tp_ip_sock {
29 	/* inet_sock has to be the first member of l2tp_ip_sock */
30 	struct inet_sock	inet;
31 
32 	u32			conn_id;
33 	u32			peer_conn_id;
34 };
35 
36 static DEFINE_RWLOCK(l2tp_ip_lock);
37 static struct hlist_head l2tp_ip_table;
38 static struct hlist_head l2tp_ip_bind_table;
39 
40 static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
41 {
42 	return (struct l2tp_ip_sock *)sk;
43 }
44 
45 static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr,
46 					  __be32 raddr, int dif, u32 tunnel_id)
47 {
48 	struct sock *sk;
49 
50 	sk_for_each_bound(sk, &l2tp_ip_bind_table) {
51 		const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
52 		const struct inet_sock *inet = inet_sk(sk);
53 		int bound_dev_if;
54 
55 		if (!net_eq(sock_net(sk), net))
56 			continue;
57 
58 		bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
59 		if (bound_dev_if && dif && bound_dev_if != dif)
60 			continue;
61 
62 		if (inet->inet_rcv_saddr && laddr &&
63 		    inet->inet_rcv_saddr != laddr)
64 			continue;
65 
66 		if (inet->inet_daddr && raddr && inet->inet_daddr != raddr)
67 			continue;
68 
69 		if (l2tp->conn_id != tunnel_id)
70 			continue;
71 
72 		goto found;
73 	}
74 
75 	sk = NULL;
76 found:
77 	return sk;
78 }
79 
80 /* When processing receive frames, there are two cases to
81  * consider. Data frames consist of a non-zero session-id and an
82  * optional cookie. Control frames consist of a regular L2TP header
83  * preceded by 32-bits of zeros.
84  *
85  * L2TPv3 Session Header Over IP
86  *
87  *  0                   1                   2                   3
88  *  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
89  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90  * |                           Session ID                          |
91  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92  * |               Cookie (optional, maximum 64 bits)...
93  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94  *                                                                 |
95  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96  *
97  * L2TPv3 Control Message Header Over IP
98  *
99  *  0                   1                   2                   3
100  *  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
101  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102  * |                      (32 bits of zeros)                       |
103  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104  * |T|L|x|x|S|x|x|x|x|x|x|x|  Ver  |             Length            |
105  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106  * |                     Control Connection ID                     |
107  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108  * |               Ns              |               Nr              |
109  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110  *
111  * All control frames are passed to userspace.
112  */
113 static int l2tp_ip_recv(struct sk_buff *skb)
114 {
115 	struct net *net = dev_net(skb->dev);
116 	struct sock *sk;
117 	u32 session_id;
118 	u32 tunnel_id;
119 	unsigned char *ptr, *optr;
120 	struct l2tp_session *session;
121 	struct l2tp_tunnel *tunnel = NULL;
122 	struct iphdr *iph;
123 
124 	if (!pskb_may_pull(skb, 4))
125 		goto discard;
126 
127 	/* Point to L2TP header */
128 	optr = skb->data;
129 	ptr = skb->data;
130 	session_id = ntohl(*((__be32 *)ptr));
131 	ptr += 4;
132 
133 	/* RFC3931: L2TP/IP packets have the first 4 bytes containing
134 	 * the session_id. If it is 0, the packet is a L2TP control
135 	 * frame and the session_id value can be discarded.
136 	 */
137 	if (session_id == 0) {
138 		__skb_pull(skb, 4);
139 		goto pass_up;
140 	}
141 
142 	/* Ok, this is a data packet. Lookup the session. */
143 	session = l2tp_v3_session_get(net, NULL, session_id);
144 	if (!session)
145 		goto discard;
146 
147 	tunnel = session->tunnel;
148 	if (!tunnel)
149 		goto discard_sess;
150 
151 	if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
152 		goto discard_sess;
153 
154 	l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
155 	l2tp_session_dec_refcount(session);
156 
157 	return 0;
158 
159 pass_up:
160 	/* Get the tunnel_id from the L2TP header */
161 	if (!pskb_may_pull(skb, 12))
162 		goto discard;
163 
164 	if ((skb->data[0] & 0xc0) != 0xc0)
165 		goto discard;
166 
167 	tunnel_id = ntohl(*(__be32 *)&skb->data[4]);
168 	iph = (struct iphdr *)skb_network_header(skb);
169 
170 	read_lock_bh(&l2tp_ip_lock);
171 	sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb),
172 				   tunnel_id);
173 	if (!sk) {
174 		read_unlock_bh(&l2tp_ip_lock);
175 		goto discard;
176 	}
177 	sock_hold(sk);
178 	read_unlock_bh(&l2tp_ip_lock);
179 
180 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
181 		goto discard_put;
182 
183 	nf_reset_ct(skb);
184 
185 	return sk_receive_skb(sk, skb, 1);
186 
187 discard_sess:
188 	l2tp_session_dec_refcount(session);
189 	goto discard;
190 
191 discard_put:
192 	sock_put(sk);
193 
194 discard:
195 	kfree_skb(skb);
196 	return 0;
197 }
198 
199 static int l2tp_ip_hash(struct sock *sk)
200 {
201 	if (sk_unhashed(sk)) {
202 		write_lock_bh(&l2tp_ip_lock);
203 		sk_add_node(sk, &l2tp_ip_table);
204 		write_unlock_bh(&l2tp_ip_lock);
205 	}
206 	return 0;
207 }
208 
209 static void l2tp_ip_unhash(struct sock *sk)
210 {
211 	if (sk_unhashed(sk))
212 		return;
213 	write_lock_bh(&l2tp_ip_lock);
214 	sk_del_node_init(sk);
215 	write_unlock_bh(&l2tp_ip_lock);
216 }
217 
218 static int l2tp_ip_open(struct sock *sk)
219 {
220 	/* Prevent autobind. We don't have ports. */
221 	inet_sk(sk)->inet_num = IPPROTO_L2TP;
222 
223 	l2tp_ip_hash(sk);
224 	return 0;
225 }
226 
227 static void l2tp_ip_close(struct sock *sk, long timeout)
228 {
229 	write_lock_bh(&l2tp_ip_lock);
230 	hlist_del_init(&sk->sk_bind_node);
231 	sk_del_node_init(sk);
232 	write_unlock_bh(&l2tp_ip_lock);
233 	sk_common_release(sk);
234 }
235 
236 static void l2tp_ip_destroy_sock(struct sock *sk)
237 {
238 	struct l2tp_tunnel *tunnel;
239 
240 	lock_sock(sk);
241 	ip_flush_pending_frames(sk);
242 	release_sock(sk);
243 
244 	tunnel = l2tp_sk_to_tunnel(sk);
245 	if (tunnel) {
246 		l2tp_tunnel_delete(tunnel);
247 		l2tp_tunnel_dec_refcount(tunnel);
248 	}
249 }
250 
251 static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
252 {
253 	struct inet_sock *inet = inet_sk(sk);
254 	struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *)uaddr;
255 	struct net *net = sock_net(sk);
256 	int ret;
257 	int chk_addr_ret;
258 
259 	if (addr_len < sizeof(struct sockaddr_l2tpip))
260 		return -EINVAL;
261 	if (addr->l2tp_family != AF_INET)
262 		return -EINVAL;
263 
264 	lock_sock(sk);
265 
266 	ret = -EINVAL;
267 	if (!sock_flag(sk, SOCK_ZAPPED))
268 		goto out;
269 
270 	if (sk->sk_state != TCP_CLOSE)
271 		goto out;
272 
273 	chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
274 	ret = -EADDRNOTAVAIL;
275 	if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
276 	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
277 		goto out;
278 
279 	if (addr->l2tp_addr.s_addr) {
280 		inet->inet_rcv_saddr = addr->l2tp_addr.s_addr;
281 		inet->inet_saddr = addr->l2tp_addr.s_addr;
282 	}
283 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
284 		inet->inet_saddr = 0;  /* Use device */
285 
286 	write_lock_bh(&l2tp_ip_lock);
287 	if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0,
288 				  sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
289 		write_unlock_bh(&l2tp_ip_lock);
290 		ret = -EADDRINUSE;
291 		goto out;
292 	}
293 
294 	sk_dst_reset(sk);
295 	l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
296 
297 	sk_add_bind_node(sk, &l2tp_ip_bind_table);
298 	sk_del_node_init(sk);
299 	write_unlock_bh(&l2tp_ip_lock);
300 
301 	ret = 0;
302 	sock_reset_flag(sk, SOCK_ZAPPED);
303 
304 out:
305 	release_sock(sk);
306 
307 	return ret;
308 }
309 
310 static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
311 {
312 	struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
313 	int rc;
314 
315 	if (addr_len < sizeof(*lsa))
316 		return -EINVAL;
317 
318 	if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
319 		return -EINVAL;
320 
321 	lock_sock(sk);
322 
323 	/* Must bind first - autobinding does not work */
324 	if (sock_flag(sk, SOCK_ZAPPED)) {
325 		rc = -EINVAL;
326 		goto out_sk;
327 	}
328 
329 	rc = __ip4_datagram_connect(sk, uaddr, addr_len);
330 	if (rc < 0)
331 		goto out_sk;
332 
333 	l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
334 
335 	write_lock_bh(&l2tp_ip_lock);
336 	hlist_del_init(&sk->sk_bind_node);
337 	sk_add_bind_node(sk, &l2tp_ip_bind_table);
338 	write_unlock_bh(&l2tp_ip_lock);
339 
340 out_sk:
341 	release_sock(sk);
342 
343 	return rc;
344 }
345 
346 static int l2tp_ip_disconnect(struct sock *sk, int flags)
347 {
348 	if (sock_flag(sk, SOCK_ZAPPED))
349 		return 0;
350 
351 	return __udp_disconnect(sk, flags);
352 }
353 
354 static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
355 			   int peer)
356 {
357 	struct sock *sk		= sock->sk;
358 	struct inet_sock *inet	= inet_sk(sk);
359 	struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
360 	struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
361 
362 	memset(lsa, 0, sizeof(*lsa));
363 	lsa->l2tp_family = AF_INET;
364 	if (peer) {
365 		if (!inet->inet_dport)
366 			return -ENOTCONN;
367 		lsa->l2tp_conn_id = lsk->peer_conn_id;
368 		lsa->l2tp_addr.s_addr = inet->inet_daddr;
369 	} else {
370 		__be32 addr = inet->inet_rcv_saddr;
371 
372 		if (!addr)
373 			addr = inet->inet_saddr;
374 		lsa->l2tp_conn_id = lsk->conn_id;
375 		lsa->l2tp_addr.s_addr = addr;
376 	}
377 	return sizeof(*lsa);
378 }
379 
380 static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
381 {
382 	int rc;
383 
384 	/* Charge it to the socket, dropping if the queue is full. */
385 	rc = sock_queue_rcv_skb(sk, skb);
386 	if (rc < 0)
387 		goto drop;
388 
389 	return 0;
390 
391 drop:
392 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
393 	kfree_skb(skb);
394 	return 0;
395 }
396 
397 /* Userspace will call sendmsg() on the tunnel socket to send L2TP
398  * control frames.
399  */
400 static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
401 {
402 	struct sk_buff *skb;
403 	int rc;
404 	struct inet_sock *inet = inet_sk(sk);
405 	struct rtable *rt = NULL;
406 	struct flowi4 *fl4;
407 	int connected = 0;
408 	__be32 daddr;
409 
410 	lock_sock(sk);
411 
412 	rc = -ENOTCONN;
413 	if (sock_flag(sk, SOCK_DEAD))
414 		goto out;
415 
416 	/* Get and verify the address. */
417 	if (msg->msg_name) {
418 		DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name);
419 
420 		rc = -EINVAL;
421 		if (msg->msg_namelen < sizeof(*lip))
422 			goto out;
423 
424 		if (lip->l2tp_family != AF_INET) {
425 			rc = -EAFNOSUPPORT;
426 			if (lip->l2tp_family != AF_UNSPEC)
427 				goto out;
428 		}
429 
430 		daddr = lip->l2tp_addr.s_addr;
431 	} else {
432 		rc = -EDESTADDRREQ;
433 		if (sk->sk_state != TCP_ESTABLISHED)
434 			goto out;
435 
436 		daddr = inet->inet_daddr;
437 		connected = 1;
438 	}
439 
440 	/* Allocate a socket buffer */
441 	rc = -ENOMEM;
442 	skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
443 			   4 + len, 0, GFP_KERNEL);
444 	if (!skb)
445 		goto error;
446 
447 	/* Reserve space for headers, putting IP header on 4-byte boundary. */
448 	skb_reserve(skb, 2 + NET_SKB_PAD);
449 	skb_reset_network_header(skb);
450 	skb_reserve(skb, sizeof(struct iphdr));
451 	skb_reset_transport_header(skb);
452 
453 	/* Insert 0 session_id */
454 	*((__be32 *)skb_put(skb, 4)) = 0;
455 
456 	/* Copy user data into skb */
457 	rc = memcpy_from_msg(skb_put(skb, len), msg, len);
458 	if (rc < 0) {
459 		kfree_skb(skb);
460 		goto error;
461 	}
462 
463 	fl4 = &inet->cork.fl.u.ip4;
464 	if (connected)
465 		rt = dst_rtable(__sk_dst_check(sk, 0));
466 
467 	rcu_read_lock();
468 	if (!rt) {
469 		const struct ip_options_rcu *inet_opt;
470 
471 		inet_opt = rcu_dereference(inet->inet_opt);
472 
473 		/* Use correct destination address if we have options. */
474 		if (inet_opt && inet_opt->opt.srr)
475 			daddr = inet_opt->opt.faddr;
476 
477 		/* If this fails, retransmit mechanism of transport layer will
478 		 * keep trying until route appears or the connection times
479 		 * itself out.
480 		 */
481 		rt = ip_route_output_ports(sock_net(sk), fl4, sk,
482 					   daddr, inet->inet_saddr,
483 					   inet->inet_dport, inet->inet_sport,
484 					   sk->sk_protocol, ip_sock_rt_tos(sk),
485 					   sk->sk_bound_dev_if);
486 		if (IS_ERR(rt))
487 			goto no_route;
488 		if (connected) {
489 			sk_setup_caps(sk, &rt->dst);
490 		} else {
491 			skb_dst_set(skb, &rt->dst);
492 			goto xmit;
493 		}
494 	}
495 
496 	/* We don't need to clone dst here, it is guaranteed to not disappear.
497 	 *  __dev_xmit_skb() might force a refcount if needed.
498 	 */
499 	skb_dst_set_noref(skb, &rt->dst);
500 
501 xmit:
502 	/* Queue the packet to IP for output */
503 	rc = ip_queue_xmit(sk, skb, &inet->cork.fl);
504 	rcu_read_unlock();
505 
506 error:
507 	if (rc >= 0)
508 		rc = len;
509 
510 out:
511 	release_sock(sk);
512 	return rc;
513 
514 no_route:
515 	rcu_read_unlock();
516 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
517 	kfree_skb(skb);
518 	rc = -EHOSTUNREACH;
519 	goto out;
520 }
521 
522 static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
523 			   size_t len, int flags, int *addr_len)
524 {
525 	struct inet_sock *inet = inet_sk(sk);
526 	size_t copied = 0;
527 	int err = -EOPNOTSUPP;
528 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
529 	struct sk_buff *skb;
530 
531 	if (flags & MSG_OOB)
532 		goto out;
533 
534 	skb = skb_recv_datagram(sk, flags, &err);
535 	if (!skb)
536 		goto out;
537 
538 	copied = skb->len;
539 	if (len < copied) {
540 		msg->msg_flags |= MSG_TRUNC;
541 		copied = len;
542 	}
543 
544 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
545 	if (err)
546 		goto done;
547 
548 	sock_recv_timestamp(msg, sk, skb);
549 
550 	/* Copy the address. */
551 	if (sin) {
552 		sin->sin_family = AF_INET;
553 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
554 		sin->sin_port = 0;
555 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
556 		*addr_len = sizeof(*sin);
557 	}
558 	if (inet_cmsg_flags(inet))
559 		ip_cmsg_recv(msg, skb);
560 	if (flags & MSG_TRUNC)
561 		copied = skb->len;
562 done:
563 	skb_free_datagram(sk, skb);
564 out:
565 	return err ? err : copied;
566 }
567 
568 int l2tp_ioctl(struct sock *sk, int cmd, int *karg)
569 {
570 	struct sk_buff *skb;
571 
572 	switch (cmd) {
573 	case SIOCOUTQ:
574 		*karg = sk_wmem_alloc_get(sk);
575 		break;
576 	case SIOCINQ:
577 		spin_lock_bh(&sk->sk_receive_queue.lock);
578 		skb = skb_peek(&sk->sk_receive_queue);
579 		*karg = skb ? skb->len : 0;
580 		spin_unlock_bh(&sk->sk_receive_queue.lock);
581 		break;
582 
583 	default:
584 		return -ENOIOCTLCMD;
585 	}
586 
587 	return 0;
588 }
589 EXPORT_SYMBOL_GPL(l2tp_ioctl);
590 
591 static struct proto l2tp_ip_prot = {
592 	.name		   = "L2TP/IP",
593 	.owner		   = THIS_MODULE,
594 	.init		   = l2tp_ip_open,
595 	.close		   = l2tp_ip_close,
596 	.bind		   = l2tp_ip_bind,
597 	.connect	   = l2tp_ip_connect,
598 	.disconnect	   = l2tp_ip_disconnect,
599 	.ioctl		   = l2tp_ioctl,
600 	.destroy	   = l2tp_ip_destroy_sock,
601 	.setsockopt	   = ip_setsockopt,
602 	.getsockopt	   = ip_getsockopt,
603 	.sendmsg	   = l2tp_ip_sendmsg,
604 	.recvmsg	   = l2tp_ip_recvmsg,
605 	.backlog_rcv	   = l2tp_ip_backlog_recv,
606 	.hash		   = l2tp_ip_hash,
607 	.unhash		   = l2tp_ip_unhash,
608 	.obj_size	   = sizeof(struct l2tp_ip_sock),
609 };
610 
611 static const struct proto_ops l2tp_ip_ops = {
612 	.family		   = PF_INET,
613 	.owner		   = THIS_MODULE,
614 	.release	   = inet_release,
615 	.bind		   = inet_bind,
616 	.connect	   = inet_dgram_connect,
617 	.socketpair	   = sock_no_socketpair,
618 	.accept		   = sock_no_accept,
619 	.getname	   = l2tp_ip_getname,
620 	.poll		   = datagram_poll,
621 	.ioctl		   = inet_ioctl,
622 	.gettstamp	   = sock_gettstamp,
623 	.listen		   = sock_no_listen,
624 	.shutdown	   = inet_shutdown,
625 	.setsockopt	   = sock_common_setsockopt,
626 	.getsockopt	   = sock_common_getsockopt,
627 	.sendmsg	   = inet_sendmsg,
628 	.recvmsg	   = sock_common_recvmsg,
629 	.mmap		   = sock_no_mmap,
630 };
631 
632 static struct inet_protosw l2tp_ip_protosw = {
633 	.type		= SOCK_DGRAM,
634 	.protocol	= IPPROTO_L2TP,
635 	.prot		= &l2tp_ip_prot,
636 	.ops		= &l2tp_ip_ops,
637 };
638 
639 static struct net_protocol l2tp_ip_protocol __read_mostly = {
640 	.handler	= l2tp_ip_recv,
641 };
642 
643 static int __init l2tp_ip_init(void)
644 {
645 	int err;
646 
647 	pr_info("L2TP IP encapsulation support (L2TPv3)\n");
648 
649 	err = proto_register(&l2tp_ip_prot, 1);
650 	if (err != 0)
651 		goto out;
652 
653 	err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
654 	if (err)
655 		goto out1;
656 
657 	inet_register_protosw(&l2tp_ip_protosw);
658 	return 0;
659 
660 out1:
661 	proto_unregister(&l2tp_ip_prot);
662 out:
663 	return err;
664 }
665 
666 static void __exit l2tp_ip_exit(void)
667 {
668 	inet_unregister_protosw(&l2tp_ip_protosw);
669 	inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
670 	proto_unregister(&l2tp_ip_prot);
671 }
672 
673 module_init(l2tp_ip_init);
674 module_exit(l2tp_ip_exit);
675 
676 MODULE_LICENSE("GPL");
677 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
678 MODULE_DESCRIPTION("L2TP over IP");
679 MODULE_VERSION("1.0");
680 
681 /* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol,
682  * because __stringify doesn't like enums
683  */
684 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 115, 2);
685 MODULE_ALIAS_NET_PF_PROTO(PF_INET, 115);
686