xl2tpd: patches from wl500g.googlecode.com project
[tomato.git] / release / src / router / xl2tpd / call.c
bloba3c12b451bed0e316e97696ed41f4dd83a7766fa
1 /*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
6 * Mark Spencer
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Handle a call as a separate thread
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/wait.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <termios.h>
27 #include "l2tp.h"
29 #include "ipsecmast.h"
31 struct buffer *new_payload (struct sockaddr_in peer)
33 struct buffer *tmp = new_buf (MAX_RECV_SIZE);
34 if (!tmp)
35 return NULL;
36 tmp->peer = peer;
37 tmp->start += sizeof (struct payload_hdr);
38 tmp->len = 0;
39 return tmp;
42 inline void recycle_payload (struct buffer *buf, struct sockaddr_in peer)
44 buf->start = buf->rstart + sizeof (struct payload_hdr);
45 buf->len = 0;
46 buf->peer = peer;
49 void add_payload_hdr (struct tunnel *t, struct call *c, struct buffer *buf)
51 struct payload_hdr *p;
52 buf->start -= sizeof (struct payload_hdr);
53 buf->len += sizeof (struct payload_hdr);
54 /* Account for no offset */
55 buf->start += 4;
56 buf->len -= 4;
57 if (!c->fbit && !c->ourfbit)
59 /* Forget about Ns and Nr fields then */
60 buf->start += 4;
61 buf->len -= 4;
63 if (!c->lbit)
65 /* Forget about specifying the length */
66 buf->start += 2;
67 buf->len -= 2;
69 p = (struct payload_hdr *) buf->start;
70 /* p->ver = htons(c->lbit | c->rbit | c->fbit | c->ourfbit | VER_L2TP); */
71 p->ver = htons (c->lbit | c->fbit | c->ourfbit | VER_L2TP);
72 if (c->lbit)
74 p->length = htons ((_u16) buf->len);
76 else
78 p = (struct payload_hdr *) (((char *) p) - 2);
80 p->tid = htons (t->tid);
81 p->cid = htons (c->cid);
82 if (c->fbit || c->ourfbit)
84 p->Ns = htons (c->data_seq_num);
85 p->Nr = htons (c->data_rec_seq_num);
87 c->data_seq_num++;
88 /* c->rbit=0; */
91 int read_packet (struct buffer *buf, int fd, int convert)
93 unsigned char c;
94 unsigned char escape = 0;
95 unsigned char *p;
96 static unsigned char rbuf[MAX_RECV_SIZE];
97 static int pos = 0;
98 static int max = 0;
99 int res;
100 int errors = 0;
102 /* Read a packet, doing async->sync conversion if necessary */
103 p = buf->start;
104 while (1)
106 if (pos >= max)
108 max = read(fd, rbuf, sizeof (rbuf));
109 res = max;
110 pos = 0;
112 else
114 res = 1;
117 c = rbuf[pos++];
119 /* if there was a short read, then see what is about */
120 if (res < 1)
122 if (res == 0)
125 * Hmm.. Nothing to read. It happens
127 return 0;
129 else if ((errno == EIO) || (errno == EINTR) || (errno == EAGAIN))
133 * Oops, we were interrupted!
134 * Or, we ran out of data too soon
135 * anyway, we discared whatever it is we
136 * have
138 return 0;
140 errors++;
141 l2tp_log (LOG_DEBUG, "%s: Error %d (%s)\n", __FUNCTION__, errno,
142 strerror (errno));
143 if (errors > 10)
145 l2tp_log (LOG_DEBUG,
146 "%s: Too many errors. Declaring call dead.\n",
147 __FUNCTION__);
148 pos=0;
149 max=0;
150 return -errno;
152 continue;
155 switch (c)
157 case PPP_FLAG:
158 if (escape)
160 l2tp_log (LOG_DEBUG, "%s: got an escaped PPP_FLAG\n",
161 __FUNCTION__);
162 pos=0;
163 max=0;
164 return -EINVAL;
167 if (convert)
169 if (buf->len >= 2) {
170 /* must be the end, drop the FCS */
171 buf->len -= 2;
173 else if (buf->len == 1) {
174 /* Do nothing, just return the single character*/
176 else {
177 /* if the buffer is empty, then we have the beginning
178 * of a packet, not the end
180 break;
183 else
185 /* if there is space, then insert the byte */
186 if (buf->len < buf->maxlen)
188 *p = c;
189 p++;
190 buf->len++;
194 /* return what we have now */
195 return buf->len;
197 case PPP_ESCAPE:
198 escape = PPP_TRANS;
199 if (convert)
200 break;
202 /* fall through */
203 default:
204 if (convert)
205 c ^= escape;
206 escape = 0;
207 if (buf->len < buf->maxlen)
209 *p = c;
210 p++;
211 buf->len++;
212 break;
214 l2tp_log (LOG_WARNING, "%s: read overrun\n", __FUNCTION__);
215 pos=0;
216 max=0;
217 return -EINVAL;
221 /* I should never get here */
222 l2tp_log (LOG_WARNING, "%s: You should not see this message. If you do, please enter "
223 "a bug report at http://lists.xelerance.com/mailman/listinfo/xl2tpd", __FUNCTION__);
224 return -EINVAL;
227 void call_close (struct call *c)
229 struct buffer *buf;
230 struct schedule_entry *se, *ose;
231 struct call *tmp, *tmp2;
232 if (!c || !c->container)
234 l2tp_log (LOG_DEBUG, "%s: called on null call or containerless call\n",
235 __FUNCTION__);
236 return;
238 if (c == c->container->self)
241 * We're actually closing the
242 * entire tunnel
245 /* First deschedule any remaining packet transmissions
246 for this tunnel. That means Hello's and any reminaing
247 packets scheduled for transmission. This is a very
248 nasty little piece of code here. */
250 se = events;
251 ose = NULL;
252 while (se)
254 if ((((struct buffer *) se->data)->tunnel == c->container)
255 || ((struct tunnel *) se->data == c->container))
257 #ifdef DEBUG_CLOSE
258 l2tp_log (LOG_DEBUG, "%s: Descheduling event\n", __FUNCTION__);
259 #endif
260 if (ose)
262 ose->next = se->next;
263 if ((struct tunnel *) se->data != c->container)
264 toss ((struct buffer *) (se->data));
265 free (se);
266 se = ose->next;
268 else
270 events = se->next;
271 if ((struct tunnel *) se->data != c->container)
272 toss ((struct buffer *) (se->data));
273 free (se);
274 se = events;
277 else
279 ose = se;
280 se = se->next;
284 if (c->closing)
286 /* Really close this tunnel, as our
287 StopCCN has been ack'd */
288 #ifdef DEBUG_CLOSE
289 l2tp_log (LOG_DEBUG, "%s: Actually closing tunnel %d\n", __FUNCTION__,
290 c->container->ourtid);
291 #endif
292 destroy_tunnel (c->container);
293 return;
297 * We need to close, but need to provide reliable delivery
298 * of the final StopCCN. We record our state to know when
299 * we have actually received an ACK on our StopCCN
301 c->closeSs = c->container->control_seq_num;
302 buf = new_outgoing (c->container);
303 add_message_type_avp (buf, StopCCN);
304 if (c->container->hbit)
306 mk_challenge (c->container->chal_them.vector, VECTOR_SIZE);
307 add_randvect_avp (buf, c->container->chal_them.vector,
308 VECTOR_SIZE);
310 add_tunnelid_avp (buf, c->container->ourtid);
311 if (c->result < 0)
312 c->result = RESULT_CLEAR;
313 if (c->error < 0)
314 c->error = 0;
315 add_result_code_avp (buf, c->result, c->error, c->errormsg,
316 strlen (c->errormsg));
317 add_control_hdr (c->container, c, buf);
318 if (gconfig.packet_dump)
319 do_packet_dump (buf);
320 #ifdef DEBUG_CLOSE
321 l2tp_log (LOG_DEBUG, "%s: enqueing close message for tunnel\n",
322 __FUNCTION__);
323 #endif
324 control_xmit (buf);
326 * We also need to stop all traffic on any calls contained
327 * within us.
329 tmp = c->container->call_head;
330 while (tmp)
332 tmp2 = tmp->next;
333 tmp->needclose = 0;
334 tmp->closing = -1;
335 call_close (tmp);
336 tmp = tmp2;
338 l2tp_log (LOG_INFO,
339 "Connection %d closed to %s, port %d (%s)\n",
340 c->container->tid,
341 IPADDY (c->container->peer.sin_addr),
342 ntohs (c->container->peer.sin_port), c->errormsg);
344 else
347 * Just close a call
349 if (c->zlb_xmit)
350 deschedule (c->zlb_xmit);
351 /* if (c->dethrottle) deschedule(c->dethrottle); */
352 if (c->closing)
354 #ifdef DEBUG_CLOSE
355 l2tp_log (LOG_DEBUG, "%s: Actually closing call %d\n", __FUNCTION__,
356 c->ourcid);
357 #endif
358 destroy_call (c);
359 return;
361 c->closeSs = c->container->control_seq_num;
362 buf = new_outgoing (c->container);
363 add_message_type_avp (buf, CDN);
364 if (c->container->hbit)
366 mk_challenge (c->container->chal_them.vector, VECTOR_SIZE);
367 add_randvect_avp (buf, c->container->chal_them.vector,
368 VECTOR_SIZE);
370 if (c->result < 0)
371 c->result = RESULT_CLEAR;
372 if (c->error < 0)
373 c->error = 0;
374 add_result_code_avp (buf, c->result, c->error, c->errormsg,
375 strlen (c->errormsg));
376 #ifdef TEST_HIDDEN
377 add_callid_avp (buf, c->ourcid, c->container);
378 #else
379 add_callid_avp (buf, c->ourcid);
380 #endif
381 add_control_hdr (c->container, c, buf);
382 if (gconfig.packet_dump)
383 do_packet_dump (buf);
384 #ifdef DEBUG_CLOSE
385 l2tp_log (LOG_DEBUG, "%s: enqueuing close message for call %d\n",
386 __FUNCTION__, c->ourcid);
387 #endif
388 control_xmit (buf);
389 l2tp_log (LOG_INFO, "%s: Call %d to %s disconnected\n", __FUNCTION__,
390 c->ourcid, IPADDY (c->container->peer.sin_addr));
393 * Note that we're in the process of closing now
395 c->closing = -1;
398 void destroy_call (struct call *c)
401 * Here, we unconditionally destroy a call.
404 struct call *p;
405 struct timeval tv;
406 pid_t pid;
408 * Close the tty
410 if (c->fd > 0)
411 close (c->fd);
412 /* if (c->dethrottle) deschedule(c->dethrottle); */
413 if (c->zlb_xmit)
414 deschedule (c->zlb_xmit);
416 #ifdef IP_ALLOCATION
417 if (c->addr)
418 unreserve_addr (c->addr);
419 #endif
422 * Kill off pppd and wait for it to
423 * return to us. This should only be called
424 * in rare cases if pppd hasn't already died
425 * voluntarily
427 pid = c->pppd;
428 if (pid)
430 /* Set c->pppd to zero to prevent recursion with child_handler */
431 c->pppd = 0;
433 * There is a bug in some pppd versions where sending a SIGTERM
434 * does not actually seem to kill pppd, and xl2tpd waits indefinately
435 * using waitpid, not accepting any new connections either. Therefor
436 * we now use some more force and send it a SIGKILL instead of SIGTERM.
437 * One confirmed buggy version of pppd is ppp-2.4.2-6.4.RHEL4
438 * See http://bugs.xelerance.com/view.php?id=739
440 * Sometimes pppd takes 7 sec to go down! We don't have that much time,
441 * since all other calls are suspended while doing this.
444 #ifdef TRUST_PPPD_TO_DIE
445 #ifdef DEBUG_PPPD
446 l2tp_log (LOG_DEBUG, "Terminating pppd: sending TERM signal to pid %d\n", pid);
447 #endif
448 kill (pid, SIGTERM);
449 #else
450 #ifdef DEBUG_PPPD
451 l2tp_log (LOG_DEBUG, "Terminating pppd: sending KILL signal to pid %d\n", pid);
452 #endif
453 kill (pid, SIGKILL);
454 #endif
456 if (c->container)
458 p = c->container->call_head;
460 * Remove us from the call list, although
461 * we might not actually be there
463 if (p)
465 if (p == c)
467 c->container->call_head = c->next;
468 c->container->count--;
470 else
472 while (p->next && (p->next != c))
473 p = p->next;
474 if (p->next)
476 p->next = c->next;
477 c->container->count--;
482 if (c->lac)
484 c->lac->c = NULL;
485 if (c->lac->redial && (c->lac->rtimeout > 0) && !c->lac->rsched &&
486 c->lac->active)
488 #ifdef DEBUG_MAGIC
489 l2tp_log (LOG_DEBUG, "Will redial in %d seconds\n",
490 c->lac->rtimeout);
491 #endif
492 tv.tv_sec = c->lac->rtimeout;
493 tv.tv_usec = 0;
494 c->lac->rsched = schedule (tv, magic_lac_dial, c->lac);
498 free (c);
502 struct call *new_call (struct tunnel *parent)
504 unsigned char entropy_buf[2] = "\0";
505 struct call *tmp = malloc (sizeof (struct call));
507 if (!tmp)
508 return NULL;
509 tmp->tx_pkts = 0;
510 tmp->rx_pkts = 0;
511 tmp->tx_bytes = 0;
512 tmp->rx_bytes = 0;
513 tmp->zlb_xmit = NULL;
514 /* tmp->throttle = 0; */
515 /* tmp->dethrottle=NULL; */
516 tmp->prx = 0;
517 /* tmp->rbit = 0; */
518 tmp->msgtype = 0;
519 /* tmp->timeout = 0; */
520 tmp->data_seq_num = 0;
521 tmp->data_rec_seq_num = 0;
522 tmp->pLr = -1;
523 tmp->nego = 0;
524 tmp->debug = 0;
525 tmp->seq_reqd = 0;
526 tmp->state = 0; /* Nothing so far */
527 if (parent->self)
529 #ifndef TESTING
530 /* while(get_call(parent->ourtid, (tmp->ourcid = (rand() && 0xFFFF)),0,0)); */
531 /* FIXME: What about possibility of multiple random #'s??? */
532 /* tmp->ourcid = (rand () & 0xFFFF); */
533 get_entropy(entropy_buf, 2);
535 unsigned short *temp;
536 temp = (unsigned short *)entropy_buf;
537 tmp->ourcid = *temp & 0xFFFF;
538 #ifdef DEBUG_ENTROPY
539 l2tp_log(LOG_DEBUG, "ourcid = %u, entropy_buf = %hx\n", tmp->ourcid, *temp);
540 #endif
542 #else
543 tmp->ourcid = 0x6227;
544 #endif
546 tmp->dialed[0] = 0;
547 tmp->dialing[0] = 0;
548 tmp->subaddy[0] = 0;
549 tmp->physchan = -1;
550 tmp->serno = 0;
551 tmp->bearer = -1;
552 tmp->cid = -1;
553 tmp->qcid = -1;
554 tmp->container = parent;
555 /* tmp->rws = -1; */
556 tmp->fd = -1;
557 tmp->oldptyconf = malloc (sizeof (struct termios));
558 tmp->pnu = 0;
559 tmp->cnu = 0;
560 tmp->needclose = 0;
561 tmp->closing = 0;
562 tmp->die = 0;
563 tmp->pppd = 0;
564 tmp->error = -1;
565 tmp->result = -1;
566 tmp->errormsg[0] = 0;
567 tmp->fbit = 0;
568 tmp->cid = 0;
569 tmp->lbit = 0;
570 /* Inherit LAC and LNS from parent */
571 tmp->lns = parent->lns;
572 tmp->lac = parent->lac;
573 tmp->addr = 0;
574 /* tmp->ourrws = DEFAULT_RWS_SIZE; */
575 /* if (tmp->ourrws >= 0)
576 tmp->ourfbit = FBIT;
577 else */
578 tmp->ourfbit = 0; /* initialize to 0 since we don't actually use this
579 value at this point anywhere in the code (I don't
580 think) We might just be able to remove it completely */
581 tmp->dial_no[0] = '\0'; /* jz: dialing number for outgoing call */
582 return tmp;
585 struct call *get_tunnel (int tunnel, unsigned int addr, int port)
587 struct tunnel *st;
588 if (tunnel)
590 st = tunnels.head;
591 while (st)
593 if (st->ourtid == tunnel)
595 return st->self;
597 st = st->next;
600 return NULL;
603 struct call *get_call (int tunnel, int call, unsigned int addr, int port,
604 IPsecSAref_t refme, IPsecSAref_t refhim)
607 * Figure out which call struct should handle this.
608 * If we have tunnel and call ID's then they are unique.
609 * Otherwise, if the tunnel is 0, look for an existing connection
610 * or create a new tunnel.
612 struct tunnel *st;
613 struct call *sc;
614 if (tunnel)
616 st = tunnels.head;
617 while (st)
619 if (st->ourtid == tunnel &&
620 (gconfig.ipsecsaref==0 ||
621 (st->refhim == refhim
622 || refhim==IPSEC_SAREF_NULL
623 || st->refhim==IPSEC_SAREF_NULL)))
625 if (call)
627 sc = st->call_head;
628 while (sc)
630 /* confirm that this is in fact a call with the right SA! */
631 if (sc->ourcid == call) return sc;
632 sc = sc->next;
634 l2tp_log (LOG_DEBUG, "%s: can't find call %d in tunnel %d\n (ref=%d/%d)",
635 __FUNCTION__, call, tunnel, refme, refhim);
636 return NULL;
638 else
640 return st->self;
643 st = st->next;
646 l2tp_log (LOG_INFO, "Can not find tunnel %u (refhim=%u)\n",
647 tunnel, refhim);
648 return NULL;
650 else
652 /* You can't specify a call number if you haven't specified
653 a tunnel silly! */
655 if (call)
657 l2tp_log (LOG_WARNING,
658 "%s: call ID specified, but no tunnel ID specified. tossing.\n",
659 __FUNCTION__);
660 return NULL;
663 * Well, nothing appropriate... Let's add a new tunnel, if
664 * we are not at capacity.
666 if (gconfig.debug_tunnel)
668 l2tp_log (LOG_DEBUG,
669 "%s: allocating new tunnel for host %s, port %d.\n",
670 __FUNCTION__, IPADDY (addr), ntohs (port));
672 if (!(st = new_tunnel ()))
674 l2tp_log (LOG_WARNING,
675 "%s: unable to allocate new tunnel for host %s, port %d.\n",
676 __FUNCTION__, IPADDY (addr), ntohs (port));
677 return NULL;
679 st->peer.sin_family = AF_INET;
680 st->peer.sin_port = port;
681 st->refme = refme;
682 st->refhim = refhim;
683 bcopy (&addr, &st->peer.sin_addr, sizeof (addr));
684 st->next = tunnels.head;
685 tunnels.head = st;
686 tunnels.count++;
687 /* Add route to the peer */
688 memset(&st->rt, 0, sizeof(&st->rt));
689 route_add(st->peer.sin_addr, &st->rt);
690 return st->self;