xref: /freebsd/usr.sbin/ppp/physical.c (revision 2cb305af778acccc5b3ee351e99990c31781efc2)
163b73463SBrian Somers /*
263b73463SBrian Somers  * Written by Eivind Eklund <eivind@yes.no>
363b73463SBrian Somers  *    for Yes Interactive
463b73463SBrian Somers  *
563b73463SBrian Somers  * Copyright (C) 1998, Yes Interactive.  All rights reserved.
663b73463SBrian Somers  *
763b73463SBrian Somers  * Redistribution and use in any form is permitted.  Redistribution in
863b73463SBrian Somers  * source form should include the above copyright and this set of
963b73463SBrian Somers  * conditions, because large sections american law seems to have been
1063b73463SBrian Somers  * created by a bunch of jerks on drugs that are now illegal, forcing
1163b73463SBrian Somers  * me to include this copyright-stuff instead of placing this in the
1263b73463SBrian Somers  * public domain.  The name of of 'Yes Interactive' or 'Eivind Eklund'
1363b73463SBrian Somers  * may not be used to endorse or promote products derived from this
1463b73463SBrian Somers  * software without specific prior written permission.
1563b73463SBrian Somers  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1663b73463SBrian Somers  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1763b73463SBrian Somers  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1863b73463SBrian Somers  *
1997d92980SPeter Wemm  * $FreeBSD$
2063b73463SBrian Somers  *
2163b73463SBrian Somers  */
2263b73463SBrian Somers 
235d9e6103SBrian Somers #include <sys/param.h>
245d9e6103SBrian Somers #include <netinet/in.h>
255d9e6103SBrian Somers #include <netinet/in_systm.h>
265d9e6103SBrian Somers #include <netinet/ip.h>
275d9e6103SBrian Somers #include <sys/un.h>
285d9e6103SBrian Somers 
295d9e6103SBrian Somers #include <errno.h>
305d9e6103SBrian Somers #include <fcntl.h>
315d9e6103SBrian Somers #include <paths.h>
3263b73463SBrian Somers #include <stdio.h>
336f384573SBrian Somers #include <stdlib.h>
3463b73463SBrian Somers #include <string.h>
355d9e6103SBrian Somers #include <sys/tty.h>	/* TIOCOUTQ */
365d9e6103SBrian Somers #include <sys/uio.h>
37fc1141b2SBrian Somers #include <time.h>
3863b73463SBrian Somers #include <unistd.h>
39fc1141b2SBrian Somers #include <utmp.h>
405d9e6103SBrian Somers #if defined(__OpenBSD__) || defined(__NetBSD__)
415d9e6103SBrian Somers #include <sys/ioctl.h>
425d9e6103SBrian Somers #include <util.h>
435d9e6103SBrian Somers #else
445d9e6103SBrian Somers #include <libutil.h>
455d9e6103SBrian Somers #endif
4663b73463SBrian Somers 
475d9e6103SBrian Somers #include "layer.h"
4867b072f7SBrian Somers #ifndef NONAT
49f02c2029SBrian Somers #include "nat_cmd.h"
505d9e6103SBrian Somers #endif
515d9e6103SBrian Somers #include "proto.h"
525d9e6103SBrian Somers #include "acf.h"
535d9e6103SBrian Somers #include "vjcomp.h"
5463b73463SBrian Somers #include "defs.h"
555d9e6103SBrian Somers #include "command.h"
5663b73463SBrian Somers #include "mbuf.h"
5742d4d396SBrian Somers #include "log.h"
58fc1141b2SBrian Somers #include "id.h"
595d9e6103SBrian Somers #include "timer.h"
605d9e6103SBrian Somers #include "fsm.h"
615d9e6103SBrian Somers #include "lqr.h"
625d9e6103SBrian Somers #include "hdlc.h"
635d9e6103SBrian Somers #include "lcp.h"
645d9e6103SBrian Somers #include "throughput.h"
655d9e6103SBrian Somers #include "sync.h"
665d9e6103SBrian Somers #include "async.h"
675d9e6103SBrian Somers #include "iplist.h"
685d9e6103SBrian Somers #include "slcompress.h"
695d9e6103SBrian Somers #include "ipcp.h"
705d9e6103SBrian Somers #include "filter.h"
715d9e6103SBrian Somers #include "descriptor.h"
725d9e6103SBrian Somers #include "ccp.h"
735d9e6103SBrian Somers #include "link.h"
745d9e6103SBrian Somers #include "physical.h"
755d9e6103SBrian Somers #include "mp.h"
765d9e6103SBrian Somers #ifndef NORADIUS
775d9e6103SBrian Somers #include "radius.h"
785d9e6103SBrian Somers #endif
795d9e6103SBrian Somers #include "bundle.h"
805d9e6103SBrian Somers #include "prompt.h"
815d9e6103SBrian Somers #include "chat.h"
825d9e6103SBrian Somers #include "auth.h"
835d9e6103SBrian Somers #include "chap.h"
845d9e6103SBrian Somers #include "cbcp.h"
855d9e6103SBrian Somers #include "datalink.h"
865d9e6103SBrian Somers #include "tcp.h"
876815097bSBrian Somers #include "udp.h"
885d9e6103SBrian Somers #include "exec.h"
895d9e6103SBrian Somers #include "tty.h"
90eb6e5e05SBrian Somers #ifndef NOI4B
91eb6e5e05SBrian Somers #include "i4b.h"
92eb6e5e05SBrian Somers #endif
9387c3786eSBrian Somers #ifndef NONETGRAPH
9487c3786eSBrian Somers #include "ether.h"
9587c3786eSBrian Somers #endif
9663b73463SBrian Somers 
9763b73463SBrian Somers 
981a80353cSBrian Somers #define PPPOTCPLINE "ppp"
991a80353cSBrian Somers 
1005d9e6103SBrian Somers static int physical_DescriptorWrite(struct descriptor *, struct bundle *,
1015d9e6103SBrian Somers                                     const fd_set *);
10263b73463SBrian Somers 
103f5a99677SBrian Somers static int
104f5a99677SBrian Somers physical_DeviceSize(void)
105f5a99677SBrian Somers {
106f5a99677SBrian Somers   return sizeof(struct device);
107f5a99677SBrian Somers }
108f5a99677SBrian Somers 
1096815097bSBrian Somers struct {
1106815097bSBrian Somers   struct device *(*create)(struct physical *);
11187c3786eSBrian Somers   struct device *(*iov2device)(int, struct physical *, struct iovec *,
11287c3786eSBrian Somers                                int *, int, int *, int *);
113f5a99677SBrian Somers   int (*DeviceSize)(void);
1146815097bSBrian Somers } devices[] = {
115eb6e5e05SBrian Somers #ifndef NOI4B
116eb6e5e05SBrian Somers   { i4b_Create, i4b_iov2device, i4b_DeviceSize },
117eb6e5e05SBrian Somers #endif
118f5a99677SBrian Somers   { tty_Create, tty_iov2device, tty_DeviceSize },
11987c3786eSBrian Somers #ifndef NONETGRAPH
12087c3786eSBrian Somers   /* This must come before ``udp'' & ``tcp'' */
12187c3786eSBrian Somers   { ether_Create, ether_iov2device, ether_DeviceSize },
12287c3786eSBrian Somers #endif
123f5a99677SBrian Somers   { tcp_Create, tcp_iov2device, tcp_DeviceSize },
124f5a99677SBrian Somers   { udp_Create, udp_iov2device, udp_DeviceSize },
125f5a99677SBrian Somers   { exec_Create, exec_iov2device, exec_DeviceSize }
1265d9e6103SBrian Somers };
12763b73463SBrian Somers 
1286815097bSBrian Somers #define NDEVICES (sizeof devices / sizeof devices[0])
1295d9e6103SBrian Somers 
1305d9e6103SBrian Somers static int
1315d9e6103SBrian Somers physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
1325d9e6103SBrian Somers                    int *n)
1333bf710a4SBrian Somers {
1345d9e6103SBrian Somers   return physical_doUpdateSet(d, r, w, e, n, 0);
1355d9e6103SBrian Somers }
1365d9e6103SBrian Somers 
13787c3786eSBrian Somers void
13887c3786eSBrian Somers physical_SetDescriptor(struct physical *p)
13987c3786eSBrian Somers {
14087c3786eSBrian Somers   p->desc.type = PHYSICAL_DESCRIPTOR;
14187c3786eSBrian Somers   p->desc.UpdateSet = physical_UpdateSet;
14287c3786eSBrian Somers   p->desc.IsSet = physical_IsSet;
14387c3786eSBrian Somers   p->desc.Read = physical_DescriptorRead;
14487c3786eSBrian Somers   p->desc.Write = physical_DescriptorWrite;
14587c3786eSBrian Somers }
14687c3786eSBrian Somers 
1475d9e6103SBrian Somers struct physical *
1485d9e6103SBrian Somers physical_Create(struct datalink *dl, int type)
1495d9e6103SBrian Somers {
1505d9e6103SBrian Somers   struct physical *p;
1515d9e6103SBrian Somers 
1525d9e6103SBrian Somers   p = (struct physical *)malloc(sizeof(struct physical));
1535d9e6103SBrian Somers   if (!p)
1545d9e6103SBrian Somers     return NULL;
1555d9e6103SBrian Somers 
1565d9e6103SBrian Somers   p->link.type = PHYSICAL_LINK;
1575d9e6103SBrian Somers   p->link.name = dl->name;
1585d9e6103SBrian Somers   p->link.len = sizeof *p;
159ab2de065SBrian Somers 
160ab2de065SBrian Somers   /* The sample period is fixed - see physical2iov() & iov2physical() */
161ab2de065SBrian Somers   throughput_init(&p->link.throughput, SAMPLE_PERIOD);
1625d9e6103SBrian Somers 
1635d9e6103SBrian Somers   memset(p->link.Queue, '\0', sizeof p->link.Queue);
1645d9e6103SBrian Somers   memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
1655d9e6103SBrian Somers   memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
1665d9e6103SBrian Somers   link_EmptyStack(&p->link);
1675d9e6103SBrian Somers 
1685d9e6103SBrian Somers   p->handler = NULL;
16987c3786eSBrian Somers   physical_SetDescriptor(p);
1705d9e6103SBrian Somers   p->type = type;
1715d9e6103SBrian Somers 
1725d9e6103SBrian Somers   hdlc_Init(&p->hdlc, &p->link.lcp);
1735d9e6103SBrian Somers   async_Init(&p->async);
1745d9e6103SBrian Somers 
1755d9e6103SBrian Somers   p->fd = -1;
1765d9e6103SBrian Somers   p->out = NULL;
1775d9e6103SBrian Somers   p->connect_count = 0;
1785d9e6103SBrian Somers   p->dl = dl;
1795d9e6103SBrian Somers   p->input.sz = 0;
1805d9e6103SBrian Somers   *p->name.full = '\0';
1815d9e6103SBrian Somers   p->name.base = p->name.full;
1825d9e6103SBrian Somers 
1835d9e6103SBrian Somers   p->Utmp = 0;
1845d9e6103SBrian Somers   p->session_owner = (pid_t)-1;
1855d9e6103SBrian Somers 
1865d9e6103SBrian Somers   p->cfg.rts_cts = MODEM_CTSRTS;
1875d9e6103SBrian Somers   p->cfg.speed = MODEM_SPEED;
1885d9e6103SBrian Somers   p->cfg.parity = CS8;
1895d9e6103SBrian Somers   memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
1905d9e6103SBrian Somers   p->cfg.ndev = NMODEMS;
191ccd587f0SBrian Somers   p->cfg.cd.necessity = CD_VARIABLE;
1925d9e6103SBrian Somers   p->cfg.cd.delay = DEF_CDDELAY;
1935d9e6103SBrian Somers 
1945d9e6103SBrian Somers   lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
1955d9e6103SBrian Somers   ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
1965d9e6103SBrian Somers 
1975d9e6103SBrian Somers   return p;
1985d9e6103SBrian Somers }
1995d9e6103SBrian Somers 
2005d9e6103SBrian Somers static const struct parity {
2015d9e6103SBrian Somers   const char *name;
2025d9e6103SBrian Somers   const char *name1;
2035d9e6103SBrian Somers   int set;
2045d9e6103SBrian Somers } validparity[] = {
2055d9e6103SBrian Somers   { "even", "P_EVEN", CS7 | PARENB },
2065d9e6103SBrian Somers   { "odd", "P_ODD", CS7 | PARENB | PARODD },
2075d9e6103SBrian Somers   { "none", "P_ZERO", CS8 },
2085d9e6103SBrian Somers   { NULL, 0 },
2095d9e6103SBrian Somers };
2105d9e6103SBrian Somers 
2115d9e6103SBrian Somers static int
2125d9e6103SBrian Somers GetParityValue(const char *str)
2135d9e6103SBrian Somers {
2145d9e6103SBrian Somers   const struct parity *pp;
2155d9e6103SBrian Somers 
2165d9e6103SBrian Somers   for (pp = validparity; pp->name; pp++) {
2175d9e6103SBrian Somers     if (strcasecmp(pp->name, str) == 0 ||
2185d9e6103SBrian Somers 	strcasecmp(pp->name1, str) == 0) {
2195d9e6103SBrian Somers       return pp->set;
2205d9e6103SBrian Somers     }
2215d9e6103SBrian Somers   }
2225d9e6103SBrian Somers   return (-1);
2235d9e6103SBrian Somers }
2245d9e6103SBrian Somers 
2255d9e6103SBrian Somers int
2265d9e6103SBrian Somers physical_SetParity(struct physical *p, const char *str)
2275d9e6103SBrian Somers {
2285d9e6103SBrian Somers   struct termios rstio;
2295d9e6103SBrian Somers   int val;
2305d9e6103SBrian Somers 
2315d9e6103SBrian Somers   val = GetParityValue(str);
2325d9e6103SBrian Somers   if (val > 0) {
2335d9e6103SBrian Somers     p->cfg.parity = val;
2345d9e6103SBrian Somers     if (p->fd >= 0) {
2355d9e6103SBrian Somers       tcgetattr(p->fd, &rstio);
2365d9e6103SBrian Somers       rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
2375d9e6103SBrian Somers       rstio.c_cflag |= val;
2385d9e6103SBrian Somers       tcsetattr(p->fd, TCSADRAIN, &rstio);
2395d9e6103SBrian Somers     }
2405d9e6103SBrian Somers     return 0;
2415d9e6103SBrian Somers   }
2425d9e6103SBrian Somers   log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
2435d9e6103SBrian Somers   return -1;
2445d9e6103SBrian Somers }
2455d9e6103SBrian Somers 
2465d9e6103SBrian Somers int
2475d9e6103SBrian Somers physical_GetSpeed(struct physical *p)
2485d9e6103SBrian Somers {
2495d9e6103SBrian Somers   if (p->handler && p->handler->speed)
2505d9e6103SBrian Somers     return (*p->handler->speed)(p);
2515d9e6103SBrian Somers 
252ab2de065SBrian Somers   return 0;
2535d9e6103SBrian Somers }
2545d9e6103SBrian Somers 
2555d9e6103SBrian Somers int
2565d9e6103SBrian Somers physical_SetSpeed(struct physical *p, int speed)
2575d9e6103SBrian Somers {
2585d9e6103SBrian Somers   if (IntToSpeed(speed) != B0) {
2595d9e6103SBrian Somers       p->cfg.speed = speed;
2605d9e6103SBrian Somers       return 1;
2615d9e6103SBrian Somers   }
2625d9e6103SBrian Somers 
2635d9e6103SBrian Somers   return 0;
2645d9e6103SBrian Somers }
2655d9e6103SBrian Somers 
2665d9e6103SBrian Somers int
2675d9e6103SBrian Somers physical_Raw(struct physical *p)
2685d9e6103SBrian Somers {
2695d9e6103SBrian Somers   if (p->handler && p->handler->raw)
2705d9e6103SBrian Somers     return (*p->handler->raw)(p);
2715d9e6103SBrian Somers 
2725d9e6103SBrian Somers   return 1;
2735d9e6103SBrian Somers }
2745d9e6103SBrian Somers 
2755d9e6103SBrian Somers void
2765d9e6103SBrian Somers physical_Offline(struct physical *p)
2775d9e6103SBrian Somers {
2785d9e6103SBrian Somers   if (p->handler && p->handler->offline)
2795d9e6103SBrian Somers     (*p->handler->offline)(p);
2805d9e6103SBrian Somers   log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
2815d9e6103SBrian Somers }
2825d9e6103SBrian Somers 
2839950fb2aSBrian Somers static int
2849950fb2aSBrian Somers physical_Lock(struct physical *p)
2859950fb2aSBrian Somers {
2869950fb2aSBrian Somers   int res;
2879950fb2aSBrian Somers 
2889950fb2aSBrian Somers   if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
2899950fb2aSBrian Somers       (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
2909950fb2aSBrian Somers     if (res == UU_LOCK_INUSE)
2919950fb2aSBrian Somers       log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
2929950fb2aSBrian Somers     else
2939950fb2aSBrian Somers       log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
2949950fb2aSBrian Somers                  p->link.name, p->name.full, uu_lockerr(res));
2959950fb2aSBrian Somers     return 0;
2969950fb2aSBrian Somers   }
2979950fb2aSBrian Somers 
2989950fb2aSBrian Somers   return 1;
2999950fb2aSBrian Somers }
3009950fb2aSBrian Somers 
3015d9e6103SBrian Somers static void
3029950fb2aSBrian Somers physical_Unlock(struct physical *p)
3039950fb2aSBrian Somers {
3049950fb2aSBrian Somers   char fn[MAXPATHLEN];
3059950fb2aSBrian Somers   if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
3069950fb2aSBrian Somers       ID0uu_unlock(p->name.base) == -1)
3079950fb2aSBrian Somers     log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name, fn);
3089950fb2aSBrian Somers }
3099950fb2aSBrian Somers 
3109950fb2aSBrian Somers void
3119950fb2aSBrian Somers physical_Close(struct physical *p)
3125d9e6103SBrian Somers {
3135d9e6103SBrian Somers   int newsid;
3149950fb2aSBrian Somers   char fn[MAXPATHLEN];
3155d9e6103SBrian Somers 
3169950fb2aSBrian Somers   if (p->fd < 0)
3179950fb2aSBrian Somers     return;
3189950fb2aSBrian Somers 
3199950fb2aSBrian Somers   log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
3209950fb2aSBrian Somers 
3219950fb2aSBrian Somers   if (p->handler && p->handler->cooked)
3229950fb2aSBrian Somers     (*p->handler->cooked)(p);
3239950fb2aSBrian Somers 
3246815097bSBrian Somers   physical_StopDeviceTimer(p);
3255d9e6103SBrian Somers   if (p->Utmp) {
3261a80353cSBrian Somers     if (p->handler && (p->handler->type == TCP_DEVICE ||
327bacfb041SBrian Somers                        p->handler->type == UDP_DEVICE))
328bacfb041SBrian Somers       /* Careful - we logged in on line ``ppp'' with IP as our host */
329bacfb041SBrian Somers       ID0logout(PPPOTCPLINE, 1);
330bacfb041SBrian Somers     else
331bacfb041SBrian Somers       ID0logout(p->name.base, 0);
3325d9e6103SBrian Somers     p->Utmp = 0;
3335d9e6103SBrian Somers   }
3345d9e6103SBrian Somers   newsid = tcgetpgrp(p->fd) == getpgrp();
3355d9e6103SBrian Somers   close(p->fd);
3365d9e6103SBrian Somers   p->fd = -1;
3375d9e6103SBrian Somers   log_SetTtyCommandMode(p->dl);
3389950fb2aSBrian Somers 
3395d9e6103SBrian Somers   throughput_stop(&p->link.throughput);
3405d9e6103SBrian Somers   throughput_log(&p->link.throughput, LogPHASE, p->link.name);
3419950fb2aSBrian Somers 
3425d9e6103SBrian Somers   if (p->session_owner != (pid_t)-1) {
3435d9e6103SBrian Somers     ID0kill(p->session_owner, SIGHUP);
3445d9e6103SBrian Somers     p->session_owner = (pid_t)-1;
3455d9e6103SBrian Somers   }
3469950fb2aSBrian Somers 
3475d9e6103SBrian Somers   if (newsid)
3485d9e6103SBrian Somers     bundle_setsid(p->dl->bundle, 0);
3499950fb2aSBrian Somers 
3509950fb2aSBrian Somers   if (*p->name.full == '/') {
3519950fb2aSBrian Somers     snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
3529950fb2aSBrian Somers #ifndef RELEASE_CRUNCH
3539950fb2aSBrian Somers     if (ID0unlink(fn) == -1)
3549950fb2aSBrian Somers       log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
3559950fb2aSBrian Somers                  p->link.name, fn, strerror(errno));
3569950fb2aSBrian Somers #else
3579950fb2aSBrian Somers     ID0unlink(fn);
3589950fb2aSBrian Somers #endif
3599950fb2aSBrian Somers   }
3609950fb2aSBrian Somers   physical_Unlock(p);
3616815097bSBrian Somers   if (p->handler && p->handler->destroy)
3626815097bSBrian Somers     (*p->handler->destroy)(p);
3635d9e6103SBrian Somers   p->handler = NULL;
3645d9e6103SBrian Somers   p->name.base = p->name.full;
3659950fb2aSBrian Somers   *p->name.full = '\0';
3665d9e6103SBrian Somers }
3675d9e6103SBrian Somers 
3685d9e6103SBrian Somers void
3695d9e6103SBrian Somers physical_Destroy(struct physical *p)
3705d9e6103SBrian Somers {
3715d9e6103SBrian Somers   physical_Close(p);
372ab2de065SBrian Somers   throughput_destroy(&p->link.throughput);
3735d9e6103SBrian Somers   free(p);
3745d9e6103SBrian Somers }
3755d9e6103SBrian Somers 
3765d9e6103SBrian Somers static int
3775d9e6103SBrian Somers physical_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
3785d9e6103SBrian Somers                          const fd_set *fdset)
3795d9e6103SBrian Somers {
3805d9e6103SBrian Somers   struct physical *p = descriptor2physical(d);
3815d9e6103SBrian Somers   int nw, result = 0;
3825d9e6103SBrian Somers 
3835d9e6103SBrian Somers   if (p->out == NULL)
3845d9e6103SBrian Somers     p->out = link_Dequeue(&p->link);
3855d9e6103SBrian Somers 
3865d9e6103SBrian Somers   if (p->out) {
3875d9e6103SBrian Somers     nw = physical_Write(p, MBUF_CTOP(p->out), p->out->cnt);
3885d9e6103SBrian Somers     log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%d) to %d\n",
3895d9e6103SBrian Somers                p->link.name, nw, p->out->cnt, p->fd);
3905d9e6103SBrian Somers     if (nw > 0) {
3915d9e6103SBrian Somers       p->out->cnt -= nw;
3925d9e6103SBrian Somers       p->out->offset += nw;
3935d9e6103SBrian Somers       if (p->out->cnt == 0)
3945d9e6103SBrian Somers 	p->out = mbuf_FreeSeg(p->out);
3955d9e6103SBrian Somers       result = 1;
3965d9e6103SBrian Somers     } else if (nw < 0) {
3975d9e6103SBrian Somers       if (errno != EAGAIN) {
3985d9e6103SBrian Somers 	log_Printf(LogPHASE, "%s: write (%d): %s\n", p->link.name,
3995d9e6103SBrian Somers                    p->fd, strerror(errno));
4005d9e6103SBrian Somers         datalink_Down(p->dl, CLOSE_NORMAL);
4015d9e6103SBrian Somers       }
4025d9e6103SBrian Somers       result = 1;
4035d9e6103SBrian Somers     }
4045d9e6103SBrian Somers     /* else we shouldn't really have been called !  select() is broken ! */
4055d9e6103SBrian Somers   }
4065d9e6103SBrian Somers 
4075d9e6103SBrian Somers   return result;
4085d9e6103SBrian Somers }
4095d9e6103SBrian Somers 
4105d9e6103SBrian Somers int
4115d9e6103SBrian Somers physical_ShowStatus(struct cmdargs const *arg)
4125d9e6103SBrian Somers {
4135d9e6103SBrian Somers   struct physical *p = arg->cx->physical;
4145d9e6103SBrian Somers   const char *dev;
4155d9e6103SBrian Somers   int n;
4165d9e6103SBrian Somers 
4175d9e6103SBrian Somers   prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
4185d9e6103SBrian Somers   prompt_Printf(arg->prompt, " State:           ");
4195d9e6103SBrian Somers   if (p->fd < 0)
4205d9e6103SBrian Somers     prompt_Printf(arg->prompt, "closed\n");
4215d9e6103SBrian Somers   else if (p->handler && p->handler->openinfo)
4225d9e6103SBrian Somers     prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
4235d9e6103SBrian Somers   else
4245d9e6103SBrian Somers     prompt_Printf(arg->prompt, "open\n");
4255d9e6103SBrian Somers 
4265d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Device:          %s",
4275d9e6103SBrian Somers                 *p->name.full ?  p->name.full :
4285d9e6103SBrian Somers                 p->type == PHYS_DIRECT ? "unknown" : "N/A");
4295d9e6103SBrian Somers   if (p->session_owner != (pid_t)-1)
4305d9e6103SBrian Somers     prompt_Printf(arg->prompt, " (session owner: %d)", (int)p->session_owner);
4315d9e6103SBrian Somers 
4325d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\n Link Type:       %s\n", mode2Nam(p->type));
4335d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Connect Count:   %d\n", p->connect_count);
4345d9e6103SBrian Somers #ifdef TIOCOUTQ
4355d9e6103SBrian Somers   if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
4365d9e6103SBrian Somers       prompt_Printf(arg->prompt, " Physical outq:   %d\n", n);
4375d9e6103SBrian Somers #endif
4385d9e6103SBrian Somers 
4395d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Queued Packets:  %d\n",
4405d9e6103SBrian Somers                 link_QueueLen(&p->link));
4415d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Phone Number:    %s\n", arg->cx->phone.chosen);
4425d9e6103SBrian Somers 
4435d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
4445d9e6103SBrian Somers 
4455d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Device List:     ");
4465d9e6103SBrian Somers   dev = p->cfg.devlist;
4475d9e6103SBrian Somers   for (n = 0; n < p->cfg.ndev; n++) {
4485d9e6103SBrian Somers     if (n)
4495d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", ");
4505d9e6103SBrian Somers     prompt_Printf(arg->prompt, "\"%s\"", dev);
4515d9e6103SBrian Somers     dev += strlen(dev) + 1;
4525d9e6103SBrian Somers   }
4535d9e6103SBrian Somers 
4545d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\n Characteristics: ");
4555d9e6103SBrian Somers   if (physical_IsSync(arg->cx->physical))
4565d9e6103SBrian Somers     prompt_Printf(arg->prompt, "sync");
4575d9e6103SBrian Somers   else
4585d9e6103SBrian Somers     prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
4595d9e6103SBrian Somers 
4605d9e6103SBrian Somers   switch (p->cfg.parity & CSIZE) {
4615d9e6103SBrian Somers   case CS7:
4625d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", cs7");
4635d9e6103SBrian Somers     break;
4645d9e6103SBrian Somers   case CS8:
4655d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", cs8");
4665d9e6103SBrian Somers     break;
4675d9e6103SBrian Somers   }
4685d9e6103SBrian Somers   if (p->cfg.parity & PARENB) {
4695d9e6103SBrian Somers     if (p->cfg.parity & PARODD)
4705d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", odd parity");
4715d9e6103SBrian Somers     else
4725d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", even parity");
4735d9e6103SBrian Somers   } else
4745d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", no parity");
4755d9e6103SBrian Somers 
4765d9e6103SBrian Somers   prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
4775d9e6103SBrian Somers 
478ccd587f0SBrian Somers   prompt_Printf(arg->prompt, " CD check delay:  ");
479ccd587f0SBrian Somers   if (p->cfg.cd.necessity == CD_NOTREQUIRED)
480ccd587f0SBrian Somers     prompt_Printf(arg->prompt, "no cd");
481ccd587f0SBrian Somers   else {
482ccd587f0SBrian Somers     prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
483ccd587f0SBrian Somers                   p->cfg.cd.delay == 1 ? "" : "s");
484ccd587f0SBrian Somers     if (p->cfg.cd.necessity == CD_REQUIRED)
485ccd587f0SBrian Somers       prompt_Printf(arg->prompt, " (required!)");
486ccd587f0SBrian Somers   }
4875d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\n\n");
4885d9e6103SBrian Somers 
4895d9e6103SBrian Somers   throughput_disp(&p->link.throughput, arg->prompt);
4905d9e6103SBrian Somers 
4915d9e6103SBrian Somers   return 0;
4925d9e6103SBrian Somers }
4935d9e6103SBrian Somers 
49487c3786eSBrian Somers void
4955d9e6103SBrian Somers physical_DescriptorRead(struct descriptor *d, struct bundle *bundle,
4965d9e6103SBrian Somers                      const fd_set *fdset)
4975d9e6103SBrian Somers {
4985d9e6103SBrian Somers   struct physical *p = descriptor2physical(d);
4995d9e6103SBrian Somers   u_char *rbuff;
5005d9e6103SBrian Somers   int n, found;
5015d9e6103SBrian Somers 
5025d9e6103SBrian Somers   rbuff = p->input.buf + p->input.sz;
5035d9e6103SBrian Somers 
5045d9e6103SBrian Somers   /* something to read */
5055d9e6103SBrian Somers   n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
5065d9e6103SBrian Somers   log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
5075d9e6103SBrian Somers              p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
5085d9e6103SBrian Somers   if (n <= 0) {
5095d9e6103SBrian Somers     if (n < 0)
5105d9e6103SBrian Somers       log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
5115d9e6103SBrian Somers                  strerror(errno));
5125d9e6103SBrian Somers     else
5135d9e6103SBrian Somers       log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
5145d9e6103SBrian Somers                  p->link.name, p->fd);
5155d9e6103SBrian Somers     datalink_Down(p->dl, CLOSE_NORMAL);
5165d9e6103SBrian Somers     return;
5175d9e6103SBrian Somers   }
5185d9e6103SBrian Somers 
5195d9e6103SBrian Somers   rbuff -= p->input.sz;
5205d9e6103SBrian Somers   n += p->input.sz;
5215d9e6103SBrian Somers 
5225d9e6103SBrian Somers   if (p->link.lcp.fsm.state <= ST_CLOSED) {
5235d9e6103SBrian Somers     if (p->type != PHYS_DEDICATED) {
5245d9e6103SBrian Somers       found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
5255d9e6103SBrian Somers       if (rbuff != p->input.buf)
5265d9e6103SBrian Somers         log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
5275d9e6103SBrian Somers                          p->input.buf);
5285d9e6103SBrian Somers       p->input.sz = n - (rbuff - p->input.buf);
5295d9e6103SBrian Somers 
5305d9e6103SBrian Somers       if (found) {
5315d9e6103SBrian Somers         /* LCP packet is detected. Turn ourselves into packet mode */
5325d9e6103SBrian Somers         log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
5335d9e6103SBrian Somers                    p->link.name);
5345d9e6103SBrian Somers         log_SetTtyCommandMode(p->dl);
5355d9e6103SBrian Somers         datalink_Up(p->dl, 0, 1);
5365d9e6103SBrian Somers         link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
5375d9e6103SBrian Somers         p->input.sz = 0;
5385d9e6103SBrian Somers       } else
5395d9e6103SBrian Somers         bcopy(rbuff, p->input.buf, p->input.sz);
5405d9e6103SBrian Somers     } else
5415d9e6103SBrian Somers       /* In -dedicated mode, we just discard input until LCP is started */
5425d9e6103SBrian Somers       p->input.sz = 0;
5435d9e6103SBrian Somers   } else if (n > 0)
5445d9e6103SBrian Somers     link_PullPacket(&p->link, rbuff, n, bundle);
5455d9e6103SBrian Somers }
5465d9e6103SBrian Somers 
5475d9e6103SBrian Somers struct physical *
5485d9e6103SBrian Somers iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
54987c3786eSBrian Somers              int fd, int *auxfd, int *nauxfd)
5505d9e6103SBrian Somers {
5515d9e6103SBrian Somers   struct physical *p;
5526815097bSBrian Somers   int len, h, type;
5535d9e6103SBrian Somers 
5545d9e6103SBrian Somers   p = (struct physical *)iov[(*niov)++].iov_base;
5555d9e6103SBrian Somers   p->link.name = dl->name;
5565d9e6103SBrian Somers   memset(p->link.Queue, '\0', sizeof p->link.Queue);
5575d9e6103SBrian Somers 
5585d9e6103SBrian Somers   p->desc.UpdateSet = physical_UpdateSet;
5595d9e6103SBrian Somers   p->desc.IsSet = physical_IsSet;
5605d9e6103SBrian Somers   p->desc.Read = physical_DescriptorRead;
5615d9e6103SBrian Somers   p->desc.Write = physical_DescriptorWrite;
5625d9e6103SBrian Somers   p->type = PHYS_DIRECT;
5635d9e6103SBrian Somers   p->dl = dl;
5645d9e6103SBrian Somers   len = strlen(_PATH_DEV);
5655d9e6103SBrian Somers   p->out = NULL;
5665d9e6103SBrian Somers   p->connect_count = 1;
5675d9e6103SBrian Somers 
568e6923505SBrian Somers   physical_SetDevice(p, p->name.full);
569e6923505SBrian Somers 
5705d9e6103SBrian Somers   p->link.lcp.fsm.bundle = dl->bundle;
5715d9e6103SBrian Somers   p->link.lcp.fsm.link = &p->link;
5725d9e6103SBrian Somers   memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
5735d9e6103SBrian Somers   memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
5745d9e6103SBrian Somers   memset(&p->link.lcp.fsm.StoppedTimer, '\0',
5755d9e6103SBrian Somers          sizeof p->link.lcp.fsm.StoppedTimer);
5765d9e6103SBrian Somers   p->link.lcp.fsm.parent = &dl->fsmp;
5775d9e6103SBrian Somers   lcp_SetupCallbacks(&p->link.lcp);
5785d9e6103SBrian Somers 
5795d9e6103SBrian Somers   p->link.ccp.fsm.bundle = dl->bundle;
5805d9e6103SBrian Somers   p->link.ccp.fsm.link = &p->link;
5815d9e6103SBrian Somers   /* Our in.state & out.state are NULL (no link-level ccp yet) */
5825d9e6103SBrian Somers   memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
5835d9e6103SBrian Somers   memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
5845d9e6103SBrian Somers   memset(&p->link.ccp.fsm.StoppedTimer, '\0',
5855d9e6103SBrian Somers          sizeof p->link.ccp.fsm.StoppedTimer);
5865d9e6103SBrian Somers   p->link.ccp.fsm.parent = &dl->fsmp;
5875d9e6103SBrian Somers   ccp_SetupCallbacks(&p->link.ccp);
5885d9e6103SBrian Somers 
5895d9e6103SBrian Somers   p->hdlc.lqm.owner = &p->link.lcp;
5905d9e6103SBrian Somers   p->hdlc.ReportTimer.state = TIMER_STOPPED;
5915d9e6103SBrian Somers   p->hdlc.lqm.timer.state = TIMER_STOPPED;
5925d9e6103SBrian Somers 
5935d9e6103SBrian Somers   p->fd = fd;
594ab2de065SBrian Somers   p->link.throughput.SampleOctets = (long long *)iov[(*niov)++].iov_base;
5955d9e6103SBrian Somers 
596f5a99677SBrian Somers   type = (long)p->handler;
597f5a99677SBrian Somers   p->handler = NULL;
598f5a99677SBrian Somers   for (h = 0; h < NDEVICES && p->handler == NULL; h++)
59987c3786eSBrian Somers     p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
60087c3786eSBrian Somers                                           auxfd, nauxfd);
601f5a99677SBrian Somers   if (p->handler == NULL) {
60287c3786eSBrian Somers     log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
603f5a99677SBrian Somers     free(iov[(*niov)++].iov_base);
604f5a99677SBrian Somers     physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
605f5a99677SBrian Somers   } else
606f5a99677SBrian Somers     log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
607f5a99677SBrian Somers                p->link.name, p->name.full, p->handler->name);
608f5a99677SBrian Somers 
6095d9e6103SBrian Somers   if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
6105d9e6103SBrian Somers     lqr_reStart(&p->link.lcp);
6115d9e6103SBrian Somers   hdlc_StartTimer(&p->hdlc);
6125d9e6103SBrian Somers 
613ab2de065SBrian Somers   throughput_restart(&p->link.throughput, "physical throughput",
6145d9e6103SBrian Somers                      Enabled(dl->bundle, OPT_THROUGHPUT));
6156815097bSBrian Somers 
6165d9e6103SBrian Somers   return p;
6175d9e6103SBrian Somers }
6185d9e6103SBrian Somers 
6195d9e6103SBrian Somers int
620f5a99677SBrian Somers physical_MaxDeviceSize()
621f5a99677SBrian Somers {
622f5a99677SBrian Somers   int biggest, sz, n;
623f5a99677SBrian Somers 
624f5a99677SBrian Somers   biggest = sizeof(struct device);
625f5a99677SBrian Somers   for (sz = n = 0; n < NDEVICES; n++)
626f5a99677SBrian Somers     if (devices[n].DeviceSize) {
627f5a99677SBrian Somers       sz = (*devices[n].DeviceSize)();
628f5a99677SBrian Somers       if (biggest < sz)
629f5a99677SBrian Somers         biggest = sz;
630f5a99677SBrian Somers     }
631f5a99677SBrian Somers 
632f5a99677SBrian Somers   return biggest;
633f5a99677SBrian Somers }
634f5a99677SBrian Somers 
635f5a99677SBrian Somers int
6365d9e6103SBrian Somers physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
6372cb305afSBrian Somers              int *auxfd, int *nauxfd)
6385d9e6103SBrian Somers {
639f5a99677SBrian Somers   struct device *h;
640f5a99677SBrian Somers   int sz;
641f5a99677SBrian Somers 
642f5a99677SBrian Somers   h = NULL;
6435d9e6103SBrian Somers   if (p) {
6445d9e6103SBrian Somers     hdlc_StopTimer(&p->hdlc);
6455d9e6103SBrian Somers     lqr_StopTimer(p);
6465d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.FsmTimer);
6475d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.FsmTimer);
6485d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.OpenTimer);
6495d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.OpenTimer);
6505d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.StoppedTimer);
6515d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.StoppedTimer);
6526815097bSBrian Somers     if (p->handler) {
653f5a99677SBrian Somers       h = p->handler;
6546815097bSBrian Somers       p->handler = (struct device *)(long)p->handler->type;
6555d9e6103SBrian Somers     }
6566815097bSBrian Somers 
657ac685e31SBrian Somers     if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
658ac685e31SBrian Somers         tcgetpgrp(p->fd) == getpgrp())
6595d9e6103SBrian Somers       p->session_owner = getpid();      /* So I'll eventually get HUP'd */
660ac685e31SBrian Somers     else
661ac685e31SBrian Somers       p->session_owner = (pid_t)-1;
6625d9e6103SBrian Somers     timer_Stop(&p->link.throughput.Timer);
6635d9e6103SBrian Somers   }
6645d9e6103SBrian Somers 
665ab2de065SBrian Somers   if (*niov + 2 >= maxiov) {
666ab2de065SBrian Somers     log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
667ab2de065SBrian Somers                " + device !\n");
6685d9e6103SBrian Somers     if (p)
6695d9e6103SBrian Somers       free(p);
6705d9e6103SBrian Somers     return -1;
6715d9e6103SBrian Somers   }
6725d9e6103SBrian Somers 
6732cb305afSBrian Somers   iov[*niov].iov_base = (void *)p;
6745d9e6103SBrian Somers   iov[*niov].iov_len = sizeof *p;
6755d9e6103SBrian Somers   (*niov)++;
6765d9e6103SBrian Somers 
6772cb305afSBrian Somers   iov[*niov].iov_base = p ? (void *)p->link.throughput.SampleOctets : NULL;
678ab2de065SBrian Somers   iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
679ab2de065SBrian Somers   (*niov)++;
680ab2de065SBrian Somers 
681f5a99677SBrian Somers   sz = physical_MaxDeviceSize();
682f5a99677SBrian Somers   if (p) {
6832cb305afSBrian Somers     if (h && h->device2iov)
6842cb305afSBrian Somers       (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
685f5a99677SBrian Somers     else {
686f5a99677SBrian Somers       iov[*niov].iov_base = malloc(sz);
6872cb305afSBrian Somers       if (h)
6882cb305afSBrian Somers         memcpy(iov[*niov].iov_base, h, sizeof *h);
689f5a99677SBrian Somers       iov[*niov].iov_len = sz;
690f5a99677SBrian Somers       (*niov)++;
691f5a99677SBrian Somers     }
692f5a99677SBrian Somers   } else {
6932cb305afSBrian Somers     iov[*niov].iov_base = NULL;
694f5a99677SBrian Somers     iov[*niov].iov_len = sz;
695f5a99677SBrian Somers     (*niov)++;
696f5a99677SBrian Somers   }
697f5a99677SBrian Somers 
6985d9e6103SBrian Somers   return p ? p->fd : 0;
6995d9e6103SBrian Somers }
7005d9e6103SBrian Somers 
7012cb305afSBrian Somers const char *
7022cb305afSBrian Somers physical_LockedDevice(struct physical *p)
7032cb305afSBrian Somers {
7042cb305afSBrian Somers   if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
7052cb305afSBrian Somers     return p->name.base;
7062cb305afSBrian Somers 
7072cb305afSBrian Somers   return NULL;
7082cb305afSBrian Somers }
7092cb305afSBrian Somers 
7105d9e6103SBrian Somers void
7115d9e6103SBrian Somers physical_ChangedPid(struct physical *p, pid_t newpid)
7125d9e6103SBrian Somers {
7132cb305afSBrian Somers   if (physical_LockedDevice(p)) {
7145d9e6103SBrian Somers     int res;
7155d9e6103SBrian Somers 
7165d9e6103SBrian Somers     if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
7175d9e6103SBrian Somers       log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
7185d9e6103SBrian Somers   }
7195d9e6103SBrian Somers }
7205d9e6103SBrian Somers 
7215d9e6103SBrian Somers int
7225d9e6103SBrian Somers physical_IsSync(struct physical *p)
7235d9e6103SBrian Somers {
7245d9e6103SBrian Somers    return p->cfg.speed == 0;
7255d9e6103SBrian Somers }
7265d9e6103SBrian Somers 
7275d9e6103SBrian Somers const char *physical_GetDevice(struct physical *p)
7285d9e6103SBrian Somers {
7295d9e6103SBrian Somers    return p->name.full;
73063b73463SBrian Somers }
73163b73463SBrian Somers 
73263b73463SBrian Somers void
733dd7e2610SBrian Somers physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
73499294c8bSBrian Somers {
73599294c8bSBrian Somers   int f, pos;
73699294c8bSBrian Somers 
73799294c8bSBrian Somers   p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
73899294c8bSBrian Somers   for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
73999294c8bSBrian Somers     if (pos)
740a8d7acdcSBrian Somers       p->cfg.devlist[pos++] = '\0';
74199294c8bSBrian Somers     strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
74299294c8bSBrian Somers     pos += strlen(p->cfg.devlist + pos);
74399294c8bSBrian Somers   }
744a8d7acdcSBrian Somers   p->cfg.ndev = f;
74563b73463SBrian Somers }
74663b73463SBrian Somers 
74763b73463SBrian Somers void
7485d9e6103SBrian Somers physical_SetSync(struct physical *p)
7495d9e6103SBrian Somers {
7505d9e6103SBrian Somers    p->cfg.speed = 0;
75163b73463SBrian Somers }
75263b73463SBrian Somers 
75363b73463SBrian Somers int
7545d9e6103SBrian Somers physical_SetRtsCts(struct physical *p, int enable)
7555d9e6103SBrian Somers {
7565d9e6103SBrian Somers    p->cfg.rts_cts = enable ? 1 : 0;
75763b73463SBrian Somers    return 1;
75863b73463SBrian Somers }
75963b73463SBrian Somers 
76063b73463SBrian Somers ssize_t
7615d9e6103SBrian Somers physical_Read(struct physical *p, void *buf, size_t nbytes)
7625d9e6103SBrian Somers {
7636815097bSBrian Somers   ssize_t ret;
7646815097bSBrian Somers 
7656815097bSBrian Somers   if (p->handler && p->handler->read)
7666815097bSBrian Somers     ret = (*p->handler->read)(p, buf, nbytes);
7676815097bSBrian Somers   else
7686815097bSBrian Somers     ret = read(p->fd, buf, nbytes);
7696815097bSBrian Somers 
7706815097bSBrian Somers   log_DumpBuff(LogPHYSICAL, "read", buf, ret);
7716815097bSBrian Somers 
7726815097bSBrian Somers   return ret;
77363b73463SBrian Somers }
77463b73463SBrian Somers 
77563b73463SBrian Somers ssize_t
7765d9e6103SBrian Somers physical_Write(struct physical *p, const void *buf, size_t nbytes)
7775d9e6103SBrian Somers {
7786815097bSBrian Somers   log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
7796815097bSBrian Somers 
7806815097bSBrian Somers   if (p->handler && p->handler->write)
7816815097bSBrian Somers     return (*p->handler->write)(p, buf, nbytes);
7826815097bSBrian Somers 
7835d9e6103SBrian Somers   return write(p->fd, buf, nbytes);
78463b73463SBrian Somers }
785ecd5172aSBrian Somers 
786ecd5172aSBrian Somers int
7875d9e6103SBrian Somers physical_doUpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
788b6dec9f0SBrian Somers                      int *n, int force)
78942d4d396SBrian Somers {
79042d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
79142d4d396SBrian Somers   int sets;
79242d4d396SBrian Somers 
79342d4d396SBrian Somers   sets = 0;
794b6dec9f0SBrian Somers   if (p->fd >= 0) {
795b6dec9f0SBrian Somers     if (r) {
796b6dec9f0SBrian Somers       FD_SET(p->fd, r);
79724989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
798b6dec9f0SBrian Somers       sets++;
799b6dec9f0SBrian Somers     }
800b6dec9f0SBrian Somers     if (e) {
801b6dec9f0SBrian Somers       FD_SET(p->fd, e);
80224989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
803b6dec9f0SBrian Somers       sets++;
804b6dec9f0SBrian Somers     }
8050f2f3eb3SBrian Somers     if (w && (force || link_QueueLen(&p->link) || p->out)) {
806b6dec9f0SBrian Somers       FD_SET(p->fd, w);
80724989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
808b6dec9f0SBrian Somers       sets++;
809b6dec9f0SBrian Somers     }
810b6dec9f0SBrian Somers     if (sets && *n < p->fd + 1)
811b6dec9f0SBrian Somers       *n = p->fd + 1;
812b6dec9f0SBrian Somers   }
81342d4d396SBrian Somers 
81442d4d396SBrian Somers   return sets;
81542d4d396SBrian Somers }
81642d4d396SBrian Somers 
81742d4d396SBrian Somers int
818ea722969SBrian Somers physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
819ea722969SBrian Somers {
82087c3786eSBrian Somers   if (p->handler && p->handler->removefromset)
82187c3786eSBrian Somers     return (*p->handler->removefromset)(p, r, w, e);
82287c3786eSBrian Somers   else {
823ea722969SBrian Somers     int sets;
824ea722969SBrian Somers 
825ea722969SBrian Somers     sets = 0;
826ea722969SBrian Somers     if (p->fd >= 0) {
827ea722969SBrian Somers       if (r && FD_ISSET(p->fd, r)) {
828ea722969SBrian Somers         FD_CLR(p->fd, r);
829ea722969SBrian Somers         log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
830ea722969SBrian Somers         sets++;
831ea722969SBrian Somers       }
832ea722969SBrian Somers       if (e && FD_ISSET(p->fd, e)) {
833ea722969SBrian Somers         FD_CLR(p->fd, e);
834ea722969SBrian Somers         log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
835ea722969SBrian Somers         sets++;
836ea722969SBrian Somers       }
837ea722969SBrian Somers       if (w && FD_ISSET(p->fd, w)) {
838ea722969SBrian Somers         FD_CLR(p->fd, w);
839ea722969SBrian Somers         log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
840ea722969SBrian Somers         sets++;
841ea722969SBrian Somers       }
842ea722969SBrian Somers     }
843ea722969SBrian Somers 
844ea722969SBrian Somers     return sets;
845ea722969SBrian Somers   }
84687c3786eSBrian Somers }
847ea722969SBrian Somers 
848ea722969SBrian Somers int
849dd7e2610SBrian Somers physical_IsSet(struct descriptor *d, const fd_set *fdset)
85042d4d396SBrian Somers {
85142d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
85242d4d396SBrian Somers   return p->fd >= 0 && FD_ISSET(p->fd, fdset);
85342d4d396SBrian Somers }
85442d4d396SBrian Somers 
85542d4d396SBrian Somers void
8565d9e6103SBrian Somers physical_Login(struct physical *p, const char *name)
857fc1141b2SBrian Somers {
858229c7625SBrian Somers   if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
859fc1141b2SBrian Somers     struct utmp ut;
8601fa75dc1SBrian Somers     const char *connstr;
861f1b965c9SBrian Somers     char *colon;
862fc1141b2SBrian Somers 
863fc1141b2SBrian Somers     memset(&ut, 0, sizeof ut);
864fc1141b2SBrian Somers     time(&ut.ut_time);
865fc1141b2SBrian Somers     strncpy(ut.ut_name, name, sizeof ut.ut_name);
866f1b965c9SBrian Somers     if (p->handler && (p->handler->type == TCP_DEVICE ||
867f1b965c9SBrian Somers                        p->handler->type == UDP_DEVICE)) {
8681a80353cSBrian Somers       strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line);
869f1b965c9SBrian Somers       strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
870f1b965c9SBrian Somers       colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
871f1b965c9SBrian Somers       if (colon)
872f1b965c9SBrian Somers         *colon = '\0';
873f1b965c9SBrian Somers     } else
8745d9e6103SBrian Somers       strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
8751fa75dc1SBrian Somers     if ((connstr = getenv("CONNECT")))
8761fa75dc1SBrian Somers       /* mgetty sets this to the connection speed */
8771fa75dc1SBrian Somers       strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
878fc1141b2SBrian Somers     ID0login(&ut);
8791a80353cSBrian Somers     p->Utmp = ut.ut_time;
880fc1141b2SBrian Somers   }
881fc1141b2SBrian Somers }
882dd0645c5SBrian Somers 
883dd0645c5SBrian Somers int
884dd0645c5SBrian Somers physical_SetMode(struct physical *p, int mode)
885dd0645c5SBrian Somers {
88692b09558SBrian Somers   if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
88792b09558SBrian Somers        mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
88892b09558SBrian Somers       (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
889dd0645c5SBrian Somers     log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
890dd0645c5SBrian Somers                mode2Nam(p->type), mode2Nam(mode));
891dd0645c5SBrian Somers     return 0;
892dd0645c5SBrian Somers   }
893dd0645c5SBrian Somers   p->type = mode;
894dd0645c5SBrian Somers   return 1;
895dd0645c5SBrian Somers }
8966f8e9f0aSBrian Somers 
8976f8e9f0aSBrian Somers void
8986f8e9f0aSBrian Somers physical_DeleteQueue(struct physical *p)
8996f8e9f0aSBrian Somers {
9006f8e9f0aSBrian Somers   if (p->out) {
9016f8e9f0aSBrian Somers     mbuf_Free(p->out);
9026f8e9f0aSBrian Somers     p->out = NULL;
9036f8e9f0aSBrian Somers   }
9046f8e9f0aSBrian Somers   link_DeleteQueue(&p->link);
9056f8e9f0aSBrian Somers }
9065d9e6103SBrian Somers 
9075d9e6103SBrian Somers void
9085d9e6103SBrian Somers physical_SetDevice(struct physical *p, const char *name)
9095d9e6103SBrian Somers {
9105d9e6103SBrian Somers   int len = strlen(_PATH_DEV);
9115d9e6103SBrian Somers 
912e6923505SBrian Somers   if (name != p->name.full) {
9135d9e6103SBrian Somers     strncpy(p->name.full, name, sizeof p->name.full - 1);
9145d9e6103SBrian Somers     p->name.full[sizeof p->name.full - 1] = '\0';
915e6923505SBrian Somers   }
9165d9e6103SBrian Somers   p->name.base = *p->name.full == '!' ?  p->name.full + 1 :
9175d9e6103SBrian Somers                  strncmp(p->name.full, _PATH_DEV, len) ?
9185d9e6103SBrian Somers                  p->name.full : p->name.full + len;
9195d9e6103SBrian Somers }
9205d9e6103SBrian Somers 
9215d9e6103SBrian Somers static void
9225d9e6103SBrian Somers physical_Found(struct physical *p)
9235d9e6103SBrian Somers {
9249950fb2aSBrian Somers   FILE *lockfile;
9259950fb2aSBrian Somers   char fn[MAXPATHLEN];
9269950fb2aSBrian Somers 
9279950fb2aSBrian Somers   if (*p->name.full == '/') {
9289950fb2aSBrian Somers     snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
9299950fb2aSBrian Somers     lockfile = ID0fopen(fn, "w");
9309950fb2aSBrian Somers     if (lockfile != NULL) {
9319950fb2aSBrian Somers       fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
9329950fb2aSBrian Somers       fclose(lockfile);
9339950fb2aSBrian Somers     }
9349950fb2aSBrian Somers #ifndef RELEASE_CRUNCH
9359950fb2aSBrian Somers     else
9369950fb2aSBrian Somers       log_Printf(LogALERT, "%s: Can't create %s: %s\n",
9379950fb2aSBrian Somers                  p->link.name, fn, strerror(errno));
9389950fb2aSBrian Somers #endif
9399950fb2aSBrian Somers   }
9409950fb2aSBrian Somers 
9415d9e6103SBrian Somers   throughput_start(&p->link.throughput, "physical throughput",
9425d9e6103SBrian Somers                    Enabled(p->dl->bundle, OPT_THROUGHPUT));
9435d9e6103SBrian Somers   p->connect_count++;
9445d9e6103SBrian Somers   p->input.sz = 0;
9455d9e6103SBrian Somers 
9465d9e6103SBrian Somers   log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
9475d9e6103SBrian Somers }
9485d9e6103SBrian Somers 
9495d9e6103SBrian Somers int
9505d9e6103SBrian Somers physical_Open(struct physical *p, struct bundle *bundle)
9515d9e6103SBrian Somers {
95287c3786eSBrian Somers   int devno, h, wasfd, err;
9535d9e6103SBrian Somers   char *dev;
9545d9e6103SBrian Somers 
9555d9e6103SBrian Somers   if (p->fd >= 0)
9565d9e6103SBrian Somers     log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
9575d9e6103SBrian Somers     /* We're going back into "term" mode */
9585d9e6103SBrian Somers   else if (p->type == PHYS_DIRECT) {
9595d9e6103SBrian Somers     physical_SetDevice(p, "");
9606815097bSBrian Somers     p->fd = STDIN_FILENO;
9616815097bSBrian Somers     for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
9626815097bSBrian Somers       p->handler = (*devices[h].create)(p);
9636815097bSBrian Somers     if (p->fd >= 0) {
96427be3ac6SBrian Somers       if (p->handler == NULL) {
965acbd1f00SBrian Somers         physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
96627be3ac6SBrian Somers         log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
96727be3ac6SBrian Somers       }
9685d9e6103SBrian Somers       physical_Found(p);
9695d9e6103SBrian Somers     }
9705d9e6103SBrian Somers   } else {
9715d9e6103SBrian Somers     dev = p->cfg.devlist;
9725d9e6103SBrian Somers     devno = 0;
9735d9e6103SBrian Somers     while (devno < p->cfg.ndev && p->fd < 0) {
9745d9e6103SBrian Somers       physical_SetDevice(p, dev);
9759950fb2aSBrian Somers       if (physical_Lock(p)) {
976ad227d8bSBrian Somers         err = 0;
977ad227d8bSBrian Somers 
978ad227d8bSBrian Somers         if (*p->name.full == '/') {
9796815097bSBrian Somers           p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
980ad227d8bSBrian Somers           if (p->fd < 0)
981ad227d8bSBrian Somers             err = errno;
982ad227d8bSBrian Somers         }
9836815097bSBrian Somers 
98487c3786eSBrian Somers         wasfd = p->fd;
9856815097bSBrian Somers         for (h = 0; h < NDEVICES && p->handler == NULL; h++)
98687c3786eSBrian Somers           if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
9879950fb2aSBrian Somers             break;
9885d9e6103SBrian Somers 
9899950fb2aSBrian Somers         if (p->fd < 0) {
990ad227d8bSBrian Somers           if (h == NDEVICES) {
991ad227d8bSBrian Somers             if (err)
992ad227d8bSBrian Somers 	      log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
993ad227d8bSBrian Somers                          strerror(errno));
994ad227d8bSBrian Somers             else
9955d9e6103SBrian Somers 	      log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
99687c3786eSBrian Somers                          " a '!' or contain at least one ':'\n", p->link.name,
9975d9e6103SBrian Somers                          p->name.full);
998ad227d8bSBrian Somers           }
9999950fb2aSBrian Somers           physical_Unlock(p);
10009950fb2aSBrian Somers         } else
10016815097bSBrian Somers           physical_Found(p);
10029950fb2aSBrian Somers       }
10035d9e6103SBrian Somers       dev += strlen(dev) + 1;
10045d9e6103SBrian Somers       devno++;
10055d9e6103SBrian Somers     }
10065d9e6103SBrian Somers   }
10075d9e6103SBrian Somers 
10085d9e6103SBrian Somers   return p->fd;
10095d9e6103SBrian Somers }
10105d9e6103SBrian Somers 
10115d9e6103SBrian Somers void
1012acbd1f00SBrian Somers physical_SetupStack(struct physical *p, const char *who, int how)
10135d9e6103SBrian Somers {
10145d9e6103SBrian Somers   link_EmptyStack(&p->link);
101587c3786eSBrian Somers   if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
10166815097bSBrian Somers       (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
10175d9e6103SBrian Somers     link_Stack(&p->link, &synclayer);
10185d9e6103SBrian Somers   else {
10195d9e6103SBrian Somers     link_Stack(&p->link, &asynclayer);
10205d9e6103SBrian Somers     link_Stack(&p->link, &hdlclayer);
10215d9e6103SBrian Somers   }
102287c3786eSBrian Somers   if (how != PHYSICAL_FORCE_SYNCNOACF)
10235d9e6103SBrian Somers     link_Stack(&p->link, &acflayer);
10245d9e6103SBrian Somers   link_Stack(&p->link, &protolayer);
10255d9e6103SBrian Somers   link_Stack(&p->link, &lqrlayer);
10265d9e6103SBrian Somers   link_Stack(&p->link, &ccplayer);
10275d9e6103SBrian Somers   link_Stack(&p->link, &vjlayer);
102867b072f7SBrian Somers #ifndef NONAT
102967b072f7SBrian Somers   link_Stack(&p->link, &natlayer);
10305d9e6103SBrian Somers #endif
10316815097bSBrian Somers   if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1032acbd1f00SBrian Somers     log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
10336815097bSBrian Somers     p->cfg.speed = MODEM_SPEED;
10346815097bSBrian Somers   } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
10356815097bSBrian Somers     log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1036acbd1f00SBrian Somers                who);
10376815097bSBrian Somers     physical_SetSync(p);
10386815097bSBrian Somers   }
10396815097bSBrian Somers }
10406815097bSBrian Somers 
10416815097bSBrian Somers void
10426815097bSBrian Somers physical_StopDeviceTimer(struct physical *p)
10436815097bSBrian Somers {
10446815097bSBrian Somers   if (p->handler && p->handler->stoptimer)
10456815097bSBrian Somers     (*p->handler->stoptimer)(p);
10465d9e6103SBrian Somers }
1047eb6e5e05SBrian Somers 
1048eb6e5e05SBrian Somers int
1049eb6e5e05SBrian Somers physical_AwaitCarrier(struct physical *p)
1050eb6e5e05SBrian Somers {
1051eb6e5e05SBrian Somers   if (p->handler && p->handler->awaitcarrier)
1052eb6e5e05SBrian Somers     return (*p->handler->awaitcarrier)(p);
1053eb6e5e05SBrian Somers 
1054eb6e5e05SBrian Somers   return CARRIER_OK;
1055eb6e5e05SBrian Somers }
1056