2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
28 #include <sys/utsname.h>
43 /* This is in netdevice.h. However, this compile will fail miserably if
44 you attempt to include netdevice.h because it has so many references
45 to __memcpy functions which it should not attempt to do. So, since I
46 really don't use it, but it must be defined, define it now. */
49 #define MAX_ADDR_LEN 7
53 #include <linux/ppp_defs.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_ppp.h>
56 #include <linux/route.h>
57 #include <linux/if_ether.h>
58 #include <netinet/in.h>
64 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
73 #include <sys/locks.h>
76 #define ok_error(num) ((num)==EIO)
78 static int tty_disc = N_TTY; /* The TTY discipline */
79 static int ppp_disc = N_PPP; /* The PPP discpline */
80 static int initfdflags = -1; /* Initial file descriptor flags for fd */
81 static int ppp_fd = -1; /* fd which is set to PPP discipline */
82 static int sock_fd = -1; /* socket for doing interface ioctls */
83 static int slave_fd = -1;
84 static int master_fd = -1;
86 static int has_proxy_arp = 0;
87 static int driver_version = 0;
88 static int driver_modification = 0;
89 static int driver_patch = 0;
90 static int driver_is_old = 0;
91 static int restore_term = 0; /* 1 => we've munged the terminal */
92 static struct termios inittermios; /* Initial TTY termios */
94 static char loop_name[20];
95 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
97 static int if_is_up; /* Interface has been marked up */
98 static u_int32_t default_route_gateway; /* Gateway for default route added */
99 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
101 static char *lock_file;
103 static struct utsname utsname; /* for the kernel version */
107 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
108 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
109 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
111 /* Prototypes for procedures local to this file. */
112 static int get_flags (void);
113 static void set_flags (int flags);
114 static int translate_speed (int bps);
115 static int baud_rate_of (int speed);
116 static char *path_to_route (void);
117 static void close_route_table (void);
118 static int open_route_table (void);
119 static int read_route_table (struct rtentry *rt);
120 static int defaultroute_exists (struct rtentry *rt);
121 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
123 static void decode_version (char *buf, int *version, int *mod, int *patch);
125 extern u_char inpacket_buf[]; /* borrowed from main.c */
128 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
132 #define SET_SA_FAMILY(addr, family) \
133 memset ((char *) &(addr), '\0', sizeof(addr)); \
134 addr.sa_family = (family);
137 * Determine if the PPP connection should still be present.
141 #define still_ppp() (hungup == 0)
144 #define LOCK_PREFIX "/var/lock/LCK.."
147 /********************************************************************
149 * Functions to read and set the flags value in the device driver
152 static void set_ppp_fd (int new_fd)
154 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
158 /********************************************************************
160 * Functions to read and set the flags value in the device driver
163 static int get_flags (void)
167 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
169 if ( ok_error (errno) )
175 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
180 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
184 /********************************************************************/
186 static void set_flags (int flags)
188 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
190 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
192 if (! ok_error (errno) )
194 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
200 /********************************************************************
202 * sys_init - System-dependent initialization.
207 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
208 setlogmask(LOG_UPTO(LOG_INFO));
211 setlogmask(LOG_UPTO(LOG_DEBUG));
214 /* Get an internet socket for doing socket ioctls. */
215 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
218 if ( ! ok_error ( errno ))
220 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
228 /********************************************************************
230 * sys_cleanup - restore any system state we modified before exiting:
231 * mark the interface down, delete default route and/or proxy arp entry.
232 * This should call die() because it's called from die().
235 void sys_cleanup(void)
239 * Take down the device
246 * Delete any routes through the device.
248 if (default_route_gateway != 0)
250 cifdefaultroute(0, 0, default_route_gateway);
255 cifproxyarp(0, proxy_arp_addr);
259 /********************************************************************
261 * sys_close - Clean up in a child process before execing.
271 /********************************************************************
273 * note_debug_level - note a change in the debug level.
276 void note_debug_level (void)
280 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
281 setlogmask(LOG_UPTO(LOG_DEBUG));
285 setlogmask(LOG_UPTO(LOG_WARNING));
289 /********************************************************************
291 * set_kdebugflag - Define the debugging level for the kernel
294 int set_kdebugflag (int requested_level)
296 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
298 if ( ! ok_error (errno) )
300 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
304 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
309 /********************************************************************
311 * establish_ppp - Turn the serial port into a ppp interface.
314 void establish_ppp (int tty_fd)
318 * The current PPP device will be the tty file.
322 * Ensure that the tty device is in exclusive mode.
324 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
326 if ( ! ok_error ( errno ))
328 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
332 * Demand mode - prime the old ppp device to relinquish the unit.
334 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
336 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
340 * Set the current tty to the PPP discpline
342 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
344 if ( ! ok_error (errno) )
346 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
351 * Find out which interface we were given.
353 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
355 if ( ! ok_error (errno))
357 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
362 * Check that we got the same unit again.
368 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
376 * Enable debug in the driver if requested.
379 set_kdebugflag (kdebugflag);
381 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
382 SC_RCV_EVNP | SC_RCV_ODDP));
384 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
385 driver_version, driver_modification, driver_patch));
387 * Fetch the initial file flags and reset blocking mode on the file.
389 initfdflags = fcntl(ppp_fd, F_GETFL);
391 if (initfdflags == -1 ||
392 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
394 if ( ! ok_error (errno))
397 "Couldn't set device to non-blocking mode: %m");
402 /********************************************************************
404 * disestablish_ppp - Restore the serial port to normal operation.
405 * This shouldn't call die() because it's called from die().
408 void disestablish_ppp(int tty_fd)
414 * Attempt to restore the previous tty settings
419 * Restore the previous line discipline
421 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
423 if ( ! ok_error (errno))
425 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
429 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
431 if ( ! ok_error (errno))
433 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
437 /* Reset non-blocking mode on fd. */
438 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
440 if ( ! ok_error (errno))
443 "Couldn't restore device fd flags: %m");
450 /********************************************************************
452 * clean_check - Fetch the flags for the device and generate
453 * appropriate error messages.
455 void clean_check(void)
462 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
465 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
468 case SC_RCV_B7_0 | SC_RCV_EVNP:
469 case SC_RCV_B7_0 | SC_RCV_ODDP:
470 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
471 s = "all had bit 7 set to 1";
475 case SC_RCV_B7_1 | SC_RCV_EVNP:
476 case SC_RCV_B7_1 | SC_RCV_ODDP:
477 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
478 s = "all had bit 7 set to 0";
482 s = "all had odd parity";
486 s = "all had even parity";
492 syslog(LOG_WARNING, "Receive serial link is not"
494 syslog(LOG_WARNING, "Problem: %s", s);
502 * List of valid speeds.
506 int speed_int, speed_val;
577 /********************************************************************
579 * Translate from bits/second to a speed_t.
582 static int translate_speed (int bps)
584 struct speed *speedp;
588 for (speedp = speeds; speedp->speed_int; speedp++)
590 if (bps == speedp->speed_int)
592 return speedp->speed_val;
595 syslog(LOG_WARNING, "speed %d not supported", bps);
600 /********************************************************************
602 * Translate from a speed_t to bits/second.
605 static int baud_rate_of (int speed)
607 struct speed *speedp;
611 for (speedp = speeds; speedp->speed_int; speedp++)
613 if (speed == speedp->speed_val)
615 return speedp->speed_int;
622 /********************************************************************
624 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
625 * at the requested speed, etc. If `local' is true, set CLOCAL
626 * regardless of whether the modem option was specified.
629 void set_up_tty (int tty_fd, int local)
634 if (tcgetattr(tty_fd, &tios) < 0)
636 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
645 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
646 tios.c_cflag |= CS8 | CREAD | HUPCL;
648 tios.c_iflag = IGNBRK | IGNPAR;
652 tios.c_cc[VTIME] = 0;
656 tios.c_cflag ^= (CLOCAL | HUPCL);
662 tios.c_cflag |= CRTSCTS;
666 tios.c_iflag |= IXON | IXOFF;
667 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
668 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
672 tios.c_cflag &= ~CRTSCTS;
679 speed = translate_speed(inspeed);
682 cfsetospeed (&tios, speed);
683 cfsetispeed (&tios, speed);
686 * We can't proceed if the serial port speed is B0,
687 * since that implies that the serial port is disabled.
691 speed = cfgetospeed(&tios);
694 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
700 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
702 syslog(LOG_ERR, "tcsetattr: %m");
706 baud_rate = baud_rate_of(speed);
710 /********************************************************************
712 * setdtr - control the DTR line on the serial port.
713 * This is called from die(), so it shouldn't call die().
716 void setdtr (int tty_fd, int on)
718 int modembits = TIOCM_DTR;
720 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
723 /********************************************************************
725 * restore_tty - restore the terminal to the saved settings.
728 void restore_tty (int tty_fd)
734 * Turn off echoing, because otherwise we can get into
735 * a loop with the tty and the modem echoing to each other.
736 * We presume we are the sole user of this tty device, so
737 * when we close it, it will revert to its defaults anyway.
741 inittermios.c_lflag &= ~(ECHO | ECHONL);
744 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
746 if (! ok_error (errno))
748 syslog(LOG_WARNING, "tcsetattr: %m");
754 /********************************************************************
756 * output - Output PPP packet.
759 void output (int unit, unsigned char *p, int len)
763 log_packet(p, len, "sent ", LOG_DEBUG);
766 if (write(ppp_fd, p, len) < 0)
768 if (errno == EWOULDBLOCK || errno == ENOBUFS
769 || errno == ENXIO || errno == EIO)
771 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
775 syslog(LOG_ERR, "write: %m(%d)", errno);
781 /********************************************************************
783 * wait_input - wait until there is data available on ppp_fd,
784 * for the length of time specified by *timo (indefinite
788 void wait_input (struct timeval *timo)
794 FD_SET(ppp_fd, &ready);
796 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
797 if (n < 0 && errno != EINTR)
799 syslog(LOG_ERR, "select: %m(%d)", errno);
804 /********************************************************************
806 * wait_loop_output - wait until there is data available on the
807 * loopback, for the length of time specified by *timo (indefinite
810 void wait_loop_output(timo)
811 struct timeval *timo;
817 FD_SET(master_fd, &ready);
818 n = select(master_fd + 1, &ready, NULL, &ready, timo);
819 if (n < 0 && errno != EINTR)
821 syslog(LOG_ERR, "select: %m(%d)", errno);
826 /********************************************************************
828 * wait_time - wait for a given length of time or until a
829 * signal is received.
833 struct timeval *timo;
837 n = select(0, NULL, NULL, NULL, timo);
838 if (n < 0 && errno != EINTR) {
839 syslog(LOG_ERR, "select: %m(%d)", errno);
844 /********************************************************************
846 * read_packet - get a PPP packet from the serial device.
849 int read_packet (unsigned char *buf)
853 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
856 if (errno == EWOULDBLOCK)
860 syslog(LOG_ERR, "read: %m(%d)", errno);
866 /********************************************************************
868 * get_loop_output - get outgoing packets from the ppp device,
869 * and detect when we want to bring the real link up.
870 * Return value is 1 if we need to bring up the link, 0 otherwise.
873 get_loop_output(void)
876 int n = read(master_fd, inbuf, sizeof(inbuf));
880 if (loop_chars(inbuf, n))
884 n = read(master_fd, inbuf, sizeof(inbuf));
889 syslog(LOG_ERR, "eof on loopback");
893 if (errno != EWOULDBLOCK)
895 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
902 /********************************************************************
904 * ppp_send_config - configure the transmit characteristics of
908 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
913 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
915 * Ensure that the link is still up.
920 * Set the MTU and other parameters for the ppp device
922 memset (&ifr, '\0', sizeof (ifr));
923 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
926 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
928 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
932 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
933 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
935 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
940 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
941 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
946 /********************************************************************
948 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
951 void ppp_set_xaccm (int unit, ext_accm accm)
953 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
954 accm[0], accm[1], accm[2], accm[3]));
956 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
958 if ( ! ok_error (errno))
960 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
965 /********************************************************************
967 * ppp_recv_config - configure the receive-side characteristics of
971 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
975 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
977 * If we were called because the link has gone down then there is nothing
978 * which may be done. Just return without incident.
985 * Set the receiver parameters
987 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
989 if ( ! ok_error (errno))
991 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
995 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
996 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
998 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1003 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1007 /********************************************************************
1009 * ccp_test - ask kernel whether a given compression method
1010 * is acceptable for use.
1013 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1015 struct ppp_option_data data;
1017 memset (&data, '\0', sizeof (data));
1019 data.length = opt_len;
1020 data.transmit = for_transmit;
1022 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1027 return (errno == ENOBUFS)? 0: -1;
1030 /********************************************************************
1032 * ccp_flags_set - inform kernel about the current state of CCP.
1035 void ccp_flags_set (int unit, int isopen, int isup)
1039 int x = get_flags();
1040 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1041 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1046 /********************************************************************
1048 * get_idle_time - return how long the link has been idle.
1051 get_idle_time(u, ip)
1053 struct ppp_idle *ip;
1055 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1058 /********************************************************************
1060 * ccp_fatal_error - returns 1 if decompression was disabled as a
1061 * result of an error detected after decompression of a packet,
1062 * 0 otherwise. This is necessary because of patent nonsense.
1065 int ccp_fatal_error (int unit)
1067 int x = get_flags();
1069 return x & SC_DC_FERROR;
1073 * path_to_route - determine the path to the proc file system data
1076 FILE *route_fd = (FILE *) 0;
1077 static char route_buffer [512];
1079 static char *path_to_route (void);
1080 static int open_route_table (void);
1081 static void close_route_table (void);
1082 static int read_route_table (struct rtentry *rt);
1084 /********************************************************************
1086 * path_to_procfs - find the path to the proc file system mount point
1089 static int path_to_procfs (void)
1091 struct mntent *mntent;
1094 fp = fopen (MOUNTED, "r");
1097 mntent = getmntent (fp);
1098 while (mntent != (struct mntent *) 0)
1100 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1102 if (strcmp (mntent->mnt_type, "proc") == 0)
1104 strncpy (route_buffer, mntent->mnt_dir,
1105 sizeof (route_buffer)-10);
1106 route_buffer [sizeof (route_buffer)-10] = '\0';
1111 mntent = getmntent (fp);
1118 /********************************************************************
1120 * path_to_route - find the path to the route tables in the proc file system
1123 static char *path_to_route (void)
1125 if (! path_to_procfs())
1127 syslog (LOG_ERR, "proc file system not mounted");
1130 strcat (route_buffer, "/net/route");
1131 return (route_buffer);
1134 /********************************************************************
1136 * close_route_table - close the interface to the route table
1139 static void close_route_table (void)
1141 if (route_fd != (FILE *) 0)
1144 route_fd = (FILE *) 0;
1148 /********************************************************************
1150 * open_route_table - open the interface to the route table
1153 static int open_route_table (void)
1157 close_route_table();
1159 path = path_to_route();
1165 route_fd = fopen (path, "r");
1166 if (route_fd == (FILE *) 0)
1168 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1174 /********************************************************************
1176 * read_route_table - read the next entry from the route table
1179 static int read_route_table (struct rtentry *rt)
1181 static char delims[] = " \t\n";
1182 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1184 memset (rt, '\0', sizeof (struct rtentry));
1188 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1194 dev_ptr = strtok (route_buffer, delims); /* interface name */
1195 dst_ptr = strtok (NULL, delims); /* destination address */
1196 gw_ptr = strtok (NULL, delims); /* gateway */
1197 flag_ptr = strtok (NULL, delims); /* flags */
1199 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1204 /* Discard that stupid header line which should never
1205 * have been there in the first place !! */
1206 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1212 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1213 strtoul (dst_ptr, NULL, 16);
1215 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1216 strtoul (gw_ptr, NULL, 16);
1218 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1219 rt->rt_dev = dev_ptr;
1224 /********************************************************************
1226 * defaultroute_exists - determine if there is a default route
1229 static int defaultroute_exists (struct rtentry *rt)
1233 if (!open_route_table())
1238 while (read_route_table(rt) != 0)
1240 if ((rt->rt_flags & RTF_UP) == 0)
1245 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1252 close_route_table();
1256 /********************************************************************
1258 * sifdefaultroute - assign a default route through the address given.
1261 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1265 if (defaultroute_exists(&rt))
1267 struct in_addr old_gateway =
1268 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1270 if (old_gateway.s_addr != gateway)
1273 "not replacing existing default route to %s [%s]",
1275 inet_ntoa (old_gateway));
1280 memset (&rt, '\0', sizeof (rt));
1281 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1282 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1284 if (strcmp(utsname.release, "2.1.0") > 0) {
1285 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1286 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1289 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1291 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1292 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1294 if ( ! ok_error ( errno ))
1296 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1301 default_route_gateway = gateway;
1305 /********************************************************************
1307 * cifdefaultroute - delete a default route through the address given.
1310 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1314 default_route_gateway = 0;
1316 memset (&rt, '\0', sizeof (rt));
1317 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1318 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1320 if (strcmp(utsname.release, "2.1.0") > 0) {
1321 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1322 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1325 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1327 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1328 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1332 if ( ! ok_error ( errno ))
1335 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1344 /********************************************************************
1346 * sifproxyarp - Make a proxy ARP entry for the peer.
1349 int sifproxyarp (int unit, u_int32_t his_adr)
1351 struct arpreq arpreq;
1353 if (has_proxy_arp == 0)
1355 memset (&arpreq, '\0', sizeof(arpreq));
1357 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1358 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1359 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1361 * Get the hardware address of an interface on the same subnet
1362 * as our local address.
1364 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1366 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1370 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1372 if ( ! ok_error ( errno ))
1374 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1380 proxy_arp_addr = his_adr;
1385 /********************************************************************
1387 * cifproxyarp - Delete the proxy ARP entry for the peer.
1390 int cifproxyarp (int unit, u_int32_t his_adr)
1392 struct arpreq arpreq;
1394 if (has_proxy_arp == 1)
1396 memset (&arpreq, '\0', sizeof(arpreq));
1397 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1398 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1399 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1401 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1403 if ( ! ok_error ( errno ))
1405 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1414 /********************************************************************
1416 * get_ether_addr - get the hardware address of an interface on the
1417 * the same subnet as ipaddr.
1420 static int get_ether_addr (u_int32_t ipaddr,
1421 struct sockaddr *hwaddr,
1424 struct ifreq *ifr, *ifend, *ifp;
1426 u_int32_t ina, mask;
1429 struct ifreq ifs[MAX_IFS];
1431 ifc.ifc_len = sizeof(ifs);
1433 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1435 if ( ! ok_error ( errno ))
1437 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1442 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1443 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1445 * Scan through looking for an interface with an Internet
1446 * address on the same subnet as `ipaddr'.
1448 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1449 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1451 if (ifr->ifr_addr.sa_family == AF_INET)
1453 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1454 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1455 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1458 * Check that the interface is up, and not point-to-point
1461 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1466 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1471 * Get its netmask and check that it's on the right subnet.
1473 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1478 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1479 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1480 ip_ntoa(ina), ntohl(mask)));
1482 if (((ipaddr ^ ina) & mask) != 0)
1495 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1496 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1498 * Now get the hardware address.
1500 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1501 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1503 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1509 sizeof (struct sockaddr));
1511 SYSDEBUG ((LOG_DEBUG,
1512 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1513 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1514 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1515 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1516 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1517 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1518 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1519 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1520 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1524 /********************************************************************
1526 * Return user specified netmask, modified by any mask we might determine
1527 * for address `addr' (in network byte order).
1528 * Here we scan through the system's list of interfaces, looking for
1529 * any non-point-to-point interfaces which might appear to be on the same
1530 * network as `addr'. If we find any, we OR in their netmask to the
1531 * user-specified netmask.
1534 u_int32_t GetMask (u_int32_t addr)
1536 u_int32_t mask, nmask, ina;
1537 struct ifreq *ifr, *ifend, ifreq;
1539 struct ifreq ifs[MAX_IFS];
1543 if (IN_CLASSA(addr)) /* determine network mask for address class */
1545 nmask = IN_CLASSA_NET;
1549 if (IN_CLASSB(addr))
1551 nmask = IN_CLASSB_NET;
1555 nmask = IN_CLASSC_NET;
1559 /* class D nets are disallowed by bad_ip_adrs */
1560 mask = netmask | htonl(nmask);
1562 * Scan through the system's network interfaces.
1564 ifc.ifc_len = sizeof(ifs);
1566 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1568 if ( ! ok_error ( errno ))
1570 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1575 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1576 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1579 * Check the interface's internet address.
1581 if (ifr->ifr_addr.sa_family != AF_INET)
1585 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1586 if (((ntohl(ina) ^ addr) & nmask) != 0)
1591 * Check that the interface is up, and not point-to-point nor loopback.
1593 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1594 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1599 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1604 * Get its netmask and OR it into our mask.
1606 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1610 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1616 /********************************************************************
1618 * Internal routine to decode the version.modification.patch level
1621 static void decode_version (char *buf, int *version,
1622 int *modification, int *patch)
1624 *version = (int) strtoul (buf, &buf, 10);
1631 *modification = (int) strtoul (buf, &buf, 10);
1635 *patch = (int) strtoul (buf, &buf, 10);
1647 /********************************************************************
1649 * Procedure to determine if the PPP line dicipline is registered.
1653 ppp_registered(void)
1659 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1662 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1666 initfdflags = fcntl(local_fd, F_GETFL);
1667 if (initfdflags == -1)
1669 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1674 initfdflags &= ~O_NONBLOCK;
1675 fcntl(local_fd, F_SETFL, initfdflags);
1677 * Read the initial line dicipline and try to put the device into the
1680 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1682 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1687 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1689 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1694 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1696 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1705 /********************************************************************
1707 * ppp_available - check whether the system has any ppp interfaces
1708 * (in fact we check whether we can do an ioctl on ppp0).
1711 int ppp_available(void)
1716 int my_version, my_modification, my_patch;
1717 extern char *no_ppp_msg;
1719 * Open a socket for doing the ioctl operations.
1721 s = socket(AF_INET, SOCK_DGRAM, 0);
1727 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1728 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1730 * If the device did not exist then attempt to create one by putting the
1731 * current tty into the PPP discipline. If this works then obtain the
1732 * flags for the device again.
1736 if (ppp_registered())
1738 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1739 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1743 * Ensure that the hardware address is for PPP and not something else
1747 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1750 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1758 "This system lacks kernel support for PPP. This could be because\n"
1759 "the PPP kernel module is not loaded, or because the kernel is\n"
1760 "not configured for PPP. See the README.linux file in the\n"
1761 "ppp-2.3.2 distribution.\n";
1764 * This is the PPP device. Validate the version of the driver at this
1765 * point to ensure that this program will work with the driver.
1769 char abBuffer [1024];
1771 ifr.ifr_data = abBuffer;
1772 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1777 decode_version (abBuffer,
1779 &driver_modification,
1786 driver_modification =
1790 * Validate the version of the driver against the version that we used.
1792 decode_version (PPP_VERSION,
1797 /* The version numbers must match */
1798 if (driver_version != my_version)
1803 /* The modification levels must be legal */
1804 if (driver_modification < my_modification)
1806 if (driver_modification >= 2) {
1807 /* we can cope with 2.2.0 and above */
1817 sprintf (route_buffer,
1818 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1819 driver_version, driver_modification, driver_patch);
1821 no_ppp_msg = route_buffer;
1827 /********************************************************************
1829 * Update the wtmp file with the appropriate user name and tty device.
1832 void logwtmp (const char *line, const char *name, const char *host)
1835 struct utmp ut, *utp;
1836 pid_t mypid = getpid();
1838 * Update the signon database for users.
1839 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1841 utmpname(_PATH_UTMP);
1843 while ((utp = getutent()) && (utp->ut_pid != mypid))
1846 /* Is this call really necessary? There is another one after the 'put' */
1851 memcpy(&ut, utp, sizeof(ut));
1855 /* some gettys/telnetds don't initialize utmp... */
1856 memset(&ut, 0, sizeof(ut));
1859 if (ut.ut_id[0] == 0)
1861 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1864 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1865 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1869 ut.ut_type = USER_PROCESS;
1872 /* Insert the host name if one is supplied */
1875 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1878 /* Insert the IP address of the remote system if IP is enabled */
1879 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1881 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1882 sizeof(ut.ut_addr));
1885 /* CL: Makes sure that the logout works */
1886 if (*host == 0 && *name==0)
1894 * Update the wtmp file.
1896 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1899 flock(wtmp, LOCK_EX);
1901 /* we really should check for error on the write for a full disk! */
1902 write (wtmp, (char *)&ut, sizeof(ut));
1905 flock(wtmp, LOCK_UN);
1909 /********************************************************************
1910 * Code for locking/unlocking the serial device.
1911 * This code is derived from chat.c.
1915 * lock - create a lock file for the named device
1918 int lock (char *dev)
1922 lock_file = malloc(strlen(dev) + 1);
1923 if (lock_file == NULL)
1925 novm("lock file name");
1927 strcpy (lock_file, dev);
1928 result = mklock (dev, (void *) 0);
1932 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1941 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1949 char hdb_lock_buffer[12];
1954 p = strrchr(dev, '/');
1960 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1961 if (lock_file == NULL)
1963 novm("lock file name");
1966 strcpy (lock_file, LOCK_PREFIX);
1967 strcat (lock_file, dev);
1969 * Attempt to create the lock file at this point.
1973 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1978 sprintf (hdb_lock_buffer, "%010d\n", pid);
1979 write (fd, hdb_lock_buffer, 11);
1981 write(fd, &pid, sizeof (pid));
1987 * If the file exists then check to see if the pid is stale
1989 if (errno == EEXIST)
1991 fd = open(lock_file, O_RDONLY, 0);
1994 if (errno == ENOENT) /* This is just a timing problem. */
2001 /* Read the lock file to find out who has the device locked */
2002 n = read (fd, hdb_lock_buffer, 11);
2006 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2010 /* See the process still exists. */
2014 hdb_lock_buffer[n] = '\0';
2015 sscanf (hdb_lock_buffer, " %d", &pid);
2017 pid = ((int *) hdb_lock_buffer)[0];
2019 if (pid == 0 || pid == getpid()
2020 || (kill(pid, 0) == -1 && errno == ESRCH))
2026 /* If the process does not exist then try to remove the lock */
2027 if (n == 0 && unlink (lock_file) == 0)
2029 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2034 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2038 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2049 /********************************************************************
2051 * unlock - remove our lockfile
2059 (void) rmlock (lock_file, (void *) 0);
2068 /********************************************************************
2070 * sifvjcomp - config tcp header compression
2073 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2075 u_int x = get_flags();
2079 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2081 if (! ok_error (errno))
2083 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno);
2089 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2090 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2096 /********************************************************************
2098 * sifup - Config the interface up and enable IP packets to pass.
2105 memset (&ifr, '\0', sizeof (ifr));
2106 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2107 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2109 if (! ok_error (errno))
2111 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2116 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2117 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2119 if (! ok_error (errno))
2121 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2129 /********************************************************************
2131 * sifdown - Config the interface down and disable IP.
2140 memset (&ifr, '\0', sizeof (ifr));
2141 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2142 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2144 if (! ok_error (errno))
2146 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2151 ifr.ifr_flags &= ~IFF_UP;
2152 ifr.ifr_flags |= IFF_POINTOPOINT;
2153 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2155 if (! ok_error (errno))
2157 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2164 /********************************************************************
2166 * sifaddr - Config the interface IP addresses and netmask.
2169 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2175 memset (&ifr, '\0', sizeof (ifr));
2176 memset (&rt, '\0', sizeof (rt));
2178 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2179 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2180 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2182 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2184 * Set our IP address
2186 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2187 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2189 if (errno != EEXIST)
2191 if (! ok_error (errno))
2193 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2198 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2203 * Set the gateway address
2205 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2206 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2208 if (! ok_error (errno))
2210 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2216 * For recent kernels, force the netmask to 255.255.255.255.
2218 if (strcmp(utsname.release, "2.1.16") >= 0)
2222 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2223 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2225 if (! ok_error (errno))
2227 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2233 * Add the device route
2235 if (strcmp(utsname.release, "2.1.16") < 0) {
2236 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2237 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2240 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2241 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2242 rt.rt_flags = RTF_UP | RTF_HOST;
2244 if (strcmp(utsname.release, "2.1.0") > 0) {
2245 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2246 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2249 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2251 if (! ok_error (errno))
2253 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2261 /********************************************************************
2263 * cifaddr - Clear the interface IP addresses, and delete routes
2264 * through the interface if possible.
2267 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2271 if (strcmp(utsname.release, "2.1.16") < 0) {
2273 * Delete the route through the device
2275 memset (&rt, '\0', sizeof (rt));
2277 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2278 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2281 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2282 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2283 rt.rt_flags = RTF_UP | RTF_HOST;
2285 if (strcmp(utsname.release, "2.1.0") > 0) {
2286 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2287 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2290 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2292 if (still_ppp() && ! ok_error (errno))
2294 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2302 /********************************************************************
2304 * open_loopback - open the device we use for getting packets
2305 * in demand mode. Under Linux, we use our existing fd
2306 * to the ppp driver.
2309 open_ppp_loopback(void)
2312 struct termios tios;
2315 for (i = 0; i < 64; ++i) {
2316 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2317 master_fd = open(loop_name, O_RDWR, 0);
2321 if (master_fd < 0) {
2322 syslog(LOG_ERR, "No free pty for loopback");
2325 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2327 slave_fd = open(loop_name, O_RDWR, 0);
2329 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2333 set_ppp_fd(slave_fd);
2335 if (tcgetattr(ppp_fd, &tios) == 0)
2337 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2338 tios.c_cflag |= CS8 | CREAD;
2339 tios.c_iflag = IGNPAR | CLOCAL;
2342 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2344 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2348 flags = fcntl(master_fd, F_GETFL);
2350 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2352 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2355 flags = fcntl(ppp_fd, F_GETFL);
2357 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2359 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2362 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2364 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2368 * Find out which interface we were given.
2370 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2372 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2376 * Enable debug in the driver if requested.
2378 set_kdebugflag (kdebugflag);
2381 /********************************************************************
2383 * restore_loop - reattach the ppp unit to the loopback.
2385 * The kernel ppp driver automatically reattaches the ppp unit to
2386 * the loopback if the serial port is set to a line discipline other
2387 * than ppp, or if it detects a modem hangup. The former will happen
2388 * in disestablish_ppp if the latter hasn't already happened, so we
2389 * shouldn't need to do anything.
2391 * Just to be sure, set the real serial port to the normal discipline.
2397 if (ppp_fd != slave_fd)
2399 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2400 set_ppp_fd(slave_fd);
2404 /********************************************************************
2406 * sifnpmode - Set the mode for handling packets for a given NP.
2410 sifnpmode(u, proto, mode)
2417 npi.protocol = proto;
2419 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2421 if (! ok_error (errno))
2423 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2424 proto, mode, errno);
2425 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2433 #include <linux/ipx.h>
2435 /********************************************************************
2437 * sipxfaddr - Config the interface IPX networknumber
2440 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2446 struct sockaddr_ipx ipx_addr;
2448 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2450 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2453 if (! ok_error (errno))
2455 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2461 memset (&ifr, '\0', sizeof (ifr));
2462 strcpy (ifr.ifr_name, ifname);
2464 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2465 sipx->sipx_family = AF_IPX;
2466 sipx->sipx_port = 0;
2467 sipx->sipx_network = htonl (network);
2468 sipx->sipx_type = IPX_FRAME_ETHERII;
2469 sipx->sipx_action = IPX_CRTITF;
2471 * Set the IPX device
2473 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2476 if (errno != EEXIST)
2478 if (! ok_error (errno))
2481 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2486 syslog (LOG_WARNING,
2487 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2496 /********************************************************************
2498 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2499 * are removed and the device is no longer able to pass IPX
2503 int cipxfaddr (int unit)
2509 struct sockaddr_ipx ipx_addr;
2511 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2513 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2516 if (! ok_error (errno))
2518 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2524 memset (&ifr, '\0', sizeof (ifr));
2525 strcpy (ifr.ifr_name, ifname);
2527 sipx->sipx_type = IPX_FRAME_ETHERII;
2528 sipx->sipx_action = IPX_DLTITF;
2529 sipx->sipx_family = AF_IPX;
2531 * Set the IPX device
2533 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2535 if (! ok_error (errno))
2538 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2549 * daemon - Detach us from controlling terminal session.
2552 daemon(nochdir, noclose)
2553 int nochdir, noclose;
2557 if ((pid = fork()) < 0)
2560 exit(0); /* parent dies */
2565 fclose(stdin); /* don't need stdin, stdout, stderr */
2572 /********************************************************************
2574 * sys_check_options - check the options that the user specified
2578 sys_check_options(void)
2581 struct stat stat_buf;
2583 * Disable the IPX protocol if the support is not present in the kernel.
2584 * If we disable it then ensure that IP support is enabled.
2586 while (ipxcp_protent.enabled_flag)
2588 if (path_to_procfs())
2590 strcat (route_buffer, "/net/ipx_interface");
2591 if (lstat (route_buffer, &stat_buf) >= 0)
2596 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2597 ipxcp_protent.enabled_flag = 0;
2598 ipcp_protent.enabled_flag = 1;
2602 if (demand && driver_is_old) {
2603 option_error("demand dialling is not supported by kernel driver version "
2604 "%d.%d.%d", driver_version, driver_modification,