2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
28 #include <sys/utsname.h>
44 /* This is in netdevice.h. However, this compile will fail miserably if
45 you attempt to include netdevice.h because it has so many references
46 to __memcpy functions which it should not attempt to do. So, since I
47 really don't use it, but it must be defined, define it now. */
50 #define MAX_ADDR_LEN 7
55 #include <net/if_arp.h>
56 #include <net/route.h>
57 #include <netinet/if_ether.h>
60 #include <linux/if_arp.h>
61 #include <linux/route.h>
62 #include <linux/if_ether.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
67 #include <linux/types.h>
68 #include <linux/ppp_defs.h>
69 #include <linux/if_ppp.h>
74 #include "patchlevel.h"
78 #include <linux/ipx.h>
79 #endif /* IPX_CHANGE */
82 #include <sys/locks.h>
85 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
89 #define ok_error(num) ((num)==EIO)
91 static int tty_disc = N_TTY; /* The TTY discipline */
92 static int ppp_disc = N_PPP; /* The PPP discpline */
93 static int initfdflags = -1; /* Initial file descriptor flags for fd */
94 static int ppp_fd = -1; /* fd which is set to PPP discipline */
95 static int sock_fd = -1; /* socket for doing interface ioctls */
96 static int slave_fd = -1;
97 static int master_fd = -1;
99 static int has_proxy_arp = 0;
100 static int driver_version = 0;
101 static int driver_modification = 0;
102 static int driver_patch = 0;
103 static int driver_is_old = 0;
104 static int restore_term = 0; /* 1 => we've munged the terminal */
105 static struct termios inittermios; /* Initial TTY termios */
107 static char loop_name[20];
108 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
110 static int if_is_up; /* Interface has been marked up */
111 static u_int32_t default_route_gateway; /* Gateway for default route added */
112 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
114 static char *lock_file;
116 static struct utsname utsname; /* for the kernel version */
117 static int kernel_version;
118 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
122 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
123 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
124 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
126 /* Prototypes for procedures local to this file. */
127 static int get_flags (void);
128 static void set_flags (int flags);
129 static int translate_speed (int bps);
130 static int baud_rate_of (int speed);
131 static char *path_to_route (void);
132 static void close_route_table (void);
133 static int open_route_table (void);
134 static int read_route_table (struct rtentry *rt);
135 static int defaultroute_exists (struct rtentry *rt);
136 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
138 static void decode_version (char *buf, int *version, int *mod, int *patch);
140 extern u_char inpacket_buf[]; /* borrowed from main.c */
143 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
147 #define SET_SA_FAMILY(addr, family) \
148 memset ((char *) &(addr), '\0', sizeof(addr)); \
149 addr.sa_family = (family);
152 * Determine if the PPP connection should still be present.
158 #define LOCK_PREFIX "/var/lock/LCK.."
161 static void set_ppp_fd (int new_fd)
163 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
167 static int still_ppp(void)
169 if (!hungup || ppp_fd == slave_fd)
172 set_ppp_fd(slave_fd);
178 /********************************************************************
180 * Functions to read and set the flags value in the device driver
183 static int get_flags (void)
187 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
189 if ( ok_error (errno) )
195 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
200 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
204 /********************************************************************/
206 static void set_flags (int flags)
208 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
210 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
212 if (! ok_error (errno) )
214 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
220 /********************************************************************
222 * sys_init - System-dependent initialization.
227 int osmaj, osmin, ospatch;
229 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
230 setlogmask(LOG_UPTO(LOG_INFO));
233 setlogmask(LOG_UPTO(LOG_DEBUG));
236 /* Get an internet socket for doing socket ioctls. */
237 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
240 if ( ! ok_error ( errno ))
242 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
248 osmaj = osmin = ospatch = 0;
249 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
250 kernel_version = KVERSION(osmaj, osmin, ospatch);
253 /********************************************************************
255 * sys_cleanup - restore any system state we modified before exiting:
256 * mark the interface down, delete default route and/or proxy arp entry.
257 * This should call die() because it's called from die().
260 void sys_cleanup(void)
263 * Take down the device
270 * Delete any routes through the device.
272 if (default_route_gateway != 0)
274 cifdefaultroute(0, 0, default_route_gateway);
279 cifproxyarp(0, proxy_arp_addr);
283 /********************************************************************
285 * sys_close - Clean up in a child process before execing.
295 /********************************************************************
297 * note_debug_level - note a change in the debug level.
300 void note_debug_level (void)
304 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
305 setlogmask(LOG_UPTO(LOG_DEBUG));
309 setlogmask(LOG_UPTO(LOG_WARNING));
313 /********************************************************************
315 * set_kdebugflag - Define the debugging level for the kernel
318 int set_kdebugflag (int requested_level)
320 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
322 if ( ! ok_error (errno) )
324 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
328 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
333 /********************************************************************
335 * establish_ppp - Turn the serial port into a ppp interface.
338 void establish_ppp (int tty_fd)
342 * The current PPP device will be the tty file.
346 * Ensure that the tty device is in exclusive mode.
348 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
350 if ( ! ok_error ( errno ))
352 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
356 * Demand mode - prime the old ppp device to relinquish the unit.
358 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
360 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
364 * Set the current tty to the PPP discpline
366 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
368 if ( ! ok_error (errno) )
370 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
375 * Find out which interface we were given.
377 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
379 if ( ! ok_error (errno))
381 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
386 * Check that we got the same unit again.
392 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
400 * Enable debug in the driver if requested.
403 set_kdebugflag (kdebugflag);
405 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
406 SC_RCV_EVNP | SC_RCV_ODDP));
408 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
409 driver_version, driver_modification, driver_patch));
411 * Fetch the initial file flags and reset blocking mode on the file.
413 initfdflags = fcntl(ppp_fd, F_GETFL);
415 if (initfdflags == -1 ||
416 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
418 if ( ! ok_error (errno))
421 "Couldn't set device to non-blocking mode: %m");
426 /********************************************************************
428 * disestablish_ppp - Restore the serial port to normal operation.
429 * This shouldn't call die() because it's called from die().
432 void disestablish_ppp(int tty_fd)
435 * Attempt to restore the previous tty settings
440 * Restore the previous line discipline
442 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
444 if ( ! ok_error (errno))
446 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
450 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
452 if ( ! ok_error (errno))
454 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
458 /* Reset non-blocking mode on fd. */
459 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
461 if ( ! ok_error (errno))
464 "Couldn't restore device fd flags: %m");
471 /********************************************************************
473 * clean_check - Fetch the flags for the device and generate
474 * appropriate error messages.
476 void clean_check(void)
483 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
486 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
489 case SC_RCV_B7_0 | SC_RCV_EVNP:
490 case SC_RCV_B7_0 | SC_RCV_ODDP:
491 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
492 s = "all had bit 7 set to 1";
496 case SC_RCV_B7_1 | SC_RCV_EVNP:
497 case SC_RCV_B7_1 | SC_RCV_ODDP:
498 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
499 s = "all had bit 7 set to 0";
503 s = "all had odd parity";
507 s = "all had even parity";
513 syslog(LOG_WARNING, "Receive serial link is not"
515 syslog(LOG_WARNING, "Problem: %s", s);
523 * List of valid speeds.
527 int speed_int, speed_val;
604 /********************************************************************
606 * Translate from bits/second to a speed_t.
609 static int translate_speed (int bps)
611 struct speed *speedp;
615 for (speedp = speeds; speedp->speed_int; speedp++)
617 if (bps == speedp->speed_int)
619 return speedp->speed_val;
622 syslog(LOG_WARNING, "speed %d not supported", bps);
627 /********************************************************************
629 * Translate from a speed_t to bits/second.
632 static int baud_rate_of (int speed)
634 struct speed *speedp;
638 for (speedp = speeds; speedp->speed_int; speedp++)
640 if (speed == speedp->speed_val)
642 return speedp->speed_int;
649 /********************************************************************
651 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
652 * at the requested speed, etc. If `local' is true, set CLOCAL
653 * regardless of whether the modem option was specified.
656 void set_up_tty (int tty_fd, int local)
661 if (tcgetattr(tty_fd, &tios) < 0)
663 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
672 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
673 tios.c_cflag |= CS8 | CREAD | HUPCL;
675 tios.c_iflag = IGNBRK | IGNPAR;
679 tios.c_cc[VTIME] = 0;
683 tios.c_cflag ^= (CLOCAL | HUPCL);
689 tios.c_cflag |= CRTSCTS;
693 tios.c_iflag |= IXON | IXOFF;
694 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
695 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
699 tios.c_cflag &= ~CRTSCTS;
706 speed = translate_speed(inspeed);
709 cfsetospeed (&tios, speed);
710 cfsetispeed (&tios, speed);
713 * We can't proceed if the serial port speed is B0,
714 * since that implies that the serial port is disabled.
718 speed = cfgetospeed(&tios);
721 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
727 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
729 syslog(LOG_ERR, "tcsetattr: %m");
733 baud_rate = baud_rate_of(speed);
737 /********************************************************************
739 * setdtr - control the DTR line on the serial port.
740 * This is called from die(), so it shouldn't call die().
743 void setdtr (int tty_fd, int on)
745 int modembits = TIOCM_DTR;
747 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
750 /********************************************************************
752 * restore_tty - restore the terminal to the saved settings.
755 void restore_tty (int tty_fd)
761 * Turn off echoing, because otherwise we can get into
762 * a loop with the tty and the modem echoing to each other.
763 * We presume we are the sole user of this tty device, so
764 * when we close it, it will revert to its defaults anyway.
768 inittermios.c_lflag &= ~(ECHO | ECHONL);
771 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
773 if (! ok_error (errno))
775 syslog(LOG_WARNING, "tcsetattr: %m");
781 /********************************************************************
783 * output - Output PPP packet.
786 void output (int unit, unsigned char *p, int len)
790 log_packet(p, len, "sent ", LOG_DEBUG);
793 if (write(ppp_fd, p, len) < 0)
795 if (errno == EWOULDBLOCK || errno == ENOBUFS
796 || errno == ENXIO || errno == EIO)
798 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
802 syslog(LOG_ERR, "write: %m(%d)", errno);
808 /********************************************************************
810 * wait_input - wait until there is data available on ppp_fd,
811 * for the length of time specified by *timo (indefinite
815 void wait_input (struct timeval *timo)
821 FD_SET(ppp_fd, &ready);
823 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
824 if (n < 0 && errno != EINTR)
826 syslog(LOG_ERR, "select: %m(%d)", errno);
831 /********************************************************************
833 * wait_loop_output - wait until there is data available on the
834 * loopback, for the length of time specified by *timo (indefinite
837 void wait_loop_output(timo)
838 struct timeval *timo;
844 FD_SET(master_fd, &ready);
845 n = select(master_fd + 1, &ready, NULL, &ready, timo);
846 if (n < 0 && errno != EINTR)
848 syslog(LOG_ERR, "select: %m(%d)", errno);
853 /********************************************************************
855 * wait_time - wait for a given length of time or until a
856 * signal is received.
860 struct timeval *timo;
864 n = select(0, NULL, NULL, NULL, timo);
865 if (n < 0 && errno != EINTR) {
866 syslog(LOG_ERR, "select: %m(%d)", errno);
871 /********************************************************************
873 * read_packet - get a PPP packet from the serial device.
876 int read_packet (unsigned char *buf)
880 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
883 if (errno == EWOULDBLOCK)
887 syslog(LOG_ERR, "read: %m(%d)", errno);
893 /********************************************************************
895 * get_loop_output - get outgoing packets from the ppp device,
896 * and detect when we want to bring the real link up.
897 * Return value is 1 if we need to bring up the link, 0 otherwise.
900 get_loop_output(void)
903 int n = read(master_fd, inbuf, sizeof(inbuf));
907 if (loop_chars(inbuf, n))
911 n = read(master_fd, inbuf, sizeof(inbuf));
916 syslog(LOG_ERR, "eof on loopback");
920 if (errno != EWOULDBLOCK)
922 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
929 /********************************************************************
931 * ppp_send_config - configure the transmit characteristics of
935 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
940 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
942 * Ensure that the link is still up.
947 * Set the MTU and other parameters for the ppp device
949 memset (&ifr, '\0', sizeof (ifr));
950 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
953 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
955 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
959 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
960 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
962 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
967 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
968 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
973 /********************************************************************
975 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
978 void ppp_set_xaccm (int unit, ext_accm accm)
980 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
981 accm[0], accm[1], accm[2], accm[3]));
983 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
985 if ( ! ok_error (errno))
987 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
992 /********************************************************************
994 * ppp_recv_config - configure the receive-side characteristics of
998 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
1002 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
1004 * If we were called because the link has gone down then there is nothing
1005 * which may be done. Just return without incident.
1012 * Set the receiver parameters
1014 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1016 if ( ! ok_error (errno))
1018 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
1022 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
1023 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
1025 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1030 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1034 /********************************************************************
1036 * ccp_test - ask kernel whether a given compression method
1037 * is acceptable for use.
1040 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1042 struct ppp_option_data data;
1044 memset (&data, '\0', sizeof (data));
1046 data.length = opt_len;
1047 data.transmit = for_transmit;
1049 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1054 return (errno == ENOBUFS)? 0: -1;
1057 /********************************************************************
1059 * ccp_flags_set - inform kernel about the current state of CCP.
1062 void ccp_flags_set (int unit, int isopen, int isup)
1066 int x = get_flags();
1067 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1068 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1073 /********************************************************************
1075 * get_idle_time - return how long the link has been idle.
1078 get_idle_time(u, ip)
1080 struct ppp_idle *ip;
1082 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1085 /********************************************************************
1087 * ccp_fatal_error - returns 1 if decompression was disabled as a
1088 * result of an error detected after decompression of a packet,
1089 * 0 otherwise. This is necessary because of patent nonsense.
1092 int ccp_fatal_error (int unit)
1094 int x = get_flags();
1096 return x & SC_DC_FERROR;
1100 * path_to_route - determine the path to the proc file system data
1103 FILE *route_fd = (FILE *) 0;
1104 static char route_buffer [512];
1106 static char *path_to_route (void);
1107 static int open_route_table (void);
1108 static void close_route_table (void);
1109 static int read_route_table (struct rtentry *rt);
1111 /********************************************************************
1113 * path_to_procfs - find the path to the proc file system mount point
1116 static int path_to_procfs (void)
1118 struct mntent *mntent;
1121 fp = fopen (MOUNTED, "r");
1124 mntent = getmntent (fp);
1125 while (mntent != (struct mntent *) 0)
1127 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1129 if (strcmp (mntent->mnt_type, "proc") == 0)
1131 strncpy (route_buffer, mntent->mnt_dir,
1132 sizeof (route_buffer)-10);
1133 route_buffer [sizeof (route_buffer)-10] = '\0';
1138 mntent = getmntent (fp);
1143 /* Default the mount location of /proc */
1144 strncpy (route_buffer, "/proc", sizeof (route_buffer)-10);
1148 /********************************************************************
1150 * path_to_route - find the path to the route tables in the proc file system
1153 static char *path_to_route (void)
1155 if (! path_to_procfs())
1157 syslog (LOG_ERR, "proc file system not mounted");
1160 strcat (route_buffer, "/net/route");
1161 return (route_buffer);
1164 /********************************************************************
1166 * close_route_table - close the interface to the route table
1169 static void close_route_table (void)
1171 if (route_fd != (FILE *) 0)
1174 route_fd = (FILE *) 0;
1178 /********************************************************************
1180 * open_route_table - open the interface to the route table
1183 static int open_route_table (void)
1187 close_route_table();
1189 path = path_to_route();
1195 route_fd = fopen (path, "r");
1196 if (route_fd == (FILE *) 0)
1198 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1204 /********************************************************************
1206 * read_route_table - read the next entry from the route table
1209 static int read_route_table (struct rtentry *rt)
1211 static char delims[] = " \t\n";
1212 char *dev_ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1214 memset (rt, '\0', sizeof (struct rtentry));
1218 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1224 dev_ptr = strtok (route_buffer, delims); /* interface name */
1225 dst_ptr = strtok (NULL, delims); /* destination address */
1226 gw_ptr = strtok (NULL, delims); /* gateway */
1227 flag_ptr = strtok (NULL, delims); /* flags */
1229 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1234 /* Discard that stupid header line which should never
1235 * have been there in the first place !! */
1236 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1242 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1243 strtoul (dst_ptr, NULL, 16);
1245 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1246 strtoul (gw_ptr, NULL, 16);
1248 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1249 rt->rt_dev = dev_ptr;
1254 /********************************************************************
1256 * defaultroute_exists - determine if there is a default route
1259 static int defaultroute_exists (struct rtentry *rt)
1263 if (!open_route_table())
1268 while (read_route_table(rt) != 0)
1270 if ((rt->rt_flags & RTF_UP) == 0)
1275 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1282 close_route_table();
1286 /********************************************************************
1288 * sifdefaultroute - assign a default route through the address given.
1291 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1295 if (defaultroute_exists(&rt))
1297 struct in_addr old_gateway =
1298 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1300 if (old_gateway.s_addr != gateway)
1303 "not replacing existing default route to %s [%s]",
1305 inet_ntoa (old_gateway));
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 (kernel_version > KVERSION(2,1,0)) {
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, SIOCADDRT, &rt) < 0)
1324 if ( ! ok_error ( errno ))
1326 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1331 default_route_gateway = gateway;
1335 /********************************************************************
1337 * cifdefaultroute - delete a default route through the address given.
1340 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1344 default_route_gateway = 0;
1346 memset (&rt, '\0', sizeof (rt));
1347 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1348 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1350 if (kernel_version > KVERSION(2,1,0)) {
1351 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1352 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1355 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1357 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1358 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1362 if ( ! ok_error ( errno ))
1365 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1374 /********************************************************************
1376 * sifproxyarp - Make a proxy ARP entry for the peer.
1379 int sifproxyarp (int unit, u_int32_t his_adr)
1381 struct arpreq arpreq;
1383 if (has_proxy_arp == 0)
1385 memset (&arpreq, '\0', sizeof(arpreq));
1387 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1388 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1389 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1391 * Get the hardware address of an interface on the same subnet
1392 * as our local address.
1394 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1396 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1400 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1402 if ( ! ok_error ( errno ))
1404 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1410 proxy_arp_addr = his_adr;
1415 /********************************************************************
1417 * cifproxyarp - Delete the proxy ARP entry for the peer.
1420 int cifproxyarp (int unit, u_int32_t his_adr)
1422 struct arpreq arpreq;
1424 if (has_proxy_arp == 1)
1426 memset (&arpreq, '\0', sizeof(arpreq));
1427 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1428 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1429 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1431 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1433 if ( ! ok_error ( errno ))
1435 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1444 /********************************************************************
1446 * get_ether_addr - get the hardware address of an interface on the
1447 * the same subnet as ipaddr.
1450 static int get_ether_addr (u_int32_t ipaddr,
1451 struct sockaddr *hwaddr,
1454 struct ifreq *ifr, *ifend;
1455 u_int32_t ina, mask;
1458 struct ifreq ifs[MAX_IFS];
1460 ifc.ifc_len = sizeof(ifs);
1462 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1464 if ( ! ok_error ( errno ))
1466 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1471 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1472 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1474 * Scan through looking for an interface with an Internet
1475 * address on the same subnet as `ipaddr'.
1477 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1478 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1480 if (ifr->ifr_addr.sa_family == AF_INET)
1482 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1483 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1484 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1487 * Check that the interface is up, and not point-to-point
1490 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1495 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1500 * Get its netmask and check that it's on the right subnet.
1502 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1507 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1508 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1509 ip_ntoa(ina), ntohl(mask)));
1511 if (((ipaddr ^ ina) & mask) != 0)
1524 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1525 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1527 * Now get the hardware address.
1529 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1530 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1532 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1538 sizeof (struct sockaddr));
1540 SYSDEBUG ((LOG_DEBUG,
1541 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1542 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1543 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1544 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1545 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1546 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1547 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1548 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1549 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1553 /********************************************************************
1555 * Return user specified netmask, modified by any mask we might determine
1556 * for address `addr' (in network byte order).
1557 * Here we scan through the system's list of interfaces, looking for
1558 * any non-point-to-point interfaces which might appear to be on the same
1559 * network as `addr'. If we find any, we OR in their netmask to the
1560 * user-specified netmask.
1563 u_int32_t GetMask (u_int32_t addr)
1565 u_int32_t mask, nmask, ina;
1566 struct ifreq *ifr, *ifend, ifreq;
1568 struct ifreq ifs[MAX_IFS];
1572 if (IN_CLASSA(addr)) /* determine network mask for address class */
1574 nmask = IN_CLASSA_NET;
1578 if (IN_CLASSB(addr))
1580 nmask = IN_CLASSB_NET;
1584 nmask = IN_CLASSC_NET;
1588 /* class D nets are disallowed by bad_ip_adrs */
1589 mask = netmask | htonl(nmask);
1591 * Scan through the system's network interfaces.
1593 ifc.ifc_len = sizeof(ifs);
1595 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1597 if ( ! ok_error ( errno ))
1599 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1604 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1605 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1608 * Check the interface's internet address.
1610 if (ifr->ifr_addr.sa_family != AF_INET)
1614 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1615 if (((ntohl(ina) ^ addr) & nmask) != 0)
1620 * Check that the interface is up, and not point-to-point nor loopback.
1622 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1623 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1628 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1633 * Get its netmask and OR it into our mask.
1635 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1639 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1645 /********************************************************************
1647 * Internal routine to decode the version.modification.patch level
1650 static void decode_version (char *buf, int *version,
1651 int *modification, int *patch)
1653 *version = (int) strtoul (buf, &buf, 10);
1660 *modification = (int) strtoul (buf, &buf, 10);
1664 *patch = (int) strtoul (buf, &buf, 10);
1676 /********************************************************************
1678 * Procedure to determine if the PPP line discipline is registered.
1682 ppp_registered(void)
1688 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1691 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1695 initfdflags = fcntl(local_fd, F_GETFL);
1696 if (initfdflags == -1)
1698 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1703 initfdflags &= ~O_NONBLOCK;
1704 fcntl(local_fd, F_SETFL, initfdflags);
1706 * Read the initial line dicipline and try to put the device into the
1709 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1711 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1716 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1718 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1723 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1725 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1734 /********************************************************************
1736 * ppp_available - check whether the system has any ppp interfaces
1737 * (in fact we check whether we can do an ioctl on ppp0).
1740 int ppp_available(void)
1745 int my_version, my_modification, my_patch;
1746 extern char *no_ppp_msg;
1748 * Open a socket for doing the ioctl operations.
1750 s = socket(AF_INET, SOCK_DGRAM, 0);
1756 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1757 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1759 * If the device did not exist then attempt to create one by putting the
1760 * current tty into the PPP discipline. If this works then obtain the
1761 * flags for the device again.
1765 if (ppp_registered())
1767 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1768 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1772 * Ensure that the hardware address is for PPP and not something else
1776 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1779 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1787 "This system lacks kernel support for PPP. This could be because\n"
1788 "the PPP kernel module is not loaded, or because the kernel is\n"
1789 "not configured for PPP. See the README.linux file in the\n"
1790 "ppp-2.3.5 distribution.\n";
1793 * This is the PPP device. Validate the version of the driver at this
1794 * point to ensure that this program will work with the driver.
1798 char abBuffer [1024];
1800 ifr.ifr_data = abBuffer;
1801 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1803 syslog(LOG_ERR, "Couldn't read driver version: %m");
1805 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
1808 decode_version(abBuffer,
1810 &driver_modification,
1813 * Validate the version of the driver against the version that we used.
1815 decode_version(VERSION,
1820 /* The version numbers must match */
1821 if (driver_version != my_version)
1826 /* The modification levels must be legal */
1827 if (driver_modification < my_modification)
1829 if (driver_modification >= 2) {
1830 /* we can cope with 2.2.0 and above */
1840 sprintf (route_buffer,
1841 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1842 driver_version, driver_modification, driver_patch);
1844 no_ppp_msg = route_buffer;
1851 /********************************************************************
1853 * Update the wtmp file with the appropriate user name and tty device.
1856 void logwtmp (const char *line, const char *name, const char *host)
1859 struct utmp ut, *utp;
1860 pid_t mypid = getpid();
1862 * Update the signon database for users.
1863 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1865 utmpname(_PATH_UTMP);
1867 while ((utp = getutent()) && (utp->ut_pid != mypid))
1870 /* Is this call really necessary? There is another one after the 'put' */
1875 memcpy(&ut, utp, sizeof(ut));
1879 /* some gettys/telnetds don't initialize utmp... */
1880 memset(&ut, 0, sizeof(ut));
1883 if (ut.ut_id[0] == 0)
1885 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1888 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1889 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1893 ut.ut_type = USER_PROCESS;
1896 /* Insert the host name if one is supplied */
1899 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1902 /* Insert the IP address of the remote system if IP is enabled */
1903 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1905 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1906 sizeof(ut.ut_addr));
1909 /* CL: Makes sure that the logout works */
1910 if (*host == 0 && *name==0)
1918 * Update the wtmp file.
1920 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1923 flock(wtmp, LOCK_EX);
1925 /* we really should check for error on the write for a full disk! */
1926 write (wtmp, (char *)&ut, sizeof(ut));
1929 flock(wtmp, LOCK_UN);
1933 /********************************************************************
1934 * Code for locking/unlocking the serial device.
1935 * This code is derived from chat.c.
1939 * lock - create a lock file for the named device
1942 int lock (char *dev)
1946 lock_file = malloc(strlen(dev) + 1);
1947 if (lock_file == NULL)
1949 novm("lock file name");
1951 strcpy (lock_file, dev);
1952 result = mklock (dev, (void *) 0);
1956 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1965 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1973 char hdb_lock_buffer[12];
1978 p = strrchr(dev, '/');
1984 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1985 if (lock_file == NULL)
1987 novm("lock file name");
1990 strcpy (lock_file, LOCK_PREFIX);
1991 strcat (lock_file, dev);
1993 * Attempt to create the lock file at this point.
1997 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
2002 sprintf (hdb_lock_buffer, "%010d\n", pid);
2003 write (fd, hdb_lock_buffer, 11);
2005 write(fd, &pid, sizeof (pid));
2011 * If the file exists then check to see if the pid is stale
2013 if (errno == EEXIST)
2015 fd = open(lock_file, O_RDONLY, 0);
2018 if (errno == ENOENT) /* This is just a timing problem. */
2025 /* Read the lock file to find out who has the device locked */
2026 n = read (fd, hdb_lock_buffer, 11);
2030 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2034 /* See the process still exists. */
2038 hdb_lock_buffer[n] = '\0';
2039 sscanf (hdb_lock_buffer, " %d", &pid);
2041 pid = ((int *) hdb_lock_buffer)[0];
2043 if (pid == 0 || pid == getpid()
2044 || (kill(pid, 0) == -1 && errno == ESRCH))
2050 /* If the process does not exist then try to remove the lock */
2051 if (n == 0 && unlink (lock_file) == 0)
2053 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2058 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2062 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2073 /********************************************************************
2075 * unlock - remove our lockfile
2083 (void) rmlock (lock_file, (void *) 0);
2092 /********************************************************************
2094 * sifvjcomp - config tcp header compression
2097 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2099 u_int x = get_flags();
2103 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2105 if (! ok_error (errno))
2107 syslog (LOG_ERR, "ioctl(PPPIOCSMAXCID): %m(%d)", errno);
2113 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2114 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2120 /********************************************************************
2122 * sifup - Config the interface up and enable IP packets to pass.
2129 memset (&ifr, '\0', sizeof (ifr));
2130 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2131 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2133 if (! ok_error (errno))
2135 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2140 ifr.ifr_flags |= (IFF_UP | 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);
2153 /********************************************************************
2155 * sifdown - Config the interface down and disable IP.
2164 memset (&ifr, '\0', sizeof (ifr));
2165 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2166 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2168 if (! ok_error (errno))
2170 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2175 ifr.ifr_flags &= ~IFF_UP;
2176 ifr.ifr_flags |= IFF_POINTOPOINT;
2177 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2179 if (! ok_error (errno))
2181 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2188 /********************************************************************
2190 * sifaddr - Config the interface IP addresses and netmask.
2193 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2199 memset (&ifr, '\0', sizeof (ifr));
2200 memset (&rt, '\0', sizeof (rt));
2202 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2203 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2204 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2206 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2208 * Set our IP address
2210 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2211 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2213 if (errno != EEXIST)
2215 if (! ok_error (errno))
2217 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2222 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2227 * Set the gateway address
2229 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2230 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2232 if (! ok_error (errno))
2234 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2240 * For recent kernels, force the netmask to 255.255.255.255.
2242 if (kernel_version >= KVERSION(2,1,16))
2246 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2247 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2249 if (! ok_error (errno))
2251 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2257 * Add the device route
2259 if (kernel_version < KVERSION(2,1,16)) {
2260 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2261 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2264 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2265 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2266 rt.rt_flags = RTF_UP | RTF_HOST;
2268 if (kernel_version > KVERSION(2,1,0)) {
2269 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2270 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2273 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2275 if (! ok_error (errno))
2277 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2285 /********************************************************************
2287 * cifaddr - Clear the interface IP addresses, and delete routes
2288 * through the interface if possible.
2291 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2295 if (kernel_version < KVERSION(2,1,16)) {
2297 * Delete the route through the device
2299 memset (&rt, '\0', sizeof (rt));
2301 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2302 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2305 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2306 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2307 rt.rt_flags = RTF_UP | RTF_HOST;
2309 if (kernel_version > KVERSION(2,1,0)) {
2310 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2311 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2314 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2316 if (still_ppp() && ! ok_error (errno))
2318 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2326 /********************************************************************
2328 * open_loopback - open the device we use for getting packets
2329 * in demand mode. Under Linux, we use our existing fd
2330 * to the ppp driver.
2333 open_ppp_loopback(void)
2336 struct termios tios;
2339 for (i = 0; i < 64; ++i) {
2340 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2341 master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2345 if (master_fd < 0) {
2346 syslog(LOG_ERR, "No free pty for loopback");
2349 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2351 slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2353 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2357 set_ppp_fd(slave_fd);
2359 if (tcgetattr(ppp_fd, &tios) == 0)
2361 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2362 tios.c_cflag |= CS8 | CREAD;
2363 tios.c_iflag = IGNPAR | CLOCAL;
2366 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2368 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2372 flags = fcntl(master_fd, F_GETFL);
2374 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2376 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2379 flags = fcntl(ppp_fd, F_GETFL);
2381 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2383 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2386 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2388 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2392 * Find out which interface we were given.
2394 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2396 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2400 * Enable debug in the driver if requested.
2402 set_kdebugflag (kdebugflag);
2405 /********************************************************************
2407 * restore_loop - reattach the ppp unit to the loopback.
2409 * The kernel ppp driver automatically reattaches the ppp unit to
2410 * the loopback if the serial port is set to a line discipline other
2411 * than ppp, or if it detects a modem hangup. The former will happen
2412 * in disestablish_ppp if the latter hasn't already happened, so we
2413 * shouldn't need to do anything.
2415 * Just to be sure, set the real serial port to the normal discipline.
2421 if (ppp_fd != slave_fd)
2423 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2424 set_ppp_fd(slave_fd);
2428 /********************************************************************
2430 * sifnpmode - Set the mode for handling packets for a given NP.
2434 sifnpmode(u, proto, mode)
2441 npi.protocol = proto;
2443 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2445 if (! ok_error (errno))
2447 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2448 proto, mode, errno);
2449 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2457 /********************************************************************
2459 * sipxfaddr - Config the interface IPX networknumber
2462 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2469 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2471 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2474 if (! ok_error (errno))
2476 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2482 memset (&ifr, '\0', sizeof (ifr));
2483 strcpy (ifr.ifr_name, ifname);
2485 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2486 sipx->sipx_family = AF_IPX;
2487 sipx->sipx_port = 0;
2488 sipx->sipx_network = htonl (network);
2489 sipx->sipx_type = IPX_FRAME_ETHERII;
2490 sipx->sipx_action = IPX_CRTITF;
2492 * Set the IPX device
2494 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2497 if (errno != EEXIST)
2499 if (! ok_error (errno))
2502 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2507 syslog (LOG_WARNING,
2508 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2517 /********************************************************************
2519 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2520 * are removed and the device is no longer able to pass IPX
2524 int cipxfaddr (int unit)
2531 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2533 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2536 if (! ok_error (errno))
2538 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2544 memset (&ifr, '\0', sizeof (ifr));
2545 strcpy (ifr.ifr_name, ifname);
2547 sipx->sipx_type = IPX_FRAME_ETHERII;
2548 sipx->sipx_action = IPX_DLTITF;
2549 sipx->sipx_family = AF_IPX;
2551 * Set the IPX device
2553 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2555 if (! ok_error (errno))
2558 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2569 * daemon - Detach us from controlling terminal session.
2572 daemon(nochdir, noclose)
2573 int nochdir, noclose;
2577 if ((pid = fork()) < 0)
2580 exit(0); /* parent dies */
2585 fclose(stdin); /* don't need stdin, stdout, stderr */
2593 * Use the hostname as part of the random number seed.
2602 for (p = hostname; *p != 0; ++p)
2607 /********************************************************************
2609 * sys_check_options - check the options that the user specified
2613 sys_check_options(void)
2616 struct stat stat_buf;
2618 * Disable the IPX protocol if the support is not present in the kernel.
2619 * If we disable it then ensure that IP support is enabled.
2621 while (ipxcp_protent.enabled_flag)
2623 if (path_to_procfs())
2625 strcat (route_buffer, "/net/ipx_interface");
2626 if (lstat (route_buffer, &stat_buf) >= 0)
2631 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2632 ipxcp_protent.enabled_flag = 0;
2633 ipcp_protent.enabled_flag = 1;
2637 if (demand && driver_is_old) {
2638 option_error("demand dialling is not supported by kernel driver version "
2639 "%d.%d.%d", driver_version, driver_modification,