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.
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <sys/errno.h>
43 #include <net/ppp_defs.h>
44 #include <net/if_arp.h>
45 #include <net/if_ppp.h>
46 #include <net/route.h>
47 #include <linux/if_ether.h>
48 #include <netinet/in.h>
54 static int initdisc = -1; /* Initial TTY discipline */
55 static int initfdflags = -1; /* Initial file descriptor flags for fd */
56 static int prev_kdebugflag = 0;
57 static int has_default_route = 0;
58 static int has_proxy_arp = 0;
59 static int driver_version = 0;
60 static int driver_modification = 0;
61 static int driver_patch = 0;
62 static int restore_term = 0; /* 1 => we've munged the terminal */
63 static struct termios inittermios; /* Initial TTY termios */
65 static int sockfd; /* socket for doing interface ioctls */
67 static int if_is_up; /* Interface has been marked up */
68 static u_int32_t default_route_gateway; /* Gateway for default route added */
69 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
71 static char *lock_file;
75 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
76 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
77 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
79 /* Prototypes for procedures local to this file. */
80 static int get_flags (void);
81 static void set_flags (int flags);
82 static int translate_speed (int bps);
83 static int baud_rate_of (int speed);
84 static char *path_to_route (void);
85 static void close_route_table (void);
86 static int open_route_table (void);
87 static int read_route_table (struct rtentry *rt);
88 static int defaultroute_exists (void);
89 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr);
90 static void decode_version (char *buf, int *version, int *mod, int *patch);
94 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
98 #define SET_SA_FAMILY(addr, family) \
99 memset ((char *) &(addr), '\0', sizeof(addr)); \
100 addr.sa_family = (family);
103 * Determine if the PPP connection should still be present.
107 #define still_ppp() (hungup == 0)
110 * Functions to read and set the flags value in the device driver
113 static int get_flags (void)
117 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
119 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
123 MAINDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
127 static void set_flags (int flags)
129 MAINDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
131 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
133 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m", flags);
139 * sys_init - System-dependent initialization.
144 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
145 setlogmask(LOG_UPTO(LOG_INFO));
148 setlogmask(LOG_UPTO(LOG_DEBUG));
151 /* Get an internet socket for doing socket ioctls. */
152 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
155 syslog(LOG_ERR, "Couldn't create IP socket: %m");
161 * sys_cleanup - restore any system state we modified before exiting:
162 * mark the interface down, delete default route and/or proxy arp entry.
163 * This should call die() because it's called from die().
171 /* XXX maybe we need to delete the route through the interface */
172 if (has_default_route)
173 cifdefaultroute(0, default_route_gateway);
175 cifproxyarp(0, proxy_arp_addr);
179 * note_debug_level - note a change in the debug level.
182 void note_debug_level (void)
186 MAINDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
187 setlogmask(LOG_UPTO(LOG_DEBUG));
191 setlogmask(LOG_UPTO(LOG_WARNING));
196 * set_kdebugflag - Define the debugging level for the kernel
199 int set_kdebugflag (int requested_level)
201 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0)
203 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
207 if (prev_kdebugflag != requested_level)
209 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0)
211 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
214 MAINDEBUG ((LOG_INFO, "set kernel debugging level to %d", requested_level));
220 * establish_ppp - Turn the serial port into a ppp interface.
223 void establish_ppp (void)
228 if (ioctl(fd, TIOCEXCL, 0) < 0)
230 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
233 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
235 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
239 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
241 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
245 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
247 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
251 set_kdebugflag (kdebugflag);
253 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
254 SC_RCV_EVNP | SC_RCV_ODDP));
256 MAINDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
257 driver_version, driver_modification, driver_patch));
260 * Set device for non-blocking reads.
262 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
263 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
265 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
270 * disestablish_ppp - Restore the serial port to normal operation.
271 * This shouldn't call die() because it's called from die().
274 void disestablish_ppp(void)
279 /* Reset non-blocking mode on the file descriptor. */
280 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
281 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
285 * If this is no longer PPP mode then there is nothing that can be done
286 * about restoring the previous mode.
294 * Check whether the link seems not to be 8-bit clean.
298 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
301 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
303 case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP:
304 s = "nothing was received";
308 case SC_RCV_B7_0 | SC_RCV_EVNP:
309 case SC_RCV_B7_0 | SC_RCV_ODDP:
310 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
311 s = "all had bit 7 set to 1";
315 case SC_RCV_B7_1 | SC_RCV_EVNP:
316 case SC_RCV_B7_1 | SC_RCV_ODDP:
317 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
318 s = "all had bit 7 set to 0";
322 s = "all had odd parity";
326 s = "all had even parity";
332 syslog(LOG_WARNING, "Receive serial link is not 8-bit clean:");
333 syslog(LOG_WARNING, "Problem: %s", s);
337 set_kdebugflag (prev_kdebugflag);
339 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
341 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
344 if (ioctl(fd, TIOCNXCL, 0) < 0)
346 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
354 * List of valid speeds.
358 int speed_int, speed_val;
430 * Translate from bits/second to a speed_t.
433 static int translate_speed (int bps)
435 struct speed *speedp;
439 for (speedp = speeds; speedp->speed_int; speedp++)
441 if (bps == speedp->speed_int)
443 return speedp->speed_val;
446 syslog(LOG_WARNING, "speed %d not supported", bps);
452 * Translate from a speed_t to bits/second.
455 static int baud_rate_of (int speed)
457 struct speed *speedp;
461 for (speedp = speeds; speedp->speed_int; speedp++)
463 if (speed == speedp->speed_val)
465 return speedp->speed_int;
473 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
474 * at the requested speed, etc. If `local' is true, set CLOCAL
475 * regardless of whether the modem option was specified.
478 void set_up_tty (int fd, int local)
483 if (tcgetattr(fd, &tios) < 0)
485 syslog(LOG_ERR, "tcgetattr: %m");
494 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
495 tios.c_cflag |= CS8 | CREAD | HUPCL;
497 tios.c_iflag = IGNBRK | IGNPAR;
501 tios.c_cc[VTIME] = 0;
505 tios.c_cflag ^= (CLOCAL | HUPCL);
511 tios.c_cflag |= CRTSCTS;
515 tios.c_iflag |= IXON | IXOFF;
516 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
517 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
521 tios.c_cflag &= ~CRTSCTS;
528 speed = translate_speed(inspeed);
531 cfsetospeed (&tios, speed);
532 cfsetispeed (&tios, speed);
535 * We can't proceed if the serial port speed is B0,
536 * since that implies that the serial port is disabled.
540 speed = cfgetospeed(&tios);
543 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
549 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
551 syslog(LOG_ERR, "tcsetattr: %m");
555 baud_rate = baud_rate_of(speed);
560 * setdtr - control the DTR line on the serial port.
561 * This is called from die(), so it shouldn't call die().
564 void setdtr (int fd, int on)
566 int modembits = TIOCM_DTR;
568 ioctl(fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
572 * restore_tty - restore the terminal to the saved settings.
575 void restore_tty (void)
581 * Turn off echoing, because otherwise we can get into
582 * a loop with the tty and the modem echoing to each other.
583 * We presume we are the sole user of this tty device, so
584 * when we close it, it will revert to its defaults anyway.
588 inittermios.c_lflag &= ~(ECHO | ECHONL);
591 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
595 syslog(LOG_WARNING, "tcsetattr: %m");
602 * output - Output PPP packet.
605 void output (int unit, unsigned char *p, int len)
609 log_packet(p, len, "sent ");
612 if (write(fd, p, len) < 0)
614 if (errno == EWOULDBLOCK || errno == ENOBUFS
615 || errno == ENXIO || errno == EIO)
617 syslog(LOG_WARNING, "write: warning: %m");
621 syslog(LOG_ERR, "write: %m");
628 * wait_input - wait until there is data available on fd,
629 * for the length of time specified by *timo (indefinite
633 void wait_input (struct timeval *timo)
641 n = select(fd+1, &ready, NULL, &ready, timo);
642 if (n < 0 && errno != EINTR)
644 syslog(LOG_ERR, "select: %m");
650 * read_packet - get a PPP packet from the serial device.
653 int read_packet (unsigned char *buf)
657 len = read(fd, buf, PPP_MTU + PPP_HDRLEN);
660 if (errno == EWOULDBLOCK)
664 syslog(LOG_ERR, "read(fd): %m");
671 * ppp_send_config - configure the transmit characteristics of
675 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
680 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
682 * If we were called because the link has gone down then there is nothing
683 * which may be done. Just return without incident.
690 * Set the MTU and other parameters for the ppp device
692 memset (&ifr, '\0', sizeof (ifr));
693 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
696 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
698 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
702 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
703 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
705 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
710 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
711 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
716 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
719 void ppp_set_xaccm (int unit, ext_accm accm)
721 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
722 accm[0], accm[1], accm[2], accm[3]));
724 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
726 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
731 * ppp_recv_config - configure the receive-side characteristics of
735 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
739 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
741 * If we were called because the link has gone down then there is nothing
742 * which may be done. Just return without incident.
749 * Set the receiver parameters
751 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
753 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
756 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
757 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
759 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
764 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
769 * ccp_test - ask kernel whether a given compression method
770 * is acceptable for use.
773 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
775 struct ppp_option_data data;
777 memset (&data, '\0', sizeof (data));
779 data.length = opt_len;
780 data.transmit = for_transmit;
782 if (ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
785 return (errno == ENOBUFS)? 0: -1;
789 * ccp_flags_set - inform kernel about the current state of CCP.
792 void ccp_flags_set (int unit, int isopen, int isup)
797 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
798 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
804 * ccp_fatal_error - returns 1 if decompression was disabled as a
805 * result of an error detected after decompression of a packet,
806 * 0 otherwise. This is necessary because of patent nonsense.
809 int ccp_fatal_error (int unit)
813 return x & SC_DC_FERROR;
817 * sifvjcomp - config tcp header compression
820 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
822 u_int x = get_flags();
826 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
828 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
833 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
834 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
841 * sifup - Config the interface up and enable IP packets to pass.
848 memset (&ifr, '\0', sizeof (ifr));
849 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
850 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
852 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
856 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
857 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
859 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
867 * sifdown - Config the interface down and disable IP.
874 memset (&ifr, '\0', sizeof (ifr));
875 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
876 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
878 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
882 ifr.ifr_flags &= ~IFF_UP;
883 ifr.ifr_flags |= IFF_POINTOPOINT;
884 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
886 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
894 * sifaddr - Config the interface IP addresses and netmask.
897 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
902 memset (&ifr, '\0', sizeof (ifr));
903 memset (&rt, '\0', sizeof (rt));
905 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
906 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
907 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
909 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
913 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
914 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
918 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
922 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
927 * Set the gateway address
929 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
930 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
932 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
940 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
941 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
943 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
948 * Add the device route
950 SET_SA_FAMILY (rt.rt_dst, AF_INET);
951 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
952 rt.rt_dev = ifname; /* MJC */
954 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
955 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
956 rt.rt_flags = RTF_UP | RTF_HOST;
958 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
960 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
967 * cifaddr - Clear the interface IP addresses, and delete routes
968 * through the interface if possible.
971 int cifaddr (int unit, int our_adr, int his_adr)
975 * Delete the route through the device
977 memset (&rt, '\0', sizeof (rt));
979 SET_SA_FAMILY (rt.rt_dst, AF_INET);
980 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
981 rt.rt_dev = ifname; /* MJC */
983 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
984 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
985 rt.rt_flags = RTF_UP | RTF_HOST;
987 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
989 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
996 * path_to_route - determine the path to the proc file system data
999 FILE *route_fd = (FILE *) 0;
1000 static char route_buffer [100];
1002 static char *path_to_route (void);
1003 static int open_route_table (void);
1004 static void close_route_table (void);
1005 static int read_route_table (struct rtentry *rt);
1006 static int defaultroute_exists (void);
1009 * path_to_route - find the path to the route tables in the proc file system
1012 static char *path_to_route (void)
1014 struct mntent *mntent;
1017 fp = fopen (MOUNTED, "r");
1020 mntent = getmntent (fp);
1021 while (mntent != (struct mntent *) 0)
1023 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1025 if (strcmp (mntent->mnt_type, "proc") == 0)
1027 strncpy (route_buffer, mntent->mnt_dir,
1028 sizeof (route_buffer)-10);
1029 route_buffer [sizeof (route_buffer)-10] = '\0';
1030 strcat (route_buffer, "/net/route");
1033 return (route_buffer);
1036 mntent = getmntent (fp);
1041 syslog (LOG_ERR, "proc file system not mounted");
1046 * close_route_table - close the interface to the route table
1049 static void close_route_table (void)
1051 if (route_fd != (FILE *) 0)
1054 route_fd = (FILE *) 0;
1059 * open_route_table - open the interface to the route table
1062 static int open_route_table (void)
1066 close_route_table();
1068 path = path_to_route();
1074 route_fd = fopen (path, "r");
1075 if (route_fd == (FILE *) 0)
1077 syslog (LOG_ERR, "can not open %s: %m", path);
1081 /* read and discard the header line. */
1082 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1084 close_route_table();
1091 * read_route_table - read the next entry from the route table
1094 static int read_route_table (struct rtentry *rt)
1096 static char delims[] = " \t\n";
1097 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1099 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1104 memset (rt, '\0', sizeof (struct rtentry));
1106 dev_ptr = strtok (route_buffer, delims); /* interface name */
1107 dst_ptr = strtok (NULL, delims); /* destination address */
1108 gw_ptr = strtok (NULL, delims); /* gateway */
1109 flag_ptr = strtok (NULL, delims); /* flags */
1111 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1112 strtoul (dst_ptr, NULL, 16);
1114 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1115 strtoul (gw_ptr, NULL, 16);
1117 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1118 rt->rt_dev = dev_ptr;
1124 * defaultroute_exists - determine if there is a default route
1127 static int defaultroute_exists (void)
1132 if (!open_route_table())
1137 while (read_route_table(&rt) != 0)
1139 if ((rt.rt_flags & RTF_UP) == 0)
1144 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L)
1147 "ppp not replacing existing default route to %s[%s]",
1149 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
1156 close_route_table();
1161 * sifdefaultroute - assign a default route through the address given.
1164 int sifdefaultroute (int unit, int gateway)
1168 if (has_default_route == 0)
1170 if (defaultroute_exists())
1175 memset (&rt, '\0', sizeof (rt));
1176 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1177 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1178 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1180 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1181 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
1183 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1187 has_default_route = 1;
1188 default_route_gateway = g;
1193 * cifdefaultroute - delete a default route through the address given.
1196 int cifdefaultroute (int unit, int gateway)
1200 if (has_default_route)
1202 memset (&rt, '\0', sizeof (rt));
1203 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1204 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1205 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1207 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1208 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1210 if (errno != ENOENT || still_ppp())
1212 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1217 has_default_route = 0;
1222 * sifproxyarp - Make a proxy ARP entry for the peer.
1225 int sifproxyarp (int unit, u_int32_t his_adr)
1227 struct arpreq arpreq;
1229 if (has_proxy_arp == 0)
1231 memset (&arpreq, '\0', sizeof(arpreq));
1233 * Get the hardware address of an interface on the same subnet
1234 * as our local address.
1236 if (!get_ether_addr(his_adr, &arpreq.arp_ha))
1238 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1242 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1243 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1244 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1246 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0)
1248 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1253 proxy_arp_addr = hisaddr;
1259 * cifproxyarp - Delete the proxy ARP entry for the peer.
1262 int cifproxyarp (int unit, u_int32_t his_adr)
1264 struct arpreq arpreq;
1266 if (has_proxy_arp == 1)
1268 memset (&arpreq, '\0', sizeof(arpreq));
1269 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1271 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1272 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0)
1274 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1283 * get_ether_addr - get the hardware address of an interface on the
1284 * the same subnet as ipaddr.
1287 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1289 struct ifreq *ifr, *ifend, *ifp;
1291 u_int32_t ina, mask;
1292 struct sockaddr_dl *dla;
1295 struct ifreq ifs[MAX_IFS];
1297 ifc.ifc_len = sizeof(ifs);
1299 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1301 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1305 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1306 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1308 * Scan through looking for an interface with an Internet
1309 * address on the same subnet as `ipaddr'.
1311 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1312 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1314 if (ifr->ifr_addr.sa_family == AF_INET)
1316 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1317 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1318 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1321 * Check that the interface is up, and not point-to-point
1324 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1329 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1334 * Get its netmask and check that it's on the right subnet.
1336 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1341 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1342 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1343 ip_ntoa(ina), ntohl(mask)));
1345 if (((ipaddr ^ ina) & mask) != 0)
1358 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1360 * Now get the hardware address.
1362 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1363 if (ioctl (sockfd, SIOCGIFHWADDR, &ifreq) < 0)
1365 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1371 sizeof (struct sockaddr));
1373 MAINDEBUG ((LOG_DEBUG,
1374 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1375 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1376 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1377 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1378 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1379 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1380 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1381 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1382 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1387 * Return user specified netmask, modified by any mask we might determine
1388 * for address `addr' (in network byte order).
1389 * Here we scan through the system's list of interfaces, looking for
1390 * any non-point-to-point interfaces which might appear to be on the same
1391 * network as `addr'. If we find any, we OR in their netmask to the
1392 * user-specified netmask.
1395 u_int32_t GetMask (u_int32_t addr)
1397 u_int32_t mask, nmask, ina;
1398 struct ifreq *ifr, *ifend, ifreq;
1400 struct ifreq ifs[MAX_IFS];
1404 if (IN_CLASSA(addr)) /* determine network mask for address class */
1406 nmask = IN_CLASSA_NET;
1410 if (IN_CLASSB(addr))
1412 nmask = IN_CLASSB_NET;
1416 nmask = IN_CLASSC_NET;
1420 /* class D nets are disallowed by bad_ip_adrs */
1421 mask = netmask | htonl(nmask);
1423 * Scan through the system's network interfaces.
1425 ifc.ifc_len = sizeof(ifs);
1427 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1429 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1433 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1434 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1437 * Check the interface's internet address.
1439 if (ifr->ifr_addr.sa_family != AF_INET)
1443 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1444 if (((ntohl(ina) ^ addr) & nmask) != 0)
1449 * Check that the interface is up, and not point-to-point nor loopback.
1451 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1452 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1457 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1462 * Get its netmask and OR it into our mask.
1464 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1468 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1475 * Internal routine to decode the version.modification.patch level
1478 static void decode_version (char *buf, int *version,
1479 int *modification, int *patch)
1481 *version = (int) strtoul (buf, &buf, 10);
1488 *modification = (int) strtoul (buf, &buf, 10);
1492 *patch = (int) strtoul (buf, &buf, 10);
1505 * Procedure to determine if the PPP line dicipline is registered.
1509 ppp_registered(void)
1512 int ppp_disc = N_PPP;
1516 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1519 syslog(LOG_ERR, "Failed to open %s: %m", devnam);
1523 initfdflags = fcntl(local_fd, F_GETFL);
1524 if (initfdflags == -1)
1526 syslog(LOG_ERR, "Couldn't get device fd flags: %m");
1531 initfdflags &= ~O_NONBLOCK;
1532 fcntl(local_fd, F_SETFL, initfdflags);
1534 * Read the initial line dicipline and try to put the device into the
1537 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1539 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
1544 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1546 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1551 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1553 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1563 * ppp_available - check whether the system has any ppp interfaces
1564 * (in fact we check whether we can do an ioctl on ppp0).
1567 int ppp_available(void)
1571 char abBuffer [1024];
1573 int my_version, my_modification, my_patch;
1575 * Open a socket for doing the ioctl operations.
1577 s = socket(AF_INET, SOCK_DGRAM, 0);
1583 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1584 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1586 * If the device did not exist then attempt to create one by putting the
1587 * current tty into the PPP discipline. If this works then obtain the
1588 * flags for the device again.
1592 if (ppp_registered())
1594 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1595 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1599 * Ensure that the hardware address is for PPP and not something else
1603 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1606 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1616 * This is the PPP device. Validate the version of the driver at this
1617 * point to ensure that this program will work with the driver.
1619 ifr.ifr_data = abBuffer;
1620 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1625 decode_version (abBuffer,
1627 &driver_modification,
1634 driver_modification =
1638 * Validate the version of the driver against the version that we used.
1640 decode_version (PPP_VERSION,
1645 /* The version numbers must match */
1646 if (driver_version != my_version)
1651 /* The modification levels must be legal */
1652 if (driver_modification < my_modification)
1659 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1660 driver_version, driver_modification, driver_patch);
1670 * Update the wtmp file with the appropriate user name and tty device.
1673 int logwtmp (char *line, char *name, char *host)
1678 * Control the 'mesg' function based upon the state of the logon
1681 mode = (*name != '\0') ? 0600 : 0622;
1682 if (chmod (devnam, mode) < 0)
1684 syslog (LOG_ERR, "chmod(\"%s\", 0%o): %m", devnam, mode);
1687 * Update the signon database for users.
1689 memset (&ut, 0, sizeof (ut));
1690 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1691 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1692 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1693 (void)time(&ut.ut_time);
1695 pututline (&ut); /* Write the line to the proper place */
1696 endutent(); /* Indicate operation is complete */
1700 * Code for locking/unlocking the serial device.
1701 * This code is derived from chat.c.
1704 #define LOCK_PREFIX "/var/lock/LCK.."
1707 * lock - create a lock file for the named device
1710 int lock (char *dev)
1712 char hdb_lock_buffer[12];
1716 p = strrchr(dev, '/');
1722 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1723 if (lock_file == NULL)
1725 novm("lock file name");
1728 strcpy (lock_file, LOCK_PREFIX);
1729 strcat (lock_file, dev);
1731 * Attempt to create the lock file at this point.
1735 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1738 sprintf(hdb_lock_buffer, "%010d\n", getpid());
1739 write(fd, hdb_lock_buffer, 11);
1744 * If the file exists then check to see if the pid is stale
1746 if (errno == EEXIST)
1748 fd = open(lock_file, O_RDONLY, 0);
1751 if (errno == ENOENT) /* This is just a timing problem. */
1758 /* Read the lock file to find out who has the device locked */
1759 n = read (fd, hdb_lock_buffer, 11);
1763 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1767 /* See the process still exists. */
1770 hdb_lock_buffer[n] = '\0';
1771 sscanf (hdb_lock_buffer, " %d", &pid);
1772 if (kill(pid, 0) == -1 && errno == ESRCH)
1778 /* If the process does not exist then try to remove the lock */
1779 if (n == 0 && unlink (lock_file) == 0)
1781 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1786 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
1790 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1800 * unlock - remove our lockfile