usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / xl2tpd / xl2tpd.c
blobdf8457772cc4c55c49b59a0b31ad6980eff9bb03
1 /*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
7 * Mark Spencer
9 * This software is distributed under the terms
10 * of the GPL, which you should have received
11 * along with this source.
13 * Main Daemon source.
17 #define _ISOC99_SOURCE
18 #define _XOPEN_SOURCE
19 #define _BSD_SOURCE
20 #define _XOPEN_SOURCE_EXTENDED
21 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/utsname.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <time.h>
33 #if (__GLIBC__ < 2)
34 # if defined(FREEBSD) || defined(OPENBSD)
35 # include <sys/signal.h>
36 # elif defined(LINUX)
37 # include <bsd/signal.h>
38 # elif defined(SOLARIS)
39 # include <signal.h>
40 # endif
41 #else
42 # include <signal.h>
43 #endif
44 #ifndef LINUX
45 # include <sys/socket.h>
46 #endif
47 #include <netdb.h>
48 #include <string.h>
49 #include <strings.h>
50 #include <fcntl.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <net/route.h>
54 #include <features.h>
55 #include <resolv.h>
56 #include <sys/ioctl.h>
57 #include "l2tp.h"
59 struct tunnel_list tunnels;
60 int rand_source;
61 int ppd = 1; /* Packet processing delay */
62 int control_fd; /* descriptor of control area */
63 char *args;
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)
78 t->head = NULL;
79 t->count = 0;
80 t->calls = 0;
83 /* Now sends to syslog instead - MvO */
84 void show_status (void)
86 struct schedule_entry *se;
87 struct tunnel *t;
88 struct call *c;
89 struct lns *tlns;
90 struct lac *tlac;
91 struct host *h;
92 unsigned long cnt = 0;
94 int s = 0;
95 l2tp_log (LOG_WARNING, "====== xl2tpd statistics ========\n");
96 l2tp_log (LOG_WARNING, " Scheduler entries:\n");
97 se = events;
98 while (se)
100 s++;
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,
120 c->cid);
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);
126 else
127 l2tp_log (LOG_WARNING, "%d: Unknown event\n", s);
128 se = se->next;
130 l2tp_log (LOG_WARNING, "Total Events scheduled: %d\n", s);
131 l2tp_log (LOG_WARNING, "Number of tunnels open: %d\n", tunnels.count);
132 t = tunnels.head;
133 while (t)
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);
143 c = t->call_head;
144 while (c)
146 cnt++;
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)",
151 (c->lac ? c->lac->
152 entname : (c->lns ? c->lns->entname : "")),
153 cnt, c->ourcid,
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);
156 c = c->next;
158 t = t->next;
160 l2tp_log (LOG_WARNING, "==========Config File===========\n");
161 tlns = lnslist;
162 while (tlns)
164 l2tp_log (LOG_WARNING, "LNS entry %s\n",
165 tlns->entname[0] ? tlns->entname : "(unnamed)");
166 tlns = tlns->next;
168 tlac = laclist;
169 while (tlac)
171 l2tp_log (LOG_WARNING, "LAC entry %s, LNS is/are:",
172 tlac->entname[0] ? tlac->entname : "(unnamed)");
173 h = tlac->lns;
174 if (h)
176 while (h)
178 l2tp_log (LOG_WARNING, " %s", h->hostname);
179 h = h->next;
182 else
183 l2tp_log (LOG_WARNING, " [none]");
184 tlac = tlac->next;
186 l2tp_log (LOG_WARNING, "================================\n");
189 void null_handler(int sig)
191 /* FIXME
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)
199 show_status ();
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
210 struct tunnel *t;
211 struct call *c;
212 pid_t pid;
213 int status;
214 /* Keep looping until all are cleared */
215 for(;;)
217 pid = waitpid (-1, &status, WNOHANG);
218 if (pid < 1)
221 * Oh well, nobody there. Maybe we reaped it
222 * somewhere else already
224 return;
226 /* find the call that "owned" the pppd which just died */
227 t = tunnels.head;
228 while (t)
230 c = t->call_head;
231 t = t->next;
232 while (c)
234 if (c->pppd == pid)
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 ) );
246 else
248 l2tp_log (LOG_DEBUG, "%s : pppd exited for call %d for unknown reason\n", __FUNCTION__,
249 c->cid );
251 c->needclose = -1;
253 * OK...pppd died, we can go ahead and close the pty for
254 * it
256 #ifdef USE_KERNEL
257 if (!kernel_support)
258 #endif
259 close (c->fd);
260 c->fd = -1;
262 * terminate tunnel and call loops, returning to the
263 * for(;;) loop (and possibly get the next pid)
265 t = NULL;
266 break;
268 c = c->next;
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;
283 int sec;
284 l2tp_log (LOG_CRIT, "%s: Fatal signal %d received\n", __FUNCTION__, signal);
285 if (signal != SIGTERM) {
286 st = tunnels.head;
287 while (st)
289 st2 = st->next;
290 strcpy (st->self->errormsg, "Server closing");
291 sec = st->self->closing;
292 if (st->lac)
293 st->lac->redial = 0;
294 call_close (st->self);
295 if (!sec)
297 st->self->closing = -1;
298 call_close (st->self);
300 st = st2;
304 /* erase pid and control files */
305 unlink (gconfig.pidfile);
306 unlink (gconfig.controlfile);
308 exit (1);
311 void sigterm_handler(int sig)
313 sigterm_received = 1;
316 void sigint_handler(int sig)
318 sigint_received = 1;
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)
333 sighup_received = 1;
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)
347 /* char a, b; */
348 char tty[512];
349 char *stropt[80];
350 struct ppp_opts *p;
351 #ifdef USE_KERNEL
352 struct sockaddr_pppol2tp sax;
353 int flags;
354 #endif
355 int pos = 1;
356 int fd2;
357 #ifdef DEBUG_PPPD
358 int x;
359 #endif
360 struct termios ptyconf;
361 struct call *sc;
362 struct tunnel *st;
364 p = opts;
365 stropt[0] = strdup (PPPD);
366 while (p)
368 stropt[pos] = (char *) malloc (strlen (p->option) + 1);
369 strncpy (stropt[pos], p->option, strlen (p->option) + 1);
370 pos++;
371 p = p->next;
373 stropt[pos] = NULL;
374 if (c->pppd > 0)
376 l2tp_log(LOG_WARNING, "%s: PPP already started on call!\n", __FUNCTION__);
377 return -EINVAL;
379 if (c->fd > -1)
381 l2tp_log (LOG_WARNING, "%s: file descriptor already assigned!\n",
382 __FUNCTION__);
383 return -EINVAL;
386 #ifdef USE_KERNEL
387 if (kernel_support)
389 fd2 = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
390 if (fd2 < 0) {
391 l2tp_log (LOG_WARNING, "%s: Unable to allocate PPPoL2TP socket.\n",
392 __FUNCTION__);
393 return -EINVAL;
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",
398 __FUNCTION__);
399 return -EINVAL;
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",
414 __FUNCTION__);
415 return -EINVAL;
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);
422 pos++;
423 stropt[pos] = NULL;
425 else
426 #endif
428 if ((c->fd = getPtyMaster (tty, sizeof(tty))) < 0)
430 l2tp_log (LOG_WARNING, "%s: unable to allocate pty, abandoning!\n",
431 __FUNCTION__);
432 return -EINVAL;
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));
444 return -EINVAL;
447 fd2 = open (tty, O_RDWR);
448 if (fd2 < 0) {
449 l2tp_log (LOG_WARNING, "unable to open tty %s, cannot start pppd", tty);
450 return -EINVAL;
452 stropt[pos++] = strdup(tty);
453 stropt[pos] = NULL;
456 #ifdef DEBUG_PPPD
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]);
462 #endif
463 #ifdef __uClinux__
464 c->pppd = vfork ();
465 #else
466 c->pppd = fork ();
467 #endif
469 if (c->pppd < 0)
471 /* parent */
472 l2tp_log(LOG_WARNING,"%s: unable to fork(), abandoning!\n", __FUNCTION__);
473 return -EINVAL;
475 else if (!c->pppd)
477 /* child */
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. */
482 #ifdef USE_KERNEL
483 if (!kernel_support)
484 #endif
487 /* connect the pty to stdin and stdout */
488 dup2 (fd2, 0);
489 dup2 (fd2, 1);
490 close(fd2);
492 /* close all the calls pty fds */
493 st = tunnels.head;
494 while (st)
496 sc = st->call_head;
497 while (sc)
499 close (sc->fd);
500 sc = sc->next;
502 st = st->next;
506 /* close the UDP socket fd */
507 close (server_socket);
509 /* close the control pipe fd */
510 close (control_fd);
512 if( c->dialing[0] )
514 setenv( "CALLER_ID", c->dialing, 1 );
516 execv (PPPD, stropt);
517 l2tp_log (LOG_WARNING, "%s: Exec of %s failed!\n", __FUNCTION__, PPPD);
518 _exit (1);
520 close (fd2);
521 pos = 0;
522 while (stropt[pos])
524 free (stropt[pos]);
525 pos++;
527 return 0;
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
536 * "suicide safe"
539 struct call *c, *me;
540 struct tunnel *p;
541 struct timeval tv;
542 if (!t)
543 return;
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
552 me = t->self;
555 * Destroy all the member calls
557 c = t->call_head;
558 while (c)
560 destroy_call (c);
561 c = c->next;
564 * Remove ourselves from the list of tunnels
567 if (tunnels.head == t)
569 tunnels.head = t->next;
570 tunnels.count--;
572 else
574 p = tunnels.head;
575 if (p)
577 while (p->next && (p->next != t))
578 p = p->next;
579 if (p->next)
581 p->next = t->next;
582 tunnels.count--;
584 else
586 l2tp_log (LOG_WARNING,
587 "%s: unable to locate tunnel in tunnel list\n",
588 __FUNCTION__);
591 else
593 l2tp_log (LOG_WARNING, "%s: tunnel list is empty!\n", __FUNCTION__);
596 if (t->lac)
598 t->lac->t = NULL;
599 if (t->lac->redial && (t->lac->rtimeout > 0) && !t->lac->rsched &&
600 t->lac->active)
602 l2tp_log (LOG_INFO, "Will redial in %d seconds\n",
603 t->lac->rtimeout);
604 tv.tv_sec = t->lac->rtimeout;
605 tv.tv_usec = 0;
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?
612 if (t->lns)
613 t->lns->t = NULL;
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);
622 route_del(&t->rt);
623 free (t);
624 free (me);
627 struct tunnel *l2tp_call (char *host, int port, struct lac *lac,
628 struct lns *lns)
631 * Establish a tunnel from us to host
632 * on port port
634 struct call *tmp = NULL;
635 struct hostent *hp;
636 struct in_addr addr;
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 */
642 res_init();
643 #endif
645 port = htons (port);
646 hp = gethostbyname (host);
647 if (!hp)
649 l2tp_log (LOG_WARNING, "Host name lookup failed for %s.\n",
650 host);
651 return NULL;
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);
662 if (!tmp)
664 l2tp_log (LOG_WARNING, "%s: Unable to create tunnel to %s.\n", __FUNCTION__,
665 host);
666 return NULL;
668 tmp->container->tid = 0;
669 tmp->container->lac = lac;
670 tmp->container->lns = lns;
671 tmp->lac = lac;
672 tmp->lns = lns;
673 if (lac)
674 lac->t = tmp->container;
675 if (lns)
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,
681 ntohs (port));
682 control_finish (tmp->container, tmp);
683 return tmp->container;
686 void magic_lac_tunnel (void *data)
688 struct lac *lac;
689 lac = (struct lac *) data;
690 if (!lac)
692 l2tp_log (LOG_WARNING, "%s: magic_lac_tunnel: called on NULL lac!\n",
693 __FUNCTION__);
694 return;
696 if (lac->lns)
698 /* FIXME: I should try different LNS's if I get failures */
699 l2tp_call (lac->lns->hostname, lac->lns->port, lac, NULL);
700 return;
702 else if (deflac && deflac->lns)
704 l2tp_call (deflac->lns->hostname, deflac->lns->port, lac, NULL);
705 return;
707 else
709 l2tp_log (LOG_WARNING, "%s: Unable to find hostname to dial for '%s'\n",
710 __FUNCTION__, lac->entname);
711 return;
715 struct call *lac_call (int tid, struct lac *lac, struct lns *lns)
717 struct tunnel *t = tunnels.head;
718 struct call *tmp;
719 while (t)
721 if (t->ourtid == tid)
723 tmp = new_call (t);
724 if (!tmp)
726 l2tp_log (LOG_WARNING, "%s: unable to create new call\n",
727 __FUNCTION__);
728 return NULL;
730 tmp->next = t->call_head;
731 t->call_head = tmp;
732 t->count++;
733 tmp->cid = 0;
734 tmp->lac = lac;
735 tmp->lns = lns;
736 if (lac)
737 lac->c = tmp;
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);
741 return tmp;
743 t = t->next;
745 l2tp_log (LOG_DEBUG, "%s: No such tunnel %d to generate call.\n", __FUNCTION__,
746 tid);
747 return NULL;
750 void magic_lac_dial (void *data)
752 struct lac *lac;
753 lac = (struct lac *) data;
754 if (!lac)
756 l2tp_log (LOG_WARNING, "%s : called on NULL lac!\n", __FUNCTION__);
757 return;
759 if (!lac->active)
761 l2tp_log (LOG_DEBUG, "%s: LAC %s not active", __FUNCTION__, lac->entname);
762 return;
764 lac->rsched = NULL;
765 lac->rtries++;
766 if (lac->rmax && (lac->rtries > lac->rmax))
768 l2tp_log (LOG_INFO, "%s: maximum retries exceeded.\n", __FUNCTION__);
769 return;
771 if (!lac->t)
773 #ifdef DEGUG_MAGIC
774 l2tp_log (LOG_DEBUG, "%s : tunnel not up! Connecting!\n", __FUNCTION__);
775 #endif
776 magic_lac_tunnel (lac);
777 return;
779 lac_call (lac->t->ourtid, lac, NULL);
782 void lac_hangup (int cid)
784 struct tunnel *t = tunnels.head;
785 struct call *tmp;
786 while (t)
788 tmp = t->call_head;
789 while (tmp)
791 if (tmp->ourcid == cid)
793 l2tp_log (LOG_INFO,
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);
799 return;
801 tmp = tmp->next;
803 t = t->next;
805 l2tp_log (LOG_DEBUG, "%s : No such call %d to hang up.\n", __FUNCTION__, cid);
806 return;
809 void lac_disconnect (int tid)
811 struct tunnel *t = tunnels.head;
812 while (t)
814 if (t->ourtid == tid)
816 l2tp_log (LOG_INFO,
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);
822 return;
824 t = t->next;
826 l2tp_log (LOG_DEBUG, "No such tunnel %d to hang up.\n", tid);
827 return;
830 struct tunnel *new_tunnel ()
832 struct tunnel *tmp = malloc (sizeof (struct tunnel));
833 unsigned char entropy_buf[2] = "\0";
834 if (!tmp)
835 return NULL;
836 tmp->control_seq_num = 0;
837 tmp->control_rec_seq_num = 0;
838 tmp->cLr = 0;
839 tmp->call_head = NULL;
840 tmp->next = NULL;
841 tmp->debug = -1;
842 tmp->tid = -1;
843 tmp->hello = NULL;
844 #ifndef TESTING
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;
853 #ifdef DEBUG_ENTROPY
854 l2tp_log(LOG_DEBUG, "ourtid = %u, entropy_buf = %hx\n", tmp->ourtid, *temp);
855 #endif
858 #else
859 tmp->ourtid = 0x6227;
860 #endif
861 tmp->nego = 0;
862 tmp->count = 0;
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));
867 #ifdef SANITY
868 tmp->sanity = -1;
869 #endif
870 tmp->qtid = -1;
871 tmp->ourfc = ASYNC_FRAMING | SYNC_FRAMING;
872 tmp->ourbc = 0;
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;
877 tmp->vendor[0] = 0;
878 tmp->secret[0] = 0;
879 if (!(tmp->self = new_call (tmp)))
881 free (tmp);
882 return NULL;
884 tmp->ourrws = DEFAULT_RWS_SIZE;
885 tmp->self->ourfbit = FBIT;
886 tmp->rxspeed = DEFAULT_RX_BPS;
887 tmp->txspeed = DEFAULT_TX_BPS;
888 tmp->lac = NULL;
889 tmp->lns = NULL;
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;
902 tmp->hbit = 0;
903 return tmp;
906 void write_res (FILE* res_file, const char *fmt, ...)
908 if (!res_file || ferror (res_file) || feof (res_file))
909 return;
910 va_list args;
911 va_start (args, fmt);
912 vfprintf (res_file, fmt, args);
913 va_end (args);
916 int parse_one_line_lac (char* bufp, struct lac *tc)
918 /* FIXME: I should check for incompatible options */
919 char *s, *d, *t;
920 int linenum = 0;
922 s = strtok (bufp, ";");
923 // parse options token by token
924 while (s != NULL)
926 linenum++;
928 while ((*s < 33) && *s)
929 s++; /* Skip over beginning white space */
930 t = s + strlen (s);
931 while ((t >= s) && (*t < 33))
932 *(t--) = 0; /* Ditch trailing white space */
933 if (!strlen (s))
934 continue;
935 if (!(t = strchr (s, '=')))
937 l2tp_log (LOG_WARNING, "%s: token %d: no '=' in data\n",
938 __FUNCTION__, linenum);
939 return -1;
941 d = t;
942 d--;
943 t++;
944 while ((d >= s) && (*d < 33))
945 d--;
946 d++;
947 *d = 0;
948 while (*t && (*t < 33))
949 t++;
950 #ifdef DEBUG_CONTROL
951 l2tp_log (LOG_DEBUG, "%s: field is %s, value is %s\n",
952 __FUNCTION__, s, t);
953 #endif
954 /* Okay, bit twidling is done. Let's handle this */
956 switch (parse_one_option (s, t, CONTEXT_LAC, tc))
958 case -1:
959 l2tp_log (LOG_WARNING, "%s: error token %d\n",
960 __FUNCTION__, linenum);
961 return -1;
962 case -2:
963 l2tp_log (LOG_CRIT, "%s: token %d: Unknown field '%s'\n",
964 __FUNCTION__, linenum, s);
965 return -1;
968 s = strtok (NULL, ";");
970 return 0;
973 void do_control ()
975 char buf[CONTROL_PIPE_MESSAGE_SIZE];
976 char *bufp; /* current buffer pointer */
977 char *host;
978 char *tunstr;
979 char *callstr;
981 char *authname = NULL;
982 char *password = NULL;
983 char delims[] = " ";
984 char *sub_str; /* jz: use by the strtok function */
985 char *tmp_ptr; /* jz: use by the strtok function */
986 struct lac *lac;
987 struct lac *prev_lac; /* for lac removing */
988 int call;
989 int tunl;
990 int cnt = -1;
991 int done = 0;
993 bzero(buf, sizeof(buf));
994 buf[0]='\0';
996 char* res_filename; /* name of file to write result of command */
997 FILE* resf; /* stream for write result of command */
999 while (!done)
1001 cnt = read (control_fd, buf, sizeof (buf));
1002 if (cnt <= 0)
1004 if(cnt < 0 && errno != EINTR) {
1005 perror("controlfd");
1007 done = 1;
1008 break;
1011 if (buf[cnt - 1] == '\n')
1012 buf[--cnt] = 0;
1013 #ifdef DEBUG_CONTROL
1014 l2tp_log (LOG_DEBUG, "%s: Got message %s (%d bytes long)\n",
1015 __FUNCTION__, buf, cnt);
1016 #endif
1017 bufp = buf;
1018 /* check if caller want to get result */
1019 if (bufp[0] == '@')
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",
1027 __FUNCTION__
1029 continue;
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");
1039 if (!resf) {
1040 l2tp_log (LOG_DEBUG, "%s: Can't open result file %s\n",
1041 __FUNCTION__, res_filename);
1042 continue;
1044 } else
1045 resf = NULL;
1047 switch (bufp[0])
1049 case 't':
1050 host = strchr (bufp, ' ') + 1;
1051 #ifdef DEBUG_CONTROL
1052 l2tp_log (LOG_DEBUG, "%s: Attempting to tunnel to %s\n",
1053 __FUNCTION__, host);
1054 #endif
1055 if (l2tp_call (host, UDP_LISTEN_PORT, NULL, NULL))
1056 write_res (resf, "%02i OK\n", 0);
1057 else
1058 write_res (resf, "%02i Error\n", 1);
1059 break;
1060 case 'c':
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);
1069 lac = laclist;
1070 while (lac && strcasecmp (lac->entname, tunstr)!=0)
1072 lac = lac->next;
1075 if(lac) {
1076 lac->active = -1;
1077 lac->rtries = 0;
1078 if (authname != NULL)
1079 strncpy (lac->authname, authname, STRLEN);
1080 if (password != NULL)
1081 strncpy (lac->password, password, STRLEN);
1082 if (!lac->c)
1084 magic_lac_dial (lac);
1085 write_res (resf, "%02i OK\n", 0);
1086 } else {
1087 l2tp_log (LOG_DEBUG,
1088 "Session '%s' already active!\n", lac->entname);
1089 write_res (resf, "%02i Session '%s' already active!\n", 1,
1090 lac->entname);
1092 break;
1095 /* did not find a tunnel by name, look by number */
1096 tunl = atoi (tunstr);
1097 if (!tunl)
1099 l2tp_log (LOG_DEBUG, "No such tunnel '%s'\n", tunstr);
1100 write_res (resf, "%02i No such tunnel '%s'\n", 1, tunstr);
1101 break;
1103 #ifdef DEBUG_CONTROL
1104 l2tp_log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n",
1105 __FUNCTION__, tunl);
1106 #endif
1107 if (lac_call (tunl, NULL, NULL))
1108 write_res (resf, "%02i OK\n", 0);
1109 else
1110 write_res (resf, "%02i Error\n", 1);
1111 break;
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);
1121 lac = laclist;
1122 while (lac && strcasecmp (lac->entname, tunstr)!=0)
1124 lac = lac->next;
1127 if(lac) {
1128 lac->active = -1;
1129 lac->rtries = 0;
1130 if (!lac->c)
1132 magic_lac_dial (lac);
1133 write_res (resf, "%02i OK\n", 0);
1134 } else {
1135 l2tp_log (LOG_DEBUG, "Session '%s' already active!\n",
1136 lac->entname);
1137 write_res (resf, "%02i Session '%s' already active!\n", 1,
1138 lac->entname);
1140 break;
1143 /* did not find a tunnel by name, look by number */
1144 tunl = atoi (tunstr);
1145 if (!tunl)
1147 l2tp_log (LOG_DEBUG, "No such tunnel '%s'\n", tunstr);
1148 write_res (resf, "%02i No such tunnel '%s'\n", 1, tunstr);
1149 break;
1151 #ifdef DEBUG_CONTROL
1152 l2tp_log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n",
1153 __FUNCTION__, tunl);
1154 #endif
1155 if (lac_call (tunl, NULL, NULL))
1156 write_res (resf, "%02i OK\n", 0);
1157 else
1158 write_res (resf, "%02i Error\n", 1);
1159 break;
1161 case 'h':
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__,
1166 call);
1167 #endif
1168 lac_hangup (call);
1169 write_res (resf, "%02i OK\n", 0);
1170 break;
1172 case 'd':
1173 tunstr = strchr (bufp, ' ') + 1;
1174 lac = laclist;
1175 while (lac)
1177 if (!strcasecmp (lac->entname, tunstr))
1179 lac->active = 0;
1180 lac->rtries = 0;
1181 if (lac->t)
1183 lac_disconnect (lac->t->ourtid);
1184 write_res (resf, "%02i OK\n", 0);
1185 } else {
1186 l2tp_log (LOG_DEBUG, "Session '%s' not up\n",
1187 lac->entname);
1188 write_res (resf, "%02i Session '%s' not up\n", 1,
1189 lac->entname);
1191 break;
1193 lac = lac->next;
1195 if (lac)
1196 break;
1197 tunl = atoi (tunstr);
1198 if (!tunl)
1200 l2tp_log (LOG_DEBUG, "No such tunnel '%s'\n", tunstr);
1201 write_res (resf, "%02i No such tunnel '%s'\n", 1, tunstr);
1202 break;
1204 #ifdef DEBUG_CONTROL
1205 l2tp_log (LOG_DEBUG, "%s: Attempting to disconnect tunnel %d\n",
1206 __FUNCTION__, tunl);
1207 #endif
1208 lac_disconnect (tunl);
1209 write_res (resf, "%02i OK\n", 0);
1210 break;
1211 case 's':
1212 show_status ();
1213 break;
1214 case 'a':
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)))
1221 write_res (resf,
1222 "%02i Configuration parse error: lac-name expected\n", 1);
1223 l2tp_log (LOG_CRIT, "%s: lac-name expected\n", __FUNCTION__);
1224 break;
1226 /* go to the end of tunnel name*/
1227 bufp = tunstr + strlen (tunstr) + 1;
1228 /* try to find lac with _tunstr_ name in laclist */
1229 lac = laclist;
1230 while (lac)
1232 if (!strcasecmp (tunstr, lac->entname))
1233 break;
1234 lac = lac->next;
1236 if (!lac)
1238 /* nothing found, create new lac */
1239 lac = new_lac ();
1240 if (!lac)
1242 write_res (resf,
1243 "%02i Could't create new lac: no memory\n", 2);
1244 l2tp_log (LOG_CRIT,
1245 "%s: Couldn't create new lac\n", __FUNCTION__);
1246 break;
1248 create_new_lac = 1;
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);
1255 break;
1257 if (create_new_lac)
1259 lac->next = laclist;
1260 laclist = lac;
1262 if (lac->autodial)
1264 #ifdef DEBUG_MAGIC
1265 l2tp_log (LOG_DEBUG, "%s: Autodialing '%s'\n", __FUNCTION__,
1266 lac->entname[0] ? lac->entname : "(unnamed)");
1267 #endif
1268 lac->active = -1;
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);
1275 break;
1276 case 'r':
1277 // find lac in laclist
1278 tunstr = strchr (bufp, ' ') + 1;
1279 lac = laclist;
1280 prev_lac = NULL;
1281 while (lac && strcasecmp (lac->entname, tunstr) != 0)
1283 prev_lac = lac;
1284 lac = lac->next;
1286 if (!lac)
1288 l2tp_log (LOG_DEBUG, "No such tunnel '%s'\n",
1289 tunstr);
1290 write_res (resf, "%02i No such tunnel '%s'\n", 1, tunstr);
1291 break;
1293 // disconnect lac
1294 lac->active = 0;
1295 lac->rtries = 0;
1296 if (lac->t)
1298 lac_disconnect (lac->t->ourtid);
1300 // removes lac from laclist
1301 if (prev_lac == NULL)
1302 laclist = lac->next;
1303 else
1304 prev_lac->next = lac->next;
1305 free(lac);
1306 lac = NULL;
1307 write_res (resf, "%02i OK\n", 0);
1308 break;
1309 default:
1310 l2tp_log (LOG_DEBUG, "Unknown command %c\n", bufp[0]);
1311 write_res (resf, "%02i Unknown command %c\n", 1, bufp[0]);
1314 if (resf)
1316 fclose (resf);
1320 /* Otherwise select goes nuts. Yeah, this just seems wrong */
1321 close (control_fd);
1322 open_controlfd();
1326 void usage(void) {
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");
1331 printf("\n");
1332 exit(1);
1335 void init_args(int argc, char *argv[])
1337 int i=0;
1339 gconfig.daemon=1;
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);
1364 exit(1);
1367 if(! strncmp(argv[i],"-c",2)) {
1368 if(++i == argc)
1369 usage();
1370 else
1371 strncpy(gconfig.configfile,argv[i],
1372 sizeof(gconfig.configfile) - 1);
1374 else if (! strncmp(argv[i],"-D",2)) {
1375 gconfig.daemon=0;
1377 else if (! strncmp(argv[i],"-s",2)) {
1378 if(++i == argc)
1379 usage();
1380 else
1381 strncpy(gconfig.authfile,argv[i],
1382 sizeof(gconfig.authfile) - 1);
1384 else if (! strncmp(argv[i],"-p",2)) {
1385 if(++i == argc)
1386 usage();
1387 else
1388 strncpy(gconfig.pidfile,argv[i],
1389 sizeof(gconfig.pidfile) - 1);
1391 else if (! strncmp(argv[i],"-C",2)) {
1392 if(++i == argc)
1393 usage();
1394 else
1395 strncpy(gconfig.controlfile,argv[i],
1396 sizeof(gconfig.controlfile) - 1);
1398 else {
1399 usage();
1405 void daemonize() {
1406 int pid=0;
1407 int i;
1409 #ifndef CONFIG_SNAPGEAR
1410 if((pid = fork()) < 0) {
1411 l2tp_log(LOG_INFO, "%s: Unable to fork ()\n",__FUNCTION__);
1412 close(server_socket);
1413 exit(1);
1415 else if (pid)
1416 exit(0);
1418 close(0);
1419 i = open("/dev/null", O_RDWR);
1420 if (i != 0) {
1421 l2tp_log(LOG_INFO, "Redirect of stdin to /dev/null failed\n");
1422 } else {
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");
1428 #endif
1431 static void consider_pidfile() {
1432 int pid=0;
1433 int i,l;
1434 char buf[STRLEN];
1436 /* Read previous pid file. */
1437 i = open(gconfig.pidfile,O_RDONLY);
1438 if (i < 0) {
1439 /* l2tp_log(LOG_DEBUG, "%s: Unable to read pid file [%s]\n",
1440 __FUNCTION__, gconfig.pidfile);
1442 } else
1444 l=read(i,buf,sizeof(buf)-1);
1445 close (i);
1446 if (l >= 0)
1448 buf[l] = '\0';
1449 pid = atoi(buf);
1452 /* If the previous server process is still running,
1453 complain and exit immediately. */
1454 if (pid && pid != getpid () && kill (pid, 0) == 0)
1456 l2tp_log(LOG_INFO,
1457 "%s: There's already a xl2tpd server running.\n",
1458 __FUNCTION__);
1459 close(server_socket);
1460 exit(1);
1464 pid = setsid();
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);
1473 close (i);
1474 exit(1);
1476 close (i);
1480 static void open_controlfd()
1482 control_fd = open (gconfig.controlfile, O_RDONLY | O_NONBLOCK, 0600);
1483 if (control_fd < 0)
1485 l2tp_log (LOG_CRIT, "%s: Unable to open %s for reading.\n",
1486 __FUNCTION__, gconfig.controlfile);
1487 exit (1);
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",
1493 strerror(errno));
1494 exit(1);
1498 void init (int argc,char *argv[])
1500 struct lac *lac;
1501 struct in_addr listenaddr;
1502 struct utsname uts;
1504 init_args (argc,argv);
1505 srand( time(NULL) );
1506 rand_source = 0;
1507 init_addr ();
1508 if (init_config ())
1510 l2tp_log (LOG_CRIT, "%s: Unable to load config file\n", __FUNCTION__);
1511 exit (1);
1513 if (uname (&uts)<0)
1515 l2tp_log (LOG_CRIT, "%s : Unable to determine host system\n",
1516 __FUNCTION__);
1517 exit (1);
1519 init_tunnel_list (&tunnels);
1520 if (init_network ())
1521 exit (1);
1523 if (gconfig.daemon)
1524 daemonize ();
1526 consider_pidfile();
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);
1533 init_scheduler ();
1535 unlink(gconfig.controlfile);
1536 mkfifo (gconfig.controlfile, 0600);
1538 open_controlfd();
1540 l2tp_log (LOG_INFO, "xl2tpd version " SERVER_VERSION " started on %s PID:%d\n",
1541 hostname, getpid ());
1542 l2tp_log (LOG_INFO,
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);
1550 lac = laclist;
1551 while (lac)
1553 if (lac->autodial)
1555 #ifdef DEBUG_MAGIC
1556 l2tp_log (LOG_DEBUG, "%s: Autodialing '%s'\n", __FUNCTION__,
1557 lac->entname[0] ? lac->entname : "(unnamed)");
1558 #endif
1559 lac->active = -1;
1560 switch_io = 1; /* If we're a LAC, autodials will be ICRQ's */
1561 magic_lac_dial (lac);
1563 lac = lac->next;
1567 int main (int argc, char *argv[])
1569 init(argc,argv);
1570 dial_no_tmp = calloc (128, sizeof (char));
1571 network_thread ();
1572 return 0;
1575 /* Route manipulation */
1577 static int
1578 route_ctrl(int ctrl, struct rtentry *rt)
1580 int s;
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));
1585 else errno = 0;
1587 close(s);
1588 return errno;
1592 route_del(struct rtentry *rt)
1594 if (rt->rt_dev) {
1595 route_ctrl(SIOCDELRT, rt);
1596 free(rt->rt_dev);
1597 rt->rt_dev = NULL;
1599 return 0;
1603 route_add(const struct in_addr inetaddr, struct rtentry *rt)
1605 char buf[256], dev[64];
1606 int metric, flags;
1607 u_int32_t dest, mask;
1609 FILE *f = fopen("/proc/net/route", "r");
1610 if (f == NULL) {
1611 l2tp_log (LOG_ERR, "/proc/net/route: %s", strerror(errno));
1612 return -1;
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)
1618 continue;
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;
1623 break;
1627 fclose(f);
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"); */
1632 return -1;
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"); */
1639 return -1;
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;
1652 rt->rt_metric++;
1653 rt->rt_dev = strdup(dev);
1655 if (!rt->rt_dev) {
1656 l2tp_log (LOG_ERR, "route_add: no memory");
1657 return -1;
1660 if (!route_ctrl(SIOCADDRT, rt))
1661 return 0;
1663 free(rt->rt_dev);
1664 rt->rt_dev = NULL;
1666 return -1;