X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fsys-linux.c;h=af5164fe3e4be1cef3aab5b30a85d8561cdb4af6;hp=425662dc14061a83d3a37eb2dca8ca1b84f15581;hb=892e254c066564ec858769c5eebb02cb21b7c9fe;hpb=23a5120d14444eea0a5c66066aa57c36f636b534 diff --git a/pppd/sys-linux.c b/pppd/sys-linux.c index 425662d..af5164f 100644 --- a/pppd/sys-linux.c +++ b/pppd/sys-linux.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -36,6 +38,8 @@ #include #include #include +#include +#include /* This is in netdevice.h. However, this compile will fail miserably if you attempt to include netdevice.h because it has so many references @@ -46,20 +50,46 @@ #define MAX_ADDR_LEN 7 #endif +#if __GLIBC__ >= 2 +#include /* glibc 2 conflicts with linux/types.h */ #include -#include #include -#include #include +#include +#else +#include +#include +#include +#include #include +#endif #include +#include + +#include +#include #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 +#else +#include +#endif +#endif /* IPX_CHANGE */ + +#ifdef LOCKLIB +#include +#endif + +#ifndef RTF_DEFAULT /* Normally in from */ +#define RTF_DEFAULT 0 #endif #define ok_error(num) ((num)==EIO) @@ -76,6 +106,7 @@ 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 */ @@ -88,6 +119,10 @@ static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */ static char *lock_file; +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) @@ -124,22 +159,27 @@ extern u_char inpacket_buf[]; /* borrowed from main.c */ */ extern int hungup; -#define still_ppp() (hungup == 0) #ifndef LOCK_PREFIX #define LOCK_PREFIX "/var/lock/LCK.." #endif -/******************************************************************** - * - * Functions to read and set the flags value in the device driver - */ - static void set_ppp_fd (int new_fd) - { - SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd)); - ppp_fd = new_fd; - } +{ + SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd)); + ppp_fd = new_fd; +} + +static int still_ppp(void) +{ + if (!hungup || ppp_fd == slave_fd) + return 1; + if (slave_fd >= 0) { + set_ppp_fd(slave_fd); + return 1; + } + return 0; +} /******************************************************************** * @@ -190,6 +230,8 @@ static void set_flags (int flags) void sys_init(void) { + int osmaj, osmin, ospatch; + openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); if (debug) @@ -207,6 +249,11 @@ void sys_init(void) die(1); } } + + uname(&utsname); + osmaj = osmin = ospatch = 0; + sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch); + kernel_version = KVERSION(osmaj, osmin, ospatch); } /******************************************************************** @@ -218,7 +265,6 @@ void sys_init(void) void sys_cleanup(void) { - struct ifreq ifr; /* * Take down the device */ @@ -231,7 +277,7 @@ void sys_cleanup(void) */ if (default_route_gateway != 0) { - cifdefaultroute(0, default_route_gateway); + cifdefaultroute(0, 0, default_route_gateway); } if (has_proxy_arp) @@ -317,11 +363,8 @@ void establish_ppp (int tty_fd) */ if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) { - if ( ! ok_error ( errno )) - { - syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno); - die(1); - } + syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno); + die(1); } /* * Set the current tty to the PPP discpline @@ -356,14 +399,14 @@ void establish_ppp (int tty_fd) ifunit, x); die(1); } - ioctl(slave_fd, TIOCSETD, &tty_disc); } ifunit = x; /* * Enable debug in the driver if requested. */ - set_kdebugflag (kdebugflag); + if (!demand) + set_kdebugflag (kdebugflag); set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)); @@ -394,25 +437,15 @@ void establish_ppp (int tty_fd) void disestablish_ppp(int tty_fd) { - int x; - char *s; -/* - * Do nothing if the PPP device is controlled by the loopback device - */ - if (tty_fd != ppp_fd) - { - return; - } /* * Attempt to restore the previous tty settings */ - if (still_ppp()) + if (!hungup) { - set_kdebugflag (0); /* * Restore the previous line discipline */ - if (ioctl(ppp_fd, TIOCSETD, &tty_disc) < 0) + if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) { if ( ! ok_error (errno)) { @@ -420,7 +453,7 @@ void disestablish_ppp(int tty_fd) } } - if (ioctl(ppp_fd, TIOCNXCL, 0) < 0) + if (ioctl(tty_fd, TIOCNXCL, 0) < 0) { if ( ! ok_error (errno)) { @@ -429,7 +462,7 @@ void disestablish_ppp(int tty_fd) } /* Reset non-blocking mode on fd. */ - if (initfdflags != -1 && fcntl(ppp_fd, F_SETFL, initfdflags) < 0) + if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) { if ( ! ok_error (errno)) { @@ -458,10 +491,6 @@ void clean_check(void) s = NULL; switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) { - case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP: - s = "nothing was received"; - break; - case SC_RCV_B7_0: case SC_RCV_B7_0 | SC_RCV_EVNP: case SC_RCV_B7_0 | SC_RCV_ODDP: @@ -568,6 +597,12 @@ struct speed { #endif #ifdef EXTB { 38400, EXTB }, +#endif +#ifdef B230400 + { 230400, B230400 }, +#endif +#ifdef B460800 + { 460800, B460800 }, #endif { 0, 0 } }; @@ -624,11 +659,12 @@ static int baud_rate_of (int speed) * regardless of whether the modem option was specified. */ -void set_up_tty (int tty_fd, int local) +void set_up_tty(int tty_fd, int local) { - int speed, x; + int speed; struct termios tios; - + + setdtr(tty_fd, 1); if (tcgetattr(tty_fd, &tios) < 0) { syslog(LOG_ERR, "tcgetattr: %m(%d)", errno); @@ -705,6 +741,14 @@ void set_up_tty (int tty_fd, int local) restore_term = TRUE; } +/* + * hangup_modem - hang up the modem by clearing DTR. + */ +void hangup_modem(int ttyfd) +{ + setdtr(ttyfd, 0); +} + /******************************************************************** * * setdtr - control the DTR line on the serial port. @@ -758,20 +802,19 @@ void output (int unit, unsigned char *p, int len) { if (debug) { - log_packet(p, len, "sent "); + log_packet(p, len, "sent ", LOG_DEBUG); } if (write(ppp_fd, p, len) < 0) { if (errno == EWOULDBLOCK || errno == ENOBUFS - || errno == ENXIO || errno == EIO) + || errno == ENXIO || errno == EIO || errno == EINTR) { - syslog(LOG_WARNING, "write: warning: %m(%d)", errno); + syslog(LOG_WARNING, "write: warning: %m (%d)", errno); } else { - syslog(LOG_ERR, "write: %m(%d)", errno); - die(1); + syslog(LOG_ERR, "write: %m (%d)", errno); } } } @@ -1061,18 +1104,21 @@ get_idle_time(u, ip) */ int ccp_fatal_error (int unit) - { +{ int x = get_flags(); return x & SC_DC_FERROR; - } +} /* * path_to_route - determine the path to the proc file system data */ - +#define ROUTE_MAX_COLS 12 FILE *route_fd = (FILE *) 0; static char route_buffer [512]; +static int route_dev_col, route_dest_col, route_gw_col; +static int route_flags_col, route_mask_col; +static int route_num_cols; static char *path_to_route (void); static int open_route_table (void); @@ -1085,33 +1131,31 @@ static int read_route_table (struct rtentry *rt); */ static int path_to_procfs (void) - { +{ struct mntent *mntent; FILE *fp; - fp = fopen (MOUNTED, "r"); - if (fp != 0) - { - mntent = getmntent (fp); - while (mntent != (struct mntent *) 0) - { - if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0) - { - if (strcmp (mntent->mnt_type, "proc") == 0) - { - strncpy (route_buffer, mntent->mnt_dir, - sizeof (route_buffer)-10); - route_buffer [sizeof (route_buffer)-10] = '\0'; - fclose (fp); - return 1; - } - } - mntent = getmntent (fp); - } - fclose (fp); - } - return 0; - } + fp = fopen(MOUNTED, "r"); + if (fp == NULL) { + /* Default the mount location of /proc */ + strncpy (route_buffer, "/proc", sizeof (route_buffer)-10); + return 1; + } + + while ((mntent = getmntent(fp)) != NULL) { + if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0) + continue; + if (strcmp(mntent->mnt_type, "proc") == 0) + break; + } + fclose (fp); + if (mntent == 0) + return 0; + + strncpy(route_buffer, mntent->mnt_dir, sizeof (route_buffer)-10); + route_buffer [sizeof (route_buffer)-10] = '\0'; + return 1; +} /******************************************************************** * @@ -1119,15 +1163,14 @@ static int path_to_procfs (void) */ static char *path_to_route (void) - { - if (! path_to_procfs()) - { +{ + if (!path_to_procfs()) { syslog (LOG_ERR, "proc file system not mounted"); return 0; - } + } strcat (route_buffer, "/net/route"); return (route_buffer); - } +} /******************************************************************** * @@ -1135,89 +1178,107 @@ static char *path_to_route (void) */ static void close_route_table (void) - { - if (route_fd != (FILE *) 0) - { +{ + if (route_fd != (FILE *) 0) { fclose (route_fd); route_fd = (FILE *) 0; - } - } + } +} /******************************************************************** * * open_route_table - open the interface to the route table */ +static char route_delims[] = " \t\n"; static int open_route_table (void) - { +{ char *path; close_route_table(); path = path_to_route(); if (path == NULL) - { return 0; - } route_fd = fopen (path, "r"); - if (route_fd == (FILE *) 0) - { - syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno); + if (route_fd == NULL) { + syslog (LOG_ERR, "can't open %s: %m (%d)", path, errno); return 0; - } + } + + route_dev_col = 0; /* default to usual columns */ + route_dest_col = 1; + route_gw_col = 2; + route_flags_col = 3; + route_mask_col = 7; + route_num_cols = 8; + + /* parse header line */ + if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) { + char *p = route_buffer, *q; + int col; + for (col = 0; col < ROUTE_MAX_COLS; ++col) { + int used = 1; + if ((q = strtok(p, route_delims)) == 0) + break; + if (strcasecmp(q, "iface") == 0) + route_dev_col = col; + else if (strcasecmp(q, "destination") == 0) + route_dest_col = col; + else if (strcasecmp(q, "gateway") == 0) + route_gw_col = col; + else if (strcasecmp(q, "flags") == 0) + route_flags_col = col; + else if (strcasecmp(q, "mask") == 0) + route_mask_col = col; + else + used = 0; + if (used && col >= route_num_cols) + route_num_cols = col + 1; + p = NULL; + } + } + return 1; - } +} /******************************************************************** * * read_route_table - read the next entry from the route table */ -static int read_route_table (struct rtentry *rt) - { - static char delims[] = " \t\n"; - char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr; +static int read_route_table(struct rtentry *rt) +{ + char *cols[ROUTE_MAX_COLS], *p; + int col; memset (rt, '\0', sizeof (struct rtentry)); - for (;;) - { - if (fgets (route_buffer, sizeof (route_buffer), route_fd) == - (char *) 0) - { - return 0; - } + if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) + return 0; - dev_ptr = strtok (route_buffer, delims); /* interface name */ - dst_ptr = strtok (NULL, delims); /* destination address */ - gw_ptr = strtok (NULL, delims); /* gateway */ - flag_ptr = strtok (NULL, delims); /* flags */ - - if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */ - { - return 0; - } - - /* Discard that stupid header line which should never - * have been there in the first place !! */ - if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr)) - { - break; - } - } + p = route_buffer; + for (col = 0; col < route_num_cols; ++col) { + cols[col] = strtok(p, route_delims); + if (cols[col] == NULL) + return 0; /* didn't get enough columns */ + } ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr = - strtoul (dst_ptr, NULL, 16); + strtoul(cols[route_dest_col], NULL, 16); ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr = - strtoul (gw_ptr, NULL, 16); + strtoul(cols[route_gw_col], NULL, 16); - rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16); - rt->rt_dev = dev_ptr; + ((struct sockaddr_in *) &rt->rt_genmask)->sin_addr.s_addr = + strtoul(cols[route_mask_col], NULL, 16); + + rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16); + rt->rt_dev = cols[route_dev_col]; return 1; - } +} /******************************************************************** * @@ -1225,52 +1286,71 @@ static int read_route_table (struct rtentry *rt) */ static int defaultroute_exists (struct rtentry *rt) - { - int result = 0; +{ + int result = 0; if (!open_route_table()) - { return 0; - } - while (read_route_table(rt) != 0) - { + while (read_route_table(rt) != 0) { if ((rt->rt_flags & RTF_UP) == 0) - { continue; - } - if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L) - { + if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L) { result = 1; break; - } - } + } + } close_route_table(); return result; - } +} + +/* + * have_route_to - determine if the system has any route to + * a given IP address. + */ +int have_route_to(u_int32_t addr) +{ + struct rtentry rt; + int result = 0; + + if (!open_route_table()) + return -1; /* don't know */ + + while (read_route_table(&rt)) { + if ((rt.rt_flags & RTF_UP) == 0) + continue; + if ((addr & ((struct sockaddr_in *)&rt.rt_genmask)->sin_addr.s_addr) + == ((struct sockaddr_in *)&rt.rt_genmask)->sin_addr.s_addr) { + result = 1; + break; + } + } + + close_route_table(); + return result; +} /******************************************************************** * * sifdefaultroute - assign a default route through the address given. */ -int sifdefaultroute (int unit, u_int32_t gateway) +int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway) { struct rtentry rt; - if (defaultroute_exists(&rt)) + if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) { - u_int32_t old_gateway = ((struct sockaddr_in *) (&rt.rt_gateway))-> - sin_addr.s_addr; + struct in_addr old_gateway = + ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr; - if (old_gateway != gateway) + if (old_gateway.s_addr != gateway) { syslog (LOG_ERR, - "ppp not replacing existing default route to %s[%s]", - rt.rt_dev, - inet_ntoa (old_gateway)); + "not replacing existing default route to %s [%s]", + rt.rt_dev, inet_ntoa (old_gateway)); } return 0; } @@ -1278,9 +1358,15 @@ int sifdefaultroute (int unit, u_int32_t gateway) memset (&rt, '\0', sizeof (rt)); SET_SA_FAMILY (rt.rt_dst, AF_INET); SET_SA_FAMILY (rt.rt_gateway, AF_INET); + + if (kernel_version > KVERSION(2,1,0)) { + SET_SA_FAMILY (rt.rt_genmask, AF_INET); + ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L; + } + ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway; - rt.rt_flags = RTF_UP | RTF_GATEWAY; + rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT; if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) { if ( ! ok_error ( errno )) @@ -1299,7 +1385,7 @@ int sifdefaultroute (int unit, u_int32_t gateway) * cifdefaultroute - delete a default route through the address given. */ -int cifdefaultroute (int unit, u_int32_t gateway) +int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway) { struct rtentry rt; @@ -1308,9 +1394,15 @@ int cifdefaultroute (int unit, u_int32_t gateway) memset (&rt, '\0', sizeof (rt)); SET_SA_FAMILY (rt.rt_dst, AF_INET); SET_SA_FAMILY (rt.rt_gateway, AF_INET); + + if (kernel_version > KVERSION(2,1,0)) { + SET_SA_FAMILY (rt.rt_genmask, AF_INET); + ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L; + } + ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway; - rt.rt_flags = RTF_UP | RTF_GATEWAY; + rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT; if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) { if (still_ppp()) @@ -1381,8 +1473,9 @@ int cifproxyarp (int unit, u_int32_t his_adr) { memset (&arpreq, '\0', sizeof(arpreq)); SET_SA_FAMILY(arpreq.arp_pa, AF_INET); - ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr; + arpreq.arp_flags = ATF_PERM | ATF_PUBL; + if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) { if ( ! ok_error ( errno )) @@ -1406,10 +1499,8 @@ static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr, char *name) { - struct ifreq *ifr, *ifend, *ifp; - int i; + struct ifreq *ifr, *ifend; u_int32_t ina, mask; - struct sockaddr_dl *dla; struct ifreq ifreq; struct ifconf ifc; struct ifreq ifs[MAX_IFS]; @@ -1632,7 +1723,7 @@ static void decode_version (char *buf, int *version, /******************************************************************** * - * Procedure to determine if the PPP line dicipline is registered. + * Procedure to determine if the PPP line discipline is registered. */ int @@ -1695,7 +1786,7 @@ ppp_registered(void) */ int ppp_available(void) - { +{ int s, ok; struct ifreq ifr; int size; @@ -1706,9 +1797,9 @@ int ppp_available(void) */ s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) - { + { return 0; - } + } strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name)); ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0; @@ -1718,98 +1809,99 @@ int ppp_available(void) * flags for the device again. */ if (!ok) - { + { if (ppp_registered()) - { + { strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name)); ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0; - } - } + } + } /* * Ensure that the hardware address is for PPP and not something else */ if (ok) - { + { ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0; - } + } if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP)) - { + { ok = 0; - } + } if (!ok) - { + { no_ppp_msg = - "This system lacks kernel support for PPP. To include PPP support\n" - "in the kernel, please follow the steps detailed in the " - "README.linux\nfile in the ppp-2.3 distribution.\n"; - } + "This system lacks kernel support for PPP. This could be because\n" + "the PPP kernel module is not loaded, or because the kernel is\n" + "not configured for PPP. See the README.linux file in the\n" + "ppp-2.3.6 distribution.\n"; + } /* * This is the PPP device. Validate the version of the driver at this * point to ensure that this program will work with the driver. */ else - { + { char abBuffer [1024]; ifr.ifr_data = abBuffer; size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr); - ok = size >= 0; + if (size < 0) { + syslog(LOG_ERR, "Couldn't read driver version: %m"); + ok = 0; + no_ppp_msg = "Sorry, couldn't verify kernel driver version\n"; - if (ok) - { - decode_version (abBuffer, - &driver_version, - &driver_modification, - &driver_patch); - } - - if (!ok) - { - driver_version = - driver_modification = - driver_patch = 0; - } + } else { + decode_version(abBuffer, + &driver_version, + &driver_modification, + &driver_patch); /* * Validate the version of the driver against the version that we used. */ - decode_version (PPP_VERSION, - &my_version, - &my_modification, - &my_patch); + decode_version(VERSION, + &my_version, + &my_modification, + &my_patch); - /* The version numbers must match */ - if (driver_version != my_version) - { - ok = 0; - } + /* The version numbers must match */ + if (driver_version != my_version) + { + ok = 0; + } - /* The modification levels must be legal */ - if (driver_modification < my_modification) - { - ok = 0; - } - - close (s); - if (!ok) - { - sprintf (route_buffer, - "Sorry - PPP driver version %d.%d.%d is out of date\n", - driver_version, driver_modification, driver_patch); - - no_ppp_msg = route_buffer; - } - } + /* The modification levels must be legal */ + if (driver_modification < 3) + { + if (driver_modification >= 2) { + /* we can cope with 2.2.0 and above */ + driver_is_old = 1; + } else { + ok = 0; + } + } + + close (s); + if (!ok) + { + sprintf (route_buffer, + "Sorry - PPP driver version %d.%d.%d is out of date\n", + driver_version, driver_modification, driver_patch); + + no_ppp_msg = route_buffer; + } + } + } return ok; - } +} /******************************************************************** * * Update the wtmp file with the appropriate user name and tty device. */ -int logwtmp (char *line, char *name, char *host) +void logwtmp (const char *line, const char *name, const char *host) { int wtmp; struct utmp ut, *utp; @@ -1897,6 +1989,35 @@ int logwtmp (char *line, char *name, char *host) int lock (char *dev) { +#ifdef LOCKLIB + int result; + lock_file = malloc(strlen(dev) + 1); + if (lock_file == NULL) + { + novm("lock file name"); + } + strcpy (lock_file, dev); + result = mklock (dev, (void *) 0); + + if (result > 0) + { + syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result); + free (lock_file); + lock_file = NULL; + result = -1; + } + else + { + if (result < 0) + { + syslog (LOG_ERR, "Can't create lock file %s", lock_file); + free (lock_file); + lock_file = NULL; + result = -1; + } + } + return (result); +#else char hdb_lock_buffer[12]; int fd, n; int pid = getpid(); @@ -1967,7 +2088,8 @@ int lock (char *dev) #else pid = ((int *) hdb_lock_buffer)[0]; #endif - if (pid == 0 || (kill(pid, 0) == -1 && errno == ESRCH)) + if (pid == 0 || pid == getpid() + || (kill(pid, 0) == -1 && errno == ESRCH)) { n = 0; } @@ -1992,6 +2114,7 @@ int lock (char *dev) free(lock_file); lock_file = NULL; return -1; +#endif } @@ -2004,7 +2127,11 @@ void unlock(void) { if (lock_file) { +#ifdef LOCKLIB + (void) rmlock (lock_file, (void *) 0); +#else unlink(lock_file); +#endif free(lock_file); lock_file = NULL; } @@ -2025,7 +2152,7 @@ int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid) { if (! ok_error (errno)) { - syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno); + syslog (LOG_ERR, "ioctl(PPPIOCSMAXCID): %m(%d)", errno); } vjcomp = 0; } @@ -2157,8 +2284,11 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr, return (0); } /* - * Set the netmask + * Set the netmask. + * For recent kernels, force the netmask to 255.255.255.255. */ + if (kernel_version >= KVERSION(2,1,16)) + net_mask = ~0L; if (net_mask != 0) { ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask; @@ -2174,22 +2304,29 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr, /* * Add the device route */ - SET_SA_FAMILY (rt.rt_dst, AF_INET); - SET_SA_FAMILY (rt.rt_gateway, AF_INET); - rt.rt_dev = ifname; /* MJC */ + if (kernel_version < KVERSION(2,1,16)) { + SET_SA_FAMILY (rt.rt_dst, AF_INET); + SET_SA_FAMILY (rt.rt_gateway, AF_INET); + rt.rt_dev = ifname; - ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L; - ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr; - rt.rt_flags = RTF_UP | RTF_HOST; + ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L; + ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr; + rt.rt_flags = RTF_UP | RTF_HOST; - if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) - { - if (! ok_error (errno)) - { - syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno); - } - return (0); + if (kernel_version > KVERSION(2,1,0)) { + SET_SA_FAMILY (rt.rt_genmask, AF_INET); + ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L; } + + if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) + { + if (! ok_error (errno)) + { + syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno); + } + return (0); + } + } return 1; } @@ -2202,27 +2339,35 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr, int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr) { struct rtentry rt; + + if (kernel_version < KVERSION(2,1,16)) { /* * Delete the route through the device */ - memset (&rt, '\0', sizeof (rt)); + memset (&rt, '\0', sizeof (rt)); - SET_SA_FAMILY (rt.rt_dst, AF_INET); - SET_SA_FAMILY (rt.rt_gateway, AF_INET); - rt.rt_dev = ifname; /* MJC */ + SET_SA_FAMILY (rt.rt_dst, AF_INET); + SET_SA_FAMILY (rt.rt_gateway, AF_INET); + rt.rt_dev = ifname; - ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0; - ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr; - rt.rt_flags = RTF_UP | RTF_HOST; + ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0; + ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr; + rt.rt_flags = RTF_UP | RTF_HOST; - if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) - { - if (still_ppp() && ! ok_error (errno)) - { - syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno); - } - return (0); + if (kernel_version > KVERSION(2,1,0)) { + SET_SA_FAMILY (rt.rt_genmask, AF_INET); + ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L; } + + if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) + { + if (still_ppp() && ! ok_error (errno)) + { + syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno); + } + return (0); + } + } return 1; } @@ -2235,15 +2380,27 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr) void open_ppp_loopback(void) { - int flags; + int flags, i; struct termios tios; - if (openpty (&master_fd, &slave_fd, loop_name, NULL, NULL) < 0) - { - syslog(LOG_ERR, "No free pty for loopback"); - die(1); - } + master_fd = -1; + for (i = 0; i < 64; ++i) { + sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16); + master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0); + if (master_fd >= 0) + break; + } + if (master_fd < 0) { + syslog(LOG_ERR, "No free pty for loopback"); + die(1); + } SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name)); + loop_name[5] = 't'; + slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0); + if (slave_fd < 0) { + syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name); + die(1); + } set_ppp_fd(slave_fd); @@ -2297,81 +2454,23 @@ open_ppp_loopback(void) * * restore_loop - reattach the ppp unit to the loopback. * - * The problem with the Linux variant is that the POSIX tty drivers will - * sieze the line when it is disconnected. In addition, when the device - * goes down all of the routes are deleted. This means that the tty needs - * to be re-opened, reconfigured, and the device reconfigured and the routes - * restored. + * The kernel ppp driver automatically reattaches the ppp unit to + * the loopback if the serial port is set to a line discipline other + * than ppp, or if it detects a modem hangup. The former will happen + * in disestablish_ppp if the latter hasn't already happened, so we + * shouldn't need to do anything. + * + * Just to be sure, set the real serial port to the normal discipline. */ void restore_loop(void) { - int x; - int fdflags; - char fname [30]; -/* - * Take down the existing interface - */ - sifdown (0); - (void) ioctl(ppp_fd, TIOCSETD, &tty_disc); -/* - * Find the existing flags. This works even if the tty has stolen the - * line discipline. - */ - fdflags = fcntl(ppp_fd, F_GETFL); - if (fdflags < 0) - { - syslog (LOG_ERR, "retrieve file flags failed: %m(%d)", errno); - fdflags = O_NONBLOCK | O_RDWR; - } -/* - * Re-open the file so the we can re-establish the previous discipline - */ - sprintf (fname, "/proc/self/fd/%d", ppp_fd); - x = open (fname, O_RDWR | O_NONBLOCK, 0); - if (x < 0) - { - syslog (LOG_ERR, "reopen of tty file failed: %m(%d)", errno); - } -/* - * Transfer the newly opened file (to the same tty) back to the tty - * file handle. - */ - else - { - dup2 (x, ppp_fd); - close (x); - fcntl (ppp_fd, F_SETFL, fdflags); - set_up_tty(ppp_fd, 0); - } -/* - * Switch to the tty slave and put that into the PPP discipline. - */ - set_ppp_fd(slave_fd); - - if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0) + if (ppp_fd != slave_fd) { - syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno); - die(1); + (void) ioctl(ppp_fd, TIOCSETD, &tty_disc); + set_ppp_fd(slave_fd); } -/* - * Fetch the current unit identifier. - */ - if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) - { - syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno); - die(1); - } -/* - * Restore the parameters for the PPP link. - */ - ppp_send_config(0, PPP_MRU, (u_int32_t) 0, 0, 0); - ppp_recv_config(0, PPP_MRU, (u_int32_t) 0, 0, 0); -/* - * Reconfigure the IP addresses for the demand dial system. - */ - (void) (ipcp_protent.demand_conf) (0); } /******************************************************************** @@ -2395,6 +2494,7 @@ sifnpmode(u, proto, mode) { syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)", proto, mode, errno); + syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd); } return 0; } @@ -2402,8 +2502,6 @@ sifnpmode(u, proto, mode) } -#include - /******************************************************************** * * sipxfaddr - Config the interface IPX networknumber @@ -2411,13 +2509,13 @@ sifnpmode(u, proto, mode) int sipxfaddr (int unit, unsigned long int network, unsigned char * node ) { - int skfd; int result = 1; - struct sockaddr_ipx ipx_addr; + +#ifdef IPX_CHANGE + int skfd; struct ifreq ifr; struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr; -#ifdef IPX_CHANGE skfd = socket (AF_IPX, SOCK_DGRAM, 0); if (skfd < 0) { @@ -2473,13 +2571,13 @@ int sipxfaddr (int unit, unsigned long int network, unsigned char * node ) int cipxfaddr (int unit) { - int skfd; int result = 1; - struct sockaddr_ipx ipx_addr; + +#ifdef IPX_CHANGE + int skfd; struct ifreq ifr; struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr; -#ifdef IPX_CHANGE skfd = socket (AF_IPX, SOCK_DGRAM, 0); if (skfd < 0) { @@ -2515,12 +2613,51 @@ int cipxfaddr (int unit) return result; } +/* + * daemon - Detach us from controlling terminal session. + */ +int +daemon(nochdir, noclose) + int nochdir, noclose; +{ + int pid; + + if ((pid = fork()) < 0) + return -1; + if (pid != 0) + exit(0); /* parent dies */ + setsid(); + if (!nochdir) + chdir("/"); + if (!noclose) { + fclose(stdin); /* don't need stdin, stdout, stderr */ + fclose(stdout); + fclose(stderr); + } + return 0; +} + +/* + * Use the hostname as part of the random number seed. + */ +int +get_host_seed() +{ + int h; + char *p = hostname; + + h = 407; + for (p = hostname; *p != 0; ++p) + h = h * 37 + *p; + return h; +} + /******************************************************************** * * sys_check_options - check the options that the user specified */ -void +int sys_check_options(void) { #ifdef IPX_CHANGE @@ -2545,4 +2682,11 @@ sys_check_options(void) break; } #endif + if (demand && driver_is_old) { + option_error("demand dialling is not supported by kernel driver version " + "%d.%d.%d", driver_version, driver_modification, + driver_patch); + return 0; + } + return 1; }