2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__)
32 #include <sys/ioctl.h>
46 #include <netgraph/ng_async.h>
47 #include <netgraph/ng_message.h>
48 #include <netgraph/ng_ppp.h>
49 #include <netgraph/ng_tty.h>
59 #include "throughput.h"
65 #include "descriptor.h"
77 #if defined(__mac68k__) || defined(__macppc__)
80 #define CRTS_IFLOW CDTRCTS
81 #define CCTS_OFLOW CDTRCTS
84 #define Online(dev) ((dev)->mbits & TIOCM_CD)
87 struct device dev; /* What struct physical knows about */
88 struct pppTimer Timer; /* CD checks */
89 int mbits; /* Current DCD status */
90 int carrier_seconds; /* seconds before CD is *required* */
93 int speed; /* Pre-line-discipline speed */
94 int fd; /* Pre-line-discipline fd */
95 int disc; /* Old line-discipline */
97 char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
98 int cs; /* A netgraph control socket (maybe) */
100 struct termios ios; /* To be able to reset from raw mode */
103 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
108 return sizeof(struct ttydevice);
112 * tty_Timeout() watches the DCD signal and mentions it if it's status
116 tty_Timeout(void *data)
118 struct physical *p = data;
119 struct ttydevice *dev = device2tty(p->handler);
122 timer_Stop(&dev->Timer);
123 dev->Timer.load = SECTICKS; /* Once a second please */
124 timer_Start(&dev->Timer);
128 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
129 /* we must be a pty ? */
130 if (p->cfg.cd.necessity != CD_DEFAULT)
131 log_Printf(LogWARN, "%s: Carrier ioctl not supported, "
132 "using ``set cd off''\n", p->link.name);
133 timer_Stop(&dev->Timer);
134 dev->mbits = TIOCM_CD;
141 /* First time looking for carrier */
143 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
144 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
145 if (dev->dev.cd.necessity == CD_REQUIRED)
146 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
147 p->link.name, p->name.full);
149 log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
150 p->link.name, p->name.full);
151 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */
153 timer_Stop(&dev->Timer);
154 /* tty_AwaitCarrier() will notice */
157 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
158 p->link.name, p->name.full, dev->carrier_seconds,
163 change = ombits ^ dev->mbits;
164 if (change & TIOCM_CD) {
165 if (dev->mbits & TIOCM_CD)
166 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
168 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
169 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
170 datalink_Down(p->dl, CLOSE_NORMAL);
171 timer_Stop(&dev->Timer);
174 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
175 Online(dev) ? "on" : "off");
180 tty_StartTimer(struct physical *p)
182 struct ttydevice *dev = device2tty(p->handler);
184 timer_Stop(&dev->Timer);
185 dev->Timer.load = SECTICKS;
186 dev->Timer.func = tty_Timeout;
187 dev->Timer.name = "tty CD";
189 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
190 p->link.name, tty_Timeout);
191 timer_Start(&dev->Timer);
195 tty_AwaitCarrier(struct physical *p)
197 struct ttydevice *dev = device2tty(p->handler);
199 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
202 if (dev->mbits == -1) {
203 if (dev->Timer.state == TIMER_STOPPED) {
204 dev->carrier_seconds = 0;
207 return CARRIER_PENDING; /* Not yet ! */
210 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
214 #define tty_SetAsyncParams NULL
215 #define tty_Write NULL
216 #define tty_Read NULL
220 isngtty(struct ttydevice *dev)
222 return dev->real.fd != -1;
226 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
228 struct ttydevice *dev = device2tty(p->handler);
229 char asyncpath[NG_PATHLEN + 1];
230 struct ng_async_cfg cfg;
233 /* Configure the async converter node */
235 snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
236 memset(&cfg, 0, sizeof cfg);
238 cfg.accm = mymap | hismap;
241 log_Printf(LogDEBUG, "Configure async node at %s\n", asyncpath);
242 if (NgSendMsg(dev->cs, asyncpath, NGM_ASYNC_COOKIE,
243 NGM_ASYNC_CMD_SET_CONFIG, &cfg, sizeof cfg) < 0)
244 log_Printf(LogWARN, "%s: Can't configure async node at %s\n",
245 p->link.name, asyncpath);
247 /* No netgraph node, just config the async layer */
248 async_SetLinkParams(&p->async, mymap, hismap);
252 LoadLineDiscipline(struct physical *p)
254 struct ttydevice *dev = device2tty(p->handler);
255 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
256 struct ng_mesg *reply;
257 struct nodeinfo *info;
258 char ttypath[NG_NODELEN + 1];
259 struct ngm_mkpeer ngm;
260 struct ngm_connect ngc;
261 int ldisc, cs, ds, hot, speed;
264 * Don't use the netgraph line discipline for now. Using it works, but
265 * carrier cannot be detected via TIOCMGET and the device doesn't become
266 * selectable with 0 bytes to read when carrier is lost :(
270 reply = (struct ng_mesg *)rbuf;
271 info = (struct nodeinfo *)reply->data;
273 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
276 /* Get the speed before loading the line discipline */
277 speed = physical_GetSpeed(p);
279 if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
280 log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
284 ldisc = NETGRAPHDISC;
285 if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
286 log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
291 /* Get the name of the tty node */
292 if (ioctl(p->fd, NGIOCGINFO, info) < 0) {
293 log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name,
295 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
298 snprintf(ttypath, sizeof ttypath, "%s:", info->name);
300 /* Create a socket node for our endpoint (and to send messages via) */
301 if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) {
302 log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name,
304 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
308 /* Set the ``hot char'' on the TTY node */
310 log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot);
311 if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE,
312 NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) {
313 log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name);
317 /* Attach an async converter node */
318 snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE);
319 snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK);
320 snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC);
321 log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name,
322 ngm.peerhook, ttypath, ngm.ourhook);
323 if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE,
324 NGM_MKPEER, &ngm, sizeof ngm) < 0) {
325 log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name,
330 /* Connect the async node to our socket */
331 snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK);
332 snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC);
333 memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook);
334 log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name,
335 ngc.path, ngc.peerhook, ngc.ourhook);
336 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT,
337 &ngc, sizeof ngc) < 0) {
338 log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n",
339 p->link.name, ngc.ourhook, ngc.path, ngc.peerhook,
344 /* Get the async node id */
345 if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) {
346 log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n",
347 p->link.name, ngc.path, strerror(errno));
350 if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) {
351 log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n",
352 p->link.name, ngc.path, strerror(errno));
356 /* All done, set up our device state */
357 snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
359 dev->real.fd = p->fd;
361 dev->real.speed = speed;
364 tty_SetAsyncParams(p, 0xffffffff, 0xffffffff);
365 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
366 log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n",
372 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
380 UnloadLineDiscipline(struct physical *p)
382 struct ttydevice *dev = device2tty(p->handler);
385 log_Printf(LogPHASE, "back to speed %d\n", dev->real.speed);
386 if (!physical_SetSpeed(p, dev->real.speed))
387 log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed);
390 p->fd = dev->real.fd;
395 if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) {
396 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
397 log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n",
400 log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
406 tty_Write(struct physical *p, const void *v, size_t n)
408 struct ttydevice *dev = device2tty(p->handler);
411 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
413 return write(p->fd, v, n);
417 tty_Read(struct physical *p, void *v, size_t n)
419 struct ttydevice *dev = device2tty(p->handler);
420 char hook[sizeof NG_ASYNC_HOOK_SYNC];
423 return NgRecvData(p->fd, v, n, hook);
425 return read(p->fd, v, n);
428 #endif /* NETGRAPH */
431 tty_Raw(struct physical *p)
433 struct ttydevice *dev = device2tty(p->handler);
437 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
439 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
440 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
441 p->link.name, p->fd, dev->mbits);
443 if (!physical_IsSync(p)) {
445 if (!LoadLineDiscipline(p))
448 tcgetattr(p->fd, &ios);
451 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
453 ios.c_cflag |= CLOCAL;
455 if (p->type != PHYS_DEDICATED)
456 ios.c_cflag |= HUPCL;
458 if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
459 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
464 oldflag = fcntl(p->fd, F_GETFL, 0);
467 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
473 tty_Offline(struct physical *p)
475 struct ttydevice *dev = device2tty(p->handler);
478 timer_Stop(&dev->Timer);
479 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */
483 tcgetattr(p->fd, &tio);
484 if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1)
485 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
492 tty_Cooked(struct physical *p)
494 struct ttydevice *dev = device2tty(p->handler);
497 tty_Offline(p); /* In case of emergency close()s */
499 tcflush(p->fd, TCIOFLUSH);
501 if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
502 log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
506 UnloadLineDiscipline(p);
509 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
510 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
514 tty_StopTimer(struct physical *p)
516 struct ttydevice *dev = device2tty(p->handler);
518 timer_Stop(&dev->Timer);
522 tty_Free(struct physical *p)
524 struct ttydevice *dev = device2tty(p->handler);
526 tty_Offline(p); /* In case of emergency close()s */
531 tty_Speed(struct physical *p)
535 if (tcgetattr(p->fd, &ios) == -1)
538 return SpeedToInt(cfgetispeed(&ios));
542 tty_OpenInfo(struct physical *p)
544 struct ttydevice *dev = device2tty(p->handler);
551 strcat(buf, " carrier");
557 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
558 int maxiov, int *auxfd, int *nauxfd)
560 struct ttydevice *dev = device2tty(d);
561 int sz = physical_MaxDeviceSize();
563 iov[*niov].iov_base = realloc(d, sz);
564 if (iov[*niov].iov_base == NULL) {
565 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
566 AbortProgram(EX_OSERR);
568 iov[*niov].iov_len = sz;
578 if (dev->Timer.state != TIMER_STOPPED) {
579 timer_Stop(&dev->Timer);
580 dev->Timer.state = TIMER_RUNNING;
584 static struct device basettydevice = {
588 { CD_VARIABLE, DEF_TTYCDDELAY },
605 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
606 int maxiov, int *auxfd, int *nauxfd)
608 if (type == TTY_DEVICE) {
609 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
611 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
613 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
615 AbortProgram(EX_OSERR);
626 /* Refresh function pointers etc */
627 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
629 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
630 if (dev->Timer.state != TIMER_STOPPED) {
631 dev->Timer.state = TIMER_STOPPED;
632 p->handler = &dev->dev; /* For the benefit of StartTimer */
642 tty_Create(struct physical *p)
644 struct ttydevice *dev;
648 if (p->fd < 0 || !isatty(p->fd))
649 /* Don't want this */
652 if (*p->name.full == '\0') {
653 physical_SetDevice(p, ttyname(p->fd));
654 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
655 p->link.name, p->name.full);
657 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
659 /* We're gonna return a ttydevice (unless something goes horribly wrong) */
661 if ((dev = malloc(sizeof *dev)) == NULL) {
662 /* Complete failure - parent doesn't continue trying to ``create'' */
668 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
669 memset(&dev->Timer, '\0', sizeof dev->Timer);
677 tcgetattr(p->fd, &ios);
680 if (p->cfg.cd.necessity != CD_DEFAULT)
681 /* Any override is ok for the tty device */
682 dev->dev.cd = p->cfg.cd;
684 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
685 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
686 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
690 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
692 ios.c_cflag |= CLOCAL;
693 ios.c_iflag |= IXOFF;
696 if (p->type != PHYS_DEDICATED)
697 ios.c_cflag |= HUPCL;
699 if (p->type != PHYS_DIRECT) {
700 /* Change tty speed when we're not in -direct mode */
701 ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
702 ios.c_cflag |= p->cfg.parity;
703 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
704 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
705 p->link.name, p->name.full, p->cfg.speed);
708 if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
709 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
711 if (p->type != PHYS_DIRECT && p->cfg.speed > 115200)
712 log_Printf(LogWARN, "%.*s Perhaps the speed is unsupported\n",
713 (int)strlen(p->link.name), "");
716 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
717 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
718 (u_long)ios.c_oflag, (u_long)ios.c_cflag);
720 oldflag = fcntl(p->fd, F_GETFL, 0);
722 /* Complete failure - parent doesn't continue trying to ``create'' */
724 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
725 p->link.name, strerror(errno));
732 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
734 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);