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 * Control Packet Handling
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 _u16 ppp_crc16_table
[256] = {
26 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
27 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
28 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
29 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
30 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
31 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
32 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
33 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
34 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
35 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
36 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
37 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
38 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
39 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
40 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
41 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
42 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
43 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
44 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
45 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
46 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
47 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
48 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
49 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
50 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
51 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
52 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
53 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
54 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
55 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
56 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
57 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
62 struct buffer
*new_outgoing (struct tunnel
*t
)
65 * Make a new outgoing control packet
67 struct buffer
*tmp
= new_buf (MAX_RECV_SIZE
);
71 tmp
->start
+= sizeof (struct control_hdr
);
78 inline void recycle_outgoing (struct buffer
*buf
, struct sockaddr_in peer
)
81 * This should only be used for ZLB's!
83 buf
->start
= buf
->rstart
+ sizeof (struct control_hdr
);
89 void add_fcs (struct buffer
*buf
)
91 _u16 fcs
= PPP_INITFCS
;
92 unsigned char *c
= buf
->start
;
94 for (x
= 0; x
< buf
->len
; x
++)
96 fcs
= PPP_FCS (fcs
, *c
);
102 *c
= (fcs
>> 8) & 0xFF;
106 void add_control_hdr (struct tunnel
*t
, struct call
*c
, struct buffer
*buf
)
108 struct control_hdr
*h
;
109 buf
->start
-= sizeof (struct control_hdr
);
110 buf
->len
+= sizeof (struct control_hdr
);
111 h
= (struct control_hdr
*) buf
->start
;
112 h
->ver
= htons (TBIT
| LBIT
| FBIT
| VER_L2TP
);
113 h
->length
= htons ((_u16
) buf
->len
);
114 h
->tid
= htons (t
->tid
);
115 h
->cid
= htons (c
->cid
);
116 h
->Ns
= htons (t
->control_seq_num
);
117 h
->Nr
= htons (t
->control_rec_seq_num
);
118 t
->control_seq_num
++;
122 void hello (void *tun
)
127 tv
.tv_sec
= HELLO_DELAY
;
129 t
= (struct tunnel
*) tun
;
130 buf
= new_outgoing (t
);
131 add_message_type_avp (buf
, Hello
);
132 add_control_hdr (t
, t
->self
, buf
);
133 if (gconfig
.packet_dump
)
134 do_packet_dump (buf
);
136 l2tp_log (LOG_DEBUG
, "%s: sending Hello on %d\n", __FUNCTION__
, t
->ourtid
);
140 * Schedule another Hello in a little bit.
143 l2tp_log (LOG_DEBUG
, "%s: scheduling another Hello on %d\n", __FUNCTION__
,
146 t
->hello
= schedule (tv
, hello
, (void *) t
);
149 void control_zlb (struct buffer
*buf
, struct tunnel
*t
, struct call
*c
)
151 recycle_outgoing (buf
, t
->peer
);
152 add_control_hdr (t
, c
, buf
);
153 t
->control_seq_num
--;
155 l2tp_log (LOG_DEBUG
, "%s: sending control ZLB on tunnel %d\n", __FUNCTION__
,
161 int control_finish (struct tunnel
*t
, struct call
*c
)
164 * After all AVP's have been handled, do anything else
165 * which needs to be done, like prepare response
166 * packets to go back. This is essentially the
167 * implementation of the state machine of section 7.2.1
169 * If we set c->needclose, the call (or tunnel) will
170 * be closed upon return.
179 char dummy_buf
[128] = "/var/l2tp/"; /* jz: needed to read /etc/ppp/var.options - just kick it if you don't like */
180 char passwdfd_buf
[32] = ""; /* buffer for the fd, not the password */
182 int pppd_passwdfd
[2];
187 l2tp_log (LOG_DEBUG
, "%s: Whoa... non-ZLB with no message type!\n",
191 if (gconfig
.debug_state
)
193 "%s: message type is %s(%d). Tunnel is %d, call is %d.\n",
194 __FUNCTION__
, msgtypes
[c
->msgtype
], c
->msgtype
, t
->tid
, c
->cid
);
199 * We need to initiate a connection.
205 t
->ourrws
= t
->lns
->tun_rws
;
206 t
->hbit
= t
->lns
->hbit
;
207 t
->rxspeed
= t
->lns
->rxspeed
;
208 t
->txspeed
= t
->lns
->txspeed
;
212 t
->ourrws
= t
->lac
->tun_rws
;
213 t
->hbit
= t
->lac
->hbit
;
214 t
->rxspeed
= t
->lac
->rxspeed
;
215 t
->txspeed
= t
->lac
->txspeed
;
217 /* This is an attempt to bring up the tunnel */
219 buf
= new_outgoing (t
);
220 add_message_type_avp (buf
, SCCRQ
);
223 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
224 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
226 add_protocol_avp (buf
);
227 add_frame_caps_avp (buf
, t
->ourfc
);
228 add_bearer_caps_avp (buf
, t
->ourbc
);
229 /* FIXME: Tie breaker */
230 add_firmware_avp (buf
);
231 if (t
->lac
&& t
->lac
->hostname
&& t
->lac
->hostname
[0])
232 add_hostname_avp (buf
, t
->lac
->hostname
);
233 else if (t
->lns
&& t
->lns
->hostname
&& t
->lns
->hostname
[0])
234 add_hostname_avp (buf
, t
->lns
->hostname
);
236 add_hostname_avp (buf
, hostname
);
237 add_vendor_avp (buf
);
238 add_tunnelid_avp (buf
, t
->ourtid
);
240 add_avp_rws (buf
, t
->ourrws
);
241 if ((t
->lac
&& t
->lac
->challenge
)
242 || (t
->lns
&& t
->lns
->challenge
))
244 if (t
->chal_them
.challenge
)
245 free(t
->chal_them
.challenge
);
246 t
->chal_them
.challenge
= malloc(MD_SIG_SIZE
);
247 if (!(t
->chal_them
.challenge
))
249 l2tp_log (LOG_WARNING
, "%s: malloc failed for challenge\n",
254 mk_challenge (t
->chal_them
.challenge
, MD_SIG_SIZE
);
255 t
->chal_them
.chal_len
= MD_SIG_SIZE
;
256 add_challenge_avp (buf
, t
->chal_them
.challenge
,
257 t
->chal_them
.chal_len
);
258 t
->chal_them
.state
= STATE_CHALLENGED
;
259 /* We generate the challenge and make a note that we plan to
260 challenge the peer, but we can't predict the response yet
261 because we don't know their hostname AVP */
263 add_control_hdr (t
, c
, buf
);
265 if (gconfig
.packet_dump
)
266 do_packet_dump (buf
);
267 if (gconfig
.debug_state
)
268 l2tp_log (LOG_DEBUG
, "%s: sending SCCRQ\n",
279 c
->lbit
= c
->lns
->lbit
? LBIT
: 0;
280 /* c->ourrws = c->lns->call_rws;
281 if (c->ourrws > -1) c->ourfbit = FBIT; else c->ourfbit = 0; */
285 c
->lbit
= c
->lac
->lbit
? LBIT
: 0;
286 /* c->ourrws = c->lac->call_rws;
287 if (c->ourrws > -1) c->ourfbit = FBIT; else c->ourfbit = 0; */
289 buf
= new_outgoing (t
);
290 add_message_type_avp (buf
, ICRQ
);
293 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
294 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
297 add_callid_avp (buf
, c
->ourcid
, t
);
299 add_callid_avp (buf
, c
->ourcid
);
301 add_serno_avp (buf
, global_serno
);
302 c
->serno
= global_serno
;
304 add_bearer_avp (buf
, 0);
305 add_control_hdr (t
, c
, buf
);
307 if (gconfig
.packet_dump
)
308 do_packet_dump (buf
);
309 if (gconfig
.debug_state
)
310 l2tp_log (LOG_DEBUG
, "%s: sending ICRQ\n", __FUNCTION__
);
314 { /* jz: sending a OCRQ */
318 c
->lbit
= c
->lns
->lbit
? LBIT
: 0;
319 /* c->ourrws = c->lns->call_rws;
320 if (c->ourrws > -1) c->ourfbit = FBIT; else c->ourfbit = 0; */
324 /* c->ourrws = c->lac->call_rws;
325 if (c->ourrws > -1) c->ourfbit = FBIT; else c->ourfbit = 0; */
328 if (t
->fc
& SYNC_FRAMING
)
329 c
->frame
= SYNC_FRAMING
;
331 c
->frame
= ASYNC_FRAMING
;
332 buf
= new_outgoing (t
);
333 add_message_type_avp (buf
, OCRQ
);
335 add_callid_avp (buf
, c
->ourcid
, t
);
337 add_callid_avp (buf
, c
->ourcid
);
339 add_serno_avp (buf
, global_serno
);
340 c
->serno
= global_serno
;
342 add_minbps_avp (buf
, DEFAULT_MIN_BPS
);
343 add_maxbps_avp (buf
, DEFAULT_MAX_BPS
);
344 add_bearer_avp (buf
, 0);
345 add_frame_avp (buf
, c
->frame
);
346 add_number_avp (buf
, c
->dial_no
);
347 add_control_hdr (t
, c
, buf
);
349 if (gconfig
.packet_dump
)
350 do_packet_dump (buf
);
357 * We've received a request, now let's
358 * formulate a response.
364 "%s: Peer did not specify assigned tunnel ID. Closing.\n",
366 set_error (c
, VENDOR_ERROR
, "Specify your assigned tunnel ID");
370 if (!(t
->lns
= get_lns (t
)))
374 "%s: Denied connection to unauthorized peer %s\n",
375 __FUNCTION__
, IPADDY (t
->peer
.sin_addr
));
376 set_error (c
, VENDOR_ERROR
, "No Authorization");
380 t
->ourrws
= t
->lns
->tun_rws
;
381 t
->hbit
= t
->lns
->hbit
;
386 "%s: Peer did not specify framing capability. Closing.\n",
388 set_error (c
, VENDOR_ERROR
, "Specify framing capability");
392 /* FIXME: Do we need to be sure they specified a version number?
393 * Theoretically, yes, but we don't have anything in the code
394 * to actually *do* anything with it, so...why check at this point?
395 * We shouldn't be requiring a bearer capabilities avp to be present in
396 * SCCRQ and SCCRP as they aren't required
398 if (DEBUG) l2tp_log(LOG_DEBUG,
399 "%s: Peer did not specify bearer capability. Closing.\n",__FUNCTION__);
400 set_error(c, VENDOR_ERROR, "Specify bearer capability");
404 if ((!strlen (t
->hostname
)) && ((t
->chal_us
.state
) || ((t
->lns
->challenge
))))
408 "%s: Peer did not specify hostname. Closing.\n",
410 set_error (c
, VENDOR_ERROR
, "Specify your hostname");
417 if ((y
->tid
== t
->tid
) &&
418 (y
->peer
.sin_addr
.s_addr
== t
->peer
.sin_addr
.s_addr
) &&
419 (!gconfig
.ipsecsaref
|| y
->refhim
== t
->refhim
) &&
422 /* This can happen if we get a duplicate
423 StartCCN or if they don't get our ack packet */
425 * But it is legitimate for two different remote systems
426 * to use the same tid
429 "%s: Peer requested tunnel %d twice, ignoring second one.\n",
430 __FUNCTION__
, t
->tid
);
438 buf
= new_outgoing (t
);
439 add_message_type_avp (buf
, SCCRP
);
442 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
443 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
445 add_protocol_avp (buf
);
446 add_frame_caps_avp (buf
, t
->ourfc
);
447 add_bearer_caps_avp (buf
, t
->ourbc
);
448 add_firmware_avp (buf
);
449 if (t
->lac
&& t
->lac
->hostname
&& t
->lac
->hostname
[0])
450 add_hostname_avp (buf
, t
->lac
->hostname
);
451 else if (t
->lns
&& t
->lns
->hostname
&& t
->lns
->hostname
[0])
452 add_hostname_avp (buf
, t
->lns
->hostname
);
454 add_hostname_avp (buf
, hostname
);
455 add_vendor_avp (buf
);
456 add_tunnelid_avp (buf
, t
->ourtid
);
458 add_avp_rws (buf
, t
->ourrws
);
459 if (t
->chal_us
.state
)
461 t
->chal_us
.ss
= SCCRP
;
462 handle_challenge (t
, &t
->chal_us
);
463 add_chalresp_avp (buf
, t
->chal_us
.response
, MD_SIG_SIZE
);
465 if (t
->lns
->challenge
)
467 if (t
->chal_them
.challenge
)
468 free(t
->chal_them
.challenge
);
469 t
->chal_them
.challenge
= malloc(MD_SIG_SIZE
);
470 if (!(t
->chal_them
.challenge
))
472 l2tp_log (LOG_WARNING
, "%s: malloc failed\n", __FUNCTION__
);
473 set_error (c
, VENDOR_ERROR
, "malloc failed");
477 mk_challenge (t
->chal_them
.challenge
, MD_SIG_SIZE
);
478 t
->chal_them
.chal_len
= MD_SIG_SIZE
;
479 t
->chal_them
.ss
= SCCCN
;
480 if (handle_challenge (t
, &t
->chal_them
))
482 /* We already know what to expect back */
483 l2tp_log (LOG_WARNING
, "%s: No secret for '%s'\n", __FUNCTION__
,
485 set_error (c
, VENDOR_ERROR
, "No secret on our side");
489 add_challenge_avp (buf
, t
->chal_them
.challenge
,
490 t
->chal_them
.chal_len
);
492 add_control_hdr (t
, c
, buf
);
493 if (gconfig
.packet_dump
)
494 do_packet_dump (buf
);
496 if (gconfig
.debug_state
)
497 l2tp_log (LOG_DEBUG
, "%s: sending SCCRP\n", __FUNCTION__
);
503 * We have a reply. If everything is okay, send
504 * a connected message
510 "%s: Peer did not specify framing capability. Closing.\n",
512 set_error (c
, VENDOR_ERROR
, "Specify framing capability");
516 /* FIXME: Do we need to be sure they specified a version number?
517 * Theoretically, yes, but we don't have anything in the code
518 * to actually *do* anything with it, so...why check at this point?
519 * We shouldn't be requiring a bearer capabilities avp to be present in
520 * SCCRQ and SCCRP as they aren't required
522 if (DEBUG) log(LOG_DEBUG,
523 "%s: Peer did not specify bearer capability. Closing.\n",__FUNCTION__);
524 set_error(c, VENDOR_ERROR, "Specify bearer capability");
528 if ((!strlen (t
->hostname
)) && ((t
->chal_them
.state
) || ((t
->chal_us
.state
))))
532 "%s: Peer did not specify hostname. Closing.\n",
534 set_error (c
, VENDOR_ERROR
, "Specify your hostname");
542 "%s: Peer did not specify assigned tunnel ID. Closing.\n",
544 set_error (c
, VENDOR_ERROR
, "Specify your assigned tunnel ID");
548 if (t
->chal_them
.state
)
550 t
->chal_them
.ss
= SCCRP
;
551 if (handle_challenge (t
, &t
->chal_them
))
553 set_error (c
, VENDOR_ERROR
, "No secret key on our side");
554 l2tp_log (LOG_WARNING
, "%s: No secret key for authenticating '%s'\n",
555 __FUNCTION__
, t
->hostname
);
560 (t
->chal_them
.reply
, t
->chal_them
.response
, MD_SIG_SIZE
))
562 set_error (c
, VENDOR_ERROR
,
563 "Invalid challenge authentication");
564 l2tp_log (LOG_DEBUG
, "%s: Invalid authentication for host '%s'\n",
565 __FUNCTION__
, t
->hostname
);
570 if (t
->chal_us
.state
)
572 t
->chal_us
.ss
= SCCCN
;
573 if (handle_challenge (t
, &t
->chal_us
))
575 l2tp_log (LOG_WARNING
, "%s: No secret for authenticating to '%s'\n",
576 __FUNCTION__
, t
->hostname
);
577 set_error (c
, VENDOR_ERROR
, "No secret key on our end");
583 buf
= new_outgoing (t
);
584 add_message_type_avp (buf
, SCCCN
);
587 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
588 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
590 if (t
->chal_us
.state
)
591 add_chalresp_avp (buf
, t
->chal_us
.response
, MD_SIG_SIZE
);
592 add_control_hdr (t
, c
, buf
);
593 if (gconfig
.packet_dump
)
594 do_packet_dump (buf
);
596 if (gconfig
.debug_state
)
597 l2tp_log (LOG_DEBUG
, "%s: sending SCCCN\n", __FUNCTION__
);
599 /* Schedule a HELLO */
600 tv
.tv_sec
= HELLO_DELAY
;
603 l2tp_log (LOG_DEBUG
, "%s: scheduling initial HELLO on %d\n", __FUNCTION__
,
606 t
->hello
= schedule (tv
, hello
, (void *) t
);
607 l2tp_log (LOG_NOTICE
,
608 "Connection established to %s, %d. Local: %d, Remote: %d (ref=%u/%u).\n",
609 IPADDY (t
->peer
.sin_addr
),
610 ntohs (t
->peer
.sin_port
), t
->ourtid
, t
->tid
, t
->refme
, t
->refhim
);
613 /* This is part of a LAC, so we want to go ahead
614 and start an ICRQ now */
615 magic_lac_dial (t
->lac
);
619 if (t
->chal_them
.state
)
622 (t
->chal_them
.reply
, t
->chal_them
.response
, MD_SIG_SIZE
))
624 set_error (c
, VENDOR_ERROR
,
625 "Invalid challenge authentication");
626 l2tp_log (LOG_DEBUG
, "%s: Invalid authentication for host '%s'\n",
627 __FUNCTION__
, t
->hostname
);
633 l2tp_log (LOG_NOTICE
,
634 "Connection established to %s, %d. Local: %d, Remote: %d (ref=%u/%u). LNS session is '%s'\n",
635 IPADDY (t
->peer
.sin_addr
),
636 ntohs (t
->peer
.sin_port
), t
->ourtid
, t
->tid
, t
->refme
, t
->refhim
,
638 /* Schedule a HELLO */
639 tv
.tv_sec
= HELLO_DELAY
;
642 l2tp_log (LOG_DEBUG
, "%s: scheduling initial HELLO on %d\n", __FUNCTION__
,
645 t
->hello
= schedule (tv
, hello
, (void *) t
);
652 "%s: Peer tried to disconnect without specifying tunnel ID\n",
657 /* Work around bug in MSL2TP client */
658 if ((t
->firmware
== 0xff00) && (!(strncmp(t
->vendor
, "Deterministic Networks Inc.", 27))))
662 if ((t
->qtid
!= tmptid
) && (tmptid
> 0))
666 "%s: Peer [Vendor:%s] [Firmware:%d (0x%.4x)] tried to disconnect with invalid TID (%d != %d)\n",
667 __FUNCTION__
, t
->vendor
, t
->firmware
, t
->firmware
, t
->qtid
, tmptid
);
670 /* In case they're disconnecting immediately after SCCN */
673 if (t
->self
->result
< 0)
677 "%s: Peer tried to disconnect without specifying result code.\n",
682 "%s: Connection closed to %s, port %d (%s), Local: %d, Remote: %d\n",
683 __FUNCTION__
, IPADDY (t
->peer
.sin_addr
),
684 ntohs (t
->peer
.sin_port
), t
->self
->errormsg
, t
->ourtid
, t
->tid
);
692 set_error (p
, ERROR_INVALID
, "This tunnel cannot accept calls\n");
696 p
->lbit
= p
->lns
->lbit
? LBIT
: 0;
697 /* p->ourrws = p->lns->call_rws;
698 if (p->ourrws > -1) p->ourfbit = FBIT; else p->ourfbit = 0; */
703 "%s: Peer tried to initiate call without call ID\n",
705 /* Here it doesn't make sense to use the needclose flag because
706 the call p did not receive any packets */
713 if (z
->cid
== p
->cid
)
715 /* This can happen if we get a duplicate
716 ICRQ or if they don't get our ack packet */
718 "%s: Peer requested call %d twice, ignoring second one.\n",
719 __FUNCTION__
, p
->cid
);
727 /* FIXME: by commenting this out, we're not checking whether the serial
728 * number avp is included in the ICRQ at all which its required to be.
729 * Since the serial number is only used for human debugging aid, this
730 * isn't a big deal, but it would be nice to have *some* sort of check
731 * for it and perhaps just log it and go on. */
732 /* JLM if (p->serno<1) {
733 if (DEBUG) log(LOG_DEBUG,
734 "%s: Peer did not specify serial number when initiating call\n", __FUNCTION__);
740 if (t
->lns
->assign_ip
) {
741 p
->addr
= get_addr (t
->lns
->range
);
744 set_error (p
, ERROR_NORES
, "No available IP address");
746 l2tp_log (LOG_DEBUG
, "%s: Out of IP addresses on tunnel %d!\n",
747 __FUNCTION__
, t
->tid
);
750 reserve_addr (p
->addr
);
757 buf
= new_outgoing (t
);
758 add_message_type_avp (buf
, ICRP
);
761 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
762 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
765 add_callid_avp (buf
, p
->ourcid
, t
);
767 add_callid_avp (buf
, p
->ourcid
);
769 /* if (p->ourrws >=0)
770 add_avp_rws(buf, p->ourrws); */
772 * FIXME: I should really calculate
773 * Packet Processing Delay
775 /* add_ppd_avp(buf,ppd); */
776 add_control_hdr (t
, p
, buf
);
777 if (gconfig
.packet_dump
)
778 do_packet_dump (buf
);
780 if (gconfig
.debug_state
)
781 l2tp_log (LOG_DEBUG
, "%s: Sending ICRP\n", __FUNCTION__
);
789 "%s: Peer tried to negotiate ICRP without specifying call ID\n",
795 if (t
->fc
& ASYNC_FRAMING
)
796 c
->frame
= ASYNC_FRAMING
;
798 c
->frame
= SYNC_FRAMING
;
800 buf
= new_outgoing (t
);
801 add_message_type_avp (buf
, ICCN
);
804 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
805 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
807 add_txspeed_avp (buf
, t
->txspeed
);
808 add_frame_avp (buf
, c
->frame
);
809 /* if (c->ourrws >= 0)
810 add_avp_rws(buf, c->ourrws); */
811 /* FIXME: Packet Processing Delay */
812 /* We don't need any kind of proxy PPP stuff */
813 /* Can we proxy authenticate ourselves??? */
814 add_rxspeed_avp (buf
, t
->rxspeed
);
815 /* add_seqreqd_avp (buf); *//* We don't have sequencing code, so
816 * don't ask for sequencing */
817 add_control_hdr (t
, c
, buf
);
818 if (gconfig
.packet_dump
)
819 do_packet_dump (buf
);
821 if (gconfig
.debug_state
)
822 l2tp_log (LOG_DEBUG
, "%s: Sending ICCN\n", __FUNCTION__
);
823 l2tp_log (LOG_NOTICE
,
824 "Call established with %s, Local: %d, Remote: %d, Serial: %d (ref=%u/%u)\n",
825 IPADDY (t
->peer
.sin_addr
), c
->ourcid
, c
->cid
,
826 c
->serno
, t
->refme
, t
->refhim
);
829 po
= add_opt (po
, "passive");
830 po
= add_opt (po
, "nodetach");
833 if (c
->lac
->defaultroute
)
834 po
= add_opt (po
, "defaultroute");
835 strncpy (ip1
, IPADDY (c
->lac
->localaddr
), sizeof (ip1
));
836 strncpy (ip2
, IPADDY (c
->lac
->remoteaddr
), sizeof (ip2
));
838 po
= add_opt (po
, "%s:%s", c
->lac
->localaddr
? ip1
: "",
839 c
->lac
->remoteaddr
? ip2
: "");
841 if (c
->lac
->authself
)
843 if (c
->lac
->pap_refuse
)
844 po
= add_opt (po
, "refuse-pap");
845 if (c
->lac
->chap_refuse
)
846 po
= add_opt (po
, "refuse-chap");
850 po
= add_opt (po
, "refuse-pap");
851 po
= add_opt (po
, "refuse-chap");
853 if (c
->lac
->authpeer
)
855 po
= add_opt (po
, "auth");
856 if (c
->lac
->pap_require
)
857 po
= add_opt (po
, "require-pap");
858 if (c
->lac
->chap_require
)
859 po
= add_opt (po
, "require-chap");
861 if (c
->lac
->authname
[0])
863 po
= add_opt (po
, "name");
864 po
= add_opt (po
, c
->lac
->authname
);
867 po
= add_opt (po
, "debug");
868 if (c
->lac
->password
[0])
870 if (pipe (pppd_passwdfd
) == -1)
873 "%s: Unable to create password pipe for pppd\n", __FUNCTION__
);
876 if (-1 == write (pppd_passwdfd
[1], c
->lac
->password
, strlen (c
->lac
->password
)))
879 "%s: Unable to write password to pipe for pppd\n", __FUNCTION__
);
880 close (pppd_passwdfd
[1]);
883 close (pppd_passwdfd
[1]);
885 /* clear memory used for password, paranoid? */
886 for (i
= 0; i
< STRLEN
; i
++)
887 c
->lac
->password
[i
] = '\0';
889 po
= add_opt (po
, "plugin");
890 po
= add_opt (po
, "passwordfd.so");
891 po
= add_opt (po
, "passwordfd");
892 snprintf (passwdfd_buf
, 32, "%d", pppd_passwdfd
[0]);
893 po
= add_opt (po
, passwdfd_buf
);
895 if (c
->lac
->pppoptfile
[0])
897 po
= add_opt (po
, "file");
898 po
= add_opt (po
, c
->lac
->pppoptfile
);
905 if (c
->lac
->password
[0])
906 close(pppd_passwdfd
[0]);
912 "%s: Peer attempted ICCN on the actual tunnel, not the call",
919 "%s: Warning: Peer did not specify transmit speed\n", __FUNCTION__
);
920 /* don't refuse the connection over this
928 "%s: Warning: Peer did not specify framing type\n", __FUNCTION__
);
929 /* don't refuse the connection over this
935 strncpy (ip1
, IPADDY (c
->lns
->localaddr
), sizeof (ip1
));
936 strncpy (ip2
, IPADDY (c
->addr
), sizeof (ip2
));
938 po
= add_opt (po
, "passive");
939 po
= add_opt (po
, "nodetach");
940 po
= add_opt (po
, "%s:%s", c
->lns
->localaddr
? ip1
: "", ip2
);
941 if (c
->lns
->authself
)
943 if (c
->lns
->pap_refuse
)
944 po
= add_opt (po
, "refuse-pap");
945 if (c
->lns
->chap_refuse
)
946 po
= add_opt (po
, "refuse-chap");
950 po
= add_opt (po
, "refuse-pap");
951 po
= add_opt (po
, "refuse-chap");
953 if (c
->lns
->authpeer
)
955 po
= add_opt (po
, "auth");
956 if (c
->lns
->pap_require
)
957 po
= add_opt (po
, "require-pap");
958 if (c
->lns
->chap_require
)
959 po
= add_opt (po
, "require-chap");
960 if (c
->lns
->passwdauth
)
961 po
= add_opt (po
, "login");
963 if (c
->lns
->authname
[0])
965 po
= add_opt (po
, "name");
966 po
= add_opt (po
, c
->lns
->authname
);
969 po
= add_opt (po
, "debug");
970 if (c
->lns
->pppoptfile
[0])
972 po
= add_opt (po
, "file");
973 po
= add_opt (po
, c
->lns
->pppoptfile
);
977 l2tp_log (LOG_NOTICE
,
978 "Call established with %s, Local: %d, Remote: %d, Serial: %d\n",
979 IPADDY (t
->peer
.sin_addr
), c
->ourcid
, c
->cid
,
982 case OCRP
: /* jz: nothing to do for OCRP, waiting for OCCN */
984 case OCCN
: /* jz: get OCCN, so the only thing we must do is to start the pppd */
986 po
= add_opt (po
, "passive");
987 po
= add_opt (po
, "nodetach");
988 po
= add_opt (po
, "file");
989 strcat (dummy_buf
, c
->dial_no
); /* jz: use /etc/ppp/dialnumber.options for pppd - kick it if you don't like */
990 strcat (dummy_buf
, ".options");
991 po
= add_opt (po
, dummy_buf
);
994 if (c
->lac
->defaultroute
)
995 po
= add_opt (po
, "defaultroute");
996 strncpy (ip1
, IPADDY (c
->lac
->localaddr
), sizeof (ip1
));
997 strncpy (ip2
, IPADDY (c
->lac
->remoteaddr
), sizeof (ip2
));
998 po
= add_opt (po
, "%s:%s", c
->lac
->localaddr
? ip1
: "",
999 c
->lac
->remoteaddr
? ip2
: "");
1000 if (c
->lac
->authself
)
1002 if (c
->lac
->pap_refuse
)
1003 po
= add_opt (po
, "refuse-pap");
1004 if (c
->lac
->chap_refuse
)
1005 po
= add_opt (po
, "refuse-chap");
1009 po
= add_opt (po
, "refuse-pap");
1010 po
= add_opt (po
, "refuse-chap");
1012 if (c
->lac
->authpeer
)
1014 po
= add_opt (po
, "auth");
1015 if (c
->lac
->pap_require
)
1016 po
= add_opt (po
, "require-pap");
1017 if (c
->lac
->chap_require
)
1018 po
= add_opt (po
, "require-chap");
1020 if (c
->lac
->authname
[0])
1022 po
= add_opt (po
, "name");
1023 po
= add_opt (po
, c
->lac
->authname
);
1026 po
= add_opt (po
, "debug");
1027 if (c
->lac
->pppoptfile
[0])
1029 po
= add_opt (po
, "file");
1030 po
= add_opt (po
, c
->lac
->pppoptfile
);
1035 /* jz: just show some information */
1037 "parameters: Local: %d , Remote: %d , Serial: %d , Pid: %d , Tunnelid: %d , Phoneid: %s\n",
1038 c
->ourcid
, c
->cid
, c
->serno
, c
->pppd
, t
->ourtid
, c
->dial_no
);
1050 l2tp_log (LOG_DEBUG
,
1051 "%s: Peer tried to disconnect without specifying call ID\n",
1058 while (p
&& (p
->cid
!= c
->qcid
))
1063 l2tp_log (LOG_DEBUG
,
1064 "%s: Unable to determine call to be disconnected.\n",
1073 /* Work around bug in MSL2TP client */
1074 if ((t
->firmware
== 0xff00) && (!(strncmp(t
->vendor
, "Deterministic Networks Inc.", 27))))
1079 if ((c
->qcid
!= tmpcid
) && tmpcid
> 0)
1082 l2tp_log (LOG_DEBUG
,
1083 "%s: Peer tried to disconnect with invalid CID (%d != %d)\n",
1084 __FUNCTION__
, c
->qcid
, c
->ourcid
);
1091 l2tp_log (LOG_DEBUG
,
1092 "%s: Peer tried to disconnect without specifying result code.\n",
1097 "%s: Connection closed to %s, serial %d (%s)\n", __FUNCTION__
,
1098 IPADDY (t
->peer
.sin_addr
), c
->serno
, c
->errormsg
);
1107 l2tp_log (LOG_DEBUG
,
1108 "%s: Don't know how to finish a message of type %d\n",
1109 __FUNCTION__
, c
->msgtype
);
1110 set_error (c
, VENDOR_ERROR
, "Unimplemented message %d\n", c
->msgtype
);
1115 inline int check_control (const struct buffer
*buf
, struct tunnel
*t
,
1119 * Check if this is a valid control
1120 * or not. Returns 0 on success
1122 struct control_hdr
*h
= (struct control_hdr
*) (buf
->start
);
1124 if (buf
->len
< sizeof (struct control_hdr
))
1128 l2tp_log (LOG_DEBUG
,
1129 "%s: Received too small of packet\n", __FUNCTION__
);
1134 if (buf
->len
!= h
->length
)
1138 l2tp_log (LOG_DEBUG
,
1139 "%s: Reported and actual sizes differ (%d != %d)\n",
1140 __FUNCTION__
, h
->length
, buf
->len
);
1145 * FIXME: H-bit handling goes here
1147 #ifdef DEBUG_CONTROL
1148 l2tp_log (LOG_DEBUG
, "%s: control, cid = %d, Ns = %d, Nr = %d\n", __FUNCTION__
,
1149 c
->cid
, h
->Ns
, h
->Nr
);
1151 if (h
->Ns
!= t
->control_rec_seq_num
)
1154 l2tp_log (LOG_DEBUG
,
1155 "%s: Received out of order control packet on tunnel %d (got %d, expected %d)\n",
1156 __FUNCTION__
, t
->tid
, h
->Ns
, t
->control_rec_seq_num
);
1157 if (((h
->Ns
< t
->control_rec_seq_num
) &&
1158 ((t
->control_rec_seq_num
- h
->Ns
) < 32768)) ||
1159 ((h
->Ns
> t
->control_rec_seq_num
) &&
1160 ((t
->control_rec_seq_num
- h
->Ns
) > 32768)))
1163 * Woopsies, they sent us a message we should have already received
1164 * so we should send them a ZLB so they know
1165 * for sure that we already have it.
1169 l2tp_log (LOG_DEBUG
, "%s: Sending an updated ZLB in reponse\n",
1172 zlb
= new_outgoing (t
);
1173 control_zlb (zlb
, t
, c
);
1177 else if (!t
->control_rec_seq_num
&& (t
->tid
== -1))
1179 /* We made this tunnel just for this message, so let's
1188 t
->control_rec_seq_num
++;
1192 * So we know what the other end has received
1199 if (!CTBIT (h
->ver
))
1203 l2tp_log (LOG_DEBUG
, "%s: Control bit not set\n", __FUNCTION__
);
1207 if (!CLBIT (h
->ver
))
1211 l2tp_log (LOG_DEBUG
, "%s: Length bit not set\n", __FUNCTION__
);
1215 if (!CFBIT (h
->ver
))
1219 l2tp_log (LOG_DEBUG
, "%s: Flow bit not set\n", __FUNCTION__
);
1223 if (CVER (h
->ver
) != VER_L2TP
)
1227 if (CVER (h
->ver
) == VER_PPTP
)
1229 l2tp_log (LOG_DEBUG
,
1230 "%s: PPTP packet received\n", __FUNCTION__
);
1232 else if (CVER (h
->ver
) < VER_L2TP
)
1234 l2tp_log (LOG_DEBUG
,
1235 "%s: L2F packet received\n", __FUNCTION__
);
1239 l2tp_log (LOG_DEBUG
,
1240 "%s: Unknown version received\n", __FUNCTION__
);
1251 inline int check_payload (struct buffer
*buf
, struct tunnel
*t
,
1255 * Check if this is a valid payload
1256 * or not. Returns 0 on success.
1259 int ehlen
= MIN_PAYLOAD_HDR_LEN
;
1260 struct payload_hdr
*h
= (struct payload_hdr
*) (buf
->start
);
1265 l2tp_log (LOG_DEBUG
, "%s: Aempted to send payload on tunnel\n",
1270 if (buf
->len
< MIN_PAYLOAD_HDR_LEN
)
1272 /* has to be at least MIN_PAYLOAD_HDR_LEN
1273 no matter what. we'll look more later */
1276 l2tp_log (LOG_DEBUG
, "%s:Recieved to small of packet\n", __FUNCTION__
);
1287 l2tp_log (LOG_DEBUG
, "%s Control bit set\n", __FUNCTION__
);
1292 ehlen
+= 2; /* Should have length information */
1295 /* if (!c->fbit && !c->ourfbit) {
1297 l2tp_log(LOG_DEBUG,"%s: flow bit set, but no RWS negotiated.\n",__FUNCTION__);
1300 ehlen
+= 4; /* Should have Ns and Nr too */
1302 /* if (!PFBIT(h->ver)) {
1303 if (c->fbit || c->ourfbit) {
1305 l2tp_log(LOG_DEBUG, "%s: no flow bit, but RWS was negotiated.\n",__FUNCTION__);
1310 ehlen
+= 2; /* Offset information */
1312 ehlen
+= h
->length
; /* include length if available */
1313 if (PVER (h
->ver
) != VER_L2TP
)
1317 if (PVER (h
->ver
) == VER_PPTP
)
1319 l2tp_log (LOG_DEBUG
, "%s: PPTP packet received\n",
1322 else if (CVER (h
->ver
) < VER_L2TP
)
1324 l2tp_log (LOG_DEBUG
, "%s: L2F packet received\n",
1329 l2tp_log (LOG_DEBUG
, "%s: Unknown version received\n",
1335 if ((buf
->len
< ehlen
) && !PLBIT (h
->ver
))
1339 l2tp_log (LOG_DEBUG
, "%s payload too small (%d < %d)\n",
1340 __FUNCTION__
, buf
->len
, ehlen
);
1344 if ((buf
->len
!= h
->length
) && PLBIT (h
->ver
))
1348 l2tp_log (LOG_DEBUG
, "%s: size mismatch (%d != %d)\n",
1349 __FUNCTION__
, buf
->len
, h
->length
);
1357 inline int expand_payload (struct buffer
*buf
, struct tunnel
*t
,
1361 * Expands payload header. Does not check for valid header,
1362 * check_payload() should already be called as a prerequisite.
1364 struct payload_hdr
*h
= (struct payload_hdr
*) (buf
->start
);
1365 _u16
*r
= (_u16
*) h
; /* Nice to have raw word pointers */
1366 struct payload_hdr
*new_hdr
;
1369 * We first calculate our offset
1371 if (!PLBIT (h
->ver
))
1372 ehlen
+= 2; /* Should have length information */
1373 if (!PFBIT (h
->ver
))
1374 ehlen
+= 4; /* Should have Ns and Nr too */
1375 if (!PSBIT (h
->ver
))
1376 ehlen
+= 2; /* Offset information */
1380 * If this payload is missing any information, we'll
1383 new_hdr
= (struct payload_hdr
*) (buf
->start
- ehlen
);
1384 if ((void *) new_hdr
< (void *) buf
->rstart
)
1386 l2tp_log (LOG_WARNING
, "%s: not enough space to decompress frame\n",
1392 if (PLBIT (new_hdr
->ver
))
1395 new_hdr
->length
= *r
;
1399 new_hdr
->length
= buf
->len
+ ehlen
;
1405 if (PFBIT (new_hdr
->ver
))
1414 new_hdr
->Nr
= c
->data_seq_num
;
1415 new_hdr
->Ns
= c
->data_rec_seq_num
;
1417 if (PSBIT (new_hdr
->ver
))
1420 new_hdr
->o_size
= *r
;
1422 // new_hdr->o_pad = *r;
1426 new_hdr
->o_size
= 0;
1427 // new_hdr->o_pad = 0;
1433 * Handle sequence numbers
1436 /* JLM if (PRBIT(new_hdr->ver)) {
1437 if (c->pSr > new_hdr->Ns) {
1438 l2tp_log(LOG_DEBUG, "%s: R-bit set with Ns < pSr!\n",__FUNCTION__);
1442 l2tp_log(LOG_DEBUG, "%s: R-bit set on packet %d\n",__FUNCTION__,new_hdr->Ns);
1446 #ifdef DEBUG_PAYLOAD
1447 l2tp_log (LOG_DEBUG
, "%s: payload, cid = %d, Ns = %d, Nr = %d\n", __FUNCTION__
,
1448 c
->cid
, new_hdr
->Ns
, new_hdr
->Nr
);
1450 if (new_hdr
->Ns
!= c
->data_seq_num
)
1452 /* RFC1982-esque comparison of serial numbers */
1453 if (((new_hdr
->Ns
< c
->data_rec_seq_num
) &&
1454 ((c
->data_rec_seq_num
- new_hdr
->Ns
) < 32768)) ||
1455 ((new_hdr
->Ns
> c
->data_rec_seq_num
) &&
1456 ((c
->data_rec_seq_num
- new_hdr
->Ns
) > 32768)))
1460 l2tp_log (LOG_DEBUG
,
1461 "%s: Already seen this packet before (%d)\n",
1462 __FUNCTION__
, new_hdr
->Ns
);
1466 else if (new_hdr
->Ns
<= c
->data_rec_seq_num
+ PAYLOAD_FUDGE
)
1468 /* FIXME: I should buffer for out of order packets */
1471 l2tp_log (LOG_DEBUG
,
1472 "%s: Oops, lost a packet or two (%d). continuing...\n",
1473 __FUNCTION__
, new_hdr
->Ns
);
1475 c
->data_rec_seq_num
= new_hdr
->Ns
;
1481 l2tp_log (LOG_DEBUG
,
1482 "%s: Received out of order payload packet (%d)\n",
1483 __FUNCTION__
, new_hdr
->Ns
);
1490 c
->data_rec_seq_num
++;
1494 * Check to see what the last thing
1497 c
->pLr
= new_hdr
->Nr
;
1498 buf
->start
= new_hdr
;
1503 void send_zlb (void *data
)
1506 * Send a ZLB. This procedure should be schedule()able
1511 c
= (struct call
*) data
;
1514 l2tp_log (LOG_WARNING
, "%s: called on NULL call\n", __FUNCTION__
);
1520 l2tp_log (LOG_WARNING
, "%s: called on call with NULL container\n",
1524 /* Update the counter so we know what Lr was when we last transmited a ZLB */
1525 c
->prx
= c
->data_rec_seq_num
;
1526 buf
= new_payload (t
->peer
);
1527 add_payload_hdr (t
, c
, buf
);
1528 c
->data_seq_num
--; /* We don't increment on ZLB's */
1531 l2tp_log (LOG_DEBUG
, "%s: sending payload ZLB\n", __FUNCTION__
);
1537 inline int write_packet (struct buffer
*buf
, struct tunnel
*t
, struct call
*c
,
1541 * Write a packet, doing sync->async conversion if
1547 static unsigned char wbuf
[MAX_RECV_SIZE
];
1553 l2tp_log (LOG_DEBUG
, "%s: tty is not open yet.\n", __FUNCTION__
);
1559 _u16 offset
= ((struct payload_hdr
*)(buf
->start
))->o_size
; // For FIXME:
1560 buf
->start
+= sizeof(struct payload_hdr
) + offset
;
1561 buf
->len
-= sizeof(struct payload_hdr
) + offset
;
1564 c
->rx_bytes
+= buf
->len
;
1567 * FIXME: What about offset?
1571 /* We are given async frames, so write them
1572 directly to the tty */
1573 err
= write (c
->fd
, buf
->start
, buf
->len
);
1574 if (err
== buf
->len
)
1580 l2tp_log (LOG_WARNING
, "%s: wrote no bytes of async packet\n",
1586 if ((errno
== EAGAIN
) || (errno
== EINTR
))
1592 l2tp_log (LOG_WARNING
, "%s: async write failed: %s\n", __FUNCTION__
,
1596 else if (err
< buf
->len
)
1598 l2tp_log (LOG_WARNING
, "%s: short write (%d of %d bytes)\n", __FUNCTION__
,
1602 else if (err
> buf
->len
)
1604 l2tp_log (LOG_WARNING
, "%s: write returned LONGER than buffer length?\n",
1611 * sync->async conversion if we're doing sync frames
1612 * since the pppd driver will expect async frames
1613 * Write leading flag character
1619 for (x
= 0; x
< buf
->len
; x
++)
1621 // we must at least still have 3 bytes left in the worst case scenario:
1622 // 1 for a possible escape, 1 for the value and 1 to end the PPP stream.
1623 if(pos
>= (sizeof(wbuf
) - 4)) {
1625 l2tp_log(LOG_CRIT
, "%s: rx packet is too big after PPP encoding (size %u, max is %u)\n",
1626 __FUNCTION__
, buf
->len
, MAX_RECV_SIZE
);
1629 e
= *((char *) buf
->start
+ x
);
1630 if ((e
< 0x20) || (e
== PPP_ESCAPE
) || (e
== PPP_FLAG
))
1634 wbuf
[pos
++] = PPP_ESCAPE
;
1639 wbuf
[pos
++] = PPP_FLAG
;
1643 l2tp_log(LOG_DEBUG
, "after sync->async, expanded %d->%d\n",
1651 err
= write (c
->fd
, wbuf
+x
, pos
-x
);
1653 if ( errno
!= EINTR
&& errno
!= EAGAIN
) {
1654 l2tp_log (LOG_WARNING
, "%s: %s(%d)\n", __FUNCTION__
, strerror (errno
),
1657 * I guess pppd died. we'll pretend
1658 * everything ended normally
1665 continue; //goto while
1673 void handle_special (struct buffer
*buf
, struct call
*c
, _u16 call
)
1676 * This procedure is called when we have received a packet
1677 * on a call which doesn't exist in our tunnel. We want to
1678 * send back a ZLB to keep the tunnel alive, on that particular
1679 * call if it was a CDN, otherwise, send a CDN to notify them
1680 * that this call has been terminated.
1682 struct buffer
*outgoing
;
1683 struct tunnel
*t
= c
->container
;
1684 /* Don't do anything unless it's a control packet */
1685 if (!CTBIT (*((_u16
*) buf
->start
)))
1687 /* Temporarily, we make the tunnel have cid of call instead of 0,
1688 but we need to stop any scheduled events (like Hello's in
1689 particular) which might use this value */
1691 if (!check_control (buf
, t
, c
))
1693 if (buf
->len
== sizeof (struct control_hdr
))
1695 /* If it's a ZLB, we ignore it */
1696 if (gconfig
.debug_tunnel
)
1697 l2tp_log (LOG_DEBUG
, "%s: ZLB for closed call\n", __FUNCTION__
);
1701 /* Make a packet with the specified call number */
1702 outgoing
= new_outgoing (t
);
1703 /* FIXME: If I'm not a CDN, I need to send a CDN */
1704 control_zlb (buf
, t
, c
);
1712 if (gconfig
.debug_tunnel
)
1713 l2tp_log (LOG_DEBUG
, "%s: invalid control packet\n", __FUNCTION__
);
1717 inline int handle_packet (struct buffer
*buf
, struct tunnel
*t
,
1721 /* tv code is commented out below
1726 if (CTBIT (*((_u16
*) buf
->start
)))
1728 /* We have a control packet */
1729 if (!check_control (buf
, t
, c
))
1732 if (buf
->len
== sizeof (struct control_hdr
))
1735 l2tp_log (LOG_DEBUG
, "%s: control ZLB received\n", __FUNCTION__
);
1737 t
->control_rec_seq_num
--;
1739 if (c
->needclose
&& c
->closing
)
1741 if (c
->container
->cLr
>= c
->closeSs
)
1744 l2tp_log (LOG_DEBUG
, "%s: ZLB for closing message found\n",
1748 /* Trigger final closing of call */
1753 else if (!handle_avps (buf
, t
, c
))
1755 return control_finish (t
, c
);
1759 if (gconfig
.debug_tunnel
)
1760 l2tp_log (LOG_DEBUG
, "%s: bad AVP handling!\n", __FUNCTION__
);
1766 l2tp_log (LOG_DEBUG
, "%s: bad control packet!\n", __FUNCTION__
);
1772 if (!check_payload (buf
, t
, c
))
1774 if (!expand_payload (buf
, t
, c
))
1776 if (buf
->len
> sizeof (struct payload_hdr
))
1778 /* if (c->throttle) {
1779 if (c->pSs > c->pLr + c->rws) {
1781 l2tp_log(LOG_DEBUG, "%s: not yet dethrottling call\n",__FUNCTION__);
1785 l2tp_log(LOG_DEBUG, "%s: dethrottling call\n",__FUNCTION__);
1787 if (c->dethrottle) deschedule(c->dethrottle);
1792 /* JLM res = write_packet(buf,t,c, c->frame & SYNC_FRAMING); */
1793 res
= write_packet (buf
, t
, c
, SYNC_FRAMING
);
1797 * Assuming we wrote to the ppp driver okay, we should
1798 * do something about ZLB's unless *we* requested no
1799 * window size or if they we have turned off our fbit.
1802 /* if (c->ourfbit && (c->ourrws > 0)) {
1803 if (c->pSr >= c->prx + c->ourrws - 2) {
1804 We've received enough to fill our receive window. At
1805 this point, we should immediately send a ZLB!
1807 l2tp_log(LOG_DEBUG, "%s: Sending immediate ZLB!\n",__FUNCTION__);
1810 Deschedule any existing zlb_xmit's
1811 deschedule(c->zlb_xmit);
1814 send_zlb((void *)c);
1817 We need to schedule sending a ZLB. FIXME: Should
1818 be 1/4 RTT instead, when rate adaptive stuff is
1819 in place. Spec allows .5 seconds though
1821 tv.tv_usec = 500000;
1823 deschedule(c->zlb_xmit);
1825 l2tp_log(LOG_DEBUG, "%s: scheduling ZLB\n",__FUNCTION__);
1827 c->zlb_xmit = schedule(tv, &send_zlb, (void *)c);
1832 else if (buf
->len
== sizeof (struct payload_hdr
))
1835 l2tp_log (LOG_DEBUG
, "%s: payload ZLB received\n",
1838 /* if (c->throttle) {
1839 if (c->pSs > c->pLr + c->rws) {
1841 l2tp_log(LOG_DEBUG, "%s: not yet dethrottling call\n",__FUNCTION__);
1845 l2tp_log(LOG_DEBUG, "%s: dethrottling call\n",__FUNCTION__);
1848 deschedule(c->dethrottle);
1853 c
->data_rec_seq_num
--;
1858 l2tp_log (LOG_DEBUG
, "%s: payload too small!\n", __FUNCTION__
);
1864 if (gconfig
.debug_tunnel
)
1865 l2tp_log (LOG_DEBUG
, "%s: unable to expand payload!\n",
1872 l2tp_log (LOG_DEBUG
, "%s: invalid payload packet!\n", __FUNCTION__
);