2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
41 /* This is in netdevice.h. However, this compile will fail miserably if
42 you attempt to include netdevice.h because it has so many references
43 to __memcpy functions which it should not attempt to do. So, since I
44 really don't use it, but it must be defined, define it now. */
47 #define MAX_ADDR_LEN 7
51 #include <linux/ppp_defs.h>
52 #include <net/if_arp.h>
53 #include <linux/if_ppp.h>
54 #include <net/route.h>
55 #include <linux/if_ether.h>
56 #include <netinet/in.h>
66 #define ok_error(num) ((num)==EIO)
68 static int tty_disc = N_TTY; /* The TTY discipline */
69 static int ppp_disc = N_PPP; /* The PPP discpline */
70 static int initfdflags = -1; /* Initial file descriptor flags for fd */
71 static int ppp_fd = -1; /* fd which is set to PPP discipline */
72 static int sock_fd = -1; /* socket for doing interface ioctls */
73 static int slave_fd = -1;
74 static int master_fd = -1;
76 static int has_proxy_arp = 0;
77 static int driver_version = 0;
78 static int driver_modification = 0;
79 static int driver_patch = 0;
80 static int restore_term = 0; /* 1 => we've munged the terminal */
81 static struct termios inittermios; /* Initial TTY termios */
83 static char loop_name[20];
84 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
86 static int if_is_up; /* Interface has been marked up */
87 static u_int32_t default_route_gateway; /* Gateway for default route added */
88 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
90 static char *lock_file;
94 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
95 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
96 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
98 /* Prototypes for procedures local to this file. */
99 static int get_flags (void);
100 static void set_flags (int flags);
101 static int translate_speed (int bps);
102 static int baud_rate_of (int speed);
103 static char *path_to_route (void);
104 static void close_route_table (void);
105 static int open_route_table (void);
106 static int read_route_table (struct rtentry *rt);
107 static int defaultroute_exists (struct rtentry *rt);
108 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
110 static void decode_version (char *buf, int *version, int *mod, int *patch);
112 extern u_char inpacket_buf[]; /* borrowed from main.c */
115 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
119 #define SET_SA_FAMILY(addr, family) \
120 memset ((char *) &(addr), '\0', sizeof(addr)); \
121 addr.sa_family = (family);
124 * Determine if the PPP connection should still be present.
128 #define still_ppp() (hungup == 0)
131 #define LOCK_PREFIX "/var/lock/LCK.."
134 /********************************************************************
136 * Functions to read and set the flags value in the device driver
139 static void set_ppp_fd (int new_fd)
141 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
145 /********************************************************************
147 * Functions to read and set the flags value in the device driver
150 static int get_flags (void)
154 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
156 if ( ok_error (errno) )
162 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
167 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
171 /********************************************************************/
173 static void set_flags (int flags)
175 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
177 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
179 if (! ok_error (errno) )
181 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
187 /********************************************************************
189 * sys_init - System-dependent initialization.
194 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
195 setlogmask(LOG_UPTO(LOG_INFO));
198 setlogmask(LOG_UPTO(LOG_DEBUG));
201 /* Get an internet socket for doing socket ioctls. */
202 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
205 if ( ! ok_error ( errno ))
207 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
213 /********************************************************************
215 * sys_cleanup - restore any system state we modified before exiting:
216 * mark the interface down, delete default route and/or proxy arp entry.
217 * This should call die() because it's called from die().
220 void sys_cleanup(void)
224 * Take down the device
231 * Delete any routes through the device.
233 if (default_route_gateway != 0)
235 cifdefaultroute(0, default_route_gateway);
240 cifproxyarp(0, proxy_arp_addr);
244 /********************************************************************
246 * sys_close - Clean up in a child process before execing.
256 /********************************************************************
258 * note_debug_level - note a change in the debug level.
261 void note_debug_level (void)
265 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
266 setlogmask(LOG_UPTO(LOG_DEBUG));
270 setlogmask(LOG_UPTO(LOG_WARNING));
274 /********************************************************************
276 * set_kdebugflag - Define the debugging level for the kernel
279 int set_kdebugflag (int requested_level)
281 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
283 if ( ! ok_error (errno) )
285 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
289 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
294 /********************************************************************
296 * establish_ppp - Turn the serial port into a ppp interface.
299 void establish_ppp (int tty_fd)
303 * The current PPP device will be the tty file.
307 * Ensure that the tty device is in exclusive mode.
309 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
311 if ( ! ok_error ( errno ))
313 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
317 * Demand mode - prime the old ppp device to relinquish the unit.
319 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
321 if ( ! ok_error ( errno ))
323 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
328 * Set the current tty to the PPP discpline
330 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
332 if ( ! ok_error (errno) )
334 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
339 * Find out which interface we were given.
341 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
343 if ( ! ok_error (errno))
345 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
350 * Check that we got the same unit again.
356 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
360 ioctl(slave_fd, TIOCSETD, &tty_disc);
365 * Enable debug in the driver if requested.
367 set_kdebugflag (kdebugflag);
369 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
370 SC_RCV_EVNP | SC_RCV_ODDP));
372 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
373 driver_version, driver_modification, driver_patch));
375 * Fetch the initial file flags and reset blocking mode on the file.
377 initfdflags = fcntl(ppp_fd, F_GETFL);
379 if (initfdflags == -1 ||
380 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
382 if ( ! ok_error (errno))
385 "Couldn't set device to non-blocking mode: %m");
390 /********************************************************************
392 * disestablish_ppp - Restore the serial port to normal operation.
393 * This shouldn't call die() because it's called from die().
396 void disestablish_ppp(int tty_fd)
401 * Do nothing if the PPP device is controlled by the loopback device
403 if (tty_fd != ppp_fd)
408 * Attempt to restore the previous tty settings
414 * Restore the previous line discipline
416 if (ioctl(ppp_fd, TIOCSETD, &tty_disc) < 0)
418 if ( ! ok_error (errno))
420 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
424 if (ioctl(ppp_fd, TIOCNXCL, 0) < 0)
426 if ( ! ok_error (errno))
428 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
432 /* Reset non-blocking mode on fd. */
433 if (initfdflags != -1 && fcntl(ppp_fd, F_SETFL, initfdflags) < 0)
435 if ( ! ok_error (errno))
438 "Couldn't restore device fd flags: %m");
445 /********************************************************************
447 * clean_check - Fetch the flags for the device and generate
448 * appropriate error messages.
450 void clean_check(void)
457 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
460 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
462 case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP:
463 s = "nothing was received";
467 case SC_RCV_B7_0 | SC_RCV_EVNP:
468 case SC_RCV_B7_0 | SC_RCV_ODDP:
469 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
470 s = "all had bit 7 set to 1";
474 case SC_RCV_B7_1 | SC_RCV_EVNP:
475 case SC_RCV_B7_1 | SC_RCV_ODDP:
476 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
477 s = "all had bit 7 set to 0";
481 s = "all had odd parity";
485 s = "all had even parity";
491 syslog(LOG_WARNING, "Receive serial link is not"
493 syslog(LOG_WARNING, "Problem: %s", s);
501 * List of valid speeds.
505 int speed_int, speed_val;
576 /********************************************************************
578 * Translate from bits/second to a speed_t.
581 static int translate_speed (int bps)
583 struct speed *speedp;
587 for (speedp = speeds; speedp->speed_int; speedp++)
589 if (bps == speedp->speed_int)
591 return speedp->speed_val;
594 syslog(LOG_WARNING, "speed %d not supported", bps);
599 /********************************************************************
601 * Translate from a speed_t to bits/second.
604 static int baud_rate_of (int speed)
606 struct speed *speedp;
610 for (speedp = speeds; speedp->speed_int; speedp++)
612 if (speed == speedp->speed_val)
614 return speedp->speed_int;
621 /********************************************************************
623 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
624 * at the requested speed, etc. If `local' is true, set CLOCAL
625 * regardless of whether the modem option was specified.
628 void set_up_tty (int tty_fd, int local)
633 if (tcgetattr(tty_fd, &tios) < 0)
635 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
644 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
645 tios.c_cflag |= CS8 | CREAD | HUPCL;
647 tios.c_iflag = IGNBRK | IGNPAR;
651 tios.c_cc[VTIME] = 0;
655 tios.c_cflag ^= (CLOCAL | HUPCL);
661 tios.c_cflag |= CRTSCTS;
665 tios.c_iflag |= IXON | IXOFF;
666 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
667 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
671 tios.c_cflag &= ~CRTSCTS;
678 speed = translate_speed(inspeed);
681 cfsetospeed (&tios, speed);
682 cfsetispeed (&tios, speed);
685 * We can't proceed if the serial port speed is B0,
686 * since that implies that the serial port is disabled.
690 speed = cfgetospeed(&tios);
693 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
699 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
701 syslog(LOG_ERR, "tcsetattr: %m");
705 baud_rate = baud_rate_of(speed);
709 /********************************************************************
711 * setdtr - control the DTR line on the serial port.
712 * This is called from die(), so it shouldn't call die().
715 void setdtr (int tty_fd, int on)
717 int modembits = TIOCM_DTR;
719 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
722 /********************************************************************
724 * restore_tty - restore the terminal to the saved settings.
727 void restore_tty (int tty_fd)
733 * Turn off echoing, because otherwise we can get into
734 * a loop with the tty and the modem echoing to each other.
735 * We presume we are the sole user of this tty device, so
736 * when we close it, it will revert to its defaults anyway.
740 inittermios.c_lflag &= ~(ECHO | ECHONL);
743 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
745 if (! ok_error (errno))
747 syslog(LOG_WARNING, "tcsetattr: %m");
753 /********************************************************************
755 * output - Output PPP packet.
758 void output (int unit, unsigned char *p, int len)
762 log_packet(p, len, "sent ");
765 if (write(ppp_fd, p, len) < 0)
767 if (errno == EWOULDBLOCK || errno == ENOBUFS
768 || errno == ENXIO || errno == EIO)
770 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
774 syslog(LOG_ERR, "write: %m(%d)", errno);
780 /********************************************************************
782 * wait_input - wait until there is data available on ppp_fd,
783 * for the length of time specified by *timo (indefinite
787 void wait_input (struct timeval *timo)
793 FD_SET(ppp_fd, &ready);
795 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
796 if (n < 0 && errno != EINTR)
798 syslog(LOG_ERR, "select: %m(%d)", errno);
803 /********************************************************************
805 * wait_loop_output - wait until there is data available on the
806 * loopback, for the length of time specified by *timo (indefinite
809 void wait_loop_output(timo)
810 struct timeval *timo;
816 FD_SET(master_fd, &ready);
817 n = select(master_fd + 1, &ready, NULL, &ready, timo);
818 if (n < 0 && errno != EINTR)
820 syslog(LOG_ERR, "select: %m(%d)", errno);
825 /********************************************************************
827 * wait_time - wait for a given length of time or until a
828 * signal is received.
832 struct timeval *timo;
836 n = select(0, NULL, NULL, NULL, timo);
837 if (n < 0 && errno != EINTR) {
838 syslog(LOG_ERR, "select: %m(%d)", errno);
843 /********************************************************************
845 * read_packet - get a PPP packet from the serial device.
848 int read_packet (unsigned char *buf)
852 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
855 if (errno == EWOULDBLOCK)
859 syslog(LOG_ERR, "read: %m(%d)", errno);
865 /********************************************************************
867 * get_loop_output - get outgoing packets from the ppp device,
868 * and detect when we want to bring the real link up.
869 * Return value is 1 if we need to bring up the link, 0 otherwise.
872 get_loop_output(void)
875 int n = read(master_fd, inbuf, sizeof(inbuf));
879 if (loop_chars(inbuf, n))
883 n = read(master_fd, inbuf, sizeof(inbuf));
888 syslog(LOG_ERR, "eof on loopback");
892 if (errno != EWOULDBLOCK)
894 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
901 /********************************************************************
903 * ppp_send_config - configure the transmit characteristics of
907 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
912 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
914 * Ensure that the link is still up.
919 * Set the MTU and other parameters for the ppp device
921 memset (&ifr, '\0', sizeof (ifr));
922 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
925 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
927 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
931 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
932 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
934 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
939 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
940 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
945 /********************************************************************
947 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
950 void ppp_set_xaccm (int unit, ext_accm accm)
952 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
953 accm[0], accm[1], accm[2], accm[3]));
955 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
957 if ( ! ok_error (errno))
959 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
964 /********************************************************************
966 * ppp_recv_config - configure the receive-side characteristics of
970 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
974 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
976 * If we were called because the link has gone down then there is nothing
977 * which may be done. Just return without incident.
984 * Set the receiver parameters
986 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
988 if ( ! ok_error (errno))
990 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
994 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
995 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
997 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1002 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1006 /********************************************************************
1008 * ccp_test - ask kernel whether a given compression method
1009 * is acceptable for use.
1012 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1014 struct ppp_option_data data;
1016 memset (&data, '\0', sizeof (data));
1018 data.length = opt_len;
1019 data.transmit = for_transmit;
1021 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1026 return (errno == ENOBUFS)? 0: -1;
1029 /********************************************************************
1031 * ccp_flags_set - inform kernel about the current state of CCP.
1034 void ccp_flags_set (int unit, int isopen, int isup)
1038 int x = get_flags();
1039 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1040 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1045 /********************************************************************
1047 * get_idle_time - return how long the link has been idle.
1050 get_idle_time(u, ip)
1052 struct ppp_idle *ip;
1054 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1057 /********************************************************************
1059 * ccp_fatal_error - returns 1 if decompression was disabled as a
1060 * result of an error detected after decompression of a packet,
1061 * 0 otherwise. This is necessary because of patent nonsense.
1064 int ccp_fatal_error (int unit)
1066 int x = get_flags();
1068 return x & SC_DC_FERROR;
1072 * path_to_route - determine the path to the proc file system data
1075 FILE *route_fd = (FILE *) 0;
1076 static char route_buffer [512];
1078 static char *path_to_route (void);
1079 static int open_route_table (void);
1080 static void close_route_table (void);
1081 static int read_route_table (struct rtentry *rt);
1083 /********************************************************************
1085 * path_to_procfs - find the path to the proc file system mount point
1088 static int path_to_procfs (void)
1090 struct mntent *mntent;
1093 fp = fopen (MOUNTED, "r");
1096 mntent = getmntent (fp);
1097 while (mntent != (struct mntent *) 0)
1099 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1101 if (strcmp (mntent->mnt_type, "proc") == 0)
1103 strncpy (route_buffer, mntent->mnt_dir,
1104 sizeof (route_buffer)-10);
1105 route_buffer [sizeof (route_buffer)-10] = '\0';
1110 mntent = getmntent (fp);
1117 /********************************************************************
1119 * path_to_route - find the path to the route tables in the proc file system
1122 static char *path_to_route (void)
1124 if (! path_to_procfs())
1126 syslog (LOG_ERR, "proc file system not mounted");
1129 strcat (route_buffer, "/net/route");
1130 return (route_buffer);
1133 /********************************************************************
1135 * close_route_table - close the interface to the route table
1138 static void close_route_table (void)
1140 if (route_fd != (FILE *) 0)
1143 route_fd = (FILE *) 0;
1147 /********************************************************************
1149 * open_route_table - open the interface to the route table
1152 static int open_route_table (void)
1156 close_route_table();
1158 path = path_to_route();
1164 route_fd = fopen (path, "r");
1165 if (route_fd == (FILE *) 0)
1167 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1173 /********************************************************************
1175 * read_route_table - read the next entry from the route table
1178 static int read_route_table (struct rtentry *rt)
1180 static char delims[] = " \t\n";
1181 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1183 memset (rt, '\0', sizeof (struct rtentry));
1187 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1193 dev_ptr = strtok (route_buffer, delims); /* interface name */
1194 dst_ptr = strtok (NULL, delims); /* destination address */
1195 gw_ptr = strtok (NULL, delims); /* gateway */
1196 flag_ptr = strtok (NULL, delims); /* flags */
1198 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1203 /* Discard that stupid header line which should never
1204 * have been there in the first place !! */
1205 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1211 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1212 strtoul (dst_ptr, NULL, 16);
1214 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1215 strtoul (gw_ptr, NULL, 16);
1217 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1218 rt->rt_dev = dev_ptr;
1223 /********************************************************************
1225 * defaultroute_exists - determine if there is a default route
1228 static int defaultroute_exists (struct rtentry *rt)
1232 if (!open_route_table())
1237 while (read_route_table(rt) != 0)
1239 if ((rt->rt_flags & RTF_UP) == 0)
1244 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1251 close_route_table();
1255 /********************************************************************
1257 * sifdefaultroute - assign a default route through the address given.
1260 int sifdefaultroute (int unit, u_int32_t gateway)
1264 if (defaultroute_exists(&rt))
1266 u_int32_t old_gateway = ((struct sockaddr_in *) (&rt.rt_gateway))->
1269 if (old_gateway != gateway)
1272 "ppp not replacing existing default route to %s[%s]",
1274 inet_ntoa (old_gateway));
1279 memset (&rt, '\0', sizeof (rt));
1280 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1281 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1282 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1284 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1285 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1287 if ( ! ok_error ( errno ))
1289 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1294 default_route_gateway = gateway;
1298 /********************************************************************
1300 * cifdefaultroute - delete a default route through the address given.
1303 int cifdefaultroute (int unit, u_int32_t gateway)
1307 default_route_gateway = 0;
1309 memset (&rt, '\0', sizeof (rt));
1310 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1311 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1312 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1314 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1315 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1319 if ( ! ok_error ( errno ))
1322 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1331 /********************************************************************
1333 * sifproxyarp - Make a proxy ARP entry for the peer.
1336 int sifproxyarp (int unit, u_int32_t his_adr)
1338 struct arpreq arpreq;
1340 if (has_proxy_arp == 0)
1342 memset (&arpreq, '\0', sizeof(arpreq));
1344 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1345 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1346 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1348 * Get the hardware address of an interface on the same subnet
1349 * as our local address.
1351 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1353 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1357 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1359 if ( ! ok_error ( errno ))
1361 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1367 proxy_arp_addr = his_adr;
1372 /********************************************************************
1374 * cifproxyarp - Delete the proxy ARP entry for the peer.
1377 int cifproxyarp (int unit, u_int32_t his_adr)
1379 struct arpreq arpreq;
1381 if (has_proxy_arp == 1)
1383 memset (&arpreq, '\0', sizeof(arpreq));
1384 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1386 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1387 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1389 if ( ! ok_error ( errno ))
1391 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1400 /********************************************************************
1402 * get_ether_addr - get the hardware address of an interface on the
1403 * the same subnet as ipaddr.
1406 static int get_ether_addr (u_int32_t ipaddr,
1407 struct sockaddr *hwaddr,
1410 struct ifreq *ifr, *ifend, *ifp;
1412 u_int32_t ina, mask;
1413 struct sockaddr_dl *dla;
1416 struct ifreq ifs[MAX_IFS];
1418 ifc.ifc_len = sizeof(ifs);
1420 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1422 if ( ! ok_error ( errno ))
1424 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1429 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1430 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1432 * Scan through looking for an interface with an Internet
1433 * address on the same subnet as `ipaddr'.
1435 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1436 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1438 if (ifr->ifr_addr.sa_family == AF_INET)
1440 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1441 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1442 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1445 * Check that the interface is up, and not point-to-point
1448 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1453 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1458 * Get its netmask and check that it's on the right subnet.
1460 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1465 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1466 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1467 ip_ntoa(ina), ntohl(mask)));
1469 if (((ipaddr ^ ina) & mask) != 0)
1482 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1483 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1485 * Now get the hardware address.
1487 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1488 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1490 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1496 sizeof (struct sockaddr));
1498 SYSDEBUG ((LOG_DEBUG,
1499 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1500 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1501 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1502 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1503 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1504 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1505 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1506 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1507 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1511 /********************************************************************
1513 * Return user specified netmask, modified by any mask we might determine
1514 * for address `addr' (in network byte order).
1515 * Here we scan through the system's list of interfaces, looking for
1516 * any non-point-to-point interfaces which might appear to be on the same
1517 * network as `addr'. If we find any, we OR in their netmask to the
1518 * user-specified netmask.
1521 u_int32_t GetMask (u_int32_t addr)
1523 u_int32_t mask, nmask, ina;
1524 struct ifreq *ifr, *ifend, ifreq;
1526 struct ifreq ifs[MAX_IFS];
1530 if (IN_CLASSA(addr)) /* determine network mask for address class */
1532 nmask = IN_CLASSA_NET;
1536 if (IN_CLASSB(addr))
1538 nmask = IN_CLASSB_NET;
1542 nmask = IN_CLASSC_NET;
1546 /* class D nets are disallowed by bad_ip_adrs */
1547 mask = netmask | htonl(nmask);
1549 * Scan through the system's network interfaces.
1551 ifc.ifc_len = sizeof(ifs);
1553 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1555 if ( ! ok_error ( errno ))
1557 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1562 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1563 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1566 * Check the interface's internet address.
1568 if (ifr->ifr_addr.sa_family != AF_INET)
1572 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1573 if (((ntohl(ina) ^ addr) & nmask) != 0)
1578 * Check that the interface is up, and not point-to-point nor loopback.
1580 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1581 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1586 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1591 * Get its netmask and OR it into our mask.
1593 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1597 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1603 /********************************************************************
1605 * Internal routine to decode the version.modification.patch level
1608 static void decode_version (char *buf, int *version,
1609 int *modification, int *patch)
1611 *version = (int) strtoul (buf, &buf, 10);
1618 *modification = (int) strtoul (buf, &buf, 10);
1622 *patch = (int) strtoul (buf, &buf, 10);
1634 /********************************************************************
1636 * Procedure to determine if the PPP line dicipline is registered.
1640 ppp_registered(void)
1646 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1649 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1653 initfdflags = fcntl(local_fd, F_GETFL);
1654 if (initfdflags == -1)
1656 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1661 initfdflags &= ~O_NONBLOCK;
1662 fcntl(local_fd, F_SETFL, initfdflags);
1664 * Read the initial line dicipline and try to put the device into the
1667 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1669 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1674 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1676 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1681 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1683 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1692 /********************************************************************
1694 * ppp_available - check whether the system has any ppp interfaces
1695 * (in fact we check whether we can do an ioctl on ppp0).
1698 int ppp_available(void)
1703 int my_version, my_modification, my_patch;
1704 extern char *no_ppp_msg;
1706 * Open a socket for doing the ioctl operations.
1708 s = socket(AF_INET, SOCK_DGRAM, 0);
1714 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1715 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1717 * If the device did not exist then attempt to create one by putting the
1718 * current tty into the PPP discipline. If this works then obtain the
1719 * flags for the device again.
1723 if (ppp_registered())
1725 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1726 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1730 * Ensure that the hardware address is for PPP and not something else
1734 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1737 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1745 "This system lacks kernel support for PPP. To include PPP support\n"
1746 "in the kernel, please follow the steps detailed in the "
1747 "README.linux\nfile in the ppp-2.3 distribution.\n";
1750 * This is the PPP device. Validate the version of the driver at this
1751 * point to ensure that this program will work with the driver.
1755 char abBuffer [1024];
1757 ifr.ifr_data = abBuffer;
1758 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1763 decode_version (abBuffer,
1765 &driver_modification,
1772 driver_modification =
1776 * Validate the version of the driver against the version that we used.
1778 decode_version (PPP_VERSION,
1783 /* The version numbers must match */
1784 if (driver_version != my_version)
1789 /* The modification levels must be legal */
1790 if (driver_modification < my_modification)
1798 sprintf (route_buffer,
1799 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1800 driver_version, driver_modification, driver_patch);
1802 no_ppp_msg = route_buffer;
1808 /********************************************************************
1810 * Update the wtmp file with the appropriate user name and tty device.
1813 int logwtmp (char *line, char *name, char *host)
1816 struct utmp ut, *utp;
1817 pid_t mypid = getpid();
1819 * Update the signon database for users.
1820 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1822 utmpname(_PATH_UTMP);
1824 while ((utp = getutent()) && (utp->ut_pid != mypid))
1827 /* Is this call really necessary? There is another one after the 'put' */
1832 memcpy(&ut, utp, sizeof(ut));
1836 /* some gettys/telnetds don't initialize utmp... */
1837 memset(&ut, 0, sizeof(ut));
1840 if (ut.ut_id[0] == 0)
1842 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1845 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1846 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1850 ut.ut_type = USER_PROCESS;
1853 /* Insert the host name if one is supplied */
1856 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1859 /* Insert the IP address of the remote system if IP is enabled */
1860 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1862 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1863 sizeof(ut.ut_addr));
1866 /* CL: Makes sure that the logout works */
1867 if (*host == 0 && *name==0)
1875 * Update the wtmp file.
1877 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1880 flock(wtmp, LOCK_EX);
1882 /* we really should check for error on the write for a full disk! */
1883 write (wtmp, (char *)&ut, sizeof(ut));
1886 flock(wtmp, LOCK_UN);
1890 /********************************************************************
1891 * Code for locking/unlocking the serial device.
1892 * This code is derived from chat.c.
1896 * lock - create a lock file for the named device
1899 int lock (char *dev)
1901 char hdb_lock_buffer[12];
1906 p = strrchr(dev, '/');
1912 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1913 if (lock_file == NULL)
1915 novm("lock file name");
1918 strcpy (lock_file, LOCK_PREFIX);
1919 strcat (lock_file, dev);
1921 * Attempt to create the lock file at this point.
1925 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1930 sprintf (hdb_lock_buffer, "%010d\n", pid);
1931 write (fd, hdb_lock_buffer, 11);
1933 write(fd, &pid, sizeof (pid));
1939 * If the file exists then check to see if the pid is stale
1941 if (errno == EEXIST)
1943 fd = open(lock_file, O_RDONLY, 0);
1946 if (errno == ENOENT) /* This is just a timing problem. */
1953 /* Read the lock file to find out who has the device locked */
1954 n = read (fd, hdb_lock_buffer, 11);
1958 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1962 /* See the process still exists. */
1966 hdb_lock_buffer[n] = '\0';
1967 sscanf (hdb_lock_buffer, " %d", &pid);
1969 pid = ((int *) hdb_lock_buffer)[0];
1971 if (pid == 0 || (kill(pid, 0) == -1 && errno == ESRCH))
1977 /* If the process does not exist then try to remove the lock */
1978 if (n == 0 && unlink (lock_file) == 0)
1980 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1985 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
1989 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
1999 /********************************************************************
2001 * unlock - remove our lockfile
2014 /********************************************************************
2016 * sifvjcomp - config tcp header compression
2019 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2021 u_int x = get_flags();
2025 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2027 if (! ok_error (errno))
2029 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno);
2035 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2036 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2042 /********************************************************************
2044 * sifup - Config the interface up and enable IP packets to pass.
2051 memset (&ifr, '\0', sizeof (ifr));
2052 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2053 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2055 if (! ok_error (errno))
2057 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2062 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2063 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2065 if (! ok_error (errno))
2067 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2075 /********************************************************************
2077 * sifdown - Config the interface down and disable IP.
2086 memset (&ifr, '\0', sizeof (ifr));
2087 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2088 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2090 if (! ok_error (errno))
2092 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2097 ifr.ifr_flags &= ~IFF_UP;
2098 ifr.ifr_flags |= IFF_POINTOPOINT;
2099 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2101 if (! ok_error (errno))
2103 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2110 /********************************************************************
2112 * sifaddr - Config the interface IP addresses and netmask.
2115 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2121 memset (&ifr, '\0', sizeof (ifr));
2122 memset (&rt, '\0', sizeof (rt));
2124 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2125 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2126 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2128 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2130 * Set our IP address
2132 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2133 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2135 if (errno != EEXIST)
2137 if (! ok_error (errno))
2139 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2144 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2149 * Set the gateway address
2151 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2152 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2154 if (! ok_error (errno))
2156 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2165 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2166 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2168 if (! ok_error (errno))
2170 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2176 * Add the device route
2178 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2179 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2180 rt.rt_dev = ifname; /* MJC */
2182 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2183 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2184 rt.rt_flags = RTF_UP | RTF_HOST;
2186 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2188 if (! ok_error (errno))
2190 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2197 /********************************************************************
2199 * cifaddr - Clear the interface IP addresses, and delete routes
2200 * through the interface if possible.
2203 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2207 * Delete the route through the device
2209 memset (&rt, '\0', sizeof (rt));
2211 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2212 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2213 rt.rt_dev = ifname; /* MJC */
2215 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2216 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2217 rt.rt_flags = RTF_UP | RTF_HOST;
2219 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2221 if (still_ppp() && ! ok_error (errno))
2223 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2230 /********************************************************************
2232 * open_loopback - open the device we use for getting packets
2233 * in demand mode. Under Linux, we use our existing fd
2234 * to the ppp driver.
2237 open_ppp_loopback(void)
2240 struct termios tios;
2242 if (openpty (&master_fd, &slave_fd, loop_name, NULL, NULL) < 0)
2244 syslog(LOG_ERR, "No free pty for loopback");
2247 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2249 set_ppp_fd(slave_fd);
2251 if (tcgetattr(ppp_fd, &tios) == 0)
2253 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2254 tios.c_cflag |= CS8 | CREAD;
2255 tios.c_iflag = IGNPAR | CLOCAL;
2258 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2260 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2264 flags = fcntl(master_fd, F_GETFL);
2266 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2268 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2271 flags = fcntl(ppp_fd, F_GETFL);
2273 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2275 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2278 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2280 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2284 * Find out which interface we were given.
2286 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2288 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2292 * Enable debug in the driver if requested.
2294 set_kdebugflag (kdebugflag);
2297 /********************************************************************
2299 * restore_loop - reattach the ppp unit to the loopback.
2301 * The problem with the Linux variant is that the POSIX tty drivers will
2302 * sieze the line when it is disconnected. In addition, when the device
2303 * goes down all of the routes are deleted. This means that the tty needs
2304 * to be re-opened, reconfigured, and the device reconfigured and the routes
2315 * Take down the existing interface
2318 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2320 * Find the existing flags. This works even if the tty has stolen the
2323 fdflags = fcntl(ppp_fd, F_GETFL);
2326 syslog (LOG_ERR, "retrieve file flags failed: %m(%d)", errno);
2327 fdflags = O_NONBLOCK | O_RDWR;
2330 * Re-open the file so the we can re-establish the previous discipline
2332 sprintf (fname, "/proc/self/fd/%d", ppp_fd);
2333 x = open (fname, O_RDWR | O_NONBLOCK, 0);
2336 syslog (LOG_ERR, "reopen of tty file failed: %m(%d)", errno);
2339 * Transfer the newly opened file (to the same tty) back to the tty
2346 fcntl (ppp_fd, F_SETFL, fdflags);
2347 set_up_tty(ppp_fd, 0);
2350 * Switch to the tty slave and put that into the PPP discipline.
2352 set_ppp_fd(slave_fd);
2354 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2356 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2360 * Fetch the current unit identifier.
2362 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2364 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2368 * Restore the parameters for the PPP link.
2370 ppp_send_config(0, PPP_MRU, (u_int32_t) 0, 0, 0);
2371 ppp_recv_config(0, PPP_MRU, (u_int32_t) 0, 0, 0);
2373 * Reconfigure the IP addresses for the demand dial system.
2375 (void) (ipcp_protent.demand_conf) (0);
2378 /********************************************************************
2380 * sifnpmode - Set the mode for handling packets for a given NP.
2384 sifnpmode(u, proto, mode)
2391 npi.protocol = proto;
2393 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2395 if (! ok_error (errno))
2397 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2398 proto, mode, errno);
2406 #include <linux/ipx.h>
2408 /********************************************************************
2410 * sipxfaddr - Config the interface IPX networknumber
2413 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2417 struct sockaddr_ipx ipx_addr;
2419 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2422 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2425 if (! ok_error (errno))
2427 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2433 memset (&ifr, '\0', sizeof (ifr));
2434 strcpy (ifr.ifr_name, ifname);
2436 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2437 sipx->sipx_family = AF_IPX;
2438 sipx->sipx_port = 0;
2439 sipx->sipx_network = htonl (network);
2440 sipx->sipx_type = IPX_FRAME_ETHERII;
2441 sipx->sipx_action = IPX_CRTITF;
2443 * Set the IPX device
2445 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2448 if (errno != EEXIST)
2450 if (! ok_error (errno))
2453 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2458 syslog (LOG_WARNING,
2459 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2468 /********************************************************************
2470 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2471 * are removed and the device is no longer able to pass IPX
2475 int cipxfaddr (int unit)
2479 struct sockaddr_ipx ipx_addr;
2481 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2484 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2487 if (! ok_error (errno))
2489 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2495 memset (&ifr, '\0', sizeof (ifr));
2496 strcpy (ifr.ifr_name, ifname);
2498 sipx->sipx_type = IPX_FRAME_ETHERII;
2499 sipx->sipx_action = IPX_DLTITF;
2500 sipx->sipx_family = AF_IPX;
2502 * Set the IPX device
2504 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2506 if (! ok_error (errno))
2509 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2519 /********************************************************************
2521 * sys_check_options - check the options that the user specified
2525 sys_check_options(void)
2528 struct stat stat_buf;
2530 * Disable the IPX protocol if the support is not present in the kernel.
2531 * If we disable it then ensure that IP support is enabled.
2533 while (ipxcp_protent.enabled_flag)
2535 if (path_to_procfs())
2537 strcat (route_buffer, "/net/ipx_interface");
2538 if (lstat (route_buffer, &stat_buf) >= 0)
2543 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2544 ipxcp_protent.enabled_flag = 0;
2545 ipcp_protent.enabled_flag = 1;