]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/tty.c
This commit was generated by cvs2svn to compensate for changes in r93982,
[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   reply = (struct ng_mesg *)rbuf;
264   info = (struct nodeinfo *)reply->data;
265
266   loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket",
267               NULL);
268
269   /* Get the speed before loading the line discipline */
270   speed = physical_GetSpeed(p);
271
272   if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) {
273     log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n",
274                p->link.name);
275     return 0;
276   }
277   ldisc = NETGRAPHDISC;
278   if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) {
279     log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n",
280                p->link.name);
281     return 0;
282   }
283
284   /* Get the name of the tty node */
285   if (ioctl(p->fd, NGIOCGINFO, info) < 0) {
286     log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name,
287                strerror(errno));
288     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
289     return 0;
290   }
291   snprintf(ttypath, sizeof ttypath, "%s:", info->name);
292
293   /* Create a socket node for our endpoint (and to send messages via) */
294   if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) {
295     log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name,
296                strerror(errno));
297     ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
298     return 0;
299   }
300
301   /* Set the ``hot char'' on the TTY node */
302   hot = HDLC_SYN;
303   log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot);
304   if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE,
305       NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) {
306     log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name);
307     goto failed;
308   }
309
310   /* Attach an async converter node */
311   snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE);
312   snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK);
313   snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC);
314   log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name,
315              ngm.peerhook, ttypath, ngm.ourhook);
316   if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE,
317       NGM_MKPEER, &ngm, sizeof ngm) < 0) {
318     log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name,
319                NG_ASYNC_NODE_TYPE);
320     goto failed;
321   }
322
323   /* Connect the async node to our socket */
324   snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK);
325   snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC);
326   memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook);
327   log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name,
328              ngc.path, ngc.peerhook, ngc.ourhook);
329   if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT,
330       &ngc, sizeof ngc) < 0) {
331     log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n",
332                p->link.name, ngc.ourhook, ngc.path, ngc.peerhook,
333                strerror(errno));
334     goto failed;
335   }
336
337   /* Get the async node id */
338   if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) {
339     log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n",
340                p->link.name, ngc.path, strerror(errno));
341     goto failed;
342   }
343   if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) {
344     log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n",
345                p->link.name, ngc.path, strerror(errno));
346     goto failed;
347   }
348
349   /* All done, set up our device state */
350   snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook);
351   dev->cs = cs;
352   dev->real.fd = p->fd;
353   p->fd = ds;
354   dev->real.speed = speed;
355   physical_SetSync(p);
356
357   tty_SetAsyncParams(p, 0xffffffff, 0xffffffff);
358   physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
359   log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n",
360              p->link.name);
361
362   return 1;
363
364 failed:
365   ID0ioctl(p->fd, TIOCSETD, &dev->real.disc);
366   close(ds);
367   close(cs);
368
369   return 0;
370 }
371
372 static void
373 UnloadLineDiscipline(struct physical *p)
374 {
375   struct ttydevice *dev = device2tty(p->handler);
376
377   if (isngtty(dev)) {
378 log_Printf(LogPHASE, "back to speed %d\n", dev->real.speed);
379     if (!physical_SetSpeed(p, dev->real.speed))
380       log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed);
381     dev->real.speed = 0;
382     close(p->fd);
383     p->fd = dev->real.fd;
384     dev->real.fd = -1;
385     close(dev->cs);
386     dev->cs = -1;
387     *dev->hook = '\0';
388     if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) {
389       physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
390       log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n",
391                  p->link.name);
392     } else
393       log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n",
394                  p->link.name);
395   }
396 }
397
398 static ssize_t
399 tty_Write(struct physical *p, const void *v, size_t n)
400 {
401   struct ttydevice *dev = device2tty(p->handler);
402
403   if (isngtty(dev))
404     return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
405   else
406     return write(p->fd, v, n);
407 }
408
409 static ssize_t
410 tty_Read(struct physical *p, void *v, size_t n)
411 {
412   struct ttydevice *dev = device2tty(p->handler);
413   char hook[sizeof NG_ASYNC_HOOK_SYNC];
414
415   if (isngtty(dev))
416     return NgRecvData(p->fd, v, n, hook);
417   else
418     return read(p->fd, v, n);
419 }
420
421 #endif /* NETGRAPH */
422
423 static int
424 tty_Raw(struct physical *p)
425 {
426   struct ttydevice *dev = device2tty(p->handler);
427   struct termios ios;
428   int oldflag;
429
430   log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
431
432   if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
433     log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
434               p->link.name, p->fd, dev->mbits);
435
436   if (!physical_IsSync(p)) {
437 #ifndef NONETGRAPH
438     if (!LoadLineDiscipline(p))
439 #endif
440     {
441       tcgetattr(p->fd, &ios);
442       cfmakeraw(&ios);
443       if (p->cfg.rts_cts)
444         ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
445       else
446         ios.c_cflag |= CLOCAL;
447
448       if (p->type != PHYS_DEDICATED)
449         ios.c_cflag |= HUPCL;
450
451       if (tcsetattr(p->fd, TCSANOW, &ios) == -1)
452         log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
453                    p->link.name);
454     }
455   }
456
457   oldflag = fcntl(p->fd, F_GETFL, 0);
458   if (oldflag < 0)
459     return 0;
460   fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
461
462   return 1;
463 }
464
465 static void
466 tty_Offline(struct physical *p)
467 {
468   struct ttydevice *dev = device2tty(p->handler);
469
470   if (p->fd >= 0) {
471     timer_Stop(&dev->Timer);
472     dev->mbits &= ~TIOCM_DTR;   /* XXX: Hmm, what's this supposed to do ? */
473     if (Online(dev)) {
474       struct termios tio;
475
476       tcgetattr(p->fd, &tio);
477       if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1)
478         log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
479                    p->link.name);
480     }
481   }
482 }
483
484 static void
485 tty_Cooked(struct physical *p)
486 {
487   struct ttydevice *dev = device2tty(p->handler);
488   int oldflag;
489
490   tty_Offline(p);       /* In case of emergency close()s */
491
492   tcflush(p->fd, TCIOFLUSH);
493
494   if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1)
495     log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n",
496                p->link.name);
497
498 #ifndef NONETGRAPH
499   UnloadLineDiscipline(p);
500 #endif
501
502   if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
503     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
504 }
505
506 static void
507 tty_StopTimer(struct physical *p)
508 {
509   struct ttydevice *dev = device2tty(p->handler);
510
511   timer_Stop(&dev->Timer);
512 }
513
514 static void
515 tty_Free(struct physical *p)
516 {
517   struct ttydevice *dev = device2tty(p->handler);
518
519   tty_Offline(p);       /* In case of emergency close()s */
520   free(dev);
521 }
522
523 static int
524 tty_Speed(struct physical *p)
525 {
526   struct termios ios;
527
528   if (tcgetattr(p->fd, &ios) == -1)
529     return 0;
530
531   return SpeedToInt(cfgetispeed(&ios));
532 }
533
534 static const char *
535 tty_OpenInfo(struct physical *p)
536 {
537   struct ttydevice *dev = device2tty(p->handler);
538   static char buf[13];
539
540   if (Online(dev))
541     strcpy(buf, "with");
542   else
543     strcpy(buf, "no");
544   strcat(buf, " carrier");
545
546   return buf;
547 }
548
549 static void
550 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
551                int maxiov, int *auxfd, int *nauxfd)
552 {
553   struct ttydevice *dev = device2tty(d);
554   int sz = physical_MaxDeviceSize();
555
556   iov[*niov].iov_base = realloc(d, sz);
557   if (iov[*niov].iov_base == NULL) {
558     log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
559     AbortProgram(EX_OSERR);
560   }
561   iov[*niov].iov_len = sz;
562   (*niov)++;
563
564 #ifndef NONETGRAPH
565   if (dev->cs >= 0) {
566     *auxfd = dev->cs;
567     (*nauxfd)++;
568   }
569 #endif
570
571   if (dev->Timer.state != TIMER_STOPPED) {
572     timer_Stop(&dev->Timer);
573     dev->Timer.state = TIMER_RUNNING;
574   }
575 }
576
577 static struct device basettydevice = {
578   TTY_DEVICE,
579   "tty",
580   0,
581   { CD_VARIABLE, DEF_TTYCDDELAY },
582   tty_AwaitCarrier,
583   NULL,
584   tty_Raw,
585   tty_Offline,
586   tty_Cooked,
587   tty_SetAsyncParams,
588   tty_StopTimer,
589   tty_Free,
590   tty_Read,
591   tty_Write,
592   tty_device2iov,
593   tty_Speed,
594   tty_OpenInfo
595 };
596
597 struct device *
598 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
599                int maxiov, int *auxfd, int *nauxfd)
600 {
601   if (type == TTY_DEVICE) {
602     struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
603
604     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
605     if (dev == NULL) {
606       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
607                  (int)(sizeof *dev));
608       AbortProgram(EX_OSERR);
609     }
610
611 #ifndef NONETGRAPH
612     if (*nauxfd) {
613       dev->cs = *auxfd;
614       (*nauxfd)--;
615     } else
616       dev->cs = -1;
617 #endif
618
619     /* Refresh function pointers etc */
620     memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
621
622     physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
623     if (dev->Timer.state != TIMER_STOPPED) {
624       dev->Timer.state = TIMER_STOPPED;
625       p->handler = &dev->dev;           /* For the benefit of StartTimer */
626       tty_StartTimer(p);
627     }
628     return &dev->dev;
629   }
630
631   return NULL;
632 }
633
634 struct device *
635 tty_Create(struct physical *p)
636 {
637   struct ttydevice *dev;
638   struct termios ios;
639   int oldflag;
640
641   if (p->fd < 0 || !isatty(p->fd))
642     /* Don't want this */
643     return NULL;
644
645   if (*p->name.full == '\0') {
646     physical_SetDevice(p, ttyname(p->fd));
647     log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
648                p->link.name, p->name.full);
649   } else
650     log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
651
652   /* We're gonna return a ttydevice (unless something goes horribly wrong) */
653
654   if ((dev = malloc(sizeof *dev)) == NULL) {
655     /* Complete failure - parent doesn't continue trying to ``create'' */
656     close(p->fd);
657     p->fd = -1;
658     return NULL;
659   }
660
661   memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
662   memset(&dev->Timer, '\0', sizeof dev->Timer);
663   dev->mbits = -1;
664 #ifndef NONETGRAPH
665   dev->real.speed = 0;
666   dev->real.fd = -1;
667   dev->real.disc = -1;
668   *dev->hook = '\0';
669 #endif
670   tcgetattr(p->fd, &ios);
671   dev->ios = ios;
672
673   if (p->cfg.cd.necessity != CD_DEFAULT)
674     /* Any override is ok for the tty device */
675     dev->dev.cd = p->cfg.cd;
676
677   log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
678              " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
679              (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
680
681   cfmakeraw(&ios);
682   if (p->cfg.rts_cts)
683     ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
684   else {
685     ios.c_cflag |= CLOCAL;
686     ios.c_iflag |= IXOFF;
687   }
688   ios.c_iflag |= IXON;
689   if (p->type != PHYS_DEDICATED)
690     ios.c_cflag |= HUPCL;
691
692   if (p->type != PHYS_DIRECT) {
693       /* Change tty speed when we're not in -direct mode */
694       ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
695       ios.c_cflag |= p->cfg.parity;
696       if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
697         log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
698                   p->link.name, p->name.full, p->cfg.speed);
699   }
700
701   if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) {
702     log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n",
703                p->link.name);
704     if (p->type != PHYS_DIRECT && p->cfg.speed > 115200)
705       log_Printf(LogWARN, "%.*s             Perhaps the speed is unsupported\n",
706                  (int)strlen(p->link.name), "");
707   }
708
709   log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
710             "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
711             (u_long)ios.c_oflag, (u_long)ios.c_cflag);
712
713   oldflag = fcntl(p->fd, F_GETFL, 0);
714   if (oldflag < 0) {
715     /* Complete failure - parent doesn't continue trying to ``create'' */
716
717     log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
718                p->link.name, strerror(errno));
719     tty_Cooked(p);
720     close(p->fd);
721     p->fd = -1;
722     free(dev);
723     return NULL;
724   } else
725     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
726
727   physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
728
729   return &dev->dev;
730 }