2 * Written by Eivind Eklund <eivind@yes.no>
5 * Copyright (C) 1998, Yes Interactive. All rights reserved.
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.
21 #include <sys/param.h>
22 #include <netinet/in.h>
23 #include <netinet/in_systm.h>
24 #include <netinet/ip.h>
25 #include <sys/socket.h>
45 #if defined(__OpenBSD__) || defined(__NetBSD__)
46 #include <sys/ioctl.h>
69 #include "throughput.h"
73 #include "slcompress.h"
77 #include "descriptor.h"
101 #include "netgraph.h"
105 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
109 physical_DeviceSize(void)
111 return sizeof(struct device);
115 struct device *(*create)(struct physical *);
116 struct device *(*iov2device)(int, struct physical *, struct iovec *,
117 int *, int, int *, int *);
118 unsigned (*DeviceSize)(void);
120 { tty_Create, tty_iov2device, tty_DeviceSize },
123 * This must come before ``udp'' so that the probe routine is
124 * able to identify it as a more specific type of SOCK_DGRAM.
126 { ether_Create, ether_iov2device, ether_DeviceSize },
127 #ifdef EXPERIMENTAL_NETGRAPH
128 { ng_Create, ng_iov2device, ng_DeviceSize },
131 { tcp_Create, tcp_iov2device, tcp_DeviceSize },
132 { udp_Create, udp_iov2device, udp_DeviceSize },
133 { exec_Create, exec_iov2device, exec_DeviceSize }
136 #define NDEVICES (sizeof devices / sizeof devices[0])
139 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
142 return physical_doUpdateSet(d, r, w, e, n, 0);
146 physical_SetDescriptor(struct physical *p)
148 p->desc.type = PHYSICAL_DESCRIPTOR;
149 p->desc.UpdateSet = physical_UpdateSet;
150 p->desc.IsSet = physical_IsSet;
151 p->desc.Read = physical_DescriptorRead;
152 p->desc.Write = physical_DescriptorWrite;
156 physical_Create(struct datalink *dl, int type)
160 p = (struct physical *)malloc(sizeof(struct physical));
164 p->link.type = PHYSICAL_LINK;
165 p->link.name = dl->name;
166 p->link.len = sizeof *p;
168 /* The sample period is fixed - see physical2iov() & iov2physical() */
169 throughput_init(&p->link.stats.total, SAMPLE_PERIOD);
170 p->link.stats.parent = dl->bundle->ncp.mp.active ?
171 &dl->bundle->ncp.mp.link.stats.total : NULL;
172 p->link.stats.gather = 1;
174 memset(p->link.Queue, '\0', sizeof p->link.Queue);
175 memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
176 memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
177 link_EmptyStack(&p->link);
180 physical_SetDescriptor(p);
183 hdlc_Init(&p->hdlc, &p->link.lcp);
184 async_Init(&p->async);
188 p->connect_count = 0;
191 *p->name.full = '\0';
192 p->name.base = p->name.full;
195 p->session_owner = (pid_t)-1;
197 p->cfg.rts_cts = MODEM_CTSRTS;
198 p->cfg.speed = MODEM_SPEED;
200 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
201 p->cfg.ndev = NMODEMS;
202 p->cfg.cd.necessity = CD_DEFAULT;
203 p->cfg.cd.delay = 0; /* reconfigured or device specific default */
205 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
206 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
211 static const struct parity {
216 { "even", "P_EVEN", CS7 | PARENB },
217 { "odd", "P_ODD", CS7 | PARENB | PARODD },
218 { "none", "P_ZERO", CS8 },
223 GetParityValue(const char *str)
225 const struct parity *pp;
227 for (pp = validparity; pp->name; pp++) {
228 if (strcasecmp(pp->name, str) == 0 ||
229 strcasecmp(pp->name1, str) == 0) {
237 physical_SetParity(struct physical *p, const char *str)
239 struct termios rstio;
242 val = GetParityValue(str);
246 tcgetattr(p->fd, &rstio);
247 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
248 rstio.c_cflag |= val;
249 tcsetattr(p->fd, TCSADRAIN, &rstio);
253 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
258 physical_GetSpeed(struct physical *p)
260 if (p->handler && p->handler->speed)
261 return (*p->handler->speed)(p);
267 physical_SetSpeed(struct physical *p, unsigned speed)
269 if (UnsignedToSpeed(speed) != B0) {
270 p->cfg.speed = speed;
278 physical_Raw(struct physical *p)
280 if (p->handler && p->handler->raw)
281 return (*p->handler->raw)(p);
287 physical_Offline(struct physical *p)
289 if (p->handler && p->handler->offline)
290 (*p->handler->offline)(p);
291 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
295 physical_Lock(struct physical *p)
299 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
300 (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
301 if (res == UU_LOCK_INUSE)
302 log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
304 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
305 p->link.name, p->name.full, uu_lockerr(res));
313 physical_Unlock(struct physical *p)
315 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
316 ID0uu_unlock(p->name.base) == -1)
317 log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name,
322 physical_Close(struct physical *p)
331 log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
333 if (p->handler && p->handler->cooked)
334 (*p->handler->cooked)(p);
336 physical_StopDeviceTimer(p);
338 memset(&ut, 0, sizeof ut);
339 ut.ut_type = DEAD_PROCESS;
340 gettimeofday(&ut.ut_tv, NULL);
341 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
345 newsid = tcgetpgrp(p->fd) == getpgrp();
348 log_SetTtyCommandMode(p->dl);
350 throughput_stop(&p->link.stats.total);
351 throughput_log(&p->link.stats.total, LogPHASE, p->link.name);
353 if (p->session_owner != (pid_t)-1) {
354 log_Printf(LogPHASE, "%s: HUPing %ld\n", p->link.name,
355 (long)p->session_owner);
356 ID0kill(p->session_owner, SIGHUP);
357 p->session_owner = (pid_t)-1;
361 bundle_setsid(p->dl->bundle, 0);
363 if (*p->name.full == '/') {
364 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
365 if (ID0unlink(fn) == -1)
366 log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
367 p->link.name, fn, strerror(errno));
370 if (p->handler && p->handler->destroy)
371 (*p->handler->destroy)(p);
373 p->name.base = p->name.full;
374 *p->name.full = '\0';
378 physical_Destroy(struct physical *p)
381 throughput_destroy(&p->link.stats.total);
386 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle __unused,
387 const fd_set *fdset __unused)
389 struct physical *p = descriptor2physical(d);
393 p->out = link_Dequeue(&p->link);
396 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->m_len);
397 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
398 p->link.name, nw, (unsigned long)p->out->m_len, p->fd);
401 p->out->m_offset += nw;
402 if (p->out->m_len == 0)
403 p->out = m_free(p->out);
408 else if (errno != ENOBUFS) {
409 log_Printf(LogPHASE, "%s: write (fd %d, len %zd): %s\n", p->link.name,
410 p->fd, p->out->m_len, strerror(errno));
411 datalink_Down(p->dl, CLOSE_NORMAL);
414 /* else we shouldn't really have been called ! select() is broken ! */
421 physical_ShowStatus(struct cmdargs const *arg)
423 struct physical *p = arg->cx->physical;
428 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
429 prompt_Printf(arg->prompt, " State: ");
431 prompt_Printf(arg->prompt, "closed\n");
433 slot = physical_Slot(p);
434 if (p->handler && p->handler->openinfo) {
436 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
438 prompt_Printf(arg->prompt, "open (%s, port %d)\n",
439 (*p->handler->openinfo)(p), slot);
440 } else if (slot == -1)
441 prompt_Printf(arg->prompt, "open\n");
443 prompt_Printf(arg->prompt, "open (port %d)\n", slot);
446 prompt_Printf(arg->prompt, " Device: %s",
447 *p->name.full ? p->name.full :
448 p->type == PHYS_DIRECT ? "unknown" : "N/A");
449 if (p->session_owner != (pid_t)-1)
450 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner);
452 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type));
453 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count);
455 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
456 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
459 prompt_Printf(arg->prompt, " Queued Packets: %lu\n",
460 (u_long)link_QueueLen(&p->link));
461 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen);
463 prompt_Printf(arg->prompt, "\nDefaults:\n");
465 prompt_Printf(arg->prompt, " Device List: ");
466 dev = p->cfg.devlist;
467 for (n = 0; n < p->cfg.ndev; n++) {
469 prompt_Printf(arg->prompt, ", ");
470 prompt_Printf(arg->prompt, "\"%s\"", dev);
471 dev += strlen(dev) + 1;
474 prompt_Printf(arg->prompt, "\n Characteristics: ");
475 if (physical_IsSync(arg->cx->physical))
476 prompt_Printf(arg->prompt, "sync");
478 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
480 switch (p->cfg.parity & CSIZE) {
482 prompt_Printf(arg->prompt, ", cs7");
485 prompt_Printf(arg->prompt, ", cs8");
488 if (p->cfg.parity & PARENB) {
489 if (p->cfg.parity & PARODD)
490 prompt_Printf(arg->prompt, ", odd parity");
492 prompt_Printf(arg->prompt, ", even parity");
494 prompt_Printf(arg->prompt, ", no parity");
496 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
498 prompt_Printf(arg->prompt, " CD check delay: ");
499 cd = p->handler ? &p->handler->cd : &p->cfg.cd;
500 if (cd->necessity == CD_NOTREQUIRED)
501 prompt_Printf(arg->prompt, "no cd");
502 else if (p->cfg.cd.necessity == CD_DEFAULT) {
503 prompt_Printf(arg->prompt, "device specific");
505 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
506 p->cfg.cd.delay == 1 ? "" : "s");
507 if (p->cfg.cd.necessity == CD_REQUIRED)
508 prompt_Printf(arg->prompt, " (required!)");
510 prompt_Printf(arg->prompt, "\n\n");
512 throughput_disp(&p->link.stats.total, arg->prompt);
518 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
519 const fd_set *fdset __unused)
521 struct physical *p = descriptor2physical(d);
525 rbuff = p->input.buf + p->input.sz;
527 /* something to read */
528 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
529 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
530 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
533 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
536 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
537 p->link.name, p->fd);
538 datalink_Down(p->dl, CLOSE_NORMAL);
542 rbuff -= p->input.sz;
545 if (p->link.lcp.fsm.state <= ST_CLOSED) {
546 if (p->type != PHYS_DEDICATED) {
547 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
548 if (rbuff != p->input.buf)
549 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
551 p->input.sz = n - (rbuff - p->input.buf);
554 /* LCP packet is detected. Turn ourselves into packet mode */
555 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
557 log_SetTtyCommandMode(p->dl);
558 datalink_Up(p->dl, 0, 1);
559 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
562 bcopy(rbuff, p->input.buf, p->input.sz);
564 /* In -dedicated mode, we just discard input until LCP is started */
567 link_PullPacket(&p->link, rbuff, n, bundle);
571 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
572 int fd, int *auxfd, int *nauxfd)
578 p = (struct physical *)iov[(*niov)++].iov_base;
579 p->link.name = dl->name;
580 memset(p->link.Queue, '\0', sizeof p->link.Queue);
582 p->desc.UpdateSet = physical_UpdateSet;
583 p->desc.IsSet = physical_IsSet;
584 p->desc.Read = physical_DescriptorRead;
585 p->desc.Write = physical_DescriptorWrite;
586 p->type = PHYS_DIRECT;
589 p->connect_count = 1;
591 physical_SetDevice(p, p->name.full);
593 p->link.lcp.fsm.bundle = dl->bundle;
594 p->link.lcp.fsm.link = &p->link;
595 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
596 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
597 memset(&p->link.lcp.fsm.StoppedTimer, '\0',
598 sizeof p->link.lcp.fsm.StoppedTimer);
599 p->link.lcp.fsm.parent = &dl->fsmp;
600 lcp_SetupCallbacks(&p->link.lcp);
602 p->link.ccp.fsm.bundle = dl->bundle;
603 p->link.ccp.fsm.link = &p->link;
604 /* Our in.state & out.state are NULL (no link-level ccp yet) */
605 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
606 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
607 memset(&p->link.ccp.fsm.StoppedTimer, '\0',
608 sizeof p->link.ccp.fsm.StoppedTimer);
609 p->link.ccp.fsm.parent = &dl->fsmp;
610 ccp_SetupCallbacks(&p->link.ccp);
612 p->hdlc.lqm.owner = &p->link.lcp;
613 p->hdlc.ReportTimer.state = TIMER_STOPPED;
614 p->hdlc.lqm.timer.state = TIMER_STOPPED;
617 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base;
618 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base;
619 p->link.stats.parent = dl->bundle->ncp.mp.active ?
620 &dl->bundle->ncp.mp.link.stats.total : NULL;
621 p->link.stats.gather = 1;
623 type = (long)p->handler;
625 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
626 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
628 if (p->handler == NULL) {
629 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
630 free(iov[(*niov)++].iov_base);
631 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
633 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
634 p->link.name, p->name.full, p->handler->name);
636 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
637 lqr_reStart(&p->link.lcp);
638 hdlc_StartTimer(&p->hdlc);
640 throughput_restart(&p->link.stats.total, "physical throughput",
641 Enabled(dl->bundle, OPT_THROUGHPUT));
647 physical_MaxDeviceSize(void)
649 unsigned biggest, sz, n;
651 biggest = sizeof(struct device);
652 for (n = 0; n < NDEVICES; n++)
653 if (devices[n].DeviceSize) {
654 sz = (*devices[n].DeviceSize)();
663 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
664 int *auxfd, int *nauxfd)
671 hdlc_StopTimer(&p->hdlc);
673 timer_Stop(&p->link.lcp.fsm.FsmTimer);
674 timer_Stop(&p->link.ccp.fsm.FsmTimer);
675 timer_Stop(&p->link.lcp.fsm.OpenTimer);
676 timer_Stop(&p->link.ccp.fsm.OpenTimer);
677 timer_Stop(&p->link.lcp.fsm.StoppedTimer);
678 timer_Stop(&p->link.ccp.fsm.StoppedTimer);
681 p->handler = (struct device *)(long)p->handler->type;
684 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
685 tcgetpgrp(p->fd) == getpgrp())
686 p->session_owner = getpid(); /* So I'll eventually get HUP'd */
688 p->session_owner = (pid_t)-1;
689 timer_Stop(&p->link.stats.total.Timer);
692 if (*niov + 2 >= maxiov) {
693 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
700 iov[*niov].iov_base = (void *)p;
701 iov[*niov].iov_len = sizeof *p;
704 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
705 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
707 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
708 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
711 sz = physical_MaxDeviceSize();
713 if (h && h->device2iov)
714 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
716 if ((iov[*niov].iov_base = malloc(sz)) == NULL) {
717 log_Printf(LogALERT, "physical2iov: Out of memory (%d bytes)\n", sz);
718 AbortProgram(EX_OSERR);
721 memcpy(iov[*niov].iov_base, h, sizeof *h);
722 iov[*niov].iov_len = sz;
726 iov[*niov].iov_base = NULL;
727 iov[*niov].iov_len = sz;
731 return p ? p->fd : 0;
735 physical_LockedDevice(struct physical *p)
737 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
744 physical_ChangedPid(struct physical *p, pid_t newpid)
746 if (physical_LockedDevice(p)) {
749 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
750 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
755 physical_IsSync(struct physical *p)
757 return p->cfg.speed == 0;
761 physical_DeviceMTU(struct physical *p)
763 return p->handler ? p->handler->mtu : 0;
766 const char *physical_GetDevice(struct physical *p)
772 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
777 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
778 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
780 p->cfg.devlist[pos++] = '\0';
781 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
782 pos += strlen(p->cfg.devlist + pos);
788 physical_SetSync(struct physical *p)
794 physical_SetRtsCts(struct physical *p, int enable)
796 p->cfg.rts_cts = enable ? 1 : 0;
801 physical_Read(struct physical *p, void *buf, size_t nbytes)
805 if (p->handler && p->handler->read)
806 ret = (*p->handler->read)(p, buf, nbytes);
808 ret = read(p->fd, buf, nbytes);
810 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
816 physical_Write(struct physical *p, const void *buf, size_t nbytes)
818 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
820 if (p->handler && p->handler->write)
821 return (*p->handler->write)(p, buf, nbytes);
823 return write(p->fd, buf, nbytes);
827 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
830 struct physical *p = descriptor2physical(d);
837 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
842 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
845 if (w && (force || link_QueueLen(&p->link) || p->out)) {
847 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
850 if (sets && *n < p->fd + 1)
858 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
860 if (p->handler && p->handler->removefromset)
861 return (*p->handler->removefromset)(p, r, w, e);
867 if (r && FD_ISSET(p->fd, r)) {
869 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
872 if (e && FD_ISSET(p->fd, e)) {
874 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
877 if (w && FD_ISSET(p->fd, w)) {
879 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
889 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
891 struct physical *p = descriptor2physical(d);
892 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
896 physical_Login(struct physical *p, const char *name)
898 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
903 memset(&ut, 0, sizeof ut);
904 ut.ut_type = USER_PROCESS;
905 gettimeofday(&ut.ut_tv, NULL);
906 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
907 strncpy(ut.ut_user, name, sizeof ut.ut_user);
908 if (p->handler && (p->handler->type == TCP_DEVICE ||
909 p->handler->type == UDP_DEVICE)) {
910 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
911 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
915 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
916 if ((connstr = getenv("CONNECT")))
917 /* mgetty sets this to the connection speed */
918 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
925 physical_SetMode(struct physical *p, int mode)
927 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
928 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
929 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
930 /* Note: The -direct -> -background is for callback ! */
931 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
932 mode2Nam(p->type), mode2Nam(mode));
940 physical_DeleteQueue(struct physical *p)
946 link_DeleteQueue(&p->link);
950 physical_SetDevice(struct physical *p, const char *name)
952 int len = strlen(_PATH_DEV);
954 if (name != p->name.full) {
955 strncpy(p->name.full, name, sizeof p->name.full - 1);
956 p->name.full[sizeof p->name.full - 1] = '\0';
958 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
959 strncmp(p->name.full, _PATH_DEV, len) ?
960 p->name.full : p->name.full + len;
964 physical_Found(struct physical *p)
969 if (*p->name.full == '/') {
970 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
971 lockfile = ID0fopen(fn, "w");
972 if (lockfile != NULL) {
973 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
976 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
977 p->link.name, fn, strerror(errno));
980 throughput_start(&p->link.stats.total, "physical throughput",
981 Enabled(p->dl->bundle, OPT_THROUGHPUT));
985 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
989 physical_Open(struct physical *p)
992 int devno, wasfd, err;
996 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
997 /* We're going back into "term" mode */
998 else if (p->type == PHYS_DIRECT) {
999 physical_SetDevice(p, "");
1000 p->fd = STDIN_FILENO;
1001 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1002 p->handler = (*devices[h].create)(p);
1003 close(STDOUT_FILENO);
1005 if (p->handler == NULL) {
1006 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1007 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1012 dev = p->cfg.devlist;
1014 while (devno < p->cfg.ndev && p->fd < 0) {
1015 physical_SetDevice(p, dev);
1016 if (physical_Lock(p)) {
1019 if (*p->name.full == '/') {
1020 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1026 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1027 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1031 if (h == NDEVICES) {
1033 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1036 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1037 " a '!' or contain at least one ':'\n", p->link.name,
1044 dev += strlen(dev) + 1;
1053 physical_SetupStack(struct physical *p, const char *who, int how)
1055 link_EmptyStack(&p->link);
1056 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1057 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1058 link_Stack(&p->link, &synclayer);
1060 link_Stack(&p->link, &asynclayer);
1061 link_Stack(&p->link, &hdlclayer);
1063 if (how != PHYSICAL_FORCE_SYNCNOACF)
1064 link_Stack(&p->link, &acflayer);
1065 link_Stack(&p->link, &protolayer);
1066 link_Stack(&p->link, &lqrlayer);
1067 link_Stack(&p->link, &ccplayer);
1068 link_Stack(&p->link, &vjlayer);
1069 link_Stack(&p->link, &tcpmsslayer);
1071 link_Stack(&p->link, &natlayer);
1073 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1074 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1075 p->cfg.speed = MODEM_SPEED;
1076 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1077 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1079 physical_SetSync(p);
1084 physical_StopDeviceTimer(struct physical *p)
1086 if (p->handler && p->handler->stoptimer)
1087 (*p->handler->stoptimer)(p);
1091 physical_AwaitCarrier(struct physical *p)
1093 if (p->handler && p->handler->awaitcarrier)
1094 return (*p->handler->awaitcarrier)(p);
1101 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1103 if (p->handler && p->handler->setasyncparams)
1104 return (*p->handler->setasyncparams)(p, mymap, hismap);
1106 async_SetLinkParams(&p->async, mymap, hismap);
1110 physical_Slot(struct physical *p)
1112 if (p->handler && p->handler->slot)
1113 return (*p->handler->slot)(p);
1119 physical_SetPPPoEnonstandard(struct physical *p, int enable)
1121 p->cfg.nonstandard_pppoe = enable ? 1 : 0;
1122 p->cfg.pppoe_configured = 1;