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.necessity == 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 (p->cfg.cd.necessity == CD_NOTREQUIRED || 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.necessity == CD_REQUIRED ? 184 CARRIER_OK : CARRIER_LOST; 185 } 186 187 static int 188 tty_Raw(struct physical *p) 189 { 190 struct ttydevice *dev = device2tty(p->handler); 191 struct termios ios; 192 int oldflag; 193 194 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name); 195 196 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev)) 197 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n", 198 p->link.name, p->fd, dev->mbits); 199 200 if (!physical_IsSync(p)) { 201 tcgetattr(p->fd, &ios); 202 cfmakeraw(&ios); 203 if (p->cfg.rts_cts) 204 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; 205 else 206 ios.c_cflag |= CLOCAL; 207 208 if (p->type != PHYS_DEDICATED) 209 ios.c_cflag |= HUPCL; 210 211 tcsetattr(p->fd, TCSANOW, &ios); 212 } 213 214 oldflag = fcntl(p->fd, F_GETFL, 0); 215 if (oldflag < 0) 216 return 0; 217 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK); 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; /* XXX: Hmm, what's this supposed to do ? */ 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 253 if (!physical_IsSync(p)) 254 tcsetattr(p->fd, TCSAFLUSH, &dev->ios); 255 256 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1) 257 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); 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 300 return buf; 301 } 302 303 static void 304 tty_device2iov(struct device *d, struct iovec *iov, int *niov, 305 int maxiov, pid_t newpid) 306 { 307 struct ttydevice *dev = device2tty(d); 308 int sz = physical_MaxDeviceSize(); 309 310 iov[*niov].iov_base = realloc(d, sz); 311 if (iov[*niov].iov_base == NULL) { 312 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); 313 AbortProgram(EX_OSERR); 314 } 315 iov[*niov].iov_len = sz; 316 (*niov)++; 317 318 if (dev->Timer.state != TIMER_STOPPED) { 319 timer_Stop(&dev->Timer); 320 dev->Timer.state = TIMER_RUNNING; 321 } 322 } 323 324 static struct device basettydevice = { 325 TTY_DEVICE, 326 "tty", 327 tty_AwaitCarrier, 328 tty_Raw, 329 tty_Offline, 330 tty_Cooked, 331 tty_StopTimer, 332 tty_Free, 333 NULL, 334 NULL, 335 tty_device2iov, 336 tty_Speed, 337 tty_OpenInfo 338 }; 339 340 struct device * 341 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 342 int maxiov) 343 { 344 if (type == TTY_DEVICE) { 345 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base; 346 347 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 348 if (dev == NULL) { 349 log_Printf(LogALERT, "Failed to allocate memory: %d\n", 350 (int)(sizeof *dev)); 351 AbortProgram(EX_OSERR); 352 } 353 354 /* Refresh function pointers etc */ 355 memcpy(&dev->dev, &basettydevice, sizeof dev->dev); 356 357 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 358 if (dev->Timer.state != TIMER_STOPPED) { 359 dev->Timer.state = TIMER_STOPPED; 360 p->handler = &dev->dev; /* For the benefit of StartTimer */ 361 tty_StartTimer(p); 362 } 363 return &dev->dev; 364 } 365 366 return NULL; 367 } 368 369 struct device * 370 tty_Create(struct physical *p) 371 { 372 struct ttydevice *dev; 373 struct termios ios; 374 int oldflag; 375 376 if (p->fd < 0 || !isatty(p->fd)) 377 /* Don't want this */ 378 return NULL; 379 380 if (*p->name.full == '\0') { 381 physical_SetDevice(p, ttyname(p->fd)); 382 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n", 383 p->link.name, p->name.full); 384 } else 385 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full); 386 387 /* We're gonna return a ttydevice (unless something goes horribly wrong) */ 388 389 if ((dev = malloc(sizeof *dev)) == NULL) { 390 /* Complete failure - parent doesn't continue trying to ``create'' */ 391 close(p->fd); 392 p->fd = -1; 393 return NULL; 394 } 395 396 memcpy(&dev->dev, &basettydevice, sizeof dev->dev); 397 memset(&dev->Timer, '\0', sizeof dev->Timer); 398 dev->mbits = -1; 399 tcgetattr(p->fd, &ios); 400 dev->ios = ios; 401 402 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d," 403 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd, 404 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag); 405 406 cfmakeraw(&ios); 407 if (p->cfg.rts_cts) 408 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; 409 else { 410 ios.c_cflag |= CLOCAL; 411 ios.c_iflag |= IXOFF; 412 } 413 ios.c_iflag |= IXON; 414 if (p->type != PHYS_DEDICATED) 415 ios.c_cflag |= HUPCL; 416 417 if (p->type != PHYS_DIRECT) { 418 /* Change tty speed when we're not in -direct mode */ 419 ios.c_cflag &= ~(CSIZE | PARODD | PARENB); 420 ios.c_cflag |= p->cfg.parity; 421 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1) 422 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n", 423 p->link.name, p->name.full, p->cfg.speed); 424 } 425 tcsetattr(p->fd, TCSADRAIN, &ios); 426 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, " 427 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag, 428 (u_long)ios.c_oflag, (u_long)ios.c_cflag); 429 430 oldflag = fcntl(p->fd, F_GETFL, 0); 431 if (oldflag < 0) { 432 /* Complete failure - parent doesn't continue trying to ``create'' */ 433 434 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n", 435 p->link.name, strerror(errno)); 436 tty_Cooked(p); 437 close(p->fd); 438 p->fd = -1; 439 free(dev); 440 return NULL; 441 } else 442 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); 443 444 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 445 446 return &dev->dev; 447 } 448