datalink.c revision 36285
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.1.2.63 1998/05/16 23:47:24 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 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) { 456 /* we've authenticated in multilink mode ! */ 457 switch (mp_Up(&dl->bundle->ncp.mp, dl)) { 458 case MP_LINKSENT: 459 /* We've handed the link off to another ppp (well, we will soon) ! */ 460 return; 461 case MP_UP: 462 /* First link in the bundle */ 463 auth_Select(dl->bundle, dl->peer.authname, dl->physical); 464 /* fall through */ 465 case MP_ADDED: 466 /* We're in multilink mode ! */ 467 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; 468 break; 469 case MP_FAILED: 470 datalink_AuthNotOk(dl); 471 return; 472 } 473 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) { 474 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name); 475 datalink_AuthNotOk(dl); 476 return; 477 } else { 478 dl->bundle->ncp.mp.peer = dl->peer; 479 ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link); 480 auth_Select(dl->bundle, dl->peer.authname, dl->physical); 481 } 482 483 fsm_Up(&dl->physical->link.ccp.fsm); 484 fsm_Open(&dl->physical->link.ccp.fsm); 485 datalink_NewState(dl, DATALINK_OPEN); 486 bundle_NewPhase(dl->bundle, PHASE_NETWORK); 487 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 488} 489 490void 491datalink_AuthNotOk(struct datalink *dl) 492{ 493 datalink_NewState(dl, DATALINK_LCP); 494 fsm_Close(&dl->physical->link.lcp.fsm); 495} 496 497static void 498datalink_LayerDown(void *v, struct fsm *fp) 499{ 500 /* The given FSM has been told to come down */ 501 struct datalink *dl = (struct datalink *)v; 502 503 if (fp->proto == PROTO_LCP) { 504 switch (dl->state) { 505 case DATALINK_OPEN: 506 peerid_Init(&dl->peer); 507 fsm_Down(&dl->physical->link.ccp.fsm); 508 fsm_Close(&dl->physical->link.ccp.fsm); 509 (*dl->parent->LayerDown)(dl->parent->object, fp); 510 /* fall through */ 511 512 case DATALINK_AUTH: 513 timer_Stop(&dl->pap.authtimer); 514 timer_Stop(&dl->chap.auth.authtimer); 515 } 516 datalink_NewState(dl, DATALINK_LCP); 517 } 518} 519 520static void 521datalink_LayerFinish(void *v, struct fsm *fp) 522{ 523 /* The given fsm is now down */ 524 struct datalink *dl = (struct datalink *)v; 525 526 if (fp->proto == PROTO_LCP) { 527 fsm_Down(fp); /* Bring us to INITIAL or STARTING */ 528 (*dl->parent->LayerFinish)(dl->parent->object, fp); 529 datalink_ComeDown(dl, 0); 530 } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 531 fsm_Open(fp); /* CCP goes to ST_STOPPED */ 532} 533 534struct datalink * 535datalink_Create(const char *name, struct bundle *bundle, int type) 536{ 537 struct datalink *dl; 538 539 dl = (struct datalink *)malloc(sizeof(struct datalink)); 540 if (dl == NULL) 541 return dl; 542 543 dl->desc.type = DATALINK_DESCRIPTOR; 544 dl->desc.next = NULL; 545 dl->desc.UpdateSet = datalink_UpdateSet; 546 dl->desc.IsSet = datalink_IsSet; 547 dl->desc.Read = datalink_Read; 548 dl->desc.Write = datalink_Write; 549 550 dl->state = DATALINK_CLOSED; 551 552 *dl->cfg.script.dial = '\0'; 553 *dl->cfg.script.login = '\0'; 554 *dl->cfg.script.hangup = '\0'; 555 *dl->cfg.phone.list = '\0'; 556 *dl->phone.list = '\0'; 557 dl->phone.next = NULL; 558 dl->phone.alt = NULL; 559 dl->phone.chosen = "N/A"; 560 dl->script.run = 1; 561 dl->script.packetmode = 1; 562 mp_linkInit(&dl->mp); 563 564 dl->bundle = bundle; 565 dl->next = NULL; 566 567 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer); 568 569 dl->dial_tries = 0; 570 dl->cfg.dial.max = 1; 571 dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 572 dl->cfg.dial.timeout = DIAL_TIMEOUT; 573 574 dl->reconnect_tries = 0; 575 dl->cfg.reconnect.max = 0; 576 dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT; 577 578 dl->name = strdup(name); 579 peerid_Init(&dl->peer); 580 dl->parent = &bundle->fsm; 581 dl->fsmp.LayerStart = datalink_LayerStart; 582 dl->fsmp.LayerUp = datalink_LayerUp; 583 dl->fsmp.LayerDown = datalink_LayerDown; 584 dl->fsmp.LayerFinish = datalink_LayerFinish; 585 dl->fsmp.object = dl; 586 587 auth_Init(&dl->pap); 588 auth_Init(&dl->chap.auth); 589 590 if ((dl->physical = modem_Create(dl, type)) == NULL) { 591 free(dl->name); 592 free(dl); 593 return NULL; 594 } 595 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 596 597 log_Printf(LogPHASE, "%s: Created in %s state\n", 598 dl->name, datalink_State(dl)); 599 600 return dl; 601} 602 603struct datalink * 604datalink_Clone(struct datalink *odl, const char *name) 605{ 606 struct datalink *dl; 607 608 dl = (struct datalink *)malloc(sizeof(struct datalink)); 609 if (dl == NULL) 610 return dl; 611 612 dl->desc.type = DATALINK_DESCRIPTOR; 613 dl->desc.next = NULL; 614 dl->desc.UpdateSet = datalink_UpdateSet; 615 dl->desc.IsSet = datalink_IsSet; 616 dl->desc.Read = datalink_Read; 617 dl->desc.Write = datalink_Write; 618 619 dl->state = DATALINK_CLOSED; 620 621 memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg); 622 mp_linkInit(&dl->mp); 623 *dl->phone.list = '\0'; 624 dl->phone.next = NULL; 625 dl->phone.alt = NULL; 626 dl->phone.chosen = "N/A"; 627 dl->bundle = odl->bundle; 628 dl->next = NULL; 629 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer); 630 dl->dial_tries = 0; 631 dl->reconnect_tries = 0; 632 dl->name = strdup(name); 633 peerid_Init(&dl->peer); 634 dl->parent = odl->parent; 635 memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp); 636 dl->fsmp.object = dl; 637 auth_Init(&dl->pap); 638 dl->pap.cfg.fsmretry = odl->pap.cfg.fsmretry; 639 640 auth_Init(&dl->chap.auth); 641 dl->chap.auth.cfg.fsmretry = odl->chap.auth.cfg.fsmretry; 642 643 if ((dl->physical = modem_Create(dl, PHYS_MANUAL)) == NULL) { 644 free(dl->name); 645 free(dl); 646 return NULL; 647 } 648 memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg); 649 memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg, 650 sizeof dl->physical->link.lcp.cfg); 651 memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg, 652 sizeof dl->physical->link.ccp.cfg); 653 memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg, 654 sizeof dl->physical->async.cfg); 655 656 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 657 658 log_Printf(LogPHASE, "%s: Cloned in %s state\n", 659 dl->name, datalink_State(dl)); 660 661 return dl; 662} 663 664struct datalink * 665datalink_Destroy(struct datalink *dl) 666{ 667 struct datalink *result; 668 669 if (dl->state != DATALINK_CLOSED) { 670 log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n", 671 datalink_State(dl)); 672 switch (dl->state) { 673 case DATALINK_HANGUP: 674 case DATALINK_DIAL: 675 case DATALINK_LOGIN: 676 chat_Destroy(&dl->chat); /* Gotta blat the timers ! */ 677 break; 678 } 679 } 680 681 result = dl->next; 682 modem_Destroy(dl->physical); 683 free(dl->name); 684 free(dl); 685 686 return result; 687} 688 689void 690datalink_Up(struct datalink *dl, int runscripts, int packetmode) 691{ 692 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 693 /* Ignore scripts */ 694 runscripts = 0; 695 696 switch (dl->state) { 697 case DATALINK_CLOSED: 698 if (bundle_Phase(dl->bundle) == PHASE_DEAD || 699 bundle_Phase(dl->bundle) == PHASE_TERMINATE) 700 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH); 701 datalink_NewState(dl, DATALINK_OPENING); 702 dl->reconnect_tries = 703 dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max; 704 dl->dial_tries = dl->cfg.dial.max; 705 dl->script.run = runscripts; 706 dl->script.packetmode = packetmode; 707 break; 708 709 case DATALINK_OPENING: 710 if (!dl->script.run && runscripts) 711 dl->script.run = 1; 712 /* fall through */ 713 714 case DATALINK_DIAL: 715 case DATALINK_LOGIN: 716 case DATALINK_READY: 717 if (!dl->script.packetmode && packetmode) { 718 dl->script.packetmode = 1; 719 if (dl->state == DATALINK_READY) 720 datalink_LoginDone(dl); 721 } 722 break; 723 } 724} 725 726void 727datalink_Close(struct datalink *dl, int stay) 728{ 729 /* Please close */ 730 switch (dl->state) { 731 case DATALINK_OPEN: 732 peerid_Init(&dl->peer); 733 fsm_Down(&dl->physical->link.ccp.fsm); 734 fsm_Close(&dl->physical->link.ccp.fsm); 735 /* fall through */ 736 737 case DATALINK_AUTH: 738 case DATALINK_LCP: 739 fsm_Close(&dl->physical->link.lcp.fsm); 740 if (stay) { 741 dl->dial_tries = -1; 742 dl->reconnect_tries = 0; 743 } 744 break; 745 746 default: 747 datalink_ComeDown(dl, stay); 748 } 749} 750 751void 752datalink_Down(struct datalink *dl, int stay) 753{ 754 /* Carrier is lost */ 755 switch (dl->state) { 756 case DATALINK_OPEN: 757 peerid_Init(&dl->peer); 758 fsm_Down(&dl->physical->link.ccp.fsm); 759 fsm_Close(&dl->physical->link.ccp.fsm); 760 /* fall through */ 761 762 case DATALINK_AUTH: 763 case DATALINK_LCP: 764 fsm_Down(&dl->physical->link.lcp.fsm); 765 if (stay) 766 fsm_Close(&dl->physical->link.lcp.fsm); 767 else 768 fsm_Open(&dl->physical->link.ccp.fsm); 769 /* fall through */ 770 771 default: 772 datalink_ComeDown(dl, stay); 773 } 774} 775 776void 777datalink_StayDown(struct datalink *dl) 778{ 779 dl->reconnect_tries = 0; 780} 781 782int 783datalink_Show(struct cmdargs const *arg) 784{ 785 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name); 786 prompt_Printf(arg->prompt, " State: %s\n", 787 datalink_State(arg->cx)); 788 prompt_Printf(arg->prompt, " CHAP Encryption: %s\n", 789 arg->cx->chap.using_MSChap ? "MSChap" : "MD5" ); 790 prompt_Printf(arg->prompt, " Peer name: "); 791 if (*arg->cx->peer.authname) 792 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname); 793 else if (arg->cx->state == DATALINK_OPEN) 794 prompt_Printf(arg->prompt, "None requested\n"); 795 else 796 prompt_Printf(arg->prompt, "N/A\n"); 797 prompt_Printf(arg->prompt, " Discriminator: %s\n", 798 mp_Enddisc(arg->cx->peer.enddisc.class, 799 arg->cx->peer.enddisc.address, 800 arg->cx->peer.enddisc.len)); 801 802 prompt_Printf(arg->prompt, "\nDefaults:\n"); 803 prompt_Printf(arg->prompt, " Phone List: %s\n", 804 arg->cx->cfg.phone.list); 805 if (arg->cx->cfg.dial.max) 806 prompt_Printf(arg->prompt, " Dial tries: %d, delay ", 807 arg->cx->cfg.dial.max); 808 else 809 prompt_Printf(arg->prompt, " Dial tries: infinite, delay "); 810 if (arg->cx->cfg.dial.next_timeout > 0) 811 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout); 812 else 813 prompt_Printf(arg->prompt, "random/"); 814 if (arg->cx->cfg.dial.timeout > 0) 815 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout); 816 else 817 prompt_Printf(arg->prompt, "random\n"); 818 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ", 819 arg->cx->cfg.reconnect.max); 820 if (arg->cx->cfg.reconnect.timeout > 0) 821 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout); 822 else 823 prompt_Printf(arg->prompt, "random\n"); 824 prompt_Printf(arg->prompt, " Dial Script: %s\n", 825 arg->cx->cfg.script.dial); 826 prompt_Printf(arg->prompt, " Login Script: %s\n", 827 arg->cx->cfg.script.login); 828 prompt_Printf(arg->prompt, " Hangup Script: %s\n", 829 arg->cx->cfg.script.hangup); 830 return 0; 831} 832 833int 834datalink_SetReconnect(struct cmdargs const *arg) 835{ 836 if (arg->argc == arg->argn+2) { 837 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]); 838 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]); 839 return 0; 840 } 841 return -1; 842} 843 844int 845datalink_SetRedial(struct cmdargs const *arg) 846{ 847 int timeout; 848 int tries; 849 char *dot; 850 851 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) { 852 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 && 853 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) { 854 arg->cx->cfg.dial.timeout = -1; 855 randinit(); 856 } else { 857 timeout = atoi(arg->argv[arg->argn]); 858 859 if (timeout >= 0) 860 arg->cx->cfg.dial.timeout = timeout; 861 else { 862 log_Printf(LogWARN, "Invalid redial timeout\n"); 863 return -1; 864 } 865 } 866 867 dot = strchr(arg->argv[arg->argn], '.'); 868 if (dot) { 869 if (strcasecmp(++dot, "random") == 0) { 870 arg->cx->cfg.dial.next_timeout = -1; 871 randinit(); 872 } else { 873 timeout = atoi(dot); 874 if (timeout >= 0) 875 arg->cx->cfg.dial.next_timeout = timeout; 876 else { 877 log_Printf(LogWARN, "Invalid next redial timeout\n"); 878 return -1; 879 } 880 } 881 } else 882 /* Default next timeout */ 883 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 884 885 if (arg->argc == arg->argn+2) { 886 tries = atoi(arg->argv[arg->argn+1]); 887 888 if (tries >= 0) { 889 arg->cx->cfg.dial.max = tries; 890 } else { 891 log_Printf(LogWARN, "Invalid retry value\n"); 892 return 1; 893 } 894 } 895 return 0; 896 } 897 return -1; 898} 899 900static const char *states[] = { 901 "closed", 902 "opening", 903 "hangup", 904 "dial", 905 "login", 906 "ready", 907 "lcp", 908 "auth", 909 "open" 910}; 911 912const char * 913datalink_State(struct datalink *dl) 914{ 915 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0]) 916 return "unknown"; 917 return states[dl->state]; 918} 919 920static void 921datalink_NewState(struct datalink *dl, int state) 922{ 923 if (state != dl->state) { 924 if (state >= 0 && state < sizeof states / sizeof states[0]) { 925 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl), 926 states[state]); 927 dl->state = state; 928 } else 929 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state); 930 } 931} 932 933struct datalink * 934iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov, 935 int fd) 936{ 937 struct datalink *dl, *cdl; 938 u_int retry; 939 char *oname; 940 941 dl = (struct datalink *)iov[(*niov)++].iov_base; 942 dl->name = iov[*niov].iov_base; 943 944 if (dl->name[DATALINK_MAXNAME-1]) { 945 dl->name[DATALINK_MAXNAME-1] = '\0'; 946 if (strlen(dl->name) == DATALINK_MAXNAME - 1) 947 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name); 948 } 949 950 /* Make sure the name is unique ! */ 951 oname = NULL; 952 do { 953 for (cdl = bundle->links; cdl; cdl = cdl->next) 954 if (!strcasecmp(dl->name, cdl->name)) { 955 if (oname) 956 free(datalink_NextName(dl)); 957 else 958 oname = datalink_NextName(dl); 959 break; /* Keep renaming 'till we have no conflicts */ 960 } 961 } while (cdl); 962 963 if (oname) { 964 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name); 965 free(oname); 966 } else { 967 dl->name = strdup(dl->name); 968 dl->physical->link.name = dl->name; 969 free(iov[*niov].iov_base); 970 } 971 (*niov)++; 972 973 dl->desc.type = DATALINK_DESCRIPTOR; 974 dl->desc.next = NULL; 975 dl->desc.UpdateSet = datalink_UpdateSet; 976 dl->desc.IsSet = datalink_IsSet; 977 dl->desc.Read = datalink_Read; 978 dl->desc.Write = datalink_Write; 979 980 mp_linkInit(&dl->mp); 981 *dl->phone.list = '\0'; 982 dl->phone.next = NULL; 983 dl->phone.alt = NULL; 984 dl->phone.chosen = "N/A"; 985 986 dl->bundle = bundle; 987 dl->next = NULL; 988 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer); 989 dl->dial_tries = 0; 990 dl->reconnect_tries = 0; 991 dl->parent = &bundle->fsm; 992 dl->fsmp.LayerStart = datalink_LayerStart; 993 dl->fsmp.LayerUp = datalink_LayerUp; 994 dl->fsmp.LayerDown = datalink_LayerDown; 995 dl->fsmp.LayerFinish = datalink_LayerFinish; 996 dl->fsmp.object = dl; 997 998 retry = dl->pap.cfg.fsmretry; 999 auth_Init(&dl->pap); 1000 dl->pap.cfg.fsmretry = retry; 1001 1002 retry = dl->chap.auth.cfg.fsmretry; 1003 auth_Init(&dl->chap.auth); 1004 dl->chap.auth.cfg.fsmretry = retry; 1005 1006 dl->physical = iov2modem(dl, iov, niov, maxiov, fd); 1007 1008 if (!dl->physical) { 1009 free(dl->name); 1010 free(dl); 1011 dl = NULL; 1012 } else { 1013 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 1014 1015 log_Printf(LogPHASE, "%s: Transferred in %s state\n", 1016 dl->name, datalink_State(dl)); 1017 } 1018 1019 return dl; 1020} 1021 1022int 1023datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov) 1024{ 1025 /* If `dl' is NULL, we're allocating before a Fromiov() */ 1026 int link_fd; 1027 1028 if (dl) { 1029 timer_Stop(&dl->dial_timer); 1030 timer_Stop(&dl->pap.authtimer); 1031 timer_Stop(&dl->chap.auth.authtimer); 1032 } 1033 1034 if (*niov >= maxiov - 1) { 1035 log_Printf(LogERROR, "Toiov: No room for datalink !\n"); 1036 if (dl) { 1037 free(dl->name); 1038 free(dl); 1039 } 1040 return -1; 1041 } 1042 1043 iov[*niov].iov_base = dl ? dl : malloc(sizeof *dl); 1044 iov[(*niov)++].iov_len = sizeof *dl; 1045 iov[*niov].iov_base = 1046 dl ? realloc(dl->name, DATALINK_MAXNAME) : malloc(DATALINK_MAXNAME); 1047 iov[(*niov)++].iov_len = DATALINK_MAXNAME; 1048 1049 link_fd = modem2iov(dl ? dl->physical : NULL, iov, niov, maxiov); 1050 1051 if (link_fd == -1 && dl) { 1052 free(dl->name); 1053 free(dl); 1054 } 1055 1056 return link_fd; 1057} 1058 1059void 1060datalink_Rename(struct datalink *dl, const char *name) 1061{ 1062 free(dl->name); 1063 dl->physical->link.name = dl->name = strdup(name); 1064} 1065 1066char * 1067datalink_NextName(struct datalink *dl) 1068{ 1069 int f, n; 1070 char *name, *oname; 1071 1072 n = strlen(dl->name); 1073 name = (char *)malloc(n+3); 1074 for (f = n - 1; f >= 0; f--) 1075 if (!isdigit(dl->name[f])) 1076 break; 1077 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name); 1078 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1); 1079 oname = dl->name; 1080 dl->physical->link.name = dl->name = name; 1081 return oname; 1082} 1083 1084int 1085datalink_SetMode(struct datalink *dl, int mode) 1086{ 1087 if (!physical_SetMode(dl->physical, mode)) 1088 return 0; 1089 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 1090 dl->script.run = 0; 1091 if (dl->physical->type == PHYS_DIRECT) 1092 dl->reconnect_tries = 0; 1093 if (mode & (PHYS_PERM|PHYS_1OFF) && dl->state <= DATALINK_READY) 1094 datalink_Up(dl, 1, 1); 1095 return 1; 1096} 1097