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