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