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 prev_kdebugflag = 0;
56 static int has_default_route = 0;
57 static int has_proxy_arp = 0;
58 static int driver_version = 0;
59 static int driver_modification = 0;
60 static int driver_patch = 0;
61 static int restore_term = 0; /* 1 => we've munged the terminal */
62 static struct termios inittermios; /* Initial TTY termios */
64 int sockfd; /* socket for doing interface ioctls */
66 static char *lock_file;
70 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
71 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
72 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
75 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
79 #define SET_SA_FAMILY(addr, family) \
80 memset ((char *) &(addr), '\0', sizeof(addr)); \
81 addr.sa_family = (family);
84 * Determine if the PPP connection should still be present.
88 #define still_ppp() (hungup == 0)
91 * Functions to read and set the flags value in the device driver
94 static int get_flags (void)
98 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
100 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
104 MAINDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
108 static void set_flags (int flags)
110 MAINDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
112 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
114 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m", flags);
120 * sys_init - System-dependent initialization.
125 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
126 setlogmask(LOG_UPTO(LOG_INFO));
129 setlogmask(LOG_UPTO(LOG_DEBUG));
132 /* Get an internet socket for doing socket ioctls. */
133 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
136 syslog(LOG_ERR, "Couldn't create IP socket: %m");
142 * note_debug_level - note a change in the debug level.
145 void note_debug_level (void)
149 MAINDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
150 setlogmask(LOG_UPTO(LOG_DEBUG));
154 setlogmask(LOG_UPTO(LOG_WARNING));
159 * set_kdebugflag - Define the debugging level for the kernel
162 int set_kdebugflag (int requested_level)
164 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0)
166 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
170 if (prev_kdebugflag != requested_level)
172 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0)
174 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
177 MAINDEBUG ((LOG_INFO, "set kernel debugging level to %d", requested_level));
183 * establish_ppp - Turn the serial port into a ppp interface.
186 void establish_ppp (void)
191 if (ioctl(fd, TIOCEXCL, 0) < 0)
193 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
196 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
198 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
202 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
204 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
208 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
210 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
214 set_kdebugflag (kdebugflag);
216 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
217 SC_RCV_EVNP | SC_RCV_ODDP));
219 MAINDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
220 driver_version, driver_modification, driver_patch));
224 * disestablish_ppp - Restore the serial port to normal operation.
225 * This shouldn't call die() because it's called from die().
228 void disestablish_ppp(void)
233 * If this is no longer PPP mode then there is nothing that can be done
234 * about restoring the previous mode.
242 * Check whether the link seems not to be 8-bit clean.
246 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
249 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
251 case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP:
252 s = "nothing was received";
256 case SC_RCV_B7_0 | SC_RCV_EVNP:
257 case SC_RCV_B7_0 | SC_RCV_ODDP:
258 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
259 s = "all had bit 7 set to 1";
263 case SC_RCV_B7_1 | SC_RCV_EVNP:
264 case SC_RCV_B7_1 | SC_RCV_ODDP:
265 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
266 s = "all had bit 7 set to 0";
270 s = "all had odd parity";
274 s = "all had even parity";
280 syslog(LOG_WARNING, "Receive serial link is not 8-bit clean:");
281 syslog(LOG_WARNING, "Problem: %s", s);
285 set_kdebugflag (prev_kdebugflag);
287 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
289 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
292 if (ioctl(fd, TIOCNXCL, 0) < 0)
294 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
302 * List of valid speeds.
306 int speed_int, speed_val;
378 * Translate from bits/second to a speed_t.
381 int translate_speed (int bps)
383 struct speed *speedp;
387 for (speedp = speeds; speedp->speed_int; speedp++)
389 if (bps == speedp->speed_int)
391 return speedp->speed_val;
394 syslog(LOG_WARNING, "speed %d not supported", bps);
400 * Translate from a speed_t to bits/second.
403 int baud_rate_of (int speed)
405 struct speed *speedp;
409 for (speedp = speeds; speedp->speed_int; speedp++)
411 if (speed == speedp->speed_val)
413 return speedp->speed_int;
421 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
422 * at the requested speed, etc. If `local' is true, set CLOCAL
423 * regardless of whether the modem option was specified.
426 void set_up_tty (int fd, int local)
431 if (tcgetattr(fd, &tios) < 0)
433 syslog(LOG_ERR, "tcgetattr: %m");
442 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
443 tios.c_cflag |= CS8 | CREAD | HUPCL;
445 tios.c_iflag = IGNBRK | IGNPAR;
449 tios.c_cc[VTIME] = 0;
453 tios.c_cflag ^= (CLOCAL | HUPCL);
459 tios.c_cflag |= CRTSCTS;
463 tios.c_iflag |= IXOFF;
464 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
465 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
469 tios.c_cflag &= ~CRTSCTS;
476 speed = translate_speed(inspeed);
479 cfsetospeed (&tios, speed);
480 cfsetispeed (&tios, speed);
483 * We can't proceed if the serial port speed is B0,
484 * since that implies that the serial port is disabled.
488 speed = cfgetospeed(&tios);
491 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
497 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
499 syslog(LOG_ERR, "tcsetattr: %m");
503 baud_rate = baud_rate_of(speed);
508 * setdtr - control the DTR line on the serial port.
509 * This is called from die(), so it shouldn't call die().
512 void setdtr (int fd, int on)
514 int modembits = TIOCM_DTR;
516 ioctl(fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
520 * restore_tty - restore the terminal to the saved settings.
523 void restore_tty (void)
529 * Turn off echoing, because otherwise we can get into
530 * a loop with the tty and the modem echoing to each other.
531 * We presume we are the sole user of this tty device, so
532 * when we close it, it will revert to its defaults anyway.
536 inittermios.c_lflag &= ~(ECHO | ECHONL);
539 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
543 syslog(LOG_WARNING, "tcsetattr: %m");
550 * output - Output PPP packet.
553 void output (int unit, unsigned char *p, int len)
557 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
562 log_packet(p, len, "sent ");
565 if (write(fd, p, len) < 0)
567 syslog(LOG_ERR, "write: %m");
573 * wait_input - wait until there is data available on fd,
574 * for the length of time specified by *timo (indefinite
578 void wait_input (struct timeval *timo)
586 n = select(fd+1, &ready, NULL, &ready, timo);
587 if (n < 0 && errno != EINTR)
589 syslog(LOG_ERR, "select: %m");
595 * read_packet - get a PPP packet from the serial device.
598 int read_packet (unsigned char *buf)
602 len = read(fd, buf, PPP_MTU + PPP_HDRLEN);
605 if (errno == EWOULDBLOCK)
609 syslog(LOG_ERR, "read(fd): %m");
616 * ppp_send_config - configure the transmit characteristics of
620 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
625 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
627 * If we were called because the link has gone down then there is nothing
628 * which may be done. Just return without incident.
635 * Set the MTU and other parameters for the ppp device
637 memset (&ifr, '\0', sizeof (ifr));
638 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
641 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
643 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
647 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
648 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
650 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
655 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
656 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
661 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
664 void ppp_set_xaccm (int unit, ext_accm accm)
666 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
667 accm[0], accm[1], accm[2], accm[3]));
669 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
671 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
676 * ppp_recv_config - configure the receive-side characteristics of
680 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
684 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
686 * If we were called because the link has gone down then there is nothing
687 * which may be done. Just return without incident.
694 * Set the receiver parameters
696 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
698 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
701 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
702 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
704 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
709 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
714 * ccp_test - ask kernel whether a given compression method
715 * is acceptable for use.
718 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
720 struct ppp_option_data data;
722 memset (&data, '\0', sizeof (data));
724 data.length = opt_len;
725 data.transmit = for_transmit;
727 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
731 * ccp_flags_set - inform kernel about the current state of CCP.
734 void ccp_flags_set (int unit, int isopen, int isup)
739 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
740 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
746 * ccp_fatal_error - returns 1 if decompression was disabled as a
747 * result of an error detected after decompression of a packet,
748 * 0 otherwise. This is necessary because of patent nonsense.
751 int ccp_fatal_error (int unit)
755 return x & SC_DC_FERROR;
759 * sifvjcomp - config tcp header compression
762 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
764 u_int x = get_flags();
768 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
770 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
775 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
776 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
783 * sifup - Config the interface up and enable IP packets to pass.
790 memset (&ifr, '\0', sizeof (ifr));
791 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
792 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
794 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
798 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
799 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
801 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
808 * sifdown - Config the interface down and disable IP.
815 memset (&ifr, '\0', sizeof (ifr));
816 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
817 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
819 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
823 ifr.ifr_flags &= ~IFF_UP;
824 ifr.ifr_flags |= IFF_POINTOPOINT;
825 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
827 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
834 * sifaddr - Config the interface IP addresses and netmask.
837 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
842 memset (&ifr, '\0', sizeof (ifr));
843 memset (&rt, '\0', sizeof (rt));
845 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
846 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
847 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
849 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
853 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
854 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
858 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
862 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
867 * Set the gateway address
869 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
870 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
872 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
880 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
881 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
883 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
888 * Add the device route
890 SET_SA_FAMILY (rt.rt_dst, AF_INET);
891 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
892 rt.rt_dev = ifname; /* MJC */
894 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
895 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
896 rt.rt_flags = RTF_UP | RTF_HOST;
898 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
900 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
907 * cifaddr - Clear the interface IP addresses, and delete routes
908 * through the interface if possible.
911 int cifaddr (int unit, int our_adr, int his_adr)
915 * Delete the route through the device
917 memset (&rt, '\0', sizeof (rt));
919 SET_SA_FAMILY (rt.rt_dst, AF_INET);
920 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
921 rt.rt_dev = ifname; /* MJC */
923 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
924 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
925 rt.rt_flags = RTF_UP | RTF_HOST;
927 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
929 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
936 * path_to_route - determine the path to the proc file system data
939 FILE *route_fd = (FILE *) 0;
940 static char route_buffer [100];
942 static char *path_to_route (void);
943 static int open_route_table (void);
944 static void close_route_table (void);
945 static int read_route_table (struct rtentry *rt);
946 static int defaultroute_exists (void);
949 * path_to_route - find the path to the route tables in the proc file system
952 static char *path_to_route (void)
954 struct mntent *mntent;
957 fp = fopen (MOUNTED, "r");
960 mntent = getmntent (fp);
961 while (mntent != (struct mntent *) 0)
963 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
965 if (strcmp (mntent->mnt_type, "proc") == 0)
967 strncpy (route_buffer, mntent->mnt_dir,
968 sizeof (route_buffer)-10);
969 route_buffer [sizeof (route_buffer)-10] = '\0';
970 strcat (route_buffer, "/net/route");
973 return (route_buffer);
976 mntent = getmntent (fp);
981 syslog (LOG_ERR, "proc file system not mounted");
986 * close_route_table - close the interface to the route table
989 static void close_route_table (void)
991 if (route_fd != (FILE *) 0)
994 route_fd = (FILE *) 0;
999 * open_route_table - open the interface to the route table
1002 static int open_route_table (void)
1006 close_route_table();
1008 path = path_to_route();
1014 route_fd = fopen (path, "r");
1015 if (route_fd == (FILE *) 0)
1017 syslog (LOG_ERR, "can not open %s: %m", path);
1021 /* read and discard the header line. */
1022 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1024 close_route_table();
1031 * read_route_table - read the next entry from the route table
1034 static int read_route_table (struct rtentry *rt)
1036 static char delims[] = " \t\n";
1037 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1039 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1044 memset (rt, '\0', sizeof (struct rtentry));
1046 dev_ptr = strtok (route_buffer, delims); /* interface name */
1047 dst_ptr = strtok (NULL, delims); /* destination address */
1048 gw_ptr = strtok (NULL, delims); /* gateway */
1049 flag_ptr = strtok (NULL, delims); /* flags */
1051 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1052 strtoul (dst_ptr, NULL, 16);
1054 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1055 strtoul (gw_ptr, NULL, 16);
1057 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1058 rt->rt_dev = dev_ptr;
1064 * defaultroute_exists - determine if there is a default route
1067 static int defaultroute_exists (void)
1072 if (!open_route_table())
1077 while (read_route_table(&rt) != 0)
1079 if ((rt.rt_flags & RTF_UP) == 0)
1084 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L)
1087 "ppp not replacing existing default route to %s[%s]",
1089 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
1096 close_route_table();
1101 * sifdefaultroute - assign a default route through the address given.
1104 int sifdefaultroute (int unit, int gateway)
1108 if (has_default_route == 0)
1110 if (defaultroute_exists())
1115 memset (&rt, '\0', sizeof (rt));
1116 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1117 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1118 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1120 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1121 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
1123 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1127 has_default_route = 1;
1132 * cifdefaultroute - delete a default route through the address given.
1135 int cifdefaultroute (int unit, int gateway)
1139 if (has_default_route)
1141 memset (&rt, '\0', sizeof (rt));
1142 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1143 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1144 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1146 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1147 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1149 if (errno != ENOENT || still_ppp())
1151 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1156 has_default_route = 0;
1161 * sifproxyarp - Make a proxy ARP entry for the peer.
1164 int sifproxyarp (int unit, u_int32_t his_adr)
1166 struct arpreq arpreq;
1168 if (has_proxy_arp == 0)
1170 memset (&arpreq, '\0', sizeof(arpreq));
1172 * Get the hardware address of an interface on the same subnet
1173 * as our local address.
1175 if (!get_ether_addr(his_adr, &arpreq.arp_ha))
1177 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1181 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1182 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1183 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1185 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0)
1187 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1197 * cifproxyarp - Delete the proxy ARP entry for the peer.
1200 int cifproxyarp (int unit, u_int32_t his_adr)
1202 struct arpreq arpreq;
1204 if (has_proxy_arp == 1)
1206 memset (&arpreq, '\0', sizeof(arpreq));
1207 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1209 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1210 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0)
1212 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1221 * get_ether_addr - get the hardware address of an interface on the
1222 * the same subnet as ipaddr.
1225 int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1227 struct ifreq *ifr, *ifend, *ifp;
1229 u_int32_t ina, mask;
1230 struct sockaddr_dl *dla;
1233 struct ifreq ifs[MAX_IFS];
1235 ifc.ifc_len = sizeof(ifs);
1237 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1239 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1243 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1244 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1246 * Scan through looking for an interface with an Internet
1247 * address on the same subnet as `ipaddr'.
1249 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1250 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1252 if (ifr->ifr_addr.sa_family == AF_INET)
1254 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1255 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1256 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1259 * Check that the interface is up, and not point-to-point
1262 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1267 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1272 * Get its netmask and check that it's on the right subnet.
1274 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1279 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1280 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1281 ip_ntoa(ina), ntohl(mask)));
1283 if (((ipaddr ^ ina) & mask) != 0)
1296 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1298 * Now get the hardware address.
1300 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1301 if (ioctl (sockfd, SIOCGIFHWADDR, &ifreq) < 0)
1303 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1309 sizeof (struct sockaddr));
1311 MAINDEBUG ((LOG_DEBUG,
1312 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1313 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1314 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1315 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1316 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1317 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1318 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1319 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1320 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1325 * Return user specified netmask, modified by any mask we might determine
1326 * for address `addr' (in network byte order).
1327 * Here we scan through the system's list of interfaces, looking for
1328 * any non-point-to-point interfaces which might appear to be on the same
1329 * network as `addr'. If we find any, we OR in their netmask to the
1330 * user-specified netmask.
1333 u_int32_t GetMask (u_int32_t addr)
1335 u_int32_t mask, nmask, ina;
1336 struct ifreq *ifr, *ifend, ifreq;
1338 struct ifreq ifs[MAX_IFS];
1342 if (IN_CLASSA(addr)) /* determine network mask for address class */
1344 nmask = IN_CLASSA_NET;
1348 if (IN_CLASSB(addr))
1350 nmask = IN_CLASSB_NET;
1354 nmask = IN_CLASSC_NET;
1358 /* class D nets are disallowed by bad_ip_adrs */
1359 mask = netmask | htonl(nmask);
1361 * Scan through the system's network interfaces.
1363 ifc.ifc_len = sizeof(ifs);
1365 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1367 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1371 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1372 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1375 * Check the interface's internet address.
1377 if (ifr->ifr_addr.sa_family != AF_INET)
1381 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1382 if (((ntohl(ina) ^ addr) & nmask) != 0)
1387 * Check that the interface is up, and not point-to-point nor loopback.
1389 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1390 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1395 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1400 * Get its netmask and OR it into our mask.
1402 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1406 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1413 * Internal routine to decode the version.modification.patch level
1416 static void decode_version (char *buf, int *version,
1417 int *modification, int *patch)
1419 *version = (int) strtoul (buf, &buf, 10);
1426 *modification = (int) strtoul (buf, &buf, 10);
1430 *patch = (int) strtoul (buf, &buf, 10);
1443 * Procedure to determine if the PPP line dicipline is registered.
1447 ppp_registered(void)
1450 int ppp_disc = N_PPP;
1454 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1457 syslog(LOG_ERR, "Failed to open %s: %m", devnam);
1461 initfdflags = fcntl(local_fd, F_GETFL);
1462 if (initfdflags == -1)
1464 syslog(LOG_ERR, "Couldn't get device fd flags: %m");
1469 initfdflags &= ~O_NONBLOCK;
1470 fcntl(local_fd, F_SETFL, initfdflags);
1472 * Read the initial line dicipline and try to put the device into the
1475 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1477 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
1482 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1484 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1489 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1491 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1501 * ppp_available - check whether the system has any ppp interfaces
1502 * (in fact we check whether we can do an ioctl on ppp0).
1505 int ppp_available(void)
1509 char abBuffer [1024];
1511 int my_version, my_modification, my_patch;
1513 * Open a socket for doing the ioctl operations.
1515 s = socket(AF_INET, SOCK_DGRAM, 0);
1521 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1522 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1524 * If the device did not exist then attempt to create one by putting the
1525 * current tty into the PPP discipline. If this works then obtain the
1526 * flags for the device again.
1530 if (ppp_registered())
1532 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1533 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1537 * Ensure that the hardware address is for PPP and not something else
1541 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1544 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1554 * This is the PPP device. Validate the version of the driver at this
1555 * point to ensure that this program will work with the driver.
1557 ifr.ifr_data = abBuffer;
1558 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1563 decode_version (abBuffer,
1565 &driver_modification,
1572 driver_modification =
1576 * Validate the version of the driver against the version that we used.
1578 decode_version (PPP_VERSION,
1583 /* The version numbers must match */
1584 if (driver_version != my_version)
1589 /* The modification levels must be legal */
1590 if (driver_modification < my_modification)
1597 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1598 driver_version, driver_modification, driver_patch);
1608 * Update the wtmp file with the appropriate user name and tty device.
1611 int logwtmp (char *line, char *name, char *host)
1616 * Control the 'mesg' function based upon the state of the logon
1619 mode = (*name != '\0') ? 0600 : 0622;
1620 if (chmod (devnam, mode) < 0)
1622 syslog (LOG_ERR, "chmod(\"%s\", 0%o): %m", devnam, mode);
1625 * Update the signon database for users.
1627 memset (&ut, 0, sizeof (ut));
1628 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1629 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1630 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1631 (void)time(&ut.ut_time);
1633 pututline (&ut); /* Write the line to the proper place */
1634 endutent(); /* Indicate operation is complete */
1638 * Code for locking/unlocking the serial device.
1639 * This code is derived from chat.c.
1642 #define LOCK_PREFIX "/var/lock/LCK.."
1645 * lock - create a lock file for the named device
1648 int lock (char *dev)
1650 char hdb_lock_buffer[12];
1654 p = strrchr(dev, '/');
1660 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1661 if (lock_file == NULL)
1663 novm("lock file name");
1666 strcpy (lock_file, LOCK_PREFIX);
1667 strcat (lock_file, dev);
1669 * Attempt to create the lock file at this point.
1673 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1676 sprintf(hdb_lock_buffer, "%010d\n", getpid());
1677 write(fd, hdb_lock_buffer, 11);
1682 * If the file exists then check to see if the pid is stale
1684 if (errno == EEXIST)
1686 fd = open(lock_file, O_RDONLY, 0);
1689 if (errno == ENOENT) /* This is just a timing problem. */
1696 /* Read the lock file to find out who has the device locked */
1697 n = read (fd, hdb_lock_buffer, 11);
1701 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1705 /* See the process still exists. */
1708 hdb_lock_buffer[n] = '\0';
1709 sscanf (hdb_lock_buffer, " %d", &pid);
1710 if (kill(pid, 0) == -1 && errno == ESRCH)
1716 /* If the process does not exist then try to remove the lock */
1717 if (n == 0 && unlink (lock_file) == 0)
1719 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1724 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
1728 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1738 * unlock - remove our lockfile