2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
9 * This software is distributed under the terms
10 * of the GPL, which you should have received
11 * along with this source.
17 #define _ISOC99_SOURCE
20 #define _XOPEN_SOURCE_EXTENDED
24 #include <sys/types.h>
25 #include <sys/utsname.h>
34 # if defined(FREEBSD) || defined(OPENBSD)
35 # include <sys/signal.h>
37 # include <bsd/signal.h>
38 # elif defined(SOLARIS)
45 # include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <net/route.h>
56 #include <sys/ioctl.h>
59 struct tunnel_list tunnels
;
61 int ppd
= 1; /* Packet processing delay */
62 int control_fd
; /* descriptor of control area */
65 char *dial_no_tmp
; /* jz: Dialnumber for Outgoing Call */
66 int switch_io
= 0; /* jz: Switch for Incoming or Outgoing Call */
68 static void open_controlfd(void);
70 volatile sig_atomic_t sigterm_received
;
71 volatile sig_atomic_t sigint_received
;
72 volatile sig_atomic_t sigchld_received
;
73 volatile sig_atomic_t sigusr1_received
;;
74 volatile sig_atomic_t sighup_received
;
76 void init_tunnel_list (struct tunnel_list
*t
)
83 /* Now sends to syslog instead - MvO */
84 void show_status (void)
86 struct schedule_entry
*se
;
92 unsigned long cnt
= 0;
95 l2tp_log (LOG_WARNING
, "====== xl2tpd statistics ========\n");
96 l2tp_log (LOG_WARNING
, " Scheduler entries:\n");
101 t
= (struct tunnel
*) se
->data
;
102 tlac
= (struct lac
*) se
->data
;
103 c
= (struct call
*) se
->data
;
104 if (se
->func
== &hello
)
106 l2tp_log (LOG_WARNING
, "%d: HELLO to %d\n", s
, t
->tid
);
108 else if (se
->func
== &magic_lac_dial
)
110 l2tp_log (LOG_WARNING
, "%d: Magic dial on %s\n", s
, tlac
->entname
);
112 else if (se
->func
== &send_zlb
)
114 l2tp_log (LOG_WARNING
, "%d: Send payload ZLB on call %d:%d\n", s
,
115 c
->container
->tid
, c
->cid
);
117 else if (se
->func
== &dethrottle
)
119 l2tp_log (LOG_WARNING
, "%d: Dethrottle call %d:%d\n", s
, c
->container
->tid
,
122 else if (se
->func
== &control_xmit
)
124 l2tp_log (LOG_WARNING
, "%d: Control xmit on %d\n", s
,((struct buffer
*)se
->data
)->tunnel
->tid
);
127 l2tp_log (LOG_WARNING
, "%d: Unknown event\n", s
);
130 l2tp_log (LOG_WARNING
, "Total Events scheduled: %d\n", s
);
131 l2tp_log (LOG_WARNING
, "Number of tunnels open: %d\n", tunnels
.count
);
135 l2tp_log (LOG_WARNING
, "Tunnel %s, ID = %d (local), %d (remote) to %s:%d,"
136 " control_seq_num = %d, control_rec_seq_num = %d,"
137 " cLr = %d, call count = %d ref=%u/refhim=%u",
138 (t
->lac
? t
->lac
->entname
: (t
->lns
? t
->lns
->entname
: "")),
139 t
->ourtid
, t
->tid
, IPADDY (t
->peer
.sin_addr
),
140 ntohs (t
->peer
.sin_port
), t
->control_seq_num
,
141 t
->control_rec_seq_num
, t
->cLr
, t
->count
,
142 t
->refme
, t
->refhim
);
147 l2tp_log (LOG_WARNING
,
148 "Call %s # %lu, ID = %d (local), %d (remote), serno = %u,"
149 " data_seq_num = %d, data_rec_seq_num = %d,"
150 " pLr = %d, tx = %u bytes (%u), rx= %u bytes (%u)",
152 entname
: (c
->lns
? c
->lns
->entname
: "")),
154 c
->cid
, c
->serno
, c
->data_seq_num
, c
->data_rec_seq_num
,
155 c
->pLr
, c
->tx_bytes
, c
->tx_pkts
, c
->rx_bytes
, c
->rx_pkts
);
160 l2tp_log (LOG_WARNING
, "==========Config File===========\n");
164 l2tp_log (LOG_WARNING
, "LNS entry %s\n",
165 tlns
->entname
[0] ? tlns
->entname
: "(unnamed)");
171 l2tp_log (LOG_WARNING
, "LAC entry %s, LNS is/are:",
172 tlac
->entname
[0] ? tlac
->entname
: "(unnamed)");
178 l2tp_log (LOG_WARNING
, " %s", h
->hostname
);
183 l2tp_log (LOG_WARNING
, " [none]");
186 l2tp_log (LOG_WARNING
, "================================\n");
189 void null_handler(int sig
)
192 * A sighup is received when a call is terminated, unknown origine ..
193 * I catch it and ll looks good, but ..
197 void status_handler (int sig
)
202 void child_handler (int signal
)
205 * Oops, somebody we launched was killed.
206 * It's time to reap them and close that call.
207 * But first, we have to find out what PID died.
208 * unfortunately, pppd will
214 /* Keep looping until all are cleared */
217 pid
= waitpid (-1, &status
, WNOHANG
);
221 * Oh well, nobody there. Maybe we reaped it
222 * somewhere else already
226 /* find the call that "owned" the pppd which just died */
236 if ( WIFEXITED( status
) )
238 l2tp_log (LOG_DEBUG
, "%s : pppd exited for call %d with code %d\n", __FUNCTION__
,
239 c
->cid
, WEXITSTATUS( status
) );
241 else if( WIFSIGNALED( status
) )
243 l2tp_log (LOG_DEBUG
, "%s : pppd terminated for call %d by signal %d\n", __FUNCTION__
,
244 c
->cid
, WTERMSIG( status
) );
248 l2tp_log (LOG_DEBUG
, "%s : pppd exited for call %d for unknown reason\n", __FUNCTION__
,
253 * OK...pppd died, we can go ahead and close the pty for
262 * terminate tunnel and call loops, returning to the
263 * for(;;) loop (and possibly get the next pid)
274 void death_handler (int signal
)
277 * If we get here, somebody terminated us with a kill or a control-c.
278 * we call call_close on each tunnel twice to get a StopCCN out
279 * for each one (we can't pause to make sure it's received.
280 * Then we close the connections
282 struct tunnel
*st
, *st2
;
284 l2tp_log (LOG_CRIT
, "%s: Fatal signal %d received\n", __FUNCTION__
, signal
);
285 if (signal
!= SIGTERM
) {
290 strcpy (st
->self
->errormsg
, "Server closing");
291 sec
= st
->self
->closing
;
294 call_close (st
->self
);
297 st
->self
->closing
= -1;
298 call_close (st
->self
);
304 /* erase pid and control files */
305 unlink (gconfig
.pidfile
);
306 unlink (gconfig
.controlfile
);
311 void sigterm_handler(int sig
)
313 sigterm_received
= 1;
316 void sigint_handler(int sig
)
321 void sigchld_handler(int sig
)
323 sigchld_received
= 1;
326 void sigusr1_handler(int sig
)
328 sigusr1_received
= 1;
331 void sighup_handler(int sig
)
336 void process_signal(void)
338 if (sigterm_received
) { sigterm_received
= 0; death_handler(SIGTERM
); }
339 if (sigint_received
) { sigint_received
= 0; death_handler(SIGINT
); }
340 if (sigchld_received
) { sigchld_received
= 0; child_handler(SIGCHLD
); }
341 if (sigusr1_received
) { sigusr1_received
= 0; status_handler(SIGUSR1
); }
342 if (sighup_received
) { sighup_received
= 0; null_handler(SIGHUP
); }
345 int start_pppd (struct call
*c
, struct ppp_opts
*opts
)
352 struct sockaddr_pppol2tp sax
;
360 struct termios ptyconf
;
365 stropt
[0] = strdup (PPPD
);
368 stropt
[pos
] = (char *) malloc (strlen (p
->option
) + 1);
369 strncpy (stropt
[pos
], p
->option
, strlen (p
->option
) + 1);
376 l2tp_log(LOG_WARNING
, "%s: PPP already started on call!\n", __FUNCTION__
);
381 l2tp_log (LOG_WARNING
, "%s: file descriptor already assigned!\n",
389 fd2
= socket(AF_PPPOX
, SOCK_DGRAM
, PX_PROTO_OL2TP
);
391 l2tp_log (LOG_WARNING
, "%s: Unable to allocate PPPoL2TP socket.\n",
395 flags
= fcntl(fd2
, F_GETFL
);
396 if (flags
== -1 || fcntl(fd2
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
397 l2tp_log (LOG_WARNING
, "%s: Unable to set PPPoL2TP socket nonblock.\n",
401 sax
.sa_family
= AF_PPPOX
;
402 sax
.sa_protocol
= PX_PROTO_OL2TP
;
403 sax
.pppol2tp
.pid
= 0;
404 sax
.pppol2tp
.fd
= server_socket
;
405 sax
.pppol2tp
.addr
.sin_addr
.s_addr
= c
->container
->peer
.sin_addr
.s_addr
;
406 sax
.pppol2tp
.addr
.sin_port
= c
->container
->peer
.sin_port
;
407 sax
.pppol2tp
.addr
.sin_family
= AF_INET
;
408 sax
.pppol2tp
.s_tunnel
= c
->container
->ourtid
;
409 sax
.pppol2tp
.s_session
= c
->ourcid
;
410 sax
.pppol2tp
.d_tunnel
= c
->container
->tid
;
411 sax
.pppol2tp
.d_session
= c
->cid
;
412 if (connect(fd2
, (struct sockaddr
*)&sax
, sizeof(sax
)) < 0) {
413 l2tp_log (LOG_WARNING
, "%s: Unable to connect PPPoL2TP socket.\n",
417 stropt
[pos
++] = strdup ("plugin");
418 stropt
[pos
++] = strdup ("pppol2tp.so");
419 stropt
[pos
++] = strdup ("pppol2tp");
420 stropt
[pos
] = (char *) malloc (10);
421 snprintf (stropt
[pos
], 10, "%d", fd2
);
428 if ((c
->fd
= getPtyMaster (tty
, sizeof(tty
))) < 0)
430 l2tp_log (LOG_WARNING
, "%s: unable to allocate pty, abandoning!\n",
435 /* set fd opened above to not echo so we don't see read our own packets
436 back of the file descriptor that we just wrote them to */
437 tcgetattr (c
->fd
, &ptyconf
);
438 *(c
->oldptyconf
) = ptyconf
;
439 ptyconf
.c_cflag
&= ~(ICANON
| ECHO
);
440 ptyconf
.c_lflag
&= ~ECHO
;
441 tcsetattr (c
->fd
, TCSANOW
, &ptyconf
);
442 if(fcntl(c
->fd
, F_SETFL
, O_NONBLOCK
)!=0) {
443 l2tp_log(LOG_WARNING
, "failed to set nonblock: %s\n", strerror(errno
));
447 fd2
= open (tty
, O_RDWR
);
449 l2tp_log (LOG_WARNING
, "unable to open tty %s, cannot start pppd", tty
);
452 stropt
[pos
++] = strdup(tty
);
457 l2tp_log (LOG_DEBUG
, "%s: I'm running: \n", __FUNCTION__
);
458 for (x
= 0; stropt
[x
]; x
++)
460 l2tp_log (LOG_DEBUG
, "\"%s\" \n", stropt
[x
]);
472 l2tp_log(LOG_WARNING
,"%s: unable to fork(), abandoning!\n", __FUNCTION__
);
479 close (0); /* redundant; the dup2() below would do that, too */
480 close (1); /* ditto */
481 /* close (2); No, we want to keep the connection to /dev/null. */
487 /* connect the pty to stdin and stdout */
492 /* close all the calls pty fds */
506 /* close the UDP socket fd */
507 close (server_socket
);
509 /* close the control pipe fd */
514 setenv( "CALLER_ID", c
->dialing
, 1 );
516 execv (PPPD
, stropt
);
517 l2tp_log (LOG_WARNING
, "%s: Exec of %s failed!\n", __FUNCTION__
, PPPD
);
530 void destroy_tunnel (struct tunnel
*t
)
533 * Immediately destroy a tunnel (and all its calls)
534 * and free its resources. This may be called
535 * by the tunnel itself,so it needs to be
546 * Save ourselves until the very
547 * end, since we might be calling this ourselves.
548 * We must divorce ourself from the tunnel
549 * structure, however, to avoid recursion
550 * because of the logic of the destroy_call
555 * Destroy all the member calls
564 * Remove ourselves from the list of tunnels
567 if (tunnels
.head
== t
)
569 tunnels
.head
= t
->next
;
577 while (p
->next
&& (p
->next
!= t
))
586 l2tp_log (LOG_WARNING
,
587 "%s: unable to locate tunnel in tunnel list\n",
593 l2tp_log (LOG_WARNING
, "%s: tunnel list is empty!\n", __FUNCTION__
);
599 if (t
->lac
->redial
&& (t
->lac
->rtimeout
> 0) && !t
->lac
->rsched
&&
602 l2tp_log (LOG_INFO
, "Will redial in %d seconds\n",
604 tv
.tv_sec
= t
->lac
->rtimeout
;
606 t
->lac
->rsched
= schedule (tv
, magic_lac_dial
, t
->lac
);
609 /* XXX L2TP/IPSec: remove relevant SAs here? NTB 20011010
610 * XXX But what if another tunnel is using same SA?
614 if (t
->chal_us
.challenge
)
615 free (t
->chal_us
.challenge
);
616 if (t
->chal_them
.challenge
)
617 free (t
->chal_them
.challenge
);
618 /* we need no free(t->chal_us.vector) here because we malloc() and free()
619 the memory pointed to by t->chal_us.vector at some other place */
620 if (t
->chal_them
.vector
)
621 free (t
->chal_them
.vector
);
627 struct tunnel
*l2tp_call (char *host
, int port
, struct lac
*lac
,
631 * Establish a tunnel from us to host
634 struct call
*tmp
= NULL
;
638 #if !defined(__UCLIBC__) \
639 || (__UCLIBC_MAJOR__ == 0 \
640 && (__UCLIBC_MINOR__ < 9 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 31)))
641 /* force ns refresh from resolv.conf with uClibc pre-0.9.31 */
646 hp
= gethostbyname (host
);
649 l2tp_log (LOG_WARNING
, "Host name lookup failed for %s.\n",
653 bcopy (hp
->h_addr
, &addr
.s_addr
, hp
->h_length
);
654 /* Force creation of a new tunnel
655 and set it's tid to 0 to cause
656 negotiation to occur */
658 * to do IPsec properly here, we need to set a socket policy,
659 * and/or communicate with pluto.
661 tmp
= get_call (0, 0, addr
, port
, IPSEC_SAREF_NULL
, IPSEC_SAREF_NULL
);
664 l2tp_log (LOG_WARNING
, "%s: Unable to create tunnel to %s.\n", __FUNCTION__
,
668 tmp
->container
->tid
= 0;
669 tmp
->container
->lac
= lac
;
670 tmp
->container
->lns
= lns
;
674 lac
->t
= tmp
->container
;
676 lns
->t
= tmp
->container
;
678 * Since our state is 0, we will establish a tunnel now
680 l2tp_log (LOG_NOTICE
, "Connecting to host %s, port %d\n", host
,
682 control_finish (tmp
->container
, tmp
);
683 return tmp
->container
;
686 void magic_lac_tunnel (void *data
)
689 lac
= (struct lac
*) data
;
692 l2tp_log (LOG_WARNING
, "%s: magic_lac_tunnel: called on NULL lac!\n",
698 /* FIXME: I should try different LNS's if I get failures */
699 l2tp_call (lac
->lns
->hostname
, lac
->lns
->port
, lac
, NULL
);
702 else if (deflac
&& deflac
->lns
)
704 l2tp_call (deflac
->lns
->hostname
, deflac
->lns
->port
, lac
, NULL
);
709 l2tp_log (LOG_WARNING
, "%s: Unable to find hostname to dial for '%s'\n",
710 __FUNCTION__
, lac
->entname
);
715 struct call
*lac_call (int tid
, struct lac
*lac
, struct lns
*lns
)
717 struct tunnel
*t
= tunnels
.head
;
721 if (t
->ourtid
== tid
)
726 l2tp_log (LOG_WARNING
, "%s: unable to create new call\n",
730 tmp
->next
= t
->call_head
;
738 l2tp_log (LOG_NOTICE
, "Calling on tunnel %d\n", tid
);
739 strcpy (tmp
->dial_no
, dial_no_tmp
); /* jz: copy dialnumber to tmp->dial_no */
740 control_finish (t
, tmp
);
745 l2tp_log (LOG_DEBUG
, "%s: No such tunnel %d to generate call.\n", __FUNCTION__
,
750 void magic_lac_dial (void *data
)
753 lac
= (struct lac
*) data
;
756 l2tp_log (LOG_WARNING
, "%s : called on NULL lac!\n", __FUNCTION__
);
761 l2tp_log (LOG_DEBUG
, "%s: LAC %s not active", __FUNCTION__
, lac
->entname
);
766 if (lac
->rmax
&& (lac
->rtries
> lac
->rmax
))
768 l2tp_log (LOG_INFO
, "%s: maximum retries exceeded.\n", __FUNCTION__
);
774 l2tp_log (LOG_DEBUG
, "%s : tunnel not up! Connecting!\n", __FUNCTION__
);
776 magic_lac_tunnel (lac
);
779 lac_call (lac
->t
->ourtid
, lac
, NULL
);
782 void lac_hangup (int cid
)
784 struct tunnel
*t
= tunnels
.head
;
791 if (tmp
->ourcid
== cid
)
794 "%s :Hanging up call %d, Local: %d, Remote: %d\n",
795 __FUNCTION__
, tmp
->serno
, tmp
->ourcid
, tmp
->cid
);
796 strcpy (tmp
->errormsg
, "Goodbye!");
797 /* tmp->needclose = -1; */
798 kill (tmp
->pppd
, SIGTERM
);
805 l2tp_log (LOG_DEBUG
, "%s : No such call %d to hang up.\n", __FUNCTION__
, cid
);
809 void lac_disconnect (int tid
)
811 struct tunnel
*t
= tunnels
.head
;
814 if (t
->ourtid
== tid
)
817 "Disconnecting from %s, Local: %d, Remote: %d\n",
818 IPADDY (t
->peer
.sin_addr
), t
->ourtid
, t
->tid
);
819 t
->self
->needclose
= -1;
820 strcpy (t
->self
->errormsg
, "Goodbye!");
821 call_close (t
->self
);
826 l2tp_log (LOG_DEBUG
, "No such tunnel %d to hang up.\n", tid
);
830 struct tunnel
*new_tunnel ()
832 struct tunnel
*tmp
= malloc (sizeof (struct tunnel
));
833 unsigned char entropy_buf
[2] = "\0";
836 tmp
->control_seq_num
= 0;
837 tmp
->control_rec_seq_num
= 0;
839 tmp
->call_head
= NULL
;
845 /* while(get_call((tmp->ourtid = rand() & 0xFFFF),0,0,0)); */
846 /* tmp->ourtid = rand () & 0xFFFF; */
847 /* get_entropy((char *)&tmp->ourtid, 2); */
848 get_entropy(entropy_buf
, 2);
850 unsigned short *temp
;
851 temp
= (unsigned short *)entropy_buf
;
852 tmp
->ourtid
= *temp
& 0xFFFF;
854 l2tp_log(LOG_DEBUG
, "ourtid = %u, entropy_buf = %hx\n", tmp
->ourtid
, *temp
);
859 tmp
->ourtid
= 0x6227;
863 tmp
->state
= 0; /* Nothing */
864 tmp
->peer
.sin_family
= AF_INET
;
865 tmp
->peer
.sin_port
= 0;
866 bzero (&(tmp
->peer
.sin_addr
), sizeof (tmp
->peer
.sin_addr
));
871 tmp
->ourfc
= ASYNC_FRAMING
| SYNC_FRAMING
;
873 tmp
->ourtb
= (((_u64
) rand ()) << 32) | ((_u64
) rand ());
874 tmp
->fc
= -1; /* These really need to be specified by the peer */
875 tmp
->bc
= -1; /* And we want to know if they forgot */
876 tmp
->hostname
[0] = 0;
879 if (!(tmp
->self
= new_call (tmp
)))
884 tmp
->ourrws
= DEFAULT_RWS_SIZE
;
885 tmp
->self
->ourfbit
= FBIT
;
886 tmp
->rxspeed
= DEFAULT_RX_BPS
;
887 tmp
->txspeed
= DEFAULT_TX_BPS
;
890 tmp
->chal_us
.state
= 0;
891 tmp
->chal_us
.secret
[0] = 0;
892 memset (tmp
->chal_us
.reply
, 0, MD_SIG_SIZE
);
893 tmp
->chal_us
.challenge
= NULL
;
894 tmp
->chal_us
.chal_len
= 0;
895 tmp
->chal_them
.state
= 0;
896 tmp
->chal_them
.secret
[0] = 0;
897 memset (tmp
->chal_them
.reply
, 0, MD_SIG_SIZE
);
898 tmp
->chal_them
.challenge
= NULL
;
899 tmp
->chal_them
.chal_len
= 0;
900 tmp
->chal_them
.vector
= (unsigned char *) malloc (VECTOR_SIZE
);
901 tmp
->chal_us
.vector
= NULL
;
906 void write_res (FILE* res_file
, const char *fmt
, ...)
908 if (!res_file
|| ferror (res_file
) || feof (res_file
))
911 va_start (args
, fmt
);
912 vfprintf (res_file
, fmt
, args
);
916 int parse_one_line_lac (char* bufp
, struct lac
*tc
)
918 /* FIXME: I should check for incompatible options */
922 s
= strtok (bufp
, ";");
923 // parse options token by token
928 while ((*s
< 33) && *s
)
929 s
++; /* Skip over beginning white space */
931 while ((t
>= s
) && (*t
< 33))
932 *(t
--) = 0; /* Ditch trailing white space */
935 if (!(t
= strchr (s
, '=')))
937 l2tp_log (LOG_WARNING
, "%s: token %d: no '=' in data\n",
938 __FUNCTION__
, linenum
);
944 while ((d
>= s
) && (*d
< 33))
948 while (*t
&& (*t
< 33))
951 l2tp_log (LOG_DEBUG
, "%s: field is %s, value is %s\n",
954 /* Okay, bit twidling is done. Let's handle this */
956 switch (parse_one_option (s
, t
, CONTEXT_LAC
, tc
))
959 l2tp_log (LOG_WARNING
, "%s: error token %d\n",
960 __FUNCTION__
, linenum
);
963 l2tp_log (LOG_CRIT
, "%s: token %d: Unknown field '%s'\n",
964 __FUNCTION__
, linenum
, s
);
968 s
= strtok (NULL
, ";");
975 char buf
[CONTROL_PIPE_MESSAGE_SIZE
];
976 char *bufp
; /* current buffer pointer */
981 char *authname
= NULL
;
982 char *password
= NULL
;
984 char *sub_str
; /* jz: use by the strtok function */
985 char *tmp_ptr
; /* jz: use by the strtok function */
987 struct lac
*prev_lac
; /* for lac removing */
993 bzero(buf
, sizeof(buf
));
996 char* res_filename
; /* name of file to write result of command */
997 FILE* resf
; /* stream for write result of command */
1001 cnt
= read (control_fd
, buf
, sizeof (buf
));
1004 if(cnt
< 0 && errno
!= EINTR
) {
1005 perror("controlfd");
1011 if (buf
[cnt
- 1] == '\n')
1013 #ifdef DEBUG_CONTROL
1014 l2tp_log (LOG_DEBUG
, "%s: Got message %s (%d bytes long)\n",
1015 __FUNCTION__
, buf
, cnt
);
1018 /* check if caller want to get result */
1021 /* parse filename (@/path/to/file *...), where * is command */
1022 res_filename
= &bufp
[1];
1023 int fnlength
= strcspn(res_filename
, " ");
1024 if ((fnlength
== 0) || (res_filename
[fnlength
] == '\0')){
1025 l2tp_log (LOG_DEBUG
,
1026 "%s: Can't parse result filename or command\n",
1031 res_filename
[fnlength
] = '\0';
1032 bufp
= &res_filename
[fnlength
+ 1]; /* skip filename in bufp */
1034 /*FIXME: check quotes to allow filenames with spaces?
1035 (do not forget quotes escaping to allow filenames with quotes)*/
1037 /*FIXME: write to res_filename may cause SIGPIPE, need to catch it*/
1038 resf
= fopen (res_filename
, "w");
1040 l2tp_log (LOG_DEBUG
, "%s: Can't open result file %s\n",
1041 __FUNCTION__
, res_filename
);
1050 host
= strchr (bufp
, ' ') + 1;
1051 #ifdef DEBUG_CONTROL
1052 l2tp_log (LOG_DEBUG
, "%s: Attempting to tunnel to %s\n",
1053 __FUNCTION__
, host
);
1055 if (l2tp_call (host
, UDP_LISTEN_PORT
, NULL
, NULL
))
1056 write_res (resf
, "%02i OK\n", 0);
1058 write_res (resf
, "%02i Error\n", 1);
1061 switch_io
= 1; /* jz: Switch for Incoming - Outgoing Calls */
1063 tunstr
= strtok (&bufp
[1], delims
);
1065 /* Are these passed on the command line? */
1066 authname
= strtok (NULL
, delims
);
1067 password
= strtok (NULL
, delims
);
1070 while (lac
&& strcasecmp (lac
->entname
, tunstr
)!=0)
1078 if (authname
!= NULL
)
1079 strncpy (lac
->authname
, authname
, STRLEN
);
1080 if (password
!= NULL
)
1081 strncpy (lac
->password
, password
, STRLEN
);
1084 magic_lac_dial (lac
);
1085 write_res (resf
, "%02i OK\n", 0);
1087 l2tp_log (LOG_DEBUG
,
1088 "Session '%s' already active!\n", lac
->entname
);
1089 write_res (resf
, "%02i Session '%s' already active!\n", 1,
1095 /* did not find a tunnel by name, look by number */
1096 tunl
= atoi (tunstr
);
1099 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1100 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1103 #ifdef DEBUG_CONTROL
1104 l2tp_log (LOG_DEBUG
, "%s: Attempting to call on tunnel %d\n",
1105 __FUNCTION__
, tunl
);
1107 if (lac_call (tunl
, NULL
, NULL
))
1108 write_res (resf
, "%02i OK\n", 0);
1110 write_res (resf
, "%02i Error\n", 1);
1113 case 'o': /* jz: option 'o' for doing a outgoing call */
1114 switch_io
= 0; /* jz: Switch for incoming - outgoing Calls */
1116 sub_str
= strchr (bufp
, ' ') + 1;
1117 tunstr
= strtok (sub_str
, " "); /* jz: using strtok function to get */
1118 tmp_ptr
= strtok (NULL
, " "); /* params out of the pipe */
1119 strcpy (dial_no_tmp
, tmp_ptr
);
1122 while (lac
&& strcasecmp (lac
->entname
, tunstr
)!=0)
1132 magic_lac_dial (lac
);
1133 write_res (resf
, "%02i OK\n", 0);
1135 l2tp_log (LOG_DEBUG
, "Session '%s' already active!\n",
1137 write_res (resf
, "%02i Session '%s' already active!\n", 1,
1143 /* did not find a tunnel by name, look by number */
1144 tunl
= atoi (tunstr
);
1147 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1148 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1151 #ifdef DEBUG_CONTROL
1152 l2tp_log (LOG_DEBUG
, "%s: Attempting to call on tunnel %d\n",
1153 __FUNCTION__
, tunl
);
1155 if (lac_call (tunl
, NULL
, NULL
))
1156 write_res (resf
, "%02i OK\n", 0);
1158 write_res (resf
, "%02i Error\n", 1);
1162 callstr
= strchr (bufp
, ' ') + 1;
1163 call
= atoi (callstr
);
1164 #ifdef DEBUG_CONTROL
1165 l2tp_log (LOG_DEBUG
, "%s: Attempting to hangup call %d\n", __FUNCTION__
,
1169 write_res (resf
, "%02i OK\n", 0);
1173 tunstr
= strchr (bufp
, ' ') + 1;
1177 if (!strcasecmp (lac
->entname
, tunstr
))
1183 lac_disconnect (lac
->t
->ourtid
);
1184 write_res (resf
, "%02i OK\n", 0);
1186 l2tp_log (LOG_DEBUG
, "Session '%s' not up\n",
1188 write_res (resf
, "%02i Session '%s' not up\n", 1,
1197 tunl
= atoi (tunstr
);
1200 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1201 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1204 #ifdef DEBUG_CONTROL
1205 l2tp_log (LOG_DEBUG
, "%s: Attempting to disconnect tunnel %d\n",
1206 __FUNCTION__
, tunl
);
1208 lac_disconnect (tunl
);
1209 write_res (resf
, "%02i OK\n", 0);
1215 /* add new or modify existing lac configuration */
1217 int create_new_lac
= 0;
1218 tunstr
= strtok (&bufp
[1], delims
);
1219 if ((!tunstr
) || (!strlen (tunstr
)))
1222 "%02i Configuration parse error: lac-name expected\n", 1);
1223 l2tp_log (LOG_CRIT
, "%s: lac-name expected\n", __FUNCTION__
);
1226 /* go to the end of tunnel name*/
1227 bufp
= tunstr
+ strlen (tunstr
) + 1;
1228 /* try to find lac with _tunstr_ name in laclist */
1232 if (!strcasecmp (tunstr
, lac
->entname
))
1238 /* nothing found, create new lac */
1243 "%02i Could't create new lac: no memory\n", 2);
1245 "%s: Couldn't create new lac\n", __FUNCTION__
);
1250 strncpy (lac
->entname
, tunstr
, sizeof (lac
->entname
));
1252 if (parse_one_line_lac (bufp
, lac
))
1254 write_res (resf
, "%02i Configuration parse error\n", 3);
1259 lac
->next
= laclist
;
1265 l2tp_log (LOG_DEBUG
, "%s: Autodialing '%s'\n", __FUNCTION__
,
1266 lac
->entname
[0] ? lac
->entname
: "(unnamed)");
1269 switch_io
= 1; /* If we're a LAC, autodials will be ICRQ's */
1270 magic_lac_dial (lac
);
1271 /* FIXME: Should I check magic_lac_dial result somehow? */
1273 write_res (resf
, "%02i OK\n", 0);
1277 // find lac in laclist
1278 tunstr
= strchr (bufp
, ' ') + 1;
1281 while (lac
&& strcasecmp (lac
->entname
, tunstr
) != 0)
1288 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n",
1290 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1298 lac_disconnect (lac
->t
->ourtid
);
1300 // removes lac from laclist
1301 if (prev_lac
== NULL
)
1302 laclist
= lac
->next
;
1304 prev_lac
->next
= lac
->next
;
1307 write_res (resf
, "%02i OK\n", 0);
1310 l2tp_log (LOG_DEBUG
, "Unknown command %c\n", bufp
[0]);
1311 write_res (resf
, "%02i Unknown command %c\n", 1, bufp
[0]);
1320 /* Otherwise select goes nuts. Yeah, this just seems wrong */
1327 printf("\nxl2tpd version: %s\n", SERVER_VERSION
);
1328 printf("Usage: xl2tpd [-c <config file>] [-s <secret file>] [-p <pid file>]\n"
1329 " [-C <control file>] [-D]\n"
1330 " [-v, --version]\n");
1335 void init_args(int argc
, char *argv
[])
1340 memset(gconfig
.altauthfile
,0,STRLEN
);
1341 memset(gconfig
.altconfigfile
,0,STRLEN
);
1342 memset(gconfig
.authfile
,0,STRLEN
);
1343 memset(gconfig
.configfile
,0,STRLEN
);
1344 memset(gconfig
.pidfile
,0,STRLEN
);
1345 memset(gconfig
.controlfile
,0,STRLEN
);
1346 strncpy(gconfig
.altauthfile
,ALT_DEFAULT_AUTH_FILE
,
1347 sizeof(gconfig
.altauthfile
) - 1);
1348 strncpy(gconfig
.altconfigfile
,ALT_DEFAULT_CONFIG_FILE
,
1349 sizeof(gconfig
.altconfigfile
) - 1);
1350 strncpy(gconfig
.authfile
,DEFAULT_AUTH_FILE
,
1351 sizeof(gconfig
.authfile
) - 1);
1352 strncpy(gconfig
.configfile
,DEFAULT_CONFIG_FILE
,
1353 sizeof(gconfig
.configfile
) - 1);
1354 strncpy(gconfig
.pidfile
,DEFAULT_PID_FILE
,
1355 sizeof(gconfig
.pidfile
) - 1);
1356 strncpy(gconfig
.controlfile
,CONTROL_PIPE
,
1357 sizeof(gconfig
.controlfile
) - 1);
1358 gconfig
.ipsecsaref
= 0;
1360 for (i
= 1; i
< argc
; i
++) {
1361 if ((! strncmp(argv
[i
],"--version",9))
1362 || (! strncmp(argv
[i
],"-v",2))) {
1363 printf("\nxl2tpd version: %s\n",SERVER_VERSION
);
1367 if(! strncmp(argv
[i
],"-c",2)) {
1371 strncpy(gconfig
.configfile
,argv
[i
],
1372 sizeof(gconfig
.configfile
) - 1);
1374 else if (! strncmp(argv
[i
],"-D",2)) {
1377 else if (! strncmp(argv
[i
],"-s",2)) {
1381 strncpy(gconfig
.authfile
,argv
[i
],
1382 sizeof(gconfig
.authfile
) - 1);
1384 else if (! strncmp(argv
[i
],"-p",2)) {
1388 strncpy(gconfig
.pidfile
,argv
[i
],
1389 sizeof(gconfig
.pidfile
) - 1);
1391 else if (! strncmp(argv
[i
],"-C",2)) {
1395 strncpy(gconfig
.controlfile
,argv
[i
],
1396 sizeof(gconfig
.controlfile
) - 1);
1409 #ifndef CONFIG_SNAPGEAR
1410 if((pid
= fork()) < 0) {
1411 l2tp_log(LOG_INFO
, "%s: Unable to fork ()\n",__FUNCTION__
);
1412 close(server_socket
);
1419 i
= open("/dev/null", O_RDWR
);
1421 l2tp_log(LOG_INFO
, "Redirect of stdin to /dev/null failed\n");
1423 if (dup2(0, 1) == -1)
1424 l2tp_log(LOG_INFO
, "Redirect of stdout to /dev/null failed\n");
1425 if (dup2(0, 2) == -1)
1426 l2tp_log(LOG_INFO
, "Redirect of stderr to /dev/null failed\n");
1431 static void consider_pidfile() {
1436 /* Read previous pid file. */
1437 i
= open(gconfig
.pidfile
,O_RDONLY
);
1439 /* l2tp_log(LOG_DEBUG, "%s: Unable to read pid file [%s]\n",
1440 __FUNCTION__, gconfig.pidfile);
1444 l
=read(i
,buf
,sizeof(buf
)-1);
1452 /* If the previous server process is still running,
1453 complain and exit immediately. */
1454 if (pid
&& pid
!= getpid () && kill (pid
, 0) == 0)
1457 "%s: There's already a xl2tpd server running.\n",
1459 close(server_socket
);
1466 unlink(gconfig
.pidfile
);
1467 if ((i
= open (gconfig
.pidfile
, O_WRONLY
| O_CREAT
, 0640)) >= 0) {
1468 snprintf (buf
, sizeof(buf
), "%d\n", (int)getpid());
1469 if (-1 == write (i
, buf
, strlen(buf
)))
1471 l2tp_log (LOG_CRIT
, "%s: Unable to write to %s.\n",
1472 __FUNCTION__
, gconfig
.pidfile
);
1480 static void open_controlfd()
1482 control_fd
= open (gconfig
.controlfile
, O_RDONLY
| O_NONBLOCK
, 0600);
1485 l2tp_log (LOG_CRIT
, "%s: Unable to open %s for reading.\n",
1486 __FUNCTION__
, gconfig
.controlfile
);
1490 /* turn off O_NONBLOCK */
1491 if(fcntl(control_fd
, F_SETFL
, O_RDONLY
)==-1) {
1492 l2tp_log(LOG_CRIT
, "Can not turn off nonblocking mode for controlfd: %s\n",
1498 void init (int argc
,char *argv
[])
1501 struct in_addr listenaddr
;
1504 init_args (argc
,argv
);
1505 srand( time(NULL
) );
1510 l2tp_log (LOG_CRIT
, "%s: Unable to load config file\n", __FUNCTION__
);
1515 l2tp_log (LOG_CRIT
, "%s : Unable to determine host system\n",
1519 init_tunnel_list (&tunnels
);
1520 if (init_network ())
1528 signal (SIGTERM
, &sigterm_handler
);
1529 signal (SIGINT
, &sigint_handler
);
1530 signal (SIGCHLD
, &sigchld_handler
);
1531 signal (SIGUSR1
, &sigusr1_handler
);
1532 signal (SIGHUP
, &sighup_handler
);
1535 unlink(gconfig
.controlfile
);
1536 mkfifo (gconfig
.controlfile
, 0600);
1540 l2tp_log (LOG_INFO
, "xl2tpd version " SERVER_VERSION
" started on %s PID:%d\n",
1541 hostname
, getpid ());
1543 "Written by Mark Spencer, Copyright (C) 1998, Adtran, Inc.\n");
1544 l2tp_log (LOG_INFO
, "Forked by Scott Balmos and David Stipp, (C) 2001\n");
1545 l2tp_log (LOG_INFO
, "Inherited by Jeff McAdams, (C) 2002\n");
1546 l2tp_log (LOG_INFO
, "Forked again by Xelerance (www.xelerance.com) (C) 2006\n");
1547 listenaddr
.s_addr
= gconfig
.listenaddr
;
1548 l2tp_log (LOG_INFO
, "Listening on IP address %s, port %d\n",
1549 inet_ntoa(listenaddr
), gconfig
.port
);
1556 l2tp_log (LOG_DEBUG
, "%s: Autodialing '%s'\n", __FUNCTION__
,
1557 lac
->entname
[0] ? lac
->entname
: "(unnamed)");
1560 switch_io
= 1; /* If we're a LAC, autodials will be ICRQ's */
1561 magic_lac_dial (lac
);
1567 int main (int argc
, char *argv
[])
1570 dial_no_tmp
= calloc (128, sizeof (char));
1575 /* Route manipulation */
1578 route_ctrl(int ctrl
, struct rtentry
*rt
)
1582 /* Open a raw socket to the kernel */
1583 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0 || ioctl(s
, ctrl
, rt
) < 0)
1584 l2tp_log (LOG_ERR
, "route_ctrl: %s", strerror(errno
));
1592 route_del(struct rtentry
*rt
)
1595 route_ctrl(SIOCDELRT
, rt
);
1603 route_add(const struct in_addr inetaddr
, struct rtentry
*rt
)
1605 char buf
[256], dev
[64];
1607 u_int32_t dest
, mask
;
1609 FILE *f
= fopen("/proc/net/route", "r");
1611 l2tp_log (LOG_ERR
, "/proc/net/route: %s", strerror(errno
));
1615 while (fgets(buf
, sizeof(buf
), f
)) {
1616 if (sscanf(buf
, "%63s %x %x %X %*s %*s %d %x", dev
, &dest
,
1617 &sin_addr(&rt
->rt_gateway
).s_addr
, &flags
, &metric
, &mask
) != 6)
1619 if ((flags
& RTF_UP
) == (RTF_UP
) && (inetaddr
.s_addr
& mask
) == dest
&&
1620 (dest
|| strncmp(dev
, "ppp", 3)) /* avoid default via pppX to avoid on-demand loops*/) {
1621 rt
->rt_metric
= metric
+ 1;
1622 rt
->rt_gateway
.sa_family
= AF_INET
;
1629 /* check for no route */
1630 if (rt
->rt_gateway
.sa_family
!= AF_INET
) {
1631 /* l2tp_log (LOG_ERR, "route_add: no route to host"); */
1635 /* check for existing route to this host,
1636 add if missing based on the existing routes */
1637 if (flags
& RTF_HOST
) {
1638 /* l2tp_log (LOG_ERR, "route_add: not adding existing route"); */
1642 sin_addr(&rt
->rt_dst
) = inetaddr
;
1643 rt
->rt_dst
.sa_family
= AF_INET
;
1645 sin_addr(&rt
->rt_genmask
).s_addr
= INADDR_BROADCAST
;
1646 rt
->rt_genmask
.sa_family
= AF_INET
;
1648 rt
->rt_flags
= RTF_UP
| RTF_HOST
;
1649 if (flags
& RTF_GATEWAY
)
1650 rt
->rt_flags
|= RTF_GATEWAY
;
1653 rt
->rt_dev
= strdup(dev
);
1656 l2tp_log (LOG_ERR
, "route_add: no memory");
1660 if (!route_ctrl(SIOCADDRT
, rt
))