xref: /freebsd/usr.sbin/ppp/tty.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
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 <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <sys/uio.h>
41 #include <termios.h>
42 #include <unistd.h>
43 
44 #include "layer.h"
45 #include "defs.h"
46 #include "mbuf.h"
47 #include "log.h"
48 #include "timer.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "throughput.h"
52 #include "fsm.h"
53 #include "lcp.h"
54 #include "ccp.h"
55 #include "link.h"
56 #include "async.h"
57 #include "descriptor.h"
58 #include "physical.h"
59 #include "mp.h"
60 #include "chat.h"
61 #include "auth.h"
62 #include "chap.h"
63 #include "cbcp.h"
64 #include "datalink.h"
65 #include "main.h"
66 #include "tty.h"
67 
68 #if defined(__mac68k__) || defined(__macppc__)
69 #undef	CRTS_IFLOW
70 #undef	CCTS_OFLOW
71 #define	CRTS_IFLOW	CDTRCTS
72 #define	CCTS_OFLOW	CDTRCTS
73 #endif
74 
75 #define	Online(dev)	((dev)->mbits & TIOCM_CD)
76 
77 struct ttydevice {
78   struct device dev;		/* What struct physical knows about */
79   struct pppTimer Timer;	/* CD checks */
80   int mbits;			/* Current DCD status */
81   int carrier_seconds;		/* seconds before CD is *required* */
82   struct termios ios;		/* To be able to reset from raw mode */
83 };
84 
85 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
86 
87 int
88 tty_DeviceSize(void)
89 {
90   return sizeof(struct ttydevice);
91 }
92 
93 /*
94  * tty_Timeout() watches the DCD signal and mentions it if it's status
95  * changes.
96  */
97 static void
98 tty_Timeout(void *data)
99 {
100   struct physical *p = data;
101   struct ttydevice *dev = device2tty(p->handler);
102   int ombits, change;
103 
104   timer_Stop(&dev->Timer);
105   dev->Timer.load = SECTICKS;		/* Once a second please */
106   timer_Start(&dev->Timer);
107   ombits = dev->mbits;
108 
109   if (p->fd >= 0) {
110     if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
111       /* we must be a pty ? */
112       log_Printf(LogDEBUG, "%s: ioctl error (%s)!\n", p->link.name,
113                  strerror(errno));
114       timer_Stop(&dev->Timer);
115       return;
116     }
117   } else
118     dev->mbits = 0;
119 
120   if (ombits == -1) {
121     /* First time looking for carrier */
122     if (Online(dev))
123       log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
124     else if (++dev->carrier_seconds >= p->cfg.cd.delay) {
125       if (p->cfg.cd.necessity == CD_REQUIRED)
126         log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
127                    p->link.name, p->name.full);
128       else {
129         log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
130                    p->link.name, p->name.full);
131         dev->mbits = TIOCM_CD;		/* Dodgy null-modem cable ? */
132       }
133       timer_Stop(&dev->Timer);
134       /* tty_AwaitCarrier() will notice */
135     } else {
136       /* Keep waiting */
137       log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
138                  p->link.name, p->name.full, dev->carrier_seconds,
139                  p->cfg.cd.delay);
140       dev->mbits = -1;
141     }
142   } else {
143     change = ombits ^ dev->mbits;
144     if (change & TIOCM_CD) {
145       if (dev->mbits & TIOCM_CD)
146         log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
147       else {
148         log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
149         log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
150         datalink_Down(p->dl, CLOSE_NORMAL);
151         timer_Stop(&dev->Timer);
152       }
153     } else
154       log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
155                  Online(dev) ? "on" : "off");
156   }
157 }
158 
159 static void
160 tty_StartTimer(struct physical *p)
161 {
162   struct ttydevice *dev = device2tty(p->handler);
163 
164   timer_Stop(&dev->Timer);
165   dev->Timer.load = SECTICKS;
166   dev->Timer.func = tty_Timeout;
167   dev->Timer.name = "tty CD";
168   dev->Timer.arg = p;
169   log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
170              p->link.name, tty_Timeout);
171   timer_Start(&dev->Timer);
172 }
173 
174 static int
175 tty_AwaitCarrier(struct physical *p)
176 {
177   struct ttydevice *dev = device2tty(p->handler);
178 
179   if (p->cfg.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
180     return CARRIER_OK;
181 
182   if (dev->mbits == -1) {
183     if (dev->Timer.state == TIMER_STOPPED) {
184       dev->carrier_seconds = 0;
185       tty_StartTimer(p);
186     }
187     return CARRIER_PENDING;			/* Not yet ! */
188   }
189 
190   return Online(dev) || !p->cfg.cd.necessity == CD_REQUIRED ?
191     CARRIER_OK : CARRIER_LOST;
192 }
193 
194 static int
195 tty_Raw(struct physical *p)
196 {
197   struct ttydevice *dev = device2tty(p->handler);
198   struct termios ios;
199   int oldflag;
200 
201   log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
202 
203   if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
204     log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
205               p->link.name, p->fd, dev->mbits);
206 
207   if (!physical_IsSync(p)) {
208     tcgetattr(p->fd, &ios);
209     cfmakeraw(&ios);
210     if (p->cfg.rts_cts)
211       ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
212     else
213       ios.c_cflag |= CLOCAL;
214 
215     if (p->type != PHYS_DEDICATED)
216       ios.c_cflag |= HUPCL;
217 
218     tcsetattr(p->fd, TCSANOW, &ios);
219   }
220 
221   oldflag = fcntl(p->fd, F_GETFL, 0);
222   if (oldflag < 0)
223     return 0;
224   fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
225 
226   return 1;
227 }
228 
229 static void
230 tty_Offline(struct physical *p)
231 {
232   struct ttydevice *dev = device2tty(p->handler);
233 
234   if (p->fd >= 0) {
235     timer_Stop(&dev->Timer);
236     dev->mbits &= ~TIOCM_DTR;	/* XXX: Hmm, what's this supposed to do ? */
237     if (Online(dev)) {
238       struct termios tio;
239 
240       tcgetattr(p->fd, &tio);
241       if (cfsetspeed(&tio, B0) == -1)
242         log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
243                    p->link.name);
244       else
245         tcsetattr(p->fd, TCSANOW, &tio);
246     }
247   }
248 }
249 
250 static void
251 tty_Cooked(struct physical *p)
252 {
253   struct ttydevice *dev = device2tty(p->handler);
254   int oldflag;
255 
256   tty_Offline(p);	/* In case of emergency close()s */
257 
258   tcflush(p->fd, TCIOFLUSH);
259 
260   if (!physical_IsSync(p))
261     tcsetattr(p->fd, TCSAFLUSH, &dev->ios);
262 
263   if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
264     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
265 }
266 
267 static void
268 tty_StopTimer(struct physical *p)
269 {
270   struct ttydevice *dev = device2tty(p->handler);
271 
272   timer_Stop(&dev->Timer);
273 }
274 
275 static void
276 tty_Free(struct physical *p)
277 {
278   struct ttydevice *dev = device2tty(p->handler);
279 
280   tty_Offline(p);	/* In case of emergency close()s */
281   free(dev);
282 }
283 
284 static int
285 tty_Speed(struct physical *p)
286 {
287   struct termios ios;
288 
289   if (tcgetattr(p->fd, &ios) == -1)
290     return 0;
291 
292   return SpeedToInt(cfgetispeed(&ios));
293 }
294 
295 static const char *
296 tty_OpenInfo(struct physical *p)
297 {
298   struct ttydevice *dev = device2tty(p->handler);
299   static char buf[13];
300 
301   if (Online(dev))
302     strcpy(buf, "with");
303   else
304     strcpy(buf, "no");
305   strcat(buf, " carrier");
306 
307   return buf;
308 }
309 
310 static void
311 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
312                int maxiov, int *auxfd, int *nauxfd, pid_t newpid)
313 {
314   struct ttydevice *dev = device2tty(d);
315   int sz = physical_MaxDeviceSize();
316 
317   iov[*niov].iov_base = realloc(d, sz);
318   if (iov[*niov].iov_base == NULL) {
319     log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
320     AbortProgram(EX_OSERR);
321   }
322   iov[*niov].iov_len = sz;
323   (*niov)++;
324 
325   if (dev->Timer.state != TIMER_STOPPED) {
326     timer_Stop(&dev->Timer);
327     dev->Timer.state = TIMER_RUNNING;
328   }
329 }
330 
331 static struct device basettydevice = {
332   TTY_DEVICE,
333   "tty",
334   tty_AwaitCarrier,
335   NULL,
336   tty_Raw,
337   tty_Offline,
338   tty_Cooked,
339   tty_StopTimer,
340   tty_Free,
341   NULL,
342   NULL,
343   tty_device2iov,
344   tty_Speed,
345   tty_OpenInfo
346 };
347 
348 struct device *
349 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
350                int maxiov, int *auxfd, int *nauxfd)
351 {
352   if (type == TTY_DEVICE) {
353     struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
354 
355     dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
356     if (dev == NULL) {
357       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
358                  (int)(sizeof *dev));
359       AbortProgram(EX_OSERR);
360     }
361 
362     /* Refresh function pointers etc */
363     memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
364 
365     physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
366     if (dev->Timer.state != TIMER_STOPPED) {
367       dev->Timer.state = TIMER_STOPPED;
368       p->handler = &dev->dev;		/* For the benefit of StartTimer */
369       tty_StartTimer(p);
370     }
371     return &dev->dev;
372   }
373 
374   return NULL;
375 }
376 
377 struct device *
378 tty_Create(struct physical *p)
379 {
380   struct ttydevice *dev;
381   struct termios ios;
382   int oldflag;
383 
384   if (p->fd < 0 || !isatty(p->fd))
385     /* Don't want this */
386     return NULL;
387 
388   if (*p->name.full == '\0') {
389     physical_SetDevice(p, ttyname(p->fd));
390     log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
391                p->link.name, p->name.full);
392   } else
393     log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
394 
395   /* We're gonna return a ttydevice (unless something goes horribly wrong) */
396 
397   if ((dev = malloc(sizeof *dev)) == NULL) {
398     /* Complete failure - parent doesn't continue trying to ``create'' */
399     close(p->fd);
400     p->fd = -1;
401     return NULL;
402   }
403 
404   memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
405   memset(&dev->Timer, '\0', sizeof dev->Timer);
406   dev->mbits = -1;
407   tcgetattr(p->fd, &ios);
408   dev->ios = ios;
409 
410   log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
411              " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
412              (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
413 
414   cfmakeraw(&ios);
415   if (p->cfg.rts_cts)
416     ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
417   else {
418     ios.c_cflag |= CLOCAL;
419     ios.c_iflag |= IXOFF;
420   }
421   ios.c_iflag |= IXON;
422   if (p->type != PHYS_DEDICATED)
423     ios.c_cflag |= HUPCL;
424 
425   if (p->type != PHYS_DIRECT) {
426       /* Change tty speed when we're not in -direct mode */
427       ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
428       ios.c_cflag |= p->cfg.parity;
429       if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
430 	log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
431 		  p->link.name, p->name.full, p->cfg.speed);
432   }
433   tcsetattr(p->fd, TCSADRAIN, &ios);
434   log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
435             "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
436             (u_long)ios.c_oflag, (u_long)ios.c_cflag);
437 
438   oldflag = fcntl(p->fd, F_GETFL, 0);
439   if (oldflag < 0) {
440     /* Complete failure - parent doesn't continue trying to ``create'' */
441 
442     log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
443                p->link.name, strerror(errno));
444     tty_Cooked(p);
445     close(p->fd);
446     p->fd = -1;
447     free(dev);
448     return NULL;
449   } else
450     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
451 
452   physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
453 
454   return &dev->dev;
455 }
456