]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-bsd.c
use strlcpy, strlcat, slprintf everywhere
[ppp.git] / pppd / sys-bsd.c
index 5f0d66d9a2f85cf2624cb072a5654f7d31bfb03f..c2475a18d0b28cee72aba20a06ea0786a6f08a3c 100644 (file)
@@ -21,7 +21,8 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: sys-bsd.c,v 1.27 1997/03/04 03:43:53 paulus Exp $";
+static char rcsid[] = "$Id: sys-bsd.c,v 1.36 1999/03/12 06:07:21 paulus Exp $";
+/*     $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */
 #endif
 
 /*
@@ -42,6 +43,13 @@ static char rcsid[] = "$Id: sys-bsd.c,v 1.27 1997/03/04 03:43:53 paulus Exp $";
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/stat.h>
+#include <sys/param.h>
+#ifdef NetBSD1_2
+#include <util.h>
+#endif
+#ifdef PPP_FILTER
+#include <net/bpf.h>
+#endif
 
 #include <net/if.h>
 #include <net/ppp_defs.h>
@@ -51,10 +59,21 @@ static char rcsid[] = "$Id: sys-bsd.c,v 1.27 1997/03/04 03:43:53 paulus Exp $";
 #include <netinet/in.h>
 
 #if RTM_VERSION >= 3
+#include <sys/param.h>
+#if defined(NetBSD) && (NetBSD >= 199703)
+#include <netinet/if_inarp.h>
+#else  /* NetBSD 1.2D or later */
+#ifdef __FreeBSD__
 #include <netinet/if_ether.h>
+#else
+#include <net/if_ether.h>
+#endif
+#endif
 #endif
 
 #include "pppd.h"
+#include "fsm.h"
+#include "ipcp.h"
 
 static int initdisc = -1;      /* Initial TTY discipline for ppp_fd */
 static int initfdflags = -1;   /* Initial file descriptor flags for ppp_fd */
@@ -109,7 +128,7 @@ sys_cleanup()
     struct ifreq ifr;
 
     if (if_is_up) {
-       strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+       strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
            && ((ifr.ifr_flags & IFF_UP) != 0)) {
            ifr.ifr_flags &= ~IFF_UP;
@@ -140,12 +159,18 @@ sys_close()
 /*
  * sys_check_options - check the options that the user specified
  */
-void
+int
 sys_check_options()
 {
+#ifndef CDTRCTS
+    if (crtscts == 2) {
+       syslog(LOG_WARNING, "DTR/CTS flow control is not supported on this system");
+       return 0;
+    }
+#endif
+    return 1;
 }
 
-
 /*
  * ppp_available - check whether the system has any ppp interfaces
  * (in fact we check whether we can do an ioctl on ppp0).
@@ -160,7 +185,7 @@ ppp_available()
     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        return 1;               /* can't tell */
 
-    strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), "ppp0");
     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
     close(s);
 
@@ -367,10 +392,19 @@ set_up_tty(fd, local)
     }
 
     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
-    if (crtscts > 0 && !local)
-       tios.c_cflag |= CRTSCTS;
-    else if (crtscts < 0)
+    if (crtscts > 0 && !local) {
+        if (crtscts == 2) {
+#ifdef CDTRCTS
+            tios.c_cflag |= CDTRCTS;
+#endif
+       } else
+           tios.c_cflag |= CRTSCTS;
+    } else if (crtscts < 0) {
        tios.c_cflag &= ~CRTSCTS;
+#ifdef CDTRCTS
+       tios.c_cflag &= ~CDTRCTS;
+#endif
+    }
 
     tios.c_cflag |= CS8 | CREAD | HUPCL;
     if (local || !modem)
@@ -523,7 +557,7 @@ output(unit, p, len)
     int len;
 {
     if (debug)
-       log_packet(p, len, "sent ");
+       log_packet(p, len, "sent ", LOG_DEBUG);
 
     if (write(ttyfd, p, len) < 0) {
        if (errno != EIO)
@@ -654,7 +688,7 @@ ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
     u_int x;
     struct ifreq ifr;
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     ifr.ifr_mtu = mtu;
     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
@@ -793,6 +827,32 @@ get_idle_time(u, ip)
 }
 
 
+#ifdef PPP_FILTER
+/*
+ * set_filters - transfer the pass and active filters to the kernel.
+ */
+int
+set_filters(pass, active)
+    struct bpf_program *pass, *active;
+{
+    int ret = 1;
+
+    if (pass->bf_len > 0) {
+       if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
+           syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
+           ret = 0;
+       }
+    }
+    if (active->bf_len > 0) {
+       if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
+           syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
+           ret = 0;
+       }
+    }
+    return ret;
+}
+#endif
+
 /*
  * sifvjcomp - config tcp header compression
  */
@@ -828,7 +888,7 @@ sifup(u)
 {
     struct ifreq ifr;
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        return 0;
@@ -879,7 +939,7 @@ sifdown(u)
     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
     /* ignore errors, because ppp_fd might have been closed by now. */
 
-    strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
        syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
        rv = 0;
@@ -912,8 +972,9 @@ sifaddr(u, o, h, m)
     u_int32_t o, h, m;
 {
     struct ifaliasreq ifra;
+    struct ifreq ifr;
 
-    strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
+    strlcpy(ifra.ifra_name, sizeof(ifra.ifra_name), ifname);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
@@ -923,6 +984,12 @@ sifaddr(u, o, h, m)
        ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
     } else
        BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
+    BZERO(&ifr, sizeof(ifr));
+    strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
+    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
+       if (errno != EADDRNOTAVAIL)
+           syslog(LOG_WARNING, "Couldn't remove interface address: %m");
+    }
     if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
        if (errno != EEXIST) {
            syslog(LOG_ERR, "Couldn't set interface address: %m");
@@ -949,7 +1016,7 @@ cifaddr(u, o, h)
     struct ifaliasreq ifra;
 
     ifaddrs[0] = 0;
-    strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
+    strlcpy(ifra.ifra_name, sizeof(ifra.ifra_name), ifname);
     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
@@ -1009,7 +1076,7 @@ dodefaultroute(g, cmd)
 
     memset(&rtmsg, 0, sizeof(rtmsg));
     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
-    rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
+    rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
     rtmsg.hdr.rtm_version = RTM_VERSION;
     rtmsg.hdr.rtm_seq = ++rtm_seq;
     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
@@ -1227,7 +1294,7 @@ get_ether_addr(ipaddr, hwaddr)
                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
        if (ifr->ifr_addr.sa_family == AF_INET) {
            ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
-           strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+           strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
            /*
             * Check that the interface is up, and not point-to-point
             * or loopback.
@@ -1326,7 +1393,7 @@ GetMask(addr)
        /*
         * Check that the interface is up, and not point-to-point or loopback.
         */
-       strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+       strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
            continue;
        if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
@@ -1343,6 +1410,26 @@ GetMask(addr)
     return mask;
 }
 
+/*
+ * have_route_to - determine if the system has any route to
+ * a given IP address.
+ * For demand mode to work properly, we have to ignore routes
+ * through our own interface.
+ */
+int have_route_to(u_int32_t addr)
+{
+    return -1;
+}
+
+/*
+ * Use the hostid as part of the random number seed.
+ */
+int
+get_host_seed()
+{
+    return gethostid();
+}
+
 /*
  * lock - create a lock file for the named lock device
  */
@@ -1355,13 +1442,15 @@ lock(dev)
     char hdb_lock_buffer[12];
     int fd, pid, n;
     char *p;
+    size_t l;
 
     if ((p = strrchr(dev, '/')) != NULL)
        dev = p + 1;
-    lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
+    l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
+    lock_file = malloc(l);
     if (lock_file == NULL)
        novm("lock file name");
-    strcat(strcpy(lock_file, LOCK_PREFIX), dev);
+    slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
 
     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
        if (errno == EEXIST