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