Deleted Added
sdiff udiff text old ( 36314 ) new ( 36345 )
full compact
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 * $Id: datalink.c,v 1.4 1998/05/23 22:24:33 brian Exp $
27 */
28
29#include <sys/types.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#include <unistd.h>
42
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#include "bundle.h"
63#include "chat.h"
64#include "auth.h"
65#include "modem.h"
66#include "prompt.h"
67#include "lcpproto.h"
68#include "pap.h"
69#include "chap.h"
70#include "command.h"
71#include "datalink.h"
72
73static void datalink_LoginDone(struct datalink *);
74static void datalink_NewState(struct datalink *, int);
75
76static void
77datalink_OpenTimeout(void *v)
78{
79 struct datalink *dl = (struct datalink *)v;
80
81 timer_Stop(&dl->dial_timer);
82 if (dl->state == DATALINK_OPENING)
83 log_Printf(LogPHASE, "%s: Redial timer expired.\n", dl->name);
84}
85
86static void
87datalink_StartDialTimer(struct datalink *dl, int Timeout)
88{
89 timer_Stop(&dl->dial_timer);
90
91 if (Timeout) {
92 if (Timeout > 0)
93 dl->dial_timer.load = Timeout * SECTICKS;
94 else
95 dl->dial_timer.load = (random() % DIAL_TIMEOUT) * SECTICKS;
96 dl->dial_timer.func = datalink_OpenTimeout;
97 dl->dial_timer.name = "dial";
98 dl->dial_timer.arg = dl;
99 timer_Start(&dl->dial_timer);
100 if (dl->state == DATALINK_OPENING)
101 log_Printf(LogPHASE, "%s: Enter pause (%d) for redialing.\n",
102 dl->name, Timeout);
103 }
104}
105
106static void
107datalink_HangupDone(struct datalink *dl)
108{
109 if (dl->physical->type == PHYS_DEDICATED && !dl->bundle->CleaningUp &&
110 physical_GetFD(dl->physical) != -1) {
111 /* Don't close our modem if the link is dedicated */
112 datalink_LoginDone(dl);
113 return;
114 }
115
116 modem_Close(dl->physical);
117 dl->phone.chosen = "N/A";
118
119 if (dl->bundle->CleaningUp ||
120 (dl->physical->type == PHYS_DIRECT) ||
121 ((!dl->dial_tries || (dl->dial_tries < 0 && !dl->reconnect_tries)) &&
122 !(dl->physical->type & (PHYS_PERM|PHYS_DEDICATED)))) {
123 datalink_NewState(dl, DATALINK_CLOSED);
124 dl->dial_tries = -1;
125 dl->reconnect_tries = 0;
126 bundle_LinkClosed(dl->bundle, dl);
127 if (!dl->bundle->CleaningUp)
128 datalink_StartDialTimer(dl, dl->cfg.dial.timeout);
129 } else {
130 datalink_NewState(dl, DATALINK_OPENING);
131 if (dl->dial_tries < 0) {
132 datalink_StartDialTimer(dl, dl->cfg.reconnect.timeout);
133 dl->dial_tries = dl->cfg.dial.max;
134 dl->reconnect_tries--;
135 } else {
136 if (dl->phone.next == NULL)
137 datalink_StartDialTimer(dl, dl->cfg.dial.timeout);
138 else
139 datalink_StartDialTimer(dl, dl->cfg.dial.next_timeout);
140 }
141 }
142}
143
144static const char *
145datalink_ChoosePhoneNumber(struct datalink *dl)
146{
147 char *phone;
148
149 if (dl->phone.alt == NULL) {
150 if (dl->phone.next == NULL) {
151 strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1);
152 dl->phone.list[sizeof dl->phone.list - 1] = '\0';
153 dl->phone.next = dl->phone.list;
154 }
155 dl->phone.alt = strsep(&dl->phone.next, ":");
156 }
157 phone = strsep(&dl->phone.alt, "|");
158 dl->phone.chosen = *phone ? phone : "[NONE]";
159 if (*phone)
160 log_Printf(LogPHASE, "Phone: %s\n", phone);
161 return phone;
162}
163
164static void
165datalink_LoginDone(struct datalink *dl)
166{
167 if (!dl->script.packetmode) {
168 dl->dial_tries = -1;
169 datalink_NewState(dl, DATALINK_READY);
170 } else if (modem_Raw(dl->physical, dl->bundle) < 0) {
171 dl->dial_tries = 0;
172 log_Printf(LogWARN, "datalink_LoginDone: Not connected.\n");
173 if (dl->script.run) {
174 datalink_NewState(dl, DATALINK_HANGUP);
175 modem_Offline(dl->physical);
176 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
177 } else {
178 if (dl->physical->type == PHYS_DEDICATED)
179 /* force a redial timeout */
180 modem_Close(dl->physical);
181 datalink_HangupDone(dl);
182 }
183 } else {
184 dl->dial_tries = -1;
185
186 hdlc_Init(&dl->physical->hdlc, &dl->physical->link.lcp);
187 async_Init(&dl->physical->async);
188
189 lcp_Setup(&dl->physical->link.lcp, dl->state == DATALINK_READY ?
190 0 : dl->physical->link.lcp.cfg.openmode);
191 ccp_Setup(&dl->physical->link.ccp);
192
193 datalink_NewState(dl, DATALINK_LCP);
194 fsm_Up(&dl->physical->link.lcp.fsm);
195 fsm_Open(&dl->physical->link.lcp.fsm);
196 }
197}
198
199static int
200datalink_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
201 int *n)
202{
203 struct datalink *dl = descriptor2datalink(d);
204 int result;
205
206 result = 0;
207 switch (dl->state) {
208 case DATALINK_CLOSED:
209 if ((dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED|PHYS_1OFF)) &&
210 !bundle_IsDead(dl->bundle))
211 /*
212 * Our first time in - DEDICATED never comes down, and STDIN & 1OFF
213 * get deleted when they enter DATALINK_CLOSED. Go to
214 * DATALINK_OPENING via datalink_Up() and fall through.
215 */
216 datalink_Up(dl, 1, 1);
217 else
218 break;
219 /* fall through */
220
221 case DATALINK_OPENING:
222 if (dl->dial_timer.state != TIMER_RUNNING) {
223 if (--dl->dial_tries < 0)
224 dl->dial_tries = 0;
225 if (modem_Open(dl->physical, dl->bundle) >= 0) {
226 if (dl->script.run) {
227 datalink_NewState(dl, DATALINK_DIAL);
228 chat_Init(&dl->chat, dl->physical, dl->cfg.script.dial, 1,
229 datalink_ChoosePhoneNumber(dl));
230 if (!(dl->physical->type & (PHYS_PERM|PHYS_DEDICATED)) &&
231 dl->cfg.dial.max)
232 log_Printf(LogCHAT, "%s: Dial attempt %u of %d\n",
233 dl->name, dl->cfg.dial.max - dl->dial_tries,
234 dl->cfg.dial.max);
235 return datalink_UpdateSet(d, r, w, e, n);
236 } else
237 datalink_LoginDone(dl);
238 } else {
239 if (!(dl->physical->type & (PHYS_PERM|PHYS_DEDICATED)) &&
240 dl->cfg.dial.max)
241 log_Printf(LogCHAT, "Failed to open modem (attempt %u of %d)\n",
242 dl->cfg.dial.max - dl->dial_tries, dl->cfg.dial.max);
243 else
244 log_Printf(LogCHAT, "Failed to open modem\n");
245
246 if (dl->bundle->CleaningUp ||
247 (!(dl->physical->type & (PHYS_PERM|PHYS_DEDICATED)) &&
248 dl->cfg.dial.max && dl->dial_tries == 0)) {
249 datalink_NewState(dl, DATALINK_CLOSED);
250 dl->reconnect_tries = 0;
251 dl->dial_tries = -1;
252 bundle_LinkClosed(dl->bundle, dl);
253 }
254 if (!dl->bundle->CleaningUp)
255 datalink_StartDialTimer(dl, dl->cfg.dial.timeout);
256 }
257 }
258 break;
259
260 case DATALINK_HANGUP:
261 case DATALINK_DIAL:
262 case DATALINK_LOGIN:
263 result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n);
264 switch (dl->chat.state) {
265 case CHAT_DONE:
266 /* script succeeded */
267 chat_Destroy(&dl->chat);
268 switch(dl->state) {
269 case DATALINK_HANGUP:
270 datalink_HangupDone(dl);
271 break;
272 case DATALINK_DIAL:
273 datalink_NewState(dl, DATALINK_LOGIN);
274 chat_Init(&dl->chat, dl->physical, dl->cfg.script.login, 0, NULL);
275 return datalink_UpdateSet(d, r, w, e, n);
276 case DATALINK_LOGIN:
277 datalink_LoginDone(dl);
278 break;
279 }
280 break;
281 case CHAT_FAILED:
282 /* Going down - script failed */
283 log_Printf(LogWARN, "Chat script failed\n");
284 chat_Destroy(&dl->chat);
285 switch(dl->state) {
286 case DATALINK_HANGUP:
287 datalink_HangupDone(dl);
288 break;
289 case DATALINK_DIAL:
290 case DATALINK_LOGIN:
291 datalink_NewState(dl, DATALINK_HANGUP);
292 modem_Offline(dl->physical);
293 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
294 return datalink_UpdateSet(d, r, w, e, n);
295 }
296 break;
297 }
298 break;
299
300 case DATALINK_READY:
301 case DATALINK_LCP:
302 case DATALINK_AUTH:
303 case DATALINK_OPEN:
304 result = descriptor_UpdateSet(&dl->physical->desc, r, w, e, n);
305 break;
306 }
307 return result;
308}
309
310int
311datalink_RemoveFromSet(struct datalink *dl, fd_set *r, fd_set *w, fd_set *e)
312{
313 return physical_RemoveFromSet(dl->physical, r, w, e);
314}
315
316static int
317datalink_IsSet(struct descriptor *d, const fd_set *fdset)
318{
319 struct datalink *dl = descriptor2datalink(d);
320
321 switch (dl->state) {
322 case DATALINK_CLOSED:
323 case DATALINK_OPENING:
324 break;
325
326 case DATALINK_HANGUP:
327 case DATALINK_DIAL:
328 case DATALINK_LOGIN:
329 return descriptor_IsSet(&dl->chat.desc, fdset);
330
331 case DATALINK_READY:
332 case DATALINK_LCP:
333 case DATALINK_AUTH:
334 case DATALINK_OPEN:
335 return descriptor_IsSet(&dl->physical->desc, fdset);
336 }
337 return 0;
338}
339
340static void
341datalink_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
342{
343 struct datalink *dl = descriptor2datalink(d);
344
345 switch (dl->state) {
346 case DATALINK_CLOSED:
347 case DATALINK_OPENING:
348 break;
349
350 case DATALINK_HANGUP:
351 case DATALINK_DIAL:
352 case DATALINK_LOGIN:
353 descriptor_Read(&dl->chat.desc, bundle, fdset);
354 break;
355
356 case DATALINK_READY:
357 case DATALINK_LCP:
358 case DATALINK_AUTH:
359 case DATALINK_OPEN:
360 descriptor_Read(&dl->physical->desc, bundle, fdset);
361 break;
362 }
363}
364
365static void
366datalink_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
367{
368 struct datalink *dl = descriptor2datalink(d);
369
370 switch (dl->state) {
371 case DATALINK_CLOSED:
372 case DATALINK_OPENING:
373 break;
374
375 case DATALINK_HANGUP:
376 case DATALINK_DIAL:
377 case DATALINK_LOGIN:
378 descriptor_Write(&dl->chat.desc, bundle, fdset);
379 break;
380
381 case DATALINK_READY:
382 case DATALINK_LCP:
383 case DATALINK_AUTH:
384 case DATALINK_OPEN:
385 descriptor_Write(&dl->physical->desc, bundle, fdset);
386 break;
387 }
388}
389
390static void
391datalink_ComeDown(struct datalink *dl, int stay)
392{
393 if (stay) {
394 dl->dial_tries = -1;
395 dl->reconnect_tries = 0;
396 }
397
398 if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) {
399 modem_Offline(dl->physical);
400 if (dl->script.run && dl->state != DATALINK_OPENING) {
401 datalink_NewState(dl, DATALINK_HANGUP);
402 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
403 } else
404 datalink_HangupDone(dl);
405 }
406}
407
408static void
409datalink_LayerStart(void *v, struct fsm *fp)
410{
411 /* The given FSM is about to start up ! */
412 struct datalink *dl = (struct datalink *)v;
413
414 if (fp->proto == PROTO_LCP)
415 (*dl->parent->LayerStart)(dl->parent->object, fp);
416}
417
418static void
419datalink_LayerUp(void *v, struct fsm *fp)
420{
421 /* The given fsm is now up */
422 struct datalink *dl = (struct datalink *)v;
423
424 if (fp->proto == PROTO_LCP) {
425 datalink_GotAuthname(dl, "", 0);
426 dl->physical->link.lcp.auth_ineed = dl->physical->link.lcp.want_auth;
427 dl->physical->link.lcp.auth_iwait = dl->physical->link.lcp.his_auth;
428 if (dl->physical->link.lcp.his_auth || dl->physical->link.lcp.want_auth) {
429 if (bundle_Phase(dl->bundle) == PHASE_ESTABLISH)
430 bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE);
431 log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name,
432 Auth2Nam(dl->physical->link.lcp.his_auth),
433 Auth2Nam(dl->physical->link.lcp.want_auth));
434 if (dl->physical->link.lcp.his_auth == PROTO_PAP)
435 auth_StartChallenge(&dl->pap, dl->physical, pap_SendChallenge);
436 if (dl->physical->link.lcp.want_auth == PROTO_CHAP)
437 auth_StartChallenge(&dl->chap.auth, dl->physical, chap_SendChallenge);
438 } else
439 datalink_AuthOk(dl);
440 }
441}
442
443void
444datalink_GotAuthname(struct datalink *dl, const char *name, int len)
445{
446 if (len >= sizeof dl->peer.authname)
447 len = sizeof dl->peer.authname - 1;
448 strncpy(dl->peer.authname, name, len);
449 dl->peer.authname[len] = '\0';
450}
451
452void
453datalink_AuthOk(struct datalink *dl)
454{
455 ccp_SetOpenMode(&dl->physical->link.ccp);
456
457 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) {
458 /* we've authenticated in multilink mode ! */
459 switch (mp_Up(&dl->bundle->ncp.mp, dl)) {
460 case MP_LINKSENT:
461 /* We've handed the link off to another ppp (well, we will soon) ! */
462 return;
463 case MP_UP:
464 /* First link in the bundle */
465 auth_Select(dl->bundle, dl->peer.authname, dl->physical);
466 /* fall through */
467 case MP_ADDED:
468 /* We're in multilink mode ! */
469 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; /* override */
470 break;
471 case MP_FAILED:
472 datalink_AuthNotOk(dl);
473 return;
474 }
475 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) {
476 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name);
477 datalink_AuthNotOk(dl);
478 return;
479 } else {
480 dl->bundle->ncp.mp.peer = dl->peer;
481 ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link);
482 auth_Select(dl->bundle, dl->peer.authname, dl->physical);
483 }
484
485 fsm_Up(&dl->physical->link.ccp.fsm);
486 fsm_Open(&dl->physical->link.ccp.fsm);
487 datalink_NewState(dl, DATALINK_OPEN);
488 bundle_NewPhase(dl->bundle, PHASE_NETWORK);
489 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm);
490}
491
492void
493datalink_AuthNotOk(struct datalink *dl)
494{
495 datalink_NewState(dl, DATALINK_LCP);
496 fsm_Close(&dl->physical->link.lcp.fsm);
497}
498
499static void
500datalink_LayerDown(void *v, struct fsm *fp)
501{
502 /* The given FSM has been told to come down */
503 struct datalink *dl = (struct datalink *)v;
504
505 if (fp->proto == PROTO_LCP) {
506 switch (dl->state) {
507 case DATALINK_OPEN:
508 peerid_Init(&dl->peer);
509 fsm_Down(&dl->physical->link.ccp.fsm);
510 fsm_Close(&dl->physical->link.ccp.fsm);
511 (*dl->parent->LayerDown)(dl->parent->object, fp);
512 /* fall through */
513
514 case DATALINK_AUTH:
515 timer_Stop(&dl->pap.authtimer);
516 timer_Stop(&dl->chap.auth.authtimer);
517 }
518 datalink_NewState(dl, DATALINK_LCP);
519 }
520}
521
522static void
523datalink_LayerFinish(void *v, struct fsm *fp)
524{
525 /* The given fsm is now down */
526 struct datalink *dl = (struct datalink *)v;
527
528 if (fp->proto == PROTO_LCP) {
529 if (fp->state == ST_STOPPED)
530 fsm_Close(fp); /* back to CLOSED */
531 fsm_Down(fp); /* Bring us to INITIAL or STARTING */
532 (*dl->parent->LayerFinish)(dl->parent->object, fp);
533 datalink_ComeDown(dl, 0);
534 } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
535 fsm_Open(fp); /* CCP goes to ST_STOPPED */
536}
537
538struct datalink *
539datalink_Create(const char *name, struct bundle *bundle, int type)
540{
541 struct datalink *dl;
542
543 dl = (struct datalink *)malloc(sizeof(struct datalink));
544 if (dl == NULL)
545 return dl;
546
547 dl->desc.type = DATALINK_DESCRIPTOR;
548 dl->desc.UpdateSet = datalink_UpdateSet;
549 dl->desc.IsSet = datalink_IsSet;
550 dl->desc.Read = datalink_Read;
551 dl->desc.Write = datalink_Write;
552
553 dl->state = DATALINK_CLOSED;
554
555 *dl->cfg.script.dial = '\0';
556 *dl->cfg.script.login = '\0';
557 *dl->cfg.script.hangup = '\0';
558 *dl->cfg.phone.list = '\0';
559 *dl->phone.list = '\0';
560 dl->phone.next = NULL;
561 dl->phone.alt = NULL;
562 dl->phone.chosen = "N/A";
563 dl->script.run = 1;
564 dl->script.packetmode = 1;
565 mp_linkInit(&dl->mp);
566
567 dl->bundle = bundle;
568 dl->next = NULL;
569
570 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer);
571
572 dl->dial_tries = 0;
573 dl->cfg.dial.max = 1;
574 dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
575 dl->cfg.dial.timeout = DIAL_TIMEOUT;
576
577 dl->reconnect_tries = 0;
578 dl->cfg.reconnect.max = 0;
579 dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT;
580
581 dl->name = strdup(name);
582 peerid_Init(&dl->peer);
583 dl->parent = &bundle->fsm;
584 dl->fsmp.LayerStart = datalink_LayerStart;
585 dl->fsmp.LayerUp = datalink_LayerUp;
586 dl->fsmp.LayerDown = datalink_LayerDown;
587 dl->fsmp.LayerFinish = datalink_LayerFinish;
588 dl->fsmp.object = dl;
589
590 auth_Init(&dl->pap);
591 auth_Init(&dl->chap.auth);
592
593 if ((dl->physical = modem_Create(dl, type)) == NULL) {
594 free(dl->name);
595 free(dl);
596 return NULL;
597 }
598 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL);
599
600 log_Printf(LogPHASE, "%s: Created in %s state\n",
601 dl->name, datalink_State(dl));
602
603 return dl;
604}
605
606struct datalink *
607datalink_Clone(struct datalink *odl, const char *name)
608{
609 struct datalink *dl;
610
611 dl = (struct datalink *)malloc(sizeof(struct datalink));
612 if (dl == NULL)
613 return dl;
614
615 dl->desc.type = DATALINK_DESCRIPTOR;
616 dl->desc.UpdateSet = datalink_UpdateSet;
617 dl->desc.IsSet = datalink_IsSet;
618 dl->desc.Read = datalink_Read;
619 dl->desc.Write = datalink_Write;
620
621 dl->state = DATALINK_CLOSED;
622
623 memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg);
624 mp_linkInit(&dl->mp);
625 *dl->phone.list = '\0';
626 dl->phone.next = NULL;
627 dl->phone.alt = NULL;
628 dl->phone.chosen = "N/A";
629 dl->bundle = odl->bundle;
630 dl->next = NULL;
631 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer);
632 dl->dial_tries = 0;
633 dl->reconnect_tries = 0;
634 dl->name = strdup(name);
635 peerid_Init(&dl->peer);
636 dl->parent = odl->parent;
637 memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp);
638 dl->fsmp.object = dl;
639 auth_Init(&dl->pap);
640 dl->pap.cfg.fsmretry = odl->pap.cfg.fsmretry;
641
642 auth_Init(&dl->chap.auth);
643 dl->chap.auth.cfg.fsmretry = odl->chap.auth.cfg.fsmretry;
644
645 if ((dl->physical = modem_Create(dl, PHYS_MANUAL)) == NULL) {
646 free(dl->name);
647 free(dl);
648 return NULL;
649 }
650 memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg);
651 memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg,
652 sizeof dl->physical->link.lcp.cfg);
653 memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg,
654 sizeof dl->physical->link.ccp.cfg);
655 memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg,
656 sizeof dl->physical->async.cfg);
657
658 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL);
659
660 log_Printf(LogPHASE, "%s: Cloned in %s state\n",
661 dl->name, datalink_State(dl));
662
663 return dl;
664}
665
666struct datalink *
667datalink_Destroy(struct datalink *dl)
668{
669 struct datalink *result;
670
671 if (dl->state != DATALINK_CLOSED) {
672 log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n",
673 datalink_State(dl));
674 switch (dl->state) {
675 case DATALINK_HANGUP:
676 case DATALINK_DIAL:
677 case DATALINK_LOGIN:
678 chat_Destroy(&dl->chat); /* Gotta blat the timers ! */
679 break;
680 }
681 }
682
683 result = dl->next;
684 modem_Destroy(dl->physical);
685 free(dl->name);
686 free(dl);
687
688 return result;
689}
690
691void
692datalink_Up(struct datalink *dl, int runscripts, int packetmode)
693{
694 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
695 /* Ignore scripts */
696 runscripts = 0;
697
698 switch (dl->state) {
699 case DATALINK_CLOSED:
700 if (bundle_Phase(dl->bundle) == PHASE_DEAD ||
701 bundle_Phase(dl->bundle) == PHASE_TERMINATE)
702 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH);
703 datalink_NewState(dl, DATALINK_OPENING);
704 dl->reconnect_tries =
705 dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max;
706 dl->dial_tries = dl->cfg.dial.max;
707 dl->script.run = runscripts;
708 dl->script.packetmode = packetmode;
709 break;
710
711 case DATALINK_OPENING:
712 if (!dl->script.run && runscripts)
713 dl->script.run = 1;
714 /* fall through */
715
716 case DATALINK_DIAL:
717 case DATALINK_LOGIN:
718 case DATALINK_READY:
719 if (!dl->script.packetmode && packetmode) {
720 dl->script.packetmode = 1;
721 if (dl->state == DATALINK_READY)
722 datalink_LoginDone(dl);
723 }
724 break;
725 }
726}
727
728void
729datalink_Close(struct datalink *dl, int stay)
730{
731 /* Please close */
732 switch (dl->state) {
733 case DATALINK_OPEN:
734 peerid_Init(&dl->peer);
735 fsm_Down(&dl->physical->link.ccp.fsm);
736 fsm_Close(&dl->physical->link.ccp.fsm);
737 /* fall through */
738
739 case DATALINK_AUTH:
740 case DATALINK_LCP:
741 fsm_Close(&dl->physical->link.lcp.fsm);
742 if (stay) {
743 dl->dial_tries = -1;
744 dl->reconnect_tries = 0;
745 }
746 break;
747
748 default:
749 datalink_ComeDown(dl, stay);
750 }
751}
752
753void
754datalink_Down(struct datalink *dl, int stay)
755{
756 /* Carrier is lost */
757 switch (dl->state) {
758 case DATALINK_OPEN:
759 peerid_Init(&dl->peer);
760 fsm_Down(&dl->physical->link.ccp.fsm);
761 fsm_Close(&dl->physical->link.ccp.fsm);
762 /* fall through */
763
764 case DATALINK_AUTH:
765 case DATALINK_LCP:
766 if (dl->physical->link.lcp.fsm.state == ST_STOPPED)
767 fsm_Close(&dl->physical->link.lcp.fsm); /* back to CLOSED */
768 fsm_Down(&dl->physical->link.lcp.fsm);
769 if (stay)
770 fsm_Close(&dl->physical->link.lcp.fsm);
771 else
772 fsm_Open(&dl->physical->link.ccp.fsm);
773 /* fall through */
774
775 default:
776 datalink_ComeDown(dl, stay);
777 }
778}
779
780void
781datalink_StayDown(struct datalink *dl)
782{
783 dl->reconnect_tries = 0;
784}
785
786int
787datalink_Show(struct cmdargs const *arg)
788{
789 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name);
790 prompt_Printf(arg->prompt, " State: %s\n",
791 datalink_State(arg->cx));
792 prompt_Printf(arg->prompt, " CHAP Encryption: %s\n",
793 arg->cx->chap.using_MSChap ? "MSChap" : "MD5" );
794 prompt_Printf(arg->prompt, " Peer name: ");
795 if (*arg->cx->peer.authname)
796 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname);
797 else if (arg->cx->state == DATALINK_OPEN)
798 prompt_Printf(arg->prompt, "None requested\n");
799 else
800 prompt_Printf(arg->prompt, "N/A\n");
801 prompt_Printf(arg->prompt, " Discriminator: %s\n",
802 mp_Enddisc(arg->cx->peer.enddisc.class,
803 arg->cx->peer.enddisc.address,
804 arg->cx->peer.enddisc.len));
805
806 prompt_Printf(arg->prompt, "\nDefaults:\n");
807 prompt_Printf(arg->prompt, " Phone List: %s\n",
808 arg->cx->cfg.phone.list);
809 if (arg->cx->cfg.dial.max)
810 prompt_Printf(arg->prompt, " Dial tries: %d, delay ",
811 arg->cx->cfg.dial.max);
812 else
813 prompt_Printf(arg->prompt, " Dial tries: infinite, delay ");
814 if (arg->cx->cfg.dial.next_timeout > 0)
815 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout);
816 else
817 prompt_Printf(arg->prompt, "random/");
818 if (arg->cx->cfg.dial.timeout > 0)
819 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout);
820 else
821 prompt_Printf(arg->prompt, "random\n");
822 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ",
823 arg->cx->cfg.reconnect.max);
824 if (arg->cx->cfg.reconnect.timeout > 0)
825 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout);
826 else
827 prompt_Printf(arg->prompt, "random\n");
828 prompt_Printf(arg->prompt, " Dial Script: %s\n",
829 arg->cx->cfg.script.dial);
830 prompt_Printf(arg->prompt, " Login Script: %s\n",
831 arg->cx->cfg.script.login);
832 prompt_Printf(arg->prompt, " Hangup Script: %s\n",
833 arg->cx->cfg.script.hangup);
834 return 0;
835}
836
837int
838datalink_SetReconnect(struct cmdargs const *arg)
839{
840 if (arg->argc == arg->argn+2) {
841 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]);
842 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]);
843 return 0;
844 }
845 return -1;
846}
847
848int
849datalink_SetRedial(struct cmdargs const *arg)
850{
851 int timeout;
852 int tries;
853 char *dot;
854
855 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) {
856 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 &&
857 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) {
858 arg->cx->cfg.dial.timeout = -1;
859 randinit();
860 } else {
861 timeout = atoi(arg->argv[arg->argn]);
862
863 if (timeout >= 0)
864 arg->cx->cfg.dial.timeout = timeout;
865 else {
866 log_Printf(LogWARN, "Invalid redial timeout\n");
867 return -1;
868 }
869 }
870
871 dot = strchr(arg->argv[arg->argn], '.');
872 if (dot) {
873 if (strcasecmp(++dot, "random") == 0) {
874 arg->cx->cfg.dial.next_timeout = -1;
875 randinit();
876 } else {
877 timeout = atoi(dot);
878 if (timeout >= 0)
879 arg->cx->cfg.dial.next_timeout = timeout;
880 else {
881 log_Printf(LogWARN, "Invalid next redial timeout\n");
882 return -1;
883 }
884 }
885 } else
886 /* Default next timeout */
887 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT;
888
889 if (arg->argc == arg->argn+2) {
890 tries = atoi(arg->argv[arg->argn+1]);
891
892 if (tries >= 0) {
893 arg->cx->cfg.dial.max = tries;
894 } else {
895 log_Printf(LogWARN, "Invalid retry value\n");
896 return 1;
897 }
898 }
899 return 0;
900 }
901 return -1;
902}
903
904static const char *states[] = {
905 "closed",
906 "opening",
907 "hangup",
908 "dial",
909 "login",
910 "ready",
911 "lcp",
912 "auth",
913 "open"
914};
915
916const char *
917datalink_State(struct datalink *dl)
918{
919 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0])
920 return "unknown";
921 return states[dl->state];
922}
923
924static void
925datalink_NewState(struct datalink *dl, int state)
926{
927 if (state != dl->state) {
928 if (state >= 0 && state < sizeof states / sizeof states[0]) {
929 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl),
930 states[state]);
931 dl->state = state;
932 } else
933 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state);
934 }
935}
936
937struct datalink *
938iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov,
939 int fd)
940{
941 struct datalink *dl, *cdl;
942 u_int retry;
943 char *oname;
944
945 dl = (struct datalink *)iov[(*niov)++].iov_base;
946 dl->name = iov[*niov].iov_base;
947
948 if (dl->name[DATALINK_MAXNAME-1]) {
949 dl->name[DATALINK_MAXNAME-1] = '\0';
950 if (strlen(dl->name) == DATALINK_MAXNAME - 1)
951 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name);
952 }
953
954 /* Make sure the name is unique ! */
955 oname = NULL;
956 do {
957 for (cdl = bundle->links; cdl; cdl = cdl->next)
958 if (!strcasecmp(dl->name, cdl->name)) {
959 if (oname)
960 free(datalink_NextName(dl));
961 else
962 oname = datalink_NextName(dl);
963 break; /* Keep renaming 'till we have no conflicts */
964 }
965 } while (cdl);
966
967 if (oname) {
968 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name);
969 free(oname);
970 } else {
971 dl->name = strdup(dl->name);
972 free(iov[*niov].iov_base);
973 }
974 (*niov)++;
975
976 dl->desc.type = DATALINK_DESCRIPTOR;
977 dl->desc.UpdateSet = datalink_UpdateSet;
978 dl->desc.IsSet = datalink_IsSet;
979 dl->desc.Read = datalink_Read;
980 dl->desc.Write = datalink_Write;
981
982 mp_linkInit(&dl->mp);
983 *dl->phone.list = '\0';
984 dl->phone.next = NULL;
985 dl->phone.alt = NULL;
986 dl->phone.chosen = "N/A";
987
988 dl->bundle = bundle;
989 dl->next = NULL;
990 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer);
991 dl->dial_tries = 0;
992 dl->reconnect_tries = 0;
993 dl->parent = &bundle->fsm;
994 dl->fsmp.LayerStart = datalink_LayerStart;
995 dl->fsmp.LayerUp = datalink_LayerUp;
996 dl->fsmp.LayerDown = datalink_LayerDown;
997 dl->fsmp.LayerFinish = datalink_LayerFinish;
998 dl->fsmp.object = dl;
999
1000 retry = dl->pap.cfg.fsmretry;
1001 auth_Init(&dl->pap);
1002 dl->pap.cfg.fsmretry = retry;
1003
1004 retry = dl->chap.auth.cfg.fsmretry;
1005 auth_Init(&dl->chap.auth);
1006 dl->chap.auth.cfg.fsmretry = retry;
1007
1008 dl->physical = iov2modem(dl, iov, niov, maxiov, fd);
1009
1010 if (!dl->physical) {
1011 free(dl->name);
1012 free(dl);
1013 dl = NULL;
1014 } else {
1015 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL);
1016
1017 log_Printf(LogPHASE, "%s: Transferred in %s state\n",
1018 dl->name, datalink_State(dl));
1019 }
1020
1021 return dl;
1022}
1023
1024int
1025datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov)
1026{
1027 /* If `dl' is NULL, we're allocating before a Fromiov() */
1028 int link_fd;
1029
1030 if (dl) {
1031 timer_Stop(&dl->dial_timer);
1032 timer_Stop(&dl->pap.authtimer);
1033 timer_Stop(&dl->chap.auth.authtimer);
1034 }
1035
1036 if (*niov >= maxiov - 1) {
1037 log_Printf(LogERROR, "Toiov: No room for datalink !\n");
1038 if (dl) {
1039 free(dl->name);
1040 free(dl);
1041 }
1042 return -1;
1043 }
1044
1045 iov[*niov].iov_base = dl ? dl : malloc(sizeof *dl);
1046 iov[(*niov)++].iov_len = sizeof *dl;
1047 iov[*niov].iov_base =
1048 dl ? realloc(dl->name, DATALINK_MAXNAME) : malloc(DATALINK_MAXNAME);
1049 iov[(*niov)++].iov_len = DATALINK_MAXNAME;
1050
1051 link_fd = modem2iov(dl ? dl->physical : NULL, iov, niov, maxiov);
1052
1053 if (link_fd == -1 && dl) {
1054 free(dl->name);
1055 free(dl);
1056 }
1057
1058 return link_fd;
1059}
1060
1061void
1062datalink_Rename(struct datalink *dl, const char *name)
1063{
1064 free(dl->name);
1065 dl->physical->link.name = dl->name = strdup(name);
1066}
1067
1068char *
1069datalink_NextName(struct datalink *dl)
1070{
1071 int f, n;
1072 char *name, *oname;
1073
1074 n = strlen(dl->name);
1075 name = (char *)malloc(n+3);
1076 for (f = n - 1; f >= 0; f--)
1077 if (!isdigit(dl->name[f]))
1078 break;
1079 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name);
1080 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1);
1081 oname = dl->name;
1082 dl->name = name;
1083 /* our physical link name isn't updated (it probably isn't created yet) */
1084 return oname;
1085}
1086
1087int
1088datalink_SetMode(struct datalink *dl, int mode)
1089{
1090 if (!physical_SetMode(dl->physical, mode))
1091 return 0;
1092 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED))
1093 dl->script.run = 0;
1094 if (dl->physical->type == PHYS_DIRECT)
1095 dl->reconnect_tries = 0;
1096 if (mode & (PHYS_PERM|PHYS_1OFF) && dl->state <= DATALINK_READY)
1097 datalink_Up(dl, 1, 1);
1098 return 1;
1099}