2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
34 #include <netinet/in.h>
45 #include "descriptor.h"
49 #include "throughput.h"
65 cbcp_Init(struct cbcp *cbcp, struct physical *p)
68 cbcp->fsm.state = CBCP_CLOSED;
71 *cbcp->fsm.phone = '\0';
72 memset(&cbcp->fsm.timer, '\0', sizeof cbcp->fsm.timer);
76 static void cbcp_SendReq(struct cbcp *);
77 static void cbcp_SendResponse(struct cbcp *);
78 static void cbcp_SendAck(struct cbcp *);
83 struct cbcp *cbcp = (struct cbcp *)v;
85 timer_Stop(&cbcp->fsm.timer);
86 if (cbcp->fsm.restart) {
87 switch (cbcp->fsm.state) {
90 log_Printf(LogCBCP, "%s: Urk - unexpected CBCP timeout !\n",
98 cbcp_SendResponse(cbcp);
107 switch (cbcp->fsm.state) {
118 missed = "Terminate REQ";
121 log_Printf(LogCBCP, "%s: Urk - unexpected CBCP timeout !\n",
127 log_Printf(LogCBCP, "%s: Timeout waiting for peer %s\n",
128 cbcp->p->dl->name, missed);
129 datalink_CBCPFailed(cbcp->p->dl);
134 cbcp_StartTimer(struct cbcp *cbcp, int timeout)
136 timer_Stop(&cbcp->fsm.timer);
137 cbcp->fsm.timer.func = cbcp_Timeout;
138 cbcp->fsm.timer.name = "cbcp";
139 cbcp->fsm.timer.load = timeout * SECTICKS;
140 cbcp->fsm.timer.arg = cbcp;
141 timer_Start(&cbcp->fsm.timer);
144 #define CBCP_CLOSED (0) /* Not in use */
145 #define CBCP_STOPPED (1) /* Waiting for a REQ */
146 #define CBCP_REQSENT (2) /* Waiting for a RESP */
147 #define CBCP_RESPSENT (3) /* Waiting for an ACK */
148 #define CBCP_ACKSENT (4) /* Waiting for an LCP Term REQ */
150 static const char * const cbcpname[] = {
151 "closed", "stopped", "req-sent", "resp-sent", "ack-sent"
155 cbcpstate(unsigned s)
157 if (s < sizeof cbcpname / sizeof cbcpname[0])
159 return HexStr(s, NULL, 0);
163 cbcp_NewPhase(struct cbcp *cbcp, int new)
165 if (cbcp->fsm.state != new) {
166 log_Printf(LogCBCP, "%s: State change %s --> %s\n", cbcp->p->dl->name,
167 cbcpstate(cbcp->fsm.state), cbcpstate(new));
168 cbcp->fsm.state = new;
175 u_int16_t length; /* Network byte order */
179 /* cbcp_header::code values */
181 #define CBCP_RESPONSE (2)
188 char addr_start[253]; /* max cbcp_data length 255 + 1 for NULL */
191 /* cbcp_data::type values */
192 #define CBCP_NONUM (1)
193 #define CBCP_CLIENTNUM (2)
194 #define CBCP_SERVERNUM (3)
195 #define CBCP_LISTNUM (4)
198 cbcp_Output(struct cbcp *cbcp, u_char code, struct cbcp_data *data)
200 struct cbcp_header *head;
203 bp = m_get(sizeof *head + data->length, MB_CBCPOUT);
204 head = (struct cbcp_header *)MBUF_CTOP(bp);
206 head->id = cbcp->fsm.id;
207 head->length = htons(sizeof *head + data->length);
208 memcpy(MBUF_CTOP(bp) + sizeof *head, data, data->length);
209 log_DumpBp(LogDEBUG, "cbcp_Output", bp);
210 link_PushPacket(&cbcp->p->link, bp, cbcp->p->dl->bundle,
211 LINK_QUEUES(&cbcp->p->link) - 1, PROTO_CBCP);
215 cbcp_data_Type(unsigned type)
217 static const char * const types[] = {
218 "No callback", "User-spec", "Server-spec", "list"
221 if (type < 1 || type > sizeof types / sizeof types[0])
222 return HexStr(type, NULL, 0);
223 return types[type-1];
228 char addr[sizeof ((struct cbcp_data *)0)->addr_start - 1]; /* ASCIIZ */
231 /* cbcp_data::type values */
232 #define CBCP_ADDR_PSTN (1)
235 cbcp_data_Show(struct cbcp_data *data)
237 struct cbcp_addr *addr;
240 addr = (struct cbcp_addr *)data->addr_start;
241 end = (char *)data + data->length;
244 log_Printf(LogCBCP, " TYPE %s\n", cbcp_data_Type(data->type));
245 if ((char *)&data->delay < end) {
246 log_Printf(LogCBCP, " DELAY %d\n", data->delay);
247 while (addr->addr < end) {
248 if (addr->type == CBCP_ADDR_PSTN)
249 log_Printf(LogCBCP, " ADDR %s\n", addr->addr);
251 log_Printf(LogCBCP, " ADDR type %d ??\n", (int)addr->type);
252 addr = (struct cbcp_addr *)(addr->addr + strlen(addr->addr) + 1);
258 cbcp_SendReq(struct cbcp *cbcp)
260 struct cbcp_data data;
261 struct cbcp_addr *addr;
262 char list[sizeof cbcp->fsm.phone], *next;
265 /* Only callees send REQs */
267 log_Printf(LogCBCP, "%s: SendReq(%d) state = %s\n", cbcp->p->dl->name,
268 cbcp->fsm.id, cbcpstate(cbcp->fsm.state));
269 data.type = cbcp->fsm.type;
271 strncpy(list, cbcp->fsm.phone, sizeof list - 1);
272 list[sizeof list - 1] = '\0';
276 addr = (struct cbcp_addr *)data.addr_start;
277 addr->type = CBCP_ADDR_PSTN;
279 data.length = addr->addr - (char *)&data;
283 addr = (struct cbcp_addr *)data.addr_start;
284 for (next = strtok(list, ","); next; next = strtok(NULL, ",")) {
286 max = data.addr_start + sizeof data.addr_start - addr->addr - 1;
288 addr->type = CBCP_ADDR_PSTN;
289 strncpy(addr->addr, next, sizeof addr->addr - 1);
290 addr->addr[sizeof addr->addr - 1] = '\0';
291 addr = (struct cbcp_addr *)((char *)addr + len + 2);
293 log_Printf(LogWARN, "CBCP ADDR \"%s\" skipped - packet too large\n",
296 data.length = (char *)addr - (char *)&data;
300 data.length = data.addr_start - (char *)&data;
304 data.length = (char *)&data.delay - (char *)&data;
308 cbcp_data_Show(&data);
309 cbcp_Output(cbcp, CBCP_REQ, &data);
311 cbcp_StartTimer(cbcp, cbcp->fsm.delay);
312 cbcp_NewPhase(cbcp, CBCP_REQSENT); /* Wait for a RESPONSE */
316 cbcp_Up(struct cbcp *cbcp)
318 struct lcp *lcp = &cbcp->p->link.lcp;
320 cbcp->fsm.delay = cbcp->p->dl->cfg.cbcp.delay;
321 if (*cbcp->p->dl->peer.authname == '\0' ||
322 !auth_SetPhoneList(cbcp->p->dl->peer.authname, cbcp->fsm.phone,
323 sizeof cbcp->fsm.phone)) {
324 strncpy(cbcp->fsm.phone, cbcp->p->dl->cfg.cbcp.phone,
325 sizeof cbcp->fsm.phone - 1);
326 cbcp->fsm.phone[sizeof cbcp->fsm.phone - 1] = '\0';
329 if (lcp->want_callback.opmask) {
330 if (*cbcp->fsm.phone == '\0')
331 cbcp->fsm.type = CBCP_NONUM;
332 else if (!strcmp(cbcp->fsm.phone, "*")) {
333 cbcp->fsm.type = CBCP_SERVERNUM;
334 *cbcp->fsm.phone = '\0';
336 cbcp->fsm.type = CBCP_CLIENTNUM;
337 cbcp_NewPhase(cbcp, CBCP_STOPPED); /* Wait for a REQ */
338 cbcp_StartTimer(cbcp, cbcp->fsm.delay * DEF_FSMTRIES);
340 if (*cbcp->fsm.phone == '\0')
341 cbcp->fsm.type = CBCP_NONUM;
342 else if (!strcmp(cbcp->fsm.phone, "*")) {
343 cbcp->fsm.type = CBCP_CLIENTNUM;
344 *cbcp->fsm.phone = '\0';
345 } else if (strchr(cbcp->fsm.phone, ','))
346 cbcp->fsm.type = CBCP_LISTNUM;
348 cbcp->fsm.type = CBCP_SERVERNUM;
349 cbcp->fsm.restart = DEF_FSMTRIES;
355 cbcp_AdjustResponse(struct cbcp *cbcp, struct cbcp_data *data)
358 * We've received a REQ (data). Adjust our response (cbcp->fsm.*)
359 * so that we (hopefully) agree with the peer
361 struct cbcp_addr *addr;
363 switch (data->type) {
365 if (cbcp->p->dl->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_NONE))
367 * if ``none'' is a configured callback possibility
368 * (ie, ``set callback cbcp none''), go along with the callees
371 cbcp->fsm.type = CBCP_NONUM;
374 * Otherwise, we send our desired response anyway. This seems to be
375 * what Win95 does - although I can't find this behaviour documented
376 * in the CBCP spec....
382 if (cbcp->fsm.type == CBCP_CLIENTNUM) {
385 if (data->length > data->addr_start - (char *)data) {
387 * The peer has given us an address type spec - make sure we
390 addr = (struct cbcp_addr *)data->addr_start;
391 if (addr->type != CBCP_ADDR_PSTN) {
392 log_Printf(LogPHASE, "CBCP: Unrecognised address type %d !\n",
397 /* we accept the REQ even if the peer didn't specify an addr->type */
398 ptr = strchr(cbcp->fsm.phone, ',');
400 *ptr = '\0'; /* Just use the first number in our list */
403 log_Printf(LogPHASE, "CBCP: no number to pass to the peer !\n");
407 if (cbcp->fsm.type == CBCP_SERVERNUM) {
408 *cbcp->fsm.phone = '\0';
411 if (data->length > data->addr_start - (char *)data) {
413 * This violates the spec, but if the peer has told us the
414 * number it wants to call back, take advantage of this fact
415 * and allow things to proceed if we've specified the same
418 addr = (struct cbcp_addr *)data->addr_start;
419 if (addr->type != CBCP_ADDR_PSTN) {
420 log_Printf(LogPHASE, "CBCP: Unrecognised address type %d !\n",
423 } else if (cbcp->fsm.type == CBCP_CLIENTNUM) {
425 * If the peer's insisting on deciding the number, make sure
426 * it's one of the ones in our list. If it is, let the peer
427 * think it's in control :-)
429 char list[sizeof cbcp->fsm.phone], *next;
431 strncpy(list, cbcp->fsm.phone, sizeof list - 1);
432 list[sizeof list - 1] = '\0';
433 for (next = strtok(list, ","); next; next = strtok(NULL, ","))
434 if (!strcmp(next, addr->addr)) {
435 cbcp->fsm.type = CBCP_SERVERNUM;
436 strcpy(cbcp->fsm.phone, next);
441 log_Printf(LogPHASE, "CBCP: Peer won't allow local decision !\n");
445 if (cbcp->fsm.type == CBCP_CLIENTNUM || cbcp->fsm.type == CBCP_LISTNUM) {
447 * Search through ``data''s addresses and see if cbcp->fsm.phone
448 * contains any of them
450 char list[sizeof cbcp->fsm.phone], *next, *end;
452 addr = (struct cbcp_addr *)data->addr_start;
453 end = (char *)data + data->length;
455 while (addr->addr < end) {
456 if (addr->type == CBCP_ADDR_PSTN) {
457 strncpy(list, cbcp->fsm.phone, sizeof list - 1);
458 list[sizeof list - 1] = '\0';
459 for (next = strtok(list, ","); next; next = strtok(NULL, ","))
460 if (!strcmp(next, addr->addr)) {
461 cbcp->fsm.type = CBCP_LISTNUM;
462 strcpy(cbcp->fsm.phone, next);
466 log_Printf(LogCBCP, "Warning: Unrecognised address type %d !\n",
468 addr = (struct cbcp_addr *)(addr->addr + strlen(addr->addr) + 1);
471 log_Printf(LogPHASE, "CBCP: no good number to pass to the peer !\n");
475 log_Printf(LogCBCP, "Unrecognised REQ type %d !\n", (int)data->type);
480 cbcp_SendResponse(struct cbcp *cbcp)
482 struct cbcp_data data;
483 struct cbcp_addr *addr;
485 /* Only callers send RESPONSEs */
487 log_Printf(LogCBCP, "%s: SendResponse(%d) state = %s\n", cbcp->p->dl->name,
488 cbcp->fsm.id, cbcpstate(cbcp->fsm.state));
490 data.type = cbcp->fsm.type;
491 data.delay = cbcp->fsm.delay;
492 addr = (struct cbcp_addr *)data.addr_start;
493 if (data.type == CBCP_NONUM)
494 data.length = (char *)&data.delay - (char *)&data;
495 else if (*cbcp->fsm.phone) {
496 addr->type = CBCP_ADDR_PSTN;
497 strncpy(addr->addr, cbcp->fsm.phone, sizeof addr->addr - 1);
498 addr->addr[sizeof addr->addr - 1] = '\0';
499 data.length = (addr->addr + strlen(addr->addr) + 1) - (char *)&data;
501 data.length = data.addr_start - (char *)&data;
503 cbcp_data_Show(&data);
504 cbcp_Output(cbcp, CBCP_RESPONSE, &data);
506 cbcp_StartTimer(cbcp, cbcp->fsm.delay);
507 cbcp_NewPhase(cbcp, CBCP_RESPSENT); /* Wait for an ACK */
510 /* What to do after checking an incoming response */
511 #define CBCP_ACTION_DOWN (0)
512 #define CBCP_ACTION_REQ (1)
513 #define CBCP_ACTION_ACK (2)
516 cbcp_CheckResponse(struct cbcp *cbcp, struct cbcp_data *data)
519 * We've received a RESPONSE (data). Check if it agrees with
520 * our REQ (cbcp->fsm)
522 struct cbcp_addr *addr;
524 addr = (struct cbcp_addr *)data->addr_start;
526 if (data->type == cbcp->fsm.type) {
527 switch (cbcp->fsm.type) {
529 return CBCP_ACTION_ACK;
532 if ((char *)data + data->length <= addr->addr)
533 log_Printf(LogPHASE, "CBCP: peer didn't respond with a number !\n");
534 else if (addr->type != CBCP_ADDR_PSTN)
535 log_Printf(LogPHASE, "CBCP: Unrecognised address type %d !\n",
538 strncpy(cbcp->fsm.phone, addr->addr, sizeof cbcp->fsm.phone - 1);
539 cbcp->fsm.phone[sizeof cbcp->fsm.phone - 1] = '\0';
540 cbcp->fsm.delay = data->delay;
541 return CBCP_ACTION_ACK;
543 return CBCP_ACTION_DOWN;
546 cbcp->fsm.delay = data->delay;
547 return CBCP_ACTION_ACK;
550 if ((char *)data + data->length <= addr->addr)
551 log_Printf(LogPHASE, "CBCP: peer didn't respond with a number !\n");
552 else if (addr->type != CBCP_ADDR_PSTN)
553 log_Printf(LogPHASE, "CBCP: Unrecognised address type %d !\n",
556 char list[sizeof cbcp->fsm.phone], *next;
558 strncpy(list, cbcp->fsm.phone, sizeof list - 1);
559 list[sizeof list - 1] = '\0';
560 for (next = strtok(list, ","); next; next = strtok(NULL, ","))
561 if (!strcmp(addr->addr, next)) {
562 strcpy(cbcp->fsm.phone, next);
563 cbcp->fsm.delay = data->delay;
564 return CBCP_ACTION_ACK;
566 log_Printf(LogPHASE, "CBCP: peer didn't respond with a "
569 return CBCP_ACTION_DOWN;
571 log_Printf(LogPHASE, "Internal CBCP error - agreed on %d !\n",
572 (int)cbcp->fsm.type);
573 return CBCP_ACTION_DOWN;
574 } else if (data->type == CBCP_NONUM && cbcp->fsm.type == CBCP_CLIENTNUM) {
576 * Client doesn't want CBCP after all....
577 * We only allow this when ``set cbcp *'' has been specified.
579 cbcp->fsm.type = CBCP_NONUM;
580 return CBCP_ACTION_ACK;
582 log_Printf(LogCBCP, "Invalid peer RESPONSE\n");
583 return CBCP_ACTION_REQ;
587 cbcp_SendAck(struct cbcp *cbcp)
589 struct cbcp_data data;
590 struct cbcp_addr *addr;
592 /* Only callees send ACKs */
594 log_Printf(LogCBCP, "%s: SendAck(%d) state = %s\n", cbcp->p->dl->name,
595 cbcp->fsm.id, cbcpstate(cbcp->fsm.state));
597 data.type = cbcp->fsm.type;
600 data.length = (char *)&data.delay - (char *)&data;
603 addr = (struct cbcp_addr *)data.addr_start;
604 addr->type = CBCP_ADDR_PSTN;
605 strncpy(addr->addr, cbcp->fsm.phone, sizeof addr->addr - 1);
606 addr->addr[sizeof addr->addr - 1] = '\0';
607 data.delay = cbcp->fsm.delay;
608 data.length = addr->addr + strlen(addr->addr) + 1 - (char *)&data;
611 data.delay = cbcp->fsm.delay;
612 data.length = data.addr_start - (char *)&data;
616 cbcp_data_Show(&data);
617 cbcp_Output(cbcp, CBCP_ACK, &data);
619 cbcp_StartTimer(cbcp, cbcp->fsm.delay);
620 cbcp_NewPhase(cbcp, CBCP_ACKSENT); /* Wait for an ACK */
624 cbcp_Input(struct bundle *bundle __unused, struct link *l, struct mbuf *bp)
626 struct physical *p = link2physical(l);
627 struct cbcp_header *head;
628 struct cbcp_data *data;
629 struct cbcp *cbcp = &p->dl->cbcp;
633 log_Printf(LogERROR, "cbcp_Input: Not a physical link - dropped\n");
640 if (len < sizeof(struct cbcp_header)) {
644 head = (struct cbcp_header *)MBUF_CTOP(bp);
645 if (ntohs(head->length) != len) {
646 log_Printf(LogWARN, "Corrupt CBCP packet (code %d, length %u not %zu)"
647 " - ignored\n", head->code, ntohs(head->length), len);
651 m_settype(bp, MB_CBCPIN);
653 /* XXX check the id */
655 bp->m_offset += sizeof(struct cbcp_header);
656 bp->m_len -= sizeof(struct cbcp_header);
657 data = (struct cbcp_data *)MBUF_CTOP(bp);
659 switch (head->code) {
661 log_Printf(LogCBCP, "%s: RecvReq(%d) state = %s\n",
662 p->dl->name, head->id, cbcpstate(cbcp->fsm.state));
663 cbcp_data_Show(data);
664 if (cbcp->fsm.state == CBCP_STOPPED || cbcp->fsm.state == CBCP_RESPSENT) {
665 timer_Stop(&cbcp->fsm.timer);
666 if (cbcp_AdjustResponse(cbcp, data)) {
667 cbcp->fsm.restart = DEF_FSMTRIES;
668 cbcp->fsm.id = head->id;
669 cbcp_SendResponse(cbcp);
671 datalink_CBCPFailed(cbcp->p->dl);
673 log_Printf(LogCBCP, "%s: unexpected REQ dropped\n", p->dl->name);
677 log_Printf(LogCBCP, "%s: RecvResponse(%d) state = %s\n",
678 p->dl->name, head->id, cbcpstate(cbcp->fsm.state));
679 cbcp_data_Show(data);
680 if (cbcp->fsm.id != head->id) {
681 log_Printf(LogCBCP, "Warning: Expected id was %d, not %d\n",
682 cbcp->fsm.id, head->id);
683 cbcp->fsm.id = head->id;
685 if (cbcp->fsm.state == CBCP_REQSENT || cbcp->fsm.state == CBCP_ACKSENT) {
686 timer_Stop(&cbcp->fsm.timer);
687 switch (cbcp_CheckResponse(cbcp, data)) {
688 case CBCP_ACTION_REQ:
692 case CBCP_ACTION_ACK:
693 cbcp->fsm.restart = DEF_FSMTRIES;
695 if (cbcp->fsm.type == CBCP_NONUM) {
697 * Don't change state in case the peer doesn't get our ACK,
698 * just bring the layer up.
700 timer_Stop(&cbcp->fsm.timer);
701 datalink_NCPUp(cbcp->p->dl);
706 datalink_CBCPFailed(cbcp->p->dl);
710 log_Printf(LogCBCP, "%s: unexpected RESPONSE dropped\n", p->dl->name);
714 log_Printf(LogCBCP, "%s: RecvAck(%d) state = %s\n",
715 p->dl->name, head->id, cbcpstate(cbcp->fsm.state));
716 cbcp_data_Show(data);
717 if (cbcp->fsm.id != head->id) {
718 log_Printf(LogCBCP, "Warning: Expected id was %d, not %d\n",
719 cbcp->fsm.id, head->id);
720 cbcp->fsm.id = head->id;
722 if (cbcp->fsm.type == CBCP_NONUM) {
724 * Don't change state in case the peer doesn't get our ACK,
725 * just bring the layer up.
727 timer_Stop(&cbcp->fsm.timer);
728 datalink_NCPUp(cbcp->p->dl);
729 } else if (cbcp->fsm.state == CBCP_RESPSENT) {
730 timer_Stop(&cbcp->fsm.timer);
731 datalink_CBCPComplete(cbcp->p->dl);
732 log_Printf(LogPHASE, "%s: CBCP: Peer will dial back\n", p->dl->name);
734 log_Printf(LogCBCP, "%s: unexpected ACK dropped\n", p->dl->name);
738 log_Printf(LogWARN, "Unrecognised CBCP packet (code %d, length %zd)\n",
748 cbcp_Down(struct cbcp *cbcp)
750 timer_Stop(&cbcp->fsm.timer);
751 cbcp_NewPhase(cbcp, CBCP_CLOSED);
756 cbcp_ReceiveTerminateReq(struct physical *p)
758 if (p->dl->cbcp.fsm.state == CBCP_ACKSENT) {
759 /* Don't change our state in case the peer doesn't get the ACK */
760 p->dl->cbcp.required = 1;
761 log_Printf(LogPHASE, "%s: CBCP: Will dial back on %s\n", p->dl->name,
762 p->dl->cbcp.fsm.phone);
764 cbcp_NewPhase(&p->dl->cbcp, CBCP_CLOSED);