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