]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
add stuff for packet filtering
[ppp.git] / pppd / sys-linux.c
index 676839bf359ad52d6b2d8715fa60cdfa0cb27dc8..448d426dfdf04249fa086dec1b6496702bb60dfe 100644 (file)
 #endif
 #endif /* IPX_CHANGE */
 
+#ifdef PPP_FILTER
+#include <net/bpf.h>
+#include <linux/filter.h>
+#endif /* PPP_FILTER */
+
 #ifdef LOCKLIB
 #include <sys/locks.h>
 #endif
@@ -282,7 +287,7 @@ void sys_init(void)
 #ifdef INET6
     sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
     if (sock6_fd < 0)
-       fatal("Couldn't create IPv6 socket: %m(%d)", errno);
+       sock6_fd = -errno;      /* save errno for later */
 #endif
 
     FD_ZERO(&in_fds);
@@ -442,8 +447,9 @@ int establish_ppp (int tty_fd)
         */
        set_ppp_fd (tty_fd);
        if (ioctl(tty_fd, PPPIOCGUNIT, &x) < 0) {       
-           if ( ! ok_error (errno))
-               fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno);
+           if (ok_error (errno))
+               goto err;
+           fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno);
        }
        /* Check that we got the same unit again. */
        if (looped && x != ifunit)
@@ -988,13 +994,13 @@ int read_packet (unsigned char *buf)
     nr = -1;
     if (ppp_fd >= 0) {
        nr = read(ppp_fd, buf, len);
-       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO)
+       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR)
            error("read: %m");
     }
     if (nr < 0 && new_style_driver && ifunit >= 0) {
        /* N.B. we read ppp_fd first since LCP packets come in there. */
        nr = read(ppp_dev_fd, buf, len);
-       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO)
+       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR)
            error("read /dev/ppp: %m");
     }
     return (new_style_driver && nr > 0)? nr+2: nr;
@@ -1158,6 +1164,33 @@ void ccp_flags_set (int unit, int isopen, int isup)
     }
 }
 
+#ifdef PPP_FILTER
+/*
+ * set_filters - set the active and pass filters in the kernel driver.
+ */
+int set_filters(struct bpf_program *pass, struct bpf_program *active)
+{
+       struct sock_fprog fp;
+
+       fp.len = pass->bf_len;
+       fp.filter = (struct sock_filter *) pass->bf_insns;
+       if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
+               if (errno == ENOTTY)
+                       warn("kernel does not support PPP filtering");
+               else
+                       error("Couldn't set pass-filter in kernel: %m");
+               return 0;
+       }
+       fp.len = active->bf_len;
+       fp.filter = (struct sock_filter *) active->bf_insns;
+       if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
+               error("Couldn't set active-filter in kernel: %m");
+               return 0;
+       }
+       return 1;
+}
+#endif /* PPP_FILTER */
+
 /********************************************************************
  *
  * get_idle_time - return how long the link has been idle.
@@ -2299,6 +2332,11 @@ int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
     struct ifreq ifr;
     struct in6_rtmsg rt6;
 
+    if (sock6_fd < 0) {
+       errno = -sock6_fd;
+       error("IPv6 socket creation failed: %m");
+       return 0;
+    }
     memset(&ifr, 0, sizeof (ifr));
     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
@@ -2343,6 +2381,11 @@ int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
     struct ifreq ifr;
     struct in6_ifreq ifr6;
 
+    if (sock6_fd < 0) {
+       errno = -sock6_fd;
+       error("IPv6 socket creation failed: %m");
+       return 0;
+    }
     memset(&ifr, 0, sizeof(ifr));
     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {