2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
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.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
28 #include <sys/utsname.h>
44 /* This is in netdevice.h. However, this compile will fail miserably if
45 you attempt to include netdevice.h because it has so many references
46 to __memcpy functions which it should not attempt to do. So, since I
47 really don't use it, but it must be defined, define it now. */
50 #define MAX_ADDR_LEN 7
55 #include <net/if_arp.h>
56 #include <net/route.h>
57 #include <netinet/if_ether.h>
60 #include <linux/if_arp.h>
61 #include <linux/route.h>
62 #include <linux/if_ether.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
67 #include <linux/types.h>
68 #include <linux/ppp_defs.h>
69 #include <linux/if_ppp.h>
74 #include "patchlevel.h"
78 #include <linux/ipx.h>
79 #endif /* IPX_CHANGE */
82 #include <sys/locks.h>
85 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
89 #define ok_error(num) ((num)==EIO)
91 static int tty_disc = N_TTY; /* The TTY discipline */
92 static int ppp_disc = N_PPP; /* The PPP discpline */
93 static int initfdflags = -1; /* Initial file descriptor flags for fd */
94 static int ppp_fd = -1; /* fd which is set to PPP discipline */
95 static int sock_fd = -1; /* socket for doing interface ioctls */
96 static int slave_fd = -1;
97 static int master_fd = -1;
99 static int has_proxy_arp = 0;
100 static int driver_version = 0;
101 static int driver_modification = 0;
102 static int driver_patch = 0;
103 static int driver_is_old = 0;
104 static int restore_term = 0; /* 1 => we've munged the terminal */
105 static struct termios inittermios; /* Initial TTY termios */
107 static char loop_name[20];
108 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
110 static int if_is_up; /* Interface has been marked up */
111 static u_int32_t default_route_gateway; /* Gateway for default route added */
112 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
114 static char *lock_file;
116 static struct utsname utsname; /* for the kernel version */
120 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
121 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
122 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
124 /* Prototypes for procedures local to this file. */
125 static int get_flags (void);
126 static void set_flags (int flags);
127 static int translate_speed (int bps);
128 static int baud_rate_of (int speed);
129 static char *path_to_route (void);
130 static void close_route_table (void);
131 static int open_route_table (void);
132 static int read_route_table (struct rtentry *rt);
133 static int defaultroute_exists (struct rtentry *rt);
134 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
136 static void decode_version (char *buf, int *version, int *mod, int *patch);
138 extern u_char inpacket_buf[]; /* borrowed from main.c */
141 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
145 #define SET_SA_FAMILY(addr, family) \
146 memset ((char *) &(addr), '\0', sizeof(addr)); \
147 addr.sa_family = (family);
150 * Determine if the PPP connection should still be present.
156 #define LOCK_PREFIX "/var/lock/LCK.."
159 static void set_ppp_fd (int new_fd)
161 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
165 static int still_ppp(void)
167 if (!hungup || ppp_fd == slave_fd)
170 set_ppp_fd(slave_fd);
176 /********************************************************************
178 * Functions to read and set the flags value in the device driver
181 static int get_flags (void)
185 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
187 if ( ok_error (errno) )
193 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
198 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
202 /********************************************************************/
204 static void set_flags (int flags)
206 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
208 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
210 if (! ok_error (errno) )
212 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
218 /********************************************************************
220 * sys_init - System-dependent initialization.
225 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
226 setlogmask(LOG_UPTO(LOG_INFO));
229 setlogmask(LOG_UPTO(LOG_DEBUG));
232 /* Get an internet socket for doing socket ioctls. */
233 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
236 if ( ! ok_error ( errno ))
238 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
246 /********************************************************************
248 * sys_cleanup - restore any system state we modified before exiting:
249 * mark the interface down, delete default route and/or proxy arp entry.
250 * This should call die() because it's called from die().
253 void sys_cleanup(void)
256 * Take down the device
263 * Delete any routes through the device.
265 if (default_route_gateway != 0)
267 cifdefaultroute(0, 0, default_route_gateway);
272 cifproxyarp(0, proxy_arp_addr);
276 /********************************************************************
278 * sys_close - Clean up in a child process before execing.
288 /********************************************************************
290 * note_debug_level - note a change in the debug level.
293 void note_debug_level (void)
297 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
298 setlogmask(LOG_UPTO(LOG_DEBUG));
302 setlogmask(LOG_UPTO(LOG_WARNING));
306 /********************************************************************
308 * set_kdebugflag - Define the debugging level for the kernel
311 int set_kdebugflag (int requested_level)
313 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
315 if ( ! ok_error (errno) )
317 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
321 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
326 /********************************************************************
328 * establish_ppp - Turn the serial port into a ppp interface.
331 void establish_ppp (int tty_fd)
335 * The current PPP device will be the tty file.
339 * Ensure that the tty device is in exclusive mode.
341 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
343 if ( ! ok_error ( errno ))
345 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
349 * Demand mode - prime the old ppp device to relinquish the unit.
351 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
353 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
357 * Set the current tty to the PPP discpline
359 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
361 if ( ! ok_error (errno) )
363 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
368 * Find out which interface we were given.
370 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
372 if ( ! ok_error (errno))
374 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
379 * Check that we got the same unit again.
385 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
393 * Enable debug in the driver if requested.
396 set_kdebugflag (kdebugflag);
398 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
399 SC_RCV_EVNP | SC_RCV_ODDP));
401 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
402 driver_version, driver_modification, driver_patch));
404 * Fetch the initial file flags and reset blocking mode on the file.
406 initfdflags = fcntl(ppp_fd, F_GETFL);
408 if (initfdflags == -1 ||
409 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
411 if ( ! ok_error (errno))
414 "Couldn't set device to non-blocking mode: %m");
419 /********************************************************************
421 * disestablish_ppp - Restore the serial port to normal operation.
422 * This shouldn't call die() because it's called from die().
425 void disestablish_ppp(int tty_fd)
428 * Attempt to restore the previous tty settings
433 * Restore the previous line discipline
435 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
437 if ( ! ok_error (errno))
439 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
443 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
445 if ( ! ok_error (errno))
447 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
451 /* Reset non-blocking mode on fd. */
452 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
454 if ( ! ok_error (errno))
457 "Couldn't restore device fd flags: %m");
464 /********************************************************************
466 * clean_check - Fetch the flags for the device and generate
467 * appropriate error messages.
469 void clean_check(void)
476 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
479 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
482 case SC_RCV_B7_0 | SC_RCV_EVNP:
483 case SC_RCV_B7_0 | SC_RCV_ODDP:
484 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
485 s = "all had bit 7 set to 1";
489 case SC_RCV_B7_1 | SC_RCV_EVNP:
490 case SC_RCV_B7_1 | SC_RCV_ODDP:
491 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
492 s = "all had bit 7 set to 0";
496 s = "all had odd parity";
500 s = "all had even parity";
506 syslog(LOG_WARNING, "Receive serial link is not"
508 syslog(LOG_WARNING, "Problem: %s", s);
516 * List of valid speeds.
520 int speed_int, speed_val;
597 /********************************************************************
599 * Translate from bits/second to a speed_t.
602 static int translate_speed (int bps)
604 struct speed *speedp;
608 for (speedp = speeds; speedp->speed_int; speedp++)
610 if (bps == speedp->speed_int)
612 return speedp->speed_val;
615 syslog(LOG_WARNING, "speed %d not supported", bps);
620 /********************************************************************
622 * Translate from a speed_t to bits/second.
625 static int baud_rate_of (int speed)
627 struct speed *speedp;
631 for (speedp = speeds; speedp->speed_int; speedp++)
633 if (speed == speedp->speed_val)
635 return speedp->speed_int;
642 /********************************************************************
644 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
645 * at the requested speed, etc. If `local' is true, set CLOCAL
646 * regardless of whether the modem option was specified.
649 void set_up_tty (int tty_fd, int local)
654 if (tcgetattr(tty_fd, &tios) < 0)
656 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
665 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
666 tios.c_cflag |= CS8 | CREAD | HUPCL;
668 tios.c_iflag = IGNBRK | IGNPAR;
672 tios.c_cc[VTIME] = 0;
676 tios.c_cflag ^= (CLOCAL | HUPCL);
682 tios.c_cflag |= CRTSCTS;
686 tios.c_iflag |= IXON | IXOFF;
687 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
688 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
692 tios.c_cflag &= ~CRTSCTS;
699 speed = translate_speed(inspeed);
702 cfsetospeed (&tios, speed);
703 cfsetispeed (&tios, speed);
706 * We can't proceed if the serial port speed is B0,
707 * since that implies that the serial port is disabled.
711 speed = cfgetospeed(&tios);
714 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
720 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
722 syslog(LOG_ERR, "tcsetattr: %m");
726 baud_rate = baud_rate_of(speed);
730 /********************************************************************
732 * setdtr - control the DTR line on the serial port.
733 * This is called from die(), so it shouldn't call die().
736 void setdtr (int tty_fd, int on)
738 int modembits = TIOCM_DTR;
740 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
743 /********************************************************************
745 * restore_tty - restore the terminal to the saved settings.
748 void restore_tty (int tty_fd)
754 * Turn off echoing, because otherwise we can get into
755 * a loop with the tty and the modem echoing to each other.
756 * We presume we are the sole user of this tty device, so
757 * when we close it, it will revert to its defaults anyway.
761 inittermios.c_lflag &= ~(ECHO | ECHONL);
764 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
766 if (! ok_error (errno))
768 syslog(LOG_WARNING, "tcsetattr: %m");
774 /********************************************************************
776 * output - Output PPP packet.
779 void output (int unit, unsigned char *p, int len)
783 log_packet(p, len, "sent ", LOG_DEBUG);
786 if (write(ppp_fd, p, len) < 0)
788 if (errno == EWOULDBLOCK || errno == ENOBUFS
789 || errno == ENXIO || errno == EIO)
791 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
795 syslog(LOG_ERR, "write: %m(%d)", errno);
801 /********************************************************************
803 * wait_input - wait until there is data available on ppp_fd,
804 * for the length of time specified by *timo (indefinite
808 void wait_input (struct timeval *timo)
814 FD_SET(ppp_fd, &ready);
816 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
817 if (n < 0 && errno != EINTR)
819 syslog(LOG_ERR, "select: %m(%d)", errno);
824 /********************************************************************
826 * wait_loop_output - wait until there is data available on the
827 * loopback, for the length of time specified by *timo (indefinite
830 void wait_loop_output(timo)
831 struct timeval *timo;
837 FD_SET(master_fd, &ready);
838 n = select(master_fd + 1, &ready, NULL, &ready, timo);
839 if (n < 0 && errno != EINTR)
841 syslog(LOG_ERR, "select: %m(%d)", errno);
846 /********************************************************************
848 * wait_time - wait for a given length of time or until a
849 * signal is received.
853 struct timeval *timo;
857 n = select(0, NULL, NULL, NULL, timo);
858 if (n < 0 && errno != EINTR) {
859 syslog(LOG_ERR, "select: %m(%d)", errno);
864 /********************************************************************
866 * read_packet - get a PPP packet from the serial device.
869 int read_packet (unsigned char *buf)
873 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
876 if (errno == EWOULDBLOCK)
880 syslog(LOG_ERR, "read: %m(%d)", errno);
886 /********************************************************************
888 * get_loop_output - get outgoing packets from the ppp device,
889 * and detect when we want to bring the real link up.
890 * Return value is 1 if we need to bring up the link, 0 otherwise.
893 get_loop_output(void)
896 int n = read(master_fd, inbuf, sizeof(inbuf));
900 if (loop_chars(inbuf, n))
904 n = read(master_fd, inbuf, sizeof(inbuf));
909 syslog(LOG_ERR, "eof on loopback");
913 if (errno != EWOULDBLOCK)
915 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
922 /********************************************************************
924 * ppp_send_config - configure the transmit characteristics of
928 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
933 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
935 * Ensure that the link is still up.
940 * Set the MTU and other parameters for the ppp device
942 memset (&ifr, '\0', sizeof (ifr));
943 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
946 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
948 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
952 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
953 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
955 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
960 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
961 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
966 /********************************************************************
968 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
971 void ppp_set_xaccm (int unit, ext_accm accm)
973 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
974 accm[0], accm[1], accm[2], accm[3]));
976 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
978 if ( ! ok_error (errno))
980 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
985 /********************************************************************
987 * ppp_recv_config - configure the receive-side characteristics of
991 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
995 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
997 * If we were called because the link has gone down then there is nothing
998 * which may be done. Just return without incident.
1005 * Set the receiver parameters
1007 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1009 if ( ! ok_error (errno))
1011 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
1015 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
1016 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
1018 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1023 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1027 /********************************************************************
1029 * ccp_test - ask kernel whether a given compression method
1030 * is acceptable for use.
1033 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1035 struct ppp_option_data data;
1037 memset (&data, '\0', sizeof (data));
1039 data.length = opt_len;
1040 data.transmit = for_transmit;
1042 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1047 return (errno == ENOBUFS)? 0: -1;
1050 /********************************************************************
1052 * ccp_flags_set - inform kernel about the current state of CCP.
1055 void ccp_flags_set (int unit, int isopen, int isup)
1059 int x = get_flags();
1060 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1061 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1066 /********************************************************************
1068 * get_idle_time - return how long the link has been idle.
1071 get_idle_time(u, ip)
1073 struct ppp_idle *ip;
1075 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1078 /********************************************************************
1080 * ccp_fatal_error - returns 1 if decompression was disabled as a
1081 * result of an error detected after decompression of a packet,
1082 * 0 otherwise. This is necessary because of patent nonsense.
1085 int ccp_fatal_error (int unit)
1087 int x = get_flags();
1089 return x & SC_DC_FERROR;
1093 * path_to_route - determine the path to the proc file system data
1096 FILE *route_fd = (FILE *) 0;
1097 static char route_buffer [512];
1099 static char *path_to_route (void);
1100 static int open_route_table (void);
1101 static void close_route_table (void);
1102 static int read_route_table (struct rtentry *rt);
1104 /********************************************************************
1106 * path_to_procfs - find the path to the proc file system mount point
1109 static int path_to_procfs (void)
1111 struct mntent *mntent;
1114 fp = fopen (MOUNTED, "r");
1117 mntent = getmntent (fp);
1118 while (mntent != (struct mntent *) 0)
1120 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1122 if (strcmp (mntent->mnt_type, "proc") == 0)
1124 strncpy (route_buffer, mntent->mnt_dir,
1125 sizeof (route_buffer)-10);
1126 route_buffer [sizeof (route_buffer)-10] = '\0';
1131 mntent = getmntent (fp);
1136 /* Default the mount location of /proc */
1137 strncpy (route_buffer, "/proc", sizeof (route_buffer)-10);
1141 /********************************************************************
1143 * path_to_route - find the path to the route tables in the proc file system
1146 static char *path_to_route (void)
1148 if (! path_to_procfs())
1150 syslog (LOG_ERR, "proc file system not mounted");
1153 strcat (route_buffer, "/net/route");
1154 return (route_buffer);
1157 /********************************************************************
1159 * close_route_table - close the interface to the route table
1162 static void close_route_table (void)
1164 if (route_fd != (FILE *) 0)
1167 route_fd = (FILE *) 0;
1171 /********************************************************************
1173 * open_route_table - open the interface to the route table
1176 static int open_route_table (void)
1180 close_route_table();
1182 path = path_to_route();
1188 route_fd = fopen (path, "r");
1189 if (route_fd == (FILE *) 0)
1191 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1197 /********************************************************************
1199 * read_route_table - read the next entry from the route table
1202 static int read_route_table (struct rtentry *rt)
1204 static char delims[] = " \t\n";
1205 char *dev_ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1207 memset (rt, '\0', sizeof (struct rtentry));
1211 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1217 dev_ptr = strtok (route_buffer, delims); /* interface name */
1218 dst_ptr = strtok (NULL, delims); /* destination address */
1219 gw_ptr = strtok (NULL, delims); /* gateway */
1220 flag_ptr = strtok (NULL, delims); /* flags */
1222 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1227 /* Discard that stupid header line which should never
1228 * have been there in the first place !! */
1229 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1235 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1236 strtoul (dst_ptr, NULL, 16);
1238 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1239 strtoul (gw_ptr, NULL, 16);
1241 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1242 rt->rt_dev = dev_ptr;
1247 /********************************************************************
1249 * defaultroute_exists - determine if there is a default route
1252 static int defaultroute_exists (struct rtentry *rt)
1256 if (!open_route_table())
1261 while (read_route_table(rt) != 0)
1263 if ((rt->rt_flags & RTF_UP) == 0)
1268 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1275 close_route_table();
1279 /********************************************************************
1281 * sifdefaultroute - assign a default route through the address given.
1284 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1288 if (defaultroute_exists(&rt))
1290 struct in_addr old_gateway =
1291 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1293 if (old_gateway.s_addr != gateway)
1296 "not replacing existing default route to %s [%s]",
1298 inet_ntoa (old_gateway));
1303 memset (&rt, '\0', sizeof (rt));
1304 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1305 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1307 if (strcmp(utsname.release, "2.1.0") > 0) {
1308 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1309 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1312 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1314 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1315 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1317 if ( ! ok_error ( errno ))
1319 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1324 default_route_gateway = gateway;
1328 /********************************************************************
1330 * cifdefaultroute - delete a default route through the address given.
1333 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1337 default_route_gateway = 0;
1339 memset (&rt, '\0', sizeof (rt));
1340 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1341 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1343 if (strcmp(utsname.release, "2.1.0") > 0) {
1344 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1345 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1348 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1350 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1351 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1355 if ( ! ok_error ( errno ))
1358 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1367 /********************************************************************
1369 * sifproxyarp - Make a proxy ARP entry for the peer.
1372 int sifproxyarp (int unit, u_int32_t his_adr)
1374 struct arpreq arpreq;
1376 if (has_proxy_arp == 0)
1378 memset (&arpreq, '\0', sizeof(arpreq));
1380 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1381 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1382 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1384 * Get the hardware address of an interface on the same subnet
1385 * as our local address.
1387 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1389 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1393 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1395 if ( ! ok_error ( errno ))
1397 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1403 proxy_arp_addr = his_adr;
1408 /********************************************************************
1410 * cifproxyarp - Delete the proxy ARP entry for the peer.
1413 int cifproxyarp (int unit, u_int32_t his_adr)
1415 struct arpreq arpreq;
1417 if (has_proxy_arp == 1)
1419 memset (&arpreq, '\0', sizeof(arpreq));
1420 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1421 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1422 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1424 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1426 if ( ! ok_error ( errno ))
1428 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1437 /********************************************************************
1439 * get_ether_addr - get the hardware address of an interface on the
1440 * the same subnet as ipaddr.
1443 static int get_ether_addr (u_int32_t ipaddr,
1444 struct sockaddr *hwaddr,
1447 struct ifreq *ifr, *ifend;
1448 u_int32_t ina, mask;
1451 struct ifreq ifs[MAX_IFS];
1453 ifc.ifc_len = sizeof(ifs);
1455 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1457 if ( ! ok_error ( errno ))
1459 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1464 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1465 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1467 * Scan through looking for an interface with an Internet
1468 * address on the same subnet as `ipaddr'.
1470 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1471 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1473 if (ifr->ifr_addr.sa_family == AF_INET)
1475 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1476 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1477 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1480 * Check that the interface is up, and not point-to-point
1483 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1488 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1493 * Get its netmask and check that it's on the right subnet.
1495 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1500 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1501 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1502 ip_ntoa(ina), ntohl(mask)));
1504 if (((ipaddr ^ ina) & mask) != 0)
1517 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1518 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1520 * Now get the hardware address.
1522 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1523 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1525 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1531 sizeof (struct sockaddr));
1533 SYSDEBUG ((LOG_DEBUG,
1534 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1535 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1536 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1537 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1538 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1539 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1540 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1541 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1542 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1546 /********************************************************************
1548 * Return user specified netmask, modified by any mask we might determine
1549 * for address `addr' (in network byte order).
1550 * Here we scan through the system's list of interfaces, looking for
1551 * any non-point-to-point interfaces which might appear to be on the same
1552 * network as `addr'. If we find any, we OR in their netmask to the
1553 * user-specified netmask.
1556 u_int32_t GetMask (u_int32_t addr)
1558 u_int32_t mask, nmask, ina;
1559 struct ifreq *ifr, *ifend, ifreq;
1561 struct ifreq ifs[MAX_IFS];
1565 if (IN_CLASSA(addr)) /* determine network mask for address class */
1567 nmask = IN_CLASSA_NET;
1571 if (IN_CLASSB(addr))
1573 nmask = IN_CLASSB_NET;
1577 nmask = IN_CLASSC_NET;
1581 /* class D nets are disallowed by bad_ip_adrs */
1582 mask = netmask | htonl(nmask);
1584 * Scan through the system's network interfaces.
1586 ifc.ifc_len = sizeof(ifs);
1588 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1590 if ( ! ok_error ( errno ))
1592 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1597 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1598 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1601 * Check the interface's internet address.
1603 if (ifr->ifr_addr.sa_family != AF_INET)
1607 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1608 if (((ntohl(ina) ^ addr) & nmask) != 0)
1613 * Check that the interface is up, and not point-to-point nor loopback.
1615 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1616 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1621 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1626 * Get its netmask and OR it into our mask.
1628 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1632 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1638 /********************************************************************
1640 * Internal routine to decode the version.modification.patch level
1643 static void decode_version (char *buf, int *version,
1644 int *modification, int *patch)
1646 *version = (int) strtoul (buf, &buf, 10);
1653 *modification = (int) strtoul (buf, &buf, 10);
1657 *patch = (int) strtoul (buf, &buf, 10);
1669 /********************************************************************
1671 * Procedure to determine if the PPP line discipline is registered.
1675 ppp_registered(void)
1681 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1684 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1688 initfdflags = fcntl(local_fd, F_GETFL);
1689 if (initfdflags == -1)
1691 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1696 initfdflags &= ~O_NONBLOCK;
1697 fcntl(local_fd, F_SETFL, initfdflags);
1699 * Read the initial line dicipline and try to put the device into the
1702 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1704 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1709 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1711 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1716 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1718 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1727 /********************************************************************
1729 * ppp_available - check whether the system has any ppp interfaces
1730 * (in fact we check whether we can do an ioctl on ppp0).
1733 int ppp_available(void)
1738 int my_version, my_modification, my_patch;
1739 extern char *no_ppp_msg;
1741 * Open a socket for doing the ioctl operations.
1743 s = socket(AF_INET, SOCK_DGRAM, 0);
1749 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1750 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1752 * If the device did not exist then attempt to create one by putting the
1753 * current tty into the PPP discipline. If this works then obtain the
1754 * flags for the device again.
1758 if (ppp_registered())
1760 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1761 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1765 * Ensure that the hardware address is for PPP and not something else
1769 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1772 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1780 "This system lacks kernel support for PPP. This could be because\n"
1781 "the PPP kernel module is not loaded, or because the kernel is\n"
1782 "not configured for PPP. See the README.linux file in the\n"
1783 "ppp-2.3.3 distribution.\n";
1786 * This is the PPP device. Validate the version of the driver at this
1787 * point to ensure that this program will work with the driver.
1791 char abBuffer [1024];
1793 ifr.ifr_data = abBuffer;
1794 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1799 decode_version (abBuffer,
1801 &driver_modification,
1808 driver_modification =
1812 * Validate the version of the driver against the version that we used.
1814 decode_version (VERSION,
1819 /* The version numbers must match */
1820 if (driver_version != my_version)
1825 /* The modification levels must be legal */
1826 if (driver_modification < my_modification)
1828 if (driver_modification >= 2) {
1829 /* we can cope with 2.2.0 and above */
1839 sprintf (route_buffer,
1840 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1841 driver_version, driver_modification, driver_patch);
1843 no_ppp_msg = route_buffer;
1849 /********************************************************************
1851 * Update the wtmp file with the appropriate user name and tty device.
1854 void logwtmp (const char *line, const char *name, const char *host)
1857 struct utmp ut, *utp;
1858 pid_t mypid = getpid();
1860 * Update the signon database for users.
1861 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1863 utmpname(_PATH_UTMP);
1865 while ((utp = getutent()) && (utp->ut_pid != mypid))
1868 /* Is this call really necessary? There is another one after the 'put' */
1873 memcpy(&ut, utp, sizeof(ut));
1877 /* some gettys/telnetds don't initialize utmp... */
1878 memset(&ut, 0, sizeof(ut));
1881 if (ut.ut_id[0] == 0)
1883 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1886 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1887 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1891 ut.ut_type = USER_PROCESS;
1894 /* Insert the host name if one is supplied */
1897 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1900 /* Insert the IP address of the remote system if IP is enabled */
1901 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1903 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1904 sizeof(ut.ut_addr));
1907 /* CL: Makes sure that the logout works */
1908 if (*host == 0 && *name==0)
1916 * Update the wtmp file.
1918 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1921 flock(wtmp, LOCK_EX);
1923 /* we really should check for error on the write for a full disk! */
1924 write (wtmp, (char *)&ut, sizeof(ut));
1927 flock(wtmp, LOCK_UN);
1931 /********************************************************************
1932 * Code for locking/unlocking the serial device.
1933 * This code is derived from chat.c.
1937 * lock - create a lock file for the named device
1940 int lock (char *dev)
1944 lock_file = malloc(strlen(dev) + 1);
1945 if (lock_file == NULL)
1947 novm("lock file name");
1949 strcpy (lock_file, dev);
1950 result = mklock (dev, (void *) 0);
1954 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1963 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1971 char hdb_lock_buffer[12];
1976 p = strrchr(dev, '/');
1982 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1983 if (lock_file == NULL)
1985 novm("lock file name");
1988 strcpy (lock_file, LOCK_PREFIX);
1989 strcat (lock_file, dev);
1991 * Attempt to create the lock file at this point.
1995 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
2000 sprintf (hdb_lock_buffer, "%010d\n", pid);
2001 write (fd, hdb_lock_buffer, 11);
2003 write(fd, &pid, sizeof (pid));
2009 * If the file exists then check to see if the pid is stale
2011 if (errno == EEXIST)
2013 fd = open(lock_file, O_RDONLY, 0);
2016 if (errno == ENOENT) /* This is just a timing problem. */
2023 /* Read the lock file to find out who has the device locked */
2024 n = read (fd, hdb_lock_buffer, 11);
2028 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2032 /* See the process still exists. */
2036 hdb_lock_buffer[n] = '\0';
2037 sscanf (hdb_lock_buffer, " %d", &pid);
2039 pid = ((int *) hdb_lock_buffer)[0];
2041 if (pid == 0 || pid == getpid()
2042 || (kill(pid, 0) == -1 && errno == ESRCH))
2048 /* If the process does not exist then try to remove the lock */
2049 if (n == 0 && unlink (lock_file) == 0)
2051 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2056 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2060 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2071 /********************************************************************
2073 * unlock - remove our lockfile
2081 (void) rmlock (lock_file, (void *) 0);
2090 /********************************************************************
2092 * sifvjcomp - config tcp header compression
2095 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2097 u_int x = get_flags();
2101 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2103 if (! ok_error (errno))
2105 syslog (LOG_ERR, "ioctl(PPPIOCSMAXCID): %m(%d)", errno);
2111 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2112 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2118 /********************************************************************
2120 * sifup - Config the interface up and enable IP packets to pass.
2127 memset (&ifr, '\0', sizeof (ifr));
2128 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2129 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2131 if (! ok_error (errno))
2133 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2138 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2139 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2141 if (! ok_error (errno))
2143 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2151 /********************************************************************
2153 * sifdown - Config the interface down and disable IP.
2162 memset (&ifr, '\0', sizeof (ifr));
2163 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2164 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2166 if (! ok_error (errno))
2168 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2173 ifr.ifr_flags &= ~IFF_UP;
2174 ifr.ifr_flags |= IFF_POINTOPOINT;
2175 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2177 if (! ok_error (errno))
2179 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2186 /********************************************************************
2188 * sifaddr - Config the interface IP addresses and netmask.
2191 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2197 memset (&ifr, '\0', sizeof (ifr));
2198 memset (&rt, '\0', sizeof (rt));
2200 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2201 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2202 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2204 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2206 * Set our IP address
2208 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2209 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2211 if (errno != EEXIST)
2213 if (! ok_error (errno))
2215 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2220 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2225 * Set the gateway address
2227 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2228 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2230 if (! ok_error (errno))
2232 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2238 * For recent kernels, force the netmask to 255.255.255.255.
2240 if (strcmp(utsname.release, "2.1.16") >= 0)
2244 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2245 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2247 if (! ok_error (errno))
2249 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2255 * Add the device route
2257 if (strcmp(utsname.release, "2.1.16") < 0) {
2258 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2259 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2262 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2263 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2264 rt.rt_flags = RTF_UP | RTF_HOST;
2266 if (strcmp(utsname.release, "2.1.0") > 0) {
2267 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2268 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2271 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2273 if (! ok_error (errno))
2275 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2283 /********************************************************************
2285 * cifaddr - Clear the interface IP addresses, and delete routes
2286 * through the interface if possible.
2289 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2293 if (strcmp(utsname.release, "2.1.16") < 0) {
2295 * Delete the route through the device
2297 memset (&rt, '\0', sizeof (rt));
2299 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2300 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2303 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2304 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2305 rt.rt_flags = RTF_UP | RTF_HOST;
2307 if (strcmp(utsname.release, "2.1.0") > 0) {
2308 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2309 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2312 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2314 if (still_ppp() && ! ok_error (errno))
2316 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2324 /********************************************************************
2326 * open_loopback - open the device we use for getting packets
2327 * in demand mode. Under Linux, we use our existing fd
2328 * to the ppp driver.
2331 open_ppp_loopback(void)
2334 struct termios tios;
2337 for (i = 0; i < 64; ++i) {
2338 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2339 master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2343 if (master_fd < 0) {
2344 syslog(LOG_ERR, "No free pty for loopback");
2347 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2349 slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2351 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2355 set_ppp_fd(slave_fd);
2357 if (tcgetattr(ppp_fd, &tios) == 0)
2359 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2360 tios.c_cflag |= CS8 | CREAD;
2361 tios.c_iflag = IGNPAR | CLOCAL;
2364 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2366 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2370 flags = fcntl(master_fd, F_GETFL);
2372 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2374 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2377 flags = fcntl(ppp_fd, F_GETFL);
2379 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2381 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2384 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2386 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2390 * Find out which interface we were given.
2392 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2394 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2398 * Enable debug in the driver if requested.
2400 set_kdebugflag (kdebugflag);
2403 /********************************************************************
2405 * restore_loop - reattach the ppp unit to the loopback.
2407 * The kernel ppp driver automatically reattaches the ppp unit to
2408 * the loopback if the serial port is set to a line discipline other
2409 * than ppp, or if it detects a modem hangup. The former will happen
2410 * in disestablish_ppp if the latter hasn't already happened, so we
2411 * shouldn't need to do anything.
2413 * Just to be sure, set the real serial port to the normal discipline.
2419 if (ppp_fd != slave_fd)
2421 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2422 set_ppp_fd(slave_fd);
2426 /********************************************************************
2428 * sifnpmode - Set the mode for handling packets for a given NP.
2432 sifnpmode(u, proto, mode)
2439 npi.protocol = proto;
2441 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2443 if (! ok_error (errno))
2445 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2446 proto, mode, errno);
2447 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2455 /********************************************************************
2457 * sipxfaddr - Config the interface IPX networknumber
2460 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2467 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2469 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2472 if (! ok_error (errno))
2474 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2480 memset (&ifr, '\0', sizeof (ifr));
2481 strcpy (ifr.ifr_name, ifname);
2483 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2484 sipx->sipx_family = AF_IPX;
2485 sipx->sipx_port = 0;
2486 sipx->sipx_network = htonl (network);
2487 sipx->sipx_type = IPX_FRAME_ETHERII;
2488 sipx->sipx_action = IPX_CRTITF;
2490 * Set the IPX device
2492 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2495 if (errno != EEXIST)
2497 if (! ok_error (errno))
2500 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2505 syslog (LOG_WARNING,
2506 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2515 /********************************************************************
2517 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2518 * are removed and the device is no longer able to pass IPX
2522 int cipxfaddr (int unit)
2529 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2531 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2534 if (! ok_error (errno))
2536 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2542 memset (&ifr, '\0', sizeof (ifr));
2543 strcpy (ifr.ifr_name, ifname);
2545 sipx->sipx_type = IPX_FRAME_ETHERII;
2546 sipx->sipx_action = IPX_DLTITF;
2547 sipx->sipx_family = AF_IPX;
2549 * Set the IPX device
2551 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2553 if (! ok_error (errno))
2556 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2567 * daemon - Detach us from controlling terminal session.
2570 daemon(nochdir, noclose)
2571 int nochdir, noclose;
2575 if ((pid = fork()) < 0)
2578 exit(0); /* parent dies */
2583 fclose(stdin); /* don't need stdin, stdout, stderr */
2591 * Use the hostname as part of the random number seed.
2600 for (p = hostname; *p != 0; ++p)
2605 /********************************************************************
2607 * sys_check_options - check the options that the user specified
2611 sys_check_options(void)
2614 struct stat stat_buf;
2616 * Disable the IPX protocol if the support is not present in the kernel.
2617 * If we disable it then ensure that IP support is enabled.
2619 while (ipxcp_protent.enabled_flag)
2621 if (path_to_procfs())
2623 strcat (route_buffer, "/net/ipx_interface");
2624 if (lstat (route_buffer, &stat_buf) >= 0)
2629 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2630 ipxcp_protent.enabled_flag = 0;
2631 ipcp_protent.enabled_flag = 1;
2635 if (demand && driver_is_old) {
2636 option_error("demand dialling is not supported by kernel driver version "
2637 "%d.%d.%d", driver_version, driver_modification,