2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
28 #include <sys/utsname.h>
44 /* This is in netdevice.h. However, this compile will fail miserably if
45 you attempt to include netdevice.h because it has so many references
46 to __memcpy functions which it should not attempt to do. So, since I
47 really don't use it, but it must be defined, define it now. */
50 #define MAX_ADDR_LEN 7
54 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
56 #include <net/if_arp.h>
57 #include <net/route.h>
58 #include <netinet/if_ether.h>
60 #include <linux/types.h>
62 #include <linux/if_arp.h>
63 #include <linux/route.h>
64 #include <linux/if_ether.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
69 #include <linux/ppp_defs.h>
70 #include <linux/if_ppp.h>
75 #include "patchlevel.h"
79 #if __GLIBC__ >= 2 && \
80 !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
81 #include <netipx/ipx.h>
83 #include <linux/ipx.h>
85 #endif /* IPX_CHANGE */
88 #include <sys/locks.h>
91 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
95 /* We can get an EIO error on an ioctl if the modem has hung up */
96 #define ok_error(num) ((num)==EIO)
98 static int tty_disc = N_TTY; /* The TTY discipline */
99 static int ppp_disc = N_PPP; /* The PPP discpline */
100 static int initfdflags = -1; /* Initial file descriptor flags for fd */
101 static int ppp_fd = -1; /* fd which is set to PPP discipline */
102 static int sock_fd = -1; /* socket for doing interface ioctls */
103 static int slave_fd = -1;
104 static int master_fd = -1;
106 static fd_set in_fds; /* set of fds that wait_input waits for */
107 static int max_in_fd; /* highest fd set in in_fds */
109 static int has_proxy_arp = 0;
110 static int driver_version = 0;
111 static int driver_modification = 0;
112 static int driver_patch = 0;
113 static int driver_is_old = 0;
114 static int restore_term = 0; /* 1 => we've munged the terminal */
115 static struct termios inittermios; /* Initial TTY termios */
117 static char loop_name[20];
118 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
120 static int if_is_up; /* Interface has been marked up */
121 static u_int32_t default_route_gateway; /* Gateway for default route added */
122 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
123 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
125 static char *lock_file;
127 static struct utsname utsname; /* for the kernel version */
128 static int kernel_version;
129 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
133 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
134 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
135 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
137 /* Prototypes for procedures local to this file. */
138 static int get_flags (void);
139 static void set_flags (int flags);
140 static int translate_speed (int bps);
141 static int baud_rate_of (int speed);
142 static char *path_to_route (void);
143 static void close_route_table (void);
144 static int open_route_table (void);
145 static int read_route_table (struct rtentry *rt);
146 static int defaultroute_exists (struct rtentry *rt);
147 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
148 char *name, int namelen);
149 static void decode_version (char *buf, int *version, int *mod, int *patch);
150 static int set_kdebugflag(int level);
151 static int ppp_registered(void);
153 extern u_char inpacket_buf[]; /* borrowed from main.c */
156 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
160 #define SET_SA_FAMILY(addr, family) \
161 memset ((char *) &(addr), '\0', sizeof(addr)); \
162 addr.sa_family = (family);
165 * Determine if the PPP connection should still be present.
171 #define LOCK_PREFIX "/var/lock/LCK.."
174 static void set_ppp_fd (int new_fd)
176 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd));
180 static int still_ppp(void)
182 if (!hungup || ppp_fd == slave_fd)
185 set_ppp_fd(slave_fd);
191 /********************************************************************
193 * Functions to read and set the flags value in the device driver
196 static int get_flags (void)
200 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
201 if ( ok_error (errno) )
204 fatal("ioctl(PPPIOCGFLAGS): %m");
207 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
211 /********************************************************************/
213 static void set_flags (int flags)
215 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
217 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0) {
218 if (! ok_error (errno) )
219 fatal("ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
223 /********************************************************************
225 * sys_init - System-dependent initialization.
230 int osmaj, osmin, ospatch;
232 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
233 setlogmask(LOG_UPTO(LOG_INFO));
235 setlogmask(LOG_UPTO(LOG_DEBUG));
237 /* Get an internet socket for doing socket ioctls. */
238 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
240 if ( ! ok_error ( errno ))
241 fatal("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 shouldn't call die() because it's called from die().
260 void sys_cleanup(void)
263 * Take down the device
268 * Delete any routes through the device.
270 if (default_route_gateway != 0)
271 cifdefaultroute(0, 0, default_route_gateway);
274 cifproxyarp(0, proxy_arp_addr);
277 /********************************************************************
279 * sys_close - Clean up in a child process before execing.
293 /********************************************************************
295 * set_kdebugflag - Define the debugging level for the kernel
298 static int set_kdebugflag (int requested_level)
300 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0) {
301 if ( ! ok_error (errno) )
302 error("ioctl(PPPIOCSDEBUG): %m");
305 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
310 /********************************************************************
312 * establish_ppp - Turn the serial port into a ppp interface.
315 int establish_ppp (int tty_fd)
319 * The current PPP device will be the tty file.
323 * Ensure that the tty device is in exclusive mode.
325 if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
326 if ( ! ok_error ( errno ))
327 warn("ioctl(TIOCEXCL): %m");
330 * Demand mode - prime the old ppp device to relinquish the unit.
332 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
333 fatal("ioctl(transfer ppp unit): %m(%d)", errno);
335 * Set the current tty to the PPP discpline
337 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0) {
338 if ( ! ok_error (errno) )
339 fatal("ioctl(TIOCSETD): %m(%d)", errno);
342 * Find out which interface we were given.
344 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0) {
345 if ( ! ok_error (errno))
346 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno);
349 * Check that we got the same unit again.
353 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
358 * Enable debug in the driver if requested.
361 set_kdebugflag (kdebugflag);
363 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
364 SC_RCV_EVNP | SC_RCV_ODDP));
366 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
367 driver_version, driver_modification, driver_patch));
369 * Fetch the initial file flags and reset blocking mode on the file.
371 initfdflags = fcntl(ppp_fd, F_GETFL);
373 if (initfdflags == -1 ||
374 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
375 if ( ! ok_error (errno))
376 warn("Couldn't set device to non-blocking mode: %m");
382 /********************************************************************
384 * disestablish_ppp - Restore the serial port to normal operation.
385 * This shouldn't call die() because it's called from die().
388 void disestablish_ppp(int tty_fd)
391 * Attempt to restore the previous tty settings
395 * Restore the previous line discipline
397 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
398 if ( ! ok_error (errno))
399 error("ioctl(TIOCSETD, N_TTY): %m");
402 if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
403 if ( ! ok_error (errno))
404 warn("ioctl(TIOCNXCL): %m(%d)", errno);
407 /* Reset non-blocking mode on fd. */
408 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
409 if ( ! ok_error (errno))
410 warn("Couldn't restore device fd flags: %m");
416 /********************************************************************
418 * clean_check - Fetch the flags for the device and generate
419 * appropriate error messages.
421 void clean_check(void)
427 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
429 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
431 case SC_RCV_B7_0 | SC_RCV_EVNP:
432 case SC_RCV_B7_0 | SC_RCV_ODDP:
433 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
434 s = "all had bit 7 set to 1";
438 case SC_RCV_B7_1 | SC_RCV_EVNP:
439 case SC_RCV_B7_1 | SC_RCV_ODDP:
440 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
441 s = "all had bit 7 set to 0";
445 s = "all had odd parity";
449 s = "all had even parity";
454 warn("Receive serial link is not 8-bit clean:");
455 warn("Problem: %s", s);
463 * List of valid speeds.
467 int speed_int, speed_val;
544 /********************************************************************
546 * Translate from bits/second to a speed_t.
549 static int translate_speed (int bps)
551 struct speed *speedp;
554 for (speedp = speeds; speedp->speed_int; speedp++) {
555 if (bps == speedp->speed_int)
556 return speedp->speed_val;
558 warn("speed %d not supported", bps);
563 /********************************************************************
565 * Translate from a speed_t to bits/second.
568 static int baud_rate_of (int speed)
570 struct speed *speedp;
573 for (speedp = speeds; speedp->speed_int; speedp++) {
574 if (speed == speedp->speed_val)
575 return speedp->speed_int;
581 /********************************************************************
583 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
584 * at the requested speed, etc. If `local' is true, set CLOCAL
585 * regardless of whether the modem option was specified.
588 void set_up_tty(int tty_fd, int local)
594 if (tcgetattr(tty_fd, &tios) < 0) {
595 if (!ok_error(errno))
596 fatal("tcgetattr: %m(%d)", errno);
603 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
604 tios.c_cflag |= CS8 | CREAD | HUPCL;
606 tios.c_iflag = IGNBRK | IGNPAR;
610 tios.c_cc[VTIME] = 0;
613 tios.c_cflag ^= (CLOCAL | HUPCL);
617 tios.c_cflag |= CRTSCTS;
621 tios.c_iflag |= IXON | IXOFF;
622 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
623 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
627 tios.c_cflag &= ~CRTSCTS;
634 speed = translate_speed(inspeed);
636 cfsetospeed (&tios, speed);
637 cfsetispeed (&tios, speed);
640 * We can't proceed if the serial port speed is B0,
641 * since that implies that the serial port is disabled.
644 speed = cfgetospeed(&tios);
646 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
649 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
650 if (!ok_error(errno))
651 fatal("tcsetattr: %m");
653 baud_rate = baud_rate_of(speed);
657 /********************************************************************
659 * setdtr - control the DTR line on the serial port.
660 * This is called from die(), so it shouldn't call die().
663 void setdtr (int tty_fd, int on)
665 int modembits = TIOCM_DTR;
667 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
670 /********************************************************************
672 * restore_tty - restore the terminal to the saved settings.
675 void restore_tty (int tty_fd)
680 * Turn off echoing, because otherwise we can get into
681 * a loop with the tty and the modem echoing to each other.
682 * We presume we are the sole user of this tty device, so
683 * when we close it, it will revert to its defaults anyway.
686 inittermios.c_lflag &= ~(ECHO | ECHONL);
688 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
689 if (! ok_error (errno))
690 warn("tcsetattr: %m");
695 /********************************************************************
697 * output - Output PPP packet.
700 void output (int unit, unsigned char *p, int len)
703 dbglog("sent %P", p, len);
705 if (write(ppp_fd, p, len) < 0) {
706 if (errno == EWOULDBLOCK || errno == ENOBUFS
707 || errno == ENXIO || errno == EIO || errno == EINTR)
708 warn("write: warning: %m (%d)", errno);
710 error("write: %m (%d)", errno);
714 /********************************************************************
716 * wait_input - wait until there is data available,
717 * for the length of time specified by *timo (indefinite
721 void wait_input(struct timeval *timo)
727 n = select(max_in_fd + 1, &ready, NULL, &ready, timo);
728 if (n < 0 && errno != EINTR)
729 fatal("select: %m(%d)", errno);
733 * add_fd - add an fd to the set that wait_input waits for.
743 * remove_fd - remove an fd from the set that wait_input waits for.
745 void remove_fd(int fd)
751 /********************************************************************
753 * wait_loop_output - wait until there is data available on the
754 * loopback, for the length of time specified by *timo (indefinite
757 void wait_loop_output(timo)
758 struct timeval *timo;
764 FD_SET(master_fd, &ready);
765 n = select(master_fd + 1, &ready, NULL, &ready, timo);
766 if (n < 0 && errno != EINTR)
767 fatal("select: %m(%d)", errno);
770 /********************************************************************
772 * wait_time - wait for a given length of time or until a
773 * signal is received.
777 struct timeval *timo;
781 n = select(0, NULL, NULL, NULL, timo);
782 if (n < 0 && errno != EINTR)
783 fatal("select: %m(%d)", errno);
787 /********************************************************************
789 * read_packet - get a PPP packet from the serial device.
792 int read_packet (unsigned char *buf)
796 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
798 if (errno == EWOULDBLOCK || errno == EIO)
800 fatal("read: %m(%d)", errno);
805 /********************************************************************
807 * get_loop_output - get outgoing packets from the ppp device,
808 * and detect when we want to bring the real link up.
809 * Return value is 1 if we need to bring up the link, 0 otherwise.
812 get_loop_output(void)
815 int n = read(master_fd, inbuf, sizeof(inbuf));
818 if (loop_chars(inbuf, n))
820 n = read(master_fd, inbuf, sizeof(inbuf));
824 fatal("eof on loopback");
826 if (errno != EWOULDBLOCK)
827 fatal("read from loopback: %m(%d)", errno);
832 /********************************************************************
834 * ppp_send_config - configure the transmit characteristics of
838 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
843 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
845 * Ensure that the link is still up.
850 * Set the MTU and other parameters for the ppp device
852 memset (&ifr, '\0', sizeof (ifr));
853 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
856 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
857 fatal("ioctl(SIOCSIFMTU): %m(%d)", errno);
859 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
860 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
861 if (!ok_error(errno))
862 fatal("ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
867 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
868 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
872 /********************************************************************
874 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
877 void ppp_set_xaccm (int unit, ext_accm accm)
879 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
880 accm[0], accm[1], accm[2], accm[3]));
882 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
883 if ( ! ok_error (errno))
884 warn("ioctl(set extended ACCM): %m(%d)", errno);
888 /********************************************************************
890 * ppp_recv_config - configure the receive-side characteristics of
894 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
898 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
900 * If we were called because the link has gone down then there is nothing
901 * which may be done. Just return without incident.
906 * Set the receiver parameters
908 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
909 if ( ! ok_error (errno))
910 error("ioctl(PPPIOCSMRU): %m(%d)", errno);
913 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
914 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
915 if (!ok_error(errno))
916 error("ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
920 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
924 /********************************************************************
926 * ccp_test - ask kernel whether a given compression method
927 * is acceptable for use.
930 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
932 struct ppp_option_data data;
934 memset (&data, '\0', sizeof (data));
936 data.length = opt_len;
937 data.transmit = for_transmit;
939 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
942 return (errno == ENOBUFS)? 0: -1;
945 /********************************************************************
947 * ccp_flags_set - inform kernel about the current state of CCP.
950 void ccp_flags_set (int unit, int isopen, int isup)
954 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
955 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
960 /********************************************************************
962 * get_idle_time - return how long the link has been idle.
969 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
972 /********************************************************************
974 * get_ppp_stats - return statistics for the link.
977 get_ppp_stats(u, stats)
979 struct pppd_stats *stats;
981 struct ifpppstatsreq req;
983 memset (&req, 0, sizeof (req));
985 req.stats_ptr = (caddr_t) &req.stats;
986 strlcpy(req.ifr__name, ifname, sizeof(req.ifr__name));
987 if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
988 error("Couldn't get PPP statistics: %m");
991 stats->bytes_in = req.stats.p.ppp_ibytes;
992 stats->bytes_out = req.stats.p.ppp_obytes;
996 /********************************************************************
998 * ccp_fatal_error - returns 1 if decompression was disabled as a
999 * result of an error detected after decompression of a packet,
1000 * 0 otherwise. This is necessary because of patent nonsense.
1003 int ccp_fatal_error (int unit)
1005 int x = get_flags();
1007 return x & SC_DC_FERROR;
1011 * path_to_route - determine the path to the proc file system data
1013 #define ROUTE_MAX_COLS 12
1014 FILE *route_fd = (FILE *) 0;
1015 static char route_buffer [512];
1016 static int route_dev_col, route_dest_col, route_gw_col;
1017 static int route_flags_col, route_mask_col;
1018 static int route_num_cols;
1020 static char *path_to_route (void);
1021 static int open_route_table (void);
1022 static void close_route_table (void);
1023 static int read_route_table (struct rtentry *rt);
1025 /********************************************************************
1027 * path_to_procfs - find the path to the proc file system mount point
1030 static int path_to_procfs (void)
1032 struct mntent *mntent;
1035 fp = fopen(MOUNTED, "r");
1037 /* Default the mount location of /proc */
1038 strlcpy (route_buffer, "/proc", sizeof (route_buffer));
1042 while ((mntent = getmntent(fp)) != NULL) {
1043 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
1045 if (strcmp(mntent->mnt_type, "proc") == 0)
1052 strlcpy(route_buffer, mntent->mnt_dir, sizeof (route_buffer));
1056 /********************************************************************
1058 * path_to_route - find the path to the route tables in the proc file system
1061 static char *path_to_route (void)
1063 if (!path_to_procfs()) {
1064 error("proc file system not mounted");
1067 strlcat (route_buffer, "/net/route", sizeof(route_buffer));
1068 return (route_buffer);
1071 /********************************************************************
1073 * close_route_table - close the interface to the route table
1076 static void close_route_table (void)
1078 if (route_fd != (FILE *) 0) {
1080 route_fd = (FILE *) 0;
1084 /********************************************************************
1086 * open_route_table - open the interface to the route table
1088 static char route_delims[] = " \t\n";
1090 static int open_route_table (void)
1094 close_route_table();
1096 path = path_to_route();
1100 route_fd = fopen (path, "r");
1101 if (route_fd == NULL) {
1102 error("can't open %s: %m (%d)", path, errno);
1106 route_dev_col = 0; /* default to usual columns */
1109 route_flags_col = 3;
1113 /* parse header line */
1114 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
1115 char *p = route_buffer, *q;
1117 for (col = 0; col < ROUTE_MAX_COLS; ++col) {
1119 if ((q = strtok(p, route_delims)) == 0)
1121 if (strcasecmp(q, "iface") == 0)
1122 route_dev_col = col;
1123 else if (strcasecmp(q, "destination") == 0)
1124 route_dest_col = col;
1125 else if (strcasecmp(q, "gateway") == 0)
1127 else if (strcasecmp(q, "flags") == 0)
1128 route_flags_col = col;
1129 else if (strcasecmp(q, "mask") == 0)
1130 route_mask_col = col;
1133 if (used && col >= route_num_cols)
1134 route_num_cols = col + 1;
1142 /********************************************************************
1144 * read_route_table - read the next entry from the route table
1147 static int read_route_table(struct rtentry *rt)
1149 char *cols[ROUTE_MAX_COLS], *p;
1152 memset (rt, '\0', sizeof (struct rtentry));
1154 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1158 for (col = 0; col < route_num_cols; ++col) {
1159 cols[col] = strtok(p, route_delims);
1160 if (cols[col] == NULL)
1161 return 0; /* didn't get enough columns */
1164 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1165 strtoul(cols[route_dest_col], NULL, 16);
1167 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1168 strtoul(cols[route_gw_col], NULL, 16);
1170 ((struct sockaddr_in *) &rt->rt_genmask)->sin_addr.s_addr =
1171 strtoul(cols[route_mask_col], NULL, 16);
1173 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
1174 rt->rt_dev = cols[route_dev_col];
1179 /********************************************************************
1181 * defaultroute_exists - determine if there is a default route
1184 static int defaultroute_exists (struct rtentry *rt)
1188 if (!open_route_table())
1191 while (read_route_table(rt) != 0) {
1192 if ((rt->rt_flags & RTF_UP) == 0)
1195 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L) {
1201 close_route_table();
1206 * have_route_to - determine if the system has any route to
1207 * a given IP address. `addr' is in network byte order.
1208 * Return value is 1 if yes, 0 if no, -1 if don't know.
1209 * For demand mode to work properly, we have to ignore routes
1210 * through our own interface.
1212 int have_route_to(u_int32_t addr)
1217 if (!open_route_table())
1218 return -1; /* don't know */
1220 while (read_route_table(&rt)) {
1221 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
1223 if ((addr & ((struct sockaddr_in *)&rt.rt_genmask)->sin_addr.s_addr)
1224 == ((struct sockaddr_in *)&rt.rt_dst)->sin_addr.s_addr) {
1230 close_route_table();
1234 /********************************************************************
1236 * sifdefaultroute - assign a default route through the address given.
1239 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1243 if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
1244 struct in_addr old_gateway =
1245 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1247 if (old_gateway.s_addr != gateway)
1248 error("not replacing existing default route to %s [%I]",
1249 rt.rt_dev, old_gateway);
1253 memset (&rt, '\0', sizeof (rt));
1254 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1255 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1257 if (kernel_version > KVERSION(2,1,0)) {
1258 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1259 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1262 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1264 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1265 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
1266 if ( ! ok_error ( errno ))
1267 error("default route ioctl(SIOCADDRT): %m(%d)", errno);
1271 default_route_gateway = gateway;
1275 /********************************************************************
1277 * cifdefaultroute - delete a default route through the address given.
1280 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1284 default_route_gateway = 0;
1286 memset (&rt, '\0', sizeof (rt));
1287 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1288 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1290 if (kernel_version > KVERSION(2,1,0)) {
1291 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1292 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1295 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1297 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1298 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
1300 if ( ! ok_error ( errno ))
1301 error("default route ioctl(SIOCDELRT): %m (%d)", errno);
1309 /********************************************************************
1311 * sifproxyarp - Make a proxy ARP entry for the peer.
1314 int sifproxyarp (int unit, u_int32_t his_adr)
1316 struct arpreq arpreq;
1318 if (has_proxy_arp == 0) {
1319 memset (&arpreq, '\0', sizeof(arpreq));
1321 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1322 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1323 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1325 * Get the hardware address of an interface on the same subnet
1326 * as our local address.
1328 if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
1329 sizeof(proxy_arp_dev))) {
1330 error("Cannot determine ethernet address for proxy ARP");
1333 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
1335 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1336 if ( ! ok_error ( errno ))
1337 error("ioctl(SIOCSARP): %m(%d)", errno);
1340 proxy_arp_addr = his_adr;
1347 /********************************************************************
1349 * cifproxyarp - Delete the proxy ARP entry for the peer.
1352 int cifproxyarp (int unit, u_int32_t his_adr)
1354 struct arpreq arpreq;
1356 if (has_proxy_arp) {
1358 memset (&arpreq, '\0', sizeof(arpreq));
1359 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1360 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1361 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1362 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
1364 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1365 if ( ! ok_error ( errno ))
1366 warn("ioctl(SIOCDARP): %m(%d)", errno);
1373 /********************************************************************
1375 * get_ether_addr - get the hardware address of an interface on the
1376 * the same subnet as ipaddr.
1379 static int get_ether_addr (u_int32_t ipaddr,
1380 struct sockaddr *hwaddr,
1381 char *name, int namelen)
1383 struct ifreq *ifr, *ifend;
1384 u_int32_t ina, mask;
1387 struct ifreq ifs[MAX_IFS];
1389 ifc.ifc_len = sizeof(ifs);
1391 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
1392 if ( ! ok_error ( errno ))
1393 error("ioctl(SIOCGIFCONF): %m(%d)", errno);
1397 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1398 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1400 * Scan through looking for an interface with an Internet
1401 * address on the same subnet as `ipaddr'.
1403 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1404 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1405 if (ifr->ifr_addr.sa_family == AF_INET) {
1406 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1407 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1408 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1411 * Check that the interface is up, and not point-to-point
1414 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1417 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1420 * Get its netmask and check that it's on the right subnet.
1422 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1425 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1426 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1427 ip_ntoa(ina), ntohl(mask)));
1429 if (((ipaddr ^ ina) & mask) != 0)
1438 strlcpy(name, ifreq.ifr_name, namelen);
1439 info("found interface %s for proxy arp", name);
1441 * Now get the hardware address.
1443 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1444 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0) {
1445 error("SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1451 sizeof (struct sockaddr));
1453 SYSDEBUG ((LOG_DEBUG,
1454 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1455 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1456 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1457 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1458 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1459 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1460 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1461 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1462 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1466 /********************************************************************
1468 * Return user specified netmask, modified by any mask we might determine
1469 * for address `addr' (in network byte order).
1470 * Here we scan through the system's list of interfaces, looking for
1471 * any non-point-to-point interfaces which might appear to be on the same
1472 * network as `addr'. If we find any, we OR in their netmask to the
1473 * user-specified netmask.
1476 u_int32_t GetMask (u_int32_t addr)
1478 u_int32_t mask, nmask, ina;
1479 struct ifreq *ifr, *ifend, ifreq;
1481 struct ifreq ifs[MAX_IFS];
1485 if (IN_CLASSA(addr)) /* determine network mask for address class */
1486 nmask = IN_CLASSA_NET;
1487 else if (IN_CLASSB(addr))
1488 nmask = IN_CLASSB_NET;
1490 nmask = IN_CLASSC_NET;
1492 /* class D nets are disallowed by bad_ip_adrs */
1493 mask = netmask | htonl(nmask);
1495 * Scan through the system's network interfaces.
1497 ifc.ifc_len = sizeof(ifs);
1499 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
1500 if ( ! ok_error ( errno ))
1501 warn("ioctl(SIOCGIFCONF): %m(%d)", errno);
1505 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1506 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1508 * Check the interface's internet address.
1510 if (ifr->ifr_addr.sa_family != AF_INET)
1512 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1513 if (((ntohl(ina) ^ addr) & nmask) != 0)
1516 * Check that the interface is up, and not point-to-point nor loopback.
1518 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1519 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1522 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1525 * Get its netmask and OR it into our mask.
1527 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1529 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1535 /********************************************************************
1537 * Internal routine to decode the version.modification.patch level
1540 static void decode_version (char *buf, int *version,
1541 int *modification, int *patch)
1543 *version = (int) strtoul (buf, &buf, 10);
1549 *modification = (int) strtoul (buf, &buf, 10);
1552 *patch = (int) strtoul (buf, &buf, 10);
1563 /********************************************************************
1565 * Procedure to determine if the PPP line discipline is registered.
1569 ppp_registered(void)
1575 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1577 error("Failed to open %s: %m(%d)", devnam, errno);
1581 initfdflags = fcntl(local_fd, F_GETFL);
1582 if (initfdflags == -1) {
1583 error("Couldn't get device fd flags: %m(%d)", errno);
1588 initfdflags &= ~O_NONBLOCK;
1589 fcntl(local_fd, F_SETFL, initfdflags);
1591 * Read the initial line dicipline and try to put the device into the
1594 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0) {
1595 error("ioctl(TIOCGETD): %m(%d)", errno);
1600 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
1601 error("ioctl(TIOCSETD): %m(%d)", errno);
1606 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0) {
1607 error("ioctl(TIOCSETD): %m(%d)", errno);
1616 /********************************************************************
1618 * ppp_available - check whether the system has any ppp interfaces
1619 * (in fact we check whether we can do an ioctl on ppp0).
1622 int ppp_available(void)
1627 int my_version, my_modification, my_patch;
1628 extern char *no_ppp_msg;
1630 * Open a socket for doing the ioctl operations.
1632 s = socket(AF_INET, SOCK_DGRAM, 0);
1636 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1637 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1639 * If the device did not exist then attempt to create one by putting the
1640 * current tty into the PPP discipline. If this works then obtain the
1641 * flags for the device again.
1644 if (ppp_registered()) {
1645 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1646 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1650 * Ensure that the hardware address is for PPP and not something else
1653 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1655 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1660 "This system lacks kernel support for PPP. This could be because\n"
1661 "the PPP kernel module is not loaded, or because the kernel is\n"
1662 "not configured for PPP. See the README.linux file in the\n"
1663 "ppp-2.3.6 distribution.\n";
1666 * This is the PPP device. Validate the version of the driver at this
1667 * point to ensure that this program will work with the driver.
1670 char abBuffer [1024];
1672 ifr.ifr_data = abBuffer;
1673 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1675 error("Couldn't read driver version: %m");
1677 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
1680 decode_version(abBuffer,
1682 &driver_modification,
1685 * Validate the version of the driver against the version that we used.
1687 decode_version(VERSION,
1692 /* The version numbers must match */
1693 if (driver_version != my_version)
1696 /* The modification levels must be legal */
1697 if (driver_modification < 3) {
1698 if (driver_modification >= 2) {
1699 /* we can cope with 2.2.0 and above */
1708 slprintf(route_buffer, sizeof(route_buffer),
1709 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1710 driver_version, driver_modification, driver_patch);
1712 no_ppp_msg = route_buffer;
1719 /********************************************************************
1721 * Update the wtmp file with the appropriate user name and tty device.
1724 void logwtmp (const char *line, const char *name, const char *host)
1727 struct utmp ut, *utp;
1728 pid_t mypid = getpid();
1730 * Update the signon database for users.
1731 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1733 utmpname(_PATH_UTMP);
1735 while ((utp = getutent()) && (utp->ut_pid != mypid))
1738 /* Is this call really necessary? There is another one after the 'put' */
1742 memcpy(&ut, utp, sizeof(ut));
1744 /* some gettys/telnetds don't initialize utmp... */
1745 memset(&ut, 0, sizeof(ut));
1747 if (ut.ut_id[0] == 0)
1748 strlcpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1750 strlcpy(ut.ut_user, name, sizeof(ut.ut_user));
1751 strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1755 ut.ut_type = USER_PROCESS;
1758 /* Insert the host name if one is supplied */
1760 strlcpy (ut.ut_host, host, sizeof(ut.ut_host));
1762 /* Insert the IP address of the remote system if IP is enabled */
1763 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1764 memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1765 sizeof(ut.ut_addr));
1767 /* CL: Makes sure that the logout works */
1768 if (*host == 0 && *name==0)
1774 * Update the wtmp file.
1776 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1778 flock(wtmp, LOCK_EX);
1780 /* we really should check for error on the write for a full disk! */
1781 write (wtmp, (char *)&ut, sizeof(ut));
1784 flock(wtmp, LOCK_UN);
1788 /********************************************************************
1789 * Code for locking/unlocking the serial device.
1790 * This code is derived from chat.c.
1794 * lock - create a lock file for the named device
1797 int lock (char *dev)
1801 lock_file = strdup(dev);
1802 if (lock_file == NULL)
1803 novm("lock file name");
1804 result = mklock (dev, (void *) 0);
1807 notice("Device %s is locked by pid %d", dev, result);
1814 error("Can't create lock file %s", lock_file);
1822 char hdb_lock_buffer[12];
1828 p = strrchr(dev, '/');
1832 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1833 lock_file = malloc(l);
1834 if (lock_file == NULL)
1835 novm("lock file name");
1837 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1839 * Attempt to create the lock file at this point.
1842 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1846 slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%010d\n", pid);
1847 write (fd, hdb_lock_buffer, 11);
1849 write(fd, &pid, sizeof (pid));
1855 * If the file exists then check to see if the pid is stale
1857 if (errno == EEXIST) {
1858 fd = open(lock_file, O_RDONLY, 0);
1860 if (errno == ENOENT) /* This is just a timing problem. */
1865 /* Read the lock file to find out who has the device locked */
1866 n = read (fd, hdb_lock_buffer, 11);
1869 error("Can't read pid from lock file %s", lock_file);
1873 /* See the process still exists. */
1876 hdb_lock_buffer[n] = '\0';
1877 sscanf (hdb_lock_buffer, " %d", &pid);
1879 pid = ((int *) hdb_lock_buffer)[0];
1881 if (pid == 0 || pid == getpid()
1882 || (kill(pid, 0) == -1 && errno == ESRCH))
1886 /* If the process does not exist then try to remove the lock */
1887 if (n == 0 && unlink (lock_file) == 0) {
1888 notice("Removed stale lock on %s (pid %d)",
1893 notice("Device %s is locked by pid %d", dev, pid);
1897 error("Can't create lock file %s: %m(%d)", lock_file, errno);
1908 /********************************************************************
1910 * unlock - remove our lockfile
1917 (void) rmlock (lock_file, (void *) 0);
1926 /********************************************************************
1928 * sifvjcomp - config tcp header compression
1931 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
1933 u_int x = get_flags();
1936 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
1937 if (! ok_error (errno))
1938 error("ioctl(PPPIOCSMAXCID): %m(%d)", errno);
1943 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
1944 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
1950 /********************************************************************
1952 * sifup - Config the interface up and enable IP packets to pass.
1959 memset (&ifr, '\0', sizeof (ifr));
1960 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1961 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1962 if (! ok_error (errno))
1963 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno);
1967 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
1968 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1969 if (! ok_error (errno))
1970 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno);
1977 /********************************************************************
1979 * sifdown - Config the interface down and disable IP.
1988 memset (&ifr, '\0', sizeof (ifr));
1989 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1990 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1991 if (! ok_error (errno))
1992 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno);
1996 ifr.ifr_flags &= ~IFF_UP;
1997 ifr.ifr_flags |= IFF_POINTOPOINT;
1998 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1999 if (! ok_error (errno))
2000 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2006 /********************************************************************
2008 * sifaddr - Config the interface IP addresses and netmask.
2011 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2017 memset (&ifr, '\0', sizeof (ifr));
2018 memset (&rt, '\0', sizeof (rt));
2020 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2021 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2022 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2024 strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2026 * Set our IP address
2028 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2029 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2030 if (errno != EEXIST) {
2031 if (! ok_error (errno))
2032 error("ioctl(SIOCSIFADDR): %m(%d)", errno);
2035 warn("ioctl(SIOCSIFADDR): Address already exists");
2040 * Set the gateway address
2042 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2043 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
2044 if (! ok_error (errno))
2045 error("ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2050 * For recent kernels, force the netmask to 255.255.255.255.
2052 if (kernel_version >= KVERSION(2,1,16))
2054 if (net_mask != 0) {
2055 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2056 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
2057 if (! ok_error (errno))
2058 error("ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2063 * Add the device route
2065 if (kernel_version < KVERSION(2,1,16)) {
2066 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2067 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2070 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2071 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2072 rt.rt_flags = RTF_UP | RTF_HOST;
2074 if (kernel_version > KVERSION(2,1,0)) {
2075 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2076 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2079 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
2080 if (! ok_error (errno))
2081 error("ioctl(SIOCADDRT) device route: %m(%d)", errno);
2088 /********************************************************************
2090 * cifaddr - Clear the interface IP addresses, and delete routes
2091 * through the interface if possible.
2094 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2098 if (kernel_version < KVERSION(2,1,16)) {
2100 * Delete the route through the device
2102 memset (&rt, '\0', sizeof (rt));
2104 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2105 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2108 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2109 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2110 rt.rt_flags = RTF_UP | RTF_HOST;
2112 if (kernel_version > KVERSION(2,1,0)) {
2113 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2114 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2117 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2118 if (still_ppp() && ! ok_error (errno))
2119 error("ioctl(SIOCDELRT) device route: %m(%d)", errno);
2127 * get_pty - get a pty master/slave pair and chown the slave side
2128 * to the uid given. Assumes slave_name points to >= 12 bytes of space.
2131 get_pty(master_fdp, slave_fdp, slave_name, uid)
2139 struct termios tios;
2142 for (i = 0; i < 64; ++i) {
2143 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
2144 'p' + i / 16, i % 16);
2145 mfd = open(pty_name, O_RDWR, 0);
2148 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
2157 strlcpy(slave_name, pty_name, 12);
2160 fchown(sfd, uid, -1);
2161 fchmod(sfd, S_IRUSR | S_IWUSR);
2162 if (tcgetattr(sfd, &tios) == 0) {
2163 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2164 tios.c_cflag |= CS8 | CREAD;
2165 tios.c_iflag = IGNPAR | CLOCAL;
2168 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
2169 warn("couldn't set attributes on pty: %m");
2171 warn("couldn't get attributes on pty: %m");
2176 /********************************************************************
2178 * open_loopback - open the device we use for getting packets
2179 * in demand mode. Under Linux, we use a pty master/slave pair.
2182 open_ppp_loopback(void)
2186 if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
2187 fatal("No free pty for loopback");
2188 SYSDEBUG(("using %s for loopback", loop_name));
2190 set_ppp_fd(slave_fd);
2192 flags = fcntl(master_fd, F_GETFL);
2194 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2195 warn("couldn't set master loopback to nonblock: %m(%d)", errno);
2197 flags = fcntl(ppp_fd, F_GETFL);
2199 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2200 warn("couldn't set slave loopback to nonblock: %m(%d)", errno);
2202 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2203 fatal("ioctl(TIOCSETD): %m(%d)", errno);
2205 * Find out which interface we were given.
2207 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2208 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno);
2210 * Enable debug in the driver if requested.
2212 set_kdebugflag (kdebugflag);
2217 /********************************************************************
2219 * restore_loop - reattach the ppp unit to the loopback.
2221 * The kernel ppp driver automatically reattaches the ppp unit to
2222 * the loopback if the serial port is set to a line discipline other
2223 * than ppp, or if it detects a modem hangup. The former will happen
2224 * in disestablish_ppp if the latter hasn't already happened, so we
2225 * shouldn't need to do anything.
2227 * Just to be sure, set the real serial port to the normal discipline.
2233 if (ppp_fd != slave_fd) {
2234 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2235 set_ppp_fd(slave_fd);
2239 /********************************************************************
2241 * sifnpmode - Set the mode for handling packets for a given NP.
2245 sifnpmode(u, proto, mode)
2252 npi.protocol = proto;
2254 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
2255 if (! ok_error (errno)) {
2256 error("ioctl(PPPIOCSNPMODE, %d, %d): %m (%d)",
2257 proto, mode, errno);
2258 error("ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2266 /********************************************************************
2268 * sipxfaddr - Config the interface IPX networknumber
2271 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2278 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2280 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2282 if (! ok_error (errno))
2283 dbglog("socket(AF_IPX): %m (%d)", errno);
2287 memset (&ifr, '\0', sizeof (ifr));
2288 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2290 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2291 sipx->sipx_family = AF_IPX;
2292 sipx->sipx_port = 0;
2293 sipx->sipx_network = htonl (network);
2294 sipx->sipx_type = IPX_FRAME_ETHERII;
2295 sipx->sipx_action = IPX_CRTITF;
2297 * Set the IPX device
2299 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2301 if (errno != EEXIST) {
2302 if (! ok_error (errno))
2303 dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (%d)", errno);
2306 warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists");
2315 /********************************************************************
2317 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2318 * are removed and the device is no longer able to pass IPX
2322 int cipxfaddr (int unit)
2329 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2331 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2333 if (! ok_error (errno))
2334 dbglog("socket(AF_IPX): %m (%d)", errno);
2338 memset (&ifr, '\0', sizeof (ifr));
2339 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2341 sipx->sipx_type = IPX_FRAME_ETHERII;
2342 sipx->sipx_action = IPX_DLTITF;
2343 sipx->sipx_family = AF_IPX;
2345 * Set the IPX device
2347 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2348 if (! ok_error (errno))
2349 info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (%d)", errno);
2359 * daemon - Detach us from controlling terminal session.
2362 daemon(nochdir, noclose)
2363 int nochdir, noclose;
2367 if ((pid = fork()) < 0)
2370 exit(0); /* parent dies */
2375 fclose(stdin); /* don't need stdin, stdout, stderr */
2383 * Use the hostname as part of the random number seed.
2392 for (p = hostname; *p != 0; ++p)
2397 /********************************************************************
2399 * sys_check_options - check the options that the user specified
2403 sys_check_options(void)
2406 struct stat stat_buf;
2408 * Disable the IPX protocol if the support is not present in the kernel.
2409 * If we disable it then ensure that IP support is enabled.
2411 while (ipxcp_protent.enabled_flag) {
2412 if (path_to_procfs()) {
2413 strlcat (route_buffer, "/net/ipx_interface", sizeof(route_buffer));
2414 if (lstat (route_buffer, &stat_buf) >= 0)
2417 error("IPX support is not present in the kernel\n");
2418 ipxcp_protent.enabled_flag = 0;
2419 ipcp_protent.enabled_flag = 1;
2423 if (demand && driver_is_old) {
2424 option_error("demand dialling is not supported by kernel driver "
2425 "version %d.%d.%d", driver_version, driver_modification,