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>
45 #include <netgraph/ng_async.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/ng_ppp.h>
48 #include <netgraph/ng_tty.h>
57 #include "throughput.h"
63 #include "descriptor.h"
75 #if defined(__mac68k__) || defined(__macppc__)
78 #define CRTS_IFLOW CDTRCTS
79 #define CCTS_OFLOW CDTRCTS
82 #define Online(dev) ((dev)->mbits & TIOCM_CD)
85 struct device dev; /* What struct physical knows about */
86 struct pppTimer Timer; /* CD checks */
87 int mbits; /* Current DCD status */
88 int carrier_seconds; /* seconds before CD is *required* */
91 int speed; /* Pre-line-discipline speed */
92 int fd; /* Pre-line-discipline fd */
93 int disc; /* Old line-discipline */
95 char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
96 int cs; /* A netgraph control socket (maybe) */
98 struct termios ios; /* To be able to reset from raw mode */
101 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
106 return sizeof(struct ttydevice);
110 * tty_Timeout() watches the DCD signal and mentions it if it's status
114 tty_Timeout(void *data)
116 struct physical *p = data;
117 struct ttydevice *dev = device2tty(p->handler);
120 timer_Stop(&dev->Timer);
121 dev->Timer.load = SECTICKS; /* Once a second please */
122 timer_Start(&dev->Timer);
126 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
127 /* we must be a pty ? */
128 if (p->cfg.cd.necessity != CD_DEFAULT)
129 log_Printf(LogWARN, "%s: Carrier ioctl not supported, "
130 "using ``set cd off''\n", p->link.name);
131 timer_Stop(&dev->Timer);
132 dev->mbits = TIOCM_CD;
139 /* First time looking for carrier */
141 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
142 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
143 if (dev->dev.cd.necessity == CD_REQUIRED)
144 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
145 p->link.name, p->name.full);
147 log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
148 p->link.name, p->name.full);
149 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */
151 timer_Stop(&dev->Timer);
152 /* tty_AwaitCarrier() will notice */
155 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
156 p->link.name, p->name.full, dev->carrier_seconds,
161 change = ombits ^ dev->mbits;
162 if (change & TIOCM_CD) {
163 if (dev->mbits & TIOCM_CD)
164 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
166 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
167 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
168 datalink_Down(p->dl, CLOSE_NORMAL);
169 timer_Stop(&dev->Timer);
172 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
173 Online(dev) ? "on" : "off");
178 tty_StartTimer(struct physical *p)
180 struct ttydevice *dev = device2tty(p->handler);
182 timer_Stop(&dev->Timer);
183 dev->Timer.load = SECTICKS;
184 dev->Timer.func = tty_Timeout;
185 dev->Timer.name = "tty CD";
187 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
188 p->link.name, tty_Timeout);
189 timer_Start(&dev->Timer);
193 tty_AwaitCarrier(struct physical *p)
195 struct ttydevice *dev = device2tty(p->handler);
197 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
200 if (dev->mbits == -1) {
201 if (dev->Timer.state == TIMER_STOPPED) {
202 dev->carrier_seconds = 0;
205 return CARRIER_PENDING; /* Not yet ! */
208 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
212 #define tty_SetAsyncParams NULL
213 #define tty_Write NULL
214 #define tty_Read NULL
218 isngtty(struct ttydevice *dev)
220 return dev->real.fd != -1;
224 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
226 struct ttydevice *dev = device2tty(p->handler);
227 char asyncpath[NG_PATHLEN + 1];
228 struct ng_async_cfg cfg;
231 /* Configure the async converter node */
233 snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
234 memset(&cfg, 0, sizeof cfg);
236 cfg.accm = mymap | hismap;
239 log_Printf(LogDEBUG, "Configure async node at %s\n", asyncpath);
240 if (NgSendMsg(dev->cs, asyncpath, NGM_ASYNC_COOKIE,
241 NGM_ASYNC_CMD_SET_CONFIG, &cfg, sizeof cfg) < 0)
242 log_Printf(LogWARN, "%s: Can't configure async node at %s\n",
243 p->link.name, asyncpath);
245 /* No netgraph node, just config the async layer */
246 async_SetLinkParams(&p->async, mymap, hismap);
250 LoadLineDiscipline(struct physical *p)
252 struct ttydevice *dev = device2tty(p->handler);
253 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
254 struct ng_mesg *reply;
255 struct nodeinfo *info;
256 char ttypath[NG_NODELEN + 1];
257 struct ngm_mkpeer ngm;
258 struct ngm_connect ngc;
259 int ldisc, cs, ds, hot, speed;
261 reply = (struct ng_mesg *)rbuf;
262 info = (struct nodeinfo *)reply->data;
264 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
267 /* Get the speed before loading the line discipline */
268 speed = physical_GetSpeed(p);
270 if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
271 log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
275 ldisc = NETGRAPHDISC;
276 if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
277 log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
282 /* Get the name of the tty node */
283 if (ioctl(p->fd, NGIOCGINFO, info) < 0) {
284 log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name,
286 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
289 snprintf(ttypath, sizeof ttypath, "%s:", info->name);
291 /* Create a socket node for our endpoint (and to send messages via) */
292 if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) {
293 log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name,
295 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
299 /* Set the ``hot char'' on the TTY node */
301 log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot);
302 if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE,
303 NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) {
304 log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name);
308 /* Attach an async converter node */
309 snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE);
310 snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK);
311 snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC);
312 log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name,
313 ngm.peerhook, ttypath, ngm.ourhook);
314 if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE,
315 NGM_MKPEER, &ngm, sizeof ngm) < 0) {
316 log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name,
321 /* Connect the async node to our socket */
322 snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK);
323 snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC);
324 memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook);
325 log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name,
326 ngc.path, ngc.peerhook, ngc.ourhook);
327 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT,
328 &ngc, sizeof ngc) < 0) {
329 log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n",
330 p->link.name, ngc.ourhook, ngc.path, ngc.peerhook,
335 /* Get the async node id */
336 if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) {
337 log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n",
338 p->link.name, ngc.path, strerror(errno));
341 if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) {
342 log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n",
343 p->link.name, ngc.path, strerror(errno));
347 /* All done, set up our device state */
348 snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
350 dev->real.fd = p->fd;
352 dev->real.speed = speed;
355 tty_SetAsyncParams(p, 0xffffffff, 0xffffffff);
356 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
357 log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n",
363 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
371 UnloadLineDiscipline(struct physical *p)
373 struct ttydevice *dev = device2tty(p->handler);
376 log_Printf(LogPHASE, "back to speed %d\n", dev->real.speed);
377 if (!physical_SetSpeed(p, dev->real.speed))
378 log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed);
381 p->fd = dev->real.fd;
386 if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) {
387 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
388 log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n",
391 log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
397 tty_Write(struct physical *p, const void *v, size_t n)
399 struct ttydevice *dev = device2tty(p->handler);
402 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
404 return write(p->fd, v, n);
408 tty_Read(struct physical *p, void *v, size_t n)
410 struct ttydevice *dev = device2tty(p->handler);
411 char hook[sizeof NG_ASYNC_HOOK_SYNC];
414 return NgRecvData(p->fd, v, n, hook);
416 return read(p->fd, v, n);
419 #endif /* NETGRAPH */
422 tty_Raw(struct physical *p)
424 struct ttydevice *dev = device2tty(p->handler);
428 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
430 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
431 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
432 p->link.name, p->fd, dev->mbits);
434 if (!physical_IsSync(p)) {
436 if (!LoadLineDiscipline(p))
439 tcgetattr(p->fd, &ios);
442 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
444 ios.c_cflag |= CLOCAL;
446 if (p->type != PHYS_DEDICATED)
447 ios.c_cflag |= HUPCL;
449 if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
450 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
455 oldflag = fcntl(p->fd, F_GETFL, 0);
458 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
464 tty_Offline(struct physical *p)
466 struct ttydevice *dev = device2tty(p->handler);
469 timer_Stop(&dev->Timer);
470 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */
474 tcgetattr(p->fd, &tio);
475 if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1)
476 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
483 tty_Cooked(struct physical *p)
485 struct ttydevice *dev = device2tty(p->handler);
488 tty_Offline(p); /* In case of emergency close()s */
490 tcflush(p->fd, TCIOFLUSH);
492 if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
493 log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
497 UnloadLineDiscipline(p);
500 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
501 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
505 tty_StopTimer(struct physical *p)
507 struct ttydevice *dev = device2tty(p->handler);
509 timer_Stop(&dev->Timer);
513 tty_Free(struct physical *p)
515 struct ttydevice *dev = device2tty(p->handler);
517 tty_Offline(p); /* In case of emergency close()s */
522 tty_Speed(struct physical *p)
526 if (tcgetattr(p->fd, &ios) == -1)
529 return SpeedToInt(cfgetispeed(&ios));
533 tty_OpenInfo(struct physical *p)
535 struct ttydevice *dev = device2tty(p->handler);
542 strcat(buf, " carrier");
548 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
549 int maxiov, int *auxfd, int *nauxfd)
551 struct ttydevice *dev = device2tty(d);
552 int sz = physical_MaxDeviceSize();
554 iov[*niov].iov_base = realloc(d, sz);
555 if (iov[*niov].iov_base == NULL) {
556 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
557 AbortProgram(EX_OSERR);
559 iov[*niov].iov_len = sz;
569 if (dev->Timer.state != TIMER_STOPPED) {
570 timer_Stop(&dev->Timer);
571 dev->Timer.state = TIMER_RUNNING;
575 static struct device basettydevice = {
579 { CD_VARIABLE, DEF_TTYCDDELAY },
596 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
597 int maxiov, int *auxfd, int *nauxfd)
599 if (type == TTY_DEVICE) {
600 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
602 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
604 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
606 AbortProgram(EX_OSERR);
617 /* Refresh function pointers etc */
618 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
620 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
621 if (dev->Timer.state != TIMER_STOPPED) {
622 dev->Timer.state = TIMER_STOPPED;
623 p->handler = &dev->dev; /* For the benefit of StartTimer */
633 tty_Create(struct physical *p)
635 struct ttydevice *dev;
639 if (p->fd < 0 || !isatty(p->fd))
640 /* Don't want this */
643 if (*p->name.full == '\0') {
644 physical_SetDevice(p, ttyname(p->fd));
645 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
646 p->link.name, p->name.full);
648 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
650 /* We're gonna return a ttydevice (unless something goes horribly wrong) */
652 if ((dev = malloc(sizeof *dev)) == NULL) {
653 /* Complete failure - parent doesn't continue trying to ``create'' */
659 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
660 memset(&dev->Timer, '\0', sizeof dev->Timer);
668 tcgetattr(p->fd, &ios);
671 if (p->cfg.cd.necessity != CD_DEFAULT)
672 /* Any override is ok for the tty device */
673 dev->dev.cd = p->cfg.cd;
675 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
676 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
677 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
681 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
683 ios.c_cflag |= CLOCAL;
684 ios.c_iflag |= IXOFF;
687 if (p->type != PHYS_DEDICATED)
688 ios.c_cflag |= HUPCL;
690 if (p->type != PHYS_DIRECT) {
691 /* Change tty speed when we're not in -direct mode */
692 ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
693 ios.c_cflag |= p->cfg.parity;
694 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
695 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
696 p->link.name, p->name.full, p->cfg.speed);
699 if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
700 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
702 if (p->type != PHYS_DIRECT && p->cfg.speed > 115200)
703 log_Printf(LogWARN, "%.*s Perhaps the speed is unsupported\n",
704 (int)strlen(p->link.name), "");
707 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
708 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
709 (u_long)ios.c_oflag, (u_long)ios.c_cflag);
711 oldflag = fcntl(p->fd, F_GETFL, 0);
713 /* Complete failure - parent doesn't continue trying to ``create'' */
715 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
716 p->link.name, strerror(errno));
723 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
725 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);