2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
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
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
29 #include "ipsecmast.h"
31 struct buffer
*new_payload (struct sockaddr_in peer
)
33 struct buffer
*tmp
= new_buf (MAX_RECV_SIZE
);
37 tmp
->start
+= sizeof (struct payload_hdr
);
42 inline void recycle_payload (struct buffer
*buf
, struct sockaddr_in peer
)
44 buf
->start
= buf
->rstart
+ sizeof (struct payload_hdr
);
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 */
57 if (!c
->fbit
&& !c
->ourfbit
)
59 /* Forget about Ns and Nr fields then */
65 /* Forget about specifying the length */
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
);
74 p
->length
= htons ((_u16
) buf
->len
);
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
);
91 int read_packet (struct buffer
*buf
, int fd
, int convert
)
94 unsigned char escape
= 0;
96 static unsigned char rbuf
[MAX_RECV_SIZE
];
102 /* Read a packet, doing async->sync conversion if necessary */
108 max
= read(fd
, rbuf
, sizeof (rbuf
));
119 /* if there was a short read, then see what is about */
125 * Hmm.. Nothing to read. It happens
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
141 l2tp_log (LOG_DEBUG
, "%s: Error %d (%s)\n", __FUNCTION__
, errno
,
146 "%s: Too many errors. Declaring call dead.\n",
160 l2tp_log (LOG_DEBUG
, "%s: got an escaped PPP_FLAG\n",
170 /* must be the end, drop the FCS */
173 else if (buf
->len
== 1) {
174 /* Do nothing, just return the single character*/
177 /* if the buffer is empty, then we have the beginning
178 * of a packet, not the end
185 /* if there is space, then insert the byte */
186 if (buf
->len
< buf
->maxlen
)
194 /* return what we have now */
207 if (buf
->len
< buf
->maxlen
)
214 l2tp_log (LOG_WARNING
, "%s: read overrun\n", __FUNCTION__
);
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__
);
227 void call_close (struct call
*c
)
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",
238 if (c
== c
->container
->self
)
241 * We're actually closing the
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. */
254 if ((((struct buffer
*) se
->data
)->tunnel
== c
->container
)
255 || ((struct tunnel
*) se
->data
== c
->container
))
258 l2tp_log (LOG_DEBUG
, "%s: Descheduling event\n", __FUNCTION__
);
262 ose
->next
= se
->next
;
263 if ((struct tunnel
*) se
->data
!= c
->container
)
264 toss ((struct buffer
*) (se
->data
));
271 if ((struct tunnel
*) se
->data
!= c
->container
)
272 toss ((struct buffer
*) (se
->data
));
286 /* Really close this tunnel, as our
287 StopCCN has been ack'd */
289 l2tp_log (LOG_DEBUG
, "%s: Actually closing tunnel %d\n", __FUNCTION__
,
290 c
->container
->ourtid
);
292 destroy_tunnel (c
->container
);
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
,
310 add_tunnelid_avp (buf
, c
->container
->ourtid
);
312 c
->result
= RESULT_CLEAR
;
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
);
321 l2tp_log (LOG_DEBUG
, "%s: enqueing close message for tunnel\n",
326 * We also need to stop all traffic on any calls contained
329 tmp
= c
->container
->call_head
;
339 "Connection %d closed to %s, port %d (%s)\n",
341 IPADDY (c
->container
->peer
.sin_addr
),
342 ntohs (c
->container
->peer
.sin_port
), c
->errormsg
);
350 deschedule (c
->zlb_xmit
);
351 /* if (c->dethrottle) deschedule(c->dethrottle); */
355 l2tp_log (LOG_DEBUG
, "%s: Actually closing call %d\n", __FUNCTION__
,
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
,
371 c
->result
= RESULT_CLEAR
;
374 add_result_code_avp (buf
, c
->result
, c
->error
, c
->errormsg
,
375 strlen (c
->errormsg
));
377 add_callid_avp (buf
, c
->ourcid
, c
->container
);
379 add_callid_avp (buf
, c
->ourcid
);
381 add_control_hdr (c
->container
, c
, buf
);
382 if (gconfig
.packet_dump
)
383 do_packet_dump (buf
);
385 l2tp_log (LOG_DEBUG
, "%s: enqueuing close message for call %d\n",
386 __FUNCTION__
, c
->ourcid
);
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
398 void destroy_call (struct call
*c
)
401 * Here, we unconditionally destroy a call.
412 /* if (c->dethrottle) deschedule(c->dethrottle); */
414 deschedule (c
->zlb_xmit
);
418 unreserve_addr (c
->addr
);
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
430 /* Set c->pppd to zero to prevent recursion with child_handler */
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
446 l2tp_log (LOG_DEBUG
, "Terminating pppd: sending TERM signal to pid %d\n", pid
);
451 l2tp_log (LOG_DEBUG
, "Terminating pppd: sending KILL signal to pid %d\n", pid
);
458 p
= c
->container
->call_head
;
460 * Remove us from the call list, although
461 * we might not actually be there
467 c
->container
->call_head
= c
->next
;
468 c
->container
->count
--;
472 while (p
->next
&& (p
->next
!= c
))
477 c
->container
->count
--;
485 if (c
->lac
->redial
&& (c
->lac
->rtimeout
> 0) && !c
->lac
->rsched
&&
489 l2tp_log (LOG_DEBUG
, "Will redial in %d seconds\n",
492 tv
.tv_sec
= c
->lac
->rtimeout
;
494 c
->lac
->rsched
= schedule (tv
, magic_lac_dial
, c
->lac
);
502 struct call
*new_call (struct tunnel
*parent
)
504 unsigned char entropy_buf
[2] = "\0";
505 struct call
*tmp
= malloc (sizeof (struct call
));
513 tmp
->zlb_xmit
= NULL
;
514 /* tmp->throttle = 0; */
515 /* tmp->dethrottle=NULL; */
519 /* tmp->timeout = 0; */
520 tmp
->data_seq_num
= 0;
521 tmp
->data_rec_seq_num
= 0;
526 tmp
->state
= 0; /* Nothing so far */
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;
539 l2tp_log(LOG_DEBUG
, "ourcid = %u, entropy_buf = %hx\n", tmp
->ourcid
, *temp
);
543 tmp
->ourcid
= 0x6227;
554 tmp
->container
= parent
;
557 tmp
->oldptyconf
= malloc (sizeof (struct termios
));
566 tmp
->errormsg
[0] = 0;
570 /* Inherit LAC and LNS from parent */
571 tmp
->lns
= parent
->lns
;
572 tmp
->lac
= parent
->lac
;
574 /* tmp->ourrws = DEFAULT_RWS_SIZE; */
575 /* if (tmp->ourrws >= 0)
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 */
585 struct call
*get_tunnel (int tunnel
, unsigned int addr
, int port
)
593 if (st
->ourtid
== tunnel
)
603 struct call
*get_call (int tunnel
, int call
, struct in_addr 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.
619 if (st
->ourtid
== tunnel
&&
620 (gconfig
.ipsecsaref
==0 ||
621 (st
->refhim
== refhim
622 || refhim
==IPSEC_SAREF_NULL
623 || st
->refhim
==IPSEC_SAREF_NULL
)))
630 /* confirm that this is in fact a call with the right SA! */
631 if (sc
->ourcid
== call
) return sc
;
634 l2tp_log (LOG_DEBUG
, "%s: can't find call %d in tunnel %d\n (ref=%d/%d)",
635 __FUNCTION__
, call
, tunnel
, refme
, refhim
);
646 l2tp_log (LOG_INFO
, "Can not find tunnel %u (refhim=%u)\n",
652 /* You can't specify a call number if you haven't specified
657 l2tp_log (LOG_WARNING
,
658 "%s: call ID specified, but no tunnel ID specified. tossing.\n",
663 * Well, nothing appropriate... Let's add a new tunnel, if
664 * we are not at capacity.
666 if (gconfig
.debug_tunnel
)
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
));
679 st
->peer
.sin_family
= AF_INET
;
680 st
->peer
.sin_port
= port
;
685 bcopy (&addr
, &st
->peer
.sin_addr
, sizeof (addr
));
686 st
->next
= tunnels
.head
;
689 /* Add route to the peer */
690 memset(&st
->rt
, 0, sizeof(&st
->rt
));
691 route_add(st
->peer
.sin_addr
, &st
->rt
);