1/* 2 * sys-linux.c - System-dependent procedures for setting up 3 * PPP interfaces on Linux systems 4 * 5 * Copyright (c) 1989 Carnegie Mellon University. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by Carnegie Mellon University. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 */ 20 21#include <sys/ioctl.h> 22#include <sys/types.h> 23#include <sys/socket.h> 24#include <sys/time.h> 25#include <sys/errno.h> 26#include <sys/file.h> 27#include <sys/stat.h> 28#include <sys/utsname.h> 29#include <sys/sysmacros.h> 30 31#include <stdio.h> 32#include <stdlib.h> 33#include <syslog.h> 34#include <string.h> 35#include <time.h> 36#include <memory.h> 37#include <utmp.h> 38#include <mntent.h> 39#include <signal.h> 40#include <fcntl.h> 41#include <ctype.h> 42#include <termios.h> 43#include <unistd.h> 44 45/* This is in netdevice.h. However, this compile will fail miserably if 46 you attempt to include netdevice.h because it has so many references 47 to __memcpy functions which it should not attempt to do. So, since I 48 really don't use it, but it must be defined, define it now. */ 49 50#ifndef MAX_ADDR_LEN 51#define MAX_ADDR_LEN 7 52#endif 53 54#if __GLIBC__ >= 2 55#include <asm/types.h> /* glibc 2 conflicts with linux/types.h */ 56#include <net/if.h> 57#include <net/if_arp.h> 58#include <net/route.h> 59#include <netinet/if_ether.h> 60#else 61#include <linux/types.h> 62#include <linux/if.h> 63#include <linux/if_arp.h> 64#include <linux/route.h> 65#include <linux/if_ether.h> 66#endif 67#include <netinet/in.h> 68#include <arpa/inet.h> 69 70#include <linux/ppp_defs.h> 71#include <linux/if_ppp.h> 72 73#include "pppd.h" 74#include "fsm.h" 75#include "ipcp.h" 76 77#ifdef IPX_CHANGE 78#include "ipxcp.h" 79#if __GLIBC__ >= 2 && !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0) 80#include <netipx/ipx.h> 81#else 82#include <linux/ipx.h> 83#endif 84#endif /* IPX_CHANGE */ 85 86#ifdef PPP_FILTER 87#include <net/bpf.h> 88#include <linux/filter.h> 89#endif /* PPP_FILTER */ 90 91#ifdef LOCKLIB 92#include <sys/locks.h> 93#endif 94 95#ifdef INET6 96#ifndef _LINUX_IN6_H 97/* 98 * This is in linux/include/net/ipv6.h. 99 */ 100 101struct in6_ifreq { 102 struct in6_addr ifr6_addr; 103 __u32 ifr6_prefixlen; 104 unsigned int ifr6_ifindex; 105}; 106#endif 107 108#define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \ 109 memset(&sin6.s6_addr, 0, sizeof(struct in6_addr)); \ 110 sin6.s6_addr16[0] = htons(0xfe80); \ 111 eui64_copy(eui64, sin6.s6_addr32[2]); \ 112 } while (0) 113 114#endif /* INET6 */ 115 116/* We can get an EIO error on an ioctl if the modem has hung up */ 117#define ok_error(num) ((num)==EIO) 118 119static int tty_disc = N_TTY; /* The TTY discipline */ 120static int ppp_disc = N_PPP; /* The PPP discpline */ 121static int initfdflags = -1; /* Initial file descriptor flags for fd */ 122static int ppp_fd = -1; /* fd which is set to PPP discipline */ 123static int sock_fd = -1; /* socket for doing interface ioctls */ 124static int slave_fd = -1; 125static int master_fd = -1; 126#ifdef INET6 127static int sock6_fd = -1; 128#endif /* INET6 */ 129static int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */ 130static int chindex; /* channel index (new style driver) */ 131 132static fd_set in_fds; /* set of fds that wait_input waits for */ 133static int max_in_fd; /* highest fd set in in_fds */ 134 135static int has_proxy_arp = 0; 136static int driver_version = 0; 137static int driver_modification = 0; 138static int driver_patch = 0; 139static int driver_is_old = 0; 140static int restore_term = 0; /* 1 => we've munged the terminal */ 141static struct termios inittermios; /* Initial TTY termios */ 142 143int new_style_driver = 0; 144 145static char loop_name[20]; 146static unsigned char inbuf[512]; /* buffer for chars read from loopback */ 147 148static int if_is_up; /* Interface has been marked up */ 149static u_int32_t default_route_gateway; /* Gateway for default route added */ 150static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */ 151static char proxy_arp_dev[16]; /* Device for proxy arp entry */ 152static u_int32_t our_old_addr; /* for detecting address changes */ 153static int dynaddr_set; /* 1 if ip_dynaddr set */ 154static int looped; /* 1 if using loop */ 155static int link_mtu; /* mtu for the link (not bundle) */ 156 157static struct utsname utsname; /* for the kernel version */ 158static int kernel_version; 159#define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p)) 160 161#define MAX_IFS 100 162 163#define FLAGS_GOOD (IFF_UP | IFF_BROADCAST) 164#define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \ 165 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP) 166 167#define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr) 168 169/* Prototypes for procedures local to this file. */ 170static int get_flags (int fd); 171static void set_flags (int fd, int flags); 172static int translate_speed (int bps); 173static int baud_rate_of (int speed); 174static void close_route_table (void); 175static int open_route_table (void); 176static int read_route_table (struct rtentry *rt); 177static int defaultroute_exists (struct rtentry *rt); 178static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr, 179 char *name, int namelen); 180static void decode_version (char *buf, int *version, int *mod, int *patch); 181static int set_kdebugflag(int level); 182static int ppp_registered(void); 183static int make_ppp_unit(void); 184static void restore_loop(void); /* Transfer ppp unit back to loopback */ 185 186extern u_char inpacket_buf[]; /* borrowed from main.c */ 187 188/* 189 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 190 * if it exists. 191 */ 192 193#define SET_SA_FAMILY(addr, family) \ 194 memset ((char *) &(addr), '\0', sizeof(addr)); \ 195 addr.sa_family = (family); 196 197/* 198 * Determine if the PPP connection should still be present. 199 */ 200 201extern int hungup; 202 203/* new_fd is the fd of a tty */ 204static void set_ppp_fd (int new_fd) 205{ 206 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd)); 207 ppp_fd = new_fd; 208 if (!new_style_driver) 209 ppp_dev_fd = new_fd; 210} 211 212static int still_ppp(void) 213{ 214 if (new_style_driver) 215 return !hungup && ppp_fd >= 0; 216 if (!hungup || ppp_fd == slave_fd) 217 return 1; 218 if (slave_fd >= 0) { 219 set_ppp_fd(slave_fd); 220 return 1; 221 } 222 return 0; 223} 224 225/******************************************************************** 226 * 227 * Functions to read and set the flags value in the device driver 228 */ 229 230static int get_flags (int fd) 231{ 232 int flags; 233 234 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) { 235 if ( ok_error (errno) ) 236 flags = 0; 237 else 238 fatal("ioctl(PPPIOCGFLAGS): %m"); 239 } 240 241 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags)); 242 return flags; 243} 244 245/********************************************************************/ 246 247static void set_flags (int fd, int flags) 248{ 249 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags)); 250 251 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0) { 252 if (! ok_error (errno) ) 253 fatal("ioctl(PPPIOCSFLAGS, %x): %m", flags, errno); 254 } 255} 256 257/******************************************************************** 258 * 259 * sys_init - System-dependent initialization. 260 */ 261 262void sys_init(void) 263{ 264 int flags; 265 266 if (new_style_driver) { 267 ppp_dev_fd = open("/dev/ppp", O_RDWR); 268 if (ppp_dev_fd < 0) 269 fatal("Couldn't open /dev/ppp: %m"); 270 flags = fcntl(ppp_dev_fd, F_GETFL); 271 if (flags == -1 272 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1) 273 warn("Couldn't set /dev/ppp to nonblock: %m"); 274 } 275 276 /* Get an internet socket for doing socket ioctls. */ 277 sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 278 if (sock_fd < 0) 279 fatal("Couldn't create IP socket: %m(%d)", errno); 280 281#ifdef INET6 282 sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0); 283 if (sock6_fd < 0) 284 sock6_fd = -errno; /* save errno for later */ 285#endif 286 287 FD_ZERO(&in_fds); 288 max_in_fd = 0; 289} 290 291/******************************************************************** 292 * 293 * sys_cleanup - restore any system state we modified before exiting: 294 * mark the interface down, delete default route and/or proxy arp entry. 295 * This shouldn't call die() because it's called from die(). 296 */ 297 298void sys_cleanup(void) 299{ 300/* 301 * Take down the device 302 */ 303 if (if_is_up) { 304 if_is_up = 0; 305 sifdown(0); 306 } 307/* 308 * Delete any routes through the device. 309 */ 310 if (default_route_gateway != 0) 311 cifdefaultroute(0, 0, default_route_gateway); 312 313 if (has_proxy_arp) 314 cifproxyarp(0, proxy_arp_addr); 315} 316 317/******************************************************************** 318 * 319 * sys_close - Clean up in a child process before execing. 320 */ 321void 322sys_close(void) 323{ 324 if (new_style_driver) 325 close(ppp_dev_fd); 326 if (sock_fd >= 0) 327 close(sock_fd); 328 if (slave_fd >= 0) 329 close(slave_fd); 330 if (master_fd >= 0) 331 close(master_fd); 332 closelog(); 333} 334 335/******************************************************************** 336 * 337 * set_kdebugflag - Define the debugging level for the kernel 338 */ 339 340static int set_kdebugflag (int requested_level) 341{ 342 if (new_style_driver && ifunit < 0) 343 return 1; 344 if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) { 345 if ( ! ok_error (errno) ) 346 error("ioctl(PPPIOCSDEBUG): %m"); 347 return (0); 348 } 349 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d", 350 requested_level)); 351 return (1); 352} 353 354 355/******************************************************************** 356 * 357 * generic_establish_ppp - Turn the fd into a ppp interface. 358 */ 359int generic_establish_ppp (int fd) 360{ 361 int x; 362/* 363 * Demand mode - prime the old ppp device to relinquish the unit. 364 */ 365 if (!new_style_driver && looped 366 && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) { 367 error("ioctl(transfer ppp unit): %m"); 368 return -1; 369 } 370 371 372 if (new_style_driver) { 373 /* Open another instance of /dev/ppp and connect the channel to it */ 374 int flags; 375 376 if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) { 377 error("Couldn't get channel number: %m"); 378 goto err; 379 } 380 dbglog("using channel %d", chindex); 381 fd = open("/dev/ppp", O_RDWR); 382 if (fd < 0) { 383 error("Couldn't reopen /dev/ppp: %m"); 384 goto err; 385 } 386 if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) { 387 error("Couldn't attach to channel %d: %m", chindex); 388 goto err_close; 389 } 390 flags = fcntl(fd, F_GETFL); 391 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) 392 warn("Couldn't set /dev/ppp (channel) to nonblock: %m"); 393 set_ppp_fd(fd); 394 395 if (!looped) 396 ifunit = -1; 397 if (!looped && !multilink) { 398 /* 399 * Create a new PPP unit. 400 */ 401 if (make_ppp_unit() < 0) 402 goto err_close; 403 } 404 405 if (looped) 406 set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) & ~SC_LOOP_TRAFFIC); 407 408 if (!multilink) { 409 add_fd(ppp_dev_fd); 410 if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) { 411 error("Couldn't attach to PPP unit %d: %m", ifunit); 412 goto err_close; 413 } 414 } 415 416 } else { 417 418 /* 419 * Old-style driver: find out which interface we were given. 420 */ 421 set_ppp_fd (fd); 422 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) { 423 if (ok_error (errno)) 424 goto err; 425 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno); 426 } 427 /* Check that we got the same unit again. */ 428 if (looped && x != ifunit) 429 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x); 430 ifunit = x; 431 432 /* 433 * Fetch the initial file flags and reset blocking mode on the file. 434 */ 435 initfdflags = fcntl(fd, F_GETFL); 436 if (initfdflags == -1 || 437 fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) { 438 if ( ! ok_error (errno)) 439 warn("Couldn't set device to non-blocking mode: %m"); 440 } 441 } 442 443 444 looped = 0; 445 446 /* 447 * Enable debug in the driver if requested. 448 */ 449 if (!looped) 450 set_kdebugflag (kdebugflag); 451 452 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver", 453 driver_version, driver_modification, driver_patch)); 454 455 return ppp_fd; 456 457 err_close: 458 close(fd); 459 err: 460 if (ioctl(fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno)) 461 warn("Couldn't reset tty to normal line discipline: %m"); 462 return -1; 463} 464 465/******************************************************************** 466 * 467 * tty_establish_ppp - Turn the serial port into a ppp interface. 468 */ 469 470int tty_establish_ppp (int tty_fd) 471{ 472 int ret_fd; 473/* 474 * Ensure that the tty device is in exclusive mode. 475 */ 476 if (ioctl(tty_fd, TIOCEXCL, 0) < 0) { 477 if ( ! ok_error ( errno )) 478 warn("Couldn't make tty exclusive: %m"); 479 } 480/* 481 * Set the current tty to the PPP discpline 482 */ 483 484#ifndef N_SYNC_PPP 485#define N_SYNC_PPP 14 486#endif 487 ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP; 488 if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) { 489 if ( ! ok_error (errno) ) { 490 error("Couldn't set tty to PPP discipline: %m"); 491 return -1; 492 } 493 } 494 495 ret_fd = generic_establish_ppp(tty_fd); 496#define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP) 497#define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \ 498 | SC_LOG_FLUSH) 499 500 set_flags(ppp_fd, ((get_flags(ppp_fd) & ~(SC_RCVB | SC_LOGB)) 501 | ((kdebugflag * SC_DEBUG) & SC_LOGB))); 502 503 return ret_fd; 504} 505 506/******************************************************************** 507 * 508 * generic_disestablish_ppp - Restore device components to normal 509 * operation, and reconnect the ppp unit to the loopback if in demand 510 * mode. This shouldn't call die() because it's called from die(). 511*/ 512void generic_disestablish_ppp(int dev_fd){ 513 /* Restore loop if needed */ 514 if(demand) 515 restore_loop(); 516 517 /* Finally detach the device */ 518 initfdflags = -1; 519 520 if (new_style_driver) { 521 close(ppp_fd); 522 ppp_fd = -1; 523 if (!looped && ifunit >= 0 && ioctl(ppp_dev_fd, PPPIOCDETACH) < 0) 524 error("Couldn't release PPP unit: %m"); 525 if (!multilink) 526 remove_fd(ppp_dev_fd); 527 } 528} 529 530/******************************************************************** 531 * 532 * tty_disestablish_ppp - Restore the serial port to normal operation. 533 * This shouldn't call die() because it's called from die(). 534 */ 535 536void tty_disestablish_ppp(int tty_fd) 537{ 538 generic_disestablish_ppp(tty_fd); 539 540 if (!hungup) { 541/* 542 * Flush the tty output buffer so that the TIOCSETD doesn't hang. 543 */ 544 if (tcflush(tty_fd, TCIOFLUSH) < 0) 545 warn("tcflush failed: %m"); 546/* 547 * Restore the previous line discipline 548 */ 549 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) { 550 if ( ! ok_error (errno)) 551 error("ioctl(TIOCSETD, N_TTY): %m"); 552 } 553 554 if (ioctl(tty_fd, TIOCNXCL, 0) < 0) { 555 if ( ! ok_error (errno)) 556 warn("ioctl(TIOCNXCL): %m(%d)", errno); 557 } 558 559 /* Reset non-blocking mode on fd. */ 560 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) { 561 if ( ! ok_error (errno)) 562 warn("Couldn't restore device fd flags: %m"); 563 } 564 } 565} 566 567/* 568 * make_ppp_unit - make a new ppp unit for ppp_dev_fd. 569 * Assumes new_style_driver. 570 */ 571static int make_ppp_unit() 572{ 573 int x; 574 575 ifunit = req_unit; 576 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit); 577 if (x < 0 && req_unit >= 0 && errno == EEXIST) { 578 warn("Couldn't allocate PPP unit %d as it is already in use"); 579 ifunit = -1; 580 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit); 581 } 582 if (x < 0) 583 error("Couldn't create new ppp unit: %m"); 584 return x; 585} 586 587/* 588 * cfg_bundle - configure the existing bundle. 589 * Used in demand mode. 590 */ 591void cfg_bundle(int mrru, int mtru, int rssn, int tssn) 592{ 593 int flags; 594 595 if (!new_style_driver) 596 return; 597 598 /* set the mrru, mtu and flags */ 599 if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0) 600 error("Couldn't set MRRU: %m"); 601 flags = get_flags(ppp_dev_fd); 602 flags &= ~(SC_MP_SHORTSEQ | SC_MP_XSHORTSEQ); 603 flags |= (rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0) 604 | (mrru? SC_MULTILINK: 0); 605 606 set_flags(ppp_dev_fd, flags); 607 608 /* connect up the channel */ 609 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0) 610 fatal("Couldn't attach to PPP unit %d: %m", ifunit); 611 add_fd(ppp_dev_fd); 612} 613 614/* 615 * make_new_bundle - create a new PPP unit (i.e. a bundle) 616 * and connect our channel to it. This should only get called 617 * if `multilink' was set at the time establish_ppp was called. 618 * In demand mode this uses our existing bundle instead of making 619 * a new one. 620 */ 621void make_new_bundle(int mrru, int mtru, int rssn, int tssn) 622{ 623 if (!new_style_driver) 624 return; 625 626 /* make us a ppp unit */ 627 if (make_ppp_unit() < 0) 628 die(1); 629 630 /* set the mrru and flags */ 631 cfg_bundle(mrru, mtru, rssn, tssn); 632} 633 634/* 635 * bundle_attach - attach our link to a given PPP unit. 636 * We assume the unit is controlled by another pppd. 637 */ 638int bundle_attach(int ifnum) 639{ 640 if (!new_style_driver) 641 return -1; 642 643 if (ioctl(ppp_dev_fd, PPPIOCATTACH, &ifnum) < 0) { 644 if (errno == ENXIO) 645 return 0; /* doesn't still exist */ 646 fatal("Couldn't attach to interface unit %d: %m\n", ifnum); 647 } 648 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0) 649 fatal("Couldn't connect to interface unit %d: %m", ifnum); 650 set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) | SC_MULTILINK); 651 652 ifunit = ifnum; 653 return 1; 654} 655 656/******************************************************************** 657 * 658 * clean_check - Fetch the flags for the device and generate 659 * appropriate error messages. 660 */ 661void clean_check(void) 662{ 663 int x; 664 char *s; 665 666 if (still_ppp()) { 667 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) { 668 s = NULL; 669 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) { 670 case SC_RCV_B7_0: 671 s = "all had bit 7 set to 1"; 672 break; 673 674 case SC_RCV_B7_1: 675 s = "all had bit 7 set to 0"; 676 break; 677 678 case SC_RCV_EVNP: 679 s = "all had odd parity"; 680 break; 681 682 case SC_RCV_ODDP: 683 s = "all had even parity"; 684 break; 685 } 686 687 if (s != NULL) { 688 warn("Receive serial link is not 8-bit clean:"); 689 warn("Problem: %s", s); 690 } 691 } 692 } 693} 694 695 696/* 697 * List of valid speeds. 698 */ 699 700struct speed { 701 int speed_int, speed_val; 702} speeds[] = { 703#ifdef B50 704 { 50, B50 }, 705#endif 706#ifdef B75 707 { 75, B75 }, 708#endif 709#ifdef B110 710 { 110, B110 }, 711#endif 712#ifdef B134 713 { 134, B134 }, 714#endif 715#ifdef B150 716 { 150, B150 }, 717#endif 718#ifdef B200 719 { 200, B200 }, 720#endif 721#ifdef B300 722 { 300, B300 }, 723#endif 724#ifdef B600 725 { 600, B600 }, 726#endif 727#ifdef B1200 728 { 1200, B1200 }, 729#endif 730#ifdef B1800 731 { 1800, B1800 }, 732#endif 733#ifdef B2000 734 { 2000, B2000 }, 735#endif 736#ifdef B2400 737 { 2400, B2400 }, 738#endif 739#ifdef B3600 740 { 3600, B3600 }, 741#endif 742#ifdef B4800 743 { 4800, B4800 }, 744#endif 745#ifdef B7200 746 { 7200, B7200 }, 747#endif 748#ifdef B9600 749 { 9600, B9600 }, 750#endif 751#ifdef B19200 752 { 19200, B19200 }, 753#endif 754#ifdef B38400 755 { 38400, B38400 }, 756#endif 757#ifdef B57600 758 { 57600, B57600 }, 759#endif 760#ifdef B76800 761 { 76800, B76800 }, 762#endif 763#ifdef B115200 764 { 115200, B115200 }, 765#endif 766#ifdef EXTA 767 { 19200, EXTA }, 768#endif 769#ifdef EXTB 770 { 38400, EXTB }, 771#endif 772#ifdef B230400 773 { 230400, B230400 }, 774#endif 775#ifdef B460800 776 { 460800, B460800 }, 777#endif 778#ifdef B921600 779 { 921600, B921600 }, 780#endif 781 { 0, 0 } 782}; 783 784/******************************************************************** 785 * 786 * Translate from bits/second to a speed_t. 787 */ 788 789static int translate_speed (int bps) 790{ 791 struct speed *speedp; 792 793 if (bps != 0) { 794 for (speedp = speeds; speedp->speed_int; speedp++) { 795 if (bps == speedp->speed_int) 796 return speedp->speed_val; 797 } 798 warn("speed %d not supported", bps); 799 } 800 return 0; 801} 802 803/******************************************************************** 804 * 805 * Translate from a speed_t to bits/second. 806 */ 807 808static int baud_rate_of (int speed) 809{ 810 struct speed *speedp; 811 812 if (speed != 0) { 813 for (speedp = speeds; speedp->speed_int; speedp++) { 814 if (speed == speedp->speed_val) 815 return speedp->speed_int; 816 } 817 } 818 return 0; 819} 820 821/******************************************************************** 822 * 823 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity, 824 * at the requested speed, etc. If `local' is true, set CLOCAL 825 * regardless of whether the modem option was specified. 826 */ 827 828void set_up_tty(int tty_fd, int local) 829{ 830 int speed; 831 struct termios tios; 832 833 setdtr(tty_fd, 1); 834 if (tcgetattr(tty_fd, &tios) < 0) { 835 if (!ok_error(errno)) 836 fatal("tcgetattr: %m(%d)", errno); 837 return; 838 } 839 840 if (!restore_term) 841 inittermios = tios; 842 843 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL); 844 tios.c_cflag |= CS8 | CREAD | HUPCL; 845 846 tios.c_iflag = IGNBRK | IGNPAR; 847 tios.c_oflag = 0; 848 tios.c_lflag = 0; 849 tios.c_cc[VMIN] = 1; 850 tios.c_cc[VTIME] = 0; 851 852 if (local || !modem) 853 tios.c_cflag ^= (CLOCAL | HUPCL); 854 855 switch (crtscts) { 856 case 1: 857 tios.c_cflag |= CRTSCTS; 858 break; 859 860 case -2: 861 tios.c_iflag |= IXON | IXOFF; 862 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */ 863 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */ 864 break; 865 866 case -1: 867 tios.c_cflag &= ~CRTSCTS; 868 break; 869 870 default: 871 break; 872 } 873 874 speed = translate_speed(inspeed); 875 if (speed) { 876 cfsetospeed (&tios, speed); 877 cfsetispeed (&tios, speed); 878 } 879/* 880 * We can't proceed if the serial port speed is B0, 881 * since that implies that the serial port is disabled. 882 */ 883 else { 884 speed = cfgetospeed(&tios); 885 if (speed == B0) 886 fatal("Baud rate for %s is 0; need explicit baud rate", devnam); 887 } 888 889 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0) 890 if (!ok_error(errno)) 891 fatal("tcsetattr: %m"); 892 893 baud_rate = baud_rate_of(speed); 894 restore_term = 1; 895} 896 897/******************************************************************** 898 * 899 * setdtr - control the DTR line on the serial port. 900 * This is called from die(), so it shouldn't call die(). 901 */ 902 903void setdtr (int tty_fd, int on) 904{ 905 int modembits = TIOCM_DTR; 906 907 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits); 908} 909 910/******************************************************************** 911 * 912 * restore_tty - restore the terminal to the saved settings. 913 */ 914 915void restore_tty (int tty_fd) 916{ 917 if (restore_term) { 918 restore_term = 0; 919/* 920 * Turn off echoing, because otherwise we can get into 921 * a loop with the tty and the modem echoing to each other. 922 * We presume we are the sole user of this tty device, so 923 * when we close it, it will revert to its defaults anyway. 924 */ 925 if (!default_device) 926 inittermios.c_lflag &= ~(ECHO | ECHONL); 927 928 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) { 929 if (! ok_error (errno)) 930 warn("tcsetattr: %m"); 931 } 932 } 933} 934 935/******************************************************************** 936 * 937 * output - Output PPP packet. 938 */ 939 940void output (int unit, unsigned char *p, int len) 941{ 942 int fd = ppp_fd; 943 int proto; 944 945 if (debug) 946 dbglog("sent %P", p, len); 947 948 if (len < PPP_HDRLEN) 949 return; 950 if (new_style_driver) { 951 p += 2; 952 len -= 2; 953 proto = (p[0] << 8) + p[1]; 954 if (ifunit >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG)) 955 fd = ppp_dev_fd; 956 } 957 if (write(fd, p, len) < 0) { 958 if (errno == EWOULDBLOCK || errno == ENOBUFS 959 || errno == ENXIO || errno == EIO || errno == EINTR) 960 warn("write: warning: %m (%d)", errno); 961 else 962 error("write: %m (%d)", errno); 963 } 964} 965 966/******************************************************************** 967 * 968 * wait_input - wait until there is data available, 969 * for the length of time specified by *timo (indefinite 970 * if timo is NULL). 971 */ 972 973void wait_input(struct timeval *timo) 974{ 975 fd_set ready, exc; 976 int n; 977 978 ready = in_fds; 979 exc = in_fds; 980 n = select(max_in_fd + 1, &ready, NULL, &exc, timo); 981 if (n < 0 && errno != EINTR) 982 fatal("select: %m(%d)", errno); 983} 984 985/* 986 * add_fd - add an fd to the set that wait_input waits for. 987 */ 988void add_fd(int fd) 989{ 990 FD_SET(fd, &in_fds); 991 if (fd > max_in_fd) 992 max_in_fd = fd; 993} 994 995/* 996 * remove_fd - remove an fd from the set that wait_input waits for. 997 */ 998void remove_fd(int fd) 999{ 1000 FD_CLR(fd, &in_fds); 1001} 1002 1003 1004/******************************************************************** 1005 * 1006 * read_packet - get a PPP packet from the serial device. 1007 */ 1008 1009int read_packet (unsigned char *buf) 1010{ 1011 int len, nr; 1012 1013 len = PPP_MRU + PPP_HDRLEN; 1014 if (new_style_driver) { 1015 *buf++ = PPP_ALLSTATIONS; 1016 *buf++ = PPP_UI; 1017 len -= 2; 1018 } 1019 nr = -1; 1020 if (ppp_fd >= 0) { 1021 nr = read(ppp_fd, buf, len); 1022 if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR) 1023 error("read: %m"); 1024 if (nr < 0 && errno == ENXIO) 1025 return 0; 1026 } 1027 if (nr < 0 && new_style_driver && ifunit >= 0) { 1028 /* N.B. we read ppp_fd first since LCP packets come in there. */ 1029 nr = read(ppp_dev_fd, buf, len); 1030 if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR) 1031 error("read /dev/ppp: %m"); 1032 if (nr < 0 && errno == ENXIO) 1033 return 0; 1034 } 1035 return (new_style_driver && nr > 0)? nr+2: nr; 1036} 1037 1038/******************************************************************** 1039 * 1040 * get_loop_output - get outgoing packets from the ppp device, 1041 * and detect when we want to bring the real link up. 1042 * Return value is 1 if we need to bring up the link, 0 otherwise. 1043 */ 1044int 1045get_loop_output(void) 1046{ 1047 int rv = 0; 1048 int n; 1049 1050 if (new_style_driver) { 1051 while ((n = read_packet(inpacket_buf)) > 0) 1052 if (loop_frame(inpacket_buf, n)) 1053 rv = 1; 1054 return rv; 1055 } 1056 1057 while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0) 1058 if (loop_chars(inbuf, n)) 1059 rv = 1; 1060 1061 if (n == 0) 1062 fatal("eof on loopback"); 1063 1064 if (errno != EWOULDBLOCK) 1065 fatal("read from loopback: %m(%d)", errno); 1066 1067 return rv; 1068} 1069 1070/* 1071 * netif_set_mtu - set the MTU on the PPP network interface. 1072 */ 1073void 1074netif_set_mtu(int unit, int mtu) 1075{ 1076 struct ifreq ifr; 1077 1078 SYSDEBUG ((LOG_DEBUG, "netif_set_mtu: mtu = %d\n", mtu)); 1079 1080 memset (&ifr, '\0', sizeof (ifr)); 1081 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); 1082 ifr.ifr_mtu = mtu; 1083 1084 if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0) 1085 fatal("ioctl(SIOCSIFMTU): %m"); 1086} 1087 1088/******************************************************************** 1089 * 1090 * tty_send_config - configure the transmit characteristics of 1091 * the ppp interface. 1092 */ 1093 1094void tty_send_config (int mtu,u_int32_t asyncmap,int pcomp,int accomp) 1095{ 1096 u_int x; 1097 1098/* 1099 * Set the asyncmap and other parameters for the ppp device 1100 */ 1101 if (!still_ppp()) 1102 return; 1103 link_mtu = mtu; 1104 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap)); 1105 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) { 1106 if (!ok_error(errno)) 1107 fatal("ioctl(PPPIOCSASYNCMAP): %m(%d)", errno); 1108 return; 1109 } 1110 1111 x = get_flags(ppp_fd); 1112 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT; 1113 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC; 1114 x = sync_serial ? x | SC_SYNC : x & ~SC_SYNC; 1115 set_flags(ppp_fd, x); 1116} 1117 1118/******************************************************************** 1119 * 1120 * tty_set_xaccm - set the extended transmit ACCM for the interface. 1121 */ 1122 1123void tty_set_xaccm (ext_accm accm) 1124{ 1125 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n", 1126 accm[0], accm[1], accm[2], accm[3])); 1127 1128 if (!still_ppp()) 1129 return; 1130 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) { 1131 if ( ! ok_error (errno)) 1132 warn("ioctl(set extended ACCM): %m(%d)", errno); 1133 } 1134} 1135 1136/******************************************************************** 1137 * 1138 * tty_recv_config - configure the receive-side characteristics of 1139 * the ppp interface. 1140 */ 1141 1142void tty_recv_config (int mru,u_int32_t asyncmap,int pcomp,int accomp) 1143{ 1144 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru)); 1145/* 1146 * If we were called because the link has gone down then there is nothing 1147 * which may be done. Just return without incident. 1148 */ 1149 if (!still_ppp()) 1150 return; 1151/* 1152 * Set the receiver parameters 1153 */ 1154 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) { 1155 if ( ! ok_error (errno)) 1156 error("ioctl(PPPIOCSMRU): %m(%d)", errno); 1157 } 1158 if (new_style_driver && ifunit >= 0 1159 && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) 1160 error("Couldn't set MRU in generic PPP layer: %m"); 1161 1162 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap)); 1163 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) { 1164 if (!ok_error(errno)) 1165 error("ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno); 1166 } 1167} 1168 1169/******************************************************************** 1170 * 1171 * ccp_test - ask kernel whether a given compression method 1172 * is acceptable for use. 1173 */ 1174 1175int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit) 1176{ 1177 struct ppp_option_data data; 1178 1179 memset (&data, '\0', sizeof (data)); 1180 data.ptr = opt_ptr; 1181 data.length = opt_len; 1182 data.transmit = for_transmit; 1183 1184 if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0) 1185 return 1; 1186 1187 return (errno == ENOBUFS)? 0: -1; 1188} 1189 1190/******************************************************************** 1191 * 1192 * ccp_flags_set - inform kernel about the current state of CCP. 1193 */ 1194 1195void ccp_flags_set (int unit, int isopen, int isup) 1196{ 1197 if (still_ppp()) { 1198 int x = get_flags(ppp_dev_fd); 1199 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN; 1200 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP; 1201 set_flags (ppp_dev_fd, x); 1202 } 1203} 1204 1205#ifdef PPP_FILTER 1206/* 1207 * set_filters - set the active and pass filters in the kernel driver. 1208 */ 1209int set_filters(struct bpf_program *pass, struct bpf_program *active) 1210{ 1211 struct sock_fprog fp; 1212 1213 fp.len = pass->bf_len; 1214 fp.filter = (struct sock_filter *) pass->bf_insns; 1215 if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) { 1216 if (errno == ENOTTY) 1217 warn("kernel does not support PPP filtering"); 1218 else 1219 error("Couldn't set pass-filter in kernel: %m"); 1220 return 0; 1221 } 1222 fp.len = active->bf_len; 1223 fp.filter = (struct sock_filter *) active->bf_insns; 1224 if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) { 1225 error("Couldn't set active-filter in kernel: %m"); 1226 return 0; 1227 } 1228 return 1; 1229} 1230#endif /* PPP_FILTER */ 1231 1232/******************************************************************** 1233 * 1234 * get_idle_time - return how long the link has been idle. 1235 */ 1236int 1237get_idle_time(u, ip) 1238 int u; 1239 struct ppp_idle *ip; 1240{ 1241 return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0; 1242} 1243 1244/******************************************************************** 1245 * 1246 * get_ppp_stats - return statistics for the link. 1247 */ 1248int 1249get_ppp_stats(u, stats) 1250 int u; 1251 struct pppd_stats *stats; 1252{ 1253 struct ifpppstatsreq req; 1254 1255 memset (&req, 0, sizeof (req)); 1256 1257 req.stats_ptr = (caddr_t) &req.stats; 1258 strlcpy(req.ifr__name, ifname, sizeof(req.ifr__name)); 1259 if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) { 1260 error("Couldn't get PPP statistics: %m"); 1261 return 0; 1262 } 1263 stats->bytes_in = req.stats.p.ppp_ibytes; 1264 stats->bytes_out = req.stats.p.ppp_obytes; 1265 return 1; 1266} 1267 1268/******************************************************************** 1269 * 1270 * ccp_fatal_error - returns 1 if decompression was disabled as a 1271 * result of an error detected after decompression of a packet, 1272 * 0 otherwise. This is necessary because of patent nonsense. 1273 */ 1274 1275int ccp_fatal_error (int unit) 1276{ 1277 int x = get_flags(ppp_dev_fd); 1278 1279 return x & SC_DC_FERROR; 1280} 1281 1282/******************************************************************** 1283 * 1284 * path_to_procfs - find the path to the proc file system mount point 1285 */ 1286static char proc_path[MAXPATHLEN]; 1287static int proc_path_len; 1288 1289static char *path_to_procfs(const char *tail) 1290{ 1291 struct mntent *mntent; 1292 FILE *fp; 1293 1294 if (proc_path_len == 0) { 1295 /* Default the mount location of /proc */ 1296 strlcpy (proc_path, "/proc", sizeof(proc_path)); 1297 proc_path_len = 5; 1298 fp = fopen(MOUNTED, "r"); 1299 if (fp != NULL) { 1300 while ((mntent = getmntent(fp)) != NULL) { 1301 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0) 1302 continue; 1303 if (strcmp(mntent->mnt_type, "proc") == 0) { 1304 strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path)); 1305 proc_path_len = strlen(proc_path); 1306 break; 1307 } 1308 } 1309 fclose (fp); 1310 } 1311 } 1312 1313 strlcpy(proc_path + proc_path_len, tail, 1314 sizeof(proc_path) - proc_path_len); 1315 return proc_path; 1316} 1317 1318/* 1319 * /proc/net/route parsing stuff. 1320 */ 1321#define ROUTE_MAX_COLS 12 1322FILE *route_fd = (FILE *) 0; 1323static char route_buffer[512]; 1324static int route_dev_col, route_dest_col, route_gw_col; 1325static int route_flags_col, route_mask_col; 1326static int route_num_cols; 1327 1328static int open_route_table (void); 1329static void close_route_table (void); 1330static int read_route_table (struct rtentry *rt); 1331 1332/******************************************************************** 1333 * 1334 * close_route_table - close the interface to the route table 1335 */ 1336 1337static void close_route_table (void) 1338{ 1339 if (route_fd != (FILE *) 0) { 1340 fclose (route_fd); 1341 route_fd = (FILE *) 0; 1342 } 1343} 1344 1345/******************************************************************** 1346 * 1347 * open_route_table - open the interface to the route table 1348 */ 1349static char route_delims[] = " \t\n"; 1350 1351static int open_route_table (void) 1352{ 1353 char *path; 1354 1355 close_route_table(); 1356 1357 path = path_to_procfs("/net/route"); 1358 route_fd = fopen (path, "r"); 1359 if (route_fd == NULL) { 1360 error("can't open routing table %s: %m", path); 1361 return 0; 1362 } 1363 1364 route_dev_col = 0; /* default to usual columns */ 1365 route_dest_col = 1; 1366 route_gw_col = 2; 1367 route_flags_col = 3; 1368 route_mask_col = 7; 1369 route_num_cols = 8; 1370 1371 /* parse header line */ 1372 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) { 1373 char *p = route_buffer, *q; 1374 int col; 1375 for (col = 0; col < ROUTE_MAX_COLS; ++col) { 1376 int used = 1; 1377 if ((q = strtok(p, route_delims)) == 0) 1378 break; 1379 if (strcasecmp(q, "iface") == 0) 1380 route_dev_col = col; 1381 else if (strcasecmp(q, "destination") == 0) 1382 route_dest_col = col; 1383 else if (strcasecmp(q, "gateway") == 0) 1384 route_gw_col = col; 1385 else if (strcasecmp(q, "flags") == 0) 1386 route_flags_col = col; 1387 else if (strcasecmp(q, "mask") == 0) 1388 route_mask_col = col; 1389 else 1390 used = 0; 1391 if (used && col >= route_num_cols) 1392 route_num_cols = col + 1; 1393 p = NULL; 1394 } 1395 } 1396 1397 return 1; 1398} 1399 1400/******************************************************************** 1401 * 1402 * read_route_table - read the next entry from the route table 1403 */ 1404 1405static int read_route_table(struct rtentry *rt) 1406{ 1407 char *cols[ROUTE_MAX_COLS], *p; 1408 int col; 1409 1410 memset (rt, '\0', sizeof (struct rtentry)); 1411 1412 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) 1413 return 0; 1414 1415 p = route_buffer; 1416 for (col = 0; col < route_num_cols; ++col) { 1417 cols[col] = strtok(p, route_delims); 1418 if (cols[col] == NULL) 1419 return 0; /* didn't get enough columns */ 1420 p = NULL; 1421 } 1422 1423 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16); 1424 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16); 1425 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16); 1426 1427 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16); 1428 rt->rt_dev = cols[route_dev_col]; 1429 1430 return 1; 1431} 1432 1433/******************************************************************** 1434 * 1435 * defaultroute_exists - determine if there is a default route 1436 */ 1437 1438static int defaultroute_exists (struct rtentry *rt) 1439{ 1440 int result = 0; 1441 1442 if (!open_route_table()) 1443 return 0; 1444 1445 while (read_route_table(rt) != 0) { 1446 if ((rt->rt_flags & RTF_UP) == 0) 1447 continue; 1448 1449 if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0) 1450 continue; 1451 if (SIN_ADDR(rt->rt_dst) == 0L) { 1452 result = 1; 1453 break; 1454 } 1455 } 1456 1457 close_route_table(); 1458 return result; 1459} 1460 1461/* 1462 * have_route_to - determine if the system has any route to 1463 * a given IP address. `addr' is in network byte order. 1464 * Return value is 1 if yes, 0 if no, -1 if don't know. 1465 * For demand mode to work properly, we have to ignore routes 1466 * through our own interface. 1467 */ 1468int have_route_to(u_int32_t addr) 1469{ 1470 struct rtentry rt; 1471 int result = 0; 1472 1473 if (!open_route_table()) 1474 return -1; /* don't know */ 1475 1476 while (read_route_table(&rt)) { 1477 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0) 1478 continue; 1479 if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) { 1480 result = 1; 1481 break; 1482 } 1483 } 1484 1485 close_route_table(); 1486 return result; 1487} 1488 1489/******************************************************************** 1490 * 1491 * sifdefaultroute - assign a default route through the address given. 1492 */ 1493 1494int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway) 1495{ 1496 struct rtentry rt; 1497 1498 if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) { 1499 u_int32_t old_gateway = SIN_ADDR(rt.rt_gateway); 1500 1501 if (old_gateway != gateway) 1502 error("not replacing existing default route to %s [%I]", 1503 rt.rt_dev, old_gateway); 1504 return 0; 1505 } 1506 1507 memset (&rt, '\0', sizeof (rt)); 1508 SET_SA_FAMILY (rt.rt_dst, AF_INET); 1509 SET_SA_FAMILY (rt.rt_gateway, AF_INET); 1510 1511 if (kernel_version > KVERSION(2,1,0)) { 1512 SET_SA_FAMILY (rt.rt_genmask, AF_INET); 1513 SIN_ADDR(rt.rt_genmask) = 0L; 1514 } 1515 1516 SIN_ADDR(rt.rt_gateway) = gateway; 1517 1518 rt.rt_flags = RTF_UP | RTF_GATEWAY; 1519 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) { 1520 if ( ! ok_error ( errno )) 1521 error("default route ioctl(SIOCADDRT): %m(%d)", errno); 1522 return 0; 1523 } 1524 1525 default_route_gateway = gateway; 1526 return 1; 1527} 1528 1529/******************************************************************** 1530 * 1531 * cifdefaultroute - delete a default route through the address given. 1532 */ 1533 1534int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway) 1535{ 1536 struct rtentry rt; 1537 1538 default_route_gateway = 0; 1539 1540 memset (&rt, '\0', sizeof (rt)); 1541 SET_SA_FAMILY (rt.rt_dst, AF_INET); 1542 SET_SA_FAMILY (rt.rt_gateway, AF_INET); 1543 1544 if (kernel_version > KVERSION(2,1,0)) { 1545 SET_SA_FAMILY (rt.rt_genmask, AF_INET); 1546 SIN_ADDR(rt.rt_genmask) = 0L; 1547 } 1548 1549 SIN_ADDR(rt.rt_gateway) = gateway; 1550 1551 rt.rt_flags = RTF_UP | RTF_GATEWAY; 1552 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) { 1553 if (still_ppp()) { 1554 if ( ! ok_error ( errno )) 1555 error("default route ioctl(SIOCDELRT): %m (%d)", errno); 1556 return 0; 1557 } 1558 } 1559 1560 return 1; 1561} 1562 1563/******************************************************************** 1564 * 1565 * sifproxyarp - Make a proxy ARP entry for the peer. 1566 */ 1567 1568int sifproxyarp (int unit, u_int32_t his_adr) 1569{ 1570 struct arpreq arpreq; 1571 char *forw_path; 1572 1573 if (has_proxy_arp == 0) { 1574 memset (&arpreq, '\0', sizeof(arpreq)); 1575 1576 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 1577 SIN_ADDR(arpreq.arp_pa) = his_adr; 1578 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 1579/* 1580 * Get the hardware address of an interface on the same subnet 1581 * as our local address. 1582 */ 1583 if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev, 1584 sizeof(proxy_arp_dev))) { 1585 error("Cannot determine ethernet address for proxy ARP"); 1586 return 0; 1587 } 1588 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev)); 1589 1590 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) { 1591 if ( ! ok_error ( errno )) 1592 error("ioctl(SIOCSARP): %m(%d)", errno); 1593 return 0; 1594 } 1595 proxy_arp_addr = his_adr; 1596 has_proxy_arp = 1; 1597 1598 if (tune_kernel) { 1599 forw_path = path_to_procfs("/sys/net/ipv4/ip_forward"); 1600 if (forw_path != 0) { 1601 int fd = open(forw_path, O_WRONLY); 1602 if (fd >= 0) { 1603 if (write(fd, "1", 1) != 1) 1604 error("Couldn't enable IP forwarding: %m"); 1605 close(fd); 1606 } 1607 } 1608 } 1609 } 1610 1611 return 1; 1612} 1613 1614/******************************************************************** 1615 * 1616 * cifproxyarp - Delete the proxy ARP entry for the peer. 1617 */ 1618 1619int cifproxyarp (int unit, u_int32_t his_adr) 1620{ 1621 struct arpreq arpreq; 1622 1623 if (has_proxy_arp) { 1624 has_proxy_arp = 0; 1625 memset (&arpreq, '\0', sizeof(arpreq)); 1626 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 1627 SIN_ADDR(arpreq.arp_pa) = his_adr; 1628 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 1629 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev)); 1630 1631 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) { 1632 if ( ! ok_error ( errno )) 1633 warn("ioctl(SIOCDARP): %m(%d)", errno); 1634 return 0; 1635 } 1636 } 1637 return 1; 1638} 1639 1640/******************************************************************** 1641 * 1642 * get_ether_addr - get the hardware address of an interface on the 1643 * the same subnet as ipaddr. 1644 */ 1645 1646static int get_ether_addr (u_int32_t ipaddr, 1647 struct sockaddr *hwaddr, 1648 char *name, int namelen) 1649{ 1650 struct ifreq *ifr, *ifend; 1651 u_int32_t ina, mask; 1652 char *aliasp; 1653 struct ifreq ifreq; 1654 struct ifconf ifc; 1655 struct ifreq ifs[MAX_IFS]; 1656 1657 ifc.ifc_len = sizeof(ifs); 1658 ifc.ifc_req = ifs; 1659 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) { 1660 if ( ! ok_error ( errno )) 1661 error("ioctl(SIOCGIFCONF): %m(%d)", errno); 1662 return 0; 1663 } 1664 1665 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s", 1666 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr))); 1667/* 1668 * Scan through looking for an interface with an Internet 1669 * address on the same subnet as `ipaddr'. 1670 */ 1671 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq)); 1672 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) { 1673 if (ifr->ifr_addr.sa_family == AF_INET) { 1674 ina = SIN_ADDR(ifr->ifr_addr); 1675 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); 1676 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s", 1677 ifreq.ifr_name)); 1678/* 1679 * Check that the interface is up, and not point-to-point 1680 * nor loopback. 1681 */ 1682 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0) 1683 continue; 1684 1685 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0) 1686 continue; 1687/* 1688 * Get its netmask and check that it's on the right subnet. 1689 */ 1690 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0) 1691 continue; 1692 1693 mask = SIN_ADDR(ifreq.ifr_addr); 1694 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx", 1695 ip_ntoa(ina), ntohl(mask))); 1696 1697 if (((ipaddr ^ ina) & mask) != 0) 1698 continue; 1699 break; 1700 } 1701 } 1702 1703 if (ifr >= ifend) 1704 return 0; 1705 1706 strlcpy(name, ifreq.ifr_name, namelen); 1707 1708 /* trim off the :1 in eth0:1 */ 1709 aliasp = strchr(name, ':'); 1710 if (aliasp != 0) 1711 *aliasp = 0; 1712 1713 info("found interface %s for proxy arp", name); 1714/* 1715 * Now get the hardware address. 1716 */ 1717 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr)); 1718 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0) { 1719 error("SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno); 1720 return 0; 1721 } 1722 1723 memcpy (hwaddr, 1724 &ifreq.ifr_hwaddr, 1725 sizeof (struct sockaddr)); 1726 1727 SYSDEBUG ((LOG_DEBUG, 1728 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", 1729 (int) ((unsigned char *) &hwaddr->sa_data)[0], 1730 (int) ((unsigned char *) &hwaddr->sa_data)[1], 1731 (int) ((unsigned char *) &hwaddr->sa_data)[2], 1732 (int) ((unsigned char *) &hwaddr->sa_data)[3], 1733 (int) ((unsigned char *) &hwaddr->sa_data)[4], 1734 (int) ((unsigned char *) &hwaddr->sa_data)[5], 1735 (int) ((unsigned char *) &hwaddr->sa_data)[6], 1736 (int) ((unsigned char *) &hwaddr->sa_data)[7])); 1737 return 1; 1738} 1739 1740/* 1741 * get_if_hwaddr - get the hardware address for the specified 1742 * network interface device. 1743 */ 1744int 1745get_if_hwaddr(u_char *addr, char *name) 1746{ 1747 struct ifreq ifreq; 1748 int ret, sock_fd; 1749 1750 sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 1751 if (sock_fd < 0) 1752 return 0; 1753 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr)); 1754 strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name)); 1755 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq); 1756 close(sock_fd); 1757 if (ret >= 0) 1758 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6); 1759 return ret; 1760} 1761 1762/* 1763 * get_first_ethernet - return the name of the first ethernet-style 1764 * interface on this system. 1765 */ 1766char * 1767get_first_ethernet() 1768{ 1769 return "eth0"; 1770} 1771 1772/******************************************************************** 1773 * 1774 * Return user specified netmask, modified by any mask we might determine 1775 * for address `addr' (in network byte order). 1776 * Here we scan through the system's list of interfaces, looking for 1777 * any non-point-to-point interfaces which might appear to be on the same 1778 * network as `addr'. If we find any, we OR in their netmask to the 1779 * user-specified netmask. 1780 */ 1781 1782u_int32_t GetMask (u_int32_t addr) 1783{ 1784 u_int32_t mask, nmask, ina; 1785 struct ifreq *ifr, *ifend, ifreq; 1786 struct ifconf ifc; 1787 struct ifreq ifs[MAX_IFS]; 1788 1789 addr = ntohl(addr); 1790 1791 if (IN_CLASSA(addr)) /* determine network mask for address class */ 1792 nmask = IN_CLASSA_NET; 1793 else if (IN_CLASSB(addr)) 1794 nmask = IN_CLASSB_NET; 1795 else 1796 nmask = IN_CLASSC_NET; 1797 1798 /* class D nets are disallowed by bad_ip_adrs */ 1799 mask = netmask | htonl(nmask); 1800/* 1801 * Scan through the system's network interfaces. 1802 */ 1803 ifc.ifc_len = sizeof(ifs); 1804 ifc.ifc_req = ifs; 1805 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) { 1806 if ( ! ok_error ( errno )) 1807 warn("ioctl(SIOCGIFCONF): %m(%d)", errno); 1808 return mask; 1809 } 1810 1811 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); 1812 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) { 1813/* 1814 * Check the interface's internet address. 1815 */ 1816 if (ifr->ifr_addr.sa_family != AF_INET) 1817 continue; 1818 ina = SIN_ADDR(ifr->ifr_addr); 1819 if (((ntohl(ina) ^ addr) & nmask) != 0) 1820 continue; 1821/* 1822 * Check that the interface is up, and not point-to-point nor loopback. 1823 */ 1824 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); 1825 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0) 1826 continue; 1827 1828 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0) 1829 continue; 1830/* 1831 * Get its netmask and OR it into our mask. 1832 */ 1833 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0) 1834 continue; 1835 mask |= SIN_ADDR(ifreq.ifr_addr); 1836 break; 1837 } 1838 return mask; 1839} 1840 1841/******************************************************************** 1842 * 1843 * Internal routine to decode the version.modification.patch level 1844 */ 1845 1846static void decode_version (char *buf, int *version, 1847 int *modification, int *patch) 1848{ 1849 char *endp; 1850 1851 *version = (int) strtoul (buf, &endp, 10); 1852 *modification = 0; 1853 *patch = 0; 1854 1855 if (endp != buf && *endp == '.') { 1856 buf = endp + 1; 1857 *modification = (int) strtoul (buf, &endp, 10); 1858 if (endp != buf && *endp == '.') { 1859 buf = endp + 1; 1860 *patch = (int) strtoul (buf, &buf, 10); 1861 } 1862 } 1863} 1864 1865/******************************************************************** 1866 * 1867 * Procedure to determine if the PPP line discipline is registered. 1868 */ 1869 1870static int 1871ppp_registered(void) 1872{ 1873 int local_fd; 1874 int mfd = -1; 1875 int ret = 0; 1876 char slave[16]; 1877 1878 /* 1879 * We used to open the serial device and set it to the ppp line 1880 * discipline here, in order to create a ppp unit. But that is 1881 * not a good idea - the user might have specified a device that 1882 * they can't open (permission, or maybe it doesn't really exist). 1883 * So we grab a pty master/slave pair and use that. 1884 */ 1885 if (!get_pty(&mfd, &local_fd, slave, 0)) { 1886 no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)"; 1887 return 0; 1888 } 1889 1890 /* 1891 * Try to put the device into the PPP discipline. 1892 */ 1893 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) { 1894 error("ioctl(TIOCSETD(PPP)): %m(%d)", errno); 1895 } else 1896 ret = 1; 1897 1898 close(local_fd); 1899 close(mfd); 1900 return ret; 1901} 1902 1903/******************************************************************** 1904 * 1905 * ppp_available - check whether the system has any ppp interfaces 1906 * (in fact we check whether we can do an ioctl on ppp0). 1907 */ 1908 1909int ppp_available(void) 1910{ 1911 int s, ok, fd; 1912 struct ifreq ifr; 1913 int size; 1914 int my_version, my_modification, my_patch; 1915 int osmaj, osmin, ospatch; 1916 1917 no_ppp_msg = 1918 "This system lacks kernel support for PPP. This could be because\n" 1919 "the PPP kernel module could not be loaded, or because PPP was not\n" 1920 "included in the kernel configuration. If PPP was included as a\n" 1921 "module, try `/sbin/modprobe -v ppp'. If that fails, check that\n" 1922 "ppp.o exists in /lib/modules/`uname -r`/net.\n" 1923 "See README.linux file in the ppp distribution for more details.\n"; 1924 1925 /* get the kernel version now, since we are called before sys_init */ 1926 uname(&utsname); 1927 osmaj = osmin = ospatch = 0; 1928 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch); 1929 kernel_version = KVERSION(osmaj, osmin, ospatch); 1930 1931 fd = open("/dev/ppp", O_RDWR); 1932 if (fd >= 0) { 1933 new_style_driver = 1; 1934 1935 driver_version = 2; 1936 driver_modification = 4; 1937 driver_patch = 0; 1938 close(fd); 1939 return 1; 1940 } 1941 if (kernel_version >= KVERSION(2,3,13)) { 1942 if (errno == ENOENT) 1943 no_ppp_msg = 1944 "pppd is unable to open the /dev/ppp device.\n" 1945 "You need to create the /dev/ppp device node by\n" 1946 "executing the following command as root:\n" 1947 " mknod /dev/ppp c 108 0\n"; 1948 return 0; 1949 } 1950 1951/* 1952 * Open a socket for doing the ioctl operations. 1953 */ 1954 s = socket(AF_INET, SOCK_DGRAM, 0); 1955 if (s < 0) 1956 return 0; 1957 1958 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name)); 1959 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0; 1960/* 1961 * If the device did not exist then attempt to create one by putting the 1962 * current tty into the PPP discipline. If this works then obtain the 1963 * flags for the device again. 1964 */ 1965 if (!ok) { 1966 if (ppp_registered()) { 1967 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name)); 1968 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0; 1969 } 1970 } 1971/* 1972 * Ensure that the hardware address is for PPP and not something else 1973 */ 1974 if (ok) 1975 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0; 1976 1977 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP)) 1978 ok = 0; 1979 1980/* 1981 * This is the PPP device. Validate the version of the driver at this 1982 * point to ensure that this program will work with the driver. 1983 */ 1984 if (ok) { 1985 char abBuffer [1024]; 1986 1987 ifr.ifr_data = abBuffer; 1988 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr); 1989 if (size < 0) { 1990 error("Couldn't read driver version: %m"); 1991 ok = 0; 1992 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n"; 1993 1994 } else { 1995 decode_version(abBuffer, 1996 &driver_version, 1997 &driver_modification, 1998 &driver_patch); 1999/* 2000 * Validate the version of the driver against the version that we used. 2001 */ 2002 decode_version(VERSION, 2003 &my_version, 2004 &my_modification, 2005 &my_patch); 2006 2007 /* The version numbers must match */ 2008 if (driver_version != my_version) 2009 ok = 0; 2010 2011 /* The modification levels must be legal */ 2012 if (driver_modification < 3) { 2013 if (driver_modification >= 2) { 2014 /* we can cope with 2.2.0 and above */ 2015 driver_is_old = 1; 2016 } else { 2017 ok = 0; 2018 } 2019 } 2020 2021 close (s); 2022 if (!ok) { 2023 slprintf(route_buffer, sizeof(route_buffer), 2024 "Sorry - PPP driver version %d.%d.%d is out of date\n", 2025 driver_version, driver_modification, driver_patch); 2026 2027 no_ppp_msg = route_buffer; 2028 } 2029 } 2030 } 2031 return ok; 2032} 2033 2034/******************************************************************** 2035 * 2036 * Update the wtmp file with the appropriate user name and tty device. 2037 */ 2038 2039void logwtmp (const char *line, const char *name, const char *host) 2040{ 2041 struct utmp ut, *utp; 2042 pid_t mypid = getpid(); 2043#if __GLIBC__ < 2 2044 int wtmp; 2045#endif 2046 2047/* 2048 * Update the signon database for users. 2049 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996 2050 */ 2051 utmpname(_PATH_UTMP); 2052 setutent(); 2053 while ((utp = getutent()) && (utp->ut_pid != mypid)) 2054 /* nothing */; 2055 2056 /* Is this call really necessary? There is another one after the 'put' */ 2057 endutent(); 2058 2059 if (utp) 2060 memcpy(&ut, utp, sizeof(ut)); 2061 else 2062 /* some gettys/telnetds don't initialize utmp... */ 2063 memset(&ut, 0, sizeof(ut)); 2064 2065 if (ut.ut_id[0] == 0) 2066 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id)); 2067 2068 strncpy(ut.ut_user, name, sizeof(ut.ut_user)); 2069 strncpy(ut.ut_line, line, sizeof(ut.ut_line)); 2070 2071 time(&ut.ut_time); 2072 2073 ut.ut_type = USER_PROCESS; 2074 ut.ut_pid = mypid; 2075 2076 /* Insert the host name if one is supplied */ 2077 if (*host) 2078 strncpy (ut.ut_host, host, sizeof(ut.ut_host)); 2079 2080 /* Insert the IP address of the remote system if IP is enabled */ 2081 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr) 2082 memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr, 2083 sizeof(ut.ut_addr)); 2084 2085 /* CL: Makes sure that the logout works */ 2086 if (*host == 0 && *name==0) 2087 ut.ut_host[0]=0; 2088 2089 pututline(&ut); 2090 endutent(); 2091/* 2092 * Update the wtmp file. 2093 */ 2094#if __GLIBC__ >= 2 2095 updwtmp(_PATH_WTMP, &ut); 2096#else 2097 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY); 2098 if (wtmp >= 0) { 2099 flock(wtmp, LOCK_EX); 2100 2101 if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut)) 2102 warn("error writing %s: %m", _PATH_WTMP); 2103 2104 flock(wtmp, LOCK_UN); 2105 2106 close (wtmp); 2107 } 2108#endif 2109} 2110 2111 2112/******************************************************************** 2113 * 2114 * sifvjcomp - config tcp header compression 2115 */ 2116 2117int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid) 2118{ 2119 u_int x = get_flags(ppp_dev_fd); 2120 2121 if (vjcomp) { 2122 if (ioctl (ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) { 2123 if (! ok_error (errno)) 2124 error("ioctl(PPPIOCSMAXCID): %m(%d)", errno); 2125 vjcomp = 0; 2126 } 2127 } 2128 2129 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP; 2130 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID; 2131 set_flags (ppp_dev_fd, x); 2132 2133 return 1; 2134} 2135 2136/******************************************************************** 2137 * 2138 * sifup - Config the interface up and enable IP packets to pass. 2139 */ 2140 2141int sifup(int u) 2142{ 2143 struct ifreq ifr; 2144 2145 memset (&ifr, '\0', sizeof (ifr)); 2146 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); 2147 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { 2148 if (! ok_error (errno)) 2149 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno); 2150 return 0; 2151 } 2152 2153 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT); 2154 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) { 2155 if (! ok_error (errno)) 2156 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno); 2157 return 0; 2158 } 2159 if_is_up++; 2160 2161 return 1; 2162} 2163 2164/******************************************************************** 2165 * 2166 * sifdown - Disable the indicated protocol and config the interface 2167 * down if there are no remaining protocols. 2168 */ 2169 2170int sifdown (int u) 2171{ 2172 struct ifreq ifr; 2173 2174 if (if_is_up && --if_is_up > 0) 2175 return 1; 2176 2177 memset (&ifr, '\0', sizeof (ifr)); 2178 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); 2179 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { 2180 if (! ok_error (errno)) 2181 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno); 2182 return 0; 2183 } 2184 2185 ifr.ifr_flags &= ~IFF_UP; 2186 ifr.ifr_flags |= IFF_POINTOPOINT; 2187 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) { 2188 if (! ok_error (errno)) 2189 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno); 2190 return 0; 2191 } 2192 return 1; 2193} 2194 2195/******************************************************************** 2196 * 2197 * sifaddr - Config the interface IP addresses and netmask. 2198 */ 2199 2200int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr, 2201 u_int32_t net_mask) 2202{ 2203 struct ifreq ifr; 2204 struct rtentry rt; 2205 2206 memset (&ifr, '\0', sizeof (ifr)); 2207 memset (&rt, '\0', sizeof (rt)); 2208 2209 SET_SA_FAMILY (ifr.ifr_addr, AF_INET); 2210 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET); 2211 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET); 2212 2213 strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); 2214/* 2215 * Set our IP address 2216 */ 2217 SIN_ADDR(ifr.ifr_addr) = our_adr; 2218 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) { 2219 if (errno != EEXIST) { 2220 if (! ok_error (errno)) 2221 error("ioctl(SIOCSIFADDR): %m(%d)", errno); 2222 } 2223 else { 2224 warn("ioctl(SIOCSIFADDR): Address already exists"); 2225 } 2226 return (0); 2227 } 2228/* 2229 * Set the gateway address 2230 */ 2231 SIN_ADDR(ifr.ifr_dstaddr) = his_adr; 2232 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) { 2233 if (! ok_error (errno)) 2234 error("ioctl(SIOCSIFDSTADDR): %m(%d)", errno); 2235 return (0); 2236 } 2237/* 2238 * Set the netmask. 2239 * For recent kernels, force the netmask to 255.255.255.255. 2240 */ 2241 if (kernel_version >= KVERSION(2,1,16)) 2242 net_mask = ~0L; 2243 if (net_mask != 0) { 2244 SIN_ADDR(ifr.ifr_netmask) = net_mask; 2245 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) { 2246 if (! ok_error (errno)) 2247 error("ioctl(SIOCSIFNETMASK): %m(%d)", errno); 2248 return (0); 2249 } 2250 } 2251/* 2252 * Add the device route 2253 */ 2254 if (kernel_version < KVERSION(2,1,16)) { 2255 SET_SA_FAMILY (rt.rt_dst, AF_INET); 2256 SET_SA_FAMILY (rt.rt_gateway, AF_INET); 2257 rt.rt_dev = ifname; 2258 2259 SIN_ADDR(rt.rt_gateway) = 0L; 2260 SIN_ADDR(rt.rt_dst) = his_adr; 2261 rt.rt_flags = RTF_UP | RTF_HOST; 2262 2263 if (kernel_version > KVERSION(2,1,0)) { 2264 SET_SA_FAMILY (rt.rt_genmask, AF_INET); 2265 SIN_ADDR(rt.rt_genmask) = -1L; 2266 } 2267 2268 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) { 2269 if (! ok_error (errno)) 2270 error("ioctl(SIOCADDRT) device route: %m(%d)", errno); 2271 return (0); 2272 } 2273 } 2274 2275 /* set ip_dynaddr in demand mode if address changes */ 2276 if (demand && tune_kernel && !dynaddr_set 2277 && our_old_addr && our_old_addr != our_adr) { 2278 /* set ip_dynaddr if possible */ 2279 char *path; 2280 int fd; 2281 2282 path = path_to_procfs("/sys/net/ipv4/ip_dynaddr"); 2283 if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) { 2284 if (write(fd, "1", 1) != 1) 2285 error("Couldn't enable dynamic IP addressing: %m"); 2286 close(fd); 2287 } 2288 dynaddr_set = 1; /* only 1 attempt */ 2289 } 2290 our_old_addr = 0; 2291 2292 return 1; 2293} 2294 2295/******************************************************************** 2296 * 2297 * cifaddr - Clear the interface IP addresses, and delete routes 2298 * through the interface if possible. 2299 */ 2300 2301int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr) 2302{ 2303 struct ifreq ifr; 2304 2305 if (kernel_version < KVERSION(2,1,16)) { 2306/* 2307 * Delete the route through the device 2308 */ 2309 struct rtentry rt; 2310 memset (&rt, '\0', sizeof (rt)); 2311 2312 SET_SA_FAMILY (rt.rt_dst, AF_INET); 2313 SET_SA_FAMILY (rt.rt_gateway, AF_INET); 2314 rt.rt_dev = ifname; 2315 2316 SIN_ADDR(rt.rt_gateway) = 0; 2317 SIN_ADDR(rt.rt_dst) = his_adr; 2318 rt.rt_flags = RTF_UP | RTF_HOST; 2319 2320 if (kernel_version > KVERSION(2,1,0)) { 2321 SET_SA_FAMILY (rt.rt_genmask, AF_INET); 2322 SIN_ADDR(rt.rt_genmask) = -1L; 2323 } 2324 2325 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) { 2326 if (still_ppp() && ! ok_error (errno)) 2327 error("ioctl(SIOCDELRT) device route: %m(%d)", errno); 2328 return (0); 2329 } 2330 } 2331 2332 /* This way it is possible to have an IPX-only or IPv6-only interface */ 2333 memset(&ifr, 0, sizeof(ifr)); 2334 SET_SA_FAMILY(ifr.ifr_addr, AF_INET); 2335 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 2336 2337 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) { 2338 if (! ok_error (errno)) { 2339 error("ioctl(SIOCSIFADDR): %m(%d)", errno); 2340 return 0; 2341 } 2342 } 2343 2344 our_old_addr = our_adr; 2345 2346 return 1; 2347} 2348 2349#ifdef INET6 2350/******************************************************************** 2351 * 2352 * sif6addr - Config the interface with an IPv6 link-local address 2353 */ 2354int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64) 2355{ 2356 struct in6_ifreq ifr6; 2357 struct ifreq ifr; 2358 struct in6_rtmsg rt6; 2359 2360 if (sock6_fd < 0) { 2361 errno = -sock6_fd; 2362 error("IPv6 socket creation failed: %m"); 2363 return 0; 2364 } 2365 memset(&ifr, 0, sizeof (ifr)); 2366 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 2367 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) { 2368 error("sif6addr: ioctl(SIOCGIFINDEX): %m (%d)", errno); 2369 return 0; 2370 } 2371 2372 /* Local interface */ 2373 memset(&ifr6, 0, sizeof(ifr6)); 2374 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64); 2375 ifr6.ifr6_ifindex = ifr.ifr_ifindex; 2376 ifr6.ifr6_prefixlen = 10; 2377 2378 if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) { 2379 error("sif6addr: ioctl(SIOCSIFADDR): %m (%d)", errno); 2380 return 0; 2381 } 2382 2383 /* Route to remote host */ 2384 memset(&rt6, 0, sizeof(rt6)); 2385 IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64); 2386 rt6.rtmsg_flags = RTF_UP; 2387 rt6.rtmsg_dst_len = 10; 2388 rt6.rtmsg_ifindex = ifr.ifr_ifindex; 2389 rt6.rtmsg_metric = 1; 2390 2391 if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) { 2392 error("sif6addr: ioctl(SIOCADDRT): %m (%d)", errno); 2393 return 0; 2394 } 2395 2396 return 1; 2397} 2398 2399 2400/******************************************************************** 2401 * 2402 * cif6addr - Remove IPv6 address from interface 2403 */ 2404int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64) 2405{ 2406 struct ifreq ifr; 2407 struct in6_ifreq ifr6; 2408 2409 if (sock6_fd < 0) { 2410 errno = -sock6_fd; 2411 error("IPv6 socket creation failed: %m"); 2412 return 0; 2413 } 2414 memset(&ifr, 0, sizeof(ifr)); 2415 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 2416 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) { 2417 error("cif6addr: ioctl(SIOCGIFINDEX): %m (%d)", errno); 2418 return 0; 2419 } 2420 2421 memset(&ifr6, 0, sizeof(ifr6)); 2422 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64); 2423 ifr6.ifr6_ifindex = ifr.ifr_ifindex; 2424 ifr6.ifr6_prefixlen = 10; 2425 2426 if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) { 2427 if (errno != EADDRNOTAVAIL) { 2428 if (! ok_error (errno)) 2429 error("cif6addr: ioctl(SIOCDIFADDR): %m (%d)", errno); 2430 } 2431 else { 2432 warn("cif6addr: ioctl(SIOCDIFADDR): No such address"); 2433 } 2434 return (0); 2435 } 2436 return 1; 2437} 2438#endif /* INET6 */ 2439 2440/* 2441 * get_pty - get a pty master/slave pair and chown the slave side 2442 * to the uid given. Assumes slave_name points to >= 16 bytes of space. 2443 */ 2444int 2445get_pty(master_fdp, slave_fdp, slave_name, uid) 2446 int *master_fdp; 2447 int *slave_fdp; 2448 char *slave_name; 2449 int uid; 2450{ 2451 int i, mfd, sfd = -1; 2452 char pty_name[16]; 2453 struct termios tios; 2454 2455#ifdef TIOCGPTN 2456 /* 2457 * Try the unix98 way first. 2458 */ 2459 mfd = open("/dev/ptmx", O_RDWR); 2460 if (mfd >= 0) { 2461 int ptn; 2462 if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) { 2463 slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn); 2464 chmod(pty_name, S_IRUSR | S_IWUSR); 2465#ifdef TIOCSPTLCK 2466 ptn = 0; 2467 if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0) 2468 warn("Couldn't unlock pty slave %s: %m", pty_name); 2469#endif 2470 if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0) 2471 warn("Couldn't open pty slave %s: %m", pty_name); 2472 } 2473 } 2474#endif /* TIOCGPTN */ 2475 2476 if (sfd < 0) { 2477 /* the old way - scan through the pty name space */ 2478 for (i = 0; i < 64; ++i) { 2479 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x", 2480 'p' + i / 16, i % 16); 2481 mfd = open(pty_name, O_RDWR, 0); 2482 if (mfd >= 0) { 2483 pty_name[5] = 't'; 2484 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0); 2485 if (sfd >= 0) { 2486 fchown(sfd, uid, -1); 2487 fchmod(sfd, S_IRUSR | S_IWUSR); 2488 break; 2489 } 2490 close(mfd); 2491 } 2492 } 2493 } 2494 2495 if (sfd < 0) 2496 return 0; 2497 2498 strlcpy(slave_name, pty_name, 16); 2499 *master_fdp = mfd; 2500 *slave_fdp = sfd; 2501 if (tcgetattr(sfd, &tios) == 0) { 2502 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB); 2503 tios.c_cflag |= CS8 | CREAD | CLOCAL; 2504 tios.c_iflag = IGNPAR; 2505 tios.c_oflag = 0; 2506 tios.c_lflag = 0; 2507 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0) 2508 warn("couldn't set attributes on pty: %m"); 2509 } else 2510 warn("couldn't get attributes on pty: %m"); 2511 2512 return 1; 2513} 2514 2515/******************************************************************** 2516 * 2517 * open_loopback - open the device we use for getting packets 2518 * in demand mode. Under Linux, we use a pty master/slave pair. 2519 */ 2520int 2521open_ppp_loopback(void) 2522{ 2523 int flags; 2524 2525 looped = 1; 2526 if (new_style_driver) { 2527 /* allocate ourselves a ppp unit */ 2528 if (make_ppp_unit() < 0) 2529 die(1); 2530 set_flags(ppp_dev_fd, SC_LOOP_TRAFFIC); 2531 set_kdebugflag(kdebugflag); 2532 ppp_fd = -1; 2533 return ppp_dev_fd; 2534 } 2535 2536 if (!get_pty(&master_fd, &slave_fd, loop_name, 0)) 2537 fatal("No free pty for loopback"); 2538 SYSDEBUG(("using %s for loopback", loop_name)); 2539 2540 set_ppp_fd(slave_fd); 2541 2542 flags = fcntl(master_fd, F_GETFL); 2543 if (flags == -1 || 2544 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1) 2545 warn("couldn't set master loopback to nonblock: %m(%d)", errno); 2546 2547 flags = fcntl(ppp_fd, F_GETFL); 2548 if (flags == -1 || 2549 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1) 2550 warn("couldn't set slave loopback to nonblock: %m(%d)", errno); 2551 2552 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0) 2553 fatal("ioctl(TIOCSETD): %m(%d)", errno); 2554/* 2555 * Find out which interface we were given. 2556 */ 2557 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) 2558 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno); 2559/* 2560 * Enable debug in the driver if requested. 2561 */ 2562 set_kdebugflag (kdebugflag); 2563 2564 return master_fd; 2565} 2566 2567/******************************************************************** 2568 * 2569 * restore_loop - reattach the ppp unit to the loopback. 2570 * 2571 * The kernel ppp driver automatically reattaches the ppp unit to 2572 * the loopback if the serial port is set to a line discipline other 2573 * than ppp, or if it detects a modem hangup. The former will happen 2574 * in disestablish_ppp if the latter hasn't already happened, so we 2575 * shouldn't need to do anything. 2576 * 2577 * Just to be sure, set the real serial port to the normal discipline. 2578 */ 2579 2580void 2581restore_loop(void) 2582{ 2583 looped = 1; 2584 if (new_style_driver) { 2585 set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) | SC_LOOP_TRAFFIC); 2586 return; 2587 } 2588 if (ppp_fd != slave_fd) { 2589 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc); 2590 set_ppp_fd(slave_fd); 2591 } 2592} 2593 2594/******************************************************************** 2595 * 2596 * sifnpmode - Set the mode for handling packets for a given NP. 2597 */ 2598 2599int 2600sifnpmode(u, proto, mode) 2601 int u; 2602 int proto; 2603 enum NPmode mode; 2604{ 2605 struct npioctl npi; 2606 2607 npi.protocol = proto; 2608 npi.mode = mode; 2609 if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) { 2610 if (! ok_error (errno)) 2611 error("ioctl(PPPIOCSNPMODE, %d, %d): %m (%d)", 2612 proto, mode, errno); 2613 return 0; 2614 } 2615 return 1; 2616} 2617 2618 2619/******************************************************************** 2620 * 2621 * sipxfaddr - Config the interface IPX networknumber 2622 */ 2623 2624int sipxfaddr (int unit, unsigned long int network, unsigned char * node ) 2625{ 2626 int result = 1; 2627 2628#ifdef IPX_CHANGE 2629 int skfd; 2630 struct ifreq ifr; 2631 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr; 2632 2633 skfd = socket (AF_IPX, SOCK_DGRAM, 0); 2634 if (skfd < 0) { 2635 if (! ok_error (errno)) 2636 dbglog("socket(AF_IPX): %m (%d)", errno); 2637 result = 0; 2638 } 2639 else { 2640 memset (&ifr, '\0', sizeof (ifr)); 2641 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 2642 2643 memcpy (sipx->sipx_node, node, IPX_NODE_LEN); 2644 sipx->sipx_family = AF_IPX; 2645 sipx->sipx_port = 0; 2646 sipx->sipx_network = htonl (network); 2647 sipx->sipx_type = IPX_FRAME_ETHERII; 2648 sipx->sipx_action = IPX_CRTITF; 2649/* 2650 * Set the IPX device 2651 */ 2652 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) { 2653 result = 0; 2654 if (errno != EEXIST) { 2655 if (! ok_error (errno)) 2656 dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (%d)", errno); 2657 } 2658 else { 2659 warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists"); 2660 } 2661 } 2662 close (skfd); 2663 } 2664#endif 2665 return result; 2666} 2667 2668/******************************************************************** 2669 * 2670 * cipxfaddr - Clear the information for the IPX network. The IPX routes 2671 * are removed and the device is no longer able to pass IPX 2672 * frames. 2673 */ 2674 2675int cipxfaddr (int unit) 2676{ 2677 int result = 1; 2678 2679#ifdef IPX_CHANGE 2680 int skfd; 2681 struct ifreq ifr; 2682 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr; 2683 2684 skfd = socket (AF_IPX, SOCK_DGRAM, 0); 2685 if (skfd < 0) { 2686 if (! ok_error (errno)) 2687 dbglog("socket(AF_IPX): %m (%d)", errno); 2688 result = 0; 2689 } 2690 else { 2691 memset (&ifr, '\0', sizeof (ifr)); 2692 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 2693 2694 sipx->sipx_type = IPX_FRAME_ETHERII; 2695 sipx->sipx_action = IPX_DLTITF; 2696 sipx->sipx_family = AF_IPX; 2697/* 2698 * Set the IPX device 2699 */ 2700 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) { 2701 if (! ok_error (errno)) 2702 info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (%d)", errno); 2703 result = 0; 2704 } 2705 close (skfd); 2706 } 2707#endif 2708 return result; 2709} 2710 2711/* 2712 * Use the hostname as part of the random number seed. 2713 */ 2714int 2715get_host_seed() 2716{ 2717 int h; 2718 char *p = hostname; 2719 2720 h = 407; 2721 for (p = hostname; *p != 0; ++p) 2722 h = h * 37 + *p; 2723 return h; 2724} 2725 2726/******************************************************************** 2727 * 2728 * sys_check_options - check the options that the user specified 2729 */ 2730 2731int 2732sys_check_options(void) 2733{ 2734#ifdef IPX_CHANGE 2735/* 2736 * Disable the IPX protocol if the support is not present in the kernel. 2737 */ 2738 char *path; 2739 2740 if (ipxcp_protent.enabled_flag) { 2741 struct stat stat_buf; 2742 if ((path = path_to_procfs("/net/ipx_interface")) == 0 2743 || lstat(path, &stat_buf) < 0) { 2744 error("IPX support is not present in the kernel\n"); 2745 ipxcp_protent.enabled_flag = 0; 2746 } 2747 } 2748#endif 2749 if (demand && driver_is_old) { 2750 option_error("demand dialling is not supported by kernel driver " 2751 "version %d.%d.%d", driver_version, driver_modification, 2752 driver_patch); 2753 return 0; 2754 } 2755 if (multilink && !new_style_driver) { 2756 warn("Warning: multilink is not supported by the kernel driver"); 2757 multilink = 0; 2758 } 2759 return 1; 2760} 2761