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>
47 #include <netgraph/ng_async.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/ng_ppp.h>
50 #include <netgraph/ng_tty.h>
60 #include "throughput.h"
66 #include "descriptor.h"
78 #if defined(__mac68k__) || defined(__macppc__)
81 #define CRTS_IFLOW CDTRCTS
82 #define CCTS_OFLOW CDTRCTS
85 #define Online(dev) ((dev)->mbits & TIOCM_CD)
88 struct device dev; /* What struct physical knows about */
89 struct pppTimer Timer; /* CD checks */
90 int mbits; /* Current DCD status */
91 int carrier_seconds; /* seconds before CD is *required* */
94 int speed; /* Pre-line-discipline speed */
95 int fd; /* Pre-line-discipline fd */
96 int disc; /* Old line-discipline */
98 char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
99 int cs; /* A netgraph control socket (maybe) */
101 struct termios ios; /* To be able to reset from raw mode */
104 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
109 return sizeof(struct ttydevice);
113 * tty_Timeout() watches the DCD signal and mentions it if it's status
117 tty_Timeout(void *data)
119 struct physical *p = data;
120 struct ttydevice *dev = device2tty(p->handler);
123 timer_Stop(&dev->Timer);
124 dev->Timer.load = SECTICKS; /* Once a second please */
125 timer_Start(&dev->Timer);
129 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
130 /* we must be a pty ? */
131 if (p->cfg.cd.necessity != CD_DEFAULT)
132 log_Printf(LogWARN, "%s: Carrier ioctl not supported, "
133 "using ``set cd off''\n", p->link.name);
134 timer_Stop(&dev->Timer);
135 dev->mbits = TIOCM_CD;
142 /* First time looking for carrier */
144 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
145 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
146 if (dev->dev.cd.necessity == CD_REQUIRED)
147 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
148 p->link.name, p->name.full);
150 log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
151 p->link.name, p->name.full);
152 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */
154 timer_Stop(&dev->Timer);
155 /* tty_AwaitCarrier() will notice */
158 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
159 p->link.name, p->name.full, dev->carrier_seconds,
164 change = ombits ^ dev->mbits;
165 if (change & TIOCM_CD) {
166 if (dev->mbits & TIOCM_CD)
167 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
169 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
170 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
171 datalink_Down(p->dl, CLOSE_NORMAL);
172 timer_Stop(&dev->Timer);
175 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
176 Online(dev) ? "on" : "off");
181 tty_StartTimer(struct physical *p)
183 struct ttydevice *dev = device2tty(p->handler);
185 timer_Stop(&dev->Timer);
186 dev->Timer.load = SECTICKS;
187 dev->Timer.func = tty_Timeout;
188 dev->Timer.name = "tty CD";
190 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
191 p->link.name, tty_Timeout);
192 timer_Start(&dev->Timer);
196 tty_AwaitCarrier(struct physical *p)
198 struct ttydevice *dev = device2tty(p->handler);
200 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
203 if (dev->mbits == -1) {
204 if (dev->Timer.state == TIMER_STOPPED) {
205 dev->carrier_seconds = 0;
208 return CARRIER_PENDING; /* Not yet ! */
211 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
215 #define tty_SetAsyncParams NULL
216 #define tty_Write NULL
217 #define tty_Read NULL
221 isngtty(struct ttydevice *dev)
223 return dev->real.fd != -1;
227 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
229 struct ttydevice *dev = device2tty(p->handler);
230 char asyncpath[NG_PATHLEN + 1];
231 struct ng_async_cfg cfg;
234 /* Configure the async converter node */
236 snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
237 memset(&cfg, 0, sizeof cfg);
239 cfg.accm = mymap | hismap;
242 log_Printf(LogDEBUG, "Configure async node at %s\n", asyncpath);
243 if (NgSendMsg(dev->cs, asyncpath, NGM_ASYNC_COOKIE,
244 NGM_ASYNC_CMD_SET_CONFIG, &cfg, sizeof cfg) < 0)
245 log_Printf(LogWARN, "%s: Can't configure async node at %s\n",
246 p->link.name, asyncpath);
248 /* No netgraph node, just config the async layer */
249 async_SetLinkParams(&p->async, mymap, hismap);
253 LoadLineDiscipline(struct physical *p)
255 struct ttydevice *dev = device2tty(p->handler);
256 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
257 struct ng_mesg *reply;
258 struct nodeinfo *info;
259 char ttypath[NG_NODELEN + 1];
260 struct ngm_mkpeer ngm;
261 struct ngm_connect ngc;
262 int ldisc, cs, ds, hot, speed;
265 * Don't use the netgraph line discipline for now. Using it works, but
266 * carrier cannot be detected via TIOCMGET and the device doesn't become
267 * selectable with 0 bytes to read when carrier is lost :(
271 reply = (struct ng_mesg *)rbuf;
272 info = (struct nodeinfo *)reply->data;
274 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
277 /* Get the speed before loading the line discipline */
278 speed = physical_GetSpeed(p);
280 if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
281 log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
285 ldisc = NETGRAPHDISC;
286 if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
287 log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
292 /* Get the name of the tty node */
293 if (ioctl(p->fd, NGIOCGINFO, info) < 0) {
294 log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name,
296 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
299 snprintf(ttypath, sizeof ttypath, "%s:", info->name);
301 /* Create a socket node for our endpoint (and to send messages via) */
302 if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) {
303 log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name,
305 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
309 /* Set the ``hot char'' on the TTY node */
311 log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot);
312 if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE,
313 NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) {
314 log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name);
318 /* Attach an async converter node */
319 snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE);
320 snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK);
321 snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC);
322 log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name,
323 ngm.peerhook, ttypath, ngm.ourhook);
324 if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE,
325 NGM_MKPEER, &ngm, sizeof ngm) < 0) {
326 log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name,
331 /* Connect the async node to our socket */
332 snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK);
333 snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC);
334 memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook);
335 log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name,
336 ngc.path, ngc.peerhook, ngc.ourhook);
337 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT,
338 &ngc, sizeof ngc) < 0) {
339 log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n",
340 p->link.name, ngc.ourhook, ngc.path, ngc.peerhook,
345 /* Get the async node id */
346 if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) {
347 log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n",
348 p->link.name, ngc.path, strerror(errno));
351 if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) {
352 log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n",
353 p->link.name, ngc.path, strerror(errno));
357 /* All done, set up our device state */
358 snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
360 dev->real.fd = p->fd;
362 dev->real.speed = speed;
365 tty_SetAsyncParams(p, 0xffffffff, 0xffffffff);
366 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
367 log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n",
373 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
381 UnloadLineDiscipline(struct physical *p)
383 struct ttydevice *dev = device2tty(p->handler);
386 log_Printf(LogPHASE, "back to speed %d\n", dev->real.speed);
387 if (!physical_SetSpeed(p, dev->real.speed))
388 log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed);
391 p->fd = dev->real.fd;
396 if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) {
397 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
398 log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n",
401 log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
407 tty_Write(struct physical *p, const void *v, size_t n)
409 struct ttydevice *dev = device2tty(p->handler);
412 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
414 return write(p->fd, v, n);
418 tty_Read(struct physical *p, void *v, size_t n)
420 struct ttydevice *dev = device2tty(p->handler);
421 char hook[sizeof NG_ASYNC_HOOK_SYNC];
424 return NgRecvData(p->fd, v, n, hook);
426 return read(p->fd, v, n);
429 #endif /* NETGRAPH */
432 tty_Raw(struct physical *p)
434 struct ttydevice *dev = device2tty(p->handler);
438 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
440 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
441 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
442 p->link.name, p->fd, dev->mbits);
444 if (!physical_IsSync(p)) {
446 if (!LoadLineDiscipline(p))
449 tcgetattr(p->fd, &ios);
452 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
454 ios.c_cflag |= CLOCAL;
456 if (p->type != PHYS_DEDICATED)
457 ios.c_cflag |= HUPCL;
459 if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
460 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
465 oldflag = fcntl(p->fd, F_GETFL, 0);
468 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
474 tty_Offline(struct physical *p)
476 struct ttydevice *dev = device2tty(p->handler);
479 timer_Stop(&dev->Timer);
480 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */
484 tcgetattr(p->fd, &tio);
485 if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1)
486 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
493 tty_Cooked(struct physical *p)
495 struct ttydevice *dev = device2tty(p->handler);
498 tty_Offline(p); /* In case of emergency close()s */
500 tcflush(p->fd, TCIOFLUSH);
502 if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
503 log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
507 UnloadLineDiscipline(p);
510 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
511 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
515 tty_StopTimer(struct physical *p)
517 struct ttydevice *dev = device2tty(p->handler);
519 timer_Stop(&dev->Timer);
523 tty_Free(struct physical *p)
525 struct ttydevice *dev = device2tty(p->handler);
527 tty_Offline(p); /* In case of emergency close()s */
532 tty_Speed(struct physical *p)
536 if (tcgetattr(p->fd, &ios) == -1)
539 return SpeedToInt(cfgetispeed(&ios));
543 tty_OpenInfo(struct physical *p)
545 struct ttydevice *dev = device2tty(p->handler);
552 strcat(buf, " carrier");
558 tty_Slot(struct physical *p)
564 for (slot = 1; (ttyp = getttyent()); ++slot)
565 if (!strcmp(ttyp->ty_name, p->name.base)) {
575 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
576 int maxiov, int *auxfd, int *nauxfd)
578 struct ttydevice *dev = device2tty(d);
579 int sz = physical_MaxDeviceSize();
581 iov[*niov].iov_base = realloc(d, sz);
582 if (iov[*niov].iov_base == NULL) {
583 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
584 AbortProgram(EX_OSERR);
586 iov[*niov].iov_len = sz;
596 if (dev->Timer.state != TIMER_STOPPED) {
597 timer_Stop(&dev->Timer);
598 dev->Timer.state = TIMER_RUNNING;
602 static struct device basettydevice = {
606 { CD_VARIABLE, DEF_TTYCDDELAY },
624 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
625 int maxiov, int *auxfd, int *nauxfd)
627 if (type == TTY_DEVICE) {
628 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
630 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
632 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
634 AbortProgram(EX_OSERR);
645 /* Refresh function pointers etc */
646 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
648 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
649 if (dev->Timer.state != TIMER_STOPPED) {
650 dev->Timer.state = TIMER_STOPPED;
651 p->handler = &dev->dev; /* For the benefit of StartTimer */
661 tty_Create(struct physical *p)
663 struct ttydevice *dev;
667 if (p->fd < 0 || !isatty(p->fd))
668 /* Don't want this */
671 if (*p->name.full == '\0') {
672 physical_SetDevice(p, ttyname(p->fd));
673 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
674 p->link.name, p->name.full);
676 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
678 /* We're gonna return a ttydevice (unless something goes horribly wrong) */
680 if ((dev = malloc(sizeof *dev)) == NULL) {
681 /* Complete failure - parent doesn't continue trying to ``create'' */
687 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
688 memset(&dev->Timer, '\0', sizeof dev->Timer);
696 tcgetattr(p->fd, &ios);
699 if (p->cfg.cd.necessity != CD_DEFAULT)
700 /* Any override is ok for the tty device */
701 dev->dev.cd = p->cfg.cd;
703 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
704 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
705 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
709 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
711 ios.c_cflag |= CLOCAL;
712 ios.c_iflag |= IXOFF;
715 if (p->type != PHYS_DEDICATED)
716 ios.c_cflag |= HUPCL;
718 if (p->type != PHYS_DIRECT) {
719 /* Change tty speed when we're not in -direct mode */
720 ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
721 ios.c_cflag |= p->cfg.parity;
722 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
723 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
724 p->link.name, p->name.full, p->cfg.speed);
727 if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
728 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
730 if (p->type != PHYS_DIRECT && p->cfg.speed > 115200)
731 log_Printf(LogWARN, "%.*s Perhaps the speed is unsupported\n",
732 (int)strlen(p->link.name), "");
735 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
736 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
737 (u_long)ios.c_oflag, (u_long)ios.c_cflag);
739 oldflag = fcntl(p->fd, F_GETFL, 0);
741 /* Complete failure - parent doesn't continue trying to ``create'' */
743 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
744 p->link.name, strerror(errno));
751 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
753 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);