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