?? sys-linux.c
字號:
/* * sys-linux.c - System-dependent procedures for setting up * PPP interfaces on Linux systems * * Copyright (c) 1989 Carnegie Mellon University. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by Carnegie Mellon University. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/time.h>#include <sys/errno.h>#include <sys/file.h>#include <sys/stat.h>#include <sys/utsname.h>#include <sys/sysmacros.h>#include <stdio.h>#include <stdlib.h>#include <syslog.h>#include <string.h>#include <time.h>#include <memory.h>#include <utmp.h>#include <mntent.h>#include <signal.h>#include <fcntl.h>#include <ctype.h>#include <termios.h>#include <unistd.h>/* This is in netdevice.h. However, this compile will fail miserably if you attempt to include netdevice.h because it has so many references to __memcpy functions which it should not attempt to do. So, since I really don't use it, but it must be defined, define it now. */#ifndef MAX_ADDR_LEN#define MAX_ADDR_LEN 7#endif#if __GLIBC__ >= 2#include <asm/types.h> /* glibc 2 conflicts with linux/types.h */#include <net/if.h>#include <net/if_arp.h>#include <net/route.h>#include <netinet/if_ether.h>#else#include <linux/types.h>#include <linux/if.h>#include <linux/if_arp.h>#include <linux/route.h>#include <linux/if_ether.h>#endif#include <netinet/in.h>#include <arpa/inet.h>#include <linux/ppp_defs.h>#include <linux/if_ppp.h>#include "pppd.h"#include "fsm.h"#include "ipcp.h"#include "patchlevel.h"#ifdef IPX_CHANGE#include "ipxcp.h"#if __GLIBC__ >= 2 && \ !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)#include <netipx/ipx.h>#else#include <linux/ipx.h>#endif#endif /* IPX_CHANGE */#ifdef LOCKLIB#include <sys/locks.h>#endif#ifdef INET6#ifndef _LINUX_IN6_H/* * This is in linux/include/net/ipv6.h. */struct in6_ifreq { struct in6_addr ifr6_addr; __u32 ifr6_prefixlen; unsigned int ifr6_ifindex;};#endif#define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \ memset(&sin6.s6_addr, 0, sizeof(struct in6_addr)); \ sin6.s6_addr16[0] = htons(0xfe80); \ eui64_copy(eui64, sin6.s6_addr32[2]); \ } while (0)#endif /* INET6 *//* We can get an EIO error on an ioctl if the modem has hung up */#define ok_error(num) ((num)==EIO)static int tty_disc = N_TTY; /* The TTY discipline */static int ppp_disc = N_PPP; /* The PPP discpline */static int initfdflags = -1; /* Initial file descriptor flags for fd */static int ppp_fd = -1; /* fd which is set to PPP discipline */static int sock_fd = -1; /* socket for doing interface ioctls */static int slave_fd = -1;static int master_fd = -1;#ifdef INET6static int sock6_fd = -1;#endif /* INET6 */static int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */static fd_set in_fds; /* set of fds that wait_input waits for */static int max_in_fd; /* highest fd set in in_fds */static int has_proxy_arp = 0;static int driver_version = 0;static int driver_modification = 0;static int driver_patch = 0;static int driver_is_old = 0;static int restore_term = 0; /* 1 => we've munged the terminal */static struct termios inittermios; /* Initial TTY termios */static int new_style_driver = 0;static char loop_name[20];static unsigned char inbuf[512]; /* buffer for chars read from loopback */static int if_is_up; /* Interface has been marked up */static u_int32_t default_route_gateway; /* Gateway for default route added */static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */static char proxy_arp_dev[16]; /* Device for proxy arp entry */static u_int32_t our_old_addr; /* for detecting address changes */static int dynaddr_set; /* 1 if ip_dynaddr set */static int looped; /* 1 if using loop */static struct utsname utsname; /* for the kernel version */static int kernel_version;#define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))#define MAX_IFS 100#define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)#define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \ IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)#define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)/* Prototypes for procedures local to this file. */static int get_flags (int fd);static void set_flags (int fd, int flags);static int translate_speed (int bps);static int baud_rate_of (int speed);static void close_route_table (void);static int open_route_table (void);static int read_route_table (struct rtentry *rt);static int defaultroute_exists (struct rtentry *rt);static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr, char *name, int namelen);static void decode_version (char *buf, int *version, int *mod, int *patch);static int set_kdebugflag(int level);static int ppp_registered(void);extern u_char inpacket_buf[]; /* borrowed from main.c *//* * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, * if it exists. */#define SET_SA_FAMILY(addr, family) \ memset ((char *) &(addr), '\0', sizeof(addr)); \ addr.sa_family = (family);/* * Determine if the PPP connection should still be present. */extern int hungup;/* new_fd is the fd of a tty */static void set_ppp_fd (int new_fd){ SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd)); ppp_fd = new_fd; if (!new_style_driver) ppp_dev_fd = new_fd;}static int still_ppp(void){ if (new_style_driver) return !hungup && ppp_fd >= 0; if (!hungup || ppp_fd == slave_fd) return 1; if (slave_fd >= 0) { set_ppp_fd(slave_fd); return 1; } return 0;}/******************************************************************** * * Functions to read and set the flags value in the device driver */static int get_flags (int fd){ int flags; if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) { if ( ok_error (errno) ) flags = 0; else fatal("ioctl(PPPIOCGFLAGS): %m"); } SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags)); return flags;}/********************************************************************/static void set_flags (int fd, int flags){ SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags)); if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0) { if (! ok_error (errno) ) fatal("ioctl(PPPIOCSFLAGS, %x): %m", flags, errno); }}/******************************************************************** * * sys_init - System-dependent initialization. */void sys_init(void){ int flags; openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); if (new_style_driver) { ppp_dev_fd = open("/dev/ppp", O_RDWR); if (ppp_dev_fd < 0) fatal("Couldn't open /dev/ppp: %m"); flags = fcntl(ppp_dev_fd, F_GETFL); if (flags == -1 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1) warn("Couldn't set /dev/ppp to nonblock: %m"); } /* Get an internet socket for doing socket ioctls. */ sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) fatal("Couldn't create IP socket: %m(%d)", errno);#ifdef INET6 sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0); if (sock6_fd < 0) fatal("Couldn't create IPv6 socket: %m(%d)", errno);#endif FD_ZERO(&in_fds); max_in_fd = 0;}/******************************************************************** * * sys_cleanup - restore any system state we modified before exiting: * mark the interface down, delete default route and/or proxy arp entry. * This shouldn't call die() because it's called from die(). */void sys_cleanup(void){/* * Take down the device */ if (if_is_up) { if_is_up = 0; sifdown(0); }/* * Delete any routes through the device. */ if (default_route_gateway != 0) cifdefaultroute(0, 0, default_route_gateway); if (has_proxy_arp) cifproxyarp(0, proxy_arp_addr);}/******************************************************************** * * sys_close - Clean up in a child process before execing. */voidsys_close(void){ if (new_style_driver) close(ppp_dev_fd); if (sock_fd >= 0) close(sock_fd); if (slave_fd >= 0) close(slave_fd); if (master_fd >= 0) close(master_fd); closelog();}/******************************************************************** * * set_kdebugflag - Define the debugging level for the kernel */static int set_kdebugflag (int requested_level){ if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) { if ( ! ok_error (errno) ) error("ioctl(PPPIOCSDEBUG): %m"); return (0); } SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d", requested_level)); return (1);}/******************************************************************** * * establish_ppp - Turn the serial port into a ppp interface. */int establish_ppp (int tty_fd){ int x;/* * The current PPP device will be the tty file. */ set_ppp_fd (tty_fd); if (new_style_driver) add_fd(tty_fd);/* * Ensure that the tty device is in exclusive mode. */ if (ioctl(tty_fd, TIOCEXCL, 0) < 0) { if ( ! ok_error ( errno )) warn("ioctl(TIOCEXCL): %m"); }/* * Demand mode - prime the old ppp device to relinquish the unit. */ if (!new_style_driver && looped && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) fatal("ioctl(transfer ppp unit): %m(%d)", errno);/* * Set the current tty to the PPP discpline */#ifndef N_SYNC_PPP#define N_SYNC_PPP 14#endif if (new_style_driver) ppp_disc = sync_serial ? N_SYNC_PPP:N_PPP; if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) { if ( ! ok_error (errno) ) fatal("ioctl(TIOCSETD): %m(%d)", errno); }/* * Find out which interface we were given. */ if (new_style_driver) { if (!looped) { /* allocate ourselves a ppp unit */ ifunit = -1; if (ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit) < 0) fatal("Couldn't create new ppp unit: %m"); set_kdebugflag(kdebugflag); } else { set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) & ~SC_LOOP_TRAFFIC); } if (ioctl(tty_fd, PPPIOCATTACH, &ifunit) < 0) { if (errno == EIO) return -1; fatal("Couldn't attach tty to PPP unit %d: %m", ifunit); } } else { if (ioctl(tty_fd, PPPIOCGUNIT, &x) < 0) { if ( ! ok_error (errno)) fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno); } /* Check that we got the same unit again. */ if (looped && x != ifunit) fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x); ifunit = x; }/* * Enable debug in the driver if requested. */ if (!looped) set_kdebugflag (kdebugflag); looped = 0; set_flags(tty_fd, get_flags(tty_fd) & ~(SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)); SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver", driver_version, driver_modification, driver_patch));/* * Fetch the initial file flags and reset blocking mode on the file. */ initfdflags = fcntl(tty_fd, F_GETFL); if (initfdflags == -1 || fcntl(tty_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) { if ( ! ok_error (errno)) warn("Couldn't set device to non-blocking mode: %m"); } return ppp_dev_fd;}/******************************************************************** * * disestablish_ppp - Restore the serial port to normal operation. * This shouldn't call die() because it's called from die(). */void disestablish_ppp(int tty_fd){ if (new_style_driver) remove_fd(tty_fd); if (!hungup) {/* * Flush the tty output buffer so that the TIOCSETD doesn't hang. */ if (tcflush(tty_fd, TCIOFLUSH) < 0) warn("tcflush failed: %m");/* * Restore the previous line discipline */ if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) { if ( ! ok_error (errno)) error("ioctl(TIOCSETD, N_TTY): %m"); } if (ioctl(tty_fd, TIOCNXCL, 0) < 0) { if ( ! ok_error (errno)) warn("ioctl(TIOCNXCL): %m(%d)", errno); } /* Reset non-blocking mode on fd. */ if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) { if ( ! ok_error (errno)) warn("Couldn't restore device fd flags: %m"); } } initfdflags = -1; if (new_style_driver && !looped) { if (ioctl(ppp_dev_fd, PPPIOCDETACH) < 0) { if (errno == ENOTTY) { /* first version of new driver didn't have PPPIOCDETACH */ int flags; close(ppp_dev_fd); ppp_dev_fd = open("/dev/ppp", O_RDWR); if (ppp_dev_fd < 0) fatal("Couldn't reopen /dev/ppp: %m"); flags = fcntl(ppp_dev_fd, F_GETFL); if (flags == -1 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1) warn("Couldn't set /dev/ppp to nonblock: %m"); } else error("Couldn't release PPP unit: %m"); } set_ppp_fd(-1); }}/********************************************************************
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -