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