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>
44 #include <net/ppp_defs.h>
45 #include <net/if_arp.h>
46 #include <net/if_ppp.h>
47 #include <net/route.h>
48 #include <linux/if_ether.h>
49 #include <netinet/in.h>
55 static int initdisc = -1; /* Initial TTY discipline */
56 static int initfdflags = -1; /* Initial file descriptor flags for fd */
57 static int prev_kdebugflag = 0;
58 static int has_default_route = 0;
59 static int has_proxy_arp = 0;
60 static int driver_version = 0;
61 static int driver_modification = 0;
62 static int driver_patch = 0;
63 static int restore_term = 0; /* 1 => we've munged the terminal */
64 static struct termios inittermios; /* Initial TTY termios */
66 static int sockfd; /* socket for doing interface ioctls */
68 static int if_is_up; /* Interface has been marked up */
69 static u_int32_t ipaddrs[2]; /* Local and remote addrs we've set */
70 static u_int32_t default_route_gateway; /* Gateway for default route added */
71 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
73 static char *lock_file;
77 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
78 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
79 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
81 /* Prototypes for procedures local to this file. */
82 static int get_flags (void);
83 static void set_flags (int flags);
84 static int translate_speed (int bps);
85 static int baud_rate_of (int speed);
86 static char *path_to_route (void);
87 static void close_route_table (void);
88 static int open_route_table (void);
89 static int read_route_table (struct rtentry *rt);
90 static int defaultroute_exists (void);
91 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr);
92 static void decode_version (char *buf, int *version, int *mod, int *patch);
96 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
100 #define SET_SA_FAMILY(addr, family) \
101 memset ((char *) &(addr), '\0', sizeof(addr)); \
102 addr.sa_family = (family);
105 * Determine if the PPP connection should still be present.
109 #define still_ppp() (hungup == 0)
112 * Functions to read and set the flags value in the device driver
115 static int get_flags (void)
119 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
121 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
125 MAINDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
129 static void set_flags (int flags)
131 MAINDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
133 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
135 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m", flags);
141 * sys_init - System-dependent initialization.
146 /* Get an internet socket for doing socket ioctls. */
147 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
150 syslog(LOG_ERR, "Couldn't create IP socket: %m");
156 * sys_cleanup - restore any system state we modified before exiting:
157 * mark the interface down, delete default route and/or proxy arp entry.
158 * This should call die() because it's called from die().
167 cifaddr(0, ipaddrs[0], ipaddrs[1]);
168 /* XXX maybe we need to delete the route through the interface */
169 if (has_default_route)
170 cifdefaultroute(0, default_route_gateway);
172 cifproxyarp(0, proxy_arp_addr);
176 * set_kdebugflag - Define the debugging level for the kernel
179 int set_kdebugflag (int requested_level)
181 if (ioctl(ttyfd, PPPIOCGDEBUG, &prev_kdebugflag) < 0)
183 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
187 if (prev_kdebugflag != requested_level)
189 if (ioctl(ttyfd, PPPIOCSDEBUG, &requested_level) < 0)
191 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
194 MAINDEBUG ((LOG_INFO, "set kernel debugging level to %d",
201 * establish_ppp - Turn the serial port into a ppp interface.
204 void establish_ppp (int fd)
209 if (ioctl(fd, TIOCEXCL, 0) < 0)
211 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
214 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
216 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
220 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
222 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
226 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
228 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
232 set_kdebugflag (kdebugflag);
234 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
235 SC_RCV_EVNP | SC_RCV_ODDP));
237 MAINDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
238 driver_version, driver_modification, driver_patch));
241 * Set device for non-blocking reads.
243 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
244 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
246 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
251 * disestablish_ppp - Restore the serial port to normal operation.
252 * This shouldn't call die() because it's called from die().
255 void disestablish_ppp(int fd)
260 /* Reset non-blocking mode on the file descriptor. */
261 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
262 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
265 if (still_ppp() && initdisc >= 0)
267 set_kdebugflag (prev_kdebugflag);
269 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
271 syslog(LOG_WARNING, "ioctl(TIOCSETD): %m");
274 if (ioctl(fd, TIOCNXCL, 0) < 0)
276 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
283 * clean_check - Fetch the flags for the device and generate
284 * appropriate error messages.
286 void clean_check(void)
288 if (still_ppp() && initdisc >= 0)
290 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
293 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
295 case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP:
296 s = "nothing was received";
300 case SC_RCV_B7_0 | SC_RCV_EVNP:
301 case SC_RCV_B7_0 | SC_RCV_ODDP:
302 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
303 s = "all had bit 7 set to 1";
307 case SC_RCV_B7_1 | SC_RCV_EVNP:
308 case SC_RCV_B7_1 | SC_RCV_ODDP:
309 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
310 s = "all had bit 7 set to 0";
314 s = "all had odd parity";
318 s = "all had even parity";
324 syslog(LOG_WARNING, "Receive serial link is not"
326 syslog(LOG_WARNING, "Problem: %s", s);
334 * List of valid speeds.
338 int speed_int, speed_val;
410 * Translate from bits/second to a speed_t.
413 static int translate_speed (int bps)
415 struct speed *speedp;
419 for (speedp = speeds; speedp->speed_int; speedp++)
421 if (bps == speedp->speed_int)
423 return speedp->speed_val;
426 syslog(LOG_WARNING, "speed %d not supported", bps);
432 * Translate from a speed_t to bits/second.
435 static int baud_rate_of (int speed)
437 struct speed *speedp;
441 for (speedp = speeds; speedp->speed_int; speedp++)
443 if (speed == speedp->speed_val)
445 return speedp->speed_int;
453 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
454 * at the requested speed, etc. If `local' is true, set CLOCAL
455 * regardless of whether the modem option was specified.
458 void set_up_tty (int fd, int local)
463 if (tcgetattr(fd, &tios) < 0)
465 syslog(LOG_ERR, "tcgetattr: %m");
474 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
475 tios.c_cflag |= CS8 | CREAD | HUPCL;
477 tios.c_iflag = IGNBRK | IGNPAR;
481 tios.c_cc[VTIME] = 0;
485 tios.c_cflag ^= (CLOCAL | HUPCL);
491 tios.c_cflag |= CRTSCTS;
495 tios.c_iflag |= IXON | IXOFF;
496 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
497 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
501 tios.c_cflag &= ~CRTSCTS;
508 speed = translate_speed(inspeed);
511 cfsetospeed (&tios, speed);
512 cfsetispeed (&tios, speed);
515 * We can't proceed if the serial port speed is B0,
516 * since that implies that the serial port is disabled.
520 speed = cfgetospeed(&tios);
523 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
529 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
531 syslog(LOG_ERR, "tcsetattr: %m");
535 baud_rate = baud_rate_of(speed);
540 * setdtr - control the DTR line on the serial port.
541 * This is called from die(), so it shouldn't call die().
544 void setdtr (int fd, int on)
546 int modembits = TIOCM_DTR;
548 ioctl(fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
552 * restore_tty - restore the terminal to the saved settings.
555 void restore_tty (int fd)
561 * Turn off echoing, because otherwise we can get into
562 * a loop with the tty and the modem echoing to each other.
563 * We presume we are the sole user of this tty device, so
564 * when we close it, it will revert to its defaults anyway.
568 inittermios.c_lflag &= ~(ECHO | ECHONL);
571 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
575 syslog(LOG_WARNING, "tcsetattr: %m");
582 * output - Output PPP packet.
585 void output (int unit, unsigned char *p, int len)
589 log_packet(p, len, "sent ");
592 if (write(ttyfd, p, len) < 0)
594 if (errno == EWOULDBLOCK || errno == ENOBUFS
595 || errno == ENXIO || errno == EIO)
597 syslog(LOG_WARNING, "write: warning: %m");
601 syslog(LOG_ERR, "write: %m");
608 * wait_input - wait until there is data available on ttyfd,
609 * for the length of time specified by *timo (indefinite
613 void wait_input (struct timeval *timo)
619 FD_SET(ttyfd, &ready);
621 n = select(ttyfd+1, &ready, NULL, &ready, timo);
622 if (n < 0 && errno != EINTR)
624 syslog(LOG_ERR, "select: %m");
630 * read_packet - get a PPP packet from the serial device.
633 int read_packet (unsigned char *buf)
637 len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN);
640 if (errno == EWOULDBLOCK)
644 syslog(LOG_ERR, "read: %m");
651 * ppp_send_config - configure the transmit characteristics of
655 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
660 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
662 * Ensure that the link is still up.
667 * Set the MTU and other parameters for the ppp device
669 memset (&ifr, '\0', sizeof (ifr));
670 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
673 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
675 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
679 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
680 if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
682 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
687 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
688 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
694 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
697 void ppp_set_xaccm (int unit, ext_accm accm)
699 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
700 accm[0], accm[1], accm[2], accm[3]));
702 if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
704 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
709 * ppp_recv_config - configure the receive-side characteristics of
713 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
717 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
719 * If we were called because the link has gone down then there is nothing
720 * which may be done. Just return without incident.
727 * Set the receiver parameters
729 if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0)
731 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
734 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
735 if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
737 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
742 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
747 * ccp_test - ask kernel whether a given compression method
748 * is acceptable for use.
751 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
753 struct ppp_option_data data;
755 memset (&data, '\0', sizeof (data));
757 data.length = opt_len;
758 data.transmit = for_transmit;
760 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
765 return (errno == ENOBUFS)? 0: -1;
769 * ccp_flags_set - inform kernel about the current state of CCP.
772 void ccp_flags_set (int unit, int isopen, int isup)
777 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
778 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
784 * ccp_fatal_error - returns 1 if decompression was disabled as a
785 * result of an error detected after decompression of a packet,
786 * 0 otherwise. This is necessary because of patent nonsense.
789 int ccp_fatal_error (int unit)
793 return x & SC_DC_FERROR;
797 * sifvjcomp - config tcp header compression
800 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
802 u_int x = get_flags();
806 if (ioctl (ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
808 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
813 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
814 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
821 * sifup - Config the interface up and enable IP packets to pass.
828 memset (&ifr, '\0', sizeof (ifr));
829 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
830 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
832 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
836 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
837 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
839 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
847 * sifdown - Config the interface down and disable IP.
854 memset (&ifr, '\0', sizeof (ifr));
855 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
856 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
858 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
862 ifr.ifr_flags &= ~IFF_UP;
863 ifr.ifr_flags |= IFF_POINTOPOINT;
864 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
866 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
874 * sifaddr - Config the interface IP addresses and netmask.
877 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
882 memset (&ifr, '\0', sizeof (ifr));
883 memset (&rt, '\0', sizeof (rt));
885 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
886 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
887 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
889 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
893 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
894 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
898 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
902 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
907 * Set the gateway address
909 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
910 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
912 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
920 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
921 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
923 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
928 * Add the device route
930 SET_SA_FAMILY (rt.rt_dst, AF_INET);
931 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
932 rt.rt_dev = ifname; /* MJC */
934 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
935 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
936 rt.rt_flags = RTF_UP | RTF_HOST;
938 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
940 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
944 ipaddrs[0] = our_adr;
945 ipaddrs[1] = his_adr;
950 * cifaddr - Clear the interface IP addresses, and delete routes
951 * through the interface if possible.
954 int cifaddr (int unit, int our_adr, int his_adr)
960 * Delete the route through the device
962 memset (&rt, '\0', sizeof (rt));
964 SET_SA_FAMILY (rt.rt_dst, AF_INET);
965 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
966 rt.rt_dev = ifname; /* MJC */
968 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
969 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
970 rt.rt_flags = RTF_UP | RTF_HOST;
972 if (ioctl(sockfd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
976 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
984 * path_to_route - determine the path to the proc file system data
987 FILE *route_fd = (FILE *) 0;
988 static char route_buffer [512];
990 static char *path_to_route (void);
991 static int open_route_table (void);
992 static void close_route_table (void);
993 static int read_route_table (struct rtentry *rt);
994 static int defaultroute_exists (void);
997 * path_to_route - find the path to the route tables in the proc file system
1000 static char *path_to_route (void)
1002 struct mntent *mntent;
1005 fp = fopen (MOUNTED, "r");
1008 mntent = getmntent (fp);
1009 while (mntent != (struct mntent *) 0)
1011 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1013 if (strcmp (mntent->mnt_type, "proc") == 0)
1015 strncpy (route_buffer, mntent->mnt_dir,
1016 sizeof (route_buffer)-10);
1017 route_buffer [sizeof (route_buffer)-10] = '\0';
1018 strcat (route_buffer, "/net/route");
1021 return (route_buffer);
1024 mntent = getmntent (fp);
1029 syslog (LOG_ERR, "proc file system not mounted");
1034 * close_route_table - close the interface to the route table
1037 static void close_route_table (void)
1039 if (route_fd != (FILE *) 0)
1042 route_fd = (FILE *) 0;
1047 * open_route_table - open the interface to the route table
1050 static int open_route_table (void)
1054 close_route_table();
1056 path = path_to_route();
1062 route_fd = fopen (path, "r");
1063 if (route_fd == (FILE *) 0)
1065 syslog (LOG_ERR, "can not open %s: %m", path);
1072 * read_route_table - read the next entry from the route table
1075 static int read_route_table (struct rtentry *rt)
1077 static char delims[] = " \t\n";
1078 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1080 memset (rt, '\0', sizeof (struct rtentry));
1084 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1090 dev_ptr = strtok (route_buffer, delims); /* interface name */
1091 dst_ptr = strtok (NULL, delims); /* destination address */
1092 gw_ptr = strtok (NULL, delims); /* gateway */
1093 flag_ptr = strtok (NULL, delims); /* flags */
1095 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1100 /* Discard that stupid header line which should never
1101 * have been there in the first place !! */
1102 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1108 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1109 strtoul (dst_ptr, NULL, 16);
1111 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1112 strtoul (gw_ptr, NULL, 16);
1114 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1115 rt->rt_dev = dev_ptr;
1121 * defaultroute_exists - determine if there is a default route
1124 static int defaultroute_exists (void)
1129 if (!open_route_table())
1134 while (read_route_table(&rt) != 0)
1136 if ((rt.rt_flags & RTF_UP) == 0)
1141 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L)
1144 "ppp not replacing existing default route to %s[%s]",
1146 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
1153 close_route_table();
1158 * sifdefaultroute - assign a default route through the address given.
1161 int sifdefaultroute (int unit, int gateway)
1165 if (has_default_route == 0)
1167 if (defaultroute_exists())
1172 memset (&rt, '\0', sizeof (rt));
1173 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1174 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1175 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1177 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1178 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
1180 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1184 has_default_route = 1;
1185 default_route_gateway = g;
1190 * cifdefaultroute - delete a default route through the address given.
1193 int cifdefaultroute (int unit, int gateway)
1197 if (has_default_route)
1199 memset (&rt, '\0', sizeof (rt));
1200 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1201 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1202 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1204 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1205 if (ioctl(sockfd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1209 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1214 has_default_route = 0;
1219 * sifproxyarp - Make a proxy ARP entry for the peer.
1222 int sifproxyarp (int unit, u_int32_t his_adr)
1224 struct arpreq arpreq;
1226 if (has_proxy_arp == 0)
1228 memset (&arpreq, '\0', sizeof(arpreq));
1230 * Get the hardware address of an interface on the same subnet
1231 * as our local address.
1233 if (!get_ether_addr(his_adr, &arpreq.arp_ha))
1235 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1239 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1240 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1241 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1243 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0)
1245 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1250 proxy_arp_addr = hisaddr;
1256 * cifproxyarp - Delete the proxy ARP entry for the peer.
1259 int cifproxyarp (int unit, u_int32_t his_adr)
1261 struct arpreq arpreq;
1263 if (has_proxy_arp == 1)
1265 memset (&arpreq, '\0', sizeof(arpreq));
1266 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1268 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1269 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0)
1271 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1280 * get_ether_addr - get the hardware address of an interface on the
1281 * the same subnet as ipaddr.
1284 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1286 struct ifreq *ifr, *ifend, *ifp;
1288 u_int32_t ina, mask;
1289 struct sockaddr_dl *dla;
1292 struct ifreq ifs[MAX_IFS];
1294 ifc.ifc_len = sizeof(ifs);
1296 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1298 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1302 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1303 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1305 * Scan through looking for an interface with an Internet
1306 * address on the same subnet as `ipaddr'.
1308 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1309 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1311 if (ifr->ifr_addr.sa_family == AF_INET)
1313 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1314 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1315 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1318 * Check that the interface is up, and not point-to-point
1321 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1326 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1331 * Get its netmask and check that it's on the right subnet.
1333 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1338 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1339 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1340 ip_ntoa(ina), ntohl(mask)));
1342 if (((ipaddr ^ ina) & mask) != 0)
1355 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1357 * Now get the hardware address.
1359 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1360 if (ioctl (sockfd, SIOCGIFHWADDR, &ifreq) < 0)
1362 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1368 sizeof (struct sockaddr));
1370 MAINDEBUG ((LOG_DEBUG,
1371 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1372 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1373 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1374 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1375 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1376 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1377 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1378 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1379 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1384 * Return user specified netmask, modified by any mask we might determine
1385 * for address `addr' (in network byte order).
1386 * Here we scan through the system's list of interfaces, looking for
1387 * any non-point-to-point interfaces which might appear to be on the same
1388 * network as `addr'. If we find any, we OR in their netmask to the
1389 * user-specified netmask.
1392 u_int32_t GetMask (u_int32_t addr)
1394 u_int32_t mask, nmask, ina;
1395 struct ifreq *ifr, *ifend, ifreq;
1397 struct ifreq ifs[MAX_IFS];
1401 if (IN_CLASSA(addr)) /* determine network mask for address class */
1403 nmask = IN_CLASSA_NET;
1407 if (IN_CLASSB(addr))
1409 nmask = IN_CLASSB_NET;
1413 nmask = IN_CLASSC_NET;
1417 /* class D nets are disallowed by bad_ip_adrs */
1418 mask = netmask | htonl(nmask);
1420 * Scan through the system's network interfaces.
1422 ifc.ifc_len = sizeof(ifs);
1424 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1426 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1430 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1431 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1434 * Check the interface's internet address.
1436 if (ifr->ifr_addr.sa_family != AF_INET)
1440 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1441 if (((ntohl(ina) ^ addr) & nmask) != 0)
1446 * Check that the interface is up, and not point-to-point nor loopback.
1448 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1449 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1454 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1459 * Get its netmask and OR it into our mask.
1461 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1465 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1472 * Internal routine to decode the version.modification.patch level
1475 static void decode_version (char *buf, int *version,
1476 int *modification, int *patch)
1478 *version = (int) strtoul (buf, &buf, 10);
1485 *modification = (int) strtoul (buf, &buf, 10);
1489 *patch = (int) strtoul (buf, &buf, 10);
1502 * Procedure to determine if the PPP line dicipline is registered.
1506 ppp_registered(void)
1509 int ppp_disc = N_PPP;
1513 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1516 syslog(LOG_ERR, "Failed to open %s: %m", devnam);
1520 initfdflags = fcntl(local_fd, F_GETFL);
1521 if (initfdflags == -1)
1523 syslog(LOG_ERR, "Couldn't get device fd flags: %m");
1528 initfdflags &= ~O_NONBLOCK;
1529 fcntl(local_fd, F_SETFL, initfdflags);
1531 * Read the initial line dicipline and try to put the device into the
1534 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1536 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
1541 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1543 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1548 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1550 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1560 * ppp_available - check whether the system has any ppp interfaces
1561 * (in fact we check whether we can do an ioctl on ppp0).
1564 int ppp_available(void)
1568 char abBuffer [1024];
1570 int my_version, my_modification, my_patch;
1572 * Open a socket for doing the ioctl operations.
1574 s = socket(AF_INET, SOCK_DGRAM, 0);
1580 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1581 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1583 * If the device did not exist then attempt to create one by putting the
1584 * current tty into the PPP discipline. If this works then obtain the
1585 * flags for the device again.
1589 if (ppp_registered())
1591 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1592 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1596 * Ensure that the hardware address is for PPP and not something else
1600 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1603 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1613 * This is the PPP device. Validate the version of the driver at this
1614 * point to ensure that this program will work with the driver.
1616 ifr.ifr_data = abBuffer;
1617 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1622 decode_version (abBuffer,
1624 &driver_modification,
1631 driver_modification =
1635 * Validate the version of the driver against the version that we used.
1637 decode_version (PPP_VERSION,
1642 /* The version numbers must match */
1643 if (driver_version != my_version)
1648 /* The modification levels must be legal */
1649 if (driver_modification < my_modification)
1656 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1657 driver_version, driver_modification, driver_patch);
1667 * Update the wtmp file with the appropriate user name and tty device.
1670 int logwtmp (char *line, char *name, char *host)
1675 * Control the 'mesg' function based upon the state of the logon
1678 mode = (*name != '\0') ? 0600 : 0622;
1679 if (chmod (devnam, mode) < 0)
1681 syslog (LOG_ERR, "chmod(\"%s\", 0%o): %m", devnam, mode);
1684 * Update the signon database for users.
1686 memset (&ut, 0, sizeof (ut));
1687 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1688 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1689 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1690 (void)time(&ut.ut_time);
1692 pututline (&ut); /* Write the line to the proper place */
1693 endutent(); /* Indicate operation is complete */
1697 * Code for locking/unlocking the serial device.
1698 * This code is derived from chat.c.
1701 #define LOCK_PREFIX "/var/lock/LCK.."
1704 * lock - create a lock file for the named device
1707 int lock (char *dev)
1709 char hdb_lock_buffer[12];
1713 p = strrchr(dev, '/');
1719 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1720 if (lock_file == NULL)
1722 novm("lock file name");
1725 strcpy (lock_file, LOCK_PREFIX);
1726 strcat (lock_file, dev);
1728 * Attempt to create the lock file at this point.
1732 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1735 sprintf(hdb_lock_buffer, "%010d\n", getpid());
1736 write(fd, hdb_lock_buffer, 11);
1741 * If the file exists then check to see if the pid is stale
1743 if (errno == EEXIST)
1745 fd = open(lock_file, O_RDONLY, 0);
1748 if (errno == ENOENT) /* This is just a timing problem. */
1755 /* Read the lock file to find out who has the device locked */
1756 n = read (fd, hdb_lock_buffer, 11);
1760 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1764 /* See the process still exists. */
1767 hdb_lock_buffer[n] = '\0';
1768 sscanf (hdb_lock_buffer, " %d", &pid);
1769 if (kill(pid, 0) == -1 && errno == ESRCH)
1775 /* If the process does not exist then try to remove the lock */
1776 if (n == 0 && unlink (lock_file) == 0)
1778 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1783 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
1787 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1797 * unlock - remove our lockfile