]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/tty.c
Merge the NETGRAPH branch into HEAD. tty devices now use netgraph's line
[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 #include <netgraph.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>
49
50 #include "layer.h"
51 #include "defs.h"
52 #include "mbuf.h"
53 #include "log.h"
54 #include "timer.h"
55 #include "lqr.h"
56 #include "hdlc.h"
57 #include "throughput.h"
58 #include "fsm.h"
59 #include "lcp.h"
60 #include "ccp.h"
61 #include "link.h"
62 #include "async.h"
63 #include "descriptor.h"
64 #include "physical.h"
65 #include "mp.h"
66 #include "chat.h"
67 #include "auth.h"
68 #include "chap.h"
69 #include "cbcp.h"
70 #include "datalink.h"
71 #include "main.h"
72 #include "id.h"
73 #include "tty.h"
74
75 #if defined(__mac68k__) || defined(__macppc__)
76 #undef  CRTS_IFLOW
77 #undef  CCTS_OFLOW
78 #define CRTS_IFLOW      CDTRCTS
79 #define CCTS_OFLOW      CDTRCTS
80 #endif
81
82 #define Online(dev)     ((dev)->mbits & TIOCM_CD)
83
84 struct ttydevice {
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* */
89 #ifndef NONETGRAPH
90   struct {
91     int speed;                  /* Pre-line-discipline speed */
92     int fd;                     /* Pre-line-discipline fd */
93     int disc;                   /* Old line-discipline */
94   } real;
95   char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */
96   int cs;                       /* A netgraph control socket (maybe) */
97 #endif
98   struct termios ios;           /* To be able to reset from raw mode */
99 };
100
101 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
102
103 int
104 tty_DeviceSize(void)
105 {
106   return sizeof(struct ttydevice);
107 }
108
109 /*
110  * tty_Timeout() watches the DCD signal and mentions it if it's status
111  * changes.
112  */
113 static void
114 tty_Timeout(void *data)
115 {
116   struct physical *p = data;
117   struct ttydevice *dev = device2tty(p->handler);
118   int ombits, change;
119
120   timer_Stop(&dev->Timer);
121   dev->Timer.load = SECTICKS;           /* Once a second please */
122   timer_Start(&dev->Timer);
123   ombits = dev->mbits;
124
125   if (p->fd >= 0) {
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;
133       return;
134     }
135   } else
136     dev->mbits = 0;
137
138   if (ombits == -1) {
139     /* First time looking for carrier */
140     if (Online(dev))
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);
146       else {
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 ? */
150       }
151       timer_Stop(&dev->Timer);
152       /* tty_AwaitCarrier() will notice */
153     } else {
154       /* Keep waiting */
155       log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
156                  p->link.name, p->name.full, dev->carrier_seconds,
157                  dev->dev.cd.delay);
158       dev->mbits = -1;
159     }
160   } else {
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);
165       else {
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);
170       }
171     } else
172       log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
173                  Online(dev) ? "on" : "off");
174   }
175 }
176
177 static void
178 tty_StartTimer(struct physical *p)
179 {
180   struct ttydevice *dev = device2tty(p->handler);
181
182   timer_Stop(&dev->Timer);
183   dev->Timer.load = SECTICKS;
184   dev->Timer.func = tty_Timeout;
185   dev->Timer.name = "tty CD";
186   dev->Timer.arg = p;
187   log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
188              p->link.name, tty_Timeout);
189   timer_Start(&dev->Timer);
190 }
191
192 static int
193 tty_AwaitCarrier(struct physical *p)
194 {
195   struct ttydevice *dev = device2tty(p->handler);
196
197   if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
198     return CARRIER_OK;
199
200   if (dev->mbits == -1) {
201     if (dev->Timer.state == TIMER_STOPPED) {
202       dev->carrier_seconds = 0;
203       tty_StartTimer(p);
204     }
205     return CARRIER_PENDING;                     /* Not yet ! */
206   }
207
208   return Online(dev) ? CARRIER_OK : CARRIER_LOST;
209 }
210
211 #ifdef NONETGRAPH
212 #define tty_SetAsyncParams      NULL
213 #define tty_Write               NULL
214 #define tty_Read                NULL
215 #else
216
217 static int
218 isngtty(struct ttydevice *dev)
219 {
220   return dev->real.fd != -1;
221 }
222
223 static void
224 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
225 {
226   struct ttydevice *dev = device2tty(p->handler);
227   char asyncpath[NG_PATHLEN + 1];
228   struct ng_async_cfg cfg;
229
230   if (isngtty(dev)) {
231     /* Configure the async converter node */
232
233     snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook);
234     memset(&cfg, 0, sizeof cfg);
235     cfg.enabled = 1;
236     cfg.accm = mymap | hismap;
237     cfg.amru = MAX_MTU;
238     cfg.smru = MAX_MRU;
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);
244   } else
245     /* No netgraph node, just config the async layer */
246     async_SetLinkParams(&p->async, mymap, hismap);
247 }
248
249 static int
250 LoadLineDiscipline(struct physical *p)
251 {
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;
260
261   reply = (struct ng_mesg *)rbuf;
262   info = (struct nodeinfo *)reply->data;
263
264   loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
265               NULL);
266
267   /* Get the speed before loading the line discipline */
268   speed = physical_GetSpeed(p);
269
270   if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
271     log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
272                p->link.name);
273     return 0;
274   }
275   ldisc = NETGRAPHDISC;
276   if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
277     log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
278                p->link.name);
279     return 0;
280   }
281
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,
285                strerror(errno));
286     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
287     return 0;
288   }
289   snprintf(ttypath, sizeof ttypath, "%s:", info->name);
290
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,
294                strerror(errno));
295     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
296     return 0;
297   }
298
299   /* Set the ``hot char'' on the TTY node */
300   hot = HDLC_SYN;
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);
305     goto failed;
306   }
307
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,
317                NG_ASYNC_NODE_TYPE);
318     goto failed;
319   }
320
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,
331                strerror(errno));
332     goto failed;
333   }
334
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));
339     goto failed;
340   }
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));
344     goto failed;
345   }
346
347   /* All done, set up our device state */
348   snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
349   dev->cs = cs;
350   dev->real.fd = p->fd;
351   p->fd = ds;
352   dev->real.speed = speed;
353   physical_SetSync(p);
354
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",
358              p->link.name);
359
360   return 1;
361
362 failed:
363   ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
364   close(ds);
365   close(cs);
366
367   return 0;
368 }
369
370 static void
371 UnloadLineDiscipline(struct physical *p)
372 {
373   struct ttydevice *dev = device2tty(p->handler);
374
375   if (isngtty(dev)) {
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);
379     dev->real.speed = 0;
380     close(p->fd);
381     p->fd = dev->real.fd;
382     dev->real.fd = -1;
383     close(dev->cs);
384     dev->cs = -1;
385     *dev->hook = '\0';
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",
389                  p->link.name);
390     } else
391       log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
392                  p->link.name);
393   }
394 }
395
396 static ssize_t
397 tty_Write(struct physical *p, const void *v, size_t n)
398 {
399   struct ttydevice *dev = device2tty(p->handler);
400
401   if (isngtty(dev))
402     return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
403   else
404     return write(p->fd, v, n);
405 }
406
407 static ssize_t
408 tty_Read(struct physical *p, void *v, size_t n)
409 {
410   struct ttydevice *dev = device2tty(p->handler);
411   char hook[sizeof NG_ASYNC_HOOK_SYNC];
412
413   if (isngtty(dev))
414     return NgRecvData(p->fd, v, n, hook);
415   else
416     return read(p->fd, v, n);
417 }
418
419 #endif /* NETGRAPH */
420
421 static int
422 tty_Raw(struct physical *p)
423 {
424   struct ttydevice *dev = device2tty(p->handler);
425   struct termios ios;
426   int oldflag;
427
428   log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
429
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);
433
434   if (!physical_IsSync(p)) {
435 #ifndef NONETGRAPH
436     if (!LoadLineDiscipline(p))
437 #endif
438     {
439       tcgetattr(p->fd, &ios);
440       cfmakeraw(&ios);
441       if (p->cfg.rts_cts)
442         ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
443       else
444         ios.c_cflag |= CLOCAL;
445
446       if (p->type != PHYS_DEDICATED)
447         ios.c_cflag |= HUPCL;
448
449       if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
450         log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
451                    p->link.name);
452     }
453   }
454
455   oldflag = fcntl(p->fd, F_GETFL, 0);
456   if (oldflag < 0)
457     return 0;
458   fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
459
460   return 1;
461 }
462
463 static void
464 tty_Offline(struct physical *p)
465 {
466   struct ttydevice *dev = device2tty(p->handler);
467
468   if (p->fd >= 0) {
469     timer_Stop(&dev->Timer);
470     dev->mbits &= ~TIOCM_DTR;   /* XXX: Hmm, what's this supposed to do ? */
471     if (Online(dev)) {
472       struct termios tio;
473
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",
477                    p->link.name);
478     }
479   }
480 }
481
482 static void
483 tty_Cooked(struct physical *p)
484 {
485   struct ttydevice *dev = device2tty(p->handler);
486   int oldflag;
487
488   tty_Offline(p);       /* In case of emergency close()s */
489
490   tcflush(p->fd, TCIOFLUSH);
491
492   if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
493     log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
494                p->link.name);
495
496 #ifndef NONETGRAPH
497   UnloadLineDiscipline(p);
498 #endif
499
500   if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
501     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
502 }
503
504 static void
505 tty_StopTimer(struct physical *p)
506 {
507   struct ttydevice *dev = device2tty(p->handler);
508
509   timer_Stop(&dev->Timer);
510 }
511
512 static void
513 tty_Free(struct physical *p)
514 {
515   struct ttydevice *dev = device2tty(p->handler);
516
517   tty_Offline(p);       /* In case of emergency close()s */
518   free(dev);
519 }
520
521 static int
522 tty_Speed(struct physical *p)
523 {
524   struct termios ios;
525
526   if (tcgetattr(p->fd, &ios) == -1)
527     return 0;
528
529   return SpeedToInt(cfgetispeed(&ios));
530 }
531
532 static const char *
533 tty_OpenInfo(struct physical *p)
534 {
535   struct ttydevice *dev = device2tty(p->handler);
536   static char buf[13];
537
538   if (Online(dev))
539     strcpy(buf, "with");
540   else
541     strcpy(buf, "no");
542   strcat(buf, " carrier");
543
544   return buf;
545 }
546
547 static void
548 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
549                int maxiov, int *auxfd, int *nauxfd)
550 {
551   struct ttydevice *dev = device2tty(d);
552   int sz = physical_MaxDeviceSize();
553
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);
558   }
559   iov[*niov].iov_len = sz;
560   (*niov)++;
561
562 #ifndef NONETGRAPH
563   if (dev->cs >= 0) {
564     *auxfd = dev->cs;
565     (*nauxfd)++;
566   }
567 #endif
568
569   if (dev->Timer.state != TIMER_STOPPED) {
570     timer_Stop(&dev->Timer);
571     dev->Timer.state = TIMER_RUNNING;
572   }
573 }
574
575 static struct device basettydevice = {
576   TTY_DEVICE,
577   "tty",
578   0,
579   { CD_VARIABLE, DEF_TTYCDDELAY },
580   tty_AwaitCarrier,
581   NULL,
582   tty_Raw,
583   tty_Offline,
584   tty_Cooked,
585   tty_SetAsyncParams,
586   tty_StopTimer,
587   tty_Free,
588   tty_Read,
589   tty_Write,
590   tty_device2iov,
591   tty_Speed,
592   tty_OpenInfo
593 };
594
595 struct device *
596 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
597                int maxiov, int *auxfd, int *nauxfd)
598 {
599   if (type == TTY_DEVICE) {
600     struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
601
602     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
603     if (dev == NULL) {
604       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
605                  (int)(sizeof *dev));
606       AbortProgram(EX_OSERR);
607     }
608
609 #ifndef NONETGRAPH
610     if (*nauxfd) {
611       dev->cs = *auxfd;
612       (*nauxfd)--;
613     } else
614       dev->cs = -1;
615 #endif
616
617     /* Refresh function pointers etc */
618     memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
619
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 */
624       tty_StartTimer(p);
625     }
626     return &dev->dev;
627   }
628
629   return NULL;
630 }
631
632 struct device *
633 tty_Create(struct physical *p)
634 {
635   struct ttydevice *dev;
636   struct termios ios;
637   int oldflag;
638
639   if (p->fd < 0 || !isatty(p->fd))
640     /* Don't want this */
641     return NULL;
642
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);
647   } else
648     log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
649
650   /* We're gonna return a ttydevice (unless something goes horribly wrong) */
651
652   if ((dev = malloc(sizeof *dev)) == NULL) {
653     /* Complete failure - parent doesn't continue trying to ``create'' */
654     close(p->fd);
655     p->fd = -1;
656     return NULL;
657   }
658
659   memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
660   memset(&dev->Timer, '\0', sizeof dev->Timer);
661   dev->mbits = -1;
662 #ifndef NONETGRAPH
663   dev->real.speed = 0;
664   dev->real.fd = -1;
665   dev->real.disc = -1;
666   *dev->hook = '\0';
667 #endif
668   tcgetattr(p->fd, &ios);
669   dev->ios = ios;
670
671   if (p->cfg.cd.necessity != CD_DEFAULT)
672     /* Any override is ok for the tty device */
673     dev->dev.cd = p->cfg.cd;
674
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);
678
679   cfmakeraw(&ios);
680   if (p->cfg.rts_cts)
681     ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
682   else {
683     ios.c_cflag |= CLOCAL;
684     ios.c_iflag |= IXOFF;
685   }
686   ios.c_iflag |= IXON;
687   if (p->type != PHYS_DEDICATED)
688     ios.c_cflag |= HUPCL;
689
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);
697   }
698
699   if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
700     log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
701                p->link.name);
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), "");
705   }
706
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);
710
711   oldflag = fcntl(p->fd, F_GETFL, 0);
712   if (oldflag < 0) {
713     /* Complete failure - parent doesn't continue trying to ``create'' */
714
715     log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
716                p->link.name, strerror(errno));
717     tty_Cooked(p);
718     close(p->fd);
719     p->fd = -1;
720     free(dev);
721     return NULL;
722   } else
723     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
724
725   physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
726
727   return &dev->dev;
728 }