1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <asm/ioctls.h>
15#include <linux/icmp.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/random.h>
19#include <linux/socket.h>
20#include <linux/l2tp.h>
21#include <linux/in.h>
22#include <net/sock.h>
23#include <net/ip.h>
24#include <net/icmp.h>
25#include <net/udp.h>
26#include <net/inet_common.h>
27#include <net/inet_hashtables.h>
28#include <net/tcp_states.h>
29#include <net/protocol.h>
30#include <net/xfrm.h>
31
32#include "l2tp_core.h"
33
34struct l2tp_ip_sock {
35
36 struct inet_sock inet;
37
38 u32 conn_id;
39 u32 peer_conn_id;
40};
41
42static DEFINE_RWLOCK(l2tp_ip_lock);
43static struct hlist_head l2tp_ip_table;
44static struct hlist_head l2tp_ip_bind_table;
45
46static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
47{
48 return (struct l2tp_ip_sock *)sk;
49}
50
51static struct sock *__l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
52{
53 struct sock *sk;
54
55 sk_for_each_bound(sk, &l2tp_ip_bind_table) {
56 struct inet_sock *inet = inet_sk(sk);
57 struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
58
59 if (l2tp == NULL)
60 continue;
61
62 if ((l2tp->conn_id == tunnel_id) &&
63 net_eq(sock_net(sk), net) &&
64 !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
65 (!sk->sk_bound_dev_if || !dif ||
66 sk->sk_bound_dev_if == dif))
67 goto found;
68 }
69
70 sk = NULL;
71found:
72 return sk;
73}
74
75static inline struct sock *l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
76{
77 struct sock *sk = __l2tp_ip_bind_lookup(net, laddr, dif, tunnel_id);
78 if (sk)
79 sock_hold(sk);
80
81 return sk;
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
117static int l2tp_ip_recv(struct sk_buff *skb)
118{
119 struct net *net = dev_net(skb->dev);
120 struct sock *sk;
121 u32 session_id;
122 u32 tunnel_id;
123 unsigned char *ptr, *optr;
124 struct l2tp_session *session;
125 struct l2tp_tunnel *tunnel = NULL;
126 int length;
127
128
129 optr = ptr = skb->data;
130
131 if (!pskb_may_pull(skb, 4))
132 goto discard;
133
134 session_id = ntohl(*((__be32 *) ptr));
135 ptr += 4;
136
137
138
139
140
141 if (session_id == 0) {
142 __skb_pull(skb, 4);
143 goto pass_up;
144 }
145
146
147 session = l2tp_session_get(net, NULL, session_id, true);
148 if (!session)
149 goto discard;
150
151 tunnel = session->tunnel;
152 if (!tunnel)
153 goto discard_sess;
154
155
156 if (tunnel->debug & L2TP_MSG_DATA) {
157 length = min(32u, skb->len);
158 if (!pskb_may_pull(skb, length))
159 goto discard_sess;
160
161 pr_debug("%s: ip recv\n", tunnel->name);
162 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
163 }
164
165 l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, tunnel->recv_payload_hook);
166 l2tp_session_dec_refcount(session);
167
168 return 0;
169
170pass_up:
171
172 if (!pskb_may_pull(skb, 12))
173 goto discard;
174
175 if ((skb->data[0] & 0xc0) != 0xc0)
176 goto discard;
177
178 tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
179 tunnel = l2tp_tunnel_find(net, tunnel_id);
180 if (tunnel) {
181 sk = tunnel->sock;
182 sock_hold(sk);
183 } else {
184 struct iphdr *iph = (struct iphdr *) skb_network_header(skb);
185
186 read_lock_bh(&l2tp_ip_lock);
187 sk = __l2tp_ip_bind_lookup(net, iph->daddr, inet_iif(skb),
188 tunnel_id);
189 if (!sk) {
190 read_unlock_bh(&l2tp_ip_lock);
191 goto discard;
192 }
193
194 sock_hold(sk);
195 read_unlock_bh(&l2tp_ip_lock);
196 }
197
198 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
199 goto discard_put;
200
201 nf_reset(skb);
202
203 return sk_receive_skb(sk, skb, 1);
204
205discard_sess:
206 if (session->deref)
207 session->deref(session);
208 l2tp_session_dec_refcount(session);
209 goto discard;
210
211discard_put:
212 sock_put(sk);
213
214discard:
215 kfree_skb(skb);
216 return 0;
217}
218
219static int l2tp_ip_open(struct sock *sk)
220{
221
222 inet_sk(sk)->inet_num = IPPROTO_L2TP;
223
224 write_lock_bh(&l2tp_ip_lock);
225 sk_add_node(sk, &l2tp_ip_table);
226 write_unlock_bh(&l2tp_ip_lock);
227
228 return 0;
229}
230
231static void l2tp_ip_close(struct sock *sk, long timeout)
232{
233 write_lock_bh(&l2tp_ip_lock);
234 hlist_del_init(&sk->sk_bind_node);
235 sk_del_node_init(sk);
236 write_unlock_bh(&l2tp_ip_lock);
237 sk_common_release(sk);
238}
239
240static void l2tp_ip_destroy_sock(struct sock *sk)
241{
242 struct sk_buff *skb;
243 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
244
245 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
246 kfree_skb(skb);
247
248 if (tunnel) {
249 l2tp_tunnel_closeall(tunnel);
250 sock_put(sk);
251 }
252
253 sk_refcnt_debug_dec(sk);
254}
255
256static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
257{
258 struct inet_sock *inet = inet_sk(sk);
259 struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
260 struct net *net = sock_net(sk);
261 int ret;
262 int chk_addr_ret;
263
264 if (addr_len < sizeof(struct sockaddr_l2tpip))
265 return -EINVAL;
266 if (addr->l2tp_family != AF_INET)
267 return -EINVAL;
268
269 lock_sock(sk);
270
271 ret = -EINVAL;
272 if (!sock_flag(sk, SOCK_ZAPPED))
273 goto out;
274
275 if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_l2tpip))
276 goto out;
277
278 chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
279 ret = -EADDRNOTAVAIL;
280 if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
281 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
282 goto out;
283
284 if (addr->l2tp_addr.s_addr)
285 inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
286 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
287 inet->inet_saddr = 0;
288
289 write_lock_bh(&l2tp_ip_lock);
290 if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr,
291 sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
292 write_unlock_bh(&l2tp_ip_lock);
293 ret = -EADDRINUSE;
294 goto out;
295 }
296
297 sk_dst_reset(sk);
298 l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
299
300 sk_add_bind_node(sk, &l2tp_ip_bind_table);
301 sk_del_node_init(sk);
302 write_unlock_bh(&l2tp_ip_lock);
303
304 ret = 0;
305 sock_reset_flag(sk, SOCK_ZAPPED);
306
307out:
308 release_sock(sk);
309
310 return ret;
311}
312
313static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
314{
315 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
316 int rc;
317
318 if (sock_flag(sk, SOCK_ZAPPED))
319 return -EINVAL;
320
321 if (addr_len < sizeof(*lsa))
322 return -EINVAL;
323
324 if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
325 return -EINVAL;
326
327 rc = ip4_datagram_connect(sk, uaddr, addr_len);
328 if (rc < 0)
329 return rc;
330
331 lock_sock(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 release_sock(sk);
341 return rc;
342}
343
344static int l2tp_ip_disconnect(struct sock *sk, int flags)
345{
346 if (sock_flag(sk, SOCK_ZAPPED))
347 return 0;
348
349 return udp_disconnect(sk, flags);
350}
351
352static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
353 int *uaddr_len, int peer)
354{
355 struct sock *sk = sock->sk;
356 struct inet_sock *inet = inet_sk(sk);
357 struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
358 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
359
360 memset(lsa, 0, sizeof(*lsa));
361 lsa->l2tp_family = AF_INET;
362 if (peer) {
363 if (!inet->inet_dport)
364 return -ENOTCONN;
365 lsa->l2tp_conn_id = lsk->peer_conn_id;
366 lsa->l2tp_addr.s_addr = inet->inet_daddr;
367 } else {
368 __be32 addr = inet->inet_rcv_saddr;
369 if (!addr)
370 addr = inet->inet_saddr;
371 lsa->l2tp_conn_id = lsk->conn_id;
372 lsa->l2tp_addr.s_addr = addr;
373 }
374 *uaddr_len = sizeof(*lsa);
375 return 0;
376}
377
378static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
379{
380 int rc;
381
382
383 rc = sock_queue_rcv_skb(sk, skb);
384 if (rc < 0)
385 goto drop;
386
387 return 0;
388
389drop:
390 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
391 kfree_skb(skb);
392 return -1;
393}
394
395
396
397
398static int l2tp_ip_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len)
399{
400 struct sk_buff *skb;
401 int rc;
402 struct inet_sock *inet = inet_sk(sk);
403 struct rtable *rt = NULL;
404 struct flowi4 *fl4;
405 int connected = 0;
406 __be32 daddr;
407
408 lock_sock(sk);
409
410 rc = -ENOTCONN;
411 if (sock_flag(sk, SOCK_DEAD))
412 goto out;
413
414
415 if (msg->msg_name) {
416 struct sockaddr_l2tpip *lip = (struct sockaddr_l2tpip *) msg->msg_name;
417 rc = -EINVAL;
418 if (msg->msg_namelen < sizeof(*lip))
419 goto out;
420
421 if (lip->l2tp_family != AF_INET) {
422 rc = -EAFNOSUPPORT;
423 if (lip->l2tp_family != AF_UNSPEC)
424 goto out;
425 }
426
427 daddr = lip->l2tp_addr.s_addr;
428 } else {
429 rc = -EDESTADDRREQ;
430 if (sk->sk_state != TCP_ESTABLISHED)
431 goto out;
432
433 daddr = inet->inet_daddr;
434 connected = 1;
435 }
436
437
438 rc = -ENOMEM;
439 skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
440 4 + len, 0, GFP_KERNEL);
441 if (!skb)
442 goto error;
443
444
445 skb_reserve(skb, 2 + NET_SKB_PAD);
446 skb_reset_network_header(skb);
447 skb_reserve(skb, sizeof(struct iphdr));
448 skb_reset_transport_header(skb);
449
450
451 *((__be32 *) skb_put(skb, 4)) = 0;
452
453
454 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
455 if (rc < 0) {
456 kfree_skb(skb);
457 goto error;
458 }
459
460 fl4 = &inet->cork.fl.u.ip4;
461 if (connected)
462 rt = (struct rtable *) __sk_dst_check(sk, 0);
463
464 rcu_read_lock();
465 if (rt == NULL) {
466 const struct ip_options_rcu *inet_opt;
467
468 inet_opt = rcu_dereference(inet->inet_opt);
469
470
471 if (inet_opt && inet_opt->opt.srr)
472 daddr = inet_opt->opt.faddr;
473
474
475
476
477
478 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
479 daddr, inet->inet_saddr,
480 inet->inet_dport, inet->inet_sport,
481 sk->sk_protocol, RT_CONN_FLAGS(sk),
482 sk->sk_bound_dev_if);
483 if (IS_ERR(rt))
484 goto no_route;
485 if (connected) {
486 sk_setup_caps(sk, &rt->dst);
487 } else {
488 skb_dst_set(skb, &rt->dst);
489 goto xmit;
490 }
491 }
492
493
494
495
496 skb_dst_set_noref(skb, &rt->dst);
497
498xmit:
499
500 rc = ip_queue_xmit(skb, &inet->cork.fl);
501 rcu_read_unlock();
502
503error:
504 if (rc >= 0)
505 rc = len;
506
507out:
508 release_sock(sk);
509 return rc;
510
511no_route:
512 rcu_read_unlock();
513 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
514 kfree_skb(skb);
515 rc = -EHOSTUNREACH;
516 goto out;
517}
518
519static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
520 size_t len, int noblock, int flags, int *addr_len)
521{
522 struct inet_sock *inet = inet_sk(sk);
523 size_t copied = 0;
524 int err = -EOPNOTSUPP;
525 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
526 struct sk_buff *skb;
527
528 if (flags & MSG_OOB)
529 goto out;
530
531 skb = skb_recv_datagram(sk, flags, noblock, &err);
532 if (!skb)
533 goto out;
534
535 copied = skb->len;
536 if (len < copied) {
537 msg->msg_flags |= MSG_TRUNC;
538 copied = len;
539 }
540
541 err = skb_copy_datagram_msg(skb, 0, msg, copied);
542 if (err)
543 goto done;
544
545 sock_recv_timestamp(msg, sk, skb);
546
547
548 if (sin) {
549 sin->sin_family = AF_INET;
550 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
551 sin->sin_port = 0;
552 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
553 *addr_len = sizeof(*sin);
554 }
555 if (inet->cmsg_flags)
556 ip_cmsg_recv(msg, skb);
557 if (flags & MSG_TRUNC)
558 copied = skb->len;
559done:
560 skb_free_datagram(sk, skb);
561out:
562 return err ? err : copied;
563}
564
565int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
566{
567 struct sk_buff *skb;
568 int amount;
569
570 switch (cmd) {
571 case SIOCOUTQ:
572 amount = sk_wmem_alloc_get(sk);
573 break;
574 case SIOCINQ:
575 spin_lock_bh(&sk->sk_receive_queue.lock);
576 skb = skb_peek(&sk->sk_receive_queue);
577 amount = skb ? skb->len : 0;
578 spin_unlock_bh(&sk->sk_receive_queue.lock);
579 break;
580
581 default:
582 return -ENOIOCTLCMD;
583 }
584
585 return put_user(amount, (int __user *)arg);
586}
587EXPORT_SYMBOL(l2tp_ioctl);
588
589static struct proto l2tp_ip_prot = {
590 .name = "L2TP/IP",
591 .owner = THIS_MODULE,
592 .init = l2tp_ip_open,
593 .close = l2tp_ip_close,
594 .bind = l2tp_ip_bind,
595 .connect = l2tp_ip_connect,
596 .disconnect = l2tp_ip_disconnect,
597 .ioctl = l2tp_ioctl,
598 .destroy = l2tp_ip_destroy_sock,
599 .setsockopt = ip_setsockopt,
600 .getsockopt = ip_getsockopt,
601 .sendmsg = l2tp_ip_sendmsg,
602 .recvmsg = l2tp_ip_recvmsg,
603 .backlog_rcv = l2tp_ip_backlog_recv,
604 .hash = inet_hash,
605 .unhash = inet_unhash,
606 .obj_size = sizeof(struct l2tp_ip_sock),
607#ifdef CONFIG_COMPAT
608 .compat_setsockopt = compat_ip_setsockopt,
609 .compat_getsockopt = compat_ip_getsockopt,
610#endif
611};
612
613static const struct proto_ops l2tp_ip_ops = {
614 .family = PF_INET,
615 .owner = THIS_MODULE,
616 .release = inet_release,
617 .bind = inet_bind,
618 .connect = inet_dgram_connect,
619 .socketpair = sock_no_socketpair,
620 .accept = sock_no_accept,
621 .getname = l2tp_ip_getname,
622 .poll = datagram_poll,
623 .ioctl = inet_ioctl,
624 .listen = sock_no_listen,
625 .shutdown = inet_shutdown,
626 .setsockopt = sock_common_setsockopt,
627 .getsockopt = sock_common_getsockopt,
628 .sendmsg = inet_sendmsg,
629 .recvmsg = sock_common_recvmsg,
630 .mmap = sock_no_mmap,
631 .sendpage = sock_no_sendpage,
632#ifdef CONFIG_COMPAT
633 .compat_setsockopt = compat_sock_common_setsockopt,
634 .compat_getsockopt = compat_sock_common_getsockopt,
635#endif
636};
637
638static struct inet_protosw l2tp_ip_protosw = {
639 .type = SOCK_DGRAM,
640 .protocol = IPPROTO_L2TP,
641 .prot = &l2tp_ip_prot,
642 .ops = &l2tp_ip_ops,
643};
644
645static struct net_protocol l2tp_ip_protocol __read_mostly = {
646 .handler = l2tp_ip_recv,
647 .netns_ok = 1,
648};
649
650static int __init l2tp_ip_init(void)
651{
652 int err;
653
654 pr_info("L2TP IP encapsulation support (L2TPv3)\n");
655
656 err = proto_register(&l2tp_ip_prot, 1);
657 if (err != 0)
658 goto out;
659
660 err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
661 if (err)
662 goto out1;
663
664 inet_register_protosw(&l2tp_ip_protosw);
665 return 0;
666
667out1:
668 proto_unregister(&l2tp_ip_prot);
669out:
670 return err;
671}
672
673static void __exit l2tp_ip_exit(void)
674{
675 inet_unregister_protosw(&l2tp_ip_protosw);
676 inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
677 proto_unregister(&l2tp_ip_prot);
678}
679
680module_init(l2tp_ip_init);
681module_exit(l2tp_ip_exit);
682
683MODULE_LICENSE("GPL");
684MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
685MODULE_DESCRIPTION("L2TP over IP");
686MODULE_VERSION("1.0");
687
688
689
690
691MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);
692