]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/datalink.c
This commit was generated by cvs2svn to compensate for changes in r55924,
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / datalink.c
1 /*-
2  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <sys/un.h>
34
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/uio.h>
40 #include <termios.h>
41
42 #include "layer.h"
43 #include "mbuf.h"
44 #include "log.h"
45 #include "defs.h"
46 #include "timer.h"
47 #include "fsm.h"
48 #include "lcp.h"
49 #include "descriptor.h"
50 #include "lqr.h"
51 #include "hdlc.h"
52 #include "async.h"
53 #include "throughput.h"
54 #include "ccp.h"
55 #include "link.h"
56 #include "physical.h"
57 #include "iplist.h"
58 #include "slcompress.h"
59 #include "ipcp.h"
60 #include "filter.h"
61 #include "mp.h"
62 #ifndef NORADIUS
63 #include "radius.h"
64 #endif
65 #include "bundle.h"
66 #include "chat.h"
67 #include "auth.h"
68 #include "prompt.h"
69 #include "proto.h"
70 #include "pap.h"
71 #include "chap.h"
72 #include "command.h"
73 #include "cbcp.h"
74 #include "datalink.h"
75
76 static void datalink_LoginDone(struct datalink *);
77 static void datalink_NewState(struct datalink *, int);
78
79 static void
80 datalink_OpenTimeout(void *v)
81 {
82   struct datalink *dl = (struct datalink *)v;
83
84   timer_Stop(&dl->dial.timer);
85   if (dl->state == DATALINK_OPENING)
86     log_Printf(LogPHASE, "%s: Redial timer expired.\n", dl->name);
87 }
88
89 static int
90 datalink_StartDialTimer(struct datalink *dl, int Timeout)
91 {
92   int result = Timeout;
93
94   timer_Stop(&dl->dial.timer);
95   if (Timeout) {
96     if (Timeout > 0)
97       dl->dial.timer.load = Timeout * SECTICKS;
98     else {
99       result = (random() % DIAL_TIMEOUT) + 1;
100       dl->dial.timer.load = result * SECTICKS;
101     }
102     dl->dial.timer.func = datalink_OpenTimeout;
103     dl->dial.timer.name = "dial";
104     dl->dial.timer.arg = dl;
105     timer_Start(&dl->dial.timer);
106     if (dl->state == DATALINK_OPENING)
107       log_Printf(LogPHASE, "%s: Enter pause (%d) for redialing.\n",
108                 dl->name, Timeout);
109   }
110   return result;
111 }
112
113 static void
114 datalink_HangupDone(struct datalink *dl)
115 {
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);
120     return;
121   }
122
123   chat_Finish(&dl->chat);
124   physical_Close(dl->physical);
125   dl->phone.chosen = "N/A";
126
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;
136     dl->dial.incs = 0;
137     dl->script.run = 1;
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);
156     dl->dial.tries = -1;
157     dl->dial.incs = 0;
158     dl->reconnect_tries = 0;
159     bundle_LinkClosed(dl->bundle, dl);
160     if (!dl->bundle->CleaningUp)
161       datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
162   } else {
163     datalink_NewState(dl, DATALINK_OPENING);
164     if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
165         bundle_Phase(dl->bundle) == PHASE_TERMINATE)
166       bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
167     if (dl->dial.tries < 0) {
168       datalink_StartDialTimer(dl, dl->cfg.reconnect.timeout);
169       dl->dial.tries = dl->cfg.dial.max;
170       dl->dial.incs = 0;
171       dl->reconnect_tries--;
172     } else {
173       if (dl->phone.next == NULL)
174         datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
175       else
176         datalink_StartDialTimer(dl, dl->cfg.dial.next_timeout);
177     }
178   }
179 }
180
181 const char *
182 datalink_ChoosePhoneNumber(struct datalink *dl)
183 {
184   char *phone;
185
186   if (dl->phone.alt == NULL) {
187     if (dl->phone.next == NULL) {
188       strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1);
189       dl->phone.list[sizeof dl->phone.list - 1] = '\0';
190       if (*dl->phone.list == '\0')
191         return "";
192       dl->phone.next = dl->phone.list;
193     }
194     dl->phone.alt = strsep(&dl->phone.next, ":");
195   }
196   phone = strsep(&dl->phone.alt, "|");
197   dl->phone.chosen = *phone ? phone : "[NONE]";
198   if (*phone)
199     log_Printf(LogPHASE, "Phone: %s\n", phone);
200   return phone;
201 }
202
203 static void
204 datalink_LoginDone(struct datalink *dl)
205 {
206   chat_Finish(&dl->chat);
207
208   if (!dl->script.packetmode) { 
209     dl->dial.tries = -1;
210     dl->dial.incs = 0;
211     datalink_NewState(dl, DATALINK_READY);
212   } else if (!physical_Raw(dl->physical)) {
213     dl->dial.tries = 0;
214     log_Printf(LogWARN, "datalink_LoginDone: Not connected.\n");
215     if (dl->script.run) { 
216       datalink_NewState(dl, DATALINK_LOGOUT);
217       if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL))
218         log_Printf(LogWARN, "Invalid logout script\n");
219     } else {
220       physical_StopDeviceTimer(dl->physical);
221       if (dl->physical->type == PHYS_DEDICATED)
222         /* force a redial timeout */
223         physical_Close(dl->physical);
224       datalink_HangupDone(dl);
225     }
226   } else {
227     dl->dial.tries = -1;
228     dl->dial.incs = 0;
229
230     hdlc_Init(&dl->physical->hdlc, &dl->physical->link.lcp);
231     async_Init(&dl->physical->async);
232
233     lcp_Setup(&dl->physical->link.lcp, dl->state == DATALINK_READY ?
234               0 : dl->physical->link.lcp.cfg.openmode);
235     ccp_Setup(&dl->physical->link.ccp);
236
237     datalink_NewState(dl, DATALINK_LCP);
238     fsm_Up(&dl->physical->link.lcp.fsm);
239     fsm_Open(&dl->physical->link.lcp.fsm);
240   }
241 }
242
243 static int
244 datalink_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
245                    int *n)
246 {
247   struct datalink *dl = descriptor2datalink(d);
248   int result;
249
250   result = 0;
251   switch (dl->state) {
252     case DATALINK_CLOSED:
253       if ((dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED|PHYS_BACKGROUND|
254                                  PHYS_FOREGROUND|PHYS_DDIAL)) &&
255           !dl->bundle->CleaningUp)
256         /*
257          * Our first time in - DEDICATED & DDIAL never come down, and
258          * DIRECT, FOREGROUND & BACKGROUND get deleted when they enter
259          * DATALINK_CLOSED.  Go to DATALINK_OPENING via datalink_Up()
260          * and fall through.
261          */
262         datalink_Up(dl, 1, 1);
263       else
264         break;
265       /* fall through */
266
267     case DATALINK_OPENING:
268       if (dl->dial.timer.state != TIMER_RUNNING) {
269         if (--dl->dial.tries < 0)
270           dl->dial.tries = 0;
271         if (physical_Open(dl->physical, dl->bundle) >= 0) {
272           log_WritePrompts(dl, "%s: Entering terminal mode on %s\r\n"
273                            "Type `~?' for help\r\n", dl->name,
274                            dl->physical->name.full);
275           if (dl->script.run) {
276             datalink_NewState(dl, DATALINK_DIAL);
277             if (!chat_Setup(&dl->chat, dl->cfg.script.dial,
278                             *dl->cfg.script.dial ?
279                             datalink_ChoosePhoneNumber(dl) : ""))
280               log_Printf(LogWARN, "Invalid dial script\n");
281             if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
282                 dl->cfg.dial.max)
283               log_Printf(LogCHAT, "%s: Dial attempt %u of %d\n",
284                         dl->name, dl->cfg.dial.max - dl->dial.tries,
285                         dl->cfg.dial.max);
286           } else
287             datalink_NewState(dl, DATALINK_CARRIER);
288           return datalink_UpdateSet(d, r, w, e, n);
289         } else {
290           if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
291               dl->cfg.dial.max)
292             log_Printf(LogCHAT, "Failed to open device (attempt %u of %d)\n",
293                        dl->cfg.dial.max - dl->dial.tries, dl->cfg.dial.max);
294           else
295             log_Printf(LogCHAT, "Failed to open device\n");
296
297           if (dl->bundle->CleaningUp ||
298               (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) &&
299                dl->cfg.dial.max && dl->dial.tries == 0)) {
300             datalink_NewState(dl, DATALINK_CLOSED);
301             dl->reconnect_tries = 0;
302             dl->dial.tries = -1;
303             log_WritePrompts(dl, "Failed to open %s\n",
304                              dl->physical->name.full);
305             bundle_LinkClosed(dl->bundle, dl);
306           }
307           if (!dl->bundle->CleaningUp) {
308             int timeout;
309
310             timeout = datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl));
311             log_WritePrompts(dl, "Failed to open %s, pause %d seconds\n",
312                              dl->physical->name.full, timeout);
313           }
314         }
315       }
316       break;
317
318     case DATALINK_CARRIER:
319       /* Wait for carrier on the device */
320       switch (physical_AwaitCarrier(dl->physical)) {
321         case CARRIER_PENDING:
322           log_Printf(LogDEBUG, "Waiting for carrier\n");
323           return 0;     /* A device timer is running to wake us up again */
324
325         case CARRIER_OK:
326           if (dl->script.run) {
327             datalink_NewState(dl, DATALINK_LOGIN);
328             if (!chat_Setup(&dl->chat, dl->cfg.script.login, NULL))
329               log_Printf(LogWARN, "Invalid login script\n");
330           } else
331             datalink_LoginDone(dl);
332           return datalink_UpdateSet(d, r, w, e, n);
333
334         case CARRIER_LOST:
335           physical_Offline(dl->physical);       /* Is this required ? */
336           if (dl->script.run) { 
337             datalink_NewState(dl, DATALINK_HANGUP);
338             if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
339               log_Printf(LogWARN, "Invalid hangup script\n");
340             return datalink_UpdateSet(d, r, w, e, n);
341           } else {
342             datalink_HangupDone(dl);
343             return 0;   /* Maybe bundle_CleanDatalinks() has something to do */
344           }
345       }
346
347     case DATALINK_HANGUP:
348     case DATALINK_DIAL:
349     case DATALINK_LOGOUT:
350     case DATALINK_LOGIN:
351       result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n);
352       switch (dl->chat.state) {
353         case CHAT_DONE:
354           /* script succeeded */
355           switch(dl->state) {
356             case DATALINK_HANGUP:
357               datalink_HangupDone(dl);
358               break;
359             case DATALINK_DIAL:
360               datalink_NewState(dl, DATALINK_CARRIER);
361               return datalink_UpdateSet(d, r, w, e, n);
362             case DATALINK_LOGOUT:
363               datalink_NewState(dl, DATALINK_HANGUP);
364               physical_Offline(dl->physical);
365               if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
366                 log_Printf(LogWARN, "Invalid hangup script\n");
367               return datalink_UpdateSet(d, r, w, e, n);
368             case DATALINK_LOGIN:
369               dl->phone.alt = NULL;
370               datalink_LoginDone(dl);
371               return datalink_UpdateSet(d, r, w, e, n);
372           }
373           break;
374         case CHAT_FAILED:
375           /* Going down - script failed */
376           log_Printf(LogWARN, "Chat script failed\n");
377           switch(dl->state) {
378             case DATALINK_HANGUP:
379               datalink_HangupDone(dl);
380               break;
381             case DATALINK_DIAL:
382             case DATALINK_LOGOUT:
383             case DATALINK_LOGIN:
384               datalink_NewState(dl, DATALINK_HANGUP);
385               physical_Offline(dl->physical);
386               if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
387                 log_Printf(LogWARN, "Invalid hangup script\n");
388               return datalink_UpdateSet(d, r, w, e, n);
389           }
390           break;
391       }
392       break;
393
394     case DATALINK_READY:
395     case DATALINK_LCP:
396     case DATALINK_AUTH:
397     case DATALINK_CBCP:
398     case DATALINK_OPEN:
399       result = descriptor_UpdateSet(&dl->chap.desc, r, w, e, n) +
400                descriptor_UpdateSet(&dl->physical->desc, r, w, e, n);
401       break;
402   }
403   return result;
404 }
405
406 int
407 datalink_RemoveFromSet(struct datalink *dl, fd_set *r, fd_set *w, fd_set *e)
408 {
409   return physical_RemoveFromSet(dl->physical, r, w, e);
410 }
411
412 static int
413 datalink_IsSet(struct descriptor *d, const fd_set *fdset)
414 {
415   struct datalink *dl = descriptor2datalink(d);
416
417   switch (dl->state) {
418     case DATALINK_CLOSED:
419     case DATALINK_OPENING:
420       break;
421
422     case DATALINK_HANGUP:
423     case DATALINK_DIAL:
424     case DATALINK_LOGOUT:
425     case DATALINK_LOGIN:
426       return descriptor_IsSet(&dl->chat.desc, fdset);
427
428     case DATALINK_READY:
429     case DATALINK_LCP:
430     case DATALINK_AUTH:
431     case DATALINK_CBCP:
432     case DATALINK_OPEN:
433       return descriptor_IsSet(&dl->chap.desc, fdset) ? 1 :
434              descriptor_IsSet(&dl->physical->desc, fdset);
435   }
436   return 0;
437 }
438
439 static void
440 datalink_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
441 {
442   struct datalink *dl = descriptor2datalink(d);
443
444   switch (dl->state) {
445     case DATALINK_CLOSED:
446     case DATALINK_OPENING:
447       break;
448
449     case DATALINK_HANGUP:
450     case DATALINK_DIAL:
451     case DATALINK_LOGOUT:
452     case DATALINK_LOGIN:
453       descriptor_Read(&dl->chat.desc, bundle, fdset);
454       break;
455
456     case DATALINK_READY:
457     case DATALINK_LCP:
458     case DATALINK_AUTH:
459     case DATALINK_CBCP:
460     case DATALINK_OPEN:
461       if (descriptor_IsSet(&dl->chap.desc, fdset))
462         descriptor_Read(&dl->chap.desc, bundle, fdset);
463       if (descriptor_IsSet(&dl->physical->desc, fdset))
464         descriptor_Read(&dl->physical->desc, bundle, fdset);
465       break;
466   }
467 }
468
469 static int
470 datalink_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
471 {
472   struct datalink *dl = descriptor2datalink(d);
473   int result = 0;
474
475   switch (dl->state) {
476     case DATALINK_CLOSED:
477     case DATALINK_OPENING:
478       break;
479
480     case DATALINK_HANGUP:
481     case DATALINK_DIAL:
482     case DATALINK_LOGOUT:
483     case DATALINK_LOGIN:
484       result = descriptor_Write(&dl->chat.desc, bundle, fdset);
485       break;
486
487     case DATALINK_READY:
488     case DATALINK_LCP:
489     case DATALINK_AUTH:
490     case DATALINK_CBCP:
491     case DATALINK_OPEN:
492       if (descriptor_IsSet(&dl->chap.desc, fdset))
493         result += descriptor_Write(&dl->chap.desc, bundle, fdset);
494       if (descriptor_IsSet(&dl->physical->desc, fdset))
495         result += descriptor_Write(&dl->physical->desc, bundle, fdset);
496       break;
497   }
498
499   return result;
500 }
501
502 static void
503 datalink_ComeDown(struct datalink *dl, int how)
504 {
505   if (how != CLOSE_NORMAL) {
506     dl->dial.tries = -1;
507     dl->reconnect_tries = 0;
508     if (dl->state >= DATALINK_READY && how == CLOSE_LCP)
509       dl->stayonline = 1;
510   }
511
512   if (dl->state >= DATALINK_READY && dl->stayonline) {
513     dl->stayonline = 0;
514     physical_StopDeviceTimer(dl->physical);
515     datalink_NewState(dl, DATALINK_READY);
516   } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) {
517     physical_Offline(dl->physical);
518     if (dl->script.run && dl->state != DATALINK_OPENING) {
519       if (dl->state == DATALINK_LOGOUT) {
520         datalink_NewState(dl, DATALINK_HANGUP);
521         if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL))
522           log_Printf(LogWARN, "Invalid hangup script\n");
523       } else {
524         datalink_NewState(dl, DATALINK_LOGOUT);
525         if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL))
526           log_Printf(LogWARN, "Invalid logout script\n");
527       }
528     } else
529       datalink_HangupDone(dl);
530   }
531 }
532
533 static void
534 datalink_LayerStart(void *v, struct fsm *fp)
535 {
536   /* The given FSM is about to start up ! */
537   struct datalink *dl = (struct datalink *)v;
538
539   if (fp->proto == PROTO_LCP)
540     (*dl->parent->LayerStart)(dl->parent->object, fp);
541 }
542
543 static void
544 datalink_LayerUp(void *v, struct fsm *fp)
545 {
546   /* The given fsm is now up */
547   struct datalink *dl = (struct datalink *)v;
548   struct lcp *lcp = &dl->physical->link.lcp;
549
550   if (fp->proto == PROTO_LCP) {
551     datalink_GotAuthname(dl, "");
552     lcp->auth_ineed = lcp->want_auth;
553     lcp->auth_iwait = lcp->his_auth;
554     if (lcp->his_auth || lcp->want_auth) {
555       if (bundle_Phase(dl->bundle) != PHASE_NETWORK)
556         bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE);
557       log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name,
558                 Auth2Nam(lcp->his_auth, lcp->his_authtype),
559                 Auth2Nam(lcp->want_auth, lcp->want_authtype));
560       if (lcp->his_auth == PROTO_PAP)
561         auth_StartReq(&dl->pap);
562       if (lcp->want_auth == PROTO_CHAP)
563         auth_StartReq(&dl->chap.auth);
564     } else
565       datalink_AuthOk(dl);
566   }
567 }
568
569 static void
570 datalink_AuthReInit(struct datalink *dl)
571 {
572   auth_StopTimer(&dl->pap);
573   auth_StopTimer(&dl->chap.auth);
574   chap_ReInit(&dl->chap);
575 }
576
577 void
578 datalink_GotAuthname(struct datalink *dl, const char *name)
579 {
580   strncpy(dl->peer.authname, name, sizeof dl->peer.authname - 1);
581   dl->peer.authname[sizeof dl->peer.authname - 1] = '\0';
582 }
583
584 void
585 datalink_NCPUp(struct datalink *dl)
586 {
587   int ccpok = ccp_SetOpenMode(&dl->physical->link.ccp);
588
589   if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) {
590     /* we've authenticated in multilink mode ! */
591     switch (mp_Up(&dl->bundle->ncp.mp, dl)) {
592       case MP_LINKSENT:
593         /* We've handed the link off to another ppp (well, we will soon) ! */
594         return;
595       case MP_UP:
596         /* First link in the bundle */
597         auth_Select(dl->bundle, dl->peer.authname);
598         bundle_CalculateBandwidth(dl->bundle);
599         /* fall through */
600       case MP_ADDED:
601         /* We're in multilink mode ! */
602         dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE;    /* override */
603         bundle_CalculateBandwidth(dl->bundle);
604         break;
605       case MP_FAILED:
606         datalink_AuthNotOk(dl);
607         return;
608     }
609   } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) {
610     log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name);
611     datalink_NewState(dl, DATALINK_OPEN);
612     bundle_CalculateBandwidth(dl->bundle);
613     (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm);
614     return;
615   } else {
616     dl->bundle->ncp.mp.peer = dl->peer;
617     ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link);
618     auth_Select(dl->bundle, dl->peer.authname);
619   }
620
621   if (ccpok) {
622     fsm_Up(&dl->physical->link.ccp.fsm);
623     fsm_Open(&dl->physical->link.ccp.fsm);
624   }
625   datalink_NewState(dl, DATALINK_OPEN);
626   bundle_NewPhase(dl->bundle, PHASE_NETWORK);
627   (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm);
628 }
629
630 void
631 datalink_CBCPComplete(struct datalink *dl)
632 {
633   datalink_NewState(dl, DATALINK_LCP);
634   datalink_AuthReInit(dl);
635   fsm_Close(&dl->physical->link.lcp.fsm);
636 }
637
638 void
639 datalink_CBCPFailed(struct datalink *dl)
640 {
641   cbcp_Down(&dl->cbcp);
642   datalink_CBCPComplete(dl);
643 }
644
645 void
646 datalink_AuthOk(struct datalink *dl)
647 {
648   if ((dl->physical->link.lcp.his_callback.opmask &
649        CALLBACK_BIT(CALLBACK_CBCP) ||
650        dl->physical->link.lcp.want_callback.opmask &
651        CALLBACK_BIT(CALLBACK_CBCP)) &&
652       !(dl->physical->link.lcp.want_callback.opmask &
653         CALLBACK_BIT(CALLBACK_AUTH))) {
654     /* We must have agreed CBCP if AUTH isn't there any more */
655     datalink_NewState(dl, DATALINK_CBCP);
656     cbcp_Up(&dl->cbcp);
657   } else if (dl->physical->link.lcp.want_callback.opmask) {
658     /* It's not CBCP */
659     log_Printf(LogPHASE, "%s: Shutdown and await peer callback\n", dl->name);
660     datalink_NewState(dl, DATALINK_LCP);
661     datalink_AuthReInit(dl);
662     fsm_Close(&dl->physical->link.lcp.fsm);
663   } else
664     switch (dl->physical->link.lcp.his_callback.opmask) {
665       case 0:
666         datalink_NCPUp(dl);
667         break;
668
669       case CALLBACK_BIT(CALLBACK_AUTH):
670         auth_SetPhoneList(dl->peer.authname, dl->cbcp.fsm.phone,
671                           sizeof dl->cbcp.fsm.phone);
672         if (*dl->cbcp.fsm.phone == '\0' || !strcmp(dl->cbcp.fsm.phone, "*")) {
673           log_Printf(LogPHASE, "%s: %s cannot be called back\n", dl->name,
674                      dl->peer.authname);
675           *dl->cbcp.fsm.phone = '\0';
676         } else {
677           char *ptr = strchr(dl->cbcp.fsm.phone, ',');
678           if (ptr)
679             *ptr = '\0';        /* Call back on the first number */
680           log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name,
681                      dl->cbcp.fsm.phone);
682           dl->cbcp.required = 1;
683         }
684         dl->cbcp.fsm.delay = 0;
685         datalink_NewState(dl, DATALINK_LCP);
686         datalink_AuthReInit(dl);
687         fsm_Close(&dl->physical->link.lcp.fsm);
688         break;
689
690       case CALLBACK_BIT(CALLBACK_E164):
691         strncpy(dl->cbcp.fsm.phone, dl->physical->link.lcp.his_callback.msg,
692                 sizeof dl->cbcp.fsm.phone - 1);
693         dl->cbcp.fsm.phone[sizeof dl->cbcp.fsm.phone - 1] = '\0';
694         log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name,
695                    dl->cbcp.fsm.phone);
696         dl->cbcp.required = 1;
697         dl->cbcp.fsm.delay = 0;
698         datalink_NewState(dl, DATALINK_LCP);
699         datalink_AuthReInit(dl);
700         fsm_Close(&dl->physical->link.lcp.fsm);
701         break;
702
703       default:
704         log_Printf(LogPHASE, "%s: Oops - Should have NAK'd peer callback !\n",
705                    dl->name);
706         datalink_NewState(dl, DATALINK_LCP);
707         datalink_AuthReInit(dl);
708         fsm_Close(&dl->physical->link.lcp.fsm);
709         break;
710     }
711 }
712
713 void
714 datalink_AuthNotOk(struct datalink *dl)
715 {
716   datalink_NewState(dl, DATALINK_LCP);
717   datalink_AuthReInit(dl);
718   fsm_Close(&dl->physical->link.lcp.fsm);
719 }
720
721 static void
722 datalink_LayerDown(void *v, struct fsm *fp)
723 {
724   /* The given FSM has been told to come down */
725   struct datalink *dl = (struct datalink *)v;
726
727   if (fp->proto == PROTO_LCP) {
728     switch (dl->state) {
729       case DATALINK_OPEN:
730         peerid_Init(&dl->peer);
731         fsm2initial(&dl->physical->link.ccp.fsm);
732         datalink_NewState(dl, DATALINK_LCP);  /* before parent TLD */
733         (*dl->parent->LayerDown)(dl->parent->object, fp);
734         /* fall through (just in case) */
735
736       case DATALINK_CBCP:
737         if (!dl->cbcp.required)
738           cbcp_Down(&dl->cbcp);
739         /* fall through (just in case) */
740
741       case DATALINK_AUTH:
742         timer_Stop(&dl->pap.authtimer);
743         timer_Stop(&dl->chap.auth.authtimer);
744     }
745     datalink_NewState(dl, DATALINK_LCP);
746     datalink_AuthReInit(dl);
747   }
748 }
749
750 static void
751 datalink_LayerFinish(void *v, struct fsm *fp)
752 {
753   /* The given fsm is now down */
754   struct datalink *dl = (struct datalink *)v;
755
756   if (fp->proto == PROTO_LCP) {
757     fsm2initial(fp);
758     (*dl->parent->LayerFinish)(dl->parent->object, fp);
759     datalink_ComeDown(dl, CLOSE_NORMAL);
760   } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
761     fsm_Open(fp);               /* CCP goes to ST_STOPPED */
762 }
763
764 struct datalink *
765 datalink_Create(const char *name, struct bundle *bundle, int type)
766 {
767   struct datalink *dl;
768
769   dl = (struct datalink *)malloc(sizeof(struct datalink));
770   if (dl == NULL)
771     return dl;
772
773   dl->desc.type = DATALINK_DESCRIPTOR;
774   dl->desc.UpdateSet = datalink_UpdateSet;
775   dl->desc.IsSet = datalink_IsSet;
776   dl->desc.Read = datalink_Read;
777   dl->desc.Write = datalink_Write;
778
779   dl->state = DATALINK_CLOSED;
780
781   *dl->cfg.script.dial = '\0';
782   *dl->cfg.script.login = '\0';
783   *dl->cfg.script.logout = '\0';
784   *dl->cfg.script.hangup = '\0';
785   *dl->cfg.phone.list = '\0';
786   *dl->phone.list = '\0';
787   dl->phone.next = NULL;
788   dl->phone.alt = NULL;
789   dl->phone.chosen = "N/A";
790   dl->stayonline = 0;
791   dl->script.run = 1;
792   dl->script.packetmode = 1;
793   mp_linkInit(&dl->mp);
794
795   dl->bundle = bundle;
796   dl->next = NULL;
797
798   memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
799
800   dl->dial.tries = 0;
801   dl->cfg.dial.max = 1;
802   dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
803   dl->cfg.dial.timeout = DIAL_TIMEOUT;
804   dl->cfg.dial.inc = 0;
805   dl->cfg.dial.maxinc = 10;
806
807   dl->reconnect_tries = 0;
808   dl->cfg.reconnect.max = 0;
809   dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT;
810
811   dl->cfg.callback.opmask = 0;
812   dl->cfg.cbcp.delay = 0;
813   *dl->cfg.cbcp.phone = '\0';
814   dl->cfg.cbcp.fsmretry = DEF_FSMRETRY;
815
816   dl->name = strdup(name);
817   peerid_Init(&dl->peer);
818   dl->parent = &bundle->fsm;
819   dl->fsmp.LayerStart = datalink_LayerStart;
820   dl->fsmp.LayerUp = datalink_LayerUp;
821   dl->fsmp.LayerDown = datalink_LayerDown;
822   dl->fsmp.LayerFinish = datalink_LayerFinish;
823   dl->fsmp.object = dl;
824
825   if ((dl->physical = physical_Create(dl, type)) == NULL) {
826     free(dl->name);
827     free(dl);
828     return NULL;
829   }
830
831   pap_Init(&dl->pap, dl->physical);
832   chap_Init(&dl->chap, dl->physical);
833   cbcp_Init(&dl->cbcp, dl->physical);
834
835   memset(&dl->chat, '\0', sizeof dl->chat);     /* Force buf{start,end} reset */
836   chat_Init(&dl->chat, dl->physical);
837
838   log_Printf(LogPHASE, "%s: Created in %s state\n",
839              dl->name, datalink_State(dl));
840
841   return dl;
842 }
843
844 struct datalink *
845 datalink_Clone(struct datalink *odl, const char *name)
846 {
847   struct datalink *dl;
848
849   dl = (struct datalink *)malloc(sizeof(struct datalink));
850   if (dl == NULL)
851     return dl;
852
853   dl->desc.type = DATALINK_DESCRIPTOR;
854   dl->desc.UpdateSet = datalink_UpdateSet;
855   dl->desc.IsSet = datalink_IsSet;
856   dl->desc.Read = datalink_Read;
857   dl->desc.Write = datalink_Write;
858
859   dl->state = DATALINK_CLOSED;
860
861   memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg);
862   mp_linkInit(&dl->mp);
863   *dl->phone.list = '\0';
864   dl->phone.next = NULL;
865   dl->phone.alt = NULL;
866   dl->phone.chosen = "N/A";
867   dl->bundle = odl->bundle;
868   dl->next = NULL;
869   memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
870   dl->dial.tries = 0;
871   dl->reconnect_tries = 0;
872   dl->name = strdup(name);
873   peerid_Init(&dl->peer);
874   dl->parent = odl->parent;
875   memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp);
876   dl->fsmp.object = dl;
877
878   if ((dl->physical = physical_Create(dl, PHYS_INTERACTIVE)) == NULL) {
879     free(dl->name);
880     free(dl);
881     return NULL;
882   }
883   pap_Init(&dl->pap, dl->physical);
884   dl->pap.cfg = odl->pap.cfg;
885
886   chap_Init(&dl->chap, dl->physical);
887   dl->chap.auth.cfg = odl->chap.auth.cfg;
888
889   memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg);
890   memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg,
891          sizeof dl->physical->link.lcp.cfg);
892   memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg,
893          sizeof dl->physical->link.ccp.cfg);
894   memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg,
895          sizeof dl->physical->async.cfg);
896
897   cbcp_Init(&dl->cbcp, dl->physical);
898
899   memset(&dl->chat, '\0', sizeof dl->chat);     /* Force buf{start,end} reset */
900   chat_Init(&dl->chat, dl->physical);
901
902   log_Printf(LogPHASE, "%s: Cloned in %s state\n",
903              dl->name, datalink_State(dl));
904
905   return dl;
906 }
907
908 struct datalink *
909 datalink_Destroy(struct datalink *dl)
910 {
911   struct datalink *result;
912
913   if (dl->state != DATALINK_CLOSED) {
914     log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n",
915               datalink_State(dl));
916     switch (dl->state) {
917       case DATALINK_HANGUP:
918       case DATALINK_DIAL:
919       case DATALINK_LOGIN:
920         chat_Finish(&dl->chat);         /* Gotta blat the timers ! */
921         break;
922     }
923   }
924
925   chat_Destroy(&dl->chat);
926   timer_Stop(&dl->dial.timer);
927   result = dl->next;
928   physical_Destroy(dl->physical);
929   free(dl->name);
930   free(dl);
931
932   return result;
933 }
934
935 void
936 datalink_Up(struct datalink *dl, int runscripts, int packetmode)
937 {
938   if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
939     /* Ignore scripts */
940     runscripts = 0;
941
942   switch (dl->state) {
943     case DATALINK_CLOSED:
944       if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
945           bundle_Phase(dl->bundle) == PHASE_TERMINATE)
946         bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
947       datalink_NewState(dl, DATALINK_OPENING);
948       dl->reconnect_tries =
949         dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max;
950       dl->dial.tries = dl->cfg.dial.max;
951       dl->script.run = runscripts;
952       dl->script.packetmode = packetmode;
953       break;
954
955     case DATALINK_OPENING:
956       if (!dl->script.run && runscripts)
957         dl->script.run = 1;
958       /* fall through */
959
960     case DATALINK_DIAL:
961     case DATALINK_LOGIN:
962     case DATALINK_READY:
963       if (!dl->script.packetmode && packetmode) {
964         dl->script.packetmode = 1;
965         if (dl->state == DATALINK_READY)
966           datalink_LoginDone(dl);
967       }
968       break;
969   }
970 }
971
972 void
973 datalink_Close(struct datalink *dl, int how)
974 {
975   /* Please close */
976   switch (dl->state) {
977     case DATALINK_OPEN:
978       peerid_Init(&dl->peer);
979       fsm2initial(&dl->physical->link.ccp.fsm);
980       /* fall through */
981
982     case DATALINK_CBCP:
983     case DATALINK_AUTH:
984     case DATALINK_LCP:
985       datalink_AuthReInit(dl);
986       fsm_Close(&dl->physical->link.lcp.fsm);
987       if (how != CLOSE_NORMAL) {
988         dl->dial.tries = -1;
989         dl->reconnect_tries = 0;
990         if (how == CLOSE_LCP)
991           dl->stayonline = 1;
992       }
993       break;
994
995     default:
996       datalink_ComeDown(dl, how);
997   }
998 }
999
1000 void
1001 datalink_Down(struct datalink *dl, int how)
1002 {
1003   /* Carrier is lost */
1004   switch (dl->state) {
1005     case DATALINK_OPEN:
1006       peerid_Init(&dl->peer);
1007       fsm2initial(&dl->physical->link.ccp.fsm);
1008       /* fall through */
1009
1010     case DATALINK_CBCP:
1011     case DATALINK_AUTH:
1012     case DATALINK_LCP:
1013       fsm2initial(&dl->physical->link.lcp.fsm);
1014       /* fall through */
1015
1016     default:
1017       datalink_ComeDown(dl, how);
1018   }
1019 }
1020
1021 void
1022 datalink_StayDown(struct datalink *dl)
1023 {
1024   dl->reconnect_tries = 0;
1025 }
1026
1027 void
1028 datalink_DontHangup(struct datalink *dl)
1029 {
1030   if (dl->state >= DATALINK_LCP)
1031     dl->stayonline = 1;
1032 }
1033
1034 int
1035 datalink_Show(struct cmdargs const *arg)
1036 {
1037   prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name);
1038   prompt_Printf(arg->prompt, " State:              %s\n",
1039                 datalink_State(arg->cx));
1040   prompt_Printf(arg->prompt, " Peer name:          ");
1041   if (*arg->cx->peer.authname)
1042     prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname);
1043   else if (arg->cx->state == DATALINK_OPEN)
1044     prompt_Printf(arg->prompt, "None requested\n");
1045   else
1046     prompt_Printf(arg->prompt, "N/A\n");
1047   prompt_Printf(arg->prompt, " Discriminator:      %s\n",
1048                 mp_Enddisc(arg->cx->peer.enddisc.class,
1049                            arg->cx->peer.enddisc.address,
1050                            arg->cx->peer.enddisc.len));
1051
1052   prompt_Printf(arg->prompt, "\nDefaults:\n");
1053   prompt_Printf(arg->prompt, " Phone List:         %s\n",
1054                 arg->cx->cfg.phone.list);
1055   if (arg->cx->cfg.dial.max)
1056     prompt_Printf(arg->prompt, " Dial tries:         %d, delay ",
1057                   arg->cx->cfg.dial.max);
1058   else
1059     prompt_Printf(arg->prompt, " Dial tries:         infinite, delay ");
1060   if (arg->cx->cfg.dial.next_timeout >= 0)
1061     prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout);
1062   else
1063     prompt_Printf(arg->prompt, "random/");
1064   if (arg->cx->cfg.dial.timeout >= 0)
1065     prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout);
1066   else
1067     prompt_Printf(arg->prompt, "random\n");
1068   prompt_Printf(arg->prompt, " Reconnect tries:    %d, delay ",
1069                 arg->cx->cfg.reconnect.max);
1070   if (arg->cx->cfg.reconnect.timeout > 0)
1071     prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout);
1072   else
1073     prompt_Printf(arg->prompt, "random\n");
1074   prompt_Printf(arg->prompt, " Callback %s ", arg->cx->physical->type ==
1075                 PHYS_DIRECT ?  "accepted: " : "requested:");
1076   if (!arg->cx->cfg.callback.opmask)
1077     prompt_Printf(arg->prompt, "none\n");
1078   else {
1079     int comma = 0;
1080
1081     if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_NONE)) {
1082       prompt_Printf(arg->prompt, "none");
1083       comma = 1;
1084     }
1085     if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_AUTH)) {
1086       prompt_Printf(arg->prompt, "%sauth", comma ? ", " : "");
1087       comma = 1;
1088     }
1089     if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_E164)) {
1090       prompt_Printf(arg->prompt, "%sE.164", comma ? ", " : "");
1091       if (arg->cx->physical->type != PHYS_DIRECT)
1092         prompt_Printf(arg->prompt, " (%s)", arg->cx->cfg.callback.msg);
1093       comma = 1;
1094     }
1095     if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_CBCP)) {
1096       prompt_Printf(arg->prompt, "%scbcp\n", comma ? ", " : "");
1097       prompt_Printf(arg->prompt, " CBCP:               delay: %ds\n",
1098                     arg->cx->cfg.cbcp.delay);
1099       prompt_Printf(arg->prompt, "                     phone: ");
1100       if (!strcmp(arg->cx->cfg.cbcp.phone, "*")) {
1101         if (arg->cx->physical->type & PHYS_DIRECT)
1102           prompt_Printf(arg->prompt, "Caller decides\n");
1103         else
1104           prompt_Printf(arg->prompt, "Dialback server decides\n");
1105       } else
1106         prompt_Printf(arg->prompt, "%s\n", arg->cx->cfg.cbcp.phone);
1107       prompt_Printf(arg->prompt, "                     timeout: %lds\n",
1108                     arg->cx->cfg.cbcp.fsmretry);
1109     } else
1110       prompt_Printf(arg->prompt, "\n");
1111   }
1112
1113   prompt_Printf(arg->prompt, " Dial Script:        %s\n",
1114                 arg->cx->cfg.script.dial);
1115   prompt_Printf(arg->prompt, " Login Script:       %s\n",
1116                 arg->cx->cfg.script.login);
1117   prompt_Printf(arg->prompt, " Logout Script:      %s\n",
1118                 arg->cx->cfg.script.logout);
1119   prompt_Printf(arg->prompt, " Hangup Script:      %s\n",
1120                 arg->cx->cfg.script.hangup);
1121   return 0;
1122 }
1123
1124 int
1125 datalink_SetReconnect(struct cmdargs const *arg)
1126 {
1127   if (arg->argc == arg->argn+2) {
1128     arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]);
1129     arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]);
1130     return 0;
1131   }
1132   return -1;
1133 }
1134
1135 int
1136 datalink_SetRedial(struct cmdargs const *arg)
1137 {
1138   const char *sep, *osep;
1139   int timeout, inc, maxinc, tries;
1140
1141   if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) {
1142     if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 &&
1143         (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) {
1144       arg->cx->cfg.dial.timeout = -1;
1145       randinit();
1146     } else {
1147       timeout = atoi(arg->argv[arg->argn]);
1148
1149       if (timeout >= 0)
1150         arg->cx->cfg.dial.timeout = timeout;
1151       else {
1152         log_Printf(LogWARN, "Invalid redial timeout\n");
1153         return -1;
1154       }
1155     }
1156
1157     sep = strchr(arg->argv[arg->argn], '+');
1158     if (sep) {
1159       inc = atoi(++sep);
1160       osep = sep;
1161       if (inc >= 0)
1162         arg->cx->cfg.dial.inc = inc;
1163       else {
1164         log_Printf(LogWARN, "Invalid timeout increment\n");
1165         return -1;
1166       }
1167       sep = strchr(sep, '-');
1168       if (sep) {
1169         maxinc = atoi(++sep);
1170         if (maxinc >= 0)
1171           arg->cx->cfg.dial.maxinc = maxinc;
1172         else {
1173           log_Printf(LogWARN, "Invalid maximum timeout increments\n");
1174           return -1;
1175         }
1176       } else {
1177         /* Default timeout increment */
1178         arg->cx->cfg.dial.maxinc = 10;
1179         sep = osep;
1180       }
1181     } else {
1182       /* Default timeout increment & max increment */
1183       arg->cx->cfg.dial.inc = 0;
1184       arg->cx->cfg.dial.maxinc = 10;
1185       sep = arg->argv[arg->argn];
1186     }
1187
1188     sep = strchr(sep, '.');
1189     if (sep) {
1190       if (strcasecmp(++sep, "random") == 0) {
1191         arg->cx->cfg.dial.next_timeout = -1;
1192         randinit();
1193       } else {
1194         timeout = atoi(sep);
1195         if (timeout >= 0)
1196           arg->cx->cfg.dial.next_timeout = timeout;
1197         else {
1198           log_Printf(LogWARN, "Invalid next redial timeout\n");
1199           return -1;
1200         }
1201       }
1202     } else
1203       /* Default next timeout */
1204       arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
1205
1206     if (arg->argc == arg->argn+2) {
1207       tries = atoi(arg->argv[arg->argn+1]);
1208
1209       if (tries >= 0) {
1210         arg->cx->cfg.dial.max = tries;
1211       } else {
1212         log_Printf(LogWARN, "Invalid retry value\n");
1213         return 1;
1214       }
1215     }
1216     return 0;
1217   }
1218
1219   return -1;
1220 }
1221
1222 static const char * const states[] = {
1223   "closed",
1224   "opening",
1225   "hangup",
1226   "dial",
1227   "carrier",
1228   "logout",
1229   "login",
1230   "ready",
1231   "lcp",
1232   "auth",
1233   "cbcp",
1234   "open"
1235 };
1236
1237 const char *
1238 datalink_State(struct datalink *dl)
1239 {
1240   if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0])
1241     return "unknown";
1242   return states[dl->state];
1243 }
1244
1245 static void
1246 datalink_NewState(struct datalink *dl, int state)
1247 {
1248   if (state != dl->state) {
1249     if (state >= 0 && state < sizeof states / sizeof states[0]) {
1250       log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl),
1251                  states[state]);
1252       dl->state = state;
1253     } else
1254       log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state);
1255   }
1256 }
1257
1258 struct datalink *
1259 iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov,
1260              int fd, int *auxfd, int *nauxfd)
1261 {
1262   struct datalink *dl, *cdl;
1263   struct fsm_retry copy;
1264   char *oname;
1265
1266   dl = (struct datalink *)iov[(*niov)++].iov_base;
1267   dl->name = iov[*niov].iov_base;
1268
1269   if (dl->name[DATALINK_MAXNAME-1]) {
1270     dl->name[DATALINK_MAXNAME-1] = '\0';
1271     if (strlen(dl->name) == DATALINK_MAXNAME - 1)
1272       log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name);
1273   }
1274
1275   /* Make sure the name is unique ! */
1276   oname = NULL;
1277   do {
1278     for (cdl = bundle->links; cdl; cdl = cdl->next)
1279       if (!strcasecmp(dl->name, cdl->name)) {
1280         if (oname)
1281           free(datalink_NextName(dl));
1282         else
1283           oname = datalink_NextName(dl);
1284         break;  /* Keep renaming 'till we have no conflicts */
1285       }
1286   } while (cdl);
1287
1288   if (oname) {
1289     log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name);
1290     free(oname);
1291   } else {
1292     dl->name = strdup(dl->name);
1293     free(iov[*niov].iov_base);
1294   }
1295   (*niov)++;
1296
1297   dl->desc.type = DATALINK_DESCRIPTOR;
1298   dl->desc.UpdateSet = datalink_UpdateSet;
1299   dl->desc.IsSet = datalink_IsSet;
1300   dl->desc.Read = datalink_Read;
1301   dl->desc.Write = datalink_Write;
1302
1303   mp_linkInit(&dl->mp);
1304   *dl->phone.list = '\0';
1305   dl->phone.next = NULL;
1306   dl->phone.alt = NULL;
1307   dl->phone.chosen = "N/A";
1308
1309   dl->bundle = bundle;
1310   dl->next = NULL;
1311   memset(&dl->dial.timer, '\0', sizeof dl->dial.timer);
1312   dl->dial.tries = 0;
1313   dl->reconnect_tries = 0;
1314   dl->parent = &bundle->fsm;
1315   dl->fsmp.LayerStart = datalink_LayerStart;
1316   dl->fsmp.LayerUp = datalink_LayerUp;
1317   dl->fsmp.LayerDown = datalink_LayerDown;
1318   dl->fsmp.LayerFinish = datalink_LayerFinish;
1319   dl->fsmp.object = dl;
1320
1321   dl->physical = iov2physical(dl, iov, niov, maxiov, fd, auxfd, nauxfd);
1322
1323   if (!dl->physical) {
1324     free(dl->name);
1325     free(dl);
1326     dl = NULL;
1327   } else {
1328     copy = dl->pap.cfg.fsm;
1329     pap_Init(&dl->pap, dl->physical);
1330     dl->pap.cfg.fsm = copy;
1331
1332     copy = dl->chap.auth.cfg.fsm;
1333     chap_Init(&dl->chap, dl->physical);
1334     dl->chap.auth.cfg.fsm = copy;
1335
1336     cbcp_Init(&dl->cbcp, dl->physical);
1337
1338     memset(&dl->chat, '\0', sizeof dl->chat);   /* Force buf{start,end} reset */
1339     chat_Init(&dl->chat, dl->physical);
1340
1341     log_Printf(LogPHASE, "%s: Transferred in %s state\n",
1342               dl->name, datalink_State(dl));
1343   }
1344
1345   return dl;
1346 }
1347
1348 int
1349 datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
1350              int *auxfd, int *nauxfd)
1351 {
1352   /* If `dl' is NULL, we're allocating before a Fromiov() */
1353   int link_fd;
1354
1355   if (dl) {
1356     timer_Stop(&dl->dial.timer);
1357     /* The following is purely for the sake of paranoia */
1358     cbcp_Down(&dl->cbcp);
1359     timer_Stop(&dl->pap.authtimer);
1360     timer_Stop(&dl->chap.auth.authtimer);
1361   }
1362
1363   if (*niov >= maxiov - 1) {
1364     log_Printf(LogERROR, "Toiov: No room for datalink !\n");
1365     if (dl) {
1366       free(dl->name);
1367       free(dl);
1368     }
1369     return -1;
1370   }
1371
1372   iov[*niov].iov_base = (void *)dl;
1373   iov[(*niov)++].iov_len = sizeof *dl;
1374   iov[*niov].iov_base = dl ? realloc(dl->name, DATALINK_MAXNAME) : NULL;
1375   iov[(*niov)++].iov_len = DATALINK_MAXNAME;
1376
1377   link_fd = physical2iov(dl ? dl->physical : NULL, iov, niov, maxiov, auxfd,
1378                          nauxfd);
1379
1380   if (link_fd == -1 && dl) {
1381     free(dl->name);
1382     free(dl);
1383   }
1384
1385   return link_fd;
1386 }
1387
1388 void
1389 datalink_Rename(struct datalink *dl, const char *name)
1390 {
1391   free(dl->name);
1392   dl->physical->link.name = dl->name = strdup(name);
1393 }
1394
1395 char *
1396 datalink_NextName(struct datalink *dl)
1397 {
1398   int f, n;
1399   char *name, *oname;
1400
1401   n = strlen(dl->name);
1402   name = (char *)malloc(n+3);
1403   for (f = n - 1; f >= 0; f--)
1404     if (!isdigit(dl->name[f]))
1405       break;
1406   n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name);
1407   sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1);
1408   oname = dl->name;
1409   dl->name = name;
1410   /* our physical link name isn't updated (it probably isn't created yet) */
1411   return oname;
1412 }
1413
1414 int
1415 datalink_SetMode(struct datalink *dl, int mode)
1416 {
1417   if (!physical_SetMode(dl->physical, mode))
1418     return 0;
1419   if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
1420     dl->script.run = 0;
1421   if (dl->physical->type == PHYS_DIRECT)
1422     dl->reconnect_tries = 0;
1423   if (mode & (PHYS_DDIAL|PHYS_BACKGROUND|PHYS_FOREGROUND) &&
1424       dl->state <= DATALINK_READY)
1425     datalink_Up(dl, 1, 1);
1426   return 1;
1427 }
1428
1429 int
1430 datalink_GetDialTimeout(struct datalink *dl)
1431 {
1432   int result = dl->cfg.dial.timeout + dl->dial.incs * dl->cfg.dial.inc;
1433
1434   if (dl->dial.incs < dl->cfg.dial.maxinc)
1435     dl->dial.incs++;
1436
1437   return result;
1438 }