diff options
author | 2016-05-28 07:00:18 +0000 | |
---|---|---|
committer | 2016-05-28 07:00:18 +0000 | |
commit | 2abf9a0d86fa9b4fb1b6fb30d464b452304d1b90 (patch) | |
tree | 3c1599cc8846249363d55b245b3c954b2c92dc84 | |
parent | Do the endpoint verification before opening the pipe on the selected (diff) | |
download | wireguard-openbsd-2abf9a0d86fa9b4fb1b6fb30d464b452304d1b90.tar.xz wireguard-openbsd-2abf9a0d86fa9b4fb1b6fb30d464b452304d1b90.zip |
Replace the /dev/bpf* open loop with a plain open("/dev/bpf0", ...).
ok deraadt jca
-rw-r--r-- | usr.sbin/arp/arp.c | 31 | ||||
-rw-r--r-- | usr.sbin/dhcpd/bpf.c | 27 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/bpf.c | 29 | ||||
-rw-r--r-- | usr.sbin/hostapd/hostapd.c | 23 | ||||
-rw-r--r-- | usr.sbin/mopd/common/pf.c | 14 | ||||
-rw-r--r-- | usr.sbin/npppd/npppd/privsep.c | 4 | ||||
-rw-r--r-- | usr.sbin/npppd/pppoe/pppoed.c | 23 | ||||
-rw-r--r-- | usr.sbin/rarpd/rarpd.c | 24 | ||||
-rw-r--r-- | usr.sbin/rbootd/bpf.c | 18 | ||||
-rw-r--r-- | usr.sbin/rbootd/pathnames.h | 3 | ||||
-rw-r--r-- | usr.sbin/rbootd/rbootd.8 | 6 |
11 files changed, 44 insertions, 158 deletions
diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c index 019d4bf0b07..77c5a6b599c 100644 --- a/usr.sbin/arp/arp.c +++ b/usr.sbin/arp/arp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arp.c,v 1.74 2016/03/23 08:28:31 mpi Exp $ */ +/* $OpenBSD: arp.c,v 1.75 2016/05/28 07:00:18 natano Exp $ */ /* $NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd Exp $ */ /* @@ -808,12 +808,7 @@ sec2str(time_t total) * POSSIBILITY OF SUCH DAMAGE. */ -#ifndef BPF_PATH_FORMAT -#define BPF_PATH_FORMAT "/dev/bpf%u" -#endif - int do_wakeup(const char *, const char *, int); -int get_bpf(void); int bind_if_to_bpf(const char *, int); int get_ether(const char *, struct ether_addr *); int send_frame(int, const struct ether_addr *); @@ -825,9 +820,8 @@ wake(const char *ether_addr, const char *iface) char *pname = NULL; int bpf; - bpf = get_bpf(); - if (bpf == -1) - errx(1, "Failed to bind to bpf."); + if ((bpf = open("/dev/bpf0", O_RDWR)) == -1) + err(1, "Failed to bind to bpf"); if (iface == NULL) { if (getifaddrs(&ifa) == -1) @@ -876,25 +870,6 @@ do_wakeup(const char *eaddr, const char *iface, int bpf) } int -get_bpf(void) -{ - char path[PATH_MAX]; - int i, fd; - - for (i = 0; ; i++) { - if (snprintf(path, sizeof(path), BPF_PATH_FORMAT, i) == -1) - return -1; - fd = open(path, O_RDWR); - if (fd != -1) - return fd; - if (errno == EBUSY) - continue; - break; - } - return -1; -} - -int bind_if_to_bpf(const char *ifname, int bpf) { struct ifreq ifr; diff --git a/usr.sbin/dhcpd/bpf.c b/usr.sbin/dhcpd/bpf.c index 6d54149c753..b84fb6d6c89 100644 --- a/usr.sbin/dhcpd/bpf.c +++ b/usr.sbin/dhcpd/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.13 2016/02/06 23:50:10 krw Exp $ */ +/* $OpenBSD: bpf.c,v 1.14 2016/05/28 07:00:18 natano Exp $ */ /* BPF socket interface code, originally contributed by Archie Cobbs. */ @@ -63,8 +63,6 @@ #include "tree.h" #include "dhcpd.h" -#define BPF_FORMAT "/dev/bpf%d" - ssize_t send_packet (struct interface_info *, struct dhcp_packet *, size_t, struct in_addr, struct sockaddr_in *, struct hardware *); @@ -76,26 +74,15 @@ ssize_t send_packet (struct interface_info *, struct dhcp_packet *, int if_register_bpf(struct interface_info *info) { - char filename[50]; - int sock, b; - - /* Open a BPF device */ - for (b = 0; 1; b++) { - snprintf(filename, sizeof(filename), BPF_FORMAT, b); - sock = open(filename, O_RDWR, 0); - if (sock == -1) { - if (errno == EBUSY) - continue; - else - error("Can't find free bpf: %m"); - } else - break; - } + int sock; + + if ((sock = open("/dev/bpf0", O_RDWR)) == -1) + error("Can't open bpf device: %m"); /* Set the BPF device to point at this interface. */ if (ioctl(sock, BIOCSETIF, info->ifp) == -1) - error("Can't attach interface %s to bpf device %s: %m", - info->name, filename); + error("Can't attach interface %s to bpf device: %m", + info->name); info->send_packet = send_packet; return (sock); diff --git a/usr.sbin/dhcrelay/bpf.c b/usr.sbin/dhcrelay/bpf.c index 85ac2c95e9e..9a498c98f23 100644 --- a/usr.sbin/dhcrelay/bpf.c +++ b/usr.sbin/dhcrelay/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.10 2016/02/07 00:49:28 krw Exp $ */ +/* $OpenBSD: bpf.c,v 1.11 2016/05/28 07:00:18 natano Exp $ */ /* BPF socket interface code, originally contributed by Archie Cobbs. */ @@ -60,9 +60,6 @@ #include "dhcp.h" #include "dhcpd.h" - -#define BPF_FORMAT "/dev/bpf%d" - /* * Called by get_interface_list for each interface that's discovered. * Opens a packet filter for each interface and adds it to the select @@ -71,26 +68,16 @@ int if_register_bpf(struct interface_info *info) { - char filename[50]; - int sock, b; - - /* Open a BPF device */ - for (b = 0; 1; b++) { - snprintf(filename, sizeof(filename), BPF_FORMAT, b); - sock = open(filename, O_RDWR, 0); - if (sock == -1) { - if (errno == EBUSY) - continue; - else - error("Can't find free bpf: %m"); - } else - break; - } + int sock; + + /* Open the BPF device */ + if ((sock = open("/dev/bpf0", O_RDWR)) == -1) + error("Can't open bpf device: %m"); /* Set the BPF device to point at this interface. */ if (ioctl(sock, BIOCSETIF, info->ifp) == -1) - error("Can't attach interface %s to bpf device %s: %m", - info->name, filename); + error("Can't attach interface %s to bpf device: %m", + info->name); return (sock); } diff --git a/usr.sbin/hostapd/hostapd.c b/usr.sbin/hostapd/hostapd.c index 942ecfbd8a2..3bb5bd5a572 100644 --- a/usr.sbin/hostapd/hostapd.c +++ b/usr.sbin/hostapd/hostapd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hostapd.c,v 1.36 2015/12/22 19:45:09 mmcc Exp $ */ +/* $OpenBSD: hostapd.c,v 1.37 2016/05/28 07:00:18 natano Exp $ */ /* * Copyright (c) 2004, 2005 Reyk Floeter <reyk@openbsd.org> @@ -170,29 +170,14 @@ hostapd_check_file_secrecy(int fd, const char *fname) int hostapd_bpf_open(u_int flags) { - u_int i; int fd = -1; - char *dev; struct bpf_version bpv; - /* - * Try to open the next available BPF device - */ - for (i = 0; i < 255; i++) { - if (asprintf(&dev, "/dev/bpf%u", i) == -1) - hostapd_fatal("failed to allocate buffer\n"); - - if ((fd = open(dev, flags)) != -1) { - free(dev); - break; - } - - free(dev); + if ((fd = open("/dev/bpf0", flags)) == -1) { + hostapd_fatal("unable to open BPF device: %s\n", + strerror(errno)); } - if (fd == -1) - hostapd_fatal("unable to open BPF device\n"); - /* * Get and validate the BPF version */ diff --git a/usr.sbin/mopd/common/pf.c b/usr.sbin/mopd/common/pf.c index e01cf27bec6..939fbf31653 100644 --- a/usr.sbin/mopd/common/pf.c +++ b/usr.sbin/mopd/common/pf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pf.c,v 1.15 2009/10/27 23:59:52 deraadt Exp $ */ +/* $OpenBSD: pf.c,v 1.16 2016/05/28 07:00:18 natano Exp $ */ /* * Copyright (c) 1993-95 Mats O Jansson. All rights reserved. @@ -79,8 +79,6 @@ int pfInit(char *interface, int mode, u_short protocol, int typ) { int fd; - int n = 0; - char device[sizeof "/dev/bpf000"]; struct ifreq ifr; u_int dlt; int immediate; @@ -100,13 +98,7 @@ pfInit(char *interface, int mode, u_short protocol, int typ) insns }; - /* Go through all the minors and find one that isn't in use. */ - do { - snprintf(device, sizeof device, "/dev/bpf%d", n++); - fd = open(device, mode); - } while (fd < 0 && errno == EBUSY); - - if (fd < 0) { + if ((fd = open("/dev/bpf0", mode)) == -1) { syslog(LOG_ERR,"pfInit: open bpf %m"); return (-1); } @@ -129,7 +121,7 @@ pfInit(char *interface, int mode, u_short protocol, int typ) return (-1); } if (dlt != DLT_EN10MB) { - syslog(LOG_ERR,"pfInit: %s is not ethernet", device); + syslog(LOG_ERR,"pfInit: %s is not ethernet", interface); return (-1); } if (promisc) diff --git a/usr.sbin/npppd/npppd/privsep.c b/usr.sbin/npppd/npppd/privsep.c index 765dead5017..d248b724bed 100644 --- a/usr.sbin/npppd/npppd/privsep.c +++ b/usr.sbin/npppd/npppd/privsep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.c,v 1.21 2016/02/02 17:51:11 sthen Exp $ */ +/* $OpenBSD: privsep.c,v 1.22 2016/05/28 07:00:18 natano Exp $ */ /* * Copyright (c) 2010 Yasuoka Masahiko <yasuoka@openbsd.org> @@ -983,7 +983,7 @@ privsep_npppd_check_open(struct PRIVSEP_OPEN_ARG *arg) int readonly; } const allow_paths[] = { { NPPPD_DIR "/", 1, 1 }, - { "/dev/bpf", 1, 0 }, + { "/dev/bpf0", 0, 0 }, { "/etc/resolv.conf", 0, 1 }, { "/dev/tun", 1, 0 }, { "/dev/pppx", 1, 0 } diff --git a/usr.sbin/npppd/pppoe/pppoed.c b/usr.sbin/npppd/pppoe/pppoed.c index 10d73d517c6..ec4e90e496b 100644 --- a/usr.sbin/npppd/pppoe/pppoed.c +++ b/usr.sbin/npppd/pppoe/pppoed.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pppoed.c,v 1.19 2015/12/17 08:09:20 tb Exp $ */ +/* $OpenBSD: pppoed.c,v 1.20 2016/05/28 07:00:18 natano Exp $ */ /*- * Copyright (c) 2009 Internet Initiative Japan Inc. @@ -28,7 +28,7 @@ /**@file * This file provides the PPPoE(RFC2516) server(access concentrator) * implementaion. - * $Id: pppoed.c,v 1.19 2015/12/17 08:09:20 tb Exp $ + * $Id: pppoed.c,v 1.20 2016/05/28 07:00:18 natano Exp $ */ #include <sys/param.h> /* ALIGN */ #include <sys/types.h> @@ -202,9 +202,7 @@ pppoed_reload_listeners(pppoed *_this) static int pppoed_listener_start(pppoed_listener *_this, int restart) { - int i; int log_level; - char buf[BUFSIZ]; struct ifreq ifreq; int ival; int found; @@ -275,17 +273,8 @@ pppoed_listener_start(pppoed_listener *_this, int restart) goto fail; } - /* Open /dev/bpfXX */ - /* FIXME: /dev/bpf of NetBSD3.0 can simultaneity open */ - for (i = 0; i < 256; i++) { - snprintf(buf, sizeof(buf), "/dev/bpf%d", i); - if ((_this->bpf = priv_open(buf, O_RDWR)) >= 0) { - break; - } else if (errno == ENXIO || errno == ENOENT) - break; /* no more entries */ - } - if (_this->bpf < 0) { - pppoed_log(_pppoed, log_level, "Cannot open bpf"); + if ((_this->bpf = priv_open("/dev/bpf0", O_RDWR)) == -1) { + pppoed_log(_pppoed, log_level, "Cannot open bpf: %m"); goto fail; } @@ -327,9 +316,9 @@ pppoed_listener_start(pppoed_listener *_this, int restart) pppoed_io_event, _this); event_add(&_this->ev_bpf, NULL); - pppoed_log(_pppoed, LOG_INFO, "Listening on %s (PPPoE) [%s] using=%s " + pppoed_log(_pppoed, LOG_INFO, "Listening on %s (PPPoE) [%s] " "address=%02x:%02x:%02x:%02x:%02x:%02x", _this->listen_ifname, - _this->tun_name, buf, _this->ether_addr[0], _this->ether_addr[1], + _this->tun_name, _this->ether_addr[0], _this->ether_addr[1], _this->ether_addr[2], _this->ether_addr[3], _this->ether_addr[4], _this->ether_addr[5]); diff --git a/usr.sbin/rarpd/rarpd.c b/usr.sbin/rarpd/rarpd.c index 7f01514f506..97dddcd2d9a 100644 --- a/usr.sbin/rarpd/rarpd.c +++ b/usr.sbin/rarpd/rarpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rarpd.c,v 1.67 2015/11/19 19:31:20 deraadt Exp $ */ +/* $OpenBSD: rarpd.c,v 1.68 2016/05/28 07:00:18 natano Exp $ */ /* $NetBSD: rarpd.c,v 1.25 1998/04/23 02:48:33 mrg Exp $ */ /* @@ -226,25 +226,6 @@ usage(void) exit(1); } -static int -bpf_open(void) -{ - int fd, n = 0; - char device[sizeof "/dev/bpf0000000000"]; - - /* Go through all the minors and find one that isn't in use. */ - do { - (void) snprintf(device, sizeof device, "/dev/bpf%d", n++); - fd = open(device, O_RDWR); - } while (fd < 0 && errno == EBUSY); - - if (fd < 0) { - error(FATAL, "%s: %s", device, strerror(errno)); - /* NOTREACHED */ - } - return fd; -} - static struct bpf_insn insns[] = { BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12), BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_REVARP, 0, 3), @@ -271,7 +252,8 @@ rarp_open(char *device) struct ifreq ifr; u_int dlt; - fd = bpf_open(); + if ((fd = open("/dev/bpf0", O_RDWR)) == -1) + error(FATAL, "/dev/bpf0: %s", strerror(errno)); /* Set immediate mode so packets are processed as they arrive. */ immediate = 1; diff --git a/usr.sbin/rbootd/bpf.c b/usr.sbin/rbootd/bpf.c index a7a068c5c6c..cce33fa6351 100644 --- a/usr.sbin/rbootd/bpf.c +++ b/usr.sbin/rbootd/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.24 2016/04/16 22:23:01 natano Exp $ */ +/* $OpenBSD: bpf.c,v 1.25 2016/05/28 07:00:18 natano Exp $ */ /* $NetBSD: bpf.c,v 1.5.2.1 1995/11/14 08:45:42 thorpej Exp $ */ /* @@ -61,7 +61,6 @@ #include <limits.h> #include <ifaddrs.h> #include "defs.h" -#include "pathnames.h" static int BpfFd = -1; static unsigned int BpfLen = 0; @@ -83,19 +82,10 @@ int BpfOpen(void) { struct ifreq ifr; - char bpfdev[32]; - int n = 0; + int n; - /* - * Open the first available BPF device. - */ - do { - (void) snprintf(bpfdev, sizeof bpfdev, _PATH_BPF, n++); - BpfFd = open(bpfdev, O_RDWR); - } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); - - if (BpfFd < 0) { - syslog(LOG_ERR, "bpf: no available devices: %m"); + if ((BpfFd = open("/dev/bpf0", O_RDWR)) == -1) { + syslog(LOG_ERR, "bpf: can't open device: %m"); DoExit(); } diff --git a/usr.sbin/rbootd/pathnames.h b/usr.sbin/rbootd/pathnames.h index 5f969b57523..8f53e32b5f5 100644 --- a/usr.sbin/rbootd/pathnames.h +++ b/usr.sbin/rbootd/pathnames.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pathnames.h,v 1.5 2004/05/01 00:39:22 deraadt Exp $ */ +/* $OpenBSD: pathnames.h,v 1.6 2016/05/28 07:00:18 natano Exp $ */ /* $NetBSD: pathnames.h,v 1.3 1995/08/21 17:05:15 thorpej Exp $ */ /* @@ -43,7 +43,6 @@ * Author: Jeff Forys, University of Utah CSS */ -#define _PATH_BPF "/dev/bpf%d" #define _PATH_RBOOTDCONF "/etc/rbootd.conf" #define _PATH_RBOOTDDBG "/tmp/rbootd.dbg" #define _PATH_RBOOTDDIR "/usr/mdec/rbootd" diff --git a/usr.sbin/rbootd/rbootd.8 b/usr.sbin/rbootd/rbootd.8 index 011959337af..cc1f4d57968 100644 --- a/usr.sbin/rbootd/rbootd.8 +++ b/usr.sbin/rbootd/rbootd.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: rbootd.8,v 1.15 2015/10/28 10:02:59 jmc Exp $ +.\" $OpenBSD: rbootd.8,v 1.16 2016/05/28 07:00:18 natano Exp $ .\" $NetBSD: rbootd.8,v 1.3 1995/08/21 17:05:16 thorpej Exp $ .\" .\" Copyright (c) 1988, 1992 The University of Utah and the Center @@ -41,7 +41,7 @@ .\" Utah Hdr: rbootd.man 3.1 92/07/06 .\" Author: Jeff Forys, University of Utah CSS .\" -.Dd $Mdocdate: October 28 2015 $ +.Dd $Mdocdate: May 28 2016 $ .Dt RBOOTD 8 .Os .Sh NAME @@ -135,7 +135,7 @@ Turn off debugging, do nothing if already off. .El .Sh FILES .Bl -tag -width /usr/libexec/rbootd -compact -.It Pa /dev/bpf# +.It Pa /dev/bpf0 packet-filter device .It Pa /etc/rbootd.conf configuration file |