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