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