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 * $FreeBSD$ 27 */ 28 29 #include <sys/param.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 "layer.h" 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 #ifndef NORADIUS 63 #include "radius.h" 64 #endif 65 #include "bundle.h" 66 #include "chat.h" 67 #include "auth.h" 68 #include "prompt.h" 69 #include "proto.h" 70 #include "pap.h" 71 #include "chap.h" 72 #include "command.h" 73 #include "cbcp.h" 74 #include "datalink.h" 75 76 static void datalink_LoginDone(struct datalink *); 77 static void datalink_NewState(struct datalink *, int); 78 79 static void 80 datalink_OpenTimeout(void *v) 81 { 82 struct datalink *dl = (struct datalink *)v; 83 84 timer_Stop(&dl->dial.timer); 85 if (dl->state == DATALINK_OPENING) 86 log_Printf(LogCHAT, "%s: Redial timer expired.\n", dl->name); 87 } 88 89 static int 90 datalink_StartDialTimer(struct datalink *dl, int Timeout) 91 { 92 int result = Timeout; 93 94 timer_Stop(&dl->dial.timer); 95 if (Timeout) { 96 if (Timeout > 0) 97 dl->dial.timer.load = Timeout * SECTICKS; 98 else { 99 result = (random() % DIAL_TIMEOUT) + 1; 100 dl->dial.timer.load = result * SECTICKS; 101 } 102 dl->dial.timer.func = datalink_OpenTimeout; 103 dl->dial.timer.name = "dial"; 104 dl->dial.timer.arg = dl; 105 timer_Start(&dl->dial.timer); 106 if (dl->state == DATALINK_OPENING) 107 log_Printf(LogPHASE, "%s: Enter pause (%d) for redialing.\n", 108 dl->name, Timeout); 109 } 110 return result; 111 } 112 113 static void 114 datalink_HangupDone(struct datalink *dl) 115 { 116 if (dl->physical->type == PHYS_DEDICATED && !dl->bundle->CleaningUp && 117 dl->physical->fd != -1) { 118 /* Don't close our device if the link is dedicated */ 119 datalink_LoginDone(dl); 120 return; 121 } 122 123 chat_Finish(&dl->chat); 124 physical_Close(dl->physical); 125 dl->phone.chosen = "N/A"; 126 127 if (dl->cbcp.required) { 128 log_Printf(LogPHASE, "Call peer back on %s\n", dl->cbcp.fsm.phone); 129 dl->cfg.callback.opmask = 0; 130 strncpy(dl->cfg.phone.list, dl->cbcp.fsm.phone, 131 sizeof dl->cfg.phone.list - 1); 132 dl->cfg.phone.list[sizeof dl->cfg.phone.list - 1] = '\0'; 133 dl->phone.alt = dl->phone.next = NULL; 134 dl->reconnect_tries = dl->cfg.reconnect.max; 135 dl->dial.tries = dl->cfg.dial.max; 136 dl->dial.incs = 0; 137 dl->script.run = 1; 138 dl->script.packetmode = 1; 139 if (!physical_SetMode(dl->physical, PHYS_BACKGROUND)) 140 log_Printf(LogERROR, "Oops - can't change mode to BACKGROUND (gulp) !\n"); 141 bundle_LinksRemoved(dl->bundle); 142 /* if dial.timeout is < 0 (random), we don't override fsm.delay */ 143 if (dl->cbcp.fsm.delay < dl->cfg.dial.timeout) 144 dl->cbcp.fsm.delay = dl->cfg.dial.timeout; 145 datalink_StartDialTimer(dl, dl->cbcp.fsm.delay); 146 cbcp_Down(&dl->cbcp); 147 datalink_NewState(dl, DATALINK_OPENING); 148 if (bundle_Phase(dl->bundle) == PHASE_DEAD || 149 bundle_Phase(dl->bundle) == PHASE_TERMINATE) 150 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH); 151 } else if (dl->bundle->CleaningUp || 152 (dl->physical->type == PHYS_DIRECT) || 153 ((!dl->dial.tries || (dl->dial.tries < 0 && !dl->reconnect_tries)) && 154 !(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)))) { 155 datalink_NewState(dl, DATALINK_CLOSED); 156 dl->dial.tries = -1; 157 dl->dial.incs = 0; 158 dl->reconnect_tries = 0; 159 bundle_LinkClosed(dl->bundle, dl); 160 if (!dl->bundle->CleaningUp && 161 !(dl->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND))) 162 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 163 } else { 164 datalink_NewState(dl, DATALINK_OPENING); 165 if (bundle_Phase(dl->bundle) == PHASE_DEAD || 166 bundle_Phase(dl->bundle) == PHASE_TERMINATE) 167 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH); 168 if (dl->dial.tries < 0) { 169 datalink_StartDialTimer(dl, dl->cfg.reconnect.timeout); 170 dl->dial.tries = dl->cfg.dial.max; 171 dl->dial.incs = 0; 172 dl->reconnect_tries--; 173 log_Printf(LogCHAT, "%s: Reconnect try %d of %d\n", 174 dl->name, dl->cfg.reconnect.max - dl->reconnect_tries, 175 dl->cfg.reconnect.max); 176 bundle_Notify(dl->bundle, EX_RECONNECT); 177 } else { 178 if (dl->phone.next == NULL) 179 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 180 else 181 datalink_StartDialTimer(dl, dl->cfg.dial.next_timeout); 182 bundle_Notify(dl->bundle, EX_REDIAL); 183 } 184 } 185 } 186 187 const char * 188 datalink_ChoosePhoneNumber(struct datalink *dl) 189 { 190 char *phone; 191 192 if (dl->phone.alt == NULL) { 193 if (dl->phone.next == NULL) { 194 strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1); 195 dl->phone.list[sizeof dl->phone.list - 1] = '\0'; 196 if (*dl->phone.list == '\0') 197 return ""; 198 dl->phone.next = dl->phone.list; 199 } 200 dl->phone.alt = strsep(&dl->phone.next, ":"); 201 } 202 phone = strsep(&dl->phone.alt, "|"); 203 dl->phone.chosen = *phone ? phone : "[NONE]"; 204 if (*phone) 205 log_Printf(LogCHAT, "Phone: %s\n", phone); 206 return phone; 207 } 208 209 static void 210 datalink_LoginDone(struct datalink *dl) 211 { 212 chat_Finish(&dl->chat); 213 214 if (!dl->script.packetmode) { 215 dl->dial.tries = -1; 216 dl->dial.incs = 0; 217 datalink_NewState(dl, DATALINK_READY); 218 } else if (!physical_Raw(dl->physical)) { 219 dl->dial.tries = 0; 220 log_Printf(LogWARN, "datalink_LoginDone: Not connected.\n"); 221 if (dl->script.run) { 222 datalink_NewState(dl, DATALINK_LOGOUT); 223 if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL)) 224 log_Printf(LogWARN, "Invalid logout script\n"); 225 } else { 226 physical_StopDeviceTimer(dl->physical); 227 if (dl->physical->type == PHYS_DEDICATED) 228 /* force a redial timeout */ 229 physical_Close(dl->physical); 230 datalink_HangupDone(dl); 231 } 232 } else { 233 dl->dial.tries = -1; 234 dl->dial.incs = 0; 235 236 hdlc_Init(&dl->physical->hdlc, &dl->physical->link.lcp); 237 async_Init(&dl->physical->async); 238 239 lcp_Setup(&dl->physical->link.lcp, dl->state == DATALINK_READY ? 240 0 : dl->physical->link.lcp.cfg.openmode); 241 ccp_Setup(&dl->physical->link.ccp); 242 243 datalink_NewState(dl, DATALINK_LCP); 244 fsm_Up(&dl->physical->link.lcp.fsm); 245 fsm_Open(&dl->physical->link.lcp.fsm); 246 } 247 } 248 249 static int 250 datalink_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 251 int *n) 252 { 253 struct datalink *dl = descriptor2datalink(d); 254 int result; 255 256 result = 0; 257 switch (dl->state) { 258 case DATALINK_CLOSED: 259 if ((dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED|PHYS_BACKGROUND| 260 PHYS_FOREGROUND|PHYS_DDIAL)) && 261 !dl->bundle->CleaningUp) 262 /* 263 * Our first time in - DEDICATED & DDIAL never come down, and 264 * DIRECT, FOREGROUND & BACKGROUND get deleted when they enter 265 * DATALINK_CLOSED. Go to DATALINK_OPENING via datalink_Up() 266 * and fall through. 267 */ 268 datalink_Up(dl, 1, 1); 269 else 270 break; 271 /* fall through */ 272 273 case DATALINK_OPENING: 274 if (dl->dial.timer.state != TIMER_RUNNING) { 275 if (--dl->dial.tries < 0) 276 dl->dial.tries = 0; 277 if (physical_Open(dl->physical, dl->bundle) >= 0) { 278 log_WritePrompts(dl, "%s: Entering terminal mode on %s\r\n" 279 "Type `~?' for help\r\n", dl->name, 280 dl->physical->name.full); 281 if (dl->script.run) { 282 datalink_NewState(dl, DATALINK_DIAL); 283 if (!chat_Setup(&dl->chat, dl->cfg.script.dial, 284 *dl->cfg.script.dial ? 285 datalink_ChoosePhoneNumber(dl) : "")) 286 log_Printf(LogWARN, "Invalid dial script\n"); 287 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 288 dl->cfg.dial.max) 289 log_Printf(LogCHAT, "%s: Dial attempt %u of %d\n", 290 dl->name, dl->cfg.dial.max - dl->dial.tries, 291 dl->cfg.dial.max); 292 } else 293 datalink_NewState(dl, DATALINK_CARRIER); 294 return datalink_UpdateSet(d, r, w, e, n); 295 } else { 296 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 297 dl->cfg.dial.max) 298 log_Printf(LogCHAT, "Failed to open device (attempt %u of %d)\n", 299 dl->cfg.dial.max - dl->dial.tries, dl->cfg.dial.max); 300 else 301 log_Printf(LogCHAT, "Failed to open device\n"); 302 303 if (dl->bundle->CleaningUp || 304 (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 305 dl->cfg.dial.max && dl->dial.tries == 0)) { 306 datalink_NewState(dl, DATALINK_CLOSED); 307 dl->reconnect_tries = 0; 308 dl->dial.tries = -1; 309 log_WritePrompts(dl, "Failed to open %s\n", 310 dl->physical->name.full); 311 bundle_LinkClosed(dl->bundle, dl); 312 } 313 if (!dl->bundle->CleaningUp) { 314 int timeout; 315 316 timeout = datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 317 bundle_Notify(dl->bundle, EX_REDIAL); 318 log_WritePrompts(dl, "Failed to open %s, pause %d seconds\n", 319 dl->physical->name.full, timeout); 320 } 321 } 322 } 323 break; 324 325 case DATALINK_CARRIER: 326 /* Wait for carrier on the device */ 327 switch (physical_AwaitCarrier(dl->physical)) { 328 case CARRIER_PENDING: 329 log_Printf(LogDEBUG, "Waiting for carrier\n"); 330 return 0; /* A device timer is running to wake us up again */ 331 332 case CARRIER_OK: 333 if (dl->script.run) { 334 datalink_NewState(dl, DATALINK_LOGIN); 335 if (!chat_Setup(&dl->chat, dl->cfg.script.login, NULL)) 336 log_Printf(LogWARN, "Invalid login script\n"); 337 } else 338 datalink_LoginDone(dl); 339 return datalink_UpdateSet(d, r, w, e, n); 340 341 case CARRIER_LOST: 342 physical_Offline(dl->physical); /* Is this required ? */ 343 if (dl->script.run) { 344 datalink_NewState(dl, DATALINK_HANGUP); 345 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL)) 346 log_Printf(LogWARN, "Invalid hangup script\n"); 347 return datalink_UpdateSet(d, r, w, e, n); 348 } else { 349 datalink_HangupDone(dl); 350 return 0; /* Maybe bundle_CleanDatalinks() has something to do */ 351 } 352 } 353 354 case DATALINK_HANGUP: 355 case DATALINK_DIAL: 356 case DATALINK_LOGOUT: 357 case DATALINK_LOGIN: 358 result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n); 359 switch (dl->chat.state) { 360 case CHAT_DONE: 361 /* script succeeded */ 362 switch(dl->state) { 363 case DATALINK_HANGUP: 364 datalink_HangupDone(dl); 365 break; 366 case DATALINK_DIAL: 367 datalink_NewState(dl, DATALINK_CARRIER); 368 return datalink_UpdateSet(d, r, w, e, n); 369 case DATALINK_LOGOUT: 370 datalink_NewState(dl, DATALINK_HANGUP); 371 physical_Offline(dl->physical); 372 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL)) 373 log_Printf(LogWARN, "Invalid hangup script\n"); 374 return datalink_UpdateSet(d, r, w, e, n); 375 case DATALINK_LOGIN: 376 dl->phone.alt = NULL; 377 datalink_LoginDone(dl); 378 return datalink_UpdateSet(d, r, w, e, n); 379 } 380 break; 381 case CHAT_FAILED: 382 /* Going down - script failed */ 383 log_Printf(LogWARN, "Chat script failed\n"); 384 switch(dl->state) { 385 case DATALINK_HANGUP: 386 datalink_HangupDone(dl); 387 break; 388 case DATALINK_DIAL: 389 case DATALINK_LOGOUT: 390 case DATALINK_LOGIN: 391 datalink_NewState(dl, DATALINK_HANGUP); 392 physical_Offline(dl->physical); 393 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL)) 394 log_Printf(LogWARN, "Invalid hangup script\n"); 395 return datalink_UpdateSet(d, r, w, e, n); 396 } 397 break; 398 } 399 break; 400 401 case DATALINK_READY: 402 case DATALINK_LCP: 403 case DATALINK_AUTH: 404 case DATALINK_CBCP: 405 case DATALINK_OPEN: 406 result = descriptor_UpdateSet(&dl->chap.desc, r, w, e, n) + 407 descriptor_UpdateSet(&dl->physical->desc, r, w, e, n); 408 break; 409 } 410 return result; 411 } 412 413 int 414 datalink_RemoveFromSet(struct datalink *dl, fd_set *r, fd_set *w, fd_set *e) 415 { 416 return physical_RemoveFromSet(dl->physical, r, w, e); 417 } 418 419 static int 420 datalink_IsSet(struct fdescriptor *d, const fd_set *fdset) 421 { 422 struct datalink *dl = descriptor2datalink(d); 423 424 switch (dl->state) { 425 case DATALINK_CLOSED: 426 case DATALINK_OPENING: 427 break; 428 429 case DATALINK_HANGUP: 430 case DATALINK_DIAL: 431 case DATALINK_LOGOUT: 432 case DATALINK_LOGIN: 433 return descriptor_IsSet(&dl->chat.desc, fdset); 434 435 case DATALINK_READY: 436 case DATALINK_LCP: 437 case DATALINK_AUTH: 438 case DATALINK_CBCP: 439 case DATALINK_OPEN: 440 return descriptor_IsSet(&dl->chap.desc, fdset) ? 1 : 441 descriptor_IsSet(&dl->physical->desc, fdset); 442 } 443 return 0; 444 } 445 446 static void 447 datalink_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 448 { 449 struct datalink *dl = descriptor2datalink(d); 450 451 switch (dl->state) { 452 case DATALINK_CLOSED: 453 case DATALINK_OPENING: 454 break; 455 456 case DATALINK_HANGUP: 457 case DATALINK_DIAL: 458 case DATALINK_LOGOUT: 459 case DATALINK_LOGIN: 460 descriptor_Read(&dl->chat.desc, bundle, fdset); 461 break; 462 463 case DATALINK_READY: 464 case DATALINK_LCP: 465 case DATALINK_AUTH: 466 case DATALINK_CBCP: 467 case DATALINK_OPEN: 468 if (descriptor_IsSet(&dl->chap.desc, fdset)) 469 descriptor_Read(&dl->chap.desc, bundle, fdset); 470 if (descriptor_IsSet(&dl->physical->desc, fdset)) 471 descriptor_Read(&dl->physical->desc, bundle, fdset); 472 break; 473 } 474 } 475 476 static int 477 datalink_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 478 { 479 struct datalink *dl = descriptor2datalink(d); 480 int result = 0; 481 482 switch (dl->state) { 483 case DATALINK_CLOSED: 484 case DATALINK_OPENING: 485 break; 486 487 case DATALINK_HANGUP: 488 case DATALINK_DIAL: 489 case DATALINK_LOGOUT: 490 case DATALINK_LOGIN: 491 result = descriptor_Write(&dl->chat.desc, bundle, fdset); 492 break; 493 494 case DATALINK_READY: 495 case DATALINK_LCP: 496 case DATALINK_AUTH: 497 case DATALINK_CBCP: 498 case DATALINK_OPEN: 499 if (descriptor_IsSet(&dl->chap.desc, fdset)) 500 result += descriptor_Write(&dl->chap.desc, bundle, fdset); 501 if (descriptor_IsSet(&dl->physical->desc, fdset)) 502 result += descriptor_Write(&dl->physical->desc, bundle, fdset); 503 break; 504 } 505 506 return result; 507 } 508 509 static void 510 datalink_ComeDown(struct datalink *dl, int how) 511 { 512 if (how != CLOSE_NORMAL) { 513 dl->dial.tries = -1; 514 dl->reconnect_tries = 0; 515 if (dl->state >= DATALINK_READY && how == CLOSE_LCP) 516 dl->stayonline = 1; 517 } 518 519 if (dl->state >= DATALINK_READY && dl->stayonline) { 520 dl->stayonline = 0; 521 physical_StopDeviceTimer(dl->physical); 522 datalink_NewState(dl, DATALINK_READY); 523 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 524 physical_Offline(dl->physical); 525 if (dl->script.run && dl->state != DATALINK_OPENING) { 526 if (dl->state == DATALINK_LOGOUT) { 527 datalink_NewState(dl, DATALINK_HANGUP); 528 if (!chat_Setup(&dl->chat, dl->cfg.script.hangup, NULL)) 529 log_Printf(LogWARN, "Invalid hangup script\n"); 530 } else { 531 datalink_NewState(dl, DATALINK_LOGOUT); 532 if (!chat_Setup(&dl->chat, dl->cfg.script.logout, NULL)) 533 log_Printf(LogWARN, "Invalid logout script\n"); 534 } 535 } else 536 datalink_HangupDone(dl); 537 } 538 } 539 540 static void 541 datalink_LayerStart(void *v, struct fsm *fp) 542 { 543 /* The given FSM is about to start up ! */ 544 struct datalink *dl = (struct datalink *)v; 545 546 if (fp->proto == PROTO_LCP) 547 (*dl->parent->LayerStart)(dl->parent->object, fp); 548 } 549 550 static void 551 datalink_LayerUp(void *v, struct fsm *fp) 552 { 553 /* The given fsm is now up */ 554 struct datalink *dl = (struct datalink *)v; 555 struct lcp *lcp = &dl->physical->link.lcp; 556 557 if (fp->proto == PROTO_LCP) { 558 datalink_GotAuthname(dl, ""); 559 lcp->auth_ineed = lcp->want_auth; 560 lcp->auth_iwait = lcp->his_auth; 561 if (lcp->his_auth || lcp->want_auth) { 562 if (bundle_Phase(dl->bundle) != PHASE_NETWORK) 563 bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE); 564 log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name, 565 Auth2Nam(lcp->his_auth, lcp->his_authtype), 566 Auth2Nam(lcp->want_auth, lcp->want_authtype)); 567 if (lcp->his_auth == PROTO_PAP) 568 auth_StartReq(&dl->pap); 569 if (lcp->want_auth == PROTO_CHAP) 570 auth_StartReq(&dl->chap.auth); 571 } else 572 datalink_AuthOk(dl); 573 } 574 } 575 576 static void 577 datalink_AuthReInit(struct datalink *dl) 578 { 579 auth_StopTimer(&dl->pap); 580 auth_StopTimer(&dl->chap.auth); 581 chap_ReInit(&dl->chap); 582 } 583 584 void 585 datalink_GotAuthname(struct datalink *dl, const char *name) 586 { 587 strncpy(dl->peer.authname, name, sizeof dl->peer.authname - 1); 588 dl->peer.authname[sizeof dl->peer.authname - 1] = '\0'; 589 } 590 591 void 592 datalink_NCPUp(struct datalink *dl) 593 { 594 int ccpok = ccp_SetOpenMode(&dl->physical->link.ccp); 595 596 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) { 597 /* we've authenticated in multilink mode ! */ 598 switch (mp_Up(&dl->bundle->ncp.mp, dl)) { 599 case MP_LINKSENT: 600 /* We've handed the link off to another ppp (well, we will soon) ! */ 601 return; 602 case MP_UP: 603 /* First link in the bundle */ 604 auth_Select(dl->bundle, dl->peer.authname); 605 bundle_CalculateBandwidth(dl->bundle); 606 /* fall through */ 607 case MP_ADDED: 608 /* We're in multilink mode ! */ 609 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; /* override */ 610 bundle_CalculateBandwidth(dl->bundle); 611 break; 612 case MP_FAILED: 613 datalink_AuthNotOk(dl); 614 return; 615 } 616 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) { 617 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name); 618 datalink_NewState(dl, DATALINK_OPEN); 619 bundle_CalculateBandwidth(dl->bundle); 620 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 621 return; 622 } else { 623 dl->bundle->ncp.mp.peer = dl->peer; 624 ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link); 625 auth_Select(dl->bundle, dl->peer.authname); 626 } 627 628 if (ccpok) { 629 fsm_Up(&dl->physical->link.ccp.fsm); 630 fsm_Open(&dl->physical->link.ccp.fsm); 631 } 632 datalink_NewState(dl, DATALINK_OPEN); 633 bundle_NewPhase(dl->bundle, PHASE_NETWORK); 634 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 635 } 636 637 void 638 datalink_CBCPComplete(struct datalink *dl) 639 { 640 datalink_NewState(dl, DATALINK_LCP); 641 datalink_AuthReInit(dl); 642 fsm_Close(&dl->physical->link.lcp.fsm); 643 } 644 645 void 646 datalink_CBCPFailed(struct datalink *dl) 647 { 648 cbcp_Down(&dl->cbcp); 649 datalink_CBCPComplete(dl); 650 } 651 652 void 653 datalink_AuthOk(struct datalink *dl) 654 { 655 if ((dl->physical->link.lcp.his_callback.opmask & 656 CALLBACK_BIT(CALLBACK_CBCP) || 657 dl->physical->link.lcp.want_callback.opmask & 658 CALLBACK_BIT(CALLBACK_CBCP)) && 659 !(dl->physical->link.lcp.want_callback.opmask & 660 CALLBACK_BIT(CALLBACK_AUTH))) { 661 /* We must have agreed CBCP if AUTH isn't there any more */ 662 datalink_NewState(dl, DATALINK_CBCP); 663 cbcp_Up(&dl->cbcp); 664 } else if (dl->physical->link.lcp.want_callback.opmask) { 665 /* It's not CBCP */ 666 log_Printf(LogPHASE, "%s: Shutdown and await peer callback\n", dl->name); 667 datalink_NewState(dl, DATALINK_LCP); 668 datalink_AuthReInit(dl); 669 fsm_Close(&dl->physical->link.lcp.fsm); 670 } else 671 switch (dl->physical->link.lcp.his_callback.opmask) { 672 case 0: 673 datalink_NCPUp(dl); 674 break; 675 676 case CALLBACK_BIT(CALLBACK_AUTH): 677 auth_SetPhoneList(dl->peer.authname, dl->cbcp.fsm.phone, 678 sizeof dl->cbcp.fsm.phone); 679 if (*dl->cbcp.fsm.phone == '\0' || !strcmp(dl->cbcp.fsm.phone, "*")) { 680 log_Printf(LogPHASE, "%s: %s cannot be called back\n", dl->name, 681 dl->peer.authname); 682 *dl->cbcp.fsm.phone = '\0'; 683 } else { 684 char *ptr = strchr(dl->cbcp.fsm.phone, ','); 685 if (ptr) 686 *ptr = '\0'; /* Call back on the first number */ 687 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name, 688 dl->cbcp.fsm.phone); 689 dl->cbcp.required = 1; 690 } 691 dl->cbcp.fsm.delay = 0; 692 datalink_NewState(dl, DATALINK_LCP); 693 datalink_AuthReInit(dl); 694 fsm_Close(&dl->physical->link.lcp.fsm); 695 break; 696 697 case CALLBACK_BIT(CALLBACK_E164): 698 strncpy(dl->cbcp.fsm.phone, dl->physical->link.lcp.his_callback.msg, 699 sizeof dl->cbcp.fsm.phone - 1); 700 dl->cbcp.fsm.phone[sizeof dl->cbcp.fsm.phone - 1] = '\0'; 701 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name, 702 dl->cbcp.fsm.phone); 703 dl->cbcp.required = 1; 704 dl->cbcp.fsm.delay = 0; 705 datalink_NewState(dl, DATALINK_LCP); 706 datalink_AuthReInit(dl); 707 fsm_Close(&dl->physical->link.lcp.fsm); 708 break; 709 710 default: 711 log_Printf(LogPHASE, "%s: Oops - Should have NAK'd peer callback !\n", 712 dl->name); 713 datalink_NewState(dl, DATALINK_LCP); 714 datalink_AuthReInit(dl); 715 fsm_Close(&dl->physical->link.lcp.fsm); 716 break; 717 } 718 } 719 720 void 721 datalink_AuthNotOk(struct datalink *dl) 722 { 723 datalink_NewState(dl, DATALINK_LCP); 724 datalink_AuthReInit(dl); 725 fsm_Close(&dl->physical->link.lcp.fsm); 726 } 727 728 static void 729 datalink_LayerDown(void *v, struct fsm *fp) 730 { 731 /* The given FSM has been told to come down */ 732 struct datalink *dl = (struct datalink *)v; 733 734 if (fp->proto == PROTO_LCP) { 735 switch (dl->state) { 736 case DATALINK_OPEN: 737 peerid_Init(&dl->peer); 738 fsm2initial(&dl->physical->link.ccp.fsm); 739 datalink_NewState(dl, DATALINK_LCP); /* before parent TLD */ 740 (*dl->parent->LayerDown)(dl->parent->object, fp); 741 /* fall through (just in case) */ 742 743 case DATALINK_CBCP: 744 if (!dl->cbcp.required) 745 cbcp_Down(&dl->cbcp); 746 /* fall through (just in case) */ 747 748 case DATALINK_AUTH: 749 timer_Stop(&dl->pap.authtimer); 750 timer_Stop(&dl->chap.auth.authtimer); 751 } 752 datalink_NewState(dl, DATALINK_LCP); 753 datalink_AuthReInit(dl); 754 } 755 } 756 757 static void 758 datalink_LayerFinish(void *v, struct fsm *fp) 759 { 760 /* The given fsm is now down */ 761 struct datalink *dl = (struct datalink *)v; 762 763 if (fp->proto == PROTO_LCP) { 764 fsm2initial(fp); 765 (*dl->parent->LayerFinish)(dl->parent->object, fp); 766 datalink_ComeDown(dl, CLOSE_NORMAL); 767 } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 768 fsm_Open(fp); /* CCP goes to ST_STOPPED */ 769 } 770 771 struct datalink * 772 datalink_Create(const char *name, struct bundle *bundle, int type) 773 { 774 struct datalink *dl; 775 776 dl = (struct datalink *)malloc(sizeof(struct datalink)); 777 if (dl == NULL) 778 return dl; 779 780 dl->desc.type = DATALINK_DESCRIPTOR; 781 dl->desc.UpdateSet = datalink_UpdateSet; 782 dl->desc.IsSet = datalink_IsSet; 783 dl->desc.Read = datalink_Read; 784 dl->desc.Write = datalink_Write; 785 786 dl->state = DATALINK_CLOSED; 787 788 *dl->cfg.script.dial = '\0'; 789 *dl->cfg.script.login = '\0'; 790 *dl->cfg.script.logout = '\0'; 791 *dl->cfg.script.hangup = '\0'; 792 *dl->cfg.phone.list = '\0'; 793 *dl->phone.list = '\0'; 794 dl->phone.next = NULL; 795 dl->phone.alt = NULL; 796 dl->phone.chosen = "N/A"; 797 dl->stayonline = 0; 798 dl->script.run = 1; 799 dl->script.packetmode = 1; 800 mp_linkInit(&dl->mp); 801 802 dl->bundle = bundle; 803 dl->next = NULL; 804 805 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 806 807 dl->dial.tries = 0; 808 dl->cfg.dial.max = 1; 809 dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 810 dl->cfg.dial.timeout = DIAL_TIMEOUT; 811 dl->cfg.dial.inc = 0; 812 dl->cfg.dial.maxinc = 10; 813 814 dl->reconnect_tries = 0; 815 dl->cfg.reconnect.max = 0; 816 dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT; 817 818 dl->cfg.callback.opmask = 0; 819 dl->cfg.cbcp.delay = 0; 820 *dl->cfg.cbcp.phone = '\0'; 821 dl->cfg.cbcp.fsmretry = DEF_FSMRETRY; 822 823 dl->name = strdup(name); 824 peerid_Init(&dl->peer); 825 dl->parent = &bundle->fsm; 826 dl->fsmp.LayerStart = datalink_LayerStart; 827 dl->fsmp.LayerUp = datalink_LayerUp; 828 dl->fsmp.LayerDown = datalink_LayerDown; 829 dl->fsmp.LayerFinish = datalink_LayerFinish; 830 dl->fsmp.object = dl; 831 832 if ((dl->physical = physical_Create(dl, type)) == NULL) { 833 free(dl->name); 834 free(dl); 835 return NULL; 836 } 837 838 pap_Init(&dl->pap, dl->physical); 839 chap_Init(&dl->chap, dl->physical); 840 cbcp_Init(&dl->cbcp, dl->physical); 841 842 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */ 843 chat_Init(&dl->chat, dl->physical); 844 845 log_Printf(LogPHASE, "%s: Created in %s state\n", 846 dl->name, datalink_State(dl)); 847 848 return dl; 849 } 850 851 struct datalink * 852 datalink_Clone(struct datalink *odl, const char *name) 853 { 854 struct datalink *dl; 855 856 dl = (struct datalink *)malloc(sizeof(struct datalink)); 857 if (dl == NULL) 858 return dl; 859 860 dl->desc.type = DATALINK_DESCRIPTOR; 861 dl->desc.UpdateSet = datalink_UpdateSet; 862 dl->desc.IsSet = datalink_IsSet; 863 dl->desc.Read = datalink_Read; 864 dl->desc.Write = datalink_Write; 865 866 dl->state = DATALINK_CLOSED; 867 868 memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg); 869 mp_linkInit(&dl->mp); 870 *dl->phone.list = '\0'; 871 dl->phone.next = NULL; 872 dl->phone.alt = NULL; 873 dl->phone.chosen = "N/A"; 874 dl->bundle = odl->bundle; 875 dl->next = NULL; 876 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 877 dl->dial.tries = 0; 878 dl->reconnect_tries = 0; 879 dl->name = strdup(name); 880 peerid_Init(&dl->peer); 881 dl->parent = odl->parent; 882 memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp); 883 dl->fsmp.object = dl; 884 885 if ((dl->physical = physical_Create(dl, PHYS_INTERACTIVE)) == NULL) { 886 free(dl->name); 887 free(dl); 888 return NULL; 889 } 890 pap_Init(&dl->pap, dl->physical); 891 dl->pap.cfg = odl->pap.cfg; 892 893 chap_Init(&dl->chap, dl->physical); 894 dl->chap.auth.cfg = odl->chap.auth.cfg; 895 896 memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg); 897 memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg, 898 sizeof dl->physical->link.lcp.cfg); 899 memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg, 900 sizeof dl->physical->link.ccp.cfg); 901 memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg, 902 sizeof dl->physical->async.cfg); 903 904 cbcp_Init(&dl->cbcp, dl->physical); 905 906 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */ 907 chat_Init(&dl->chat, dl->physical); 908 909 log_Printf(LogPHASE, "%s: Cloned in %s state\n", 910 dl->name, datalink_State(dl)); 911 912 return dl; 913 } 914 915 struct datalink * 916 datalink_Destroy(struct datalink *dl) 917 { 918 struct datalink *result; 919 920 if (dl->state != DATALINK_CLOSED) { 921 log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n", 922 datalink_State(dl)); 923 switch (dl->state) { 924 case DATALINK_HANGUP: 925 case DATALINK_DIAL: 926 case DATALINK_LOGIN: 927 chat_Finish(&dl->chat); /* Gotta blat the timers ! */ 928 break; 929 } 930 } 931 932 chat_Destroy(&dl->chat); 933 timer_Stop(&dl->dial.timer); 934 result = dl->next; 935 physical_Destroy(dl->physical); 936 free(dl->name); 937 free(dl); 938 939 return result; 940 } 941 942 void 943 datalink_Up(struct datalink *dl, int runscripts, int packetmode) 944 { 945 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 946 /* Ignore scripts */ 947 runscripts = 0; 948 949 switch (dl->state) { 950 case DATALINK_CLOSED: 951 if (bundle_Phase(dl->bundle) == PHASE_DEAD || 952 bundle_Phase(dl->bundle) == PHASE_TERMINATE) 953 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH); 954 datalink_NewState(dl, DATALINK_OPENING); 955 dl->reconnect_tries = 956 dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max; 957 dl->dial.tries = dl->cfg.dial.max; 958 dl->script.run = runscripts; 959 dl->script.packetmode = packetmode; 960 break; 961 962 case DATALINK_OPENING: 963 if (!dl->script.run && runscripts) 964 dl->script.run = 1; 965 /* fall through */ 966 967 case DATALINK_DIAL: 968 case DATALINK_LOGIN: 969 case DATALINK_READY: 970 if (!dl->script.packetmode && packetmode) { 971 dl->script.packetmode = 1; 972 if (dl->state == DATALINK_READY) 973 datalink_LoginDone(dl); 974 } 975 break; 976 } 977 } 978 979 void 980 datalink_Close(struct datalink *dl, int how) 981 { 982 /* Please close */ 983 switch (dl->state) { 984 case DATALINK_OPEN: 985 peerid_Init(&dl->peer); 986 fsm2initial(&dl->physical->link.ccp.fsm); 987 /* fall through */ 988 989 case DATALINK_CBCP: 990 case DATALINK_AUTH: 991 case DATALINK_LCP: 992 datalink_AuthReInit(dl); 993 fsm_Close(&dl->physical->link.lcp.fsm); 994 if (how != CLOSE_NORMAL) { 995 dl->dial.tries = -1; 996 dl->reconnect_tries = 0; 997 if (how == CLOSE_LCP) 998 dl->stayonline = 1; 999 } 1000 break; 1001 1002 default: 1003 datalink_ComeDown(dl, how); 1004 } 1005 } 1006 1007 void 1008 datalink_Down(struct datalink *dl, int how) 1009 { 1010 /* Carrier is lost */ 1011 switch (dl->state) { 1012 case DATALINK_OPEN: 1013 peerid_Init(&dl->peer); 1014 fsm2initial(&dl->physical->link.ccp.fsm); 1015 /* fall through */ 1016 1017 case DATALINK_CBCP: 1018 case DATALINK_AUTH: 1019 case DATALINK_LCP: 1020 fsm2initial(&dl->physical->link.lcp.fsm); 1021 if (dl->state == DATALINK_OPENING) 1022 return; /* we're doing a callback... */ 1023 /* fall through */ 1024 1025 default: 1026 datalink_ComeDown(dl, how); 1027 } 1028 } 1029 1030 void 1031 datalink_StayDown(struct datalink *dl) 1032 { 1033 dl->reconnect_tries = 0; 1034 } 1035 1036 void 1037 datalink_DontHangup(struct datalink *dl) 1038 { 1039 if (dl->state >= DATALINK_LCP) 1040 dl->stayonline = 1; 1041 } 1042 1043 int 1044 datalink_Show(struct cmdargs const *arg) 1045 { 1046 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name); 1047 prompt_Printf(arg->prompt, " State: %s\n", 1048 datalink_State(arg->cx)); 1049 prompt_Printf(arg->prompt, " Peer name: "); 1050 if (*arg->cx->peer.authname) 1051 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname); 1052 else if (arg->cx->state == DATALINK_OPEN) 1053 prompt_Printf(arg->prompt, "None requested\n"); 1054 else 1055 prompt_Printf(arg->prompt, "N/A\n"); 1056 prompt_Printf(arg->prompt, " Discriminator: %s\n", 1057 mp_Enddisc(arg->cx->peer.enddisc.class, 1058 arg->cx->peer.enddisc.address, 1059 arg->cx->peer.enddisc.len)); 1060 1061 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1062 prompt_Printf(arg->prompt, " Phone List: %s\n", 1063 arg->cx->cfg.phone.list); 1064 if (arg->cx->cfg.dial.max) 1065 prompt_Printf(arg->prompt, " Dial tries: %d, delay ", 1066 arg->cx->cfg.dial.max); 1067 else 1068 prompt_Printf(arg->prompt, " Dial tries: infinite, delay "); 1069 if (arg->cx->cfg.dial.next_timeout >= 0) 1070 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout); 1071 else 1072 prompt_Printf(arg->prompt, "random/"); 1073 if (arg->cx->cfg.dial.timeout >= 0) 1074 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout); 1075 else 1076 prompt_Printf(arg->prompt, "random\n"); 1077 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ", 1078 arg->cx->cfg.reconnect.max); 1079 if (arg->cx->cfg.reconnect.timeout > 0) 1080 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout); 1081 else 1082 prompt_Printf(arg->prompt, "random\n"); 1083 prompt_Printf(arg->prompt, " Callback %s ", arg->cx->physical->type == 1084 PHYS_DIRECT ? "accepted: " : "requested:"); 1085 if (!arg->cx->cfg.callback.opmask) 1086 prompt_Printf(arg->prompt, "none\n"); 1087 else { 1088 int comma = 0; 1089 1090 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_NONE)) { 1091 prompt_Printf(arg->prompt, "none"); 1092 comma = 1; 1093 } 1094 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_AUTH)) { 1095 prompt_Printf(arg->prompt, "%sauth", comma ? ", " : ""); 1096 comma = 1; 1097 } 1098 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_E164)) { 1099 prompt_Printf(arg->prompt, "%sE.164", comma ? ", " : ""); 1100 if (arg->cx->physical->type != PHYS_DIRECT) 1101 prompt_Printf(arg->prompt, " (%s)", arg->cx->cfg.callback.msg); 1102 comma = 1; 1103 } 1104 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_CBCP)) { 1105 prompt_Printf(arg->prompt, "%scbcp\n", comma ? ", " : ""); 1106 prompt_Printf(arg->prompt, " CBCP: delay: %ds\n", 1107 arg->cx->cfg.cbcp.delay); 1108 prompt_Printf(arg->prompt, " phone: "); 1109 if (!strcmp(arg->cx->cfg.cbcp.phone, "*")) { 1110 if (arg->cx->physical->type & PHYS_DIRECT) 1111 prompt_Printf(arg->prompt, "Caller decides\n"); 1112 else 1113 prompt_Printf(arg->prompt, "Dialback server decides\n"); 1114 } else 1115 prompt_Printf(arg->prompt, "%s\n", arg->cx->cfg.cbcp.phone); 1116 prompt_Printf(arg->prompt, " timeout: %lds\n", 1117 arg->cx->cfg.cbcp.fsmretry); 1118 } else 1119 prompt_Printf(arg->prompt, "\n"); 1120 } 1121 1122 prompt_Printf(arg->prompt, " Dial Script: %s\n", 1123 arg->cx->cfg.script.dial); 1124 prompt_Printf(arg->prompt, " Login Script: %s\n", 1125 arg->cx->cfg.script.login); 1126 prompt_Printf(arg->prompt, " Logout Script: %s\n", 1127 arg->cx->cfg.script.logout); 1128 prompt_Printf(arg->prompt, " Hangup Script: %s\n", 1129 arg->cx->cfg.script.hangup); 1130 return 0; 1131 } 1132 1133 int 1134 datalink_SetReconnect(struct cmdargs const *arg) 1135 { 1136 if (arg->argc == arg->argn+2) { 1137 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]); 1138 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]); 1139 return 0; 1140 } 1141 return -1; 1142 } 1143 1144 int 1145 datalink_SetRedial(struct cmdargs const *arg) 1146 { 1147 const char *sep, *osep; 1148 int timeout, inc, maxinc, tries; 1149 1150 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) { 1151 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 && 1152 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) { 1153 arg->cx->cfg.dial.timeout = -1; 1154 randinit(); 1155 } else { 1156 timeout = atoi(arg->argv[arg->argn]); 1157 1158 if (timeout >= 0) 1159 arg->cx->cfg.dial.timeout = timeout; 1160 else { 1161 log_Printf(LogWARN, "Invalid redial timeout\n"); 1162 return -1; 1163 } 1164 } 1165 1166 sep = strchr(arg->argv[arg->argn], '+'); 1167 if (sep) { 1168 inc = atoi(++sep); 1169 osep = sep; 1170 if (inc >= 0) 1171 arg->cx->cfg.dial.inc = inc; 1172 else { 1173 log_Printf(LogWARN, "Invalid timeout increment\n"); 1174 return -1; 1175 } 1176 sep = strchr(sep, '-'); 1177 if (sep) { 1178 maxinc = atoi(++sep); 1179 if (maxinc >= 0) 1180 arg->cx->cfg.dial.maxinc = maxinc; 1181 else { 1182 log_Printf(LogWARN, "Invalid maximum timeout increments\n"); 1183 return -1; 1184 } 1185 } else { 1186 /* Default timeout increment */ 1187 arg->cx->cfg.dial.maxinc = 10; 1188 sep = osep; 1189 } 1190 } else { 1191 /* Default timeout increment & max increment */ 1192 arg->cx->cfg.dial.inc = 0; 1193 arg->cx->cfg.dial.maxinc = 10; 1194 sep = arg->argv[arg->argn]; 1195 } 1196 1197 sep = strchr(sep, '.'); 1198 if (sep) { 1199 if (strcasecmp(++sep, "random") == 0) { 1200 arg->cx->cfg.dial.next_timeout = -1; 1201 randinit(); 1202 } else { 1203 timeout = atoi(sep); 1204 if (timeout >= 0) 1205 arg->cx->cfg.dial.next_timeout = timeout; 1206 else { 1207 log_Printf(LogWARN, "Invalid next redial timeout\n"); 1208 return -1; 1209 } 1210 } 1211 } else 1212 /* Default next timeout */ 1213 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 1214 1215 if (arg->argc == arg->argn+2) { 1216 tries = atoi(arg->argv[arg->argn+1]); 1217 1218 if (tries >= 0) { 1219 arg->cx->cfg.dial.max = tries; 1220 } else { 1221 log_Printf(LogWARN, "Invalid retry value\n"); 1222 return 1; 1223 } 1224 } 1225 return 0; 1226 } 1227 1228 return -1; 1229 } 1230 1231 static const char * const states[] = { 1232 "closed", 1233 "opening", 1234 "hangup", 1235 "dial", 1236 "carrier", 1237 "logout", 1238 "login", 1239 "ready", 1240 "lcp", 1241 "auth", 1242 "cbcp", 1243 "open" 1244 }; 1245 1246 const char * 1247 datalink_State(struct datalink *dl) 1248 { 1249 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0]) 1250 return "unknown"; 1251 return states[dl->state]; 1252 } 1253 1254 static void 1255 datalink_NewState(struct datalink *dl, int state) 1256 { 1257 if (state != dl->state) { 1258 if (state >= 0 && state < sizeof states / sizeof states[0]) { 1259 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl), 1260 states[state]); 1261 dl->state = state; 1262 } else 1263 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state); 1264 } 1265 } 1266 1267 struct datalink * 1268 iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov, 1269 int fd, int *auxfd, int *nauxfd) 1270 { 1271 struct datalink *dl, *cdl; 1272 struct fsm_retry copy; 1273 char *oname; 1274 1275 dl = (struct datalink *)iov[(*niov)++].iov_base; 1276 dl->name = iov[*niov].iov_base; 1277 1278 if (dl->name[DATALINK_MAXNAME-1]) { 1279 dl->name[DATALINK_MAXNAME-1] = '\0'; 1280 if (strlen(dl->name) == DATALINK_MAXNAME - 1) 1281 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name); 1282 } 1283 1284 /* Make sure the name is unique ! */ 1285 oname = NULL; 1286 do { 1287 for (cdl = bundle->links; cdl; cdl = cdl->next) 1288 if (!strcasecmp(dl->name, cdl->name)) { 1289 if (oname) 1290 free(datalink_NextName(dl)); 1291 else 1292 oname = datalink_NextName(dl); 1293 break; /* Keep renaming 'till we have no conflicts */ 1294 } 1295 } while (cdl); 1296 1297 if (oname) { 1298 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name); 1299 free(oname); 1300 } else { 1301 dl->name = strdup(dl->name); 1302 free(iov[*niov].iov_base); 1303 } 1304 (*niov)++; 1305 1306 dl->desc.type = DATALINK_DESCRIPTOR; 1307 dl->desc.UpdateSet = datalink_UpdateSet; 1308 dl->desc.IsSet = datalink_IsSet; 1309 dl->desc.Read = datalink_Read; 1310 dl->desc.Write = datalink_Write; 1311 1312 mp_linkInit(&dl->mp); 1313 *dl->phone.list = '\0'; 1314 dl->phone.next = NULL; 1315 dl->phone.alt = NULL; 1316 dl->phone.chosen = "N/A"; 1317 1318 dl->bundle = bundle; 1319 dl->next = NULL; 1320 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 1321 dl->dial.tries = 0; 1322 dl->reconnect_tries = 0; 1323 dl->parent = &bundle->fsm; 1324 dl->fsmp.LayerStart = datalink_LayerStart; 1325 dl->fsmp.LayerUp = datalink_LayerUp; 1326 dl->fsmp.LayerDown = datalink_LayerDown; 1327 dl->fsmp.LayerFinish = datalink_LayerFinish; 1328 dl->fsmp.object = dl; 1329 1330 dl->physical = iov2physical(dl, iov, niov, maxiov, fd, auxfd, nauxfd); 1331 1332 if (!dl->physical) { 1333 free(dl->name); 1334 free(dl); 1335 dl = NULL; 1336 } else { 1337 copy = dl->pap.cfg.fsm; 1338 pap_Init(&dl->pap, dl->physical); 1339 dl->pap.cfg.fsm = copy; 1340 1341 copy = dl->chap.auth.cfg.fsm; 1342 chap_Init(&dl->chap, dl->physical); 1343 dl->chap.auth.cfg.fsm = copy; 1344 1345 cbcp_Init(&dl->cbcp, dl->physical); 1346 1347 memset(&dl->chat, '\0', sizeof dl->chat); /* Force buf{start,end} reset */ 1348 chat_Init(&dl->chat, dl->physical); 1349 1350 log_Printf(LogPHASE, "%s: Transferred in %s state\n", 1351 dl->name, datalink_State(dl)); 1352 } 1353 1354 return dl; 1355 } 1356 1357 int 1358 datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 1359 int *auxfd, int *nauxfd) 1360 { 1361 /* If `dl' is NULL, we're allocating before a Fromiov() */ 1362 int link_fd; 1363 1364 if (dl) { 1365 timer_Stop(&dl->dial.timer); 1366 /* The following is purely for the sake of paranoia */ 1367 cbcp_Down(&dl->cbcp); 1368 timer_Stop(&dl->pap.authtimer); 1369 timer_Stop(&dl->chap.auth.authtimer); 1370 } 1371 1372 if (*niov >= maxiov - 1) { 1373 log_Printf(LogERROR, "Toiov: No room for datalink !\n"); 1374 if (dl) { 1375 free(dl->name); 1376 free(dl); 1377 } 1378 return -1; 1379 } 1380 1381 iov[*niov].iov_base = (void *)dl; 1382 iov[(*niov)++].iov_len = sizeof *dl; 1383 iov[*niov].iov_base = dl ? realloc(dl->name, DATALINK_MAXNAME) : NULL; 1384 iov[(*niov)++].iov_len = DATALINK_MAXNAME; 1385 1386 link_fd = physical2iov(dl ? dl->physical : NULL, iov, niov, maxiov, auxfd, 1387 nauxfd); 1388 1389 if (link_fd == -1 && dl) { 1390 free(dl->name); 1391 free(dl); 1392 } 1393 1394 return link_fd; 1395 } 1396 1397 void 1398 datalink_Rename(struct datalink *dl, const char *name) 1399 { 1400 free(dl->name); 1401 dl->physical->link.name = dl->name = strdup(name); 1402 } 1403 1404 char * 1405 datalink_NextName(struct datalink *dl) 1406 { 1407 int f, n; 1408 char *name, *oname; 1409 1410 n = strlen(dl->name); 1411 name = (char *)malloc(n+3); 1412 for (f = n - 1; f >= 0; f--) 1413 if (!isdigit(dl->name[f])) 1414 break; 1415 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name); 1416 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1); 1417 oname = dl->name; 1418 dl->name = name; 1419 /* our physical link name isn't updated (it probably isn't created yet) */ 1420 return oname; 1421 } 1422 1423 int 1424 datalink_SetMode(struct datalink *dl, int mode) 1425 { 1426 if (!physical_SetMode(dl->physical, mode)) 1427 return 0; 1428 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 1429 dl->script.run = 0; 1430 if (dl->physical->type == PHYS_DIRECT) 1431 dl->reconnect_tries = 0; 1432 if (mode & (PHYS_DDIAL|PHYS_BACKGROUND|PHYS_FOREGROUND) && 1433 dl->state <= DATALINK_READY) 1434 datalink_Up(dl, 1, 1); 1435 return 1; 1436 } 1437 1438 int 1439 datalink_GetDialTimeout(struct datalink *dl) 1440 { 1441 int result = dl->cfg.dial.timeout + dl->dial.incs * dl->cfg.dial.inc; 1442 1443 if (dl->dial.incs < dl->cfg.dial.maxinc) 1444 dl->dial.incs++; 1445 1446 return result; 1447 } 1448