datalink.c revision 37012
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.9 1998/06/15 19:05:15 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 how) 393{ 394 if (how != CLOSE_NORMAL) { 395 dl->dial_tries = -1; 396 dl->reconnect_tries = 0; 397 if (dl->state >= DATALINK_READY && how == CLOSE_LCP) 398 dl->stayonline = 1; 399 } 400 401 if (dl->state >= DATALINK_READY && dl->stayonline) { 402 dl->stayonline = 0; 403 datalink_NewState(dl, DATALINK_READY); 404 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 405 modem_Offline(dl->physical); 406 if (dl->script.run && dl->state != DATALINK_OPENING) { 407 datalink_NewState(dl, DATALINK_HANGUP); 408 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL); 409 } else 410 datalink_HangupDone(dl); 411 } 412} 413 414static void 415datalink_LayerStart(void *v, struct fsm *fp) 416{ 417 /* The given FSM is about to start up ! */ 418 struct datalink *dl = (struct datalink *)v; 419 420 if (fp->proto == PROTO_LCP) 421 (*dl->parent->LayerStart)(dl->parent->object, fp); 422} 423 424static void 425datalink_LayerUp(void *v, struct fsm *fp) 426{ 427 /* The given fsm is now up */ 428 struct datalink *dl = (struct datalink *)v; 429 430 if (fp->proto == PROTO_LCP) { 431 datalink_GotAuthname(dl, "", 0); 432 dl->physical->link.lcp.auth_ineed = dl->physical->link.lcp.want_auth; 433 dl->physical->link.lcp.auth_iwait = dl->physical->link.lcp.his_auth; 434 if (dl->physical->link.lcp.his_auth || dl->physical->link.lcp.want_auth) { 435 if (bundle_Phase(dl->bundle) == PHASE_ESTABLISH) 436 bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE); 437 log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name, 438 Auth2Nam(dl->physical->link.lcp.his_auth), 439 Auth2Nam(dl->physical->link.lcp.want_auth)); 440 if (dl->physical->link.lcp.his_auth == PROTO_PAP) 441 auth_StartChallenge(&dl->pap, dl->physical, pap_SendChallenge); 442 if (dl->physical->link.lcp.want_auth == PROTO_CHAP) 443 auth_StartChallenge(&dl->chap.auth, dl->physical, chap_SendChallenge); 444 } else 445 datalink_AuthOk(dl); 446 } 447} 448 449void 450datalink_GotAuthname(struct datalink *dl, const char *name, int len) 451{ 452 if (len >= sizeof dl->peer.authname) 453 len = sizeof dl->peer.authname - 1; 454 strncpy(dl->peer.authname, name, len); 455 dl->peer.authname[len] = '\0'; 456} 457 458void 459datalink_AuthOk(struct datalink *dl) 460{ 461 ccp_SetOpenMode(&dl->physical->link.ccp); 462 463 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) { 464 /* we've authenticated in multilink mode ! */ 465 switch (mp_Up(&dl->bundle->ncp.mp, dl)) { 466 case MP_LINKSENT: 467 /* We've handed the link off to another ppp (well, we will soon) ! */ 468 return; 469 case MP_UP: 470 /* First link in the bundle */ 471 auth_Select(dl->bundle, dl->peer.authname, dl->physical); 472 /* fall through */ 473 case MP_ADDED: 474 /* We're in multilink mode ! */ 475 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; /* override */ 476 break; 477 case MP_FAILED: 478 datalink_AuthNotOk(dl); 479 return; 480 } 481 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) { 482 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name); 483 datalink_AuthNotOk(dl); 484 return; 485 } else { 486 dl->bundle->ncp.mp.peer = dl->peer; 487 ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link); 488 auth_Select(dl->bundle, dl->peer.authname, dl->physical); 489 } 490 491 fsm_Up(&dl->physical->link.ccp.fsm); 492 fsm_Open(&dl->physical->link.ccp.fsm); 493 datalink_NewState(dl, DATALINK_OPEN); 494 bundle_NewPhase(dl->bundle, PHASE_NETWORK); 495 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 496} 497 498void 499datalink_AuthNotOk(struct datalink *dl) 500{ 501 datalink_NewState(dl, DATALINK_LCP); 502 fsm_Close(&dl->physical->link.lcp.fsm); 503} 504 505static void 506datalink_LayerDown(void *v, struct fsm *fp) 507{ 508 /* The given FSM has been told to come down */ 509 struct datalink *dl = (struct datalink *)v; 510 511 if (fp->proto == PROTO_LCP) { 512 switch (dl->state) { 513 case DATALINK_OPEN: 514 peerid_Init(&dl->peer); 515 fsm_Down(&dl->physical->link.ccp.fsm); 516 fsm_Close(&dl->physical->link.ccp.fsm); 517 datalink_NewState(dl, DATALINK_LCP); /* before parent TLD */ 518 (*dl->parent->LayerDown)(dl->parent->object, fp); 519 /* fall through */ 520 521 case DATALINK_AUTH: 522 timer_Stop(&dl->pap.authtimer); 523 timer_Stop(&dl->chap.auth.authtimer); 524 } 525 datalink_NewState(dl, DATALINK_LCP); 526 } 527} 528 529static void 530datalink_LayerFinish(void *v, struct fsm *fp) 531{ 532 /* The given fsm is now down */ 533 struct datalink *dl = (struct datalink *)v; 534 535 if (fp->proto == PROTO_LCP) { 536 if (fp->state == ST_STOPPED) 537 fsm_Close(fp); /* back to CLOSED */ 538 fsm_Down(fp); /* Bring us to INITIAL or STARTING */ 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 fsm_Down(&dl->physical->link.ccp.fsm); 744 fsm_Close(&dl->physical->link.ccp.fsm); 745 /* fall through */ 746 747 case DATALINK_AUTH: 748 case DATALINK_LCP: 749 fsm_Close(&dl->physical->link.lcp.fsm); 750 if (how != CLOSE_NORMAL) { 751 dl->dial_tries = -1; 752 dl->reconnect_tries = 0; 753 if (how == CLOSE_LCP) 754 dl->stayonline = 1; 755 } 756 break; 757 758 default: 759 datalink_ComeDown(dl, how); 760 } 761} 762 763void 764datalink_Down(struct datalink *dl, int how) 765{ 766 /* Carrier is lost */ 767 switch (dl->state) { 768 case DATALINK_OPEN: 769 peerid_Init(&dl->peer); 770 fsm_Down(&dl->physical->link.ccp.fsm); 771 fsm_Close(&dl->physical->link.ccp.fsm); 772 /* fall through */ 773 774 case DATALINK_AUTH: 775 case DATALINK_LCP: 776 if (dl->physical->link.lcp.fsm.state == ST_STOPPED) 777 fsm_Close(&dl->physical->link.lcp.fsm); /* back to CLOSED */ 778 fsm_Down(&dl->physical->link.lcp.fsm); 779 if (how != CLOSE_NORMAL) 780 fsm_Close(&dl->physical->link.lcp.fsm); 781 else 782 fsm_Open(&dl->physical->link.ccp.fsm); 783 /* fall through */ 784 785 default: 786 datalink_ComeDown(dl, how); 787 } 788} 789 790void 791datalink_StayDown(struct datalink *dl) 792{ 793 dl->reconnect_tries = 0; 794} 795 796void 797datalink_DontHangup(struct datalink *dl) 798{ 799 if (dl->state >= DATALINK_LCP) 800 dl->stayonline = 1; 801} 802 803int 804datalink_Show(struct cmdargs const *arg) 805{ 806 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name); 807 prompt_Printf(arg->prompt, " State: %s\n", 808 datalink_State(arg->cx)); 809 prompt_Printf(arg->prompt, " CHAP Encryption: %s\n", 810 arg->cx->chap.using_MSChap ? "MSChap" : "MD5" ); 811 prompt_Printf(arg->prompt, " Peer name: "); 812 if (*arg->cx->peer.authname) 813 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname); 814 else if (arg->cx->state == DATALINK_OPEN) 815 prompt_Printf(arg->prompt, "None requested\n"); 816 else 817 prompt_Printf(arg->prompt, "N/A\n"); 818 prompt_Printf(arg->prompt, " Discriminator: %s\n", 819 mp_Enddisc(arg->cx->peer.enddisc.class, 820 arg->cx->peer.enddisc.address, 821 arg->cx->peer.enddisc.len)); 822 823 prompt_Printf(arg->prompt, "\nDefaults:\n"); 824 prompt_Printf(arg->prompt, " Phone List: %s\n", 825 arg->cx->cfg.phone.list); 826 if (arg->cx->cfg.dial.max) 827 prompt_Printf(arg->prompt, " Dial tries: %d, delay ", 828 arg->cx->cfg.dial.max); 829 else 830 prompt_Printf(arg->prompt, " Dial tries: infinite, delay "); 831 if (arg->cx->cfg.dial.next_timeout > 0) 832 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout); 833 else 834 prompt_Printf(arg->prompt, "random/"); 835 if (arg->cx->cfg.dial.timeout > 0) 836 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout); 837 else 838 prompt_Printf(arg->prompt, "random\n"); 839 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ", 840 arg->cx->cfg.reconnect.max); 841 if (arg->cx->cfg.reconnect.timeout > 0) 842 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout); 843 else 844 prompt_Printf(arg->prompt, "random\n"); 845 prompt_Printf(arg->prompt, " Dial Script: %s\n", 846 arg->cx->cfg.script.dial); 847 prompt_Printf(arg->prompt, " Login Script: %s\n", 848 arg->cx->cfg.script.login); 849 prompt_Printf(arg->prompt, " Hangup Script: %s\n", 850 arg->cx->cfg.script.hangup); 851 return 0; 852} 853 854int 855datalink_SetReconnect(struct cmdargs const *arg) 856{ 857 if (arg->argc == arg->argn+2) { 858 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]); 859 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]); 860 return 0; 861 } 862 return -1; 863} 864 865int 866datalink_SetRedial(struct cmdargs const *arg) 867{ 868 int timeout; 869 int tries; 870 char *dot; 871 872 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) { 873 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 && 874 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) { 875 arg->cx->cfg.dial.timeout = -1; 876 randinit(); 877 } else { 878 timeout = atoi(arg->argv[arg->argn]); 879 880 if (timeout >= 0) 881 arg->cx->cfg.dial.timeout = timeout; 882 else { 883 log_Printf(LogWARN, "Invalid redial timeout\n"); 884 return -1; 885 } 886 } 887 888 dot = strchr(arg->argv[arg->argn], '.'); 889 if (dot) { 890 if (strcasecmp(++dot, "random") == 0) { 891 arg->cx->cfg.dial.next_timeout = -1; 892 randinit(); 893 } else { 894 timeout = atoi(dot); 895 if (timeout >= 0) 896 arg->cx->cfg.dial.next_timeout = timeout; 897 else { 898 log_Printf(LogWARN, "Invalid next redial timeout\n"); 899 return -1; 900 } 901 } 902 } else 903 /* Default next timeout */ 904 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 905 906 if (arg->argc == arg->argn+2) { 907 tries = atoi(arg->argv[arg->argn+1]); 908 909 if (tries >= 0) { 910 arg->cx->cfg.dial.max = tries; 911 } else { 912 log_Printf(LogWARN, "Invalid retry value\n"); 913 return 1; 914 } 915 } 916 return 0; 917 } 918 return -1; 919} 920 921static const char *states[] = { 922 "closed", 923 "opening", 924 "hangup", 925 "dial", 926 "login", 927 "ready", 928 "lcp", 929 "auth", 930 "open" 931}; 932 933const char * 934datalink_State(struct datalink *dl) 935{ 936 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0]) 937 return "unknown"; 938 return states[dl->state]; 939} 940 941static void 942datalink_NewState(struct datalink *dl, int state) 943{ 944 if (state != dl->state) { 945 if (state >= 0 && state < sizeof states / sizeof states[0]) { 946 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl), 947 states[state]); 948 dl->state = state; 949 } else 950 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state); 951 } 952} 953 954struct datalink * 955iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov, 956 int fd) 957{ 958 struct datalink *dl, *cdl; 959 u_int retry; 960 char *oname; 961 962 dl = (struct datalink *)iov[(*niov)++].iov_base; 963 dl->name = iov[*niov].iov_base; 964 965 if (dl->name[DATALINK_MAXNAME-1]) { 966 dl->name[DATALINK_MAXNAME-1] = '\0'; 967 if (strlen(dl->name) == DATALINK_MAXNAME - 1) 968 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name); 969 } 970 971 /* Make sure the name is unique ! */ 972 oname = NULL; 973 do { 974 for (cdl = bundle->links; cdl; cdl = cdl->next) 975 if (!strcasecmp(dl->name, cdl->name)) { 976 if (oname) 977 free(datalink_NextName(dl)); 978 else 979 oname = datalink_NextName(dl); 980 break; /* Keep renaming 'till we have no conflicts */ 981 } 982 } while (cdl); 983 984 if (oname) { 985 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name); 986 free(oname); 987 } else { 988 dl->name = strdup(dl->name); 989 free(iov[*niov].iov_base); 990 } 991 (*niov)++; 992 993 dl->desc.type = DATALINK_DESCRIPTOR; 994 dl->desc.UpdateSet = datalink_UpdateSet; 995 dl->desc.IsSet = datalink_IsSet; 996 dl->desc.Read = datalink_Read; 997 dl->desc.Write = datalink_Write; 998 999 mp_linkInit(&dl->mp); 1000 *dl->phone.list = '\0'; 1001 dl->phone.next = NULL; 1002 dl->phone.alt = NULL; 1003 dl->phone.chosen = "N/A"; 1004 1005 dl->bundle = bundle; 1006 dl->next = NULL; 1007 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer); 1008 dl->dial_tries = 0; 1009 dl->reconnect_tries = 0; 1010 dl->parent = &bundle->fsm; 1011 dl->fsmp.LayerStart = datalink_LayerStart; 1012 dl->fsmp.LayerUp = datalink_LayerUp; 1013 dl->fsmp.LayerDown = datalink_LayerDown; 1014 dl->fsmp.LayerFinish = datalink_LayerFinish; 1015 dl->fsmp.object = dl; 1016 1017 retry = dl->pap.cfg.fsmretry; 1018 auth_Init(&dl->pap); 1019 dl->pap.cfg.fsmretry = retry; 1020 1021 retry = dl->chap.auth.cfg.fsmretry; 1022 auth_Init(&dl->chap.auth); 1023 dl->chap.auth.cfg.fsmretry = retry; 1024 1025 dl->physical = iov2modem(dl, iov, niov, maxiov, fd); 1026 1027 if (!dl->physical) { 1028 free(dl->name); 1029 free(dl); 1030 dl = NULL; 1031 } else { 1032 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 1033 1034 log_Printf(LogPHASE, "%s: Transferred in %s state\n", 1035 dl->name, datalink_State(dl)); 1036 } 1037 1038 return dl; 1039} 1040 1041int 1042datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 1043 pid_t newpid) 1044{ 1045 /* If `dl' is NULL, we're allocating before a Fromiov() */ 1046 int link_fd; 1047 1048 if (dl) { 1049 timer_Stop(&dl->dial_timer); 1050 timer_Stop(&dl->pap.authtimer); 1051 timer_Stop(&dl->chap.auth.authtimer); 1052 } 1053 1054 if (*niov >= maxiov - 1) { 1055 log_Printf(LogERROR, "Toiov: No room for datalink !\n"); 1056 if (dl) { 1057 free(dl->name); 1058 free(dl); 1059 } 1060 return -1; 1061 } 1062 1063 iov[*niov].iov_base = dl ? dl : malloc(sizeof *dl); 1064 iov[(*niov)++].iov_len = sizeof *dl; 1065 iov[*niov].iov_base = 1066 dl ? realloc(dl->name, DATALINK_MAXNAME) : malloc(DATALINK_MAXNAME); 1067 iov[(*niov)++].iov_len = DATALINK_MAXNAME; 1068 1069 link_fd = modem2iov(dl ? dl->physical : NULL, iov, niov, maxiov, newpid); 1070 1071 if (link_fd == -1 && dl) { 1072 free(dl->name); 1073 free(dl); 1074 } 1075 1076 return link_fd; 1077} 1078 1079void 1080datalink_Rename(struct datalink *dl, const char *name) 1081{ 1082 free(dl->name); 1083 dl->physical->link.name = dl->name = strdup(name); 1084} 1085 1086char * 1087datalink_NextName(struct datalink *dl) 1088{ 1089 int f, n; 1090 char *name, *oname; 1091 1092 n = strlen(dl->name); 1093 name = (char *)malloc(n+3); 1094 for (f = n - 1; f >= 0; f--) 1095 if (!isdigit(dl->name[f])) 1096 break; 1097 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name); 1098 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1); 1099 oname = dl->name; 1100 dl->name = name; 1101 /* our physical link name isn't updated (it probably isn't created yet) */ 1102 return oname; 1103} 1104 1105int 1106datalink_SetMode(struct datalink *dl, int mode) 1107{ 1108 if (!physical_SetMode(dl->physical, mode)) 1109 return 0; 1110 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 1111 dl->script.run = 0; 1112 if (dl->physical->type == PHYS_DIRECT) 1113 dl->reconnect_tries = 0; 1114 if (mode & (PHYS_DDIAL|PHYS_BACKGROUND) && dl->state <= DATALINK_READY) 1115 datalink_Up(dl, 1, 1); 1116 return 1; 1117} 1118