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