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>
41 /* This is in netdevice.h. However, this compile will fail miserably if
42 you attempt to include netdevice.h because it has so many references
43 to __memcpy functions which it should not attempt to do. So, since I
44 really don't use it, but it must be defined, define it now. */
47 #define MAX_ADDR_LEN 7
50 #include <linux/version.h>
52 #include <linux/ppp_defs.h>
53 #include <net/if_arp.h>
54 #include <linux/if_ppp.h>
55 #include <net/route.h>
56 #include <linux/if_ether.h>
57 #include <netinet/in.h>
63 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
72 #include <sys/locks.h>
75 #define ok_error(num) ((num)==EIO)
77 static int tty_disc = N_TTY; /* The TTY discipline */
78 static int ppp_disc = N_PPP; /* The PPP discpline */
79 static int initfdflags = -1; /* Initial file descriptor flags for fd */
80 static int ppp_fd = -1; /* fd which is set to PPP discipline */
81 static int sock_fd = -1; /* socket for doing interface ioctls */
82 static int slave_fd = -1;
83 static int master_fd = -1;
85 static int has_proxy_arp = 0;
86 static int driver_version = 0;
87 static int driver_modification = 0;
88 static int driver_patch = 0;
89 static int restore_term = 0; /* 1 => we've munged the terminal */
90 static struct termios inittermios; /* Initial TTY termios */
92 static char loop_name[20];
93 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
95 static int if_is_up; /* Interface has been marked up */
96 static u_int32_t default_route_gateway; /* Gateway for default route added */
97 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
99 static char *lock_file;
103 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
104 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
105 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
107 /* Prototypes for procedures local to this file. */
108 static int get_flags (void);
109 static void set_flags (int flags);
110 static int translate_speed (int bps);
111 static int baud_rate_of (int speed);
112 static char *path_to_route (void);
113 static void close_route_table (void);
114 static int open_route_table (void);
115 static int read_route_table (struct rtentry *rt);
116 static int defaultroute_exists (struct rtentry *rt);
117 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
119 static void decode_version (char *buf, int *version, int *mod, int *patch);
121 extern u_char inpacket_buf[]; /* borrowed from main.c */
124 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
128 #define SET_SA_FAMILY(addr, family) \
129 memset ((char *) &(addr), '\0', sizeof(addr)); \
130 addr.sa_family = (family);
133 * Determine if the PPP connection should still be present.
137 #define still_ppp() (hungup == 0)
140 #define LOCK_PREFIX "/var/lock/LCK.."
143 /********************************************************************
145 * Functions to read and set the flags value in the device driver
148 static void set_ppp_fd (int new_fd)
150 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
154 /********************************************************************
156 * Functions to read and set the flags value in the device driver
159 static int get_flags (void)
163 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
165 if ( ok_error (errno) )
171 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
176 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
180 /********************************************************************/
182 static void set_flags (int flags)
184 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
186 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
188 if (! ok_error (errno) )
190 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
196 /********************************************************************
198 * sys_init - System-dependent initialization.
203 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
204 setlogmask(LOG_UPTO(LOG_INFO));
207 setlogmask(LOG_UPTO(LOG_DEBUG));
210 /* Get an internet socket for doing socket ioctls. */
211 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
214 if ( ! ok_error ( errno ))
216 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
222 /********************************************************************
224 * sys_cleanup - restore any system state we modified before exiting:
225 * mark the interface down, delete default route and/or proxy arp entry.
226 * This should call die() because it's called from die().
229 void sys_cleanup(void)
233 * Take down the device
240 * Delete any routes through the device.
242 if (default_route_gateway != 0)
244 cifdefaultroute(0, 0, default_route_gateway);
249 cifproxyarp(0, proxy_arp_addr);
253 /********************************************************************
255 * sys_close - Clean up in a child process before execing.
265 /********************************************************************
267 * note_debug_level - note a change in the debug level.
270 void note_debug_level (void)
274 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
275 setlogmask(LOG_UPTO(LOG_DEBUG));
279 setlogmask(LOG_UPTO(LOG_WARNING));
283 /********************************************************************
285 * set_kdebugflag - Define the debugging level for the kernel
288 int set_kdebugflag (int requested_level)
290 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
292 if ( ! ok_error (errno) )
294 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
298 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
303 /********************************************************************
305 * establish_ppp - Turn the serial port into a ppp interface.
308 void establish_ppp (int tty_fd)
312 * The current PPP device will be the tty file.
316 * Ensure that the tty device is in exclusive mode.
318 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
320 if ( ! ok_error ( errno ))
322 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
326 * Demand mode - prime the old ppp device to relinquish the unit.
328 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
330 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
334 * Set the current tty to the PPP discpline
336 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
338 if ( ! ok_error (errno) )
340 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
345 * Find out which interface we were given.
347 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
349 if ( ! ok_error (errno))
351 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
356 * Check that we got the same unit again.
362 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
370 * Enable debug in the driver if requested.
373 set_kdebugflag (kdebugflag);
375 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
376 SC_RCV_EVNP | SC_RCV_ODDP));
378 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
379 driver_version, driver_modification, driver_patch));
381 * Fetch the initial file flags and reset blocking mode on the file.
383 initfdflags = fcntl(ppp_fd, F_GETFL);
385 if (initfdflags == -1 ||
386 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
388 if ( ! ok_error (errno))
391 "Couldn't set device to non-blocking mode: %m");
396 /********************************************************************
398 * disestablish_ppp - Restore the serial port to normal operation.
399 * This shouldn't call die() because it's called from die().
402 void disestablish_ppp(int tty_fd)
408 * Attempt to restore the previous tty settings
413 * Restore the previous line discipline
415 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
417 if ( ! ok_error (errno))
419 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
423 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
425 if ( ! ok_error (errno))
427 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
431 /* Reset non-blocking mode on fd. */
432 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
434 if ( ! ok_error (errno))
437 "Couldn't restore device fd flags: %m");
444 /********************************************************************
446 * clean_check - Fetch the flags for the device and generate
447 * appropriate error messages.
449 void clean_check(void)
456 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
459 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
462 case SC_RCV_B7_0 | SC_RCV_EVNP:
463 case SC_RCV_B7_0 | SC_RCV_ODDP:
464 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
465 s = "all had bit 7 set to 1";
469 case SC_RCV_B7_1 | SC_RCV_EVNP:
470 case SC_RCV_B7_1 | SC_RCV_ODDP:
471 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
472 s = "all had bit 7 set to 0";
476 s = "all had odd parity";
480 s = "all had even parity";
486 syslog(LOG_WARNING, "Receive serial link is not"
488 syslog(LOG_WARNING, "Problem: %s", s);
496 * List of valid speeds.
500 int speed_int, speed_val;
571 /********************************************************************
573 * Translate from bits/second to a speed_t.
576 static int translate_speed (int bps)
578 struct speed *speedp;
582 for (speedp = speeds; speedp->speed_int; speedp++)
584 if (bps == speedp->speed_int)
586 return speedp->speed_val;
589 syslog(LOG_WARNING, "speed %d not supported", bps);
594 /********************************************************************
596 * Translate from a speed_t to bits/second.
599 static int baud_rate_of (int speed)
601 struct speed *speedp;
605 for (speedp = speeds; speedp->speed_int; speedp++)
607 if (speed == speedp->speed_val)
609 return speedp->speed_int;
616 /********************************************************************
618 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
619 * at the requested speed, etc. If `local' is true, set CLOCAL
620 * regardless of whether the modem option was specified.
623 void set_up_tty (int tty_fd, int local)
628 if (tcgetattr(tty_fd, &tios) < 0)
630 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
639 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
640 tios.c_cflag |= CS8 | CREAD | HUPCL;
642 tios.c_iflag = IGNBRK | IGNPAR;
646 tios.c_cc[VTIME] = 0;
650 tios.c_cflag ^= (CLOCAL | HUPCL);
656 tios.c_cflag |= CRTSCTS;
660 tios.c_iflag |= IXON | IXOFF;
661 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
662 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
666 tios.c_cflag &= ~CRTSCTS;
673 speed = translate_speed(inspeed);
676 cfsetospeed (&tios, speed);
677 cfsetispeed (&tios, speed);
680 * We can't proceed if the serial port speed is B0,
681 * since that implies that the serial port is disabled.
685 speed = cfgetospeed(&tios);
688 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
694 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
696 syslog(LOG_ERR, "tcsetattr: %m");
700 baud_rate = baud_rate_of(speed);
704 /********************************************************************
706 * setdtr - control the DTR line on the serial port.
707 * This is called from die(), so it shouldn't call die().
710 void setdtr (int tty_fd, int on)
712 int modembits = TIOCM_DTR;
714 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
717 /********************************************************************
719 * restore_tty - restore the terminal to the saved settings.
722 void restore_tty (int tty_fd)
728 * Turn off echoing, because otherwise we can get into
729 * a loop with the tty and the modem echoing to each other.
730 * We presume we are the sole user of this tty device, so
731 * when we close it, it will revert to its defaults anyway.
735 inittermios.c_lflag &= ~(ECHO | ECHONL);
738 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
740 if (! ok_error (errno))
742 syslog(LOG_WARNING, "tcsetattr: %m");
748 /********************************************************************
750 * output - Output PPP packet.
753 void output (int unit, unsigned char *p, int len)
757 log_packet(p, len, "sent ");
760 if (write(ppp_fd, p, len) < 0)
762 if (errno == EWOULDBLOCK || errno == ENOBUFS
763 || errno == ENXIO || errno == EIO)
765 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
769 syslog(LOG_ERR, "write: %m(%d)", errno);
775 /********************************************************************
777 * wait_input - wait until there is data available on ppp_fd,
778 * for the length of time specified by *timo (indefinite
782 void wait_input (struct timeval *timo)
788 FD_SET(ppp_fd, &ready);
790 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
791 if (n < 0 && errno != EINTR)
793 syslog(LOG_ERR, "select: %m(%d)", errno);
798 /********************************************************************
800 * wait_loop_output - wait until there is data available on the
801 * loopback, for the length of time specified by *timo (indefinite
804 void wait_loop_output(timo)
805 struct timeval *timo;
811 FD_SET(master_fd, &ready);
812 n = select(master_fd + 1, &ready, NULL, &ready, timo);
813 if (n < 0 && errno != EINTR)
815 syslog(LOG_ERR, "select: %m(%d)", errno);
820 /********************************************************************
822 * wait_time - wait for a given length of time or until a
823 * signal is received.
827 struct timeval *timo;
831 n = select(0, NULL, NULL, NULL, timo);
832 if (n < 0 && errno != EINTR) {
833 syslog(LOG_ERR, "select: %m(%d)", errno);
838 /********************************************************************
840 * read_packet - get a PPP packet from the serial device.
843 int read_packet (unsigned char *buf)
847 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
850 if (errno == EWOULDBLOCK)
854 syslog(LOG_ERR, "read: %m(%d)", errno);
860 /********************************************************************
862 * get_loop_output - get outgoing packets from the ppp device,
863 * and detect when we want to bring the real link up.
864 * Return value is 1 if we need to bring up the link, 0 otherwise.
867 get_loop_output(void)
870 int n = read(master_fd, inbuf, sizeof(inbuf));
874 if (loop_chars(inbuf, n))
878 n = read(master_fd, inbuf, sizeof(inbuf));
883 syslog(LOG_ERR, "eof on loopback");
887 if (errno != EWOULDBLOCK)
889 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
896 /********************************************************************
898 * ppp_send_config - configure the transmit characteristics of
902 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
907 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
909 * Ensure that the link is still up.
914 * Set the MTU and other parameters for the ppp device
916 memset (&ifr, '\0', sizeof (ifr));
917 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
920 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
922 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
926 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
927 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
929 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
934 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
935 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
940 /********************************************************************
942 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
945 void ppp_set_xaccm (int unit, ext_accm accm)
947 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
948 accm[0], accm[1], accm[2], accm[3]));
950 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
952 if ( ! ok_error (errno))
954 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
959 /********************************************************************
961 * ppp_recv_config - configure the receive-side characteristics of
965 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
969 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
971 * If we were called because the link has gone down then there is nothing
972 * which may be done. Just return without incident.
979 * Set the receiver parameters
981 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
983 if ( ! ok_error (errno))
985 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
989 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
990 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
992 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
997 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1001 /********************************************************************
1003 * ccp_test - ask kernel whether a given compression method
1004 * is acceptable for use.
1007 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1009 struct ppp_option_data data;
1011 memset (&data, '\0', sizeof (data));
1013 data.length = opt_len;
1014 data.transmit = for_transmit;
1016 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1021 return (errno == ENOBUFS)? 0: -1;
1024 /********************************************************************
1026 * ccp_flags_set - inform kernel about the current state of CCP.
1029 void ccp_flags_set (int unit, int isopen, int isup)
1033 int x = get_flags();
1034 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1035 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1040 /********************************************************************
1042 * get_idle_time - return how long the link has been idle.
1045 get_idle_time(u, ip)
1047 struct ppp_idle *ip;
1049 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1052 /********************************************************************
1054 * ccp_fatal_error - returns 1 if decompression was disabled as a
1055 * result of an error detected after decompression of a packet,
1056 * 0 otherwise. This is necessary because of patent nonsense.
1059 int ccp_fatal_error (int unit)
1061 int x = get_flags();
1063 return x & SC_DC_FERROR;
1067 * path_to_route - determine the path to the proc file system data
1070 FILE *route_fd = (FILE *) 0;
1071 static char route_buffer [512];
1073 static char *path_to_route (void);
1074 static int open_route_table (void);
1075 static void close_route_table (void);
1076 static int read_route_table (struct rtentry *rt);
1078 /********************************************************************
1080 * path_to_procfs - find the path to the proc file system mount point
1083 static int path_to_procfs (void)
1085 struct mntent *mntent;
1088 fp = fopen (MOUNTED, "r");
1091 mntent = getmntent (fp);
1092 while (mntent != (struct mntent *) 0)
1094 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1096 if (strcmp (mntent->mnt_type, "proc") == 0)
1098 strncpy (route_buffer, mntent->mnt_dir,
1099 sizeof (route_buffer)-10);
1100 route_buffer [sizeof (route_buffer)-10] = '\0';
1105 mntent = getmntent (fp);
1112 /********************************************************************
1114 * path_to_route - find the path to the route tables in the proc file system
1117 static char *path_to_route (void)
1119 if (! path_to_procfs())
1121 syslog (LOG_ERR, "proc file system not mounted");
1124 strcat (route_buffer, "/net/route");
1125 return (route_buffer);
1128 /********************************************************************
1130 * close_route_table - close the interface to the route table
1133 static void close_route_table (void)
1135 if (route_fd != (FILE *) 0)
1138 route_fd = (FILE *) 0;
1142 /********************************************************************
1144 * open_route_table - open the interface to the route table
1147 static int open_route_table (void)
1151 close_route_table();
1153 path = path_to_route();
1159 route_fd = fopen (path, "r");
1160 if (route_fd == (FILE *) 0)
1162 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1168 /********************************************************************
1170 * read_route_table - read the next entry from the route table
1173 static int read_route_table (struct rtentry *rt)
1175 static char delims[] = " \t\n";
1176 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1178 memset (rt, '\0', sizeof (struct rtentry));
1182 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1188 dev_ptr = strtok (route_buffer, delims); /* interface name */
1189 dst_ptr = strtok (NULL, delims); /* destination address */
1190 gw_ptr = strtok (NULL, delims); /* gateway */
1191 flag_ptr = strtok (NULL, delims); /* flags */
1193 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1198 /* Discard that stupid header line which should never
1199 * have been there in the first place !! */
1200 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1206 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1207 strtoul (dst_ptr, NULL, 16);
1209 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1210 strtoul (gw_ptr, NULL, 16);
1212 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1213 rt->rt_dev = dev_ptr;
1218 /********************************************************************
1220 * defaultroute_exists - determine if there is a default route
1223 static int defaultroute_exists (struct rtentry *rt)
1227 if (!open_route_table())
1232 while (read_route_table(rt) != 0)
1234 if ((rt->rt_flags & RTF_UP) == 0)
1239 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1246 close_route_table();
1250 /********************************************************************
1252 * sifdefaultroute - assign a default route through the address given.
1255 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1259 if (defaultroute_exists(&rt))
1261 u_int32_t old_gateway = ((struct sockaddr_in *) (&rt.rt_gateway))->
1264 if (old_gateway != gateway)
1267 "ppp not replacing existing default route to %s[%s]",
1269 inet_ntoa (old_gateway));
1274 memset (&rt, '\0', sizeof (rt));
1275 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1276 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1278 #if LINUX_VERSION_CODE > 0x020100
1279 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1280 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1283 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1285 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1286 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1288 if ( ! ok_error ( errno ))
1290 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1295 default_route_gateway = gateway;
1299 /********************************************************************
1301 * cifdefaultroute - delete a default route through the address given.
1304 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1308 default_route_gateway = 0;
1310 memset (&rt, '\0', sizeof (rt));
1311 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1312 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1314 #if LINUX_VERSION_CODE > 0x020100
1315 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1316 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1319 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1321 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1322 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1326 if ( ! ok_error ( errno ))
1329 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1338 /********************************************************************
1340 * sifproxyarp - Make a proxy ARP entry for the peer.
1343 int sifproxyarp (int unit, u_int32_t his_adr)
1345 struct arpreq arpreq;
1347 if (has_proxy_arp == 0)
1349 memset (&arpreq, '\0', sizeof(arpreq));
1351 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1352 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1353 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1355 * Get the hardware address of an interface on the same subnet
1356 * as our local address.
1358 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1360 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1364 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1366 if ( ! ok_error ( errno ))
1368 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1374 proxy_arp_addr = his_adr;
1379 /********************************************************************
1381 * cifproxyarp - Delete the proxy ARP entry for the peer.
1384 int cifproxyarp (int unit, u_int32_t his_adr)
1386 struct arpreq arpreq;
1388 if (has_proxy_arp == 1)
1390 memset (&arpreq, '\0', sizeof(arpreq));
1391 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1392 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1393 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1395 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1397 if ( ! ok_error ( errno ))
1399 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1408 /********************************************************************
1410 * get_ether_addr - get the hardware address of an interface on the
1411 * the same subnet as ipaddr.
1414 static int get_ether_addr (u_int32_t ipaddr,
1415 struct sockaddr *hwaddr,
1418 struct ifreq *ifr, *ifend, *ifp;
1420 u_int32_t ina, mask;
1423 struct ifreq ifs[MAX_IFS];
1425 ifc.ifc_len = sizeof(ifs);
1427 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1429 if ( ! ok_error ( errno ))
1431 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1436 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1437 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1439 * Scan through looking for an interface with an Internet
1440 * address on the same subnet as `ipaddr'.
1442 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1443 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1445 if (ifr->ifr_addr.sa_family == AF_INET)
1447 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1448 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1449 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1452 * Check that the interface is up, and not point-to-point
1455 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1460 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1465 * Get its netmask and check that it's on the right subnet.
1467 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1472 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1473 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1474 ip_ntoa(ina), ntohl(mask)));
1476 if (((ipaddr ^ ina) & mask) != 0)
1489 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1490 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1492 * Now get the hardware address.
1494 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1495 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1497 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1503 sizeof (struct sockaddr));
1505 SYSDEBUG ((LOG_DEBUG,
1506 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1507 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1508 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1509 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1510 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1511 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1512 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1513 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1514 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1518 /********************************************************************
1520 * Return user specified netmask, modified by any mask we might determine
1521 * for address `addr' (in network byte order).
1522 * Here we scan through the system's list of interfaces, looking for
1523 * any non-point-to-point interfaces which might appear to be on the same
1524 * network as `addr'. If we find any, we OR in their netmask to the
1525 * user-specified netmask.
1528 u_int32_t GetMask (u_int32_t addr)
1530 u_int32_t mask, nmask, ina;
1531 struct ifreq *ifr, *ifend, ifreq;
1533 struct ifreq ifs[MAX_IFS];
1537 if (IN_CLASSA(addr)) /* determine network mask for address class */
1539 nmask = IN_CLASSA_NET;
1543 if (IN_CLASSB(addr))
1545 nmask = IN_CLASSB_NET;
1549 nmask = IN_CLASSC_NET;
1553 /* class D nets are disallowed by bad_ip_adrs */
1554 mask = netmask | htonl(nmask);
1556 * Scan through the system's network interfaces.
1558 ifc.ifc_len = sizeof(ifs);
1560 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1562 if ( ! ok_error ( errno ))
1564 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1569 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1570 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1573 * Check the interface's internet address.
1575 if (ifr->ifr_addr.sa_family != AF_INET)
1579 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1580 if (((ntohl(ina) ^ addr) & nmask) != 0)
1585 * Check that the interface is up, and not point-to-point nor loopback.
1587 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1588 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1593 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1598 * Get its netmask and OR it into our mask.
1600 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1604 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1610 /********************************************************************
1612 * Internal routine to decode the version.modification.patch level
1615 static void decode_version (char *buf, int *version,
1616 int *modification, int *patch)
1618 *version = (int) strtoul (buf, &buf, 10);
1625 *modification = (int) strtoul (buf, &buf, 10);
1629 *patch = (int) strtoul (buf, &buf, 10);
1641 /********************************************************************
1643 * Procedure to determine if the PPP line dicipline is registered.
1647 ppp_registered(void)
1653 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1656 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1660 initfdflags = fcntl(local_fd, F_GETFL);
1661 if (initfdflags == -1)
1663 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1668 initfdflags &= ~O_NONBLOCK;
1669 fcntl(local_fd, F_SETFL, initfdflags);
1671 * Read the initial line dicipline and try to put the device into the
1674 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1676 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1681 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1683 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1688 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1690 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1699 /********************************************************************
1701 * ppp_available - check whether the system has any ppp interfaces
1702 * (in fact we check whether we can do an ioctl on ppp0).
1705 int ppp_available(void)
1710 int my_version, my_modification, my_patch;
1711 extern char *no_ppp_msg;
1713 * Open a socket for doing the ioctl operations.
1715 s = socket(AF_INET, SOCK_DGRAM, 0);
1721 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1722 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1724 * If the device did not exist then attempt to create one by putting the
1725 * current tty into the PPP discipline. If this works then obtain the
1726 * flags for the device again.
1730 if (ppp_registered())
1732 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1733 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1737 * Ensure that the hardware address is for PPP and not something else
1741 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1744 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1752 "This system lacks kernel support for PPP. To include PPP support\n"
1753 "in the kernel, please follow the steps detailed in the "
1754 "README.linux\nfile in the ppp-2.3 distribution.\n";
1757 * This is the PPP device. Validate the version of the driver at this
1758 * point to ensure that this program will work with the driver.
1762 char abBuffer [1024];
1764 ifr.ifr_data = abBuffer;
1765 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1770 decode_version (abBuffer,
1772 &driver_modification,
1779 driver_modification =
1783 * Validate the version of the driver against the version that we used.
1785 decode_version (PPP_VERSION,
1790 /* The version numbers must match */
1791 if (driver_version != my_version)
1796 /* The modification levels must be legal */
1797 if (driver_modification < my_modification)
1805 sprintf (route_buffer,
1806 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1807 driver_version, driver_modification, driver_patch);
1809 no_ppp_msg = route_buffer;
1815 /********************************************************************
1817 * Update the wtmp file with the appropriate user name and tty device.
1820 void logwtmp (const char *line, const char *name, const char *host)
1823 struct utmp ut, *utp;
1824 pid_t mypid = getpid();
1826 * Update the signon database for users.
1827 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1829 utmpname(_PATH_UTMP);
1831 while ((utp = getutent()) && (utp->ut_pid != mypid))
1834 /* Is this call really necessary? There is another one after the 'put' */
1839 memcpy(&ut, utp, sizeof(ut));
1843 /* some gettys/telnetds don't initialize utmp... */
1844 memset(&ut, 0, sizeof(ut));
1847 if (ut.ut_id[0] == 0)
1849 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1852 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1853 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1857 ut.ut_type = USER_PROCESS;
1860 /* Insert the host name if one is supplied */
1863 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1866 /* Insert the IP address of the remote system if IP is enabled */
1867 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1869 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1870 sizeof(ut.ut_addr));
1873 /* CL: Makes sure that the logout works */
1874 if (*host == 0 && *name==0)
1882 * Update the wtmp file.
1884 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1887 flock(wtmp, LOCK_EX);
1889 /* we really should check for error on the write for a full disk! */
1890 write (wtmp, (char *)&ut, sizeof(ut));
1893 flock(wtmp, LOCK_UN);
1897 /********************************************************************
1898 * Code for locking/unlocking the serial device.
1899 * This code is derived from chat.c.
1903 * lock - create a lock file for the named device
1906 int lock (char *dev)
1910 lock_file = malloc(strlen(dev) + 1);
1911 if (lock_file == NULL)
1913 novm("lock file name");
1915 strcpy (lock_file, dev);
1916 result = mklock (dev, (void *) 0);
1920 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1929 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1937 char hdb_lock_buffer[12];
1942 p = strrchr(dev, '/');
1948 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1949 if (lock_file == NULL)
1951 novm("lock file name");
1954 strcpy (lock_file, LOCK_PREFIX);
1955 strcat (lock_file, dev);
1957 * Attempt to create the lock file at this point.
1961 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1966 sprintf (hdb_lock_buffer, "%010d\n", pid);
1967 write (fd, hdb_lock_buffer, 11);
1969 write(fd, &pid, sizeof (pid));
1975 * If the file exists then check to see if the pid is stale
1977 if (errno == EEXIST)
1979 fd = open(lock_file, O_RDONLY, 0);
1982 if (errno == ENOENT) /* This is just a timing problem. */
1989 /* Read the lock file to find out who has the device locked */
1990 n = read (fd, hdb_lock_buffer, 11);
1994 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1998 /* See the process still exists. */
2002 hdb_lock_buffer[n] = '\0';
2003 sscanf (hdb_lock_buffer, " %d", &pid);
2005 pid = ((int *) hdb_lock_buffer)[0];
2007 if (pid == 0 || pid == getpid()
2008 || (kill(pid, 0) == -1 && errno == ESRCH))
2014 /* If the process does not exist then try to remove the lock */
2015 if (n == 0 && unlink (lock_file) == 0)
2017 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2022 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2026 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2037 /********************************************************************
2039 * unlock - remove our lockfile
2047 (void) rmlock (lock_file, (void *) 0);
2056 /********************************************************************
2058 * sifvjcomp - config tcp header compression
2061 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2063 u_int x = get_flags();
2067 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2069 if (! ok_error (errno))
2071 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno);
2077 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2078 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2084 /********************************************************************
2086 * sifup - Config the interface up and enable IP packets to pass.
2093 memset (&ifr, '\0', sizeof (ifr));
2094 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2095 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2097 if (! ok_error (errno))
2099 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2104 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2105 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2107 if (! ok_error (errno))
2109 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2117 /********************************************************************
2119 * sifdown - Config the interface down and disable IP.
2128 memset (&ifr, '\0', sizeof (ifr));
2129 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2130 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2132 if (! ok_error (errno))
2134 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2139 ifr.ifr_flags &= ~IFF_UP;
2140 ifr.ifr_flags |= IFF_POINTOPOINT;
2141 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2143 if (! ok_error (errno))
2145 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2152 /********************************************************************
2154 * sifaddr - Config the interface IP addresses and netmask.
2157 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2163 memset (&ifr, '\0', sizeof (ifr));
2164 memset (&rt, '\0', sizeof (rt));
2166 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2167 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2168 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2170 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2172 * Set our IP address
2174 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2175 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2177 if (errno != EEXIST)
2179 if (! ok_error (errno))
2181 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2186 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2191 * Set the gateway address
2193 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2194 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2196 if (! ok_error (errno))
2198 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2207 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2208 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2210 if (! ok_error (errno))
2212 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2218 * Add the device route
2220 #if LINUX_VERSION_CODE < 0x020100+16 /* 2.1.16 */
2221 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2222 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2225 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2226 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2227 rt.rt_flags = RTF_UP | RTF_HOST;
2229 #if LINUX_VERSION_CODE > 0x020100
2230 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2231 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2234 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2236 if (! ok_error (errno))
2238 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2246 /********************************************************************
2248 * cifaddr - Clear the interface IP addresses, and delete routes
2249 * through the interface if possible.
2252 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2254 #if LINUX_VERSION_CODE < 0x020100+16 /* 2.1.16 */
2257 * Delete the route through the device
2259 memset (&rt, '\0', sizeof (rt));
2261 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2262 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2265 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2266 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2267 rt.rt_flags = RTF_UP | RTF_HOST;
2269 #if LINUX_VERSION_CODE > 0x020100
2270 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2271 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2274 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2276 if (still_ppp() && ! ok_error (errno))
2278 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2286 /********************************************************************
2288 * open_loopback - open the device we use for getting packets
2289 * in demand mode. Under Linux, we use our existing fd
2290 * to the ppp driver.
2293 open_ppp_loopback(void)
2296 struct termios tios;
2298 if (openpty (&master_fd, &slave_fd, loop_name, NULL, NULL) < 0)
2300 syslog(LOG_ERR, "No free pty for loopback");
2303 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2305 set_ppp_fd(slave_fd);
2307 if (tcgetattr(ppp_fd, &tios) == 0)
2309 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2310 tios.c_cflag |= CS8 | CREAD;
2311 tios.c_iflag = IGNPAR | CLOCAL;
2314 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2316 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2320 flags = fcntl(master_fd, F_GETFL);
2322 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2324 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2327 flags = fcntl(ppp_fd, F_GETFL);
2329 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2331 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2334 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2336 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2340 * Find out which interface we were given.
2342 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2344 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2348 * Enable debug in the driver if requested.
2350 set_kdebugflag (kdebugflag);
2353 /********************************************************************
2355 * restore_loop - reattach the ppp unit to the loopback.
2357 * The kernel ppp driver automatically reattaches the ppp unit to
2358 * the loopback if the serial port is set to a line discipline other
2359 * than ppp, or if it detects a modem hangup. The former will happen
2360 * in disestablish_ppp if the latter hasn't already happened, so we
2361 * shouldn't need to do anything.
2363 * Just to be sure, set the real serial port to the normal discipline.
2369 if (ppp_fd != slave_fd)
2371 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2372 set_ppp_fd(slave_fd);
2376 /********************************************************************
2378 * sifnpmode - Set the mode for handling packets for a given NP.
2382 sifnpmode(u, proto, mode)
2389 npi.protocol = proto;
2391 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2393 if (! ok_error (errno))
2395 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2396 proto, mode, errno);
2397 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2405 #include <linux/ipx.h>
2407 /********************************************************************
2409 * sipxfaddr - Config the interface IPX networknumber
2412 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2418 struct sockaddr_ipx ipx_addr;
2420 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2422 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2425 if (! ok_error (errno))
2427 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2433 memset (&ifr, '\0', sizeof (ifr));
2434 strcpy (ifr.ifr_name, ifname);
2436 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2437 sipx->sipx_family = AF_IPX;
2438 sipx->sipx_port = 0;
2439 sipx->sipx_network = htonl (network);
2440 sipx->sipx_type = IPX_FRAME_ETHERII;
2441 sipx->sipx_action = IPX_CRTITF;
2443 * Set the IPX device
2445 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2448 if (errno != EEXIST)
2450 if (! ok_error (errno))
2453 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2458 syslog (LOG_WARNING,
2459 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2468 /********************************************************************
2470 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2471 * are removed and the device is no longer able to pass IPX
2475 int cipxfaddr (int unit)
2481 struct sockaddr_ipx ipx_addr;
2483 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2485 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2488 if (! ok_error (errno))
2490 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2496 memset (&ifr, '\0', sizeof (ifr));
2497 strcpy (ifr.ifr_name, ifname);
2499 sipx->sipx_type = IPX_FRAME_ETHERII;
2500 sipx->sipx_action = IPX_DLTITF;
2501 sipx->sipx_family = AF_IPX;
2503 * Set the IPX device
2505 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2507 if (! ok_error (errno))
2510 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2520 /********************************************************************
2522 * sys_check_options - check the options that the user specified
2526 sys_check_options(void)
2529 struct stat stat_buf;
2531 * Disable the IPX protocol if the support is not present in the kernel.
2532 * If we disable it then ensure that IP support is enabled.
2534 while (ipxcp_protent.enabled_flag)
2536 if (path_to_procfs())
2538 strcat (route_buffer, "/net/ipx_interface");
2539 if (lstat (route_buffer, &stat_buf) >= 0)
2544 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2545 ipxcp_protent.enabled_flag = 0;
2546 ipcp_protent.enabled_flag = 1;