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
54 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
56 #include <net/if_arp.h>
57 #include <net/route.h>
58 #include <netinet/if_ether.h>
60 #include <linux/types.h>
62 #include <linux/if_arp.h>
63 #include <linux/route.h>
64 #include <linux/if_ether.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
69 #include <linux/ppp_defs.h>
70 #include <linux/if_ppp.h>
75 #include "patchlevel.h"
79 #if __GLIBC__ >= 2 && \
80 !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
81 #include <netipx/ipx.h>
83 #include <linux/ipx.h>
85 #endif /* IPX_CHANGE */
88 #include <sys/locks.h>
91 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
95 #define ok_error(num) ((num)==EIO)
97 static int tty_disc = N_TTY; /* The TTY discipline */
98 static int ppp_disc = N_PPP; /* The PPP discpline */
99 static int initfdflags = -1; /* Initial file descriptor flags for fd */
100 static int ppp_fd = -1; /* fd which is set to PPP discipline */
101 static int sock_fd = -1; /* socket for doing interface ioctls */
102 static int slave_fd = -1;
103 static int master_fd = -1;
105 static int has_proxy_arp = 0;
106 static int driver_version = 0;
107 static int driver_modification = 0;
108 static int driver_patch = 0;
109 static int driver_is_old = 0;
110 static int restore_term = 0; /* 1 => we've munged the terminal */
111 static struct termios inittermios; /* Initial TTY termios */
113 static char loop_name[20];
114 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
116 static int if_is_up; /* Interface has been marked up */
117 static u_int32_t default_route_gateway; /* Gateway for default route added */
118 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
120 static char *lock_file;
122 static struct utsname utsname; /* for the kernel version */
123 static int kernel_version;
124 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
128 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
129 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
130 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
132 /* Prototypes for procedures local to this file. */
133 static int get_flags (void);
134 static void set_flags (int flags);
135 static int translate_speed (int bps);
136 static int baud_rate_of (int speed);
137 static char *path_to_route (void);
138 static void close_route_table (void);
139 static int open_route_table (void);
140 static int read_route_table (struct rtentry *rt);
141 static int defaultroute_exists (struct rtentry *rt);
142 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
144 static void decode_version (char *buf, int *version, int *mod, int *patch);
146 extern u_char inpacket_buf[]; /* borrowed from main.c */
149 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
153 #define SET_SA_FAMILY(addr, family) \
154 memset ((char *) &(addr), '\0', sizeof(addr)); \
155 addr.sa_family = (family);
158 * Determine if the PPP connection should still be present.
164 #define LOCK_PREFIX "/var/lock/LCK.."
167 static void set_ppp_fd (int new_fd)
169 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd));
173 static int still_ppp(void)
175 if (!hungup || ppp_fd == slave_fd)
178 set_ppp_fd(slave_fd);
184 /********************************************************************
186 * Functions to read and set the flags value in the device driver
189 static int get_flags (void)
193 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
195 if ( ok_error (errno) )
201 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
206 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
210 /********************************************************************/
212 static void set_flags (int flags)
214 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
216 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
218 if (! ok_error (errno) )
220 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
226 /********************************************************************
228 * sys_init - System-dependent initialization.
233 int osmaj, osmin, ospatch;
235 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
236 setlogmask(LOG_UPTO(LOG_INFO));
239 setlogmask(LOG_UPTO(LOG_DEBUG));
242 /* Get an internet socket for doing socket ioctls. */
243 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
246 if ( ! ok_error ( errno ))
248 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
254 osmaj = osmin = ospatch = 0;
255 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
256 kernel_version = KVERSION(osmaj, osmin, ospatch);
259 /********************************************************************
261 * sys_cleanup - restore any system state we modified before exiting:
262 * mark the interface down, delete default route and/or proxy arp entry.
263 * This should call die() because it's called from die().
266 void sys_cleanup(void)
269 * Take down the device
276 * Delete any routes through the device.
278 if (default_route_gateway != 0)
280 cifdefaultroute(0, 0, default_route_gateway);
285 cifproxyarp(0, proxy_arp_addr);
289 /********************************************************************
291 * sys_close - Clean up in a child process before execing.
301 /********************************************************************
303 * note_debug_level - note a change in the debug level.
306 void note_debug_level (void)
310 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
311 setlogmask(LOG_UPTO(LOG_DEBUG));
315 setlogmask(LOG_UPTO(LOG_WARNING));
319 /********************************************************************
321 * set_kdebugflag - Define the debugging level for the kernel
324 int set_kdebugflag (int requested_level)
326 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
328 if ( ! ok_error (errno) )
330 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
334 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
339 /********************************************************************
341 * establish_ppp - Turn the serial port into a ppp interface.
344 void establish_ppp (int tty_fd)
348 * The current PPP device will be the tty file.
352 * Ensure that the tty device is in exclusive mode.
354 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
356 if ( ! ok_error ( errno ))
358 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
362 * Demand mode - prime the old ppp device to relinquish the unit.
364 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
366 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
370 * Set the current tty to the PPP discpline
372 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
374 if ( ! ok_error (errno) )
376 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
381 * Find out which interface we were given.
383 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
385 if ( ! ok_error (errno))
387 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
392 * Check that we got the same unit again.
398 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
406 * Enable debug in the driver if requested.
409 set_kdebugflag (kdebugflag);
411 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
412 SC_RCV_EVNP | SC_RCV_ODDP));
414 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
415 driver_version, driver_modification, driver_patch));
417 * Fetch the initial file flags and reset blocking mode on the file.
419 initfdflags = fcntl(ppp_fd, F_GETFL);
421 if (initfdflags == -1 ||
422 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
424 if ( ! ok_error (errno))
427 "Couldn't set device to non-blocking mode: %m");
432 /********************************************************************
434 * disestablish_ppp - Restore the serial port to normal operation.
435 * This shouldn't call die() because it's called from die().
438 void disestablish_ppp(int tty_fd)
441 * Attempt to restore the previous tty settings
446 * Restore the previous line discipline
448 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
450 if ( ! ok_error (errno))
452 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
456 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
458 if ( ! ok_error (errno))
460 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
464 /* Reset non-blocking mode on fd. */
465 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
467 if ( ! ok_error (errno))
470 "Couldn't restore device fd flags: %m");
477 /********************************************************************
479 * clean_check - Fetch the flags for the device and generate
480 * appropriate error messages.
482 void clean_check(void)
489 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
492 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
495 case SC_RCV_B7_0 | SC_RCV_EVNP:
496 case SC_RCV_B7_0 | SC_RCV_ODDP:
497 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
498 s = "all had bit 7 set to 1";
502 case SC_RCV_B7_1 | SC_RCV_EVNP:
503 case SC_RCV_B7_1 | SC_RCV_ODDP:
504 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
505 s = "all had bit 7 set to 0";
509 s = "all had odd parity";
513 s = "all had even parity";
519 syslog(LOG_WARNING, "Receive serial link is not"
521 syslog(LOG_WARNING, "Problem: %s", s);
529 * List of valid speeds.
533 int speed_int, speed_val;
610 /********************************************************************
612 * Translate from bits/second to a speed_t.
615 static int translate_speed (int bps)
617 struct speed *speedp;
621 for (speedp = speeds; speedp->speed_int; speedp++)
623 if (bps == speedp->speed_int)
625 return speedp->speed_val;
628 syslog(LOG_WARNING, "speed %d not supported", bps);
633 /********************************************************************
635 * Translate from a speed_t to bits/second.
638 static int baud_rate_of (int speed)
640 struct speed *speedp;
644 for (speedp = speeds; speedp->speed_int; speedp++)
646 if (speed == speedp->speed_val)
648 return speedp->speed_int;
655 /********************************************************************
657 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
658 * at the requested speed, etc. If `local' is true, set CLOCAL
659 * regardless of whether the modem option was specified.
662 void set_up_tty(int tty_fd, int local)
668 if (tcgetattr(tty_fd, &tios) < 0)
670 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
679 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
680 tios.c_cflag |= CS8 | CREAD | HUPCL;
682 tios.c_iflag = IGNBRK | IGNPAR;
686 tios.c_cc[VTIME] = 0;
690 tios.c_cflag ^= (CLOCAL | HUPCL);
696 tios.c_cflag |= CRTSCTS;
700 tios.c_iflag |= IXON | IXOFF;
701 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
702 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
706 tios.c_cflag &= ~CRTSCTS;
713 speed = translate_speed(inspeed);
716 cfsetospeed (&tios, speed);
717 cfsetispeed (&tios, speed);
720 * We can't proceed if the serial port speed is B0,
721 * since that implies that the serial port is disabled.
725 speed = cfgetospeed(&tios);
728 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
734 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
736 syslog(LOG_ERR, "tcsetattr: %m");
740 baud_rate = baud_rate_of(speed);
744 /********************************************************************
746 * setdtr - control the DTR line on the serial port.
747 * This is called from die(), so it shouldn't call die().
750 void setdtr (int tty_fd, int on)
752 int modembits = TIOCM_DTR;
754 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
757 /********************************************************************
759 * restore_tty - restore the terminal to the saved settings.
762 void restore_tty (int tty_fd)
768 * Turn off echoing, because otherwise we can get into
769 * a loop with the tty and the modem echoing to each other.
770 * We presume we are the sole user of this tty device, so
771 * when we close it, it will revert to its defaults anyway.
775 inittermios.c_lflag &= ~(ECHO | ECHONL);
778 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
780 if (! ok_error (errno))
782 syslog(LOG_WARNING, "tcsetattr: %m");
788 /********************************************************************
790 * output - Output PPP packet.
793 void output (int unit, unsigned char *p, int len)
797 log_packet(p, len, "sent ", LOG_DEBUG);
800 if (write(ppp_fd, p, len) < 0)
802 if (errno == EWOULDBLOCK || errno == ENOBUFS
803 || errno == ENXIO || errno == EIO || errno == EINTR)
805 syslog(LOG_WARNING, "write: warning: %m (%d)", errno);
809 syslog(LOG_ERR, "write: %m (%d)", errno);
814 /********************************************************************
816 * wait_input - wait until there is data available on ppp_fd,
817 * for the length of time specified by *timo (indefinite
821 void wait_input (struct timeval *timo)
827 FD_SET(ppp_fd, &ready);
829 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
830 if (n < 0 && errno != EINTR)
832 syslog(LOG_ERR, "select: %m(%d)", errno);
837 /********************************************************************
839 * wait_loop_output - wait until there is data available on the
840 * loopback, for the length of time specified by *timo (indefinite
843 void wait_loop_output(timo)
844 struct timeval *timo;
850 FD_SET(master_fd, &ready);
851 n = select(master_fd + 1, &ready, NULL, &ready, timo);
852 if (n < 0 && errno != EINTR)
854 syslog(LOG_ERR, "select: %m(%d)", errno);
859 /********************************************************************
861 * wait_time - wait for a given length of time or until a
862 * signal is received.
866 struct timeval *timo;
870 n = select(0, NULL, NULL, NULL, timo);
871 if (n < 0 && errno != EINTR) {
872 syslog(LOG_ERR, "select: %m(%d)", errno);
877 /********************************************************************
879 * read_packet - get a PPP packet from the serial device.
882 int read_packet (unsigned char *buf)
886 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
889 if (errno == EWOULDBLOCK)
893 syslog(LOG_ERR, "read: %m(%d)", errno);
899 /********************************************************************
901 * get_loop_output - get outgoing packets from the ppp device,
902 * and detect when we want to bring the real link up.
903 * Return value is 1 if we need to bring up the link, 0 otherwise.
906 get_loop_output(void)
909 int n = read(master_fd, inbuf, sizeof(inbuf));
913 if (loop_chars(inbuf, n))
917 n = read(master_fd, inbuf, sizeof(inbuf));
922 syslog(LOG_ERR, "eof on loopback");
926 if (errno != EWOULDBLOCK)
928 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
935 /********************************************************************
937 * ppp_send_config - configure the transmit characteristics of
941 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
946 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
948 * Ensure that the link is still up.
953 * Set the MTU and other parameters for the ppp device
955 memset (&ifr, '\0', sizeof (ifr));
956 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
959 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
961 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
965 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
966 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
968 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
973 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
974 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
979 /********************************************************************
981 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
984 void ppp_set_xaccm (int unit, ext_accm accm)
986 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
987 accm[0], accm[1], accm[2], accm[3]));
989 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
991 if ( ! ok_error (errno))
993 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
998 /********************************************************************
1000 * ppp_recv_config - configure the receive-side characteristics of
1001 * the ppp interface.
1004 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
1008 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
1010 * If we were called because the link has gone down then there is nothing
1011 * which may be done. Just return without incident.
1018 * Set the receiver parameters
1020 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1022 if ( ! ok_error (errno))
1024 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
1028 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
1029 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
1031 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1036 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1040 /********************************************************************
1042 * ccp_test - ask kernel whether a given compression method
1043 * is acceptable for use.
1046 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1048 struct ppp_option_data data;
1050 memset (&data, '\0', sizeof (data));
1052 data.length = opt_len;
1053 data.transmit = for_transmit;
1055 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1060 return (errno == ENOBUFS)? 0: -1;
1063 /********************************************************************
1065 * ccp_flags_set - inform kernel about the current state of CCP.
1068 void ccp_flags_set (int unit, int isopen, int isup)
1072 int x = get_flags();
1073 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1074 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1079 /********************************************************************
1081 * get_idle_time - return how long the link has been idle.
1084 get_idle_time(u, ip)
1086 struct ppp_idle *ip;
1088 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1091 /********************************************************************
1093 * ccp_fatal_error - returns 1 if decompression was disabled as a
1094 * result of an error detected after decompression of a packet,
1095 * 0 otherwise. This is necessary because of patent nonsense.
1098 int ccp_fatal_error (int unit)
1100 int x = get_flags();
1102 return x & SC_DC_FERROR;
1106 * path_to_route - determine the path to the proc file system data
1108 #define ROUTE_MAX_COLS 12
1109 FILE *route_fd = (FILE *) 0;
1110 static char route_buffer [512];
1111 static int route_dev_col, route_dest_col, route_gw_col;
1112 static int route_flags_col, route_mask_col;
1113 static int route_num_cols;
1115 static char *path_to_route (void);
1116 static int open_route_table (void);
1117 static void close_route_table (void);
1118 static int read_route_table (struct rtentry *rt);
1120 /********************************************************************
1122 * path_to_procfs - find the path to the proc file system mount point
1125 static int path_to_procfs (void)
1127 struct mntent *mntent;
1130 fp = fopen(MOUNTED, "r");
1132 /* Default the mount location of /proc */
1133 strlcpy (route_buffer, sizeof (route_buffer), "/proc");
1137 while ((mntent = getmntent(fp)) != NULL) {
1138 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
1140 if (strcmp(mntent->mnt_type, "proc") == 0)
1147 strlcpy(route_buffer, sizeof (route_buffer), mntent->mnt_dir);
1151 /********************************************************************
1153 * path_to_route - find the path to the route tables in the proc file system
1156 static char *path_to_route (void)
1158 if (!path_to_procfs()) {
1159 syslog (LOG_ERR, "proc file system not mounted");
1162 strlcat (route_buffer, sizeof(route_buffer), "/net/route");
1163 return (route_buffer);
1166 /********************************************************************
1168 * close_route_table - close the interface to the route table
1171 static void close_route_table (void)
1173 if (route_fd != (FILE *) 0) {
1175 route_fd = (FILE *) 0;
1179 /********************************************************************
1181 * open_route_table - open the interface to the route table
1183 static char route_delims[] = " \t\n";
1185 static int open_route_table (void)
1189 close_route_table();
1191 path = path_to_route();
1195 route_fd = fopen (path, "r");
1196 if (route_fd == NULL) {
1197 syslog (LOG_ERR, "can't open %s: %m (%d)", path, errno);
1201 route_dev_col = 0; /* default to usual columns */
1204 route_flags_col = 3;
1208 /* parse header line */
1209 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
1210 char *p = route_buffer, *q;
1212 for (col = 0; col < ROUTE_MAX_COLS; ++col) {
1214 if ((q = strtok(p, route_delims)) == 0)
1216 if (strcasecmp(q, "iface") == 0)
1217 route_dev_col = col;
1218 else if (strcasecmp(q, "destination") == 0)
1219 route_dest_col = col;
1220 else if (strcasecmp(q, "gateway") == 0)
1222 else if (strcasecmp(q, "flags") == 0)
1223 route_flags_col = col;
1224 else if (strcasecmp(q, "mask") == 0)
1225 route_mask_col = col;
1228 if (used && col >= route_num_cols)
1229 route_num_cols = col + 1;
1237 /********************************************************************
1239 * read_route_table - read the next entry from the route table
1242 static int read_route_table(struct rtentry *rt)
1244 char *cols[ROUTE_MAX_COLS], *p;
1247 memset (rt, '\0', sizeof (struct rtentry));
1249 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1253 for (col = 0; col < route_num_cols; ++col) {
1254 cols[col] = strtok(p, route_delims);
1255 if (cols[col] == NULL)
1256 return 0; /* didn't get enough columns */
1259 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1260 strtoul(cols[route_dest_col], NULL, 16);
1262 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1263 strtoul(cols[route_gw_col], NULL, 16);
1265 ((struct sockaddr_in *) &rt->rt_genmask)->sin_addr.s_addr =
1266 strtoul(cols[route_mask_col], NULL, 16);
1268 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
1269 rt->rt_dev = cols[route_dev_col];
1274 /********************************************************************
1276 * defaultroute_exists - determine if there is a default route
1279 static int defaultroute_exists (struct rtentry *rt)
1283 if (!open_route_table())
1286 while (read_route_table(rt) != 0) {
1287 if ((rt->rt_flags & RTF_UP) == 0)
1290 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L) {
1296 close_route_table();
1301 * have_route_to - determine if the system has any route to
1302 * a given IP address. `addr' is in network byte order.
1303 * Return value is 1 if yes, 0 if no, -1 if don't know.
1304 * For demand mode to work properly, we have to ignore routes
1305 * through our own interface.
1307 int have_route_to(u_int32_t addr)
1312 if (!open_route_table())
1313 return -1; /* don't know */
1315 while (read_route_table(&rt)) {
1316 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
1318 if ((addr & ((struct sockaddr_in *)&rt.rt_genmask)->sin_addr.s_addr)
1319 == ((struct sockaddr_in *)&rt.rt_dst)->sin_addr.s_addr) {
1325 close_route_table();
1329 /********************************************************************
1331 * sifdefaultroute - assign a default route through the address given.
1334 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1338 if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0)
1340 struct in_addr old_gateway =
1341 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1343 if (old_gateway.s_addr != gateway)
1346 "not replacing existing default route to %s [%s]",
1347 rt.rt_dev, inet_ntoa (old_gateway));
1352 memset (&rt, '\0', sizeof (rt));
1353 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1354 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1356 if (kernel_version > KVERSION(2,1,0)) {
1357 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1358 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1361 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1363 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1364 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1366 if ( ! ok_error ( errno ))
1368 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1373 default_route_gateway = gateway;
1377 /********************************************************************
1379 * cifdefaultroute - delete a default route through the address given.
1382 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1386 default_route_gateway = 0;
1388 memset (&rt, '\0', sizeof (rt));
1389 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1390 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1392 if (kernel_version > KVERSION(2,1,0)) {
1393 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1394 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1397 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1399 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1400 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1404 if ( ! ok_error ( errno ))
1407 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1416 /********************************************************************
1418 * sifproxyarp - Make a proxy ARP entry for the peer.
1421 int sifproxyarp (int unit, u_int32_t his_adr)
1423 struct arpreq arpreq;
1425 if (has_proxy_arp == 0)
1427 memset (&arpreq, '\0', sizeof(arpreq));
1429 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1430 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1431 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1433 * Get the hardware address of an interface on the same subnet
1434 * as our local address.
1436 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1438 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1442 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1444 if ( ! ok_error ( errno ))
1446 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1452 proxy_arp_addr = his_adr;
1457 /********************************************************************
1459 * cifproxyarp - Delete the proxy ARP entry for the peer.
1462 int cifproxyarp (int unit, u_int32_t his_adr)
1464 struct arpreq arpreq;
1466 if (has_proxy_arp == 1)
1468 memset (&arpreq, '\0', sizeof(arpreq));
1469 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1470 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1471 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1473 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1475 if ( ! ok_error ( errno ))
1477 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1486 /********************************************************************
1488 * get_ether_addr - get the hardware address of an interface on the
1489 * the same subnet as ipaddr.
1492 static int get_ether_addr (u_int32_t ipaddr,
1493 struct sockaddr *hwaddr,
1496 struct ifreq *ifr, *ifend;
1497 u_int32_t ina, mask;
1500 struct ifreq ifs[MAX_IFS];
1502 ifc.ifc_len = sizeof(ifs);
1504 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1506 if ( ! ok_error ( errno ))
1508 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1513 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1514 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1516 * Scan through looking for an interface with an Internet
1517 * address on the same subnet as `ipaddr'.
1519 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1520 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1522 if (ifr->ifr_addr.sa_family == AF_INET)
1524 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1525 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1526 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1529 * Check that the interface is up, and not point-to-point
1532 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1537 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1542 * Get its netmask and check that it's on the right subnet.
1544 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1549 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1550 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1551 ip_ntoa(ina), ntohl(mask)));
1553 if (((ipaddr ^ ina) & mask) != 0)
1566 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1567 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1569 * Now get the hardware address.
1571 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1572 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1574 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1580 sizeof (struct sockaddr));
1582 SYSDEBUG ((LOG_DEBUG,
1583 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1584 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1585 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1586 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1587 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1588 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1589 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1590 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1591 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1595 /********************************************************************
1597 * Return user specified netmask, modified by any mask we might determine
1598 * for address `addr' (in network byte order).
1599 * Here we scan through the system's list of interfaces, looking for
1600 * any non-point-to-point interfaces which might appear to be on the same
1601 * network as `addr'. If we find any, we OR in their netmask to the
1602 * user-specified netmask.
1605 u_int32_t GetMask (u_int32_t addr)
1607 u_int32_t mask, nmask, ina;
1608 struct ifreq *ifr, *ifend, ifreq;
1610 struct ifreq ifs[MAX_IFS];
1614 if (IN_CLASSA(addr)) /* determine network mask for address class */
1616 nmask = IN_CLASSA_NET;
1620 if (IN_CLASSB(addr))
1622 nmask = IN_CLASSB_NET;
1626 nmask = IN_CLASSC_NET;
1630 /* class D nets are disallowed by bad_ip_adrs */
1631 mask = netmask | htonl(nmask);
1633 * Scan through the system's network interfaces.
1635 ifc.ifc_len = sizeof(ifs);
1637 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1639 if ( ! ok_error ( errno ))
1641 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1646 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1647 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1650 * Check the interface's internet address.
1652 if (ifr->ifr_addr.sa_family != AF_INET)
1656 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1657 if (((ntohl(ina) ^ addr) & nmask) != 0)
1662 * Check that the interface is up, and not point-to-point nor loopback.
1664 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1665 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1670 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1675 * Get its netmask and OR it into our mask.
1677 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1681 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1687 /********************************************************************
1689 * Internal routine to decode the version.modification.patch level
1692 static void decode_version (char *buf, int *version,
1693 int *modification, int *patch)
1695 *version = (int) strtoul (buf, &buf, 10);
1702 *modification = (int) strtoul (buf, &buf, 10);
1706 *patch = (int) strtoul (buf, &buf, 10);
1718 /********************************************************************
1720 * Procedure to determine if the PPP line discipline is registered.
1724 ppp_registered(void)
1730 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1733 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1737 initfdflags = fcntl(local_fd, F_GETFL);
1738 if (initfdflags == -1)
1740 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1745 initfdflags &= ~O_NONBLOCK;
1746 fcntl(local_fd, F_SETFL, initfdflags);
1748 * Read the initial line dicipline and try to put the device into the
1751 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1753 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1758 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1760 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1765 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1767 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1776 /********************************************************************
1778 * ppp_available - check whether the system has any ppp interfaces
1779 * (in fact we check whether we can do an ioctl on ppp0).
1782 int ppp_available(void)
1787 int my_version, my_modification, my_patch;
1788 extern char *no_ppp_msg;
1790 * Open a socket for doing the ioctl operations.
1792 s = socket(AF_INET, SOCK_DGRAM, 0);
1798 strlcpy (ifr.ifr_name, sizeof (ifr.ifr_name), "ppp0");
1799 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1801 * If the device did not exist then attempt to create one by putting the
1802 * current tty into the PPP discipline. If this works then obtain the
1803 * flags for the device again.
1807 if (ppp_registered())
1809 strlcpy (ifr.ifr_name, sizeof (ifr.ifr_name), "ppp0");
1810 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1814 * Ensure that the hardware address is for PPP and not something else
1818 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1821 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1829 "This system lacks kernel support for PPP. This could be because\n"
1830 "the PPP kernel module is not loaded, or because the kernel is\n"
1831 "not configured for PPP. See the README.linux file in the\n"
1832 "ppp-2.3.6 distribution.\n";
1835 * This is the PPP device. Validate the version of the driver at this
1836 * point to ensure that this program will work with the driver.
1840 char abBuffer [1024];
1842 ifr.ifr_data = abBuffer;
1843 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1845 syslog(LOG_ERR, "Couldn't read driver version: %m");
1847 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
1850 decode_version(abBuffer,
1852 &driver_modification,
1855 * Validate the version of the driver against the version that we used.
1857 decode_version(VERSION,
1862 /* The version numbers must match */
1863 if (driver_version != my_version)
1868 /* The modification levels must be legal */
1869 if (driver_modification < 3)
1871 if (driver_modification >= 2) {
1872 /* we can cope with 2.2.0 and above */
1882 sprintf (route_buffer,
1883 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1884 driver_version, driver_modification, driver_patch);
1886 no_ppp_msg = route_buffer;
1893 /********************************************************************
1895 * Update the wtmp file with the appropriate user name and tty device.
1898 void logwtmp (const char *line, const char *name, const char *host)
1901 struct utmp ut, *utp;
1902 pid_t mypid = getpid();
1904 * Update the signon database for users.
1905 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1907 utmpname(_PATH_UTMP);
1909 while ((utp = getutent()) && (utp->ut_pid != mypid))
1912 /* Is this call really necessary? There is another one after the 'put' */
1917 memcpy(&ut, utp, sizeof(ut));
1921 /* some gettys/telnetds don't initialize utmp... */
1922 memset(&ut, 0, sizeof(ut));
1925 if (ut.ut_id[0] == 0)
1927 strlcpy(ut.ut_id, sizeof(ut.ut_id), line + 3);
1930 strlcpy(ut.ut_user, sizeof(ut.ut_user), name);
1931 strlcpy(ut.ut_line, sizeof(ut.ut_line), line);
1935 ut.ut_type = USER_PROCESS;
1938 /* Insert the host name if one is supplied */
1941 strlcpy (ut.ut_host, sizeof(ut.ut_host), host);
1944 /* Insert the IP address of the remote system if IP is enabled */
1945 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1947 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1948 sizeof(ut.ut_addr));
1951 /* CL: Makes sure that the logout works */
1952 if (*host == 0 && *name==0)
1960 * Update the wtmp file.
1962 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1965 flock(wtmp, LOCK_EX);
1967 /* we really should check for error on the write for a full disk! */
1968 write (wtmp, (char *)&ut, sizeof(ut));
1971 flock(wtmp, LOCK_UN);
1975 /********************************************************************
1976 * Code for locking/unlocking the serial device.
1977 * This code is derived from chat.c.
1981 * lock - create a lock file for the named device
1984 int lock (char *dev)
1988 lock_file = strdup(dev);
1989 if (lock_file == NULL)
1990 novm("lock file name");
1991 result = mklock (dev, (void *) 0);
1994 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
2001 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
2009 char hdb_lock_buffer[12];
2015 p = strrchr(dev, '/');
2019 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
2020 lock_file = malloc(l);
2021 if (lock_file == NULL)
2022 novm("lock file name");
2024 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
2026 * Attempt to create the lock file at this point.
2029 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
2033 sprintf (hdb_lock_buffer, "%010d\n", pid);
2034 write (fd, hdb_lock_buffer, 11);
2036 write(fd, &pid, sizeof (pid));
2042 * If the file exists then check to see if the pid is stale
2044 if (errno == EEXIST) {
2045 fd = open(lock_file, O_RDONLY, 0);
2047 if (errno == ENOENT) /* This is just a timing problem. */
2052 /* Read the lock file to find out who has the device locked */
2053 n = read (fd, hdb_lock_buffer, 11);
2056 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2060 /* See the process still exists. */
2063 hdb_lock_buffer[n] = '\0';
2064 sscanf (hdb_lock_buffer, " %d", &pid);
2066 pid = ((int *) hdb_lock_buffer)[0];
2068 if (pid == 0 || pid == getpid()
2069 || (kill(pid, 0) == -1 && errno == ESRCH))
2073 /* If the process does not exist then try to remove the lock */
2074 if (n == 0 && unlink (lock_file) == 0) {
2075 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2080 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2084 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2095 /********************************************************************
2097 * unlock - remove our lockfile
2105 (void) rmlock (lock_file, (void *) 0);
2114 /********************************************************************
2116 * sifvjcomp - config tcp header compression
2119 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2121 u_int x = get_flags();
2125 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2127 if (! ok_error (errno))
2129 syslog (LOG_ERR, "ioctl(PPPIOCSMAXCID): %m(%d)", errno);
2135 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2136 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2142 /********************************************************************
2144 * sifup - Config the interface up and enable IP packets to pass.
2151 memset (&ifr, '\0', sizeof (ifr));
2152 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
2153 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2155 if (! ok_error (errno))
2157 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2162 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2163 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2165 if (! ok_error (errno))
2167 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2175 /********************************************************************
2177 * sifdown - Config the interface down and disable IP.
2186 memset (&ifr, '\0', sizeof (ifr));
2187 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
2188 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2190 if (! ok_error (errno))
2192 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2197 ifr.ifr_flags &= ~IFF_UP;
2198 ifr.ifr_flags |= IFF_POINTOPOINT;
2199 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2201 if (! ok_error (errno))
2203 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2210 /********************************************************************
2212 * sifaddr - Config the interface IP addresses and netmask.
2215 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2221 memset (&ifr, '\0', sizeof (ifr));
2222 memset (&rt, '\0', sizeof (rt));
2224 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2225 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2226 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2228 strlcpy (ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
2230 * Set our IP address
2232 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2233 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2235 if (errno != EEXIST)
2237 if (! ok_error (errno))
2239 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2244 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2249 * Set the gateway address
2251 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2252 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2254 if (! ok_error (errno))
2256 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2262 * For recent kernels, force the netmask to 255.255.255.255.
2264 if (kernel_version >= KVERSION(2,1,16))
2268 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2269 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2271 if (! ok_error (errno))
2273 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2279 * Add the device route
2281 if (kernel_version < KVERSION(2,1,16)) {
2282 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2283 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2286 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2287 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2288 rt.rt_flags = RTF_UP | RTF_HOST;
2290 if (kernel_version > KVERSION(2,1,0)) {
2291 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2292 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2295 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2297 if (! ok_error (errno))
2299 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2307 /********************************************************************
2309 * cifaddr - Clear the interface IP addresses, and delete routes
2310 * through the interface if possible.
2313 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2317 if (kernel_version < KVERSION(2,1,16)) {
2319 * Delete the route through the device
2321 memset (&rt, '\0', sizeof (rt));
2323 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2324 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2327 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2328 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2329 rt.rt_flags = RTF_UP | RTF_HOST;
2331 if (kernel_version > KVERSION(2,1,0)) {
2332 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2333 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2336 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2338 if (still_ppp() && ! ok_error (errno))
2340 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2348 /********************************************************************
2350 * open_loopback - open the device we use for getting packets
2351 * in demand mode. Under Linux, we use our existing fd
2352 * to the ppp driver.
2355 open_ppp_loopback(void)
2358 struct termios tios;
2361 for (i = 0; i < 64; ++i) {
2362 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2363 master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2367 if (master_fd < 0) {
2368 syslog(LOG_ERR, "No free pty for loopback");
2371 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2373 slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2375 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2379 set_ppp_fd(slave_fd);
2381 if (tcgetattr(ppp_fd, &tios) == 0)
2383 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2384 tios.c_cflag |= CS8 | CREAD;
2385 tios.c_iflag = IGNPAR | CLOCAL;
2388 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2390 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2394 flags = fcntl(master_fd, F_GETFL);
2396 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2398 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2401 flags = fcntl(ppp_fd, F_GETFL);
2403 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2405 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2408 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2410 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2414 * Find out which interface we were given.
2416 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2418 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2422 * Enable debug in the driver if requested.
2424 set_kdebugflag (kdebugflag);
2427 /********************************************************************
2429 * restore_loop - reattach the ppp unit to the loopback.
2431 * The kernel ppp driver automatically reattaches the ppp unit to
2432 * the loopback if the serial port is set to a line discipline other
2433 * than ppp, or if it detects a modem hangup. The former will happen
2434 * in disestablish_ppp if the latter hasn't already happened, so we
2435 * shouldn't need to do anything.
2437 * Just to be sure, set the real serial port to the normal discipline.
2443 if (ppp_fd != slave_fd)
2445 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2446 set_ppp_fd(slave_fd);
2450 /********************************************************************
2452 * sifnpmode - Set the mode for handling packets for a given NP.
2456 sifnpmode(u, proto, mode)
2463 npi.protocol = proto;
2465 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2467 if (! ok_error (errno))
2469 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2470 proto, mode, errno);
2471 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2479 /********************************************************************
2481 * sipxfaddr - Config the interface IPX networknumber
2484 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2491 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2493 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2496 if (! ok_error (errno))
2498 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2504 memset (&ifr, '\0', sizeof (ifr));
2505 strlcpy (ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
2507 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2508 sipx->sipx_family = AF_IPX;
2509 sipx->sipx_port = 0;
2510 sipx->sipx_network = htonl (network);
2511 sipx->sipx_type = IPX_FRAME_ETHERII;
2512 sipx->sipx_action = IPX_CRTITF;
2514 * Set the IPX device
2516 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2519 if (errno != EEXIST)
2521 if (! ok_error (errno))
2524 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2529 syslog (LOG_WARNING,
2530 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2539 /********************************************************************
2541 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2542 * are removed and the device is no longer able to pass IPX
2546 int cipxfaddr (int unit)
2553 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2555 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2558 if (! ok_error (errno))
2560 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2566 memset (&ifr, '\0', sizeof (ifr));
2567 strlcpy (ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
2569 sipx->sipx_type = IPX_FRAME_ETHERII;
2570 sipx->sipx_action = IPX_DLTITF;
2571 sipx->sipx_family = AF_IPX;
2573 * Set the IPX device
2575 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2577 if (! ok_error (errno))
2580 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2591 * daemon - Detach us from controlling terminal session.
2594 daemon(nochdir, noclose)
2595 int nochdir, noclose;
2599 if ((pid = fork()) < 0)
2602 exit(0); /* parent dies */
2607 fclose(stdin); /* don't need stdin, stdout, stderr */
2615 * Use the hostname as part of the random number seed.
2624 for (p = hostname; *p != 0; ++p)
2629 /********************************************************************
2631 * sys_check_options - check the options that the user specified
2635 sys_check_options(void)
2638 struct stat stat_buf;
2640 * Disable the IPX protocol if the support is not present in the kernel.
2641 * If we disable it then ensure that IP support is enabled.
2643 while (ipxcp_protent.enabled_flag)
2645 if (path_to_procfs())
2647 strlcat (route_buffer, sizeof(route_buffer), "/net/ipx_interface");
2648 if (lstat (route_buffer, &stat_buf) >= 0)
2653 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2654 ipxcp_protent.enabled_flag = 0;
2655 ipcp_protent.enabled_flag = 1;
2659 if (demand && driver_is_old) {
2660 option_error("demand dialling is not supported by kernel driver version "
2661 "%d.%d.%d", driver_version, driver_modification,