2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Network routines for UDP handling
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
24 #include <sys/ioctl.h>
30 #include "ipsecmast.h"
31 #include "misc.h" /* for IPADDY macro */
34 struct sockaddr_in server
, from
; /* Server and transmitter structs */
35 int server_socket
; /* Server socket */
37 int kernel_support
; /* Kernel Support there or not? */
42 char * modules
[] = { "l2tp_ppp", "pppol2tp", NULL
};
45 int pid
, exit_status
, fd
;
47 FILE * fmod
= fopen("/proc/modules", "r");
52 while (fgets(buf
, 255, fmod
) != NULL
) {
53 if ((tok
= strtok(buf
, " ")) != NULL
) {
54 for (module
= modules
; *module
!= NULL
; ++module
) {
55 if (!strcmp(*module
, tok
)) {
65 for (module
= modules
; *module
!= NULL
; ++module
) {
66 if ((pid
= fork()) >= 0) {
68 setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
69 if ((fd
= open("/dev/null", O_RDWR
)) > -1) {
73 execlp("modprobe", "modprobe", "-q", *module
, (char *)NULL
);
76 if ((pid
= waitpid(pid
, &exit_status
, 0)) != -1 && WIFEXITED(exit_status
)) {
77 if (WEXITSTATUS(exit_status
) == 0)
86 int init_network (void)
89 unsigned int length
= sizeof (server
);
90 gethostname (hostname
, sizeof (hostname
));
91 server
.sin_family
= AF_INET
;
92 server
.sin_addr
.s_addr
= gconfig
.listenaddr
;
93 server
.sin_port
= htons (gconfig
.port
);
95 if ((server_socket
= socket (PF_INET
, SOCK_DGRAM
, 0)) < 0)
97 l2tp_log (LOG_CRIT
, "%s: Unable to allocate socket. Terminating.\n",
103 setsockopt(server_socket
, SOL_SOCKET
, SO_REUSEADDR
, &flags
, sizeof(flags
));
104 setsockopt(server_socket
, SOL_SOCKET
, SO_NO_CHECK
, &flags
, sizeof(flags
));
106 if (bind (server_socket
, (struct sockaddr
*) &server
, sizeof (server
)))
108 close (server_socket
);
109 l2tp_log (LOG_CRIT
, "%s: Unable to bind socket: %s. Terminating.\n",
110 __FUNCTION__
, strerror(errno
), errno
);
113 if (getsockname (server_socket
, (struct sockaddr
*) &server
, &length
))
115 l2tp_log (LOG_CRIT
, "%s: Unable to read socket name.Terminating.\n",
122 * For L2TP/IPsec with KLIPSng, set the socket to receive IPsec REFINFO
126 if(setsockopt(server_socket
, IPPROTO_IP
, gconfig
.sarefnum
,
127 &arg
, sizeof(arg
)) != 0) {
128 l2tp_log(LOG_CRIT
, "setsockopt recvref[%d]: %s\n", gconfig
.sarefnum
, strerror(errno
));
130 gconfig
.ipsecsaref
=0;
133 l2tp_log(LOG_INFO
, "No attempt being made to use IPsec SAref's since we're not on a Linux machine.\n");
138 if (gconfig
.forceuserspace
)
140 l2tp_log (LOG_INFO
, "Not looking for kernel support.\n");
146 int kernel_fd
= socket(AF_PPPOX
, SOCK_DGRAM
, PX_PROTO_OL2TP
);
149 l2tp_log (LOG_INFO
, "L2TP kernel support not detected.\n");
155 l2tp_log (LOG_INFO
, "Using l2tp kernel support.\n");
160 l2tp_log (LOG_INFO
, "This binary does not support kernel L2TP.\n");
162 arg
= fcntl (server_socket
, F_GETFL
);
164 fcntl (server_socket
, F_SETFL
, arg
);
165 gconfig
.port
= ntohs (server
.sin_port
);
169 inline void extract (void *buf
, int *tunnel
, int *call
)
172 * Extract the tunnel and call #'s, and fix the order of the
176 struct payload_hdr
*p
= (struct payload_hdr
*) buf
;
189 inline void fix_hdr (void *buf
)
192 * Fix the byte order of the header
195 struct payload_hdr
*p
= (struct payload_hdr
*) buf
;
196 _u16 ver
= ntohs (p
->ver
);
200 * Control headers are always
201 * exactly 12 bytes big.
218 void dethrottle (void *call
)
220 /* struct call *c = (struct call *)call; */
221 /* if (c->throttle) {
223 log(LOG_DEBUG, "%s: dethrottling call %d, and setting R-bit\n",__FUNCTION__,c->ourcid);
224 #endif c->rbit = RBIT;
227 log(LOG_DEBUG, "%s: call %d already dethrottled?\n",__FUNCTION__,c->ourcid);
231 void control_xmit (void *b
)
233 struct buffer
*buf
= (struct buffer
*) b
;
240 l2tp_log (LOG_WARNING
, "%s: called on NULL buffer!\n", __FUNCTION__
);
245 #ifdef DEBUG_CONTROL_XMIT
248 "trying to send control packet to %d\n",
254 ns
= ntohs (((struct control_hdr
*) (buf
->start
))->Ns
);
259 #ifdef DEBUG_CONTROL_XMIT
260 l2tp_log (LOG_DEBUG
, "%s: Tossing packet %d\n", __FUNCTION__
, ns
);
262 /* Okay, it's been received. Let's toss it now */
267 if (buf
->retries
> DEFAULT_MAX_RETRIES
)
270 * Too many retries. Either kill the tunnel, or
271 * if there is no tunnel, just stop retransmitting.
275 if (t
->self
->needclose
)
278 "Unable to deliver closing message for tunnel %d. Destroying anyway.\n",
280 t
->self
->needclose
= 0;
281 t
->self
->closing
= -1;
285 l2tp_log (LOG_NOTICE
,
286 "Maximum retries exceeded for tunnel %d. Closing.\n",
288 strcpy (t
->self
->errormsg
, "Timeout");
289 t
->self
->needclose
= -1;
298 * FIXME: How about adaptive timeouts?
302 schedule (tv
, control_xmit
, buf
);
303 #ifdef DEBUG_CONTROL_XMIT
304 l2tp_log (LOG_DEBUG
, "%s: Scheduling and transmitting packet %d\n",
311 void udp_xmit (struct buffer
*buf
, struct tunnel
*t
)
313 struct cmsghdr
*cmsg
;
314 char cbuf
[CMSG_SPACE(sizeof (unsigned int))];
321 * OKAY, now send a packet with the right SAref values.
323 memset(&msgh
, 0, sizeof(struct msghdr
));
325 msgh
.msg_control
= cbuf
;
326 msgh
.msg_controllen
= 0;
328 if(gconfig
.ipsecsaref
&& t
->refhim
!= IPSEC_SAREF_NULL
) {
329 msgh
.msg_controllen
= sizeof(cbuf
);
331 cmsg
= CMSG_FIRSTHDR(&msgh
);
332 cmsg
->cmsg_level
= IPPROTO_IP
;
333 cmsg
->cmsg_type
= gconfig
.sarefnum
;
334 cmsg
->cmsg_len
= CMSG_LEN(sizeof(unsigned int));
336 if(gconfig
.debug_network
) {
337 l2tp_log(LOG_DEBUG
,"sending with saref=%d using sarefnum=%d\n", t
->refhim
, gconfig
.sarefnum
);
339 refp
= (unsigned int *)CMSG_DATA(cmsg
);
342 msgh
.msg_controllen
= cmsg
->cmsg_len
;
345 iov
.iov_base
= buf
->start
;
346 iov
.iov_len
= buf
->len
;
348 /* return packet from whence it came */
349 msgh
.msg_name
= &buf
->peer
;
350 msgh
.msg_namelen
= sizeof(buf
->peer
);
357 /* Receive one packet. */
358 if ((err
= sendmsg(server_socket
, &msgh
, 0)) < 0) {
359 l2tp_log(LOG_ERR
, "udp_xmit failed to %s:%d with err=%d:%s\n",
360 IPADDY(t
->peer
.sin_addr
), ntohs(t
->peer
.sin_port
),
361 err
,strerror(errno
));
365 int build_fdset (fd_set
*readfds
)
376 if (tun
->udp_fd
> -1) {
377 if (tun
->udp_fd
> max
)
379 FD_SET (tun
->udp_fd
, readfds
);
381 call
= tun
->call_head
;
384 if (call
->needclose
^ call
->closing
)
387 call
= tun
->call_head
;
394 if (!call
->needclose
&& !call
->closing
)
398 FD_SET (call
->fd
, readfds
);
403 /* Now that call fds have been collected, and checked for
404 * closing, check if the tunnel needs to be closed too
406 if (tun
->self
->needclose
^ tun
->self
->closing
)
408 if (gconfig
.debug_tunnel
)
409 l2tp_log (LOG_DEBUG
, "%s: closing down tunnel %d\n",
410 __FUNCTION__
, tun
->ourtid
);
411 call_close (tun
->self
);
412 /* Reset the while loop
413 * and check for NULL */
421 FD_SET (server_socket
, readfds
);
422 if (server_socket
> max
)
424 FD_SET (control_fd
, readfds
);
425 if (control_fd
> max
)
430 void network_thread ()
433 * We loop forever waiting on either data from the ppp drivers or from
434 * our network socket. Control handling is no longer done here.
436 struct sockaddr_in from
, to
;
437 unsigned int fromlen
;
438 int tunnel
, call
; /* Tunnel and call */
439 int recvsize
; /* Length of data received */
440 struct buffer
*buf
; /* Payload buffer */
441 struct call
*c
, *sc
; /* Call to send this off to */
442 struct tunnel
*st
; /* Tunnel */
443 fd_set readfds
; /* Descriptors to watch for reading */
444 int max
; /* Highest fd */
445 struct timeval tv
, *ptv
; /* Timeout for select */
449 unsigned int refme
, refhim
;
451 int server_socket_processed
;
453 /* This one buffer can be recycled for everything except control packets */
454 buf
= new_buf (MAX_RECV_SIZE
);
463 max
= build_fdset (&readfds
);
464 ptv
= process_schedule(&tv
);
465 ret
= select (max
+ 1, &readfds
, NULL
, NULL
, ptv
);
470 if (gconfig
.debug_network
)
472 l2tp_log (LOG_DEBUG
, "%s: select timeout\n", __FUNCTION__
);
477 if (gconfig
.debug_network
)
480 "%s: select returned error %d (%s)\n",
481 __FUNCTION__
, errno
, strerror (errno
));
486 if (FD_ISSET (control_fd
, &readfds
))
490 server_socket_processed
= 0;
493 while (st
|| !server_socket_processed
) {
494 if (st
&& (st
->udp_fd
== -1)) {
499 currentfd
= &st
->udp_fd
;
501 currentfd
= &server_socket
;
502 server_socket_processed
= 1;
504 if (FD_ISSET (*currentfd
, &readfds
))
507 * Okay, now we're ready for reading and processing new data.
511 /* Reserve space for expanding payload packet headers */
512 buf
->start
+= PAYLOAD_BUF
;
513 buf
->len
-= PAYLOAD_BUF
;
515 memset(&from
, 0, sizeof(from
));
516 memset(&to
, 0, sizeof(to
));
518 fromlen
= sizeof(from
);
520 memset(&msgh
, 0, sizeof(struct msghdr
));
521 iov
.iov_base
= buf
->start
;
522 iov
.iov_len
= buf
->len
;
523 msgh
.msg_control
= cbuf
;
524 msgh
.msg_controllen
= sizeof(cbuf
);
525 msgh
.msg_name
= &from
;
526 msgh
.msg_namelen
= fromlen
;
531 /* Receive one packet. */
532 recvsize
= recvmsg(*currentfd
, &msgh
, 0);
534 if (recvsize
< MIN_PAYLOAD_HDR_LEN
)
538 if (errno
== ECONNREFUSED
) {
541 if ((errno
== ECONNREFUSED
) ||
546 l2tp_log (LOG_WARNING
,
547 "%s: recvfrom returned error %d (%s)\n",
548 __FUNCTION__
, errno
, strerror (errno
));
552 l2tp_log (LOG_WARNING
, "%s: received too small a packet\n",
561 /* extract IPsec info out */
562 if(gconfig
.ipsecsaref
) {
563 struct cmsghdr
*cmsg
;
564 /* Process auxiliary received data in msgh */
565 for (cmsg
= CMSG_FIRSTHDR(&msgh
);
567 cmsg
= CMSG_NXTHDR(&msgh
,cmsg
)) {
568 if (cmsg
->cmsg_level
== IPPROTO_IP
569 && cmsg
->cmsg_type
== gconfig
.sarefnum
) {
572 refp
= (unsigned int *)CMSG_DATA(cmsg
);
580 * some logic could be added here to verify that we only
581 * get L2TP packets inside of IPsec, or to provide different
582 * classes of service to packets not inside of IPsec.
585 fix_hdr (buf
->start
);
586 extract (buf
->start
, &tunnel
, &call
);
588 if (gconfig
.debug_network
)
590 l2tp_log(LOG_DEBUG
, "%s: recv packet from %s, size = %d, "
591 "tunnel = %d, call = %d ref=%u refhim=%u\n",
592 __FUNCTION__
, inet_ntoa (from
.sin_addr
),
593 recvsize
, tunnel
, call
, refme
, refhim
);
596 if (gconfig
.packet_dump
)
598 do_packet_dump (buf
);
601 (c
= get_call (tunnel
, call
, from
.sin_addr
,
602 from
.sin_port
, refme
, refhim
)))
605 get_tunnel (tunnel
, from
.sin_addr
.s_addr
,
609 * It is theoretically possible that we could be sent
610 * a control message (say a StopCCN) on a call that we
611 * have already closed or some such nonsense. To
612 * prevent this from closing the tunnel, if we get a
613 * call on a valid tunnel, but not with a valid CID,
614 * we'll just send a ZLB to ack receiving the packet.
616 if (gconfig
.debug_tunnel
)
618 "%s: no such call %d on tunnel %d. Sending special ZLB\n",
620 handle_special (buf
, c
, call
);
622 /* get a new buffer */
623 buf
= new_buf (MAX_RECV_SIZE
);
627 "%s: unable to find call or tunnel to handle packet. call = %d, tunnel = %d Dumping.\n",
628 __FUNCTION__
, call
, tunnel
);
634 /* Handle the packet */
635 c
->container
->chal_us
.vector
= NULL
;
636 if (handle_packet (buf
, c
->container
, c
))
638 if (gconfig
.debug_tunnel
)
639 l2tp_log (LOG_DEBUG
, "%s: bad packet\n", __FUNCTION__
);
643 /* Send Zero Byte Packet */
644 control_zlb (buf
, c
->container
, c
);
653 * finished obvious sources, look for data from PPP connections.
661 if ((sc
->fd
>= 0) && FD_ISSET (sc
->fd
, &readfds
))
663 /* Got some payload to send */
665 recycle_payload (buf
, sc
->container
->peer
);
667 #ifdef DEBUG_FLOW_MORE
668 l2tp_log (LOG_DEBUG, "%s: rws = %d, pSs = %d, pLr = %d\n",
669 __FUNCTION__, sc->rws, sc->pSs, sc->pLr);
671 if ((sc->rws>0) && (sc->pSs > sc->pLr + sc->rws) && !sc->rbit) {
673 log(LOG_DEBUG, "%s: throttling payload (call = %d, tunnel = %d, Lr = %d, Ss = %d, rws = %d)!\n",__FUNCTION__,
674 sc->cid, sc->container->tid, sc->pLr, sc->pSs, sc->rws);
677 We unthrottle in handle_packet if we get a payload packet,
678 valid or ZLB, but we also schedule a dethrottle in which
679 case the R-bit will be set
680 FIXME: Rate Adaptive timeout?
683 sc->dethrottle = schedule(tv, dethrottle, sc);
685 /* while ((result=read_packet(buf,sc->fd,sc->frame & SYNC_FRAMING))>0) { */
687 read_packet (buf
, sc
->fd
, SYNC_FRAMING
)) > 0)
689 add_payload_hdr (sc
->container
, sc
, buf
);
690 if (gconfig
.packet_dump
)
692 do_packet_dump (buf
);
696 sc
->prx
= sc
->data_rec_seq_num
;
699 deschedule (sc
->zlb_xmit
);
702 sc
->tx_bytes
+= buf
->len
;
705 recycle_payload (buf
, sc
->container
->peer
);
709 l2tp_log (LOG_WARNING
,
710 "%s: tossing read packet, error = %s (%d). Closing call.\n",
711 __FUNCTION__
, strerror (-result
), -result
);
712 strcpy (sc
->errormsg
, strerror (-result
));
724 int connect_pppol2tp(struct tunnel
*t
) {
726 if (kernel_support
) {
727 int ufd
= -1, fd2
= -1;
729 struct sockaddr_pppol2tp sax
;
731 struct sockaddr_in server
;
732 server
.sin_family
= AF_INET
;
733 server
.sin_addr
.s_addr
= gconfig
.listenaddr
;
734 server
.sin_port
= htons (gconfig
.port
);
735 if ((ufd
= socket (PF_INET
, SOCK_DGRAM
, 0)) < 0)
737 l2tp_log (LOG_CRIT
, "%s: Unable to allocate UDP socket. Terminating.\n",
743 setsockopt(ufd
, SOL_SOCKET
, SO_REUSEADDR
, &flags
, sizeof(flags
));
744 setsockopt(ufd
, SOL_SOCKET
, SO_NO_CHECK
, &flags
, sizeof(flags
));
746 if (bind (ufd
, (struct sockaddr
*) &server
, sizeof (server
)))
749 l2tp_log (LOG_CRIT
, "%s: Unable to bind UDP socket: %s. Terminating.\n",
750 __FUNCTION__
, strerror(errno
), errno
);
754 flags
= fcntl(ufd
, F_GETFL
);
755 if (flags
== -1 || fcntl(ufd
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
756 l2tp_log (LOG_WARNING
, "%s: Unable to set UDP socket nonblock.\n",
760 if (connect (ufd
, (struct sockaddr
*) &server
, sizeof(server
)) < 0) {
761 l2tp_log (LOG_CRIT
, "%s: Unable to connect UDP peer. Terminating.\n",
768 fd2
= socket(AF_PPPOX
, SOCK_DGRAM
, PX_PROTO_OL2TP
);
770 l2tp_log (LOG_WARNING
, "%s: Unable to allocate PPPoL2TP socket.\n",
774 flags
= fcntl(fd2
, F_GETFL
);
775 if (flags
== -1 || fcntl(fd2
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
776 l2tp_log (LOG_WARNING
, "%s: Unable to set PPPoL2TP socket nonblock.\n",
780 sax
.sa_family
= AF_PPPOX
;
781 sax
.sa_protocol
= PX_PROTO_OL2TP
;
782 sax
.pppol2tp
.pid
= 0;
783 sax
.pppol2tp
.fd
= t
->udp_fd
;
784 sax
.pppol2tp
.addr
.sin_addr
.s_addr
= t
->peer
.sin_addr
.s_addr
;
785 sax
.pppol2tp
.addr
.sin_port
= t
->peer
.sin_port
;
786 sax
.pppol2tp
.addr
.sin_family
= AF_INET
;
787 sax
.pppol2tp
.s_tunnel
= t
->ourtid
;
788 sax
.pppol2tp
.s_session
= 0;
789 sax
.pppol2tp
.d_tunnel
= t
->tid
;
790 sax
.pppol2tp
.d_session
= 0;
791 if ((connect(fd2
, (struct sockaddr
*)&sax
, sizeof(sax
))) < 0) {
792 l2tp_log (LOG_WARNING
, "%s: Unable to connect PPPoL2TP socket. %d %s\n",
793 __FUNCTION__
, errno
, strerror(errno
));