1 /* 2 * Written by Eivind Eklund <eivind@yes.no> 3 * for Yes Interactive 4 * 5 * Copyright (C) 1998, Yes Interactive. All rights reserved. 6 * 7 * Redistribution and use in any form is permitted. Redistribution in 8 * source form should include the above copyright and this set of 9 * conditions, because large sections american law seems to have been 10 * created by a bunch of jerks on drugs that are now illegal, forcing 11 * me to include this copyright-stuff instead of placing this in the 12 * public domain. The name of of 'Yes Interactive' or 'Eivind Eklund' 13 * may not be used to endorse or promote products derived from this 14 * software without specific prior written permission. 15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 18 * 19 * $FreeBSD$ 20 * 21 */ 22 23 #include <sys/param.h> 24 #include <netinet/in.h> 25 #include <netinet/in_systm.h> 26 #include <netinet/ip.h> 27 #include <sys/socket.h> 28 #include <sys/un.h> 29 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <paths.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/tty.h> /* TIOCOUTQ */ 37 #include <sys/uio.h> 38 #include <time.h> 39 #include <unistd.h> 40 #include <utmp.h> 41 #if defined(__OpenBSD__) || defined(__NetBSD__) 42 #include <sys/ioctl.h> 43 #include <util.h> 44 #else 45 #include <libutil.h> 46 #endif 47 48 #include "layer.h" 49 #ifndef NONAT 50 #include "nat_cmd.h" 51 #endif 52 #include "proto.h" 53 #include "acf.h" 54 #include "vjcomp.h" 55 #include "defs.h" 56 #include "command.h" 57 #include "mbuf.h" 58 #include "log.h" 59 #include "id.h" 60 #include "timer.h" 61 #include "fsm.h" 62 #include "lqr.h" 63 #include "hdlc.h" 64 #include "lcp.h" 65 #include "throughput.h" 66 #include "sync.h" 67 #include "async.h" 68 #include "iplist.h" 69 #include "slcompress.h" 70 #include "ncpaddr.h" 71 #include "ipcp.h" 72 #include "filter.h" 73 #include "descriptor.h" 74 #include "ccp.h" 75 #include "link.h" 76 #include "physical.h" 77 #include "mp.h" 78 #ifndef NORADIUS 79 #include "radius.h" 80 #endif 81 #include "ipv6cp.h" 82 #include "ncp.h" 83 #include "bundle.h" 84 #include "prompt.h" 85 #include "chat.h" 86 #include "auth.h" 87 #include "chap.h" 88 #include "cbcp.h" 89 #include "datalink.h" 90 #include "tcp.h" 91 #include "udp.h" 92 #include "exec.h" 93 #include "tty.h" 94 #ifndef NOI4B 95 #include "i4b.h" 96 #endif 97 #ifndef NONETGRAPH 98 #include "ether.h" 99 #include "netgraph.h" 100 #endif 101 #ifndef NOATM 102 #include "atm.h" 103 #endif 104 #include "tcpmss.h" 105 106 #define PPPOTCPLINE "ppp" 107 108 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *, 109 const fd_set *); 110 111 static int 112 physical_DeviceSize(void) 113 { 114 return sizeof(struct device); 115 } 116 117 struct { 118 struct device *(*create)(struct physical *); 119 struct device *(*iov2device)(int, struct physical *, struct iovec *, 120 int *, int, int *, int *); 121 int (*DeviceSize)(void); 122 } devices[] = { 123 #ifndef NOI4B 124 /* 125 * This must come before ``tty'' so that the probe routine is 126 * able to identify it as a more specific type of terminal device. 127 */ 128 { i4b_Create, i4b_iov2device, i4b_DeviceSize }, 129 #endif 130 { tty_Create, tty_iov2device, tty_DeviceSize }, 131 #ifndef NONETGRAPH 132 /* 133 * This must come before ``udp'' so that the probe routine is 134 * able to identify it as a more specific type of SOCK_DGRAM. 135 */ 136 { ether_Create, ether_iov2device, ether_DeviceSize }, 137 #ifdef EXPERIMENTAL_NETGRAPH 138 { ng_Create, ng_iov2device, ng_DeviceSize }, 139 #endif 140 #endif 141 #ifndef NOATM 142 /* Ditto for ATM devices */ 143 { atm_Create, atm_iov2device, atm_DeviceSize }, 144 #endif 145 { tcp_Create, tcp_iov2device, tcp_DeviceSize }, 146 { udp_Create, udp_iov2device, udp_DeviceSize }, 147 { exec_Create, exec_iov2device, exec_DeviceSize } 148 }; 149 150 #define NDEVICES (sizeof devices / sizeof devices[0]) 151 152 static int 153 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 154 int *n) 155 { 156 return physical_doUpdateSet(d, r, w, e, n, 0); 157 } 158 159 void 160 physical_SetDescriptor(struct physical *p) 161 { 162 p->desc.type = PHYSICAL_DESCRIPTOR; 163 p->desc.UpdateSet = physical_UpdateSet; 164 p->desc.IsSet = physical_IsSet; 165 p->desc.Read = physical_DescriptorRead; 166 p->desc.Write = physical_DescriptorWrite; 167 } 168 169 struct physical * 170 physical_Create(struct datalink *dl, int type) 171 { 172 struct physical *p; 173 174 p = (struct physical *)malloc(sizeof(struct physical)); 175 if (!p) 176 return NULL; 177 178 p->link.type = PHYSICAL_LINK; 179 p->link.name = dl->name; 180 p->link.len = sizeof *p; 181 182 /* The sample period is fixed - see physical2iov() & iov2physical() */ 183 throughput_init(&p->link.stats.total, SAMPLE_PERIOD); 184 p->link.stats.parent = dl->bundle->ncp.mp.active ? 185 &dl->bundle->ncp.mp.link.stats.total : NULL; 186 p->link.stats.gather = 1; 187 188 memset(p->link.Queue, '\0', sizeof p->link.Queue); 189 memset(p->link.proto_in, '\0', sizeof p->link.proto_in); 190 memset(p->link.proto_out, '\0', sizeof p->link.proto_out); 191 link_EmptyStack(&p->link); 192 193 p->handler = NULL; 194 physical_SetDescriptor(p); 195 p->type = type; 196 197 hdlc_Init(&p->hdlc, &p->link.lcp); 198 async_Init(&p->async); 199 200 p->fd = -1; 201 p->out = NULL; 202 p->connect_count = 0; 203 p->dl = dl; 204 p->input.sz = 0; 205 *p->name.full = '\0'; 206 p->name.base = p->name.full; 207 208 p->Utmp = 0; 209 p->session_owner = (pid_t)-1; 210 211 p->cfg.rts_cts = MODEM_CTSRTS; 212 p->cfg.speed = MODEM_SPEED; 213 p->cfg.parity = CS8; 214 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST); 215 p->cfg.ndev = NMODEMS; 216 p->cfg.cd.necessity = CD_DEFAULT; 217 p->cfg.cd.delay = 0; /* reconfigured or device specific default */ 218 219 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp); 220 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp); 221 222 return p; 223 } 224 225 static const struct parity { 226 const char *name; 227 const char *name1; 228 int set; 229 } validparity[] = { 230 { "even", "P_EVEN", CS7 | PARENB }, 231 { "odd", "P_ODD", CS7 | PARENB | PARODD }, 232 { "none", "P_ZERO", CS8 }, 233 { NULL, 0 }, 234 }; 235 236 static int 237 GetParityValue(const char *str) 238 { 239 const struct parity *pp; 240 241 for (pp = validparity; pp->name; pp++) { 242 if (strcasecmp(pp->name, str) == 0 || 243 strcasecmp(pp->name1, str) == 0) { 244 return pp->set; 245 } 246 } 247 return (-1); 248 } 249 250 int 251 physical_SetParity(struct physical *p, const char *str) 252 { 253 struct termios rstio; 254 int val; 255 256 val = GetParityValue(str); 257 if (val > 0) { 258 p->cfg.parity = val; 259 if (p->fd >= 0) { 260 tcgetattr(p->fd, &rstio); 261 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB); 262 rstio.c_cflag |= val; 263 tcsetattr(p->fd, TCSADRAIN, &rstio); 264 } 265 return 0; 266 } 267 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str); 268 return -1; 269 } 270 271 int 272 physical_GetSpeed(struct physical *p) 273 { 274 if (p->handler && p->handler->speed) 275 return (*p->handler->speed)(p); 276 277 return 0; 278 } 279 280 int 281 physical_SetSpeed(struct physical *p, int speed) 282 { 283 if (IntToSpeed(speed) != B0) { 284 p->cfg.speed = speed; 285 return 1; 286 } 287 288 return 0; 289 } 290 291 int 292 physical_Raw(struct physical *p) 293 { 294 if (p->handler && p->handler->raw) 295 return (*p->handler->raw)(p); 296 297 return 1; 298 } 299 300 void 301 physical_Offline(struct physical *p) 302 { 303 if (p->handler && p->handler->offline) 304 (*p->handler->offline)(p); 305 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name); 306 } 307 308 static int 309 physical_Lock(struct physical *p) 310 { 311 int res; 312 313 if (*p->name.full == '/' && p->type != PHYS_DIRECT && 314 (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) { 315 if (res == UU_LOCK_INUSE) 316 log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full); 317 else 318 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n", 319 p->link.name, p->name.full, uu_lockerr(res)); 320 return 0; 321 } 322 323 return 1; 324 } 325 326 static void 327 physical_Unlock(struct physical *p) 328 { 329 if (*p->name.full == '/' && p->type != PHYS_DIRECT && 330 ID0uu_unlock(p->name.base) == -1) 331 log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name, 332 p->name.base); 333 } 334 335 void 336 physical_Close(struct physical *p) 337 { 338 int newsid; 339 char fn[PATH_MAX]; 340 341 if (p->fd < 0) 342 return; 343 344 log_Printf(LogDEBUG, "%s: Close\n", p->link.name); 345 346 if (p->handler && p->handler->cooked) 347 (*p->handler->cooked)(p); 348 349 physical_StopDeviceTimer(p); 350 if (p->Utmp) { 351 if (p->handler && (p->handler->type == TCP_DEVICE || 352 p->handler->type == UDP_DEVICE)) 353 /* Careful - we logged in on line ``ppp'' with IP as our host */ 354 ID0logout(PPPOTCPLINE, 1); 355 else 356 ID0logout(p->name.base, 0); 357 p->Utmp = 0; 358 } 359 newsid = tcgetpgrp(p->fd) == getpgrp(); 360 close(p->fd); 361 p->fd = -1; 362 log_SetTtyCommandMode(p->dl); 363 364 throughput_stop(&p->link.stats.total); 365 throughput_log(&p->link.stats.total, LogPHASE, p->link.name); 366 367 if (p->session_owner != (pid_t)-1) { 368 log_Printf(LogPHASE, "%s: HUPing %ld\n", p->link.name, 369 (long)p->session_owner); 370 ID0kill(p->session_owner, SIGHUP); 371 p->session_owner = (pid_t)-1; 372 } 373 374 if (newsid) 375 bundle_setsid(p->dl->bundle, 0); 376 377 if (*p->name.full == '/') { 378 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base); 379 #ifndef RELEASE_CRUNCH 380 if (ID0unlink(fn) == -1) 381 log_Printf(LogALERT, "%s: Can't remove %s: %s\n", 382 p->link.name, fn, strerror(errno)); 383 #else 384 ID0unlink(fn); 385 #endif 386 } 387 physical_Unlock(p); 388 if (p->handler && p->handler->destroy) 389 (*p->handler->destroy)(p); 390 p->handler = NULL; 391 p->name.base = p->name.full; 392 *p->name.full = '\0'; 393 } 394 395 void 396 physical_Destroy(struct physical *p) 397 { 398 physical_Close(p); 399 throughput_destroy(&p->link.stats.total); 400 free(p); 401 } 402 403 static int 404 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 405 const fd_set *fdset) 406 { 407 struct physical *p = descriptor2physical(d); 408 int nw, result = 0; 409 410 if (p->out == NULL) 411 p->out = link_Dequeue(&p->link); 412 413 if (p->out) { 414 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->m_len); 415 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%lu) to %d\n", 416 p->link.name, nw, (unsigned long)p->out->m_len, p->fd); 417 if (nw > 0) { 418 p->out->m_len -= nw; 419 p->out->m_offset += nw; 420 if (p->out->m_len == 0) 421 p->out = m_free(p->out); 422 result = 1; 423 } else if (nw < 0) { 424 if (errno == EAGAIN) 425 result = 1; 426 else if (errno != ENOBUFS) { 427 log_Printf(LogPHASE, "%s: write (%d): %s\n", p->link.name, 428 p->fd, strerror(errno)); 429 datalink_Down(p->dl, CLOSE_NORMAL); 430 } 431 } 432 /* else we shouldn't really have been called ! select() is broken ! */ 433 } 434 435 return result; 436 } 437 438 int 439 physical_ShowStatus(struct cmdargs const *arg) 440 { 441 struct physical *p = arg->cx->physical; 442 struct cd *cd; 443 const char *dev; 444 int n, slot; 445 446 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name); 447 prompt_Printf(arg->prompt, " State: "); 448 if (p->fd < 0) 449 prompt_Printf(arg->prompt, "closed\n"); 450 else { 451 slot = physical_Slot(p); 452 if (p->handler && p->handler->openinfo) { 453 if (slot == -1) 454 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p)); 455 else 456 prompt_Printf(arg->prompt, "open (%s, port %d)\n", 457 (*p->handler->openinfo)(p), slot); 458 } else if (slot == -1) 459 prompt_Printf(arg->prompt, "open\n"); 460 else 461 prompt_Printf(arg->prompt, "open (port %d)\n", slot); 462 } 463 464 prompt_Printf(arg->prompt, " Device: %s", 465 *p->name.full ? p->name.full : 466 p->type == PHYS_DIRECT ? "unknown" : "N/A"); 467 if (p->session_owner != (pid_t)-1) 468 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner); 469 470 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type)); 471 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count); 472 #ifdef TIOCOUTQ 473 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0) 474 prompt_Printf(arg->prompt, " Physical outq: %d\n", n); 475 #endif 476 477 prompt_Printf(arg->prompt, " Queued Packets: %lu\n", 478 (u_long)link_QueueLen(&p->link)); 479 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen); 480 481 prompt_Printf(arg->prompt, "\nDefaults:\n"); 482 483 prompt_Printf(arg->prompt, " Device List: "); 484 dev = p->cfg.devlist; 485 for (n = 0; n < p->cfg.ndev; n++) { 486 if (n) 487 prompt_Printf(arg->prompt, ", "); 488 prompt_Printf(arg->prompt, "\"%s\"", dev); 489 dev += strlen(dev) + 1; 490 } 491 492 prompt_Printf(arg->prompt, "\n Characteristics: "); 493 if (physical_IsSync(arg->cx->physical)) 494 prompt_Printf(arg->prompt, "sync"); 495 else 496 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed); 497 498 switch (p->cfg.parity & CSIZE) { 499 case CS7: 500 prompt_Printf(arg->prompt, ", cs7"); 501 break; 502 case CS8: 503 prompt_Printf(arg->prompt, ", cs8"); 504 break; 505 } 506 if (p->cfg.parity & PARENB) { 507 if (p->cfg.parity & PARODD) 508 prompt_Printf(arg->prompt, ", odd parity"); 509 else 510 prompt_Printf(arg->prompt, ", even parity"); 511 } else 512 prompt_Printf(arg->prompt, ", no parity"); 513 514 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off")); 515 516 prompt_Printf(arg->prompt, " CD check delay: "); 517 cd = p->handler ? &p->handler->cd : &p->cfg.cd; 518 if (cd->necessity == CD_NOTREQUIRED) 519 prompt_Printf(arg->prompt, "no cd"); 520 else if (p->cfg.cd.necessity == CD_DEFAULT) { 521 prompt_Printf(arg->prompt, "device specific"); 522 } else { 523 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay, 524 p->cfg.cd.delay == 1 ? "" : "s"); 525 if (p->cfg.cd.necessity == CD_REQUIRED) 526 prompt_Printf(arg->prompt, " (required!)"); 527 } 528 prompt_Printf(arg->prompt, "\n\n"); 529 530 throughput_disp(&p->link.stats.total, arg->prompt); 531 532 return 0; 533 } 534 535 void 536 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 537 const fd_set *fdset) 538 { 539 struct physical *p = descriptor2physical(d); 540 u_char *rbuff; 541 int n, found; 542 543 rbuff = p->input.buf + p->input.sz; 544 545 /* something to read */ 546 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz); 547 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n", 548 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd); 549 if (n <= 0) { 550 if (n < 0) 551 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd, 552 strerror(errno)); 553 else 554 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n", 555 p->link.name, p->fd); 556 datalink_Down(p->dl, CLOSE_NORMAL); 557 return; 558 } 559 560 rbuff -= p->input.sz; 561 n += p->input.sz; 562 563 if (p->link.lcp.fsm.state <= ST_CLOSED) { 564 if (p->type != PHYS_DEDICATED) { 565 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p)); 566 if (rbuff != p->input.buf) 567 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf), 568 p->input.buf); 569 p->input.sz = n - (rbuff - p->input.buf); 570 571 if (found) { 572 /* LCP packet is detected. Turn ourselves into packet mode */ 573 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n", 574 p->link.name); 575 log_SetTtyCommandMode(p->dl); 576 datalink_Up(p->dl, 0, 1); 577 link_PullPacket(&p->link, rbuff, p->input.sz, bundle); 578 p->input.sz = 0; 579 } else 580 bcopy(rbuff, p->input.buf, p->input.sz); 581 } else 582 /* In -dedicated mode, we just discard input until LCP is started */ 583 p->input.sz = 0; 584 } else if (n > 0) 585 link_PullPacket(&p->link, rbuff, n, bundle); 586 } 587 588 struct physical * 589 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 590 int fd, int *auxfd, int *nauxfd) 591 { 592 struct physical *p; 593 int len, h, type; 594 595 p = (struct physical *)iov[(*niov)++].iov_base; 596 p->link.name = dl->name; 597 memset(p->link.Queue, '\0', sizeof p->link.Queue); 598 599 p->desc.UpdateSet = physical_UpdateSet; 600 p->desc.IsSet = physical_IsSet; 601 p->desc.Read = physical_DescriptorRead; 602 p->desc.Write = physical_DescriptorWrite; 603 p->type = PHYS_DIRECT; 604 p->dl = dl; 605 len = strlen(_PATH_DEV); 606 p->out = NULL; 607 p->connect_count = 1; 608 609 physical_SetDevice(p, p->name.full); 610 611 p->link.lcp.fsm.bundle = dl->bundle; 612 p->link.lcp.fsm.link = &p->link; 613 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer); 614 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer); 615 memset(&p->link.lcp.fsm.StoppedTimer, '\0', 616 sizeof p->link.lcp.fsm.StoppedTimer); 617 p->link.lcp.fsm.parent = &dl->fsmp; 618 lcp_SetupCallbacks(&p->link.lcp); 619 620 p->link.ccp.fsm.bundle = dl->bundle; 621 p->link.ccp.fsm.link = &p->link; 622 /* Our in.state & out.state are NULL (no link-level ccp yet) */ 623 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer); 624 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer); 625 memset(&p->link.ccp.fsm.StoppedTimer, '\0', 626 sizeof p->link.ccp.fsm.StoppedTimer); 627 p->link.ccp.fsm.parent = &dl->fsmp; 628 ccp_SetupCallbacks(&p->link.ccp); 629 630 p->hdlc.lqm.owner = &p->link.lcp; 631 p->hdlc.ReportTimer.state = TIMER_STOPPED; 632 p->hdlc.lqm.timer.state = TIMER_STOPPED; 633 634 p->fd = fd; 635 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base; 636 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base; 637 p->link.stats.parent = dl->bundle->ncp.mp.active ? 638 &dl->bundle->ncp.mp.link.stats.total : NULL; 639 p->link.stats.gather = 1; 640 641 type = (long)p->handler; 642 p->handler = NULL; 643 for (h = 0; h < NDEVICES && p->handler == NULL; h++) 644 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov, 645 auxfd, nauxfd); 646 if (p->handler == NULL) { 647 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name); 648 free(iov[(*niov)++].iov_base); 649 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE); 650 } else 651 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n", 652 p->link.name, p->name.full, p->handler->name); 653 654 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load) 655 lqr_reStart(&p->link.lcp); 656 hdlc_StartTimer(&p->hdlc); 657 658 throughput_restart(&p->link.stats.total, "physical throughput", 659 Enabled(dl->bundle, OPT_THROUGHPUT)); 660 661 return p; 662 } 663 664 int 665 physical_MaxDeviceSize() 666 { 667 int biggest, sz, n; 668 669 biggest = sizeof(struct device); 670 for (sz = n = 0; n < NDEVICES; n++) 671 if (devices[n].DeviceSize) { 672 sz = (*devices[n].DeviceSize)(); 673 if (biggest < sz) 674 biggest = sz; 675 } 676 677 return biggest; 678 } 679 680 int 681 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov, 682 int *auxfd, int *nauxfd) 683 { 684 struct device *h; 685 int sz; 686 687 h = NULL; 688 if (p) { 689 hdlc_StopTimer(&p->hdlc); 690 lqr_StopTimer(p); 691 timer_Stop(&p->link.lcp.fsm.FsmTimer); 692 timer_Stop(&p->link.ccp.fsm.FsmTimer); 693 timer_Stop(&p->link.lcp.fsm.OpenTimer); 694 timer_Stop(&p->link.ccp.fsm.OpenTimer); 695 timer_Stop(&p->link.lcp.fsm.StoppedTimer); 696 timer_Stop(&p->link.ccp.fsm.StoppedTimer); 697 if (p->handler) { 698 h = p->handler; 699 p->handler = (struct device *)(long)p->handler->type; 700 } 701 702 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) || 703 tcgetpgrp(p->fd) == getpgrp()) 704 p->session_owner = getpid(); /* So I'll eventually get HUP'd */ 705 else 706 p->session_owner = (pid_t)-1; 707 timer_Stop(&p->link.stats.total.Timer); 708 } 709 710 if (*niov + 2 >= maxiov) { 711 log_Printf(LogERROR, "physical2iov: No room for physical + throughput" 712 " + device !\n"); 713 if (p) 714 free(p); 715 return -1; 716 } 717 718 iov[*niov].iov_base = (void *)p; 719 iov[*niov].iov_len = sizeof *p; 720 (*niov)++; 721 722 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL; 723 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long); 724 (*niov)++; 725 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL; 726 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long); 727 (*niov)++; 728 729 sz = physical_MaxDeviceSize(); 730 if (p) { 731 if (h && h->device2iov) 732 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd); 733 else { 734 iov[*niov].iov_base = malloc(sz); 735 if (h) 736 memcpy(iov[*niov].iov_base, h, sizeof *h); 737 iov[*niov].iov_len = sz; 738 (*niov)++; 739 } 740 } else { 741 iov[*niov].iov_base = NULL; 742 iov[*niov].iov_len = sz; 743 (*niov)++; 744 } 745 746 return p ? p->fd : 0; 747 } 748 749 const char * 750 physical_LockedDevice(struct physical *p) 751 { 752 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT) 753 return p->name.base; 754 755 return NULL; 756 } 757 758 void 759 physical_ChangedPid(struct physical *p, pid_t newpid) 760 { 761 if (physical_LockedDevice(p)) { 762 int res; 763 764 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK) 765 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res)); 766 } 767 } 768 769 int 770 physical_IsSync(struct physical *p) 771 { 772 return p->cfg.speed == 0; 773 } 774 775 u_short 776 physical_DeviceMTU(struct physical *p) 777 { 778 return p->handler ? p->handler->mtu : 0; 779 } 780 781 const char *physical_GetDevice(struct physical *p) 782 { 783 return p->name.full; 784 } 785 786 void 787 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv) 788 { 789 int f, pos; 790 791 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0'; 792 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) { 793 if (pos) 794 p->cfg.devlist[pos++] = '\0'; 795 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1); 796 pos += strlen(p->cfg.devlist + pos); 797 } 798 p->cfg.ndev = f; 799 } 800 801 void 802 physical_SetSync(struct physical *p) 803 { 804 p->cfg.speed = 0; 805 } 806 807 int 808 physical_SetRtsCts(struct physical *p, int enable) 809 { 810 p->cfg.rts_cts = enable ? 1 : 0; 811 return 1; 812 } 813 814 ssize_t 815 physical_Read(struct physical *p, void *buf, size_t nbytes) 816 { 817 ssize_t ret; 818 819 if (p->handler && p->handler->read) 820 ret = (*p->handler->read)(p, buf, nbytes); 821 else 822 ret = read(p->fd, buf, nbytes); 823 824 log_DumpBuff(LogPHYSICAL, "read", buf, ret); 825 826 return ret; 827 } 828 829 ssize_t 830 physical_Write(struct physical *p, const void *buf, size_t nbytes) 831 { 832 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes); 833 834 if (p->handler && p->handler->write) 835 return (*p->handler->write)(p, buf, nbytes); 836 837 return write(p->fd, buf, nbytes); 838 } 839 840 int 841 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 842 int *n, int force) 843 { 844 struct physical *p = descriptor2physical(d); 845 int sets; 846 847 sets = 0; 848 if (p->fd >= 0) { 849 if (r) { 850 FD_SET(p->fd, r); 851 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd); 852 sets++; 853 } 854 if (e) { 855 FD_SET(p->fd, e); 856 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd); 857 sets++; 858 } 859 if (w && (force || link_QueueLen(&p->link) || p->out)) { 860 FD_SET(p->fd, w); 861 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd); 862 sets++; 863 } 864 if (sets && *n < p->fd + 1) 865 *n = p->fd + 1; 866 } 867 868 return sets; 869 } 870 871 int 872 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e) 873 { 874 if (p->handler && p->handler->removefromset) 875 return (*p->handler->removefromset)(p, r, w, e); 876 else { 877 int sets; 878 879 sets = 0; 880 if (p->fd >= 0) { 881 if (r && FD_ISSET(p->fd, r)) { 882 FD_CLR(p->fd, r); 883 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd); 884 sets++; 885 } 886 if (e && FD_ISSET(p->fd, e)) { 887 FD_CLR(p->fd, e); 888 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd); 889 sets++; 890 } 891 if (w && FD_ISSET(p->fd, w)) { 892 FD_CLR(p->fd, w); 893 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd); 894 sets++; 895 } 896 } 897 898 return sets; 899 } 900 } 901 902 int 903 physical_IsSet(struct fdescriptor *d, const fd_set *fdset) 904 { 905 struct physical *p = descriptor2physical(d); 906 return p->fd >= 0 && FD_ISSET(p->fd, fdset); 907 } 908 909 void 910 physical_Login(struct physical *p, const char *name) 911 { 912 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) { 913 struct utmp ut; 914 const char *connstr; 915 char *colon; 916 917 memset(&ut, 0, sizeof ut); 918 time(&ut.ut_time); 919 strncpy(ut.ut_name, name, sizeof ut.ut_name); 920 if (p->handler && (p->handler->type == TCP_DEVICE || 921 p->handler->type == UDP_DEVICE)) { 922 strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line); 923 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host); 924 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host); 925 if (colon) 926 *colon = '\0'; 927 } else 928 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line); 929 if ((connstr = getenv("CONNECT"))) 930 /* mgetty sets this to the connection speed */ 931 strncpy(ut.ut_host, connstr, sizeof ut.ut_host); 932 ID0login(&ut); 933 p->Utmp = ut.ut_time; 934 } 935 } 936 937 int 938 physical_SetMode(struct physical *p, int mode) 939 { 940 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) || 941 mode & (PHYS_DIRECT|PHYS_DEDICATED)) && 942 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) { 943 /* Note: The -direct -> -background is for callback ! */ 944 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name, 945 mode2Nam(p->type), mode2Nam(mode)); 946 return 0; 947 } 948 p->type = mode; 949 return 1; 950 } 951 952 void 953 physical_DeleteQueue(struct physical *p) 954 { 955 if (p->out) { 956 m_freem(p->out); 957 p->out = NULL; 958 } 959 link_DeleteQueue(&p->link); 960 } 961 962 void 963 physical_SetDevice(struct physical *p, const char *name) 964 { 965 int len = strlen(_PATH_DEV); 966 967 if (name != p->name.full) { 968 strncpy(p->name.full, name, sizeof p->name.full - 1); 969 p->name.full[sizeof p->name.full - 1] = '\0'; 970 } 971 p->name.base = *p->name.full == '!' ? p->name.full + 1 : 972 strncmp(p->name.full, _PATH_DEV, len) ? 973 p->name.full : p->name.full + len; 974 } 975 976 static void 977 physical_Found(struct physical *p) 978 { 979 FILE *lockfile; 980 char fn[PATH_MAX]; 981 982 if (*p->name.full == '/') { 983 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base); 984 lockfile = ID0fopen(fn, "w"); 985 if (lockfile != NULL) { 986 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit); 987 fclose(lockfile); 988 } 989 #ifndef RELEASE_CRUNCH 990 else 991 log_Printf(LogALERT, "%s: Can't create %s: %s\n", 992 p->link.name, fn, strerror(errno)); 993 #endif 994 } 995 996 throughput_start(&p->link.stats.total, "physical throughput", 997 Enabled(p->dl->bundle, OPT_THROUGHPUT)); 998 p->connect_count++; 999 p->input.sz = 0; 1000 1001 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name); 1002 } 1003 1004 int 1005 physical_Open(struct physical *p, struct bundle *bundle) 1006 { 1007 int devno, h, wasfd, err; 1008 char *dev; 1009 1010 if (p->fd >= 0) 1011 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name); 1012 /* We're going back into "term" mode */ 1013 else if (p->type == PHYS_DIRECT) { 1014 physical_SetDevice(p, ""); 1015 p->fd = STDIN_FILENO; 1016 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++) 1017 p->handler = (*devices[h].create)(p); 1018 if (p->fd >= 0) { 1019 if (p->handler == NULL) { 1020 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE); 1021 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name); 1022 } 1023 physical_Found(p); 1024 } 1025 } else { 1026 dev = p->cfg.devlist; 1027 devno = 0; 1028 while (devno < p->cfg.ndev && p->fd < 0) { 1029 physical_SetDevice(p, dev); 1030 if (physical_Lock(p)) { 1031 err = 0; 1032 1033 if (*p->name.full == '/') { 1034 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK); 1035 if (p->fd < 0) 1036 err = errno; 1037 } 1038 1039 wasfd = p->fd; 1040 for (h = 0; h < NDEVICES && p->handler == NULL; h++) 1041 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd) 1042 break; 1043 1044 if (p->fd < 0) { 1045 if (h == NDEVICES) { 1046 if (err) 1047 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full, 1048 strerror(errno)); 1049 else 1050 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/'," 1051 " a '!' or contain at least one ':'\n", p->link.name, 1052 p->name.full); 1053 } 1054 physical_Unlock(p); 1055 } else 1056 physical_Found(p); 1057 } 1058 dev += strlen(dev) + 1; 1059 devno++; 1060 } 1061 } 1062 1063 return p->fd; 1064 } 1065 1066 void 1067 physical_SetupStack(struct physical *p, const char *who, int how) 1068 { 1069 link_EmptyStack(&p->link); 1070 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF || 1071 (how == PHYSICAL_NOFORCE && physical_IsSync(p))) 1072 link_Stack(&p->link, &synclayer); 1073 else { 1074 link_Stack(&p->link, &asynclayer); 1075 link_Stack(&p->link, &hdlclayer); 1076 } 1077 if (how != PHYSICAL_FORCE_SYNCNOACF) 1078 link_Stack(&p->link, &acflayer); 1079 link_Stack(&p->link, &protolayer); 1080 link_Stack(&p->link, &lqrlayer); 1081 link_Stack(&p->link, &ccplayer); 1082 link_Stack(&p->link, &vjlayer); 1083 link_Stack(&p->link, &tcpmsslayer); 1084 #ifndef NONAT 1085 link_Stack(&p->link, &natlayer); 1086 #endif 1087 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) { 1088 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who); 1089 p->cfg.speed = MODEM_SPEED; 1090 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) { 1091 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n", 1092 who); 1093 physical_SetSync(p); 1094 } 1095 } 1096 1097 void 1098 physical_StopDeviceTimer(struct physical *p) 1099 { 1100 if (p->handler && p->handler->stoptimer) 1101 (*p->handler->stoptimer)(p); 1102 } 1103 1104 int 1105 physical_AwaitCarrier(struct physical *p) 1106 { 1107 if (p->handler && p->handler->awaitcarrier) 1108 return (*p->handler->awaitcarrier)(p); 1109 1110 return CARRIER_OK; 1111 } 1112 1113 1114 void 1115 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap) 1116 { 1117 if (p->handler && p->handler->setasyncparams) 1118 return (*p->handler->setasyncparams)(p, mymap, hismap); 1119 1120 async_SetLinkParams(&p->async, mymap, hismap); 1121 } 1122 1123 int 1124 physical_Slot(struct physical *p) 1125 { 1126 if (p->handler && p->handler->slot) 1127 return (*p->handler->slot)(p); 1128 1129 return -1; 1130 } 1131