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