l2tp_ip.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * L2TPv3 IP encapsulation support
 *
 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

E
Eric Dumazet 已提交
14
#include <asm/ioctls.h>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <linux/icmp.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/random.h>
#include <linux/socket.h>
#include <linux/l2tp.h>
#include <linux/in.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/udp.h>
#include <net/inet_common.h>
#include <net/inet_hashtables.h>
#include <net/tcp_states.h>
#include <net/protocol.h>
#include <net/xfrm.h>

#include "l2tp_core.h"

struct l2tp_ip_sock {
	/* inet_sock has to be the first member of l2tp_ip_sock */
	struct inet_sock	inet;

38 39
	u32			conn_id;
	u32			peer_conn_id;
40 41 42 43 44 45 46 47 48 49 50
};

static DEFINE_RWLOCK(l2tp_ip_lock);
static struct hlist_head l2tp_ip_table;
static struct hlist_head l2tp_ip_bind_table;

static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
{
	return (struct l2tp_ip_sock *)sk;
}

51 52
static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr,
					  __be32 raddr, int dif, u32 tunnel_id)
53 54 55
{
	struct sock *sk;

56
	sk_for_each_bound(sk, &l2tp_ip_bind_table) {
57 58
		const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
		const struct inet_sock *inet = inet_sk(sk);
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		if (!net_eq(sock_net(sk), net))
			continue;

		if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif)
			continue;

		if (inet->inet_rcv_saddr && laddr &&
		    inet->inet_rcv_saddr != laddr)
			continue;

		if (inet->inet_daddr && raddr && inet->inet_daddr != raddr)
			continue;

		if (l2tp->conn_id != tunnel_id)
			continue;

		goto found;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	}

	sk = NULL;
found:
	return sk;
}

/* When processing receive frames, there are two cases to
 * consider. Data frames consist of a non-zero session-id and an
 * optional cookie. Control frames consist of a regular L2TP header
 * preceded by 32-bits of zeros.
 *
 * L2TPv3 Session Header Over IP
 *
 *  0                   1                   2                   3
 *  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
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                           Session ID                          |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |               Cookie (optional, maximum 64 bits)...
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *                                                                 |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * L2TPv3 Control Message Header Over IP
 *
 *  0                   1                   2                   3
 *  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
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                      (32 bits of zeros)                       |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |T|L|x|x|S|x|x|x|x|x|x|x|  Ver  |             Length            |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                     Control Connection ID                     |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |               Ns              |               Nr              |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * All control frames are passed to userspace.
 */
static int l2tp_ip_recv(struct sk_buff *skb)
{
119
	struct net *net = dev_net(skb->dev);
120 121 122 123 124 125
	struct sock *sk;
	u32 session_id;
	u32 tunnel_id;
	unsigned char *ptr, *optr;
	struct l2tp_session *session;
	struct l2tp_tunnel *tunnel = NULL;
126
	struct iphdr *iph;
127 128 129 130 131
	int length;

	if (!pskb_may_pull(skb, 4))
		goto discard;

132 133
	/* Point to L2TP header */
	optr = ptr = skb->data;
134 135 136 137 138 139 140 141 142 143 144 145 146
	session_id = ntohl(*((__be32 *) ptr));
	ptr += 4;

	/* RFC3931: L2TP/IP packets have the first 4 bytes containing
	 * the session_id. If it is 0, the packet is a L2TP control
	 * frame and the session_id value can be discarded.
	 */
	if (session_id == 0) {
		__skb_pull(skb, 4);
		goto pass_up;
	}

	/* Ok, this is a data packet. Lookup the session. */
147
	session = l2tp_session_get(net, NULL, session_id);
148
	if (!session)
149 150 151
		goto discard;

	tunnel = session->tunnel;
152 153
	if (!tunnel)
		goto discard_sess;
154 155 156 157 158

	/* Trace packet contents, if enabled */
	if (tunnel->debug & L2TP_MSG_DATA) {
		length = min(32u, skb->len);
		if (!pskb_may_pull(skb, length))
159
			goto discard_sess;
160

161 162 163
		/* Point to L2TP header */
		optr = ptr = skb->data;
		ptr += 4;
164 165
		pr_debug("%s: ip recv\n", tunnel->name);
		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
166 167 168
	}

	l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, tunnel->recv_payload_hook);
169
	l2tp_session_dec_refcount(session);
170 171 172 173 174 175 176 177 178 179 180 181

	return 0;

pass_up:
	/* Get the tunnel_id from the L2TP header */
	if (!pskb_may_pull(skb, 12))
		goto discard;

	if ((skb->data[0] & 0xc0) != 0xc0)
		goto discard;

	tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
182 183 184 185 186 187
	iph = (struct iphdr *)skb_network_header(skb);

	read_lock_bh(&l2tp_ip_lock);
	sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb),
				   tunnel_id);
	if (!sk) {
188
		read_unlock_bh(&l2tp_ip_lock);
189
		goto discard;
190
	}
191 192
	sock_hold(sk);
	read_unlock_bh(&l2tp_ip_lock);
193 194 195 196 197 198 199 200

	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
		goto discard_put;

	nf_reset(skb);

	return sk_receive_skb(sk, skb, 1);

201 202 203 204
discard_sess:
	l2tp_session_dec_refcount(session);
	goto discard;

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
discard_put:
	sock_put(sk);

discard:
	kfree_skb(skb);
	return 0;
}

static int l2tp_ip_open(struct sock *sk)
{
	/* Prevent autobind. We don't have ports. */
	inet_sk(sk)->inet_num = IPPROTO_L2TP;

	write_lock_bh(&l2tp_ip_lock);
	sk_add_node(sk, &l2tp_ip_table);
	write_unlock_bh(&l2tp_ip_lock);

	return 0;
}

static void l2tp_ip_close(struct sock *sk, long timeout)
{
	write_lock_bh(&l2tp_ip_lock);
	hlist_del_init(&sk->sk_bind_node);
229
	sk_del_node_init(sk);
230 231 232 233 234 235 236
	write_unlock_bh(&l2tp_ip_lock);
	sk_common_release(sk);
}

static void l2tp_ip_destroy_sock(struct sock *sk)
{
	struct sk_buff *skb;
237
	struct l2tp_tunnel *tunnel = sk->sk_user_data;
238 239 240 241

	while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
		kfree_skb(skb);

242 243
	if (tunnel)
		l2tp_tunnel_delete(tunnel);
244 245 246 247 248 249
}

static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
	struct inet_sock *inet = inet_sk(sk);
	struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
250
	struct net *net = sock_net(sk);
251
	int ret;
252 253
	int chk_addr_ret;

254 255 256 257 258
	if (addr_len < sizeof(struct sockaddr_l2tpip))
		return -EINVAL;
	if (addr->l2tp_family != AF_INET)
		return -EINVAL;

259
	lock_sock(sk);
260 261

	ret = -EINVAL;
262 263 264
	if (!sock_flag(sk, SOCK_ZAPPED))
		goto out;

265
	if (sk->sk_state != TCP_CLOSE)
266 267
		goto out;

268
	chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
269 270 271 272 273
	ret = -EADDRNOTAVAIL;
	if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
		goto out;

274 275
	if (addr->l2tp_addr.s_addr)
		inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
276 277 278
	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
		inet->inet_saddr = 0;  /* Use device */

279
	write_lock_bh(&l2tp_ip_lock);
280
	if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0,
281 282 283 284 285 286 287
				  sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
		write_unlock_bh(&l2tp_ip_lock);
		ret = -EADDRINUSE;
		goto out;
	}

	sk_dst_reset(sk);
288 289 290 291 292
	l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;

	sk_add_bind_node(sk, &l2tp_ip_bind_table);
	sk_del_node_init(sk);
	write_unlock_bh(&l2tp_ip_lock);
293

294
	ret = 0;
295 296
	sock_reset_flag(sk, SOCK_ZAPPED);

297 298 299 300 301 302 303 304 305
out:
	release_sock(sk);

	return ret;
}

static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
	struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
306
	int rc;
307 308

	if (addr_len < sizeof(*lsa))
309
		return -EINVAL;
D
David S. Miller 已提交
310

311
	if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
312
		return -EINVAL;
313

314
	lock_sock(sk);
315

316 317 318 319 320 321 322 323 324 325
	/* Must bind first - autobinding does not work */
	if (sock_flag(sk, SOCK_ZAPPED)) {
		rc = -EINVAL;
		goto out_sk;
	}

	rc = __ip4_datagram_connect(sk, uaddr, addr_len);
	if (rc < 0)
		goto out_sk;

326 327 328 329 330 331 332
	l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;

	write_lock_bh(&l2tp_ip_lock);
	hlist_del_init(&sk->sk_bind_node);
	sk_add_bind_node(sk, &l2tp_ip_bind_table);
	write_unlock_bh(&l2tp_ip_lock);

333
out_sk:
D
David S. Miller 已提交
334
	release_sock(sk);
335

336 337 338
	return rc;
}

339 340 341 342 343
static int l2tp_ip_disconnect(struct sock *sk, int flags)
{
	if (sock_flag(sk, SOCK_ZAPPED))
		return 0;

344
	return __udp_disconnect(sk, flags);
345 346
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
			   int *uaddr_len, int peer)
{
	struct sock *sk		= sock->sk;
	struct inet_sock *inet	= inet_sk(sk);
	struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
	struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;

	memset(lsa, 0, sizeof(*lsa));
	lsa->l2tp_family = AF_INET;
	if (peer) {
		if (!inet->inet_dport)
			return -ENOTCONN;
		lsa->l2tp_conn_id = lsk->peer_conn_id;
		lsa->l2tp_addr.s_addr = inet->inet_daddr;
	} else {
		__be32 addr = inet->inet_rcv_saddr;
		if (!addr)
			addr = inet->inet_saddr;
		lsa->l2tp_conn_id = lsk->conn_id;
		lsa->l2tp_addr.s_addr = addr;
	}
	*uaddr_len = sizeof(*lsa);
	return 0;
}

static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
{
	int rc;

	/* Charge it to the socket, dropping if the queue is full. */
	rc = sock_queue_rcv_skb(sk, skb);
	if (rc < 0)
		goto drop;

	return 0;

drop:
385
	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
386
	kfree_skb(skb);
387
	return 0;
388 389 390 391 392
}

/* Userspace will call sendmsg() on the tunnel socket to send L2TP
 * control frames.
 */
393
static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
394 395 396 397 398
{
	struct sk_buff *skb;
	int rc;
	struct inet_sock *inet = inet_sk(sk);
	struct rtable *rt = NULL;
399
	struct flowi4 *fl4;
400 401 402
	int connected = 0;
	__be32 daddr;

D
David S. Miller 已提交
403 404 405
	lock_sock(sk);

	rc = -ENOTCONN;
406
	if (sock_flag(sk, SOCK_DEAD))
D
David S. Miller 已提交
407
		goto out;
408 409 410

	/* Get and verify the address. */
	if (msg->msg_name) {
411
		DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name);
D
David S. Miller 已提交
412
		rc = -EINVAL;
413
		if (msg->msg_namelen < sizeof(*lip))
D
David S. Miller 已提交
414
			goto out;
415 416

		if (lip->l2tp_family != AF_INET) {
D
David S. Miller 已提交
417
			rc = -EAFNOSUPPORT;
418
			if (lip->l2tp_family != AF_UNSPEC)
D
David S. Miller 已提交
419
				goto out;
420 421 422 423
		}

		daddr = lip->l2tp_addr.s_addr;
	} else {
424
		rc = -EDESTADDRREQ;
425
		if (sk->sk_state != TCP_ESTABLISHED)
426
			goto out;
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

		daddr = inet->inet_daddr;
		connected = 1;
	}

	/* Allocate a socket buffer */
	rc = -ENOMEM;
	skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
			   4 + len, 0, GFP_KERNEL);
	if (!skb)
		goto error;

	/* Reserve space for headers, putting IP header on 4-byte boundary. */
	skb_reserve(skb, 2 + NET_SKB_PAD);
	skb_reset_network_header(skb);
	skb_reserve(skb, sizeof(struct iphdr));
	skb_reset_transport_header(skb);

	/* Insert 0 session_id */
	*((__be32 *) skb_put(skb, 4)) = 0;

	/* Copy user data into skb */
A
Al Viro 已提交
449
	rc = memcpy_from_msg(skb_put(skb, len), msg, len);
450 451 452 453 454
	if (rc < 0) {
		kfree_skb(skb);
		goto error;
	}

455
	fl4 = &inet->cork.fl.u.ip4;
456 457 458
	if (connected)
		rt = (struct rtable *) __sk_dst_check(sk, 0);

459
	rcu_read_lock();
460
	if (rt == NULL) {
461
		const struct ip_options_rcu *inet_opt;
462

D
David S. Miller 已提交
463
		inet_opt = rcu_dereference(inet->inet_opt);
464

465
		/* Use correct destination address if we have options. */
466 467
		if (inet_opt && inet_opt->opt.srr)
			daddr = inet_opt->opt.faddr;
468

469 470 471 472
		/* If this fails, retransmit mechanism of transport layer will
		 * keep trying until route appears or the connection times
		 * itself out.
		 */
473
		rt = ip_route_output_ports(sock_net(sk), fl4, sk,
474 475 476 477 478 479
					   daddr, inet->inet_saddr,
					   inet->inet_dport, inet->inet_sport,
					   sk->sk_protocol, RT_CONN_FLAGS(sk),
					   sk->sk_bound_dev_if);
		if (IS_ERR(rt))
			goto no_route;
480
		if (connected) {
481
			sk_setup_caps(sk, &rt->dst);
482 483 484 485
		} else {
			skb_dst_set(skb, &rt->dst);
			goto xmit;
		}
486
	}
487 488 489 490 491

	/* We dont need to clone dst here, it is guaranteed to not disappear.
	 *  __dev_xmit_skb() might force a refcount if needed.
	 */
	skb_dst_set_noref(skb, &rt->dst);
492

493
xmit:
494
	/* Queue the packet to IP for output */
495
	rc = ip_queue_xmit(sk, skb, &inet->cork.fl);
496
	rcu_read_unlock();
497 498

error:
499
	if (rc >= 0)
500 501
		rc = len;

D
David S. Miller 已提交
502 503
out:
	release_sock(sk);
504 505 506
	return rc;

no_route:
507
	rcu_read_unlock();
508 509
	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
	kfree_skb(skb);
D
David S. Miller 已提交
510 511
	rc = -EHOSTUNREACH;
	goto out;
512 513
}

514
static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
515 516 517 518 519
			   size_t len, int noblock, int flags, int *addr_len)
{
	struct inet_sock *inet = inet_sk(sk);
	size_t copied = 0;
	int err = -EOPNOTSUPP;
520
	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	struct sk_buff *skb;

	if (flags & MSG_OOB)
		goto out;

	skb = skb_recv_datagram(sk, flags, noblock, &err);
	if (!skb)
		goto out;

	copied = skb->len;
	if (len < copied) {
		msg->msg_flags |= MSG_TRUNC;
		copied = len;
	}

536
	err = skb_copy_datagram_msg(skb, 0, msg, copied);
537 538 539 540 541 542 543 544 545 546 547
	if (err)
		goto done;

	sock_recv_timestamp(msg, sk, skb);

	/* Copy the address. */
	if (sin) {
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
		sin->sin_port = 0;
		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
548
		*addr_len = sizeof(*sin);
549 550 551 552 553 554 555 556
	}
	if (inet->cmsg_flags)
		ip_cmsg_recv(msg, skb);
	if (flags & MSG_TRUNC)
		copied = skb->len;
done:
	skb_free_datagram(sk, skb);
out:
557
	return err ? err : copied;
558 559
}

E
Eric Dumazet 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
{
	struct sk_buff *skb;
	int amount;

	switch (cmd) {
	case SIOCOUTQ:
		amount = sk_wmem_alloc_get(sk);
		break;
	case SIOCINQ:
		spin_lock_bh(&sk->sk_receive_queue.lock);
		skb = skb_peek(&sk->sk_receive_queue);
		amount = skb ? skb->len : 0;
		spin_unlock_bh(&sk->sk_receive_queue.lock);
		break;

	default:
		return -ENOIOCTLCMD;
	}

	return put_user(amount, (int __user *)arg);
}
EXPORT_SYMBOL(l2tp_ioctl);

584
static struct proto l2tp_ip_prot = {
585 586 587 588 589 590
	.name		   = "L2TP/IP",
	.owner		   = THIS_MODULE,
	.init		   = l2tp_ip_open,
	.close		   = l2tp_ip_close,
	.bind		   = l2tp_ip_bind,
	.connect	   = l2tp_ip_connect,
591
	.disconnect	   = l2tp_ip_disconnect,
E
Eric Dumazet 已提交
592
	.ioctl		   = l2tp_ioctl,
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	.destroy	   = l2tp_ip_destroy_sock,
	.setsockopt	   = ip_setsockopt,
	.getsockopt	   = ip_getsockopt,
	.sendmsg	   = l2tp_ip_sendmsg,
	.recvmsg	   = l2tp_ip_recvmsg,
	.backlog_rcv	   = l2tp_ip_backlog_recv,
	.hash		   = inet_hash,
	.unhash		   = inet_unhash,
	.obj_size	   = sizeof(struct l2tp_ip_sock),
#ifdef CONFIG_COMPAT
	.compat_setsockopt = compat_ip_setsockopt,
	.compat_getsockopt = compat_ip_getsockopt,
#endif
};

static const struct proto_ops l2tp_ip_ops = {
	.family		   = PF_INET,
	.owner		   = THIS_MODULE,
	.release	   = inet_release,
	.bind		   = inet_bind,
	.connect	   = inet_dgram_connect,
	.socketpair	   = sock_no_socketpair,
	.accept		   = sock_no_accept,
	.getname	   = l2tp_ip_getname,
	.poll		   = datagram_poll,
	.ioctl		   = inet_ioctl,
	.listen		   = sock_no_listen,
	.shutdown	   = inet_shutdown,
	.setsockopt	   = sock_common_setsockopt,
	.getsockopt	   = sock_common_getsockopt,
	.sendmsg	   = inet_sendmsg,
	.recvmsg	   = sock_common_recvmsg,
	.mmap		   = sock_no_mmap,
	.sendpage	   = sock_no_sendpage,
#ifdef CONFIG_COMPAT
	.compat_setsockopt = compat_sock_common_setsockopt,
	.compat_getsockopt = compat_sock_common_getsockopt,
#endif
};

static struct inet_protosw l2tp_ip_protosw = {
	.type		= SOCK_DGRAM,
	.protocol	= IPPROTO_L2TP,
	.prot		= &l2tp_ip_prot,
	.ops		= &l2tp_ip_ops,
};

static struct net_protocol l2tp_ip_protocol __read_mostly = {
	.handler	= l2tp_ip_recv,
642
	.netns_ok	= 1,
643 644 645 646 647 648
};

static int __init l2tp_ip_init(void)
{
	int err;

649
	pr_info("L2TP IP encapsulation support (L2TPv3)\n");
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

	err = proto_register(&l2tp_ip_prot, 1);
	if (err != 0)
		goto out;

	err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
	if (err)
		goto out1;

	inet_register_protosw(&l2tp_ip_protosw);
	return 0;

out1:
	proto_unregister(&l2tp_ip_prot);
out:
	return err;
}

static void __exit l2tp_ip_exit(void)
{
	inet_unregister_protosw(&l2tp_ip_protosw);
	inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
	proto_unregister(&l2tp_ip_prot);
}

module_init(l2tp_ip_init);
module_exit(l2tp_ip_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
MODULE_DESCRIPTION("L2TP over IP");
MODULE_VERSION("1.0");
M
Michal Marek 已提交
682

683
/* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
M
Michal Marek 已提交
684 685 686
 * enums
 */
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);
S
stephen hemminger 已提交
687
MODULE_ALIAS_NET_PF_PROTO(PF_INET, IPPROTO_L2TP);