]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/tty.c
This commit was generated by cvs2svn to compensate for changes in r95419,
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / tty.c
1 /*-
2  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/un.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__)
32 #include <sys/ioctl.h>
33 #endif
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <sys/uio.h>
42 #include <termios.h>
43 #include <unistd.h>
44 #ifndef NONETGRAPH
45 #include <netgraph.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>
50 #endif
51
52 #include "layer.h"
53 #include "defs.h"
54 #include "mbuf.h"
55 #include "log.h"
56 #include "timer.h"
57 #include "lqr.h"
58 #include "hdlc.h"
59 #include "throughput.h"
60 #include "fsm.h"
61 #include "lcp.h"
62 #include "ccp.h"
63 #include "link.h"
64 #include "async.h"
65 #include "descriptor.h"
66 #include "physical.h"
67 #include "mp.h"
68 #include "chat.h"
69 #include "auth.h"
70 #include "chap.h"
71 #include "cbcp.h"
72 #include "datalink.h"
73 #include "main.h"
74 #include "id.h"
75 #include "tty.h"
76
77 #if defined(__mac68k__) || defined(__macppc__)
78 #undef  CRTS_IFLOW
79 #undef  CCTS_OFLOW
80 #define CRTS_IFLOW      CDTRCTS
81 #define CCTS_OFLOW      CDTRCTS
82 #endif
83
84 #define Online(dev)     ((dev)->mbits & TIOCM_CD)
85
86 struct ttydevice {
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* */
91 #ifndef NONETGRAPH
92   struct {
93     int speed;                  /* Pre-line-discipline speed */
94     int fd;                     /* Pre-line-discipline fd */
95     int disc;                   /* Old line-discipline */
96   } real;
97   char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
98   int cs;                       /* A netgraph control socket (maybe) */
99 #endif
100   struct termios ios;           /* To be able to reset from raw mode */
101 };
102
103 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
104
105 int
106 tty_DeviceSize(void)
107 {
108   return sizeof(struct ttydevice);
109 }
110
111 /*
112  * tty_Timeout() watches the DCD signal and mentions it if it's status
113  * changes.
114  */
115 static void
116 tty_Timeout(void *data)
117 {
118   struct physical *p = data;
119   struct ttydevice *dev = device2tty(p->handler);
120   int ombits, change;
121
122   timer_Stop(&dev->Timer);
123   dev->Timer.load = SECTICKS;           /* Once a second please */
124   timer_Start(&dev->Timer);
125   ombits = dev->mbits;
126
127   if (p->fd >= 0) {
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;
135       return;
136     }
137   } else
138     dev->mbits = 0;
139
140   if (ombits == -1) {
141     /* First time looking for carrier */
142     if (Online(dev))
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);
148       else {
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 ? */
152       }
153       timer_Stop(&dev->Timer);
154       /* tty_AwaitCarrier() will notice */
155     } else {
156       /* Keep waiting */
157       log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
158                  p->link.name, p->name.full, dev->carrier_seconds,
159                  dev->dev.cd.delay);
160       dev->mbits = -1;
161     }
162   } else {
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);
167       else {
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);
172       }
173     } else
174       log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
175                  Online(dev) ? "on" : "off");
176   }
177 }
178
179 static void
180 tty_StartTimer(struct physical *p)
181 {
182   struct ttydevice *dev = device2tty(p->handler);
183
184   timer_Stop(&dev->Timer);
185   dev->Timer.load = SECTICKS;
186   dev->Timer.func = tty_Timeout;
187   dev->Timer.name = "tty CD";
188   dev->Timer.arg = p;
189   log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
190              p->link.name, tty_Timeout);
191   timer_Start(&dev->Timer);
192 }
193
194 static int
195 tty_AwaitCarrier(struct physical *p)
196 {
197   struct ttydevice *dev = device2tty(p->handler);
198
199   if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
200     return CARRIER_OK;
201
202   if (dev->mbits == -1) {
203     if (dev->Timer.state == TIMER_STOPPED) {
204       dev->carrier_seconds = 0;
205       tty_StartTimer(p);
206     }
207     return CARRIER_PENDING;                     /* Not yet ! */
208   }
209
210   return Online(dev) ? CARRIER_OK : CARRIER_LOST;
211 }
212
213 #ifdef NONETGRAPH
214 #define tty_SetAsyncParams      NULL
215 #define tty_Write               NULL
216 #define tty_Read                NULL
217 #else
218
219 static int
220 isngtty(struct ttydevice *dev)
221 {
222   return dev->real.fd != -1;
223 }
224
225 static void
226 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
227 {
228   struct ttydevice *dev = device2tty(p->handler);
229   char asyncpath[NG_PATHLEN + 1];
230   struct ng_async_cfg cfg;
231
232   if (isngtty(dev)) {
233     /* Configure the async converter node */
234
235     snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
236     memset(&cfg, 0, sizeof cfg);
237     cfg.enabled = 1;
238     cfg.accm = mymap | hismap;
239     cfg.amru = MAX_MTU;
240     cfg.smru = MAX_MRU;
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);
246   } else
247     /* No netgraph node, just config the async layer */
248     async_SetLinkParams(&p->async, mymap, hismap);
249 }
250
251 static int
252 LoadLineDiscipline(struct physical *p)
253 {
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;
262
263   /*
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 :(
267    */
268   return 0;
269
270   reply = (struct ng_mesg *)rbuf;
271   info = (struct nodeinfo *)reply->data;
272
273   loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
274               NULL);
275
276   /* Get the speed before loading the line discipline */
277   speed = physical_GetSpeed(p);
278
279   if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
280     log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
281                p->link.name);
282     return 0;
283   }
284   ldisc = NETGRAPHDISC;
285   if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
286     log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
287                p->link.name);
288     return 0;
289   }
290
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,
294                strerror(errno));
295     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
296     return 0;
297   }
298   snprintf(ttypath, sizeof ttypath, "%s:", info->name);
299
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,
303                strerror(errno));
304     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
305     return 0;
306   }
307
308   /* Set the ``hot char'' on the TTY node */
309   hot = HDLC_SYN;
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);
314     goto failed;
315   }
316
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,
326                NG_ASYNC_NODE_TYPE);
327     goto failed;
328   }
329
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,
340                strerror(errno));
341     goto failed;
342   }
343
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));
348     goto failed;
349   }
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));
353     goto failed;
354   }
355
356   /* All done, set up our device state */
357   snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
358   dev->cs = cs;
359   dev->real.fd = p->fd;
360   p->fd = ds;
361   dev->real.speed = speed;
362   physical_SetSync(p);
363
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",
367              p->link.name);
368
369   return 1;
370
371 failed:
372   ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
373   close(ds);
374   close(cs);
375
376   return 0;
377 }
378
379 static void
380 UnloadLineDiscipline(struct physical *p)
381 {
382   struct ttydevice *dev = device2tty(p->handler);
383
384   if (isngtty(dev)) {
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);
388     dev->real.speed = 0;
389     close(p->fd);
390     p->fd = dev->real.fd;
391     dev->real.fd = -1;
392     close(dev->cs);
393     dev->cs = -1;
394     *dev->hook = '\0';
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",
398                  p->link.name);
399     } else
400       log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
401                  p->link.name);
402   }
403 }
404
405 static ssize_t
406 tty_Write(struct physical *p, const void *v, size_t n)
407 {
408   struct ttydevice *dev = device2tty(p->handler);
409
410   if (isngtty(dev))
411     return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
412   else
413     return write(p->fd, v, n);
414 }
415
416 static ssize_t
417 tty_Read(struct physical *p, void *v, size_t n)
418 {
419   struct ttydevice *dev = device2tty(p->handler);
420   char hook[sizeof NG_ASYNC_HOOK_SYNC];
421
422   if (isngtty(dev))
423     return NgRecvData(p->fd, v, n, hook);
424   else
425     return read(p->fd, v, n);
426 }
427
428 #endif /* NETGRAPH */
429
430 static int
431 tty_Raw(struct physical *p)
432 {
433   struct ttydevice *dev = device2tty(p->handler);
434   struct termios ios;
435   int oldflag;
436
437   log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
438
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);
442
443   if (!physical_IsSync(p)) {
444 #ifndef NONETGRAPH
445     if (!LoadLineDiscipline(p))
446 #endif
447     {
448       tcgetattr(p->fd, &ios);
449       cfmakeraw(&ios);
450       if (p->cfg.rts_cts)
451         ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
452       else
453         ios.c_cflag |= CLOCAL;
454
455       if (p->type != PHYS_DEDICATED)
456         ios.c_cflag |= HUPCL;
457
458       if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
459         log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
460                    p->link.name);
461     }
462   }
463
464   oldflag = fcntl(p->fd, F_GETFL, 0);
465   if (oldflag < 0)
466     return 0;
467   fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
468
469   return 1;
470 }
471
472 static void
473 tty_Offline(struct physical *p)
474 {
475   struct ttydevice *dev = device2tty(p->handler);
476
477   if (p->fd >= 0) {
478     timer_Stop(&dev->Timer);
479     dev->mbits &= ~TIOCM_DTR;   /* XXX: Hmm, what's this supposed to do ? */
480     if (Online(dev)) {
481       struct termios tio;
482
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",
486                    p->link.name);
487     }
488   }
489 }
490
491 static void
492 tty_Cooked(struct physical *p)
493 {
494   struct ttydevice *dev = device2tty(p->handler);
495   int oldflag;
496
497   tty_Offline(p);       /* In case of emergency close()s */
498
499   tcflush(p->fd, TCIOFLUSH);
500
501   if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
502     log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
503                p->link.name);
504
505 #ifndef NONETGRAPH
506   UnloadLineDiscipline(p);
507 #endif
508
509   if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
510     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
511 }
512
513 static void
514 tty_StopTimer(struct physical *p)
515 {
516   struct ttydevice *dev = device2tty(p->handler);
517
518   timer_Stop(&dev->Timer);
519 }
520
521 static void
522 tty_Free(struct physical *p)
523 {
524   struct ttydevice *dev = device2tty(p->handler);
525
526   tty_Offline(p);       /* In case of emergency close()s */
527   free(dev);
528 }
529
530 static int
531 tty_Speed(struct physical *p)
532 {
533   struct termios ios;
534
535   if (tcgetattr(p->fd, &ios) == -1)
536     return 0;
537
538   return SpeedToInt(cfgetispeed(&ios));
539 }
540
541 static const char *
542 tty_OpenInfo(struct physical *p)
543 {
544   struct ttydevice *dev = device2tty(p->handler);
545   static char buf[13];
546
547   if (Online(dev))
548     strcpy(buf, "with");
549   else
550     strcpy(buf, "no");
551   strcat(buf, " carrier");
552
553   return buf;
554 }
555
556 static void
557 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
558                int maxiov, int *auxfd, int *nauxfd)
559 {
560   struct ttydevice *dev = device2tty(d);
561   int sz = physical_MaxDeviceSize();
562
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);
567   }
568   iov[*niov].iov_len = sz;
569   (*niov)++;
570
571 #ifndef NONETGRAPH
572   if (dev->cs >= 0) {
573     *auxfd = dev->cs;
574     (*nauxfd)++;
575   }
576 #endif
577
578   if (dev->Timer.state != TIMER_STOPPED) {
579     timer_Stop(&dev->Timer);
580     dev->Timer.state = TIMER_RUNNING;
581   }
582 }
583
584 static struct device basettydevice = {
585   TTY_DEVICE,
586   "tty",
587   0,
588   { CD_VARIABLE, DEF_TTYCDDELAY },
589   tty_AwaitCarrier,
590   NULL,
591   tty_Raw,
592   tty_Offline,
593   tty_Cooked,
594   tty_SetAsyncParams,
595   tty_StopTimer,
596   tty_Free,
597   tty_Read,
598   tty_Write,
599   tty_device2iov,
600   tty_Speed,
601   tty_OpenInfo
602 };
603
604 struct device *
605 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
606                int maxiov, int *auxfd, int *nauxfd)
607 {
608   if (type == TTY_DEVICE) {
609     struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
610
611     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
612     if (dev == NULL) {
613       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
614                  (int)(sizeof *dev));
615       AbortProgram(EX_OSERR);
616     }
617
618 #ifndef NONETGRAPH
619     if (*nauxfd) {
620       dev->cs = *auxfd;
621       (*nauxfd)--;
622     } else
623       dev->cs = -1;
624 #endif
625
626     /* Refresh function pointers etc */
627     memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
628
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 */
633       tty_StartTimer(p);
634     }
635     return &dev->dev;
636   }
637
638   return NULL;
639 }
640
641 struct device *
642 tty_Create(struct physical *p)
643 {
644   struct ttydevice *dev;
645   struct termios ios;
646   int oldflag;
647
648   if (p->fd < 0 || !isatty(p->fd))
649     /* Don't want this */
650     return NULL;
651
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);
656   } else
657     log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
658
659   /* We're gonna return a ttydevice (unless something goes horribly wrong) */
660
661   if ((dev = malloc(sizeof *dev)) == NULL) {
662     /* Complete failure - parent doesn't continue trying to ``create'' */
663     close(p->fd);
664     p->fd = -1;
665     return NULL;
666   }
667
668   memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
669   memset(&dev->Timer, '\0', sizeof dev->Timer);
670   dev->mbits = -1;
671 #ifndef NONETGRAPH
672   dev->real.speed = 0;
673   dev->real.fd = -1;
674   dev->real.disc = -1;
675   *dev->hook = '\0';
676 #endif
677   tcgetattr(p->fd, &ios);
678   dev->ios = ios;
679
680   if (p->cfg.cd.necessity != CD_DEFAULT)
681     /* Any override is ok for the tty device */
682     dev->dev.cd = p->cfg.cd;
683
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);
687
688   cfmakeraw(&ios);
689   if (p->cfg.rts_cts)
690     ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
691   else {
692     ios.c_cflag |= CLOCAL;
693     ios.c_iflag |= IXOFF;
694   }
695   ios.c_iflag |= IXON;
696   if (p->type != PHYS_DEDICATED)
697     ios.c_cflag |= HUPCL;
698
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);
706   }
707
708   if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
709     log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
710                p->link.name);
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), "");
714   }
715
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);
719
720   oldflag = fcntl(p->fd, F_GETFL, 0);
721   if (oldflag < 0) {
722     /* Complete failure - parent doesn't continue trying to ``create'' */
723
724     log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
725                p->link.name, strerror(errno));
726     tty_Cooked(p);
727     close(p->fd);
728     p->fd = -1;
729     free(dev);
730     return NULL;
731   } else
732     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
733
734   physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
735
736   return &dev->dev;
737 }