2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <sys/socket.h>
49 #include "descriptor.h"
54 #include "throughput.h"
59 #include "slcompress.h"
81 static void datalink_LoginDone(struct datalink *);
82 static void datalink_NewState(struct datalink *, int);
85 datalink_OpenTimeout(void *v)
87 struct datalink *dl = (struct datalink *)v;
89 timer_Stop(&dl->dial.timer);
90 if (dl->state == DATALINK_OPENING)
91 log_Printf(LogCHAT, "%s: Redial timer expired.\n", dl->name);
95 datalink_StartDialTimer(struct datalink *dl, int Timeout)
99 timer_Stop(&dl->dial.timer);
101 result = (random() % DIAL_TIMEOUT) + 1;
102 dl->dial.timer.load = result ? result * SECTICKS : 1;
103 dl->dial.timer.func = datalink_OpenTimeout;
104 dl->dial.timer.name = "dial";
105 dl->dial.timer.arg = dl;
106 timer_Start(&dl->dial.timer);
107 if (dl->state == DATALINK_OPENING)
108 log_Printf(LogPHASE, "%s: Enter pause (%d) for redialing.\n",
114 datalink_HangupDone(struct datalink *dl)
116 if (dl->physical->type == PHYS_DEDICATED && !dl->bundle->CleaningUp &&
117 dl->physical->fd != -1) {
118 /* Don't close our device if the link is dedicated */
119 datalink_LoginDone(dl);
123 chat_Finish(&dl->chat);
124 physical_Close(dl->physical);
125 dl->phone.chosen = "N/A";
127 if (dl->cbcp.required) {
128 log_Printf(LogPHASE, "Call peer back on %s\n", dl->cbcp.fsm.phone);
129 dl->cfg.callback.opmask = 0;
130 strncpy(dl->cfg.phone.list, dl->cbcp.fsm.phone,
131 sizeof dl->cfg.phone.list - 1);
132 dl->cfg.phone.list[sizeof dl->cfg.phone.list - 1] = '\0';
133 dl->phone.alt = dl->phone.next = NULL;
134 dl->reconnect_tries = dl->cfg.reconnect.max;
135 dl->dial.tries = dl->cfg.dial.max;
138 dl->script.packetmode = 1;
139 if (!physical_SetMode(dl->physical, PHYS_BACKGROUND))
140 log_Printf(LogERROR, "Oops - can't change mode to BACKGROUND (gulp) !\n");
141 bundle_LinksRemoved(dl->bundle);
142 /* if dial.timeout is < 0 (random), we don't override fsm.delay */
143 if (dl->cbcp.fsm.delay < dl->cfg.dial.timeout)
144 dl->cbcp.fsm.delay = dl->cfg.dial.timeout;
145 datalink_StartDialTimer(dl, dl->cbcp.fsm.delay);
146 cbcp_Down(&dl->cbcp);
147 datalink_NewState(dl, DATALINK_OPENING);
148 if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
149 bundle_Phase(dl->bundle) == PHASE_TERMINATE)
150 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
151 } else if (dl->bundle->CleaningUp ||
152 (dl->physical->type == PHYS_DIRECT) ||
153 ((!dl->dial.tries || (dl->dial.tries < 0 && !dl->reconnect_tries)) &&
154 !(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)))) {
155 datalink_NewState(dl, DATALINK_CLOSED);
158 dl->reconnect_tries = 0;
159 bundle_LinkClosed(dl->bundle, dl);
160 if (!dl->bundle->CleaningUp &&
161 !(dl->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)))
162 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
164 datalink_NewState(dl, DATALINK_OPENING);
165 if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
166 bundle_Phase(dl->bundle) == PHASE_TERMINATE)
167 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
168 if (dl->dial.tries < 0) {
169 datalink_StartDialTimer(dl, dl->cfg.reconnect.timeout);
170 dl->dial.tries = dl->cfg.dial.max;
172 dl->reconnect_tries--;
173 log_Printf(LogCHAT, "%s: Reconnect try %d of %d\n",
174 dl->name, dl->cfg.reconnect.max - dl->reconnect_tries,
175 dl->cfg.reconnect.max);
176 bundle_Notify(dl->bundle, EX_RECONNECT);
178 if (dl->phone.next == NULL)
179 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
181 datalink_StartDialTimer(dl, dl->cfg.dial.next_timeout);
182 bundle_Notify(dl->bundle, EX_REDIAL);
188 datalink_ChoosePhoneNumber(struct datalink *dl)
192 if (dl->phone.alt == NULL) {
193 if (dl->phone.next == NULL) {
194 strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1);
195 dl->phone.list[sizeof dl->phone.list - 1] = '\0';
196 if (*dl->phone.list == '\0')
198 dl->phone.next = dl->phone.list;
200 dl->phone.alt = strsep(&dl->phone.next, ":");
202 phone = strsep(&dl->phone.alt, "|");
203 dl->phone.chosen = *phone ? phone : "[NONE]";
205 log_Printf(LogCHAT, "Phone: %s\n", phone);
210 datalink_LoginDone(struct datalink *dl)
212 chat_Finish(&dl->chat);
214 if (!dl->script.packetmode) {
217 datalink_NewState(dl, DATALINK_READY);
218 } else if (!physical_Raw(dl->physical)) {
220 log_Printf(LogWARN, "datalink_LoginDone: Not connected.\n");
221 if (dl->script.run) {
222 datalink_NewState(dl, DATALINK_LOGOUT);
223 if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL))
224 log_Printf(LogWARN, "Invalid logout script\n");
226 physical_StopDeviceTimer(dl->physical);
227 if (dl->physical->type == PHYS_DEDICATED)
228 /* force a redial timeout */
229 physical_Close(dl->physical);
230 datalink_HangupDone(dl);
236 hdlc_Init(&dl->physical->hdlc, &dl->physical->link.lcp);
237 async_Setup(&dl->physical->async);
239 lcp_Setup(&dl->physical->link.lcp, dl->state == DATALINK_READY ?
240 0 : dl->physical->link.lcp.cfg.openmode);
241 ccp_Setup(&dl->physical->link.ccp);
243 datalink_NewState(dl, DATALINK_LCP);
244 fsm_Up(&dl->physical->link.lcp.fsm);
245 fsm_Open(&dl->physical->link.lcp.fsm);
250 datalink_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
253 struct datalink *dl = descriptor2datalink(d);
258 case DATALINK_CLOSED:
259 if ((dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED|PHYS_BACKGROUND|
260 PHYS_FOREGROUND|PHYS_DDIAL)) &&
261 !dl->bundle->CleaningUp)
263 * Our first time in - DEDICATED & DDIAL never come down, and
264 * DIRECT, FOREGROUND & BACKGROUND get deleted when they enter
265 * DATALINK_CLOSED. Go to DATALINK_OPENING via datalink_Up()
268 datalink_Up(dl, 1, 1);
273 case DATALINK_OPENING:
274 if (dl->dial.timer.state != TIMER_RUNNING) {
275 if (--dl->dial.tries < 0)
277 if (physical_Open(dl->physical, dl->bundle) >= 0) {
278 log_WritePrompts(dl, "%s: Entering terminal mode on %s\r\n"
279 "Type `~?' for help\r\n", dl->name,
280 dl->physical->name.full);
281 if (dl->script.run) {
282 datalink_NewState(dl, DATALINK_DIAL);
283 if (!chat_Setup(&dl->chat, dl->cfg.script.dial,
284 *dl->cfg.script.dial ?
285 datalink_ChoosePhoneNumber(dl) : ""))
286 log_Printf(LogWARN, "Invalid dial script\n");
287 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
289 log_Printf(LogCHAT, "%s: Dial attempt %u of %d\n",
290 dl->name, dl->cfg.dial.max - dl->dial.tries,
293 datalink_NewState(dl, DATALINK_CARRIER);
294 return datalink_UpdateSet(d, r, w, e, n);
296 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
298 log_Printf(LogCHAT, "Failed to open device (attempt %u of %d)\n",
299 dl->cfg.dial.max - dl->dial.tries, dl->cfg.dial.max);
301 log_Printf(LogCHAT, "Failed to open device\n");
303 if (dl->bundle->CleaningUp ||
304 (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
305 dl->cfg.dial.max && dl->dial.tries == 0)) {
306 datalink_NewState(dl, DATALINK_CLOSED);
307 dl->reconnect_tries = 0;
309 log_WritePrompts(dl, "Failed to open %s\n",
310 dl->physical->name.full);
311 bundle_LinkClosed(dl->bundle, dl);
313 if (!dl->bundle->CleaningUp) {
316 timeout = datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
317 bundle_Notify(dl->bundle, EX_REDIAL);
318 log_WritePrompts(dl, "Failed to open %s, pause %d seconds\n",
319 dl->physical->name.full, timeout);
325 case DATALINK_CARRIER:
326 /* Wait for carrier on the device */
327 switch (physical_AwaitCarrier(dl->physical)) {
328 case CARRIER_PENDING:
329 log_Printf(LogDEBUG, "Waiting for carrier\n");
330 return 0; /* A device timer is running to wake us up again */
333 if (dl->script.run) {
334 datalink_NewState(dl, DATALINK_LOGIN);
335 if (!chat_Setup(&dl->chat, dl->cfg.script.login, NULL))
336 log_Printf(LogWARN, "Invalid login script\n");
338 datalink_LoginDone(dl);
339 return datalink_UpdateSet(d, r, w, e, n);
342 physical_Offline(dl->physical); /* Is this required ? */
343 if (dl->script.run) {
344 datalink_NewState(dl, DATALINK_HANGUP);
345 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
346 log_Printf(LogWARN, "Invalid hangup script\n");
347 return datalink_UpdateSet(d, r, w, e, n);
349 datalink_HangupDone(dl);
350 return 0; /* Maybe bundle_CleanDatalinks() has something to do */
354 case DATALINK_HANGUP:
356 case DATALINK_LOGOUT:
358 result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n);
359 switch (dl->chat.state) {
361 /* script succeeded */
363 case DATALINK_HANGUP:
364 datalink_HangupDone(dl);
367 datalink_NewState(dl, DATALINK_CARRIER);
368 return datalink_UpdateSet(d, r, w, e, n);
369 case DATALINK_LOGOUT:
370 datalink_NewState(dl, DATALINK_HANGUP);
371 physical_Offline(dl->physical);
372 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
373 log_Printf(LogWARN, "Invalid hangup script\n");
374 return datalink_UpdateSet(d, r, w, e, n);
376 dl->phone.alt = NULL;
377 datalink_LoginDone(dl);
378 return datalink_UpdateSet(d, r, w, e, n);
382 /* Going down - script failed */
383 log_Printf(LogWARN, "Chat script failed\n");
385 case DATALINK_HANGUP:
386 datalink_HangupDone(dl);
389 case DATALINK_LOGOUT:
391 datalink_NewState(dl, DATALINK_HANGUP);
392 physical_Offline(dl->physical);
393 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
394 log_Printf(LogWARN, "Invalid hangup script\n");
395 return datalink_UpdateSet(d, r, w, e, n);
406 result = descriptor_UpdateSet(&dl->chap.desc, r, w, e, n) +
407 descriptor_UpdateSet(&dl->physical->desc, r, w, e, n);
414 datalink_RemoveFromSet(struct datalink *dl, fd_set *r, fd_set *w, fd_set *e)
416 return physical_RemoveFromSet(dl->physical, r, w, e);
420 datalink_IsSet(struct fdescriptor *d, const fd_set *fdset)
422 struct datalink *dl = descriptor2datalink(d);
425 case DATALINK_CLOSED:
426 case DATALINK_OPENING:
429 case DATALINK_HANGUP:
431 case DATALINK_LOGOUT:
433 return descriptor_IsSet(&dl->chat.desc, fdset);
440 return descriptor_IsSet(&dl->chap.desc, fdset) ? 1 :
441 descriptor_IsSet(&dl->physical->desc, fdset);
447 datalink_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
449 struct datalink *dl = descriptor2datalink(d);
452 case DATALINK_CLOSED:
453 case DATALINK_OPENING:
456 case DATALINK_HANGUP:
458 case DATALINK_LOGOUT:
460 descriptor_Read(&dl->chat.desc, bundle, fdset);
468 if (descriptor_IsSet(&dl->chap.desc, fdset))
469 descriptor_Read(&dl->chap.desc, bundle, fdset);
470 if (descriptor_IsSet(&dl->physical->desc, fdset))
471 descriptor_Read(&dl->physical->desc, bundle, fdset);
477 datalink_Write(struct fdescriptor *d, struct bundle *bundle,
480 struct datalink *dl = descriptor2datalink(d);
484 case DATALINK_CLOSED:
485 case DATALINK_OPENING:
488 case DATALINK_HANGUP:
490 case DATALINK_LOGOUT:
492 if ((result = descriptor_Write(&dl->chat.desc, bundle, fdset)) == -1) {
493 datalink_ComeDown(dl, CLOSE_NORMAL);
503 if (descriptor_IsSet(&dl->chap.desc, fdset))
504 switch (descriptor_Write(&dl->chap.desc, bundle, fdset)) {
506 datalink_ComeDown(dl, CLOSE_NORMAL);
511 if (descriptor_IsSet(&dl->physical->desc, fdset))
512 switch (descriptor_Write(&dl->physical->desc, bundle, fdset)) {
514 datalink_ComeDown(dl, CLOSE_NORMAL);
526 datalink_ComeDown(struct datalink *dl, int how)
530 if (how == CLOSE_LCP)
531 datalink_DontHangup(dl);
532 else if (how == CLOSE_STAYDOWN)
533 datalink_StayDown(dl);
535 stayonline = dl->stayonline;
538 if (dl->state >= DATALINK_READY && stayonline) {
539 physical_StopDeviceTimer(dl->physical);
540 datalink_NewState(dl, DATALINK_READY);
541 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) {
542 physical_Offline(dl->physical);
543 if (dl->script.run && dl->state != DATALINK_OPENING) {
544 if (dl->state == DATALINK_LOGOUT) {
545 datalink_NewState(dl, DATALINK_HANGUP);
546 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
547 log_Printf(LogWARN, "Invalid hangup script\n");
549 datalink_NewState(dl, DATALINK_LOGOUT);
550 if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL))
551 log_Printf(LogWARN, "Invalid logout script\n");
554 datalink_HangupDone(dl);
559 datalink_LayerStart(void *v, struct fsm *fp)
561 /* The given FSM is about to start up ! */
562 struct datalink *dl = (struct datalink *)v;
564 if (fp->proto == PROTO_LCP)
565 (*dl->parent->LayerStart)(dl->parent->object, fp);
569 datalink_LayerUp(void *v, struct fsm *fp)
571 /* The given fsm is now up */
572 struct datalink *dl = (struct datalink *)v;
573 struct lcp *lcp = &dl->physical->link.lcp;
575 if (fp->proto == PROTO_LCP) {
576 datalink_GotAuthname(dl, "");
577 lcp->auth_ineed = lcp->want_auth;
578 lcp->auth_iwait = lcp->his_auth;
579 if (lcp->his_auth || lcp->want_auth) {
580 if (bundle_Phase(dl->bundle) != PHASE_NETWORK)
581 bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE);
582 log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name,
583 Auth2Nam(lcp->his_auth, lcp->his_authtype),
584 Auth2Nam(lcp->want_auth, lcp->want_authtype));
585 if (lcp->his_auth == PROTO_PAP)
586 auth_StartReq(&dl->pap);
587 if (lcp->want_auth == PROTO_CHAP)
588 auth_StartReq(&dl->chap.auth);
591 } else if (fp->proto == PROTO_CCP)
592 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.ccp.fsm);
596 datalink_AuthReInit(struct datalink *dl)
598 auth_StopTimer(&dl->pap);
599 auth_StopTimer(&dl->chap.auth);
600 chap_ReInit(&dl->chap);
604 datalink_GotAuthname(struct datalink *dl, const char *name)
606 strncpy(dl->peer.authname, name, sizeof dl->peer.authname - 1);
607 dl->peer.authname[sizeof dl->peer.authname - 1] = '\0';
611 datalink_NCPUp(struct datalink *dl)
613 int ccpok = ccp_SetOpenMode(&dl->physical->link.ccp);
615 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) {
616 /* we've authenticated in multilink mode ! */
617 switch (mp_Up(&dl->bundle->ncp.mp, dl)) {
619 /* We've handed the link off to another ppp (well, we will soon) ! */
622 /* First link in the bundle */
623 auth_Select(dl->bundle, dl->peer.authname);
624 bundle_CalculateBandwidth(dl->bundle);
627 /* We're in multilink mode ! */
628 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; /* override */
629 bundle_CalculateBandwidth(dl->bundle);
632 datalink_AuthNotOk(dl);
635 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) {
636 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name);
637 datalink_NewState(dl, DATALINK_OPEN);
638 bundle_CalculateBandwidth(dl->bundle);
639 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm);
642 dl->bundle->ncp.mp.peer = dl->peer;
643 ncp_SetLink(&dl->bundle->ncp, &dl->physical->link);
644 auth_Select(dl->bundle, dl->peer.authname);
648 fsm_Up(&dl->physical->link.ccp.fsm);
649 fsm_Open(&dl->physical->link.ccp.fsm);
651 datalink_NewState(dl, DATALINK_OPEN);
652 bundle_NewPhase(dl->bundle, PHASE_NETWORK);
653 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm);
657 datalink_CBCPComplete(struct datalink *dl)
659 datalink_NewState(dl, DATALINK_LCP);
660 datalink_AuthReInit(dl);
661 fsm_Close(&dl->physical->link.lcp.fsm);
665 datalink_CBCPFailed(struct datalink *dl)
667 cbcp_Down(&dl->cbcp);
668 datalink_CBCPComplete(dl);
672 datalink_AuthOk(struct datalink *dl)
674 if ((dl->physical->link.lcp.his_callback.opmask &
675 CALLBACK_BIT(CALLBACK_CBCP) ||
676 dl->physical->link.lcp.want_callback.opmask &
677 CALLBACK_BIT(CALLBACK_CBCP)) &&
678 !(dl->physical->link.lcp.want_callback.opmask &
679 CALLBACK_BIT(CALLBACK_AUTH))) {
680 /* We must have agreed CBCP if AUTH isn't there any more */
681 datalink_NewState(dl, DATALINK_CBCP);
683 } else if (dl->physical->link.lcp.want_callback.opmask) {
685 log_Printf(LogPHASE, "%s: Shutdown and await peer callback\n", dl->name);
686 datalink_NewState(dl, DATALINK_LCP);
687 datalink_AuthReInit(dl);
688 fsm_Close(&dl->physical->link.lcp.fsm);
690 switch (dl->physical->link.lcp.his_callback.opmask) {
695 case CALLBACK_BIT(CALLBACK_AUTH):
696 auth_SetPhoneList(dl->peer.authname, dl->cbcp.fsm.phone,
697 sizeof dl->cbcp.fsm.phone);
698 if (*dl->cbcp.fsm.phone == '\0' || !strcmp(dl->cbcp.fsm.phone, "*")) {
699 log_Printf(LogPHASE, "%s: %s cannot be called back\n", dl->name,
701 *dl->cbcp.fsm.phone = '\0';
703 char *ptr = strchr(dl->cbcp.fsm.phone, ',');
705 *ptr = '\0'; /* Call back on the first number */
706 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name,
708 dl->cbcp.required = 1;
710 dl->cbcp.fsm.delay = 0;
711 datalink_NewState(dl, DATALINK_LCP);
712 datalink_AuthReInit(dl);
713 fsm_Close(&dl->physical->link.lcp.fsm);
716 case CALLBACK_BIT(CALLBACK_E164):
717 strncpy(dl->cbcp.fsm.phone, dl->physical->link.lcp.his_callback.msg,
718 sizeof dl->cbcp.fsm.phone - 1);
719 dl->cbcp.fsm.phone[sizeof dl->cbcp.fsm.phone - 1] = '\0';
720 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name,
722 dl->cbcp.required = 1;
723 dl->cbcp.fsm.delay = 0;
724 datalink_NewState(dl, DATALINK_LCP);
725 datalink_AuthReInit(dl);
726 fsm_Close(&dl->physical->link.lcp.fsm);
730 log_Printf(LogPHASE, "%s: Oops - Should have NAK'd peer callback !\n",
732 datalink_NewState(dl, DATALINK_LCP);
733 datalink_AuthReInit(dl);
734 fsm_Close(&dl->physical->link.lcp.fsm);
740 datalink_AuthNotOk(struct datalink *dl)
742 datalink_NewState(dl, DATALINK_LCP);
743 datalink_AuthReInit(dl);
744 fsm_Close(&dl->physical->link.lcp.fsm);
748 datalink_LayerDown(void *v, struct fsm *fp)
750 /* The given FSM has been told to come down */
751 struct datalink *dl = (struct datalink *)v;
753 if (fp->proto == PROTO_LCP) {
756 peerid_Init(&dl->peer);
757 fsm2initial(&dl->physical->link.ccp.fsm);
758 datalink_NewState(dl, DATALINK_LCP); /* before parent TLD */
759 (*dl->parent->LayerDown)(dl->parent->object, fp);
760 /* fall through (just in case) */
763 if (!dl->cbcp.required)
764 cbcp_Down(&dl->cbcp);
765 /* fall through (just in case) */
768 timer_Stop(&dl->pap.authtimer);
769 timer_Stop(&dl->chap.auth.authtimer);
771 datalink_NewState(dl, DATALINK_LCP);
772 datalink_AuthReInit(dl);
777 datalink_LayerFinish(void *v, struct fsm *fp)
779 /* The given fsm is now down */
780 struct datalink *dl = (struct datalink *)v;
782 if (fp->proto == PROTO_LCP) {
784 (*dl->parent->LayerFinish)(dl->parent->object, fp);
785 datalink_ComeDown(dl, CLOSE_NORMAL);
786 } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
787 fsm_Open(fp); /* CCP goes to ST_STOPPED */
791 datalink_Create(const char *name, struct bundle *bundle, int type)
795 dl = (struct datalink *)malloc(sizeof(struct datalink));
799 dl->desc.type = DATALINK_DESCRIPTOR;
800 dl->desc.UpdateSet = datalink_UpdateSet;
801 dl->desc.IsSet = datalink_IsSet;
802 dl->desc.Read = datalink_Read;
803 dl->desc.Write = datalink_Write;
805 dl->state = DATALINK_CLOSED;
807 *dl->cfg.script.dial = '\0';
808 *dl->cfg.script.login = '\0';
809 *dl->cfg.script.logout = '\0';
810 *dl->cfg.script.hangup = '\0';
811 *dl->cfg.phone.list = '\0';
812 *dl->phone.list = '\0';
813 dl->phone.next = NULL;
814 dl->phone.alt = NULL;
815 dl->phone.chosen = "N/A";
818 dl->script.packetmode = 1;
819 mp_linkInit(&dl->mp);
824 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
827 dl->cfg.dial.max = 1;
828 dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
829 dl->cfg.dial.timeout = DIAL_TIMEOUT;
830 dl->cfg.dial.inc = 0;
831 dl->cfg.dial.maxinc = 10;
833 dl->reconnect_tries = 0;
834 dl->cfg.reconnect.max = 0;
835 dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT;
837 dl->cfg.callback.opmask = 0;
838 dl->cfg.cbcp.delay = 0;
839 *dl->cfg.cbcp.phone = '\0';
840 dl->cfg.cbcp.fsmretry = DEF_FSMRETRY;
842 dl->name = strdup(name);
843 peerid_Init(&dl->peer);
844 dl->parent = &bundle->fsm;
845 dl->fsmp.LayerStart = datalink_LayerStart;
846 dl->fsmp.LayerUp = datalink_LayerUp;
847 dl->fsmp.LayerDown = datalink_LayerDown;
848 dl->fsmp.LayerFinish = datalink_LayerFinish;
849 dl->fsmp.object = dl;
851 if ((dl->physical = physical_Create(dl, type)) == NULL) {
857 pap_Init(&dl->pap, dl->physical);
858 chap_Init(&dl->chap, dl->physical);
859 cbcp_Init(&dl->cbcp, dl->physical);
861 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */
862 chat_Init(&dl->chat, dl->physical);
864 log_Printf(LogPHASE, "%s: Created in %s state\n",
865 dl->name, datalink_State(dl));
871 datalink_Clone(struct datalink *odl, const char *name)
875 dl = (struct datalink *)malloc(sizeof(struct datalink));
879 dl->desc.type = DATALINK_DESCRIPTOR;
880 dl->desc.UpdateSet = datalink_UpdateSet;
881 dl->desc.IsSet = datalink_IsSet;
882 dl->desc.Read = datalink_Read;
883 dl->desc.Write = datalink_Write;
885 dl->state = DATALINK_CLOSED;
887 memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg);
888 mp_linkInit(&dl->mp);
889 *dl->phone.list = '\0';
890 dl->phone.next = NULL;
891 dl->phone.alt = NULL;
892 dl->phone.chosen = "N/A";
893 dl->bundle = odl->bundle;
895 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
897 dl->reconnect_tries = 0;
898 dl->name = strdup(name);
899 peerid_Init(&dl->peer);
900 dl->parent = odl->parent;
901 memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp);
902 dl->fsmp.object = dl;
904 if ((dl->physical = physical_Create(dl, PHYS_INTERACTIVE)) == NULL) {
909 pap_Init(&dl->pap, dl->physical);
910 dl->pap.cfg = odl->pap.cfg;
912 chap_Init(&dl->chap, dl->physical);
913 dl->chap.auth.cfg = odl->chap.auth.cfg;
915 memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg);
916 memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg,
917 sizeof dl->physical->link.lcp.cfg);
918 memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg,
919 sizeof dl->physical->link.ccp.cfg);
920 memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg,
921 sizeof dl->physical->async.cfg);
923 cbcp_Init(&dl->cbcp, dl->physical);
925 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */
926 chat_Init(&dl->chat, dl->physical);
928 log_Printf(LogPHASE, "%s: Cloned in %s state\n",
929 dl->name, datalink_State(dl));
935 datalink_Destroy(struct datalink *dl)
937 struct datalink *result;
939 if (dl->state != DATALINK_CLOSED) {
940 log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n",
943 case DATALINK_HANGUP:
946 chat_Finish(&dl->chat); /* Gotta blat the timers ! */
951 chat_Destroy(&dl->chat);
952 timer_Stop(&dl->dial.timer);
954 physical_Destroy(dl->physical);
962 datalink_Up(struct datalink *dl, int runscripts, int packetmode)
964 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
969 case DATALINK_CLOSED:
970 if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
971 bundle_Phase(dl->bundle) == PHASE_TERMINATE)
972 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
973 datalink_NewState(dl, DATALINK_OPENING);
974 dl->reconnect_tries =
975 dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max;
976 dl->dial.tries = dl->cfg.dial.max;
977 dl->script.run = runscripts;
978 dl->script.packetmode = packetmode;
981 case DATALINK_OPENING:
982 if (!dl->script.run && runscripts)
989 if (!dl->script.packetmode && packetmode) {
990 dl->script.packetmode = 1;
991 if (dl->state == DATALINK_READY) {
993 datalink_NewState(dl, DATALINK_CARRIER);
1001 datalink_Close(struct datalink *dl, int how)
1004 switch (dl->state) {
1006 peerid_Init(&dl->peer);
1007 fsm2initial(&dl->physical->link.ccp.fsm);
1013 datalink_AuthReInit(dl);
1014 if (how == CLOSE_LCP)
1015 datalink_DontHangup(dl);
1016 else if (how == CLOSE_STAYDOWN)
1017 datalink_StayDown(dl);
1018 fsm_Close(&dl->physical->link.lcp.fsm);
1022 datalink_ComeDown(dl, how);
1027 datalink_Down(struct datalink *dl, int how)
1029 /* Carrier is lost */
1030 switch (dl->state) {
1032 peerid_Init(&dl->peer);
1033 fsm2initial(&dl->physical->link.ccp.fsm);
1039 fsm2initial(&dl->physical->link.lcp.fsm);
1040 if (dl->state == DATALINK_OPENING)
1041 return; /* we're doing a callback... */
1045 datalink_ComeDown(dl, how);
1050 datalink_StayDown(struct datalink *dl)
1052 dl->dial.tries = -1;
1053 dl->reconnect_tries = 0;
1058 datalink_DontHangup(struct datalink *dl)
1060 dl->dial.tries = -1;
1061 dl->reconnect_tries = 0;
1062 dl->stayonline = dl->state >= DATALINK_LCP ? 1 : 0;
1066 datalink_Show(struct cmdargs const *arg)
1068 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name);
1069 prompt_Printf(arg->prompt, " State: %s\n",
1070 datalink_State(arg->cx));
1071 prompt_Printf(arg->prompt, " Peer name: ");
1072 if (*arg->cx->peer.authname)
1073 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname);
1074 else if (arg->cx->state == DATALINK_OPEN)
1075 prompt_Printf(arg->prompt, "None requested\n");
1077 prompt_Printf(arg->prompt, "N/A\n");
1078 prompt_Printf(arg->prompt, " Discriminator: %s\n",
1079 mp_Enddisc(arg->cx->peer.enddisc.class,
1080 arg->cx->peer.enddisc.address,
1081 arg->cx->peer.enddisc.len));
1083 prompt_Printf(arg->prompt, "\nDefaults:\n");
1084 prompt_Printf(arg->prompt, " Phone List: %s\n",
1085 arg->cx->cfg.phone.list);
1086 if (arg->cx->cfg.dial.max)
1087 prompt_Printf(arg->prompt, " Dial tries: %d, delay ",
1088 arg->cx->cfg.dial.max);
1090 prompt_Printf(arg->prompt, " Dial tries: infinite, delay ");
1091 if (arg->cx->cfg.dial.next_timeout >= 0)
1092 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout);
1094 prompt_Printf(arg->prompt, "random/");
1095 if (arg->cx->cfg.dial.timeout >= 0)
1096 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout);
1098 prompt_Printf(arg->prompt, "random\n");
1099 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ",
1100 arg->cx->cfg.reconnect.max);
1101 if (arg->cx->cfg.reconnect.timeout > 0)
1102 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout);
1104 prompt_Printf(arg->prompt, "random\n");
1105 prompt_Printf(arg->prompt, " Callback %s ", arg->cx->physical->type ==
1106 PHYS_DIRECT ? "accepted: " : "requested:");
1107 if (!arg->cx->cfg.callback.opmask)
1108 prompt_Printf(arg->prompt, "none\n");
1112 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_NONE)) {
1113 prompt_Printf(arg->prompt, "none");
1116 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_AUTH)) {
1117 prompt_Printf(arg->prompt, "%sauth", comma ? ", " : "");
1120 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_E164)) {
1121 prompt_Printf(arg->prompt, "%sE.164", comma ? ", " : "");
1122 if (arg->cx->physical->type != PHYS_DIRECT)
1123 prompt_Printf(arg->prompt, " (%s)", arg->cx->cfg.callback.msg);
1126 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_CBCP)) {
1127 prompt_Printf(arg->prompt, "%scbcp\n", comma ? ", " : "");
1128 prompt_Printf(arg->prompt, " CBCP: delay: %ds\n",
1129 arg->cx->cfg.cbcp.delay);
1130 prompt_Printf(arg->prompt, " phone: ");
1131 if (!strcmp(arg->cx->cfg.cbcp.phone, "*")) {
1132 if (arg->cx->physical->type & PHYS_DIRECT)
1133 prompt_Printf(arg->prompt, "Caller decides\n");
1135 prompt_Printf(arg->prompt, "Dialback server decides\n");
1137 prompt_Printf(arg->prompt, "%s\n", arg->cx->cfg.cbcp.phone);
1138 prompt_Printf(arg->prompt, " timeout: %lds\n",
1139 arg->cx->cfg.cbcp.fsmretry);
1141 prompt_Printf(arg->prompt, "\n");
1144 prompt_Printf(arg->prompt, " Dial Script: %s\n",
1145 arg->cx->cfg.script.dial);
1146 prompt_Printf(arg->prompt, " Login Script: %s\n",
1147 arg->cx->cfg.script.login);
1148 prompt_Printf(arg->prompt, " Logout Script: %s\n",
1149 arg->cx->cfg.script.logout);
1150 prompt_Printf(arg->prompt, " Hangup Script: %s\n",
1151 arg->cx->cfg.script.hangup);
1156 datalink_SetReconnect(struct cmdargs const *arg)
1158 if (arg->argc == arg->argn+2) {
1159 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]);
1160 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]);
1167 datalink_SetRedial(struct cmdargs const *arg)
1169 const char *sep, *osep;
1170 int timeout, inc, maxinc, tries;
1172 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) {
1173 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 &&
1174 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) {
1175 arg->cx->cfg.dial.timeout = -1;
1178 timeout = atoi(arg->argv[arg->argn]);
1181 arg->cx->cfg.dial.timeout = timeout;
1183 log_Printf(LogWARN, "Invalid redial timeout\n");
1188 sep = strchr(arg->argv[arg->argn], '+');
1193 arg->cx->cfg.dial.inc = inc;
1195 log_Printf(LogWARN, "Invalid timeout increment\n");
1198 sep = strchr(sep, '-');
1200 maxinc = atoi(++sep);
1202 arg->cx->cfg.dial.maxinc = maxinc;
1204 log_Printf(LogWARN, "Invalid maximum timeout increments\n");
1208 /* Default timeout increment */
1209 arg->cx->cfg.dial.maxinc = 10;
1213 /* Default timeout increment & max increment */
1214 arg->cx->cfg.dial.inc = 0;
1215 arg->cx->cfg.dial.maxinc = 10;
1216 sep = arg->argv[arg->argn];
1219 sep = strchr(sep, '.');
1221 if (strcasecmp(++sep, "random") == 0) {
1222 arg->cx->cfg.dial.next_timeout = -1;
1225 timeout = atoi(sep);
1227 arg->cx->cfg.dial.next_timeout = timeout;
1229 log_Printf(LogWARN, "Invalid next redial timeout\n");
1234 /* Default next timeout */
1235 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
1237 if (arg->argc == arg->argn+2) {
1238 tries = atoi(arg->argv[arg->argn+1]);
1241 arg->cx->cfg.dial.max = tries;
1243 log_Printf(LogWARN, "Invalid retry value\n");
1253 static const char * const states[] = {
1269 datalink_State(struct datalink *dl)
1271 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0])
1273 return states[dl->state];
1277 datalink_NewState(struct datalink *dl, int state)
1279 if (state != dl->state) {
1280 if (state >= 0 && state < sizeof states / sizeof states[0]) {
1281 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl),
1285 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state);
1290 iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov,
1291 int fd, int *auxfd, int *nauxfd)
1293 struct datalink *dl, *cdl;
1294 struct fsm_retry copy;
1297 dl = (struct datalink *)iov[(*niov)++].iov_base;
1298 dl->name = iov[*niov].iov_base;
1300 if (dl->name[DATALINK_MAXNAME-1]) {
1301 dl->name[DATALINK_MAXNAME-1] = '\0';
1302 if (strlen(dl->name) == DATALINK_MAXNAME - 1)
1303 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name);
1306 /* Make sure the name is unique ! */
1309 for (cdl = bundle->links; cdl; cdl = cdl->next)
1310 if (!strcasecmp(dl->name, cdl->name)) {
1312 free(datalink_NextName(dl));
1314 oname = datalink_NextName(dl);
1315 break; /* Keep renaming 'till we have no conflicts */
1320 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name);
1323 dl->name = strdup(dl->name);
1324 free(iov[*niov].iov_base);
1328 dl->desc.type = DATALINK_DESCRIPTOR;
1329 dl->desc.UpdateSet = datalink_UpdateSet;
1330 dl->desc.IsSet = datalink_IsSet;
1331 dl->desc.Read = datalink_Read;
1332 dl->desc.Write = datalink_Write;
1334 mp_linkInit(&dl->mp);
1335 *dl->phone.list = '\0';
1336 dl->phone.next = NULL;
1337 dl->phone.alt = NULL;
1338 dl->phone.chosen = "N/A";
1340 dl->bundle = bundle;
1342 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
1344 dl->reconnect_tries = 0;
1345 dl->parent = &bundle->fsm;
1346 dl->fsmp.LayerStart = datalink_LayerStart;
1347 dl->fsmp.LayerUp = datalink_LayerUp;
1348 dl->fsmp.LayerDown = datalink_LayerDown;
1349 dl->fsmp.LayerFinish = datalink_LayerFinish;
1350 dl->fsmp.object = dl;
1352 dl->physical = iov2physical(dl, iov, niov, maxiov, fd, auxfd, nauxfd);
1354 if (!dl->physical) {
1359 copy = dl->pap.cfg.fsm;
1360 pap_Init(&dl->pap, dl->physical);
1361 dl->pap.cfg.fsm = copy;
1363 copy = dl->chap.auth.cfg.fsm;
1364 chap_Init(&dl->chap, dl->physical);
1365 dl->chap.auth.cfg.fsm = copy;
1367 cbcp_Init(&dl->cbcp, dl->physical);
1369 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */
1370 chat_Init(&dl->chat, dl->physical);
1372 log_Printf(LogPHASE, "%s: Transferred in %s state\n",
1373 dl->name, datalink_State(dl));
1380 datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
1381 int *auxfd, int *nauxfd)
1383 /* If `dl' is NULL, we're allocating before a Fromiov() */
1387 timer_Stop(&dl->dial.timer);
1388 /* The following is purely for the sake of paranoia */
1389 cbcp_Down(&dl->cbcp);
1390 timer_Stop(&dl->pap.authtimer);
1391 timer_Stop(&dl->chap.auth.authtimer);
1394 if (*niov >= maxiov - 1) {
1395 log_Printf(LogERROR, "Toiov: No room for datalink !\n");
1403 iov[*niov].iov_base = (void *)dl;
1404 iov[(*niov)++].iov_len = sizeof *dl;
1405 iov[*niov].iov_base = dl ? realloc(dl->name, DATALINK_MAXNAME) : NULL;
1406 iov[(*niov)++].iov_len = DATALINK_MAXNAME;
1408 link_fd = physical2iov(dl ? dl->physical : NULL, iov, niov, maxiov, auxfd,
1411 if (link_fd == -1 && dl) {
1420 datalink_Rename(struct datalink *dl, const char *name)
1423 dl->physical->link.name = dl->name = strdup(name);
1427 datalink_NextName(struct datalink *dl)
1432 n = strlen(dl->name);
1433 name = (char *)malloc(n+3);
1434 for (f = n - 1; f >= 0; f--)
1435 if (!isdigit(dl->name[f]))
1437 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name);
1438 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1);
1441 /* our physical link name isn't updated (it probably isn't created yet) */
1446 datalink_SetMode(struct datalink *dl, int mode)
1448 if (!physical_SetMode(dl->physical, mode))
1450 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
1452 if (dl->physical->type == PHYS_DIRECT)
1453 dl->reconnect_tries = 0;
1454 if (mode & (PHYS_DDIAL|PHYS_BACKGROUND|PHYS_FOREGROUND) &&
1455 dl->state <= DATALINK_READY)
1456 datalink_Up(dl, 1, 1);
1461 datalink_GetDialTimeout(struct datalink *dl)
1463 int result = dl->cfg.dial.timeout + dl->dial.incs * dl->cfg.dial.inc;
1465 if (dl->dial.incs < dl->cfg.dial.maxinc)