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
);
286 if (kernel_support
|| signal
!= SIGTERM
) {
288 if (signal
!= SIGTERM
) {
294 strcpy (st
->self
->errormsg
, "Server closing");
295 sec
= st
->self
->closing
;
298 call_close (st
->self
);
301 st
->self
->closing
= -1;
302 call_close (st
->self
);
308 /* erase pid and control files */
309 unlink (gconfig
.pidfile
);
310 unlink (gconfig
.controlfile
);
315 void sigterm_handler(int sig
)
317 sigterm_received
= 1;
320 void sigint_handler(int sig
)
325 void sigchld_handler(int sig
)
327 sigchld_received
= 1;
330 void sigusr1_handler(int sig
)
332 sigusr1_received
= 1;
335 void sighup_handler(int sig
)
340 void process_signal(void)
342 if (sigterm_received
) { sigterm_received
= 0; death_handler(SIGTERM
); }
343 if (sigint_received
) { sigint_received
= 0; death_handler(SIGINT
); }
344 if (sigchld_received
) { sigchld_received
= 0; child_handler(SIGCHLD
); }
345 if (sigusr1_received
) { sigusr1_received
= 0; status_handler(SIGUSR1
); }
346 if (sighup_received
) { sighup_received
= 0; null_handler(SIGHUP
); }
349 int start_pppd (struct call
*c
, struct ppp_opts
*opts
)
356 struct sockaddr_pppol2tp sax
;
364 struct termios ptyconf
;
369 stropt
[0] = strdup (PPPD
);
372 stropt
[pos
] = (char *) malloc (strlen (p
->option
) + 1);
373 strncpy (stropt
[pos
], p
->option
, strlen (p
->option
) + 1);
380 l2tp_log(LOG_WARNING
, "%s: PPP already started on call!\n", __FUNCTION__
);
385 l2tp_log (LOG_WARNING
, "%s: file descriptor already assigned!\n",
393 fd2
= socket(AF_PPPOX
, SOCK_DGRAM
, PX_PROTO_OL2TP
);
395 l2tp_log (LOG_WARNING
, "%s: Unable to allocate PPPoL2TP socket.\n",
399 flags
= fcntl(fd2
, F_GETFL
);
400 if (flags
== -1 || fcntl(fd2
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
401 l2tp_log (LOG_WARNING
, "%s: Unable to set PPPoL2TP socket nonblock.\n",
405 sax
.sa_family
= AF_PPPOX
;
406 sax
.sa_protocol
= PX_PROTO_OL2TP
;
407 sax
.pppol2tp
.pid
= 0;
408 sax
.pppol2tp
.fd
= c
->container
->udp_fd
;
409 sax
.pppol2tp
.addr
.sin_addr
.s_addr
= c
->container
->peer
.sin_addr
.s_addr
;
410 sax
.pppol2tp
.addr
.sin_port
= c
->container
->peer
.sin_port
;
411 sax
.pppol2tp
.addr
.sin_family
= AF_INET
;
412 sax
.pppol2tp
.s_tunnel
= c
->container
->ourtid
;
413 sax
.pppol2tp
.s_session
= c
->ourcid
;
414 sax
.pppol2tp
.d_tunnel
= c
->container
->tid
;
415 sax
.pppol2tp
.d_session
= c
->cid
;
416 if (connect(fd2
, (struct sockaddr
*)&sax
, sizeof(sax
)) < 0) {
417 l2tp_log (LOG_WARNING
, "%s: Unable to connect PPPoL2TP socket.\n",
422 stropt
[pos
++] = strdup ("plugin");
423 stropt
[pos
++] = strdup ("pppol2tp.so");
424 stropt
[pos
++] = strdup ("pppol2tp");
425 stropt
[pos
] = (char *) malloc (10);
426 snprintf (stropt
[pos
], 10, "%d", fd2
);
433 if ((c
->fd
= getPtyMaster (tty
, sizeof(tty
))) < 0)
435 l2tp_log (LOG_WARNING
, "%s: unable to allocate pty, abandoning!\n",
440 /* set fd opened above to not echo so we don't see read our own packets
441 back of the file descriptor that we just wrote them to */
442 tcgetattr (c
->fd
, &ptyconf
);
443 *(c
->oldptyconf
) = ptyconf
;
444 ptyconf
.c_cflag
&= ~(ICANON
| ECHO
);
445 ptyconf
.c_lflag
&= ~ECHO
;
446 tcsetattr (c
->fd
, TCSANOW
, &ptyconf
);
447 if(fcntl(c
->fd
, F_SETFL
, O_NONBLOCK
)!=0) {
448 l2tp_log(LOG_WARNING
, "failed to set nonblock: %s\n", strerror(errno
));
452 fd2
= open (tty
, O_RDWR
);
454 l2tp_log (LOG_WARNING
, "unable to open tty %s, cannot start pppd", tty
);
457 stropt
[pos
++] = strdup(tty
);
462 l2tp_log (LOG_DEBUG
, "%s: I'm running: \n", __FUNCTION__
);
463 for (x
= 0; stropt
[x
]; x
++)
465 l2tp_log (LOG_DEBUG
, "\"%s\" \n", stropt
[x
]);
477 l2tp_log(LOG_WARNING
,"%s: unable to fork(), abandoning!\n", __FUNCTION__
);
484 close (0); /* redundant; the dup2() below would do that, too */
485 close (1); /* ditto */
486 /* close (2); No, we want to keep the connection to /dev/null. */
492 /* connect the pty to stdin and stdout */
497 /* close all the calls pty fds */
505 if (kernel_support
) {
506 close(st
->udp_fd
); /* tunnel UDP fd */
507 close(st
->pppox_fd
); /* tunnel PPPoX fd */
510 close (sc
->fd
); /* call pty fd */
516 /* close the UDP socket fd */
517 close (server_socket
);
519 /* close the control pipe fd */
524 setenv( "CALLER_ID", c
->dialing
, 1 );
526 execv (PPPD
, stropt
);
527 l2tp_log (LOG_WARNING
, "%s: Exec of %s failed!\n", __FUNCTION__
, PPPD
);
540 void destroy_tunnel (struct tunnel
*t
)
543 * Immediately destroy a tunnel (and all its calls)
544 * and free its resources. This may be called
545 * by the tunnel itself,so it needs to be
556 * Save ourselves until the very
557 * end, since we might be calling this ourselves.
558 * We must divorce ourself from the tunnel
559 * structure, however, to avoid recursion
560 * because of the logic of the destroy_call
565 * Destroy all the member calls
574 * Remove ourselves from the list of tunnels
577 if (tunnels
.head
== t
)
579 tunnels
.head
= t
->next
;
587 while (p
->next
&& (p
->next
!= t
))
596 l2tp_log (LOG_WARNING
,
597 "%s: unable to locate tunnel in tunnel list\n",
603 l2tp_log (LOG_WARNING
, "%s: tunnel list is empty!\n", __FUNCTION__
);
609 if (t
->lac
->redial
&& (t
->lac
->rtimeout
> 0) && !t
->lac
->rsched
&&
612 l2tp_log (LOG_INFO
, "Will redial in %d seconds\n",
614 tv
.tv_sec
= t
->lac
->rtimeout
;
616 t
->lac
->rsched
= schedule (tv
, magic_lac_dial
, t
->lac
);
619 /* XXX L2TP/IPSec: remove relevant SAs here? NTB 20011010
620 * XXX But what if another tunnel is using same SA?
624 if (t
->chal_us
.challenge
)
625 free (t
->chal_us
.challenge
);
626 if (t
->chal_them
.challenge
)
627 free (t
->chal_them
.challenge
);
628 /* we need no free(t->chal_us.vector) here because we malloc() and free()
629 the memory pointed to by t->chal_us.vector at some other place */
630 if (t
->chal_them
.vector
)
631 free (t
->chal_them
.vector
);
632 if (t
->pppox_fd
> -1 )
641 struct tunnel
*l2tp_call (char *host
, int port
, struct lac
*lac
,
645 * Establish a tunnel from us to host
648 struct call
*tmp
= NULL
;
652 #if !defined(__UCLIBC__) \
653 || (__UCLIBC_MAJOR__ == 0 \
654 && (__UCLIBC_MINOR__ < 9 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 31)))
655 /* force ns refresh from resolv.conf with uClibc pre-0.9.31 */
660 hp
= gethostbyname (host
);
663 l2tp_log (LOG_WARNING
, "Host name lookup failed for %s.\n",
667 bcopy (hp
->h_addr
, &addr
.s_addr
, hp
->h_length
);
668 /* Force creation of a new tunnel
669 and set it's tid to 0 to cause
670 negotiation to occur */
672 * to do IPsec properly here, we need to set a socket policy,
673 * and/or communicate with pluto.
675 tmp
= get_call (0, 0, addr
, port
, IPSEC_SAREF_NULL
, IPSEC_SAREF_NULL
);
678 l2tp_log (LOG_WARNING
, "%s: Unable to create tunnel to %s.\n", __FUNCTION__
,
682 tmp
->container
->tid
= 0;
683 tmp
->container
->lac
= lac
;
684 tmp
->container
->lns
= lns
;
688 lac
->t
= tmp
->container
;
690 lns
->t
= tmp
->container
;
692 * Since our state is 0, we will establish a tunnel now
694 l2tp_log (LOG_NOTICE
, "Connecting to host %s, port %d\n", host
,
696 control_finish (tmp
->container
, tmp
);
697 return tmp
->container
;
700 void magic_lac_tunnel (void *data
)
703 lac
= (struct lac
*) data
;
706 l2tp_log (LOG_WARNING
, "%s: magic_lac_tunnel: called on NULL lac!\n",
712 /* FIXME: I should try different LNS's if I get failures */
713 l2tp_call (lac
->lns
->hostname
, lac
->lns
->port
, lac
, NULL
);
716 else if (deflac
&& deflac
->lns
)
718 l2tp_call (deflac
->lns
->hostname
, deflac
->lns
->port
, lac
, NULL
);
723 l2tp_log (LOG_WARNING
, "%s: Unable to find hostname to dial for '%s'\n",
724 __FUNCTION__
, lac
->entname
);
729 struct call
*lac_call (int tid
, struct lac
*lac
, struct lns
*lns
)
731 struct tunnel
*t
= tunnels
.head
;
735 if (t
->ourtid
== tid
)
740 l2tp_log (LOG_WARNING
, "%s: unable to create new call\n",
744 tmp
->next
= t
->call_head
;
752 l2tp_log (LOG_NOTICE
, "Calling on tunnel %d\n", tid
);
753 strcpy (tmp
->dial_no
, dial_no_tmp
); /* jz: copy dialnumber to tmp->dial_no */
754 control_finish (t
, tmp
);
759 l2tp_log (LOG_DEBUG
, "%s: No such tunnel %d to generate call.\n", __FUNCTION__
,
764 void magic_lac_dial (void *data
)
767 lac
= (struct lac
*) data
;
770 l2tp_log (LOG_WARNING
, "%s : called on NULL lac!\n", __FUNCTION__
);
775 l2tp_log (LOG_DEBUG
, "%s: LAC %s not active", __FUNCTION__
, lac
->entname
);
780 if (lac
->rmax
&& (lac
->rtries
> lac
->rmax
))
782 l2tp_log (LOG_INFO
, "%s: maximum retries exceeded.\n", __FUNCTION__
);
788 l2tp_log (LOG_DEBUG
, "%s : tunnel not up! Connecting!\n", __FUNCTION__
);
790 magic_lac_tunnel (lac
);
793 lac_call (lac
->t
->ourtid
, lac
, NULL
);
796 void lac_hangup (int cid
)
798 struct tunnel
*t
= tunnels
.head
;
805 if (tmp
->ourcid
== cid
)
808 "%s :Hanging up call %d, Local: %d, Remote: %d\n",
809 __FUNCTION__
, tmp
->serno
, tmp
->ourcid
, tmp
->cid
);
810 strcpy (tmp
->errormsg
, "Goodbye!");
811 /* tmp->needclose = -1; */
812 kill (tmp
->pppd
, SIGTERM
);
819 l2tp_log (LOG_DEBUG
, "%s : No such call %d to hang up.\n", __FUNCTION__
, cid
);
823 void lac_disconnect (int tid
)
825 struct tunnel
*t
= tunnels
.head
;
828 if (t
->ourtid
== tid
)
831 "Disconnecting from %s, Local: %d, Remote: %d\n",
832 IPADDY (t
->peer
.sin_addr
), t
->ourtid
, t
->tid
);
833 t
->self
->needclose
= -1;
834 strcpy (t
->self
->errormsg
, "Goodbye!");
835 call_close (t
->self
);
840 l2tp_log (LOG_DEBUG
, "No such tunnel %d to hang up.\n", tid
);
844 struct tunnel
*new_tunnel ()
846 struct tunnel
*tmp
= malloc (sizeof (struct tunnel
));
847 unsigned char entropy_buf
[2] = "\0";
850 tmp
->control_seq_num
= 0;
851 tmp
->control_rec_seq_num
= 0;
853 tmp
->call_head
= NULL
;
859 /* while(get_call((tmp->ourtid = rand() & 0xFFFF),0,0,0)); */
860 /* tmp->ourtid = rand () & 0xFFFF; */
861 /* get_entropy((char *)&tmp->ourtid, 2); */
862 get_entropy(entropy_buf
, 2);
864 unsigned short *temp
;
865 temp
= (unsigned short *)entropy_buf
;
866 tmp
->ourtid
= *temp
& 0xFFFF;
868 l2tp_log(LOG_DEBUG
, "ourtid = %u, entropy_buf = %hx\n", tmp
->ourtid
, *temp
);
873 tmp
->ourtid
= 0x6227;
877 tmp
->state
= 0; /* Nothing */
878 tmp
->peer
.sin_family
= AF_INET
;
879 tmp
->peer
.sin_port
= 0;
880 bzero (&(tmp
->peer
.sin_addr
), sizeof (tmp
->peer
.sin_addr
));
885 tmp
->ourfc
= ASYNC_FRAMING
| SYNC_FRAMING
;
887 tmp
->ourtb
= (((_u64
) rand ()) << 32) | ((_u64
) rand ());
888 tmp
->fc
= -1; /* These really need to be specified by the peer */
889 tmp
->bc
= -1; /* And we want to know if they forgot */
890 tmp
->hostname
[0] = 0;
893 if (!(tmp
->self
= new_call (tmp
)))
898 tmp
->ourrws
= DEFAULT_RWS_SIZE
;
899 tmp
->self
->ourfbit
= FBIT
;
900 tmp
->rxspeed
= DEFAULT_RX_BPS
;
901 tmp
->txspeed
= DEFAULT_TX_BPS
;
904 tmp
->chal_us
.state
= 0;
905 tmp
->chal_us
.secret
[0] = 0;
906 memset (tmp
->chal_us
.reply
, 0, MD_SIG_SIZE
);
907 tmp
->chal_us
.challenge
= NULL
;
908 tmp
->chal_us
.chal_len
= 0;
909 tmp
->chal_them
.state
= 0;
910 tmp
->chal_them
.secret
[0] = 0;
911 memset (tmp
->chal_them
.reply
, 0, MD_SIG_SIZE
);
912 tmp
->chal_them
.challenge
= NULL
;
913 tmp
->chal_them
.chal_len
= 0;
914 tmp
->chal_them
.vector
= (unsigned char *) malloc (VECTOR_SIZE
);
915 tmp
->chal_us
.vector
= NULL
;
920 void write_res (FILE* res_file
, const char *fmt
, ...)
922 if (!res_file
|| ferror (res_file
) || feof (res_file
))
925 va_start (args
, fmt
);
926 vfprintf (res_file
, fmt
, args
);
930 int parse_one_line_lac (char* bufp
, struct lac
*tc
)
932 /* FIXME: I should check for incompatible options */
936 s
= strtok (bufp
, ";");
937 // parse options token by token
942 while ((*s
< 33) && *s
)
943 s
++; /* Skip over beginning white space */
945 while ((t
>= s
) && (*t
< 33))
946 *(t
--) = 0; /* Ditch trailing white space */
949 if (!(t
= strchr (s
, '=')))
951 l2tp_log (LOG_WARNING
, "%s: token %d: no '=' in data\n",
952 __FUNCTION__
, linenum
);
958 while ((d
>= s
) && (*d
< 33))
962 while (*t
&& (*t
< 33))
965 l2tp_log (LOG_DEBUG
, "%s: field is %s, value is %s\n",
968 /* Okay, bit twidling is done. Let's handle this */
970 switch (parse_one_option (s
, t
, CONTEXT_LAC
, tc
))
973 l2tp_log (LOG_WARNING
, "%s: error token %d\n",
974 __FUNCTION__
, linenum
);
977 l2tp_log (LOG_CRIT
, "%s: token %d: Unknown field '%s'\n",
978 __FUNCTION__
, linenum
, s
);
982 s
= strtok (NULL
, ";");
989 char buf
[CONTROL_PIPE_MESSAGE_SIZE
];
990 char *bufp
; /* current buffer pointer */
995 char *authname
= NULL
;
996 char *password
= NULL
;
998 char *sub_str
; /* jz: use by the strtok function */
999 char *tmp_ptr
; /* jz: use by the strtok function */
1001 struct lac
*prev_lac
; /* for lac removing */
1007 bzero(buf
, sizeof(buf
));
1010 char* res_filename
; /* name of file to write result of command */
1011 FILE* resf
; /* stream for write result of command */
1015 cnt
= read (control_fd
, buf
, sizeof (buf
));
1018 if(cnt
< 0 && errno
!= EINTR
) {
1019 perror("controlfd");
1025 if (buf
[cnt
- 1] == '\n')
1027 #ifdef DEBUG_CONTROL
1028 l2tp_log (LOG_DEBUG
, "%s: Got message %s (%d bytes long)\n",
1029 __FUNCTION__
, buf
, cnt
);
1032 /* check if caller want to get result */
1035 /* parse filename (@/path/to/file *...), where * is command */
1036 res_filename
= &bufp
[1];
1037 int fnlength
= strcspn(res_filename
, " ");
1038 if ((fnlength
== 0) || (res_filename
[fnlength
] == '\0')){
1039 l2tp_log (LOG_DEBUG
,
1040 "%s: Can't parse result filename or command\n",
1045 res_filename
[fnlength
] = '\0';
1046 bufp
= &res_filename
[fnlength
+ 1]; /* skip filename in bufp */
1048 /*FIXME: check quotes to allow filenames with spaces?
1049 (do not forget quotes escaping to allow filenames with quotes)*/
1051 /*FIXME: write to res_filename may cause SIGPIPE, need to catch it*/
1052 resf
= fopen (res_filename
, "w");
1054 l2tp_log (LOG_DEBUG
, "%s: Can't open result file %s\n",
1055 __FUNCTION__
, res_filename
);
1064 host
= strchr (bufp
, ' ') + 1;
1065 #ifdef DEBUG_CONTROL
1066 l2tp_log (LOG_DEBUG
, "%s: Attempting to tunnel to %s\n",
1067 __FUNCTION__
, host
);
1069 if (l2tp_call (host
, UDP_LISTEN_PORT
, NULL
, NULL
))
1070 write_res (resf
, "%02i OK\n", 0);
1072 write_res (resf
, "%02i Error\n", 1);
1075 switch_io
= 1; /* jz: Switch for Incoming - Outgoing Calls */
1077 tunstr
= strtok (&bufp
[1], delims
);
1079 /* Are these passed on the command line? */
1080 authname
= strtok (NULL
, delims
);
1081 password
= strtok (NULL
, delims
);
1084 while (lac
&& strcasecmp (lac
->entname
, tunstr
)!=0)
1092 if (authname
!= NULL
)
1093 strncpy (lac
->authname
, authname
, STRLEN
);
1094 if (password
!= NULL
)
1095 strncpy (lac
->password
, password
, STRLEN
);
1098 magic_lac_dial (lac
);
1099 write_res (resf
, "%02i OK\n", 0);
1101 l2tp_log (LOG_DEBUG
,
1102 "Session '%s' already active!\n", lac
->entname
);
1103 write_res (resf
, "%02i Session '%s' already active!\n", 1,
1109 /* did not find a tunnel by name, look by number */
1110 tunl
= atoi (tunstr
);
1113 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1114 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1117 #ifdef DEBUG_CONTROL
1118 l2tp_log (LOG_DEBUG
, "%s: Attempting to call on tunnel %d\n",
1119 __FUNCTION__
, tunl
);
1121 if (lac_call (tunl
, NULL
, NULL
))
1122 write_res (resf
, "%02i OK\n", 0);
1124 write_res (resf
, "%02i Error\n", 1);
1127 case 'o': /* jz: option 'o' for doing a outgoing call */
1128 switch_io
= 0; /* jz: Switch for incoming - outgoing Calls */
1130 sub_str
= strchr (bufp
, ' ') + 1;
1131 tunstr
= strtok (sub_str
, " "); /* jz: using strtok function to get */
1132 tmp_ptr
= strtok (NULL
, " "); /* params out of the pipe */
1133 strcpy (dial_no_tmp
, tmp_ptr
);
1136 while (lac
&& strcasecmp (lac
->entname
, tunstr
)!=0)
1146 magic_lac_dial (lac
);
1147 write_res (resf
, "%02i OK\n", 0);
1149 l2tp_log (LOG_DEBUG
, "Session '%s' already active!\n",
1151 write_res (resf
, "%02i Session '%s' already active!\n", 1,
1157 /* did not find a tunnel by name, look by number */
1158 tunl
= atoi (tunstr
);
1161 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1162 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1165 #ifdef DEBUG_CONTROL
1166 l2tp_log (LOG_DEBUG
, "%s: Attempting to call on tunnel %d\n",
1167 __FUNCTION__
, tunl
);
1169 if (lac_call (tunl
, NULL
, NULL
))
1170 write_res (resf
, "%02i OK\n", 0);
1172 write_res (resf
, "%02i Error\n", 1);
1176 callstr
= strchr (bufp
, ' ') + 1;
1177 call
= atoi (callstr
);
1178 #ifdef DEBUG_CONTROL
1179 l2tp_log (LOG_DEBUG
, "%s: Attempting to hangup call %d\n", __FUNCTION__
,
1183 write_res (resf
, "%02i OK\n", 0);
1187 tunstr
= strchr (bufp
, ' ') + 1;
1191 if (!strcasecmp (lac
->entname
, tunstr
))
1197 lac_disconnect (lac
->t
->ourtid
);
1198 write_res (resf
, "%02i OK\n", 0);
1200 l2tp_log (LOG_DEBUG
, "Session '%s' not up\n",
1202 write_res (resf
, "%02i Session '%s' not up\n", 1,
1211 tunl
= atoi (tunstr
);
1214 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n", tunstr
);
1215 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1218 #ifdef DEBUG_CONTROL
1219 l2tp_log (LOG_DEBUG
, "%s: Attempting to disconnect tunnel %d\n",
1220 __FUNCTION__
, tunl
);
1222 lac_disconnect (tunl
);
1223 write_res (resf
, "%02i OK\n", 0);
1229 /* add new or modify existing lac configuration */
1231 int create_new_lac
= 0;
1232 tunstr
= strtok (&bufp
[1], delims
);
1233 if ((!tunstr
) || (!strlen (tunstr
)))
1236 "%02i Configuration parse error: lac-name expected\n", 1);
1237 l2tp_log (LOG_CRIT
, "%s: lac-name expected\n", __FUNCTION__
);
1240 /* go to the end of tunnel name*/
1241 bufp
= tunstr
+ strlen (tunstr
) + 1;
1242 /* try to find lac with _tunstr_ name in laclist */
1246 if (!strcasecmp (tunstr
, lac
->entname
))
1252 /* nothing found, create new lac */
1257 "%02i Could't create new lac: no memory\n", 2);
1259 "%s: Couldn't create new lac\n", __FUNCTION__
);
1264 strncpy (lac
->entname
, tunstr
, sizeof (lac
->entname
));
1266 if (parse_one_line_lac (bufp
, lac
))
1268 write_res (resf
, "%02i Configuration parse error\n", 3);
1273 lac
->next
= laclist
;
1279 l2tp_log (LOG_DEBUG
, "%s: Autodialing '%s'\n", __FUNCTION__
,
1280 lac
->entname
[0] ? lac
->entname
: "(unnamed)");
1283 switch_io
= 1; /* If we're a LAC, autodials will be ICRQ's */
1284 magic_lac_dial (lac
);
1285 /* FIXME: Should I check magic_lac_dial result somehow? */
1287 write_res (resf
, "%02i OK\n", 0);
1291 // find lac in laclist
1292 tunstr
= strchr (bufp
, ' ') + 1;
1295 while (lac
&& strcasecmp (lac
->entname
, tunstr
) != 0)
1302 l2tp_log (LOG_DEBUG
, "No such tunnel '%s'\n",
1304 write_res (resf
, "%02i No such tunnel '%s'\n", 1, tunstr
);
1312 lac_disconnect (lac
->t
->ourtid
);
1314 // removes lac from laclist
1315 if (prev_lac
== NULL
)
1316 laclist
= lac
->next
;
1318 prev_lac
->next
= lac
->next
;
1321 write_res (resf
, "%02i OK\n", 0);
1324 l2tp_log (LOG_DEBUG
, "Unknown command %c\n", bufp
[0]);
1325 write_res (resf
, "%02i Unknown command %c\n", 1, bufp
[0]);
1334 /* Otherwise select goes nuts. Yeah, this just seems wrong */
1341 printf("\nxl2tpd version: %s\n", SERVER_VERSION
);
1342 printf("Usage: xl2tpd [-c <config file>] [-s <secret file>] [-p <pid file>]\n"
1343 " [-C <control file>] [-D]\n"
1344 " [-v, --version]\n");
1349 void init_args(int argc
, char *argv
[])
1354 memset(gconfig
.altauthfile
,0,STRLEN
);
1355 memset(gconfig
.altconfigfile
,0,STRLEN
);
1356 memset(gconfig
.authfile
,0,STRLEN
);
1357 memset(gconfig
.configfile
,0,STRLEN
);
1358 memset(gconfig
.pidfile
,0,STRLEN
);
1359 memset(gconfig
.controlfile
,0,STRLEN
);
1360 strncpy(gconfig
.altauthfile
,ALT_DEFAULT_AUTH_FILE
,
1361 sizeof(gconfig
.altauthfile
) - 1);
1362 strncpy(gconfig
.altconfigfile
,ALT_DEFAULT_CONFIG_FILE
,
1363 sizeof(gconfig
.altconfigfile
) - 1);
1364 strncpy(gconfig
.authfile
,DEFAULT_AUTH_FILE
,
1365 sizeof(gconfig
.authfile
) - 1);
1366 strncpy(gconfig
.configfile
,DEFAULT_CONFIG_FILE
,
1367 sizeof(gconfig
.configfile
) - 1);
1368 strncpy(gconfig
.pidfile
,DEFAULT_PID_FILE
,
1369 sizeof(gconfig
.pidfile
) - 1);
1370 strncpy(gconfig
.controlfile
,CONTROL_PIPE
,
1371 sizeof(gconfig
.controlfile
) - 1);
1372 gconfig
.ipsecsaref
= 0;
1374 for (i
= 1; i
< argc
; i
++) {
1375 if ((! strncmp(argv
[i
],"--version",9))
1376 || (! strncmp(argv
[i
],"-v",2))) {
1377 printf("\nxl2tpd version: %s\n",SERVER_VERSION
);
1381 if(! strncmp(argv
[i
],"-c",2)) {
1385 strncpy(gconfig
.configfile
,argv
[i
],
1386 sizeof(gconfig
.configfile
) - 1);
1388 else if (! strncmp(argv
[i
],"-D",2)) {
1391 else if (! strncmp(argv
[i
],"-s",2)) {
1395 strncpy(gconfig
.authfile
,argv
[i
],
1396 sizeof(gconfig
.authfile
) - 1);
1398 else if (! strncmp(argv
[i
],"-p",2)) {
1402 strncpy(gconfig
.pidfile
,argv
[i
],
1403 sizeof(gconfig
.pidfile
) - 1);
1405 else if (! strncmp(argv
[i
],"-C",2)) {
1409 strncpy(gconfig
.controlfile
,argv
[i
],
1410 sizeof(gconfig
.controlfile
) - 1);
1423 #ifndef CONFIG_SNAPGEAR
1424 if((pid
= fork()) < 0) {
1425 l2tp_log(LOG_INFO
, "%s: Unable to fork ()\n",__FUNCTION__
);
1426 close(server_socket
);
1433 i
= open("/dev/null", O_RDWR
);
1435 l2tp_log(LOG_INFO
, "Redirect of stdin to /dev/null failed\n");
1437 if (dup2(0, 1) == -1)
1438 l2tp_log(LOG_INFO
, "Redirect of stdout to /dev/null failed\n");
1439 if (dup2(0, 2) == -1)
1440 l2tp_log(LOG_INFO
, "Redirect of stderr to /dev/null failed\n");
1445 static void consider_pidfile() {
1450 /* Read previous pid file. */
1451 i
= open(gconfig
.pidfile
,O_RDONLY
);
1453 /* l2tp_log(LOG_DEBUG, "%s: Unable to read pid file [%s]\n",
1454 __FUNCTION__, gconfig.pidfile);
1458 l
=read(i
,buf
,sizeof(buf
)-1);
1466 /* If the previous server process is still running,
1467 complain and exit immediately. */
1468 if (pid
&& pid
!= getpid () && kill (pid
, 0) == 0)
1471 "%s: There's already a xl2tpd server running.\n",
1473 close(server_socket
);
1480 unlink(gconfig
.pidfile
);
1481 if ((i
= open (gconfig
.pidfile
, O_WRONLY
| O_CREAT
, 0640)) >= 0) {
1482 snprintf (buf
, sizeof(buf
), "%d\n", (int)getpid());
1483 if (-1 == write (i
, buf
, strlen(buf
)))
1485 l2tp_log (LOG_CRIT
, "%s: Unable to write to %s.\n",
1486 __FUNCTION__
, gconfig
.pidfile
);
1494 static void open_controlfd()
1496 control_fd
= open (gconfig
.controlfile
, O_RDONLY
| O_NONBLOCK
, 0600);
1499 l2tp_log (LOG_CRIT
, "%s: Unable to open %s for reading.\n",
1500 __FUNCTION__
, gconfig
.controlfile
);
1504 /* turn off O_NONBLOCK */
1505 if(fcntl(control_fd
, F_SETFL
, O_RDONLY
)==-1) {
1506 l2tp_log(LOG_CRIT
, "Can not turn off nonblocking mode for controlfd: %s\n",
1512 void init (int argc
,char *argv
[])
1515 struct in_addr listenaddr
;
1518 init_args (argc
,argv
);
1519 srand( time(NULL
) );
1524 l2tp_log (LOG_CRIT
, "%s: Unable to load config file\n", __FUNCTION__
);
1529 l2tp_log (LOG_CRIT
, "%s : Unable to determine host system\n",
1533 init_tunnel_list (&tunnels
);
1534 if (init_network ())
1542 signal (SIGTERM
, &sigterm_handler
);
1543 signal (SIGINT
, &sigint_handler
);
1544 signal (SIGCHLD
, &sigchld_handler
);
1545 signal (SIGUSR1
, &sigusr1_handler
);
1546 signal (SIGHUP
, &sighup_handler
);
1549 unlink(gconfig
.controlfile
);
1550 mkfifo (gconfig
.controlfile
, 0600);
1554 l2tp_log (LOG_INFO
, "xl2tpd version " SERVER_VERSION
" started on %s PID:%d\n",
1555 hostname
, getpid ());
1557 "Written by Mark Spencer, Copyright (C) 1998, Adtran, Inc.\n");
1558 l2tp_log (LOG_INFO
, "Forked by Scott Balmos and David Stipp, (C) 2001\n");
1559 l2tp_log (LOG_INFO
, "Inherited by Jeff McAdams, (C) 2002\n");
1560 l2tp_log (LOG_INFO
, "Forked again by Xelerance (www.xelerance.com) (C) 2006\n");
1561 listenaddr
.s_addr
= gconfig
.listenaddr
;
1562 l2tp_log (LOG_INFO
, "Listening on IP address %s, port %d\n",
1563 inet_ntoa(listenaddr
), gconfig
.port
);
1570 l2tp_log (LOG_DEBUG
, "%s: Autodialing '%s'\n", __FUNCTION__
,
1571 lac
->entname
[0] ? lac
->entname
: "(unnamed)");
1574 switch_io
= 1; /* If we're a LAC, autodials will be ICRQ's */
1575 magic_lac_dial (lac
);
1581 int main (int argc
, char *argv
[])
1584 dial_no_tmp
= calloc (128, sizeof (char));
1589 /* Route manipulation */
1592 route_ctrl(int ctrl
, struct rtentry
*rt
)
1596 /* Open a raw socket to the kernel */
1597 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0 || ioctl(s
, ctrl
, rt
) < 0)
1598 l2tp_log (LOG_ERR
, "route_ctrl: %s", strerror(errno
));
1606 route_del(struct rtentry
*rt
)
1609 route_ctrl(SIOCDELRT
, rt
);
1617 route_add(const struct in_addr inetaddr
, struct rtentry
*rt
)
1619 char buf
[256], dev
[64];
1621 u_int32_t dest
, mask
;
1623 FILE *f
= fopen("/proc/net/route", "r");
1625 l2tp_log (LOG_ERR
, "/proc/net/route: %s", strerror(errno
));
1629 while (fgets(buf
, sizeof(buf
), f
)) {
1630 if (sscanf(buf
, "%63s %x %x %X %*s %*s %d %x", dev
, &dest
,
1631 &sin_addr(&rt
->rt_gateway
).s_addr
, &flags
, &metric
, &mask
) != 6)
1633 if ((flags
& RTF_UP
) == (RTF_UP
) && (inetaddr
.s_addr
& mask
) == dest
&&
1634 (dest
|| strncmp(dev
, "ppp", 3)) /* avoid default via pppX to avoid on-demand loops*/) {
1635 rt
->rt_metric
= metric
+ 1;
1636 rt
->rt_gateway
.sa_family
= AF_INET
;
1643 /* check for no route */
1644 if (rt
->rt_gateway
.sa_family
!= AF_INET
) {
1645 /* l2tp_log (LOG_ERR, "route_add: no route to host"); */
1649 /* check for existing route to this host,
1650 add if missing based on the existing routes */
1651 if (flags
& RTF_HOST
) {
1652 /* l2tp_log (LOG_ERR, "route_add: not adding existing route"); */
1656 sin_addr(&rt
->rt_dst
) = inetaddr
;
1657 rt
->rt_dst
.sa_family
= AF_INET
;
1659 sin_addr(&rt
->rt_genmask
).s_addr
= INADDR_BROADCAST
;
1660 rt
->rt_genmask
.sa_family
= AF_INET
;
1662 rt
->rt_flags
= RTF_UP
| RTF_HOST
;
1663 if (flags
& RTF_GATEWAY
)
1664 rt
->rt_flags
|= RTF_GATEWAY
;
1667 rt
->rt_dev
= strdup(dev
);
1670 l2tp_log (LOG_ERR
, "route_add: no memory");
1674 if (!route_ctrl(SIOCADDRT
, rt
))