2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #if defined(__OpenBSD__) || defined(__NetBSD__)
34 #include <sys/ioctl.h>
49 #include <netgraph/ng_async.h>
50 #include <netgraph/ng_message.h>
51 #include <netgraph/ng_ppp.h>
52 #include <netgraph/ng_tty.h>
62 #include "throughput.h"
68 #include "descriptor.h"
80 #if defined(__mac68k__) || defined(__macppc__)
83 #define CRTS_IFLOW CDTRCTS
84 #define CCTS_OFLOW CDTRCTS
87 #define Online(dev) ((dev)->mbits & TIOCM_CD)
90 struct device dev; /* What struct physical knows about */
91 struct pppTimer Timer; /* CD checks */
92 int mbits; /* Current DCD status */
93 int carrier_seconds; /* seconds before CD is *required* */
96 unsigned speed; /* Pre-line-discipline speed */
97 int fd; /* Pre-line-discipline fd */
98 int disc; /* Old line-discipline */
100 char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
101 int cs; /* A netgraph control socket (maybe) */
103 struct termios ios; /* To be able to reset from raw mode */
106 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
111 return sizeof(struct ttydevice);
115 * tty_Timeout() watches the DCD signal and mentions it if it's status
119 tty_Timeout(void *data)
121 struct physical *p = data;
122 struct ttydevice *dev = device2tty(p->handler);
125 timer_Stop(&dev->Timer);
126 dev->Timer.load = SECTICKS; /* Once a second please */
127 timer_Start(&dev->Timer);
131 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
132 /* we must be a pty ? */
133 if (p->cfg.cd.necessity != CD_DEFAULT)
134 log_Printf(LogWARN, "%s: Carrier ioctl not supported, "
135 "using ``set cd off''\n", p->link.name);
136 timer_Stop(&dev->Timer);
137 dev->mbits = TIOCM_CD;
144 /* First time looking for carrier */
146 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
147 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
148 if (dev->dev.cd.necessity == CD_REQUIRED)
149 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
150 p->link.name, p->name.full);
152 log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
153 p->link.name, p->name.full);
154 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */
156 timer_Stop(&dev->Timer);
157 /* tty_AwaitCarrier() will notice */
160 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
161 p->link.name, p->name.full, dev->carrier_seconds,
166 change = ombits ^ dev->mbits;
167 if (change & TIOCM_CD) {
168 if (dev->mbits & TIOCM_CD)
169 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
171 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
172 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
173 datalink_Down(p->dl, CLOSE_NORMAL);
174 timer_Stop(&dev->Timer);
177 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
178 Online(dev) ? "on" : "off");
183 tty_StartTimer(struct physical *p)
185 struct ttydevice *dev = device2tty(p->handler);
187 timer_Stop(&dev->Timer);
188 dev->Timer.load = SECTICKS;
189 dev->Timer.func = tty_Timeout;
190 dev->Timer.name = "tty CD";
192 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
193 p->link.name, tty_Timeout);
194 timer_Start(&dev->Timer);
198 tty_AwaitCarrier(struct physical *p)
200 struct ttydevice *dev = device2tty(p->handler);
202 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
205 if (dev->mbits == -1) {
206 if (dev->Timer.state == TIMER_STOPPED) {
207 dev->carrier_seconds = 0;
210 return CARRIER_PENDING; /* Not yet ! */
213 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
217 #define tty_SetAsyncParams NULL
218 #define tty_Write NULL
219 #define tty_Read NULL
223 isngtty(struct ttydevice *dev)
225 return dev->real.fd != -1;
229 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
231 struct ttydevice *dev = device2tty(p->handler);
232 char asyncpath[NG_PATHSIZ];
233 struct ng_async_cfg cfg;
236 /* Configure the async converter node */
238 snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
239 memset(&cfg, 0, sizeof cfg);
241 cfg.accm = mymap | hismap;
244 log_Printf(LogDEBUG, "Configure async node at %s\n", asyncpath);
245 if (NgSendMsg(dev->cs, asyncpath, NGM_ASYNC_COOKIE,
246 NGM_ASYNC_CMD_SET_CONFIG, &cfg, sizeof cfg) < 0)
247 log_Printf(LogWARN, "%s: Can't configure async node at %s\n",
248 p->link.name, asyncpath);
250 /* No netgraph node, just config the async layer */
251 async_SetLinkParams(&p->async, mymap, hismap);
255 LoadLineDiscipline(struct physical *p)
257 struct ttydevice *dev = device2tty(p->handler);
258 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
259 struct ng_mesg *reply;
260 struct nodeinfo *info;
261 char ttypath[NG_NODESIZ];
262 struct ngm_mkpeer ngm;
263 struct ngm_connect ngc;
264 int ldisc, cs, ds, hot;
268 * Don't use the netgraph line discipline for now. Using it works, but
269 * carrier cannot be detected via TIOCMGET and the device doesn't become
270 * selectable with 0 bytes to read when carrier is lost :(
274 reply = (struct ng_mesg *)rbuf;
275 info = (struct nodeinfo *)reply->data;
277 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
280 /* Get the speed before loading the line discipline */
281 speed = physical_GetSpeed(p);
283 if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
284 log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
288 ldisc = NETGRAPHDISC;
289 if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
290 log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
295 /* Get the name of the tty node */
296 if (ioctl(p->fd, NGIOCGINFO, info) < 0) {
297 log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name,
299 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
302 snprintf(ttypath, sizeof ttypath, "%s:", info->name);
304 /* Create a socket node for our endpoint (and to send messages via) */
305 if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) {
306 log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name,
308 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
312 /* Set the ``hot char'' on the TTY node */
314 log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot);
315 if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE,
316 NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) {
317 log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name);
321 /* Attach an async converter node */
322 snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE);
323 snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK);
324 snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC);
325 log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name,
326 ngm.peerhook, ttypath, ngm.ourhook);
327 if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE,
328 NGM_MKPEER, &ngm, sizeof ngm) < 0) {
329 log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name,
334 /* Connect the async node to our socket */
335 snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK);
336 snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC);
337 memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook);
338 log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name,
339 ngc.path, ngc.peerhook, ngc.ourhook);
340 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT,
341 &ngc, sizeof ngc) < 0) {
342 log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n",
343 p->link.name, ngc.ourhook, ngc.path, ngc.peerhook,
348 /* Get the async node id */
349 if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) {
350 log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n",
351 p->link.name, ngc.path, strerror(errno));
354 if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) {
355 log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n",
356 p->link.name, ngc.path, strerror(errno));
360 /* All done, set up our device state */
361 snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
363 dev->real.fd = p->fd;
365 dev->real.speed = speed;
368 tty_SetAsyncParams(p, 0xffffffff, 0xffffffff);
369 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
370 log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n",
376 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
384 UnloadLineDiscipline(struct physical *p)
386 struct ttydevice *dev = device2tty(p->handler);
389 if (!physical_SetSpeed(p, dev->real.speed))
390 log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed);
393 p->fd = dev->real.fd;
398 if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) {
399 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
400 log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n",
403 log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
409 tty_Write(struct physical *p, const void *v, size_t n)
411 struct ttydevice *dev = device2tty(p->handler);
414 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : (ssize_t)n;
416 return write(p->fd, v, n);
420 tty_Read(struct physical *p, void *v, size_t n)
422 struct ttydevice *dev = device2tty(p->handler);
423 char hook[sizeof NG_ASYNC_HOOK_SYNC];
426 return NgRecvData(p->fd, v, n, hook);
428 return read(p->fd, v, n);
431 #endif /* NETGRAPH */
434 tty_Raw(struct physical *p)
436 struct ttydevice *dev = device2tty(p->handler);
440 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
442 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
443 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
444 p->link.name, p->fd, dev->mbits);
446 if (!physical_IsSync(p)) {
448 if (!LoadLineDiscipline(p))
451 tcgetattr(p->fd, &ios);
454 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
456 ios.c_cflag |= CLOCAL;
458 if (p->type != PHYS_DEDICATED)
459 ios.c_cflag |= HUPCL;
461 if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
462 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
467 oldflag = fcntl(p->fd, F_GETFL, 0);
470 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
476 tty_Offline(struct physical *p)
478 struct ttydevice *dev = device2tty(p->handler);
481 timer_Stop(&dev->Timer);
482 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */
486 tcgetattr(p->fd, &tio);
487 if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1)
488 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
495 tty_Cooked(struct physical *p)
497 struct ttydevice *dev = device2tty(p->handler);
500 tty_Offline(p); /* In case of emergency close()s */
502 tcflush(p->fd, TCIOFLUSH);
504 if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
505 log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
509 UnloadLineDiscipline(p);
512 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
513 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
517 tty_StopTimer(struct physical *p)
519 struct ttydevice *dev = device2tty(p->handler);
521 timer_Stop(&dev->Timer);
525 tty_Free(struct physical *p)
527 struct ttydevice *dev = device2tty(p->handler);
529 tty_Offline(p); /* In case of emergency close()s */
534 tty_Speed(struct physical *p)
538 if (tcgetattr(p->fd, &ios) == -1)
541 return SpeedToUnsigned(cfgetispeed(&ios));
545 tty_OpenInfo(struct physical *p)
547 struct ttydevice *dev = device2tty(p->handler);
554 strcat(buf, " carrier");
560 tty_Slot(struct physical *p)
566 for (slot = 1; (ttyp = getttyent()); ++slot)
567 if (!strcmp(ttyp->ty_name, p->name.base)) {
577 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
580 int *auxfd, int *nauxfd
582 int *auxfd __unused, int *nauxfd __unused
586 struct ttydevice *dev;
587 int sz = physical_MaxDeviceSize();
589 iov[*niov].iov_base = d = realloc(d, sz);
591 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
592 AbortProgram(EX_OSERR);
594 iov[*niov].iov_len = sz;
606 if (dev->Timer.state != TIMER_STOPPED) {
607 timer_Stop(&dev->Timer);
608 dev->Timer.state = TIMER_RUNNING;
612 static struct device basettydevice = {
616 { CD_VARIABLE, DEF_TTYCDDELAY },
634 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
637 int *auxfd, int *nauxfd
639 int *auxfd __unused, int *nauxfd __unused
643 if (type == TTY_DEVICE) {
644 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
646 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
648 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
650 AbortProgram(EX_OSERR);
661 /* Refresh function pointers etc */
662 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
664 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
665 if (dev->Timer.state != TIMER_STOPPED) {
666 dev->Timer.state = TIMER_STOPPED;
667 p->handler = &dev->dev; /* For the benefit of StartTimer */
677 tty_Create(struct physical *p)
679 struct ttydevice *dev;
683 if (p->fd < 0 || !isatty(p->fd))
684 /* Don't want this */
687 if (*p->name.full == '\0') {
688 physical_SetDevice(p, ttyname(p->fd));
689 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
690 p->link.name, p->name.full);
692 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
694 /* We're gonna return a ttydevice (unless something goes horribly wrong) */
696 if ((dev = malloc(sizeof *dev)) == NULL) {
697 /* Complete failure - parent doesn't continue trying to ``create'' */
703 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
704 memset(&dev->Timer, '\0', sizeof dev->Timer);
712 tcgetattr(p->fd, &ios);
715 if (p->cfg.cd.necessity != CD_DEFAULT)
716 /* Any override is ok for the tty device */
717 dev->dev.cd = p->cfg.cd;
719 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
720 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
721 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
725 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
727 ios.c_cflag |= CLOCAL;
728 ios.c_iflag |= IXOFF;
731 if (p->type != PHYS_DEDICATED)
732 ios.c_cflag |= HUPCL;
734 if (p->type != PHYS_DIRECT) {
735 /* Change tty speed when we're not in -direct mode */
736 ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
737 ios.c_cflag |= p->cfg.parity;
738 if (cfsetspeed(&ios, UnsignedToSpeed(p->cfg.speed)) == -1)
739 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
740 p->link.name, p->name.full, p->cfg.speed);
743 if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
744 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
746 if (p->type != PHYS_DIRECT && p->cfg.speed > 115200)
747 log_Printf(LogWARN, "%.*s Perhaps the speed is unsupported\n",
748 (int)strlen(p->link.name), "");
751 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
752 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
753 (u_long)ios.c_oflag, (u_long)ios.c_cflag);
755 oldflag = fcntl(p->fd, F_GETFL, 0);
757 /* Complete failure - parent doesn't continue trying to ``create'' */
759 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
760 p->link.name, strerror(errno));
767 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
769 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);