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__
);
602 /* Schedule a HELLO */
603 tv
.tv_sec
= HELLO_DELAY
;
606 l2tp_log (LOG_DEBUG
, "%s: scheduling initial HELLO on %d\n", __FUNCTION__
,
609 t
->hello
= schedule (tv
, hello
, (void *) t
);
610 l2tp_log (LOG_NOTICE
,
611 "Connection established to %s, %d. Local: %d, Remote: %d (ref=%u/%u).\n",
612 IPADDY (t
->peer
.sin_addr
),
613 ntohs (t
->peer
.sin_port
), t
->ourtid
, t
->tid
, t
->refme
, t
->refhim
);
617 /* This is part of a LAC, so we want to go ahead
618 and start an ICRQ now */
619 magic_lac_dial (t
->lac
);
623 if (t
->chal_them
.state
)
626 (t
->chal_them
.reply
, t
->chal_them
.response
, MD_SIG_SIZE
))
628 set_error (c
, VENDOR_ERROR
,
629 "Invalid challenge authentication");
630 l2tp_log (LOG_DEBUG
, "%s: Invalid authentication for host '%s'\n",
631 __FUNCTION__
, t
->hostname
);
637 l2tp_log (LOG_NOTICE
,
638 "Connection established to %s, %d. Local: %d, Remote: %d (ref=%u/%u). LNS session is '%s'\n",
639 IPADDY (t
->peer
.sin_addr
),
640 ntohs (t
->peer
.sin_port
), t
->ourtid
, t
->tid
, t
->refme
, t
->refhim
,
645 /* Schedule a HELLO */
646 tv
.tv_sec
= HELLO_DELAY
;
649 l2tp_log (LOG_DEBUG
, "%s: scheduling initial HELLO on %d\n", __FUNCTION__
,
652 t
->hello
= schedule (tv
, hello
, (void *) t
);
659 "%s: Peer tried to disconnect without specifying tunnel ID\n",
664 /* Work around bug in MSL2TP client */
665 if ((t
->firmware
== 0xff00) && (!(strncmp(t
->vendor
, "Deterministic Networks Inc.", 27))))
669 if ((t
->qtid
!= tmptid
) && (tmptid
> 0))
673 "%s: Peer [Vendor:%s] [Firmware:%d (0x%.4x)] tried to disconnect with invalid TID (%d != %d)\n",
674 __FUNCTION__
, t
->vendor
, t
->firmware
, t
->firmware
, t
->qtid
, tmptid
);
677 /* In case they're disconnecting immediately after SCCN */
680 if (t
->self
->result
< 0)
684 "%s: Peer tried to disconnect without specifying result code.\n",
689 "%s: Connection closed to %s, port %d (%s), Local: %d, Remote: %d\n",
690 __FUNCTION__
, IPADDY (t
->peer
.sin_addr
),
691 ntohs (t
->peer
.sin_port
), t
->self
->errormsg
, t
->ourtid
, t
->tid
);
699 set_error (p
, ERROR_INVALID
, "This tunnel cannot accept calls\n");
703 p
->lbit
= p
->lns
->lbit
? LBIT
: 0;
704 /* p->ourrws = p->lns->call_rws;
705 if (p->ourrws > -1) p->ourfbit = FBIT; else p->ourfbit = 0; */
710 "%s: Peer tried to initiate call without call ID\n",
712 /* Here it doesn't make sense to use the needclose flag because
713 the call p did not receive any packets */
720 if (z
->cid
== p
->cid
)
722 /* This can happen if we get a duplicate
723 ICRQ or if they don't get our ack packet */
725 "%s: Peer requested call %d twice, ignoring second one.\n",
726 __FUNCTION__
, p
->cid
);
734 /* FIXME: by commenting this out, we're not checking whether the serial
735 * number avp is included in the ICRQ at all which its required to be.
736 * Since the serial number is only used for human debugging aid, this
737 * isn't a big deal, but it would be nice to have *some* sort of check
738 * for it and perhaps just log it and go on. */
739 /* JLM if (p->serno<1) {
740 if (DEBUG) log(LOG_DEBUG,
741 "%s: Peer did not specify serial number when initiating call\n", __FUNCTION__);
747 if (t
->lns
->assign_ip
) {
748 p
->addr
= get_addr (t
->lns
->range
);
751 set_error (p
, ERROR_NORES
, "No available IP address");
753 l2tp_log (LOG_DEBUG
, "%s: Out of IP addresses on tunnel %d!\n",
754 __FUNCTION__
, t
->tid
);
757 reserve_addr (p
->addr
);
764 buf
= new_outgoing (t
);
765 add_message_type_avp (buf
, ICRP
);
768 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
769 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
772 add_callid_avp (buf
, p
->ourcid
, t
);
774 add_callid_avp (buf
, p
->ourcid
);
776 /* if (p->ourrws >=0)
777 add_avp_rws(buf, p->ourrws); */
779 * FIXME: I should really calculate
780 * Packet Processing Delay
782 /* add_ppd_avp(buf,ppd); */
783 add_control_hdr (t
, p
, buf
);
784 if (gconfig
.packet_dump
)
785 do_packet_dump (buf
);
787 if (gconfig
.debug_state
)
788 l2tp_log (LOG_DEBUG
, "%s: Sending ICRP\n", __FUNCTION__
);
796 "%s: Peer tried to negotiate ICRP without specifying call ID\n",
802 if (t
->fc
& ASYNC_FRAMING
)
803 c
->frame
= ASYNC_FRAMING
;
805 c
->frame
= SYNC_FRAMING
;
807 buf
= new_outgoing (t
);
808 add_message_type_avp (buf
, ICCN
);
811 mk_challenge (t
->chal_them
.vector
, VECTOR_SIZE
);
812 add_randvect_avp (buf
, t
->chal_them
.vector
, VECTOR_SIZE
);
814 add_txspeed_avp (buf
, t
->txspeed
);
815 add_frame_avp (buf
, c
->frame
);
816 /* if (c->ourrws >= 0)
817 add_avp_rws(buf, c->ourrws); */
818 /* FIXME: Packet Processing Delay */
819 /* We don't need any kind of proxy PPP stuff */
820 /* Can we proxy authenticate ourselves??? */
821 add_rxspeed_avp (buf
, t
->rxspeed
);
822 /* add_seqreqd_avp (buf); *//* We don't have sequencing code, so
823 * don't ask for sequencing */
824 add_control_hdr (t
, c
, buf
);
825 if (gconfig
.packet_dump
)
826 do_packet_dump (buf
);
828 if (gconfig
.debug_state
)
829 l2tp_log (LOG_DEBUG
, "%s: Sending ICCN\n", __FUNCTION__
);
830 l2tp_log (LOG_NOTICE
,
831 "Call established with %s, Local: %d, Remote: %d, Serial: %d (ref=%u/%u)\n",
832 IPADDY (t
->peer
.sin_addr
), c
->ourcid
, c
->cid
,
833 c
->serno
, t
->refme
, t
->refhim
);
836 po
= add_opt (po
, "passive");
837 po
= add_opt (po
, "nodetach");
840 if (c
->lac
->defaultroute
)
841 po
= add_opt (po
, "defaultroute");
842 strncpy (ip1
, IPADDY (c
->lac
->localaddr
), sizeof (ip1
));
843 strncpy (ip2
, IPADDY (c
->lac
->remoteaddr
), sizeof (ip2
));
845 po
= add_opt (po
, "%s:%s", c
->lac
->localaddr
? ip1
: "",
846 c
->lac
->remoteaddr
? ip2
: "");
848 if (c
->lac
->authself
)
850 if (c
->lac
->pap_refuse
)
851 po
= add_opt (po
, "refuse-pap");
852 if (c
->lac
->chap_refuse
)
853 po
= add_opt (po
, "refuse-chap");
857 po
= add_opt (po
, "refuse-pap");
858 po
= add_opt (po
, "refuse-chap");
860 if (c
->lac
->authpeer
)
862 po
= add_opt (po
, "auth");
863 if (c
->lac
->pap_require
)
864 po
= add_opt (po
, "require-pap");
865 if (c
->lac
->chap_require
)
866 po
= add_opt (po
, "require-chap");
868 if (c
->lac
->authname
[0])
870 po
= add_opt (po
, "name");
871 po
= add_opt (po
, c
->lac
->authname
);
874 po
= add_opt (po
, "debug");
875 if (c
->lac
->password
[0])
877 if (pipe (pppd_passwdfd
) == -1)
880 "%s: Unable to create password pipe for pppd\n", __FUNCTION__
);
883 if (-1 == write (pppd_passwdfd
[1], c
->lac
->password
, strlen (c
->lac
->password
)))
886 "%s: Unable to write password to pipe for pppd\n", __FUNCTION__
);
887 close (pppd_passwdfd
[1]);
890 close (pppd_passwdfd
[1]);
892 /* clear memory used for password, paranoid? */
893 for (i
= 0; i
< STRLEN
; i
++)
894 c
->lac
->password
[i
] = '\0';
896 po
= add_opt (po
, "plugin");
897 po
= add_opt (po
, "passwordfd.so");
898 po
= add_opt (po
, "passwordfd");
899 snprintf (passwdfd_buf
, 32, "%d", pppd_passwdfd
[0]);
900 po
= add_opt (po
, passwdfd_buf
);
902 if (c
->lac
->pppoptfile
[0])
904 po
= add_opt (po
, "file");
905 po
= add_opt (po
, c
->lac
->pppoptfile
);
908 po
= add_opt (po
, "ipparam");
909 po
= add_opt (po
, IPADDY (t
->peer
.sin_addr
));
914 if (c
->lac
->password
[0])
915 close(pppd_passwdfd
[0]);
921 "%s: Peer attempted ICCN on the actual tunnel, not the call",
928 "%s: Warning: Peer did not specify transmit speed\n", __FUNCTION__
);
929 /* don't refuse the connection over this
937 "%s: Warning: Peer did not specify framing type\n", __FUNCTION__
);
938 /* don't refuse the connection over this
944 strncpy (ip1
, IPADDY (c
->lns
->localaddr
), sizeof (ip1
));
945 strncpy (ip2
, IPADDY (c
->addr
), sizeof (ip2
));
947 po
= add_opt (po
, "passive");
948 po
= add_opt (po
, "nodetach");
949 po
= add_opt (po
, "%s:%s", c
->lns
->localaddr
? ip1
: "", ip2
);
950 if (c
->lns
->authself
)
952 if (c
->lns
->pap_refuse
)
953 po
= add_opt (po
, "refuse-pap");
954 if (c
->lns
->chap_refuse
)
955 po
= add_opt (po
, "refuse-chap");
959 po
= add_opt (po
, "refuse-pap");
960 po
= add_opt (po
, "refuse-chap");
962 if (c
->lns
->authpeer
)
964 po
= add_opt (po
, "auth");
965 if (c
->lns
->pap_require
)
966 po
= add_opt (po
, "require-pap");
967 if (c
->lns
->chap_require
)
968 po
= add_opt (po
, "require-chap");
969 if (c
->lns
->passwdauth
)
970 po
= add_opt (po
, "login");
972 if (c
->lns
->authname
[0])
974 po
= add_opt (po
, "name");
975 po
= add_opt (po
, c
->lns
->authname
);
978 po
= add_opt (po
, "debug");
979 if (c
->lns
->pppoptfile
[0])
981 po
= add_opt (po
, "file");
982 po
= add_opt (po
, c
->lns
->pppoptfile
);
984 po
= add_opt (po
, "ipparam");
985 po
= add_opt (po
, IPADDY (t
->peer
.sin_addr
));
988 l2tp_log (LOG_NOTICE
,
989 "Call established with %s, Local: %d, Remote: %d, Serial: %d\n",
990 IPADDY (t
->peer
.sin_addr
), c
->ourcid
, c
->cid
,
993 case OCRP
: /* jz: nothing to do for OCRP, waiting for OCCN */
995 case OCCN
: /* jz: get OCCN, so the only thing we must do is to start the pppd */
997 po
= add_opt (po
, "passive");
998 po
= add_opt (po
, "nodetach");
999 po
= add_opt (po
, "file");
1000 strcat (dummy_buf
, c
->dial_no
); /* jz: use /etc/ppp/dialnumber.options for pppd - kick it if you don't like */
1001 strcat (dummy_buf
, ".options");
1002 po
= add_opt (po
, dummy_buf
);
1005 if (c
->lac
->defaultroute
)
1006 po
= add_opt (po
, "defaultroute");
1007 strncpy (ip1
, IPADDY (c
->lac
->localaddr
), sizeof (ip1
));
1008 strncpy (ip2
, IPADDY (c
->lac
->remoteaddr
), sizeof (ip2
));
1009 po
= add_opt (po
, "%s:%s", c
->lac
->localaddr
? ip1
: "",
1010 c
->lac
->remoteaddr
? ip2
: "");
1011 if (c
->lac
->authself
)
1013 if (c
->lac
->pap_refuse
)
1014 po
= add_opt (po
, "refuse-pap");
1015 if (c
->lac
->chap_refuse
)
1016 po
= add_opt (po
, "refuse-chap");
1020 po
= add_opt (po
, "refuse-pap");
1021 po
= add_opt (po
, "refuse-chap");
1023 if (c
->lac
->authpeer
)
1025 po
= add_opt (po
, "auth");
1026 if (c
->lac
->pap_require
)
1027 po
= add_opt (po
, "require-pap");
1028 if (c
->lac
->chap_require
)
1029 po
= add_opt (po
, "require-chap");
1031 if (c
->lac
->authname
[0])
1033 po
= add_opt (po
, "name");
1034 po
= add_opt (po
, c
->lac
->authname
);
1037 po
= add_opt (po
, "debug");
1038 if (c
->lac
->pppoptfile
[0])
1040 po
= add_opt (po
, "file");
1041 po
= add_opt (po
, c
->lac
->pppoptfile
);
1044 po
= add_opt (po
, "ipparam");
1045 po
= add_opt (po
, IPADDY (t
->peer
.sin_addr
));
1048 /* jz: just show some information */
1050 "parameters: Local: %d , Remote: %d , Serial: %d , Pid: %d , Tunnelid: %d , Phoneid: %s\n",
1051 c
->ourcid
, c
->cid
, c
->serno
, c
->pppd
, t
->ourtid
, c
->dial_no
);
1063 l2tp_log (LOG_DEBUG
,
1064 "%s: Peer tried to disconnect without specifying call ID\n",
1071 while (p
&& (p
->cid
!= c
->qcid
))
1076 l2tp_log (LOG_DEBUG
,
1077 "%s: Unable to determine call to be disconnected.\n",
1086 /* Work around bug in MSL2TP client */
1087 if ((t
->firmware
== 0xff00) && (!(strncmp(t
->vendor
, "Deterministic Networks Inc.", 27))))
1092 if ((c
->qcid
!= tmpcid
) && tmpcid
> 0)
1095 l2tp_log (LOG_DEBUG
,
1096 "%s: Peer tried to disconnect with invalid CID (%d != %d)\n",
1097 __FUNCTION__
, c
->qcid
, c
->ourcid
);
1104 l2tp_log (LOG_DEBUG
,
1105 "%s: Peer tried to disconnect without specifying result code.\n",
1110 "%s: Connection closed to %s, serial %d (%s)\n", __FUNCTION__
,
1111 IPADDY (t
->peer
.sin_addr
), c
->serno
, c
->errormsg
);
1120 l2tp_log (LOG_DEBUG
,
1121 "%s: Don't know how to finish a message of type %d\n",
1122 __FUNCTION__
, c
->msgtype
);
1123 set_error (c
, VENDOR_ERROR
, "Unimplemented message %d\n", c
->msgtype
);
1128 inline int check_control (const struct buffer
*buf
, struct tunnel
*t
,
1132 * Check if this is a valid control
1133 * or not. Returns 0 on success
1135 struct control_hdr
*h
= (struct control_hdr
*) (buf
->start
);
1137 if (buf
->len
< sizeof (struct control_hdr
))
1141 l2tp_log (LOG_DEBUG
,
1142 "%s: Received too small of packet\n", __FUNCTION__
);
1147 if (buf
->len
!= h
->length
)
1151 l2tp_log (LOG_DEBUG
,
1152 "%s: Reported and actual sizes differ (%d != %d)\n",
1153 __FUNCTION__
, h
->length
, buf
->len
);
1158 * FIXME: H-bit handling goes here
1160 #ifdef DEBUG_CONTROL
1161 l2tp_log (LOG_DEBUG
, "%s: control, cid = %d, Ns = %d, Nr = %d\n", __FUNCTION__
,
1162 c
->cid
, h
->Ns
, h
->Nr
);
1164 if (h
->Ns
!= t
->control_rec_seq_num
)
1167 l2tp_log (LOG_DEBUG
,
1168 "%s: Received out of order control packet on tunnel %d (got %d, expected %d)\n",
1169 __FUNCTION__
, t
->tid
, h
->Ns
, t
->control_rec_seq_num
);
1170 if (((h
->Ns
< t
->control_rec_seq_num
) &&
1171 ((t
->control_rec_seq_num
- h
->Ns
) < 32768)) ||
1172 ((h
->Ns
> t
->control_rec_seq_num
) &&
1173 ((t
->control_rec_seq_num
- h
->Ns
) > 32768)))
1176 * Woopsies, they sent us a message we should have already received
1177 * so we should send them a ZLB so they know
1178 * for sure that we already have it.
1182 l2tp_log (LOG_DEBUG
, "%s: Sending an updated ZLB in reponse\n",
1185 zlb
= new_outgoing (t
);
1186 control_zlb (zlb
, t
, c
);
1190 else if (!t
->control_rec_seq_num
&& (t
->tid
== -1))
1192 /* We made this tunnel just for this message, so let's
1201 t
->control_rec_seq_num
++;
1205 * So we know what the other end has received
1212 if (!CTBIT (h
->ver
))
1216 l2tp_log (LOG_DEBUG
, "%s: Control bit not set\n", __FUNCTION__
);
1220 if (!CLBIT (h
->ver
))
1224 l2tp_log (LOG_DEBUG
, "%s: Length bit not set\n", __FUNCTION__
);
1228 if (!CFBIT (h
->ver
))
1232 l2tp_log (LOG_DEBUG
, "%s: Flow bit not set\n", __FUNCTION__
);
1236 if (CVER (h
->ver
) != VER_L2TP
)
1240 if (CVER (h
->ver
) == VER_PPTP
)
1242 l2tp_log (LOG_DEBUG
,
1243 "%s: PPTP packet received\n", __FUNCTION__
);
1245 else if (CVER (h
->ver
) < VER_L2TP
)
1247 l2tp_log (LOG_DEBUG
,
1248 "%s: L2F packet received\n", __FUNCTION__
);
1252 l2tp_log (LOG_DEBUG
,
1253 "%s: Unknown version received\n", __FUNCTION__
);
1264 inline int check_payload (struct buffer
*buf
, struct tunnel
*t
,
1268 * Check if this is a valid payload
1269 * or not. Returns 0 on success.
1272 int ehlen
= MIN_PAYLOAD_HDR_LEN
;
1273 struct payload_hdr
*h
= (struct payload_hdr
*) (buf
->start
);
1278 l2tp_log (LOG_DEBUG
, "%s: Aempted to send payload on tunnel\n",
1283 if (buf
->len
< MIN_PAYLOAD_HDR_LEN
)
1285 /* has to be at least MIN_PAYLOAD_HDR_LEN
1286 no matter what. we'll look more later */
1289 l2tp_log (LOG_DEBUG
, "%s:Recieved to small of packet\n", __FUNCTION__
);
1300 l2tp_log (LOG_DEBUG
, "%s Control bit set\n", __FUNCTION__
);
1305 ehlen
+= 2; /* Should have length information */
1308 /* if (!c->fbit && !c->ourfbit) {
1310 l2tp_log(LOG_DEBUG,"%s: flow bit set, but no RWS negotiated.\n",__FUNCTION__);
1313 ehlen
+= 4; /* Should have Ns and Nr too */
1315 /* if (!PFBIT(h->ver)) {
1316 if (c->fbit || c->ourfbit) {
1318 l2tp_log(LOG_DEBUG, "%s: no flow bit, but RWS was negotiated.\n",__FUNCTION__);
1323 ehlen
+= 2; /* Offset information */
1325 ehlen
+= h
->length
; /* include length if available */
1326 if (PVER (h
->ver
) != VER_L2TP
)
1330 if (PVER (h
->ver
) == VER_PPTP
)
1332 l2tp_log (LOG_DEBUG
, "%s: PPTP packet received\n",
1335 else if (CVER (h
->ver
) < VER_L2TP
)
1337 l2tp_log (LOG_DEBUG
, "%s: L2F packet received\n",
1342 l2tp_log (LOG_DEBUG
, "%s: Unknown version received\n",
1348 if ((buf
->len
< ehlen
) && !PLBIT (h
->ver
))
1352 l2tp_log (LOG_DEBUG
, "%s payload too small (%d < %d)\n",
1353 __FUNCTION__
, buf
->len
, ehlen
);
1357 if ((buf
->len
!= h
->length
) && PLBIT (h
->ver
))
1361 l2tp_log (LOG_DEBUG
, "%s: size mismatch (%d != %d)\n",
1362 __FUNCTION__
, buf
->len
, h
->length
);
1370 inline int expand_payload (struct buffer
*buf
, struct tunnel
*t
,
1374 * Expands payload header. Does not check for valid header,
1375 * check_payload() should already be called as a prerequisite.
1377 struct payload_hdr
*h
= (struct payload_hdr
*) (buf
->start
);
1378 _u16
*r
= (_u16
*) h
; /* Nice to have raw word pointers */
1379 struct payload_hdr
*new_hdr
;
1382 * We first calculate our offset
1384 if (!PLBIT (h
->ver
))
1385 ehlen
+= 2; /* Should have length information */
1386 if (!PFBIT (h
->ver
))
1387 ehlen
+= 4; /* Should have Ns and Nr too */
1388 if (!PSBIT (h
->ver
))
1389 ehlen
+= 2; /* Offset information */
1393 * If this payload is missing any information, we'll
1396 new_hdr
= (struct payload_hdr
*) (buf
->start
- ehlen
);
1397 if ((void *) new_hdr
< (void *) buf
->rstart
)
1399 l2tp_log (LOG_WARNING
, "%s: not enough space to decompress frame\n",
1405 if (PLBIT (new_hdr
->ver
))
1408 new_hdr
->length
= *r
;
1412 new_hdr
->length
= buf
->len
+ ehlen
;
1418 if (PFBIT (new_hdr
->ver
))
1427 new_hdr
->Nr
= c
->data_seq_num
;
1428 new_hdr
->Ns
= c
->data_rec_seq_num
;
1430 if (PSBIT (new_hdr
->ver
))
1433 new_hdr
->o_size
= *r
;
1435 // new_hdr->o_pad = *r;
1439 new_hdr
->o_size
= 0;
1440 // new_hdr->o_pad = 0;
1446 * Handle sequence numbers
1449 /* JLM if (PRBIT(new_hdr->ver)) {
1450 if (c->pSr > new_hdr->Ns) {
1451 l2tp_log(LOG_DEBUG, "%s: R-bit set with Ns < pSr!\n",__FUNCTION__);
1455 l2tp_log(LOG_DEBUG, "%s: R-bit set on packet %d\n",__FUNCTION__,new_hdr->Ns);
1459 #ifdef DEBUG_PAYLOAD
1460 l2tp_log (LOG_DEBUG
, "%s: payload, cid = %d, Ns = %d, Nr = %d\n", __FUNCTION__
,
1461 c
->cid
, new_hdr
->Ns
, new_hdr
->Nr
);
1463 if (new_hdr
->Ns
!= c
->data_seq_num
)
1465 /* RFC1982-esque comparison of serial numbers */
1466 if (((new_hdr
->Ns
< c
->data_rec_seq_num
) &&
1467 ((c
->data_rec_seq_num
- new_hdr
->Ns
) < 32768)) ||
1468 ((new_hdr
->Ns
> c
->data_rec_seq_num
) &&
1469 ((c
->data_rec_seq_num
- new_hdr
->Ns
) > 32768)))
1473 l2tp_log (LOG_DEBUG
,
1474 "%s: Already seen this packet before (%d)\n",
1475 __FUNCTION__
, new_hdr
->Ns
);
1479 else if (new_hdr
->Ns
<= c
->data_rec_seq_num
+ PAYLOAD_FUDGE
)
1481 /* FIXME: I should buffer for out of order packets */
1484 l2tp_log (LOG_DEBUG
,
1485 "%s: Oops, lost a packet or two (%d). continuing...\n",
1486 __FUNCTION__
, new_hdr
->Ns
);
1488 c
->data_rec_seq_num
= new_hdr
->Ns
;
1494 l2tp_log (LOG_DEBUG
,
1495 "%s: Received out of order payload packet (%d)\n",
1496 __FUNCTION__
, new_hdr
->Ns
);
1503 c
->data_rec_seq_num
++;
1507 * Check to see what the last thing
1510 c
->pLr
= new_hdr
->Nr
;
1511 buf
->start
= new_hdr
;
1516 void send_zlb (void *data
)
1519 * Send a ZLB. This procedure should be schedule()able
1524 c
= (struct call
*) data
;
1527 l2tp_log (LOG_WARNING
, "%s: called on NULL call\n", __FUNCTION__
);
1533 l2tp_log (LOG_WARNING
, "%s: called on call with NULL container\n",
1537 /* Update the counter so we know what Lr was when we last transmited a ZLB */
1538 c
->prx
= c
->data_rec_seq_num
;
1539 buf
= new_payload (t
->peer
);
1540 add_payload_hdr (t
, c
, buf
);
1541 c
->data_seq_num
--; /* We don't increment on ZLB's */
1544 l2tp_log (LOG_DEBUG
, "%s: sending payload ZLB\n", __FUNCTION__
);
1550 inline int write_packet (struct buffer
*buf
, struct tunnel
*t
, struct call
*c
,
1554 * Write a packet, doing sync->async conversion if
1560 static unsigned char wbuf
[MAX_RECV_SIZE
];
1566 l2tp_log (LOG_DEBUG
, "%s: tty is not open yet.\n", __FUNCTION__
);
1572 _u16 offset
= ((struct payload_hdr
*)(buf
->start
))->o_size
; // For FIXME:
1573 buf
->start
+= sizeof(struct payload_hdr
) + offset
;
1574 buf
->len
-= sizeof(struct payload_hdr
) + offset
;
1577 c
->rx_bytes
+= buf
->len
;
1580 * FIXME: What about offset?
1584 /* We are given async frames, so write them
1585 directly to the tty */
1586 err
= write (c
->fd
, buf
->start
, buf
->len
);
1587 if (err
== buf
->len
)
1593 l2tp_log (LOG_WARNING
, "%s: wrote no bytes of async packet\n",
1599 if ((errno
== EAGAIN
) || (errno
== EINTR
))
1605 l2tp_log (LOG_WARNING
, "%s: async write failed: %s\n", __FUNCTION__
,
1609 else if (err
< buf
->len
)
1611 l2tp_log (LOG_WARNING
, "%s: short write (%d of %d bytes)\n", __FUNCTION__
,
1615 else if (err
> buf
->len
)
1617 l2tp_log (LOG_WARNING
, "%s: write returned LONGER than buffer length?\n",
1624 * sync->async conversion if we're doing sync frames
1625 * since the pppd driver will expect async frames
1626 * Write leading flag character
1632 for (x
= 0; x
< buf
->len
; x
++)
1634 // we must at least still have 3 bytes left in the worst case scenario:
1635 // 1 for a possible escape, 1 for the value and 1 to end the PPP stream.
1636 if(pos
>= (sizeof(wbuf
) - 4)) {
1638 l2tp_log(LOG_CRIT
, "%s: rx packet is too big after PPP encoding (size %u, max is %u)\n",
1639 __FUNCTION__
, buf
->len
, MAX_RECV_SIZE
);
1642 e
= *((char *) buf
->start
+ x
);
1643 if ((e
< 0x20) || (e
== PPP_ESCAPE
) || (e
== PPP_FLAG
))
1647 wbuf
[pos
++] = PPP_ESCAPE
;
1652 wbuf
[pos
++] = PPP_FLAG
;
1656 l2tp_log(LOG_DEBUG
, "after sync->async, expanded %d->%d\n",
1664 err
= write (c
->fd
, wbuf
+x
, pos
-x
);
1666 if ( errno
!= EINTR
&& errno
!= EAGAIN
) {
1667 l2tp_log (LOG_WARNING
, "%s: %s(%d)\n", __FUNCTION__
, strerror (errno
),
1670 * I guess pppd died. we'll pretend
1671 * everything ended normally
1678 continue; //goto while
1686 void handle_special (struct buffer
*buf
, struct call
*c
, _u16 call
)
1689 * This procedure is called when we have received a packet
1690 * on a call which doesn't exist in our tunnel. We want to
1691 * send back a ZLB to keep the tunnel alive, on that particular
1692 * call if it was a CDN, otherwise, send a CDN to notify them
1693 * that this call has been terminated.
1695 struct tunnel
*t
= c
->container
;
1696 /* Don't do anything unless it's a control packet */
1697 if (!CTBIT (*((_u16
*) buf
->start
)))
1699 /* Temporarily, we make the tunnel have cid of call instead of 0,
1700 but we need to stop any scheduled events (like Hello's in
1701 particular) which might use this value */
1703 if (!check_control (buf
, t
, c
))
1705 if (buf
->len
== sizeof (struct control_hdr
))
1707 /* If it's a ZLB, we ignore it */
1708 if (gconfig
.debug_tunnel
)
1709 l2tp_log (LOG_DEBUG
, "%s: ZLB for closed call\n", __FUNCTION__
);
1713 /* Make a packet with the specified call number */
1714 /* FIXME: If I'm not a CDN, I need to send a CDN */
1715 control_zlb (buf
, t
, c
);
1723 if (gconfig
.debug_tunnel
)
1724 l2tp_log (LOG_DEBUG
, "%s: invalid control packet\n", __FUNCTION__
);
1728 inline int handle_packet (struct buffer
*buf
, struct tunnel
*t
,
1732 /* tv code is commented out below
1737 if (CTBIT (*((_u16
*) buf
->start
)))
1739 /* We have a control packet */
1740 if (!check_control (buf
, t
, c
))
1743 if (buf
->len
== sizeof (struct control_hdr
))
1746 l2tp_log (LOG_DEBUG
, "%s: control ZLB received\n", __FUNCTION__
);
1748 t
->control_rec_seq_num
--;
1750 if (c
->needclose
&& c
->closing
)
1752 if (c
->container
->cLr
>= c
->closeSs
)
1755 l2tp_log (LOG_DEBUG
, "%s: ZLB for closing message found\n",
1759 /* Trigger final closing of call */
1764 else if (!handle_avps (buf
, t
, c
))
1766 return control_finish (t
, c
);
1770 if (gconfig
.debug_tunnel
)
1771 l2tp_log (LOG_DEBUG
, "%s: bad AVP handling!\n", __FUNCTION__
);
1777 l2tp_log (LOG_DEBUG
, "%s: bad control packet!\n", __FUNCTION__
);
1783 if (!check_payload (buf
, t
, c
))
1785 if (!expand_payload (buf
, t
, c
))
1787 if (buf
->len
> sizeof (struct payload_hdr
))
1789 /* if (c->throttle) {
1790 if (c->pSs > c->pLr + c->rws) {
1792 l2tp_log(LOG_DEBUG, "%s: not yet dethrottling call\n",__FUNCTION__);
1796 l2tp_log(LOG_DEBUG, "%s: dethrottling call\n",__FUNCTION__);
1798 if (c->dethrottle) deschedule(c->dethrottle);
1803 /* JLM res = write_packet(buf,t,c, c->frame & SYNC_FRAMING); */
1804 res
= write_packet (buf
, t
, c
, SYNC_FRAMING
);
1808 * Assuming we wrote to the ppp driver okay, we should
1809 * do something about ZLB's unless *we* requested no
1810 * window size or if they we have turned off our fbit.
1813 /* if (c->ourfbit && (c->ourrws > 0)) {
1814 if (c->pSr >= c->prx + c->ourrws - 2) {
1815 We've received enough to fill our receive window. At
1816 this point, we should immediately send a ZLB!
1818 l2tp_log(LOG_DEBUG, "%s: Sending immediate ZLB!\n",__FUNCTION__);
1821 Deschedule any existing zlb_xmit's
1822 deschedule(c->zlb_xmit);
1825 send_zlb((void *)c);
1828 We need to schedule sending a ZLB. FIXME: Should
1829 be 1/4 RTT instead, when rate adaptive stuff is
1830 in place. Spec allows .5 seconds though
1832 tv.tv_usec = 500000;
1834 deschedule(c->zlb_xmit);
1836 l2tp_log(LOG_DEBUG, "%s: scheduling ZLB\n",__FUNCTION__);
1838 c->zlb_xmit = schedule(tv, &send_zlb, (void *)c);
1843 else if (buf
->len
== sizeof (struct payload_hdr
))
1846 l2tp_log (LOG_DEBUG
, "%s: payload ZLB received\n",
1849 /* if (c->throttle) {
1850 if (c->pSs > c->pLr + c->rws) {
1852 l2tp_log(LOG_DEBUG, "%s: not yet dethrottling call\n",__FUNCTION__);
1856 l2tp_log(LOG_DEBUG, "%s: dethrottling call\n",__FUNCTION__);
1859 deschedule(c->dethrottle);
1864 c
->data_rec_seq_num
--;
1869 l2tp_log (LOG_DEBUG
, "%s: payload too small!\n", __FUNCTION__
);
1875 if (gconfig
.debug_tunnel
)
1876 l2tp_log (LOG_DEBUG
, "%s: unable to expand payload!\n",
1883 l2tp_log (LOG_DEBUG
, "%s: invalid payload packet!\n", __FUNCTION__
);