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>
43 /* This is in netdevice.h. However, this compile will fail miserably if
44 you attempt to include netdevice.h because it has so many references
45 to __memcpy functions which it should not attempt to do. So, since I
46 really don't use it, but it must be defined, define it now. */
49 #define MAX_ADDR_LEN 7
53 #include <net/ppp_defs.h>
54 #include <net/if_arp.h>
55 #include <net/if_ppp.h>
56 #include <net/route.h>
57 #include <netinet/if_ether.h>
60 #include <linux/ppp_defs.h>
61 #include <linux/if_arp.h>
62 #include <linux/if_ppp.h>
63 #include <linux/route.h>
64 #include <linux/if_ether.h>
66 #include <netinet/in.h>
71 #include "patchlevel.h"
75 #include <linux/ipx.h>
76 #endif /* IPX_CHANGE */
79 #include <sys/locks.h>
82 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
86 #define ok_error(num) ((num)==EIO)
88 static int tty_disc = N_TTY; /* The TTY discipline */
89 static int ppp_disc = N_PPP; /* The PPP discpline */
90 static int initfdflags = -1; /* Initial file descriptor flags for fd */
91 static int ppp_fd = -1; /* fd which is set to PPP discipline */
92 static int sock_fd = -1; /* socket for doing interface ioctls */
93 static int slave_fd = -1;
94 static int master_fd = -1;
96 static int has_proxy_arp = 0;
97 static int driver_version = 0;
98 static int driver_modification = 0;
99 static int driver_patch = 0;
100 static int driver_is_old = 0;
101 static int restore_term = 0; /* 1 => we've munged the terminal */
102 static struct termios inittermios; /* Initial TTY termios */
104 static char loop_name[20];
105 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
107 static int if_is_up; /* Interface has been marked up */
108 static u_int32_t default_route_gateway; /* Gateway for default route added */
109 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
111 static char *lock_file;
113 static struct utsname utsname; /* for the kernel version */
117 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
118 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
119 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
121 /* Prototypes for procedures local to this file. */
122 static int get_flags (void);
123 static void set_flags (int flags);
124 static int translate_speed (int bps);
125 static int baud_rate_of (int speed);
126 static char *path_to_route (void);
127 static void close_route_table (void);
128 static int open_route_table (void);
129 static int read_route_table (struct rtentry *rt);
130 static int defaultroute_exists (struct rtentry *rt);
131 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
133 static void decode_version (char *buf, int *version, int *mod, int *patch);
135 extern u_char inpacket_buf[]; /* borrowed from main.c */
138 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
142 #define SET_SA_FAMILY(addr, family) \
143 memset ((char *) &(addr), '\0', sizeof(addr)); \
144 addr.sa_family = (family);
147 * Determine if the PPP connection should still be present.
153 #define LOCK_PREFIX "/var/lock/LCK.."
156 static void set_ppp_fd (int new_fd)
158 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
162 static int still_ppp(void)
164 if (!hungup || ppp_fd == slave_fd)
167 set_ppp_fd(slave_fd);
173 /********************************************************************
175 * Functions to read and set the flags value in the device driver
178 static int get_flags (void)
182 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
184 if ( ok_error (errno) )
190 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
195 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
199 /********************************************************************/
201 static void set_flags (int flags)
203 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
205 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
207 if (! ok_error (errno) )
209 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
215 /********************************************************************
217 * sys_init - System-dependent initialization.
222 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
223 setlogmask(LOG_UPTO(LOG_INFO));
226 setlogmask(LOG_UPTO(LOG_DEBUG));
229 /* Get an internet socket for doing socket ioctls. */
230 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
233 if ( ! ok_error ( errno ))
235 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
243 /********************************************************************
245 * sys_cleanup - restore any system state we modified before exiting:
246 * mark the interface down, delete default route and/or proxy arp entry.
247 * This should call die() because it's called from die().
250 void sys_cleanup(void)
254 * Take down the device
261 * Delete any routes through the device.
263 if (default_route_gateway != 0)
265 cifdefaultroute(0, 0, default_route_gateway);
270 cifproxyarp(0, proxy_arp_addr);
274 /********************************************************************
276 * sys_close - Clean up in a child process before execing.
286 /********************************************************************
288 * note_debug_level - note a change in the debug level.
291 void note_debug_level (void)
295 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
296 setlogmask(LOG_UPTO(LOG_DEBUG));
300 setlogmask(LOG_UPTO(LOG_WARNING));
304 /********************************************************************
306 * set_kdebugflag - Define the debugging level for the kernel
309 int set_kdebugflag (int requested_level)
311 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
313 if ( ! ok_error (errno) )
315 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
319 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
324 /********************************************************************
326 * establish_ppp - Turn the serial port into a ppp interface.
329 void establish_ppp (int tty_fd)
333 * The current PPP device will be the tty file.
337 * Ensure that the tty device is in exclusive mode.
339 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
341 if ( ! ok_error ( errno ))
343 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
347 * Demand mode - prime the old ppp device to relinquish the unit.
349 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
351 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
355 * Set the current tty to the PPP discpline
357 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
359 if ( ! ok_error (errno) )
361 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
366 * Find out which interface we were given.
368 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
370 if ( ! ok_error (errno))
372 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
377 * Check that we got the same unit again.
383 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
391 * Enable debug in the driver if requested.
394 set_kdebugflag (kdebugflag);
396 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
397 SC_RCV_EVNP | SC_RCV_ODDP));
399 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
400 driver_version, driver_modification, driver_patch));
402 * Fetch the initial file flags and reset blocking mode on the file.
404 initfdflags = fcntl(ppp_fd, F_GETFL);
406 if (initfdflags == -1 ||
407 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
409 if ( ! ok_error (errno))
412 "Couldn't set device to non-blocking mode: %m");
417 /********************************************************************
419 * disestablish_ppp - Restore the serial port to normal operation.
420 * This shouldn't call die() because it's called from die().
423 void disestablish_ppp(int tty_fd)
429 * Attempt to restore the previous tty settings
434 * Restore the previous line discipline
436 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
438 if ( ! ok_error (errno))
440 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
444 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
446 if ( ! ok_error (errno))
448 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
452 /* Reset non-blocking mode on fd. */
453 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
455 if ( ! ok_error (errno))
458 "Couldn't restore device fd flags: %m");
465 /********************************************************************
467 * clean_check - Fetch the flags for the device and generate
468 * appropriate error messages.
470 void clean_check(void)
477 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
480 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
483 case SC_RCV_B7_0 | SC_RCV_EVNP:
484 case SC_RCV_B7_0 | SC_RCV_ODDP:
485 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
486 s = "all had bit 7 set to 1";
490 case SC_RCV_B7_1 | SC_RCV_EVNP:
491 case SC_RCV_B7_1 | SC_RCV_ODDP:
492 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
493 s = "all had bit 7 set to 0";
497 s = "all had odd parity";
501 s = "all had even parity";
507 syslog(LOG_WARNING, "Receive serial link is not"
509 syslog(LOG_WARNING, "Problem: %s", s);
517 * List of valid speeds.
521 int speed_int, speed_val;
598 /********************************************************************
600 * Translate from bits/second to a speed_t.
603 static int translate_speed (int bps)
605 struct speed *speedp;
609 for (speedp = speeds; speedp->speed_int; speedp++)
611 if (bps == speedp->speed_int)
613 return speedp->speed_val;
616 syslog(LOG_WARNING, "speed %d not supported", bps);
621 /********************************************************************
623 * Translate from a speed_t to bits/second.
626 static int baud_rate_of (int speed)
628 struct speed *speedp;
632 for (speedp = speeds; speedp->speed_int; speedp++)
634 if (speed == speedp->speed_val)
636 return speedp->speed_int;
643 /********************************************************************
645 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
646 * at the requested speed, etc. If `local' is true, set CLOCAL
647 * regardless of whether the modem option was specified.
650 void set_up_tty (int tty_fd, int local)
655 if (tcgetattr(tty_fd, &tios) < 0)
657 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
666 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
667 tios.c_cflag |= CS8 | CREAD | HUPCL;
669 tios.c_iflag = IGNBRK | IGNPAR;
673 tios.c_cc[VTIME] = 0;
677 tios.c_cflag ^= (CLOCAL | HUPCL);
683 tios.c_cflag |= CRTSCTS;
687 tios.c_iflag |= IXON | IXOFF;
688 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
689 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
693 tios.c_cflag &= ~CRTSCTS;
700 speed = translate_speed(inspeed);
703 cfsetospeed (&tios, speed);
704 cfsetispeed (&tios, speed);
707 * We can't proceed if the serial port speed is B0,
708 * since that implies that the serial port is disabled.
712 speed = cfgetospeed(&tios);
715 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
721 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
723 syslog(LOG_ERR, "tcsetattr: %m");
727 baud_rate = baud_rate_of(speed);
731 /********************************************************************
733 * setdtr - control the DTR line on the serial port.
734 * This is called from die(), so it shouldn't call die().
737 void setdtr (int tty_fd, int on)
739 int modembits = TIOCM_DTR;
741 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
744 /********************************************************************
746 * restore_tty - restore the terminal to the saved settings.
749 void restore_tty (int tty_fd)
755 * Turn off echoing, because otherwise we can get into
756 * a loop with the tty and the modem echoing to each other.
757 * We presume we are the sole user of this tty device, so
758 * when we close it, it will revert to its defaults anyway.
762 inittermios.c_lflag &= ~(ECHO | ECHONL);
765 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
767 if (! ok_error (errno))
769 syslog(LOG_WARNING, "tcsetattr: %m");
775 /********************************************************************
777 * output - Output PPP packet.
780 void output (int unit, unsigned char *p, int len)
784 log_packet(p, len, "sent ", LOG_DEBUG);
787 if (write(ppp_fd, p, len) < 0)
789 if (errno == EWOULDBLOCK || errno == ENOBUFS
790 || errno == ENXIO || errno == EIO)
792 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
796 syslog(LOG_ERR, "write: %m(%d)", errno);
802 /********************************************************************
804 * wait_input - wait until there is data available on ppp_fd,
805 * for the length of time specified by *timo (indefinite
809 void wait_input (struct timeval *timo)
815 FD_SET(ppp_fd, &ready);
817 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
818 if (n < 0 && errno != EINTR)
820 syslog(LOG_ERR, "select: %m(%d)", errno);
825 /********************************************************************
827 * wait_loop_output - wait until there is data available on the
828 * loopback, for the length of time specified by *timo (indefinite
831 void wait_loop_output(timo)
832 struct timeval *timo;
838 FD_SET(master_fd, &ready);
839 n = select(master_fd + 1, &ready, NULL, &ready, timo);
840 if (n < 0 && errno != EINTR)
842 syslog(LOG_ERR, "select: %m(%d)", errno);
847 /********************************************************************
849 * wait_time - wait for a given length of time or until a
850 * signal is received.
854 struct timeval *timo;
858 n = select(0, NULL, NULL, NULL, timo);
859 if (n < 0 && errno != EINTR) {
860 syslog(LOG_ERR, "select: %m(%d)", errno);
865 /********************************************************************
867 * read_packet - get a PPP packet from the serial device.
870 int read_packet (unsigned char *buf)
874 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
877 if (errno == EWOULDBLOCK)
881 syslog(LOG_ERR, "read: %m(%d)", errno);
887 /********************************************************************
889 * get_loop_output - get outgoing packets from the ppp device,
890 * and detect when we want to bring the real link up.
891 * Return value is 1 if we need to bring up the link, 0 otherwise.
894 get_loop_output(void)
897 int n = read(master_fd, inbuf, sizeof(inbuf));
901 if (loop_chars(inbuf, n))
905 n = read(master_fd, inbuf, sizeof(inbuf));
910 syslog(LOG_ERR, "eof on loopback");
914 if (errno != EWOULDBLOCK)
916 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
923 /********************************************************************
925 * ppp_send_config - configure the transmit characteristics of
929 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
934 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
936 * Ensure that the link is still up.
941 * Set the MTU and other parameters for the ppp device
943 memset (&ifr, '\0', sizeof (ifr));
944 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
947 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
949 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
953 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
954 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
956 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
961 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
962 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
967 /********************************************************************
969 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
972 void ppp_set_xaccm (int unit, ext_accm accm)
974 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
975 accm[0], accm[1], accm[2], accm[3]));
977 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
979 if ( ! ok_error (errno))
981 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
986 /********************************************************************
988 * ppp_recv_config - configure the receive-side characteristics of
992 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
996 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
998 * If we were called because the link has gone down then there is nothing
999 * which may be done. Just return without incident.
1006 * Set the receiver parameters
1008 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1010 if ( ! ok_error (errno))
1012 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
1016 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
1017 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
1019 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1024 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1028 /********************************************************************
1030 * ccp_test - ask kernel whether a given compression method
1031 * is acceptable for use.
1034 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1036 struct ppp_option_data data;
1038 memset (&data, '\0', sizeof (data));
1040 data.length = opt_len;
1041 data.transmit = for_transmit;
1043 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1048 return (errno == ENOBUFS)? 0: -1;
1051 /********************************************************************
1053 * ccp_flags_set - inform kernel about the current state of CCP.
1056 void ccp_flags_set (int unit, int isopen, int isup)
1060 int x = get_flags();
1061 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1062 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1067 /********************************************************************
1069 * get_idle_time - return how long the link has been idle.
1072 get_idle_time(u, ip)
1074 struct ppp_idle *ip;
1076 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1079 /********************************************************************
1081 * ccp_fatal_error - returns 1 if decompression was disabled as a
1082 * result of an error detected after decompression of a packet,
1083 * 0 otherwise. This is necessary because of patent nonsense.
1086 int ccp_fatal_error (int unit)
1088 int x = get_flags();
1090 return x & SC_DC_FERROR;
1094 * path_to_route - determine the path to the proc file system data
1097 FILE *route_fd = (FILE *) 0;
1098 static char route_buffer [512];
1100 static char *path_to_route (void);
1101 static int open_route_table (void);
1102 static void close_route_table (void);
1103 static int read_route_table (struct rtentry *rt);
1105 /********************************************************************
1107 * path_to_procfs - find the path to the proc file system mount point
1110 static int path_to_procfs (void)
1112 struct mntent *mntent;
1115 fp = fopen (MOUNTED, "r");
1118 mntent = getmntent (fp);
1119 while (mntent != (struct mntent *) 0)
1121 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1123 if (strcmp (mntent->mnt_type, "proc") == 0)
1125 strncpy (route_buffer, mntent->mnt_dir,
1126 sizeof (route_buffer)-10);
1127 route_buffer [sizeof (route_buffer)-10] = '\0';
1132 mntent = getmntent (fp);
1139 /********************************************************************
1141 * path_to_route - find the path to the route tables in the proc file system
1144 static char *path_to_route (void)
1146 if (! path_to_procfs())
1148 syslog (LOG_ERR, "proc file system not mounted");
1151 strcat (route_buffer, "/net/route");
1152 return (route_buffer);
1155 /********************************************************************
1157 * close_route_table - close the interface to the route table
1160 static void close_route_table (void)
1162 if (route_fd != (FILE *) 0)
1165 route_fd = (FILE *) 0;
1169 /********************************************************************
1171 * open_route_table - open the interface to the route table
1174 static int open_route_table (void)
1178 close_route_table();
1180 path = path_to_route();
1186 route_fd = fopen (path, "r");
1187 if (route_fd == (FILE *) 0)
1189 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1195 /********************************************************************
1197 * read_route_table - read the next entry from the route table
1200 static int read_route_table (struct rtentry *rt)
1202 static char delims[] = " \t\n";
1203 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1205 memset (rt, '\0', sizeof (struct rtentry));
1209 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1215 dev_ptr = strtok (route_buffer, delims); /* interface name */
1216 dst_ptr = strtok (NULL, delims); /* destination address */
1217 gw_ptr = strtok (NULL, delims); /* gateway */
1218 flag_ptr = strtok (NULL, delims); /* flags */
1220 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1225 /* Discard that stupid header line which should never
1226 * have been there in the first place !! */
1227 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1233 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1234 strtoul (dst_ptr, NULL, 16);
1236 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1237 strtoul (gw_ptr, NULL, 16);
1239 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1240 rt->rt_dev = dev_ptr;
1245 /********************************************************************
1247 * defaultroute_exists - determine if there is a default route
1250 static int defaultroute_exists (struct rtentry *rt)
1254 if (!open_route_table())
1259 while (read_route_table(rt) != 0)
1261 if ((rt->rt_flags & RTF_UP) == 0)
1266 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1273 close_route_table();
1277 /********************************************************************
1279 * sifdefaultroute - assign a default route through the address given.
1282 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1286 if (defaultroute_exists(&rt))
1288 struct in_addr old_gateway =
1289 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1291 if (old_gateway.s_addr != gateway)
1294 "not replacing existing default route to %s [%s]",
1296 inet_ntoa (old_gateway));
1301 memset (&rt, '\0', sizeof (rt));
1302 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1303 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1305 if (strcmp(utsname.release, "2.1.0") > 0) {
1306 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1307 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1310 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1312 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1313 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1315 if ( ! ok_error ( errno ))
1317 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1322 default_route_gateway = gateway;
1326 /********************************************************************
1328 * cifdefaultroute - delete a default route through the address given.
1331 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1335 default_route_gateway = 0;
1337 memset (&rt, '\0', sizeof (rt));
1338 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1339 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1341 if (strcmp(utsname.release, "2.1.0") > 0) {
1342 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1343 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1346 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1348 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1349 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1353 if ( ! ok_error ( errno ))
1356 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1365 /********************************************************************
1367 * sifproxyarp - Make a proxy ARP entry for the peer.
1370 int sifproxyarp (int unit, u_int32_t his_adr)
1372 struct arpreq arpreq;
1374 if (has_proxy_arp == 0)
1376 memset (&arpreq, '\0', sizeof(arpreq));
1378 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1379 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1380 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1382 * Get the hardware address of an interface on the same subnet
1383 * as our local address.
1385 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1387 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1391 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1393 if ( ! ok_error ( errno ))
1395 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1401 proxy_arp_addr = his_adr;
1406 /********************************************************************
1408 * cifproxyarp - Delete the proxy ARP entry for the peer.
1411 int cifproxyarp (int unit, u_int32_t his_adr)
1413 struct arpreq arpreq;
1415 if (has_proxy_arp == 1)
1417 memset (&arpreq, '\0', sizeof(arpreq));
1418 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1419 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1420 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1422 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1424 if ( ! ok_error ( errno ))
1426 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1435 /********************************************************************
1437 * get_ether_addr - get the hardware address of an interface on the
1438 * the same subnet as ipaddr.
1441 static int get_ether_addr (u_int32_t ipaddr,
1442 struct sockaddr *hwaddr,
1445 struct ifreq *ifr, *ifend, *ifp;
1447 u_int32_t ina, mask;
1450 struct ifreq ifs[MAX_IFS];
1452 ifc.ifc_len = sizeof(ifs);
1454 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1456 if ( ! ok_error ( errno ))
1458 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1463 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1464 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1466 * Scan through looking for an interface with an Internet
1467 * address on the same subnet as `ipaddr'.
1469 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1470 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1472 if (ifr->ifr_addr.sa_family == AF_INET)
1474 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1475 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1476 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1479 * Check that the interface is up, and not point-to-point
1482 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1487 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1492 * Get its netmask and check that it's on the right subnet.
1494 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1499 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1500 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1501 ip_ntoa(ina), ntohl(mask)));
1503 if (((ipaddr ^ ina) & mask) != 0)
1516 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1517 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1519 * Now get the hardware address.
1521 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1522 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1524 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1530 sizeof (struct sockaddr));
1532 SYSDEBUG ((LOG_DEBUG,
1533 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1534 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1535 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1536 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1537 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1538 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1539 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1540 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1541 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1545 /********************************************************************
1547 * Return user specified netmask, modified by any mask we might determine
1548 * for address `addr' (in network byte order).
1549 * Here we scan through the system's list of interfaces, looking for
1550 * any non-point-to-point interfaces which might appear to be on the same
1551 * network as `addr'. If we find any, we OR in their netmask to the
1552 * user-specified netmask.
1555 u_int32_t GetMask (u_int32_t addr)
1557 u_int32_t mask, nmask, ina;
1558 struct ifreq *ifr, *ifend, ifreq;
1560 struct ifreq ifs[MAX_IFS];
1564 if (IN_CLASSA(addr)) /* determine network mask for address class */
1566 nmask = IN_CLASSA_NET;
1570 if (IN_CLASSB(addr))
1572 nmask = IN_CLASSB_NET;
1576 nmask = IN_CLASSC_NET;
1580 /* class D nets are disallowed by bad_ip_adrs */
1581 mask = netmask | htonl(nmask);
1583 * Scan through the system's network interfaces.
1585 ifc.ifc_len = sizeof(ifs);
1587 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1589 if ( ! ok_error ( errno ))
1591 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1596 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1597 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1600 * Check the interface's internet address.
1602 if (ifr->ifr_addr.sa_family != AF_INET)
1606 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1607 if (((ntohl(ina) ^ addr) & nmask) != 0)
1612 * Check that the interface is up, and not point-to-point nor loopback.
1614 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1615 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1620 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1625 * Get its netmask and OR it into our mask.
1627 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1631 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1637 /********************************************************************
1639 * Internal routine to decode the version.modification.patch level
1642 static void decode_version (char *buf, int *version,
1643 int *modification, int *patch)
1645 *version = (int) strtoul (buf, &buf, 10);
1652 *modification = (int) strtoul (buf, &buf, 10);
1656 *patch = (int) strtoul (buf, &buf, 10);
1668 /********************************************************************
1670 * Procedure to determine if the PPP line discipline is registered.
1674 ppp_registered(void)
1680 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1683 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1687 initfdflags = fcntl(local_fd, F_GETFL);
1688 if (initfdflags == -1)
1690 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1695 initfdflags &= ~O_NONBLOCK;
1696 fcntl(local_fd, F_SETFL, initfdflags);
1698 * Read the initial line dicipline and try to put the device into the
1701 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1703 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1708 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1710 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1715 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1717 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1726 /********************************************************************
1728 * ppp_available - check whether the system has any ppp interfaces
1729 * (in fact we check whether we can do an ioctl on ppp0).
1732 int ppp_available(void)
1737 int my_version, my_modification, my_patch;
1738 extern char *no_ppp_msg;
1740 * Open a socket for doing the ioctl operations.
1742 s = socket(AF_INET, SOCK_DGRAM, 0);
1748 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1749 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1751 * If the device did not exist then attempt to create one by putting the
1752 * current tty into the PPP discipline. If this works then obtain the
1753 * flags for the device again.
1757 if (ppp_registered())
1759 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1760 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1764 * Ensure that the hardware address is for PPP and not something else
1768 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1771 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1779 "This system lacks kernel support for PPP. This could be because\n"
1780 "the PPP kernel module is not loaded, or because the kernel is\n"
1781 "not configured for PPP. See the README.linux file in the\n"
1782 "ppp-2.3.3 distribution.\n";
1785 * This is the PPP device. Validate the version of the driver at this
1786 * point to ensure that this program will work with the driver.
1790 char abBuffer [1024];
1792 ifr.ifr_data = abBuffer;
1793 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1798 decode_version (abBuffer,
1800 &driver_modification,
1807 driver_modification =
1811 * Validate the version of the driver against the version that we used.
1813 decode_version (VERSION,
1818 /* The version numbers must match */
1819 if (driver_version != my_version)
1824 /* The modification levels must be legal */
1825 if (driver_modification < my_modification)
1827 if (driver_modification >= 2) {
1828 /* we can cope with 2.2.0 and above */
1838 sprintf (route_buffer,
1839 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1840 driver_version, driver_modification, driver_patch);
1842 no_ppp_msg = route_buffer;
1848 /********************************************************************
1850 * Update the wtmp file with the appropriate user name and tty device.
1853 void logwtmp (const char *line, const char *name, const char *host)
1856 struct utmp ut, *utp;
1857 pid_t mypid = getpid();
1859 * Update the signon database for users.
1860 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1862 utmpname(_PATH_UTMP);
1864 while ((utp = getutent()) && (utp->ut_pid != mypid))
1867 /* Is this call really necessary? There is another one after the 'put' */
1872 memcpy(&ut, utp, sizeof(ut));
1876 /* some gettys/telnetds don't initialize utmp... */
1877 memset(&ut, 0, sizeof(ut));
1880 if (ut.ut_id[0] == 0)
1882 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1885 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1886 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1890 ut.ut_type = USER_PROCESS;
1893 /* Insert the host name if one is supplied */
1896 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1899 /* Insert the IP address of the remote system if IP is enabled */
1900 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1902 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1903 sizeof(ut.ut_addr));
1906 /* CL: Makes sure that the logout works */
1907 if (*host == 0 && *name==0)
1915 * Update the wtmp file.
1917 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1920 flock(wtmp, LOCK_EX);
1922 /* we really should check for error on the write for a full disk! */
1923 write (wtmp, (char *)&ut, sizeof(ut));
1926 flock(wtmp, LOCK_UN);
1930 /********************************************************************
1931 * Code for locking/unlocking the serial device.
1932 * This code is derived from chat.c.
1936 * lock - create a lock file for the named device
1939 int lock (char *dev)
1943 lock_file = malloc(strlen(dev) + 1);
1944 if (lock_file == NULL)
1946 novm("lock file name");
1948 strcpy (lock_file, dev);
1949 result = mklock (dev, (void *) 0);
1953 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1962 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1970 char hdb_lock_buffer[12];
1975 p = strrchr(dev, '/');
1981 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1982 if (lock_file == NULL)
1984 novm("lock file name");
1987 strcpy (lock_file, LOCK_PREFIX);
1988 strcat (lock_file, dev);
1990 * Attempt to create the lock file at this point.
1994 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1999 sprintf (hdb_lock_buffer, "%010d\n", pid);
2000 write (fd, hdb_lock_buffer, 11);
2002 write(fd, &pid, sizeof (pid));
2008 * If the file exists then check to see if the pid is stale
2010 if (errno == EEXIST)
2012 fd = open(lock_file, O_RDONLY, 0);
2015 if (errno == ENOENT) /* This is just a timing problem. */
2022 /* Read the lock file to find out who has the device locked */
2023 n = read (fd, hdb_lock_buffer, 11);
2027 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2031 /* See the process still exists. */
2035 hdb_lock_buffer[n] = '\0';
2036 sscanf (hdb_lock_buffer, " %d", &pid);
2038 pid = ((int *) hdb_lock_buffer)[0];
2040 if (pid == 0 || pid == getpid()
2041 || (kill(pid, 0) == -1 && errno == ESRCH))
2047 /* If the process does not exist then try to remove the lock */
2048 if (n == 0 && unlink (lock_file) == 0)
2050 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2055 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2059 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2070 /********************************************************************
2072 * unlock - remove our lockfile
2080 (void) rmlock (lock_file, (void *) 0);
2089 /********************************************************************
2091 * sifvjcomp - config tcp header compression
2094 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2096 u_int x = get_flags();
2100 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2102 if (! ok_error (errno))
2104 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno);
2110 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2111 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2117 /********************************************************************
2119 * sifup - Config the interface up and enable IP packets to pass.
2126 memset (&ifr, '\0', sizeof (ifr));
2127 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2128 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2130 if (! ok_error (errno))
2132 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2137 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2138 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2140 if (! ok_error (errno))
2142 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2150 /********************************************************************
2152 * sifdown - Config the interface down and disable IP.
2161 memset (&ifr, '\0', sizeof (ifr));
2162 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2163 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2165 if (! ok_error (errno))
2167 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2172 ifr.ifr_flags &= ~IFF_UP;
2173 ifr.ifr_flags |= IFF_POINTOPOINT;
2174 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2176 if (! ok_error (errno))
2178 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2185 /********************************************************************
2187 * sifaddr - Config the interface IP addresses and netmask.
2190 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2196 memset (&ifr, '\0', sizeof (ifr));
2197 memset (&rt, '\0', sizeof (rt));
2199 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2200 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2201 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2203 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2205 * Set our IP address
2207 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2208 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2210 if (errno != EEXIST)
2212 if (! ok_error (errno))
2214 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2219 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2224 * Set the gateway address
2226 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2227 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2229 if (! ok_error (errno))
2231 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2237 * For recent kernels, force the netmask to 255.255.255.255.
2239 if (strcmp(utsname.release, "2.1.16") >= 0)
2243 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2244 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2246 if (! ok_error (errno))
2248 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2254 * Add the device route
2256 if (strcmp(utsname.release, "2.1.16") < 0) {
2257 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2258 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2261 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2262 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2263 rt.rt_flags = RTF_UP | RTF_HOST;
2265 if (strcmp(utsname.release, "2.1.0") > 0) {
2266 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2267 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2270 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2272 if (! ok_error (errno))
2274 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2282 /********************************************************************
2284 * cifaddr - Clear the interface IP addresses, and delete routes
2285 * through the interface if possible.
2288 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2292 if (strcmp(utsname.release, "2.1.16") < 0) {
2294 * Delete the route through the device
2296 memset (&rt, '\0', sizeof (rt));
2298 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2299 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2302 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2303 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2304 rt.rt_flags = RTF_UP | RTF_HOST;
2306 if (strcmp(utsname.release, "2.1.0") > 0) {
2307 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2308 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2311 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2313 if (still_ppp() && ! ok_error (errno))
2315 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2323 /********************************************************************
2325 * open_loopback - open the device we use for getting packets
2326 * in demand mode. Under Linux, we use our existing fd
2327 * to the ppp driver.
2330 open_ppp_loopback(void)
2333 struct termios tios;
2336 for (i = 0; i < 64; ++i) {
2337 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2338 master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2342 if (master_fd < 0) {
2343 syslog(LOG_ERR, "No free pty for loopback");
2346 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2348 slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2350 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2354 set_ppp_fd(slave_fd);
2356 if (tcgetattr(ppp_fd, &tios) == 0)
2358 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2359 tios.c_cflag |= CS8 | CREAD;
2360 tios.c_iflag = IGNPAR | CLOCAL;
2363 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2365 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2369 flags = fcntl(master_fd, F_GETFL);
2371 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2373 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2376 flags = fcntl(ppp_fd, F_GETFL);
2378 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2380 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2383 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2385 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2389 * Find out which interface we were given.
2391 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2393 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2397 * Enable debug in the driver if requested.
2399 set_kdebugflag (kdebugflag);
2402 /********************************************************************
2404 * restore_loop - reattach the ppp unit to the loopback.
2406 * The kernel ppp driver automatically reattaches the ppp unit to
2407 * the loopback if the serial port is set to a line discipline other
2408 * than ppp, or if it detects a modem hangup. The former will happen
2409 * in disestablish_ppp if the latter hasn't already happened, so we
2410 * shouldn't need to do anything.
2412 * Just to be sure, set the real serial port to the normal discipline.
2418 if (ppp_fd != slave_fd)
2420 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2421 set_ppp_fd(slave_fd);
2425 /********************************************************************
2427 * sifnpmode - Set the mode for handling packets for a given NP.
2431 sifnpmode(u, proto, mode)
2438 npi.protocol = proto;
2440 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2442 if (! ok_error (errno))
2444 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2445 proto, mode, errno);
2446 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2454 /********************************************************************
2456 * sipxfaddr - Config the interface IPX networknumber
2459 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2465 struct sockaddr_ipx ipx_addr;
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)
2528 struct sockaddr_ipx ipx_addr;
2530 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2532 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2535 if (! ok_error (errno))
2537 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2543 memset (&ifr, '\0', sizeof (ifr));
2544 strcpy (ifr.ifr_name, ifname);
2546 sipx->sipx_type = IPX_FRAME_ETHERII;
2547 sipx->sipx_action = IPX_DLTITF;
2548 sipx->sipx_family = AF_IPX;
2550 * Set the IPX device
2552 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2554 if (! ok_error (errno))
2557 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2568 * daemon - Detach us from controlling terminal session.
2571 daemon(nochdir, noclose)
2572 int nochdir, noclose;
2576 if ((pid = fork()) < 0)
2579 exit(0); /* parent dies */
2584 fclose(stdin); /* don't need stdin, stdout, stderr */
2591 /********************************************************************
2593 * sys_check_options - check the options that the user specified
2597 sys_check_options(void)
2600 struct stat stat_buf;
2602 * Disable the IPX protocol if the support is not present in the kernel.
2603 * If we disable it then ensure that IP support is enabled.
2605 while (ipxcp_protent.enabled_flag)
2607 if (path_to_procfs())
2609 strcat (route_buffer, "/net/ipx_interface");
2610 if (lstat (route_buffer, &stat_buf) >= 0)
2615 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2616 ipxcp_protent.enabled_flag = 0;
2617 ipcp_protent.enabled_flag = 1;
2621 if (demand && driver_is_old) {
2622 option_error("demand dialling is not supported by kernel driver version "
2623 "%d.%d.%d", driver_version, driver_modification,