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