1 | /*
2 | * Layer Two Tunnelling Protocol Daemon
3 | * Copyright (C) 1998 Adtran, Inc.
4 | *
5 | * Mark Spencer
6 | *
7 | * This software is distributed under the terms
8 | * of the GPL, which you should have received
9 | * along with this source.
10 | *
11 | * Main Daemon source.
12 | *
13 | */
14 |
15 | #include <stdlib.h>
16 | #include <sys/utsname.h>
17 | #include <sys/stat.h>
18 | #include <sys/wait.h>
19 | #include <stdio.h>
20 | #include <errno.h>
21 | #include <unistd.h>
22 | #if (__GLIBC__ < 2)
23 | # if defined(FREEBSD)
24 | # include <sys/signal.h>
25 | # elif defined(LINUX)
26 | # include <bsd/signal.h>
27 | # elif defined(SOLARIS)
28 | # include <signal.h>
29 | # endif
30 | #else
31 | # include <signal.h>
32 | #endif
33 | #include <netdb.h>
34 | #include <string.h>
35 | #include <fcntl.h>
36 | #include <netinet/in.h>
37 | #include <arpa/inet.h>
38 | #ifdef USE_KERNEL
39 | #include <sys/ioctl.h>
40 | #endif
41 | #include "l2tp.h"
42 |
43 | struct tunnel_list tunnels;
44 | int max_tunnels = DEF_MAX_TUNNELS;
45 | struct utsname uts;
46 | int ppd = 1; /* Packet processing delay */
47 | int control_fd; /* descriptor of control area */
48 | char *args;
49 |
50 | char *dial_no_tmp; /* jz: Dialnumber for Outgoing Call */
51 | int switch_io = 0; /* jz: Switch for Incoming or Outgoing Call */
52 |
53 | void init_tunnel_list (struct tunnel_list *t)
54 | {
55 | t->head = NULL;
56 | t->count = 0;
57 | t->calls = 0;
58 | }
59 |
60 | void show_status (int fd)
61 | {
62 | struct schedule_entry *se;
63 | struct tunnel *t;
64 | struct call *c;
65 | struct lns *tlns;
66 | struct lac *tlac;
67 | struct host *h;
68 | int s = 0;
69 | int fd2 = dup (fd);
70 | FILE *f = fdopen (fd2, "a");
71 | if (!f)
72 | {
73 | log (LOG_WARN, "show_status: fdopen() failed on fd %d\n", fd);
74 | return;
75 | }
76 | fprintf (f, "====== l2tpd statistics ========\n");
77 | fprintf (f, " Scheduler entries:\n");
78 | se = events;
79 | while (se)
80 | {
81 | s++;
82 | t = (struct tunnel *) se->data;
83 | tlac = (struct lac *) se->data;
84 | c = (struct call *) se->data;
85 | if (se->func == &hello)
86 | {
87 | fprintf (f, "%d: HELLO to %d\n", s, t->tid);
88 | }
89 | else if (se->func == &magic_lac_dial)
90 | {
91 | fprintf (f, "%d: Magic dial on %s\n", s, tlac->entname);
92 | }
93 | else if (se->func == &send_zlb)
94 | {
95 | fprintf (f, "%d: Send payload ZLB on call %d:%d\n", s,
96 | c->container->tid, c->cid);
97 | }
98 | else if (se->func == &dethrottle)
99 | {
100 | fprintf (f, "%d: Dethrottle call %d:%d\n", s, c->container->tid,
101 | c->cid);
102 | }
103 | else
104 | fprintf (f, "%d: Unknown event\n", s);
105 | se = se->next;
106 | };
107 | fprintf (f, "Total Events scheduled: %d\n", s);
108 | fprintf (f, "Number of tunnels open: %d\n", tunnels.count);
109 | fprintf (f, "Highest file descriptor: %d\n", fd2);
110 | t = tunnels.head;
111 | while (t)
112 | {
113 | fprintf (f, "Tunnel %s, ID = %d (local), %d (remote) to %s:%d\n"
114 | " cSs = %d, cSr = %d, cLr = %d\n",
115 | (t->lac ? t->lac->entname : (t->lns ? t->lns->entname : "")),
116 | t->ourtid, t->tid, IPADDY (t->peer.sin_addr),
117 | ntohs (t->peer.sin_port), t->cSs, t->cSr, t->cLr);
118 | c = t->call_head;
119 | while (c)
120 | {
121 | fprintf (f,
122 | " Call %s, ID = %d (local), %d (remote), serno = %d\n"
123 | " pSs = %d, pSr = %d, pLr = %d, tx = %d bytes (%d), rx= %d bytes (%d)\n",
124 | (c->lac ? c->lac->
125 | entname : (c->lns ? c->lns->entname : "")), c->ourcid,
126 | c->cid, c->serno, c->pSs, c->pSr, c->pLr, c->tx_bytes,
127 | c->tx_pkts, c->rx_bytes, c->rx_pkts);
128 | c = c->next;
129 | }
130 | t = t->next;
131 | }
132 | fprintf (f, "==========Config File===========\n");
133 | tlns = lnslist;
134 | while (tlns)
135 | {
136 | fprintf (f, "LNS entry %s\n",
137 | tlns->entname[0] ? tlns->entname : "(unnamed)");
138 | tlns = tlns->next;
139 | };
140 | tlac = laclist;
141 | while (tlac)
142 | {
143 | fprintf (f, "LAC entry %s, LNS is/are:",
144 | tlac->entname[0] ? tlac->entname : "(unnamed)");
145 | h = tlac->lns;
146 | if (h)
147 | {
148 | while (h)
149 | {
150 | fprintf (f, " %s", h->hostname);
151 | h = h->next;
152 | }
153 | }
154 | else
155 | fprintf (f, " [none]");
156 | fprintf (f, "\n");
157 | tlac = tlac->next;
158 | };
159 | fprintf (f, "================================\n");
160 | fclose (f);
161 | close (fd2);
162 | }
163 |
164 | void status_handler (int sig)
165 | {
166 | show_status (1);
167 | }
168 |
169 | void child_handler (int signal)
170 | {
171 | /*
172 | * Oops, somebody we launched was killed.
173 | * It's time to reap them and close that call.
174 | * But first, we have to find out what PID died.
175 | * unfortunately, pppd will
176 | */
177 | struct tunnel *t;
178 | struct call *c;
179 | pid_t pid;
180 | int status;
181 | t = tunnels.head;
182 | pid = waitpid (-1, &status, WNOHANG);
183 | if (pid < 1)
184 | {
185 | /*
186 | * Oh well, nobody there. Maybe we reaped it
187 | * somewhere else already
188 | */
189 | return;
190 | }
191 | while (t)
192 | {
193 | c = t->call_head;
194 | while (c)
195 | {
196 | if (c->pppd == pid)
197 | {
198 | log (LOG_DEBUG, "%s : pppd died for call %d\n", __FUNCTION__,
199 | c->cid);
200 |
201 | c->needclose = -1;
202 | return;
203 | }
204 | c = c->next;
205 | }
206 | t = t->next;
207 | }
208 | }
209 |
210 | void death_handler (int signal)
211 | {
212 | /*
213 | * If we get here, somebody terminated us with a kill or a control-c.
214 | * we call call_close on each tunnel twice to get a StopCCN out
215 | * for each one (we can't pause to make sure it's received.
216 | * Then we close the connections
217 | */
218 | struct tunnel *st, *st2;
219 | int sec;
220 | log (LOG_CRIT, "%s: Fatal signal %d received\n", __FUNCTION__, signal);
221 | st = tunnels.head;
222 | while (st)
223 | {
224 | st2 = st->next;
225 | strcpy (st->self->errormsg, "Server closing");
226 | sec = st->self->closing;
227 | if (st->lac)
228 | st->lac->redial = 0;
229 | call_close (st->self);
230 | if (!sec)
231 | {
232 | st->self->closing = -1;
233 | call_close (st->self);
234 | }
235 | st = st2;
236 | }
237 | exit (1);
238 | }
239 |
240 | int start_pppd (struct call *c, struct ppp_opts *opts)
241 | {
242 | char a, b;
243 | char tty[80];
244 | char *stropt[80];
245 | struct ppp_opts *p;
246 | #ifdef USE_KERNEL
247 | struct l2tp_call_opts co;
248 | #endif
249 | int pos = 1;
250 | int fd2;
251 | #ifdef DEBUG_PPPD
252 | int x;
253 | #endif
254 | struct termios ptyconf;
255 | char *str;
256 | p = opts;
257 | stropt[0] = strdup (PPPD);
258 | while (p)
259 | {
260 | stropt[pos] = (char *) malloc (strlen (p->option) + 1);
261 | strncpy (stropt[pos], p->option, strlen (p->option) + 1);
262 | pos++;
263 | p = p->next;
264 | }
265 | stropt[pos] = NULL;
266 | if (c->pppd > 0)
267 | {
268 | log (LOG_WARN, "%s: PPP already started on call!\n", __FUNCTION__);
269 | return -EINVAL;
270 | }
271 | if (c->fd > -1)
272 | {
273 | log (LOG_WARN, "%s: file descriptor already assigned!\n",
274 | __FUNCTION__);
275 | return -EINVAL;
276 | }
277 | #ifdef USE_KERNEL
278 | if (kernel_support)
279 | {
280 | co.ourtid = c->container->ourtid;
281 | co.ourcid = c->ourcid;
282 | ioctl (server_socket, L2TPIOCGETCALLOPTS, &co);
283 | stropt[pos++] = strdup ("channel");
284 | stropt[pos] = (char *) malloc (10);
285 | snprintf (stropt[pos], 10, "%d", co.id);
286 | pos++;
287 | stropt[pos] = NULL;
288 | }
289 | else
290 | {
291 | #endif
292 | if ((c->fd = getPtyMaster (&a, &b)) < 0)
293 | {
294 | log (LOG_WARN, "%s: unable to allocate pty, abandoning!\n",
295 | __FUNCTION__);
296 | return -EINVAL;
297 | }
298 |
299 | /* set fd opened above to not echo so we don't see read our own packets
300 | back of the file descriptor that we just wrote them to */
301 | tcgetattr (c->fd, &ptyconf);
302 | *(c->oldptyconf) = ptyconf;
303 | ptyconf.c_cflag &= ~(ICANON | ECHO);
304 | tcsetattr (c->fd, TCSANOW, &ptyconf);
305 |
306 | snprintf (tty, sizeof (tty), "/dev/tty%c%c", a, b);
307 | fd2 = open (tty, O_RDWR);
308 |
309 | #ifdef USE_KERNEL
310 | }
311 | #endif
312 | str = stropt[0];
313 | #ifdef DEBUG_PPPD
314 | log (LOG_DEBUG, "%s: I'm running: ", __FUNCTION__);
315 | for (x = 0; stropt[x]; x++)
316 | {
317 | log (LOG_DEBUG, "\"%s\" ", stropt[x]);
318 | };
319 | log (LOG_DEBUG, "\n");
320 | #endif
321 | c->pppd = fork ();
322 | if (c->pppd < 0)
323 | {
324 | log (LOG_WARN, "%s: unable to fork(), abandoning!\n", __FUNCTION__);
325 | return -EINVAL;
326 | }
327 | else if (!c->pppd)
328 | {
329 | struct call *sc;
330 | struct tunnel *st;
331 |
332 | close (0);
333 | close (1);
334 | close (2);
335 | #ifdef USE_KERNEL
336 | if (!kernel_support && (fd2 < 0))
337 | #else
338 | if (fd2 < 0)
339 | #endif
340 | {
341 | log (LOG_WARN, "%s: Unable to open %s to launch pppd!\n",
342 | __FUNCTION__, tty);
343 | exit (1);
344 | }
345 | dup2 (fd2, 0);
346 | dup2 (fd2, 1);
347 |
348 |
349 | /* close all the calls pty fds */
350 | st = tunnels.head;
351 | while (st)
352 | {
353 | sc = st->call_head;
354 | while (sc)
355 | {
356 | close (sc->fd);
357 | sc = sc->next;
358 | }
359 | st = st->next;
360 | }
361 |
362 | /* close the UDP socket fd */
363 | close (server_socket);
364 |
365 | /* close the control pipe fd */
366 | close (control_fd);
367 |
368 |
369 | execv (PPPD, stropt);
370 | log (LOG_WARN, "%s: Exec of %s failed!\n", PPPD);
371 | exit (1);
372 | };
373 | close (fd2);
374 | pos = 0;
375 | while (stropt[pos])
376 | {
377 | free (stropt[pos]);
378 | pos++;
379 | };
380 | return 0;
381 | }
382 |
383 | void destroy_tunnel (struct tunnel *t)
384 | {
385 | /*
386 | * Immediately destroy a tunnel (and all its calls)
387 | * and free its resources. This may be called
388 | * by the tunnel itself,so it needs to be
389 | * "suicide safe"
390 | */
391 |
392 | struct call *c, *me;
393 | struct tunnel *p;
394 | struct timeval tv;
395 | if (!t)
396 | return;
397 |
398 | /*
399 | * Save ourselves until the very
400 | * end, since we might be calling this ourselves.
401 | * We must divorce ourself from the tunnel
402 | * structure, however, to avoid recursion
403 | * because of the logic of the destroy_call
404 | */
405 | me = t->self;
406 |
407 | /*
408 | * Destroy all the member calls
409 | */
410 | c = t->call_head;
411 | while (c)
412 | {
413 | destroy_call (c);
414 | c = c->next;
415 | };
416 | /*
417 | * Remove ourselves from the list of tunnels
418 | */
419 |
420 | if (tunnels.head == t)
421 | {
422 | tunnels.head = t->next;
423 | tunnels.count--;
424 | }
425 | else
426 | {
427 | p = tunnels.head;
428 | if (p)
429 | {
430 | while (p->next && (p->next != t))
431 | p = p->next;
432 | if (p->next)
433 | {
434 | p->next = t->next;
435 | tunnels.count--;
436 | }
437 | else
438 | {
439 | log (LOG_WARN,
440 | "%s: unable to locate tunnel in tunnel list\n",
441 | __FUNCTION__);
442 | }
443 | }
444 | else
445 | {
446 | log (LOG_WARN, "%s: tunnel list is empty!\n", __FUNCTION__);
447 | }
448 | }
449 | if (t->lac)
450 | {
451 | t->lac->t = NULL;
452 | if (t->lac->redial && (t->lac->rtimeout > 0) && !t->lac->rsched &&
453 | t->lac->active)
454 | {
455 | log (LOG_LOG, "%s: Will redial in %d seconds\n", __FUNCTION__,
456 | t->lac->rtimeout);
457 | tv.tv_sec = t->lac->rtimeout;
458 | tv.tv_usec = 0;
459 | t->lac->rsched = schedule (tv, magic_lac_dial, t->lac);
460 | }
461 | }
462 | /* XXX L2TP/IPSec: remove relevant SAs here? NTB 20011010
463 | * XXX But what if another tunnel is using same SA?
464 | */
465 | if (t->lns)
466 | t->lns->t = NULL;
467 | free (t);
468 | free (me);
469 | }
470 |
471 | struct tunnel *l2tp_call (char *host, int port, struct lac *lac,
472 | struct lns *lns)
473 | {
474 | /*
475 | * Establish a tunnel from us to host
476 | * on port port
477 | */
478 | struct call *tmp = NULL;
479 | struct hostent *hp;
480 | unsigned int addr;
481 | port = htons (port);
482 | hp = gethostbyname (host);
483 | if (!hp)
484 | {
485 | log (LOG_WARN, "%s: gethostbyname() failed for %s.\n", __FUNCTION__,
486 | host);
487 | return NULL;
488 | }
489 | bcopy (hp->h_addr, &addr, hp->h_length);
490 | /* Force creation of a new tunnel
491 | and set it's tid to 0 to cause
492 | negotiation to occur */
493 | /* XXX L2TP/IPSec: Set up SA to addr:port here? NTB 20011010
494 | */
495 | tmp = get_call (0, 0, addr, port);
496 | if (!tmp)
497 | {
498 | log (LOG_WARN, "%s: Unable to create tunnel to %s.\n", __FUNCTION__,
499 | host);
500 | return NULL;
501 | }
502 | tmp->container->tid = 0;
503 | tmp->container->lac = lac;
504 | tmp->container->lns = lns;
505 | tmp->lac = lac;
506 | tmp->lns = lns;
507 | if (lac)
508 | lac->t = tmp->container;
509 | if (lns)
510 | lns->t = tmp->container;
511 | /*
512 | * Since our state is 0, we will establish a tunnel now
513 | */
514 | log (LOG_LOG, "%s:Connecting to host %s, port %d\n", __FUNCTION__, host,
515 | ntohs (port));
516 | control_finish (tmp->container, tmp);
517 | return tmp->container;
518 | }
519 |
520 | void magic_lac_tunnel (void *data)
521 | {
522 | struct lac *lac;
523 | lac = (struct lac *) data;
524 | if (!lac)
525 | {
526 | log (LOG_WARN, "%s: magic_lac_tunnel: called on NULL lac!\n",
527 | __FUNCTION__);
528 | return;
529 | }
530 | if (lac->lns)
531 | {
532 | /* FIXME: I should try different LNS's if I get failures */
533 | l2tp_call (lac->lns->hostname, lac->lns->port, lac, NULL);
534 | return;
535 | }
536 | else if (deflac && deflac->lns)
537 | {
538 | l2tp_call (deflac->lns->hostname, deflac->lns->port, lac, NULL);
539 | return;
540 | }
541 | else
542 | {
543 | log (LOG_WARN, "%s: Unable to find hostname to dial for '%s'\n",
544 | __FUNCTION__, lac->entname);
545 | return;
546 | }
547 | }
548 |
549 | struct call *lac_call (int tid, struct lac *lac, struct lns *lns)
550 | {
551 | struct tunnel *t = tunnels.head;
552 | struct call *tmp;
553 | while (t)
554 | {
555 | if (t->ourtid == tid)
556 | {
557 | tmp = new_call (t);
558 | if (!tmp)
559 | {
560 | log (LOG_WARN, "%s: unable to create new call\n",
561 | __FUNCTION__);
562 | return NULL;
563 | }
564 | tmp->next = t->call_head;
565 | t->call_head = tmp;
566 | t->count++;
567 | tmp->cid = 0;
568 | tmp->lac = lac;
569 | tmp->lns = lns;
570 | if (lac)
571 | lac->c = tmp;
572 | log (LOG_LOG, "%s: Calling on tunnel %d\n", __FUNCTION__, tid);
573 | strcpy (tmp->dial_no, dial_no_tmp); /* jz: copy dialnumber to tmp->dial_no */
574 | control_finish (t, tmp);
575 | return tmp;
576 | }
577 | t = t->next;
578 | };
579 | log (LOG_DEBUG, "%s: No such tunnel %d to generate call.\n", __FUNCTION__,
580 | tid);
581 | return NULL;
582 | }
583 |
584 | void magic_lac_dial (void *data)
585 | {
586 | struct lac *lac;
587 | lac = (struct lac *) data;
588 | if (!lac->active)
589 | {
590 | log (LOG_DEBUG, "%s: LAC %s not active", __FUNCTION__, lac->entname);
591 | return;
592 | }
593 | lac->rsched = NULL;
594 | lac->rtries++;
595 | if (lac->rmax && (lac->rtries > lac->rmax))
596 | {
597 | log (LOG_LOG, "%s: maximum retries exceeded.\n", __FUNCTION__);
598 | return;
599 | }
600 | if (!lac)
601 | {
602 | log (LOG_WARN, "%s : called on NULL lac!\n", __FUNCTION__);
603 | return;
604 | }
605 | if (!lac->t)
606 | {
607 | #ifdef DEGUG_MAGIC
608 | log (LOG_DEBUG, "%s : tunnel not up! Connecting!\n", __FUNCTION__);
609 | #endif
610 | magic_lac_tunnel (lac);
611 | return;
612 | }
613 | lac_call (lac->t->ourtid, lac, NULL);
614 | }
615 |
616 | void lac_hangup (int cid)
617 | {
618 | struct tunnel *t = tunnels.head;
619 | struct call *tmp;
620 | while (t)
621 | {
622 | tmp = t->call_head;
623 | while (tmp)
624 | {
625 | if (tmp->ourcid == cid)
626 | {
627 | log (LOG_LOG,
628 | "%s :Hanging up call %d, Local: %d, Remote: %d\n",
629 | __FUNCTION__, tmp->serno, tmp->ourcid, tmp->cid);
630 | strcpy (tmp->errormsg, "Goodbye!");
631 | /* tmp->needclose = -1; */
632 | kill (tmp->pppd, SIGTERM);
633 | return;
634 | }
635 | tmp = tmp->next;
636 | }
637 | t = t->next;
638 | };
639 | log (LOG_DEBUG, "%s : No such call %d to hang up.\n", __FUNCTION__, cid);
640 | return;
641 | }
642 |
643 | void lac_disconnect (int tid)
644 | {
645 | struct tunnel *t = tunnels.head;
646 | while (t)
647 | {
648 | if (t->ourtid == tid)
649 | {
650 | log (LOG_LOG,
651 | "%s: Disconnecting from %s, Local: %d, Remote: %d\n",
652 | __FUNCTION__, IPADDY (t->peer.sin_addr), t->ourtid, t->tid);
653 | t->self->needclose = -1;
654 | strcpy (t->self->errormsg, "Goodbye!");
655 | call_close (t->self);
656 | return;
657 | }
658 | t = t->next;
659 | };
660 | log (LOG_DEBUG, "%s: No such tunnel %d to hang up.\n", __FUNCTION__, tid);
661 | return;
662 | }
663 |
664 | struct tunnel *new_tunnel ()
665 | {
666 | struct tunnel *tmp = malloc (sizeof (struct tunnel));
667 | if (!tmp)
668 | return NULL;
669 | tmp->cSs = 0;
670 | tmp->cSr = 0;
671 | tmp->cLr = 0;
672 | tmp->call_head = NULL;
673 | tmp->next = NULL;
674 | tmp->debug = -1;
675 | tmp->tid = -1;
676 | tmp->hello = NULL;
677 | #ifndef TESTING
678 | /* while(get_call((tmp->ourtid = rand() & 0xFFFF),0,0,0)); */
679 | #ifdef USE_KERNEL
680 | if (kernel_support)
681 | tmp->ourtid = ioctl (server_socket, L2TPIOCADDTUNNEL, 0);
682 | else
683 | #endif
684 | tmp->ourtid = rand () & 0xFFFF;
685 | #else
686 | tmp->ourtid = 0x6227;
687 | #endif
688 | tmp->nego = 0;
689 | tmp->count = 0;
690 | tmp->state = 0; /* Nothing */
691 | tmp->peer.sin_family = AF_INET;
692 | tmp->peer.sin_port = 0;
693 | bzero (&(tmp->peer.sin_addr), sizeof (tmp->peer.sin_addr));
694 | tmp->sanity = -1;
695 | tmp->qtid = -1;
696 | tmp->ourfc = ASYNC_FRAMING | SYNC_FRAMING;
697 | tmp->ourbc = 0;
698 | tmp->ourtb = (((_u64) rand ()) << 32) | ((_u64) rand ());
699 | tmp->fc = -1; /* These really need to be specified by the peer */
700 | tmp->bc = -1; /* And we want to know if they forgot */
701 | tmp->hostname[0] = 0;
702 | tmp->vendor[0] = 0;
703 | tmp->secret[0] = 0;
704 | if (!(tmp->self = new_call (tmp)))
705 | {
706 | free (tmp);
707 | return NULL;
708 | };
709 | tmp->ourrws = DEFAULT_RWS_SIZE;
710 | tmp->self->ourfbit = FBIT;
711 | tmp->lac = NULL;
712 | tmp->lns = NULL;
713 | tmp->chal_us.state = 0;
714 | tmp->chal_us.secret[0] = 0;
715 | memset (tmp->chal_us.reply, 0, MD_SIG_SIZE);
716 | tmp->chal_them.state = 0;
717 | tmp->chal_them.secret[0] = 0;
718 | memset (tmp->chal_them.reply, 0, MD_SIG_SIZE);
719 | tmp->chal_them.vector = (unsigned char *) malloc (VECTOR_SIZE);
720 | tmp->chal_us.vector = NULL;
721 | tmp->hbit = 0;
722 | return tmp;
723 | }
724 |
725 | void do_control ()
726 | {
727 | char buf[1024];
728 | char *host;
729 | char *tunstr;
730 | char *callstr;
731 |
732 | char *sub_str; /* jz: use by the strtok function */
733 | char *tmp_ptr; /* jz: use by the strtok function */
734 | struct lac *lac;
735 | int call;
736 | int tunl;
737 | int cnt = -1;
738 | while (cnt)
739 | {
740 | cnt = read (control_fd, buf, sizeof (buf));
741 | if (cnt > 0)
742 | {
743 | if (buf[cnt - 1] == '\n')
744 | buf[--cnt] = 0;
745 | #ifdef DEBUG_CONTROL
746 | log (LOG_DEBUG, "%s: Got message %s (%d bytes long)\n",
747 | __FUNCTION__, buf, cnt);
748 | #endif
749 | switch (buf[0])
750 | {
751 | case 't':
752 | host = strchr (buf, ' ') + 1;
753 | #ifdef DEBUG_CONTROL
754 | log (LOG_DEBUG, "%s: Attempting to tunnel to %s\n",
755 | __FUNCTION__, host);
756 | #endif
757 | l2tp_call (host, UDP_LISTEN_PORT, NULL, NULL);
758 | break;
759 | case 'c':
760 |
761 | switch_io = 1; /* jz: Switch for Incoming - Outgoing Calls */
762 |
763 | tunstr = strchr (buf, ' ') + 1;
764 | lac = laclist;
765 | while (lac)
766 | {
767 | if (!strcasecmp (lac->entname, tunstr))
768 | {
769 | lac->active = -1;
770 | lac->rtries = 0;
771 | if (!lac->c)
772 | magic_lac_dial (lac);
773 | else
774 | log (LOG_DEBUG,
775 | "%s: Session '%s' already active!\n",
776 | __FUNCTION__, lac->entname);
777 | break;
778 | }
779 | lac = lac->next;
780 | }
781 | if (lac)
782 | break;
783 | tunl = atoi (tunstr);
784 | if (!tunl)
785 | {
786 | log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__,
787 | tunstr);
788 | break;
789 | }
790 | #ifdef DEBUG_CONTROL
791 | log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n",
792 | __FUNCTION__, tunl);
793 | #endif
794 | lac_call (tunl, NULL, NULL);
795 | break;
796 |
797 | case 'o': /* jz: option 'o' for doing a outgoing call */
798 |
799 | switch_io = 0; /* jz: Switch for incoming - outgoing Calls */
800 |
801 | sub_str = strchr (buf, ' ') + 1;
802 |
803 | tunstr = strtok (sub_str, " "); /* jz: using strtok function to get */
804 | tmp_ptr = strtok (NULL, " "); /* params out of the pipe */
805 | strcpy (dial_no_tmp, tmp_ptr);
806 |
807 | lac = laclist;
808 | while (lac)
809 | {
810 | if (!strcasecmp (lac->entname, tunstr))
811 | {
812 | lac->active = -1;
813 | lac->rtries = 0;
814 | if (!lac->c)
815 | magic_lac_dial (lac);
816 | else
817 | log (LOG_DEBUG,
818 | "%s: Session '%s' already active!\n",
819 | __FUNCTION__, lac->entname);
820 | break;
821 | }
822 | lac = lac->next;
823 | }
824 | if (lac)
825 | break;
826 | tunl = atoi (tunstr);
827 | if (!tunl)
828 | {
829 | log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__,
830 | tunstr);
831 | break;
832 | }
833 | #ifdef DEBUG_CONTROL
834 | log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n",
835 | __FUNCTION__, tunl);
836 | #endif
837 | lac_call (tunl, NULL, NULL);
838 | break;
839 |
840 | case 'h':
841 | callstr = strchr (buf, ' ') + 1;
842 | call = atoi (callstr);
843 | #ifdef DEBUG_CONTROL
844 | log (LOG_DEBUG, "%s: Attempting to call %d\n", __FUNCTION__,
845 | call);
846 | #endif
847 | lac_hangup (call);
848 | break;
849 | case 'd':
850 | tunstr = strchr (buf, ' ') + 1;
851 | lac = laclist;
852 | while (lac)
853 | {
854 | if (!strcasecmp (lac->entname, tunstr))
855 | {
856 | lac->active = 0;
857 | lac->rtries = 0;
858 | if (lac->t)
859 | lac_disconnect (lac->t->ourtid);
860 | else
861 | log (LOG_DEBUG, "%s: Session '%s' not up\n",
862 | __FUNCTION__, lac->entname);
863 | break;
864 | }
865 | lac = lac->next;
866 | }
867 | if (lac)
868 | break;
869 | tunl = atoi (tunstr);
870 | if (!tunl)
871 | {
872 | log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__,
873 | tunstr);
874 | break;
875 | }
876 | #ifdef DEBUG_CONTROL
877 | log (LOG_DEBUG, "%s: Attempting to disconnect tunnel %d\n",
878 | __FUNCTION__, tunl);
879 | #endif
880 | lac_disconnect (tunl);
881 | break;
882 | case 's':
883 | show_status (1);
884 | break;
885 | default:
886 | log (LOG_DEBUG, "%s: Unknown command %c\n", __FUNCTION__,
887 | buf[0]);
888 | }
889 | }
890 | }
891 | /* Otherwise select goes nuts */
892 | close (control_fd);
893 | control_fd = open (CONTROL_PIPE, O_RDONLY | O_NONBLOCK, 0600);
894 | }
895 |
896 | void init ()
897 | {
898 | struct lac *lac;
899 | init_addr ();
900 | if (init_config ())
901 | {
902 | log (LOG_CRIT, "%s: Unable to load config file\n", __FUNCTION__);
903 | exit (1);
904 | }
905 | if (uname (&uts))
906 | {
907 | log (LOG_CRIT, "%s : Unable to determine host system\n",
908 | __FUNCTION__);
909 | exit (1);
910 | }
911 | init_tunnel_list (&tunnels);
912 | if (init_network ())
913 | exit (1);
914 | signal (SIGTERM, &death_handler);
915 | signal (SIGINT, &death_handler);
916 | signal (SIGCHLD, &child_handler);
917 | signal (SIGUSR1, &status_handler);
918 | init_scheduler ();
919 | mkfifo (CONTROL_PIPE, 0600);
920 | control_fd = open (CONTROL_PIPE, O_RDONLY | O_NONBLOCK, 0600);
921 | if (control_fd < 0)
922 | {
923 | log (LOG_CRIT, "%s: Unable to open " CONTROL_PIPE " for reading.",
924 | __FUNCTION__);
925 | exit (1);
926 | }
927 | log (LOG_LOG, "l2tpd version " SERVER_VERSION " started on %s PID:%d\n",
928 | hostname, getpid ());
929 | log (LOG_LOG,
930 | "Written by Mark Spencer, Copyright (C) 1998, Adtran, Inc.\n");
931 | log (LOG_LOG, "Forked by Scott Balmos and David Stipp, (C) 2001\n");
932 | log (LOG_LOG, "%s version %s on a %s, port %d\n", uts.sysname,
933 | uts.release, uts.machine, gconfig.port);
934 | lac = laclist;
935 | while (lac)
936 | {
937 | if (lac->autodial)
938 | {
939 | #ifdef DEBUG_MAGIC
940 | log (LOG_DEBUG, "%s: Autodialing '%s'\n", __FUNCTION__,
941 | lac->entname[0] ? lac->entname : "(unnamed)");
942 | #endif
943 | lac->active = -1;
944 | switch_io = 1; /* If we're a LAC, autodials will be ICRQ's */
945 | magic_lac_dial (lac);
946 | }
947 | lac = lac->next;
948 | }
949 | }
950 |
951 | int main (int argc, char *argv[])
952 | {
953 | args = argv[0];
954 | init ();
955 | dial_no_tmp = calloc (128, sizeof (char));
956 | network_thread ();
957 | return 0;
958 | }