summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkrw <krw@openbsd.org>2019-01-03 16:42:30 +0000
committerkrw <krw@openbsd.org>2019-01-03 16:42:30 +0000
commit641cab3e3338882b84177e04710102cc952b7f18 (patch)
tree3a8933aaba6eb861df264678102179b24afaa0d1
parentbe less verbose on resolver "errors", only log_warnx() real errors and not (diff)
downloadwireguard-openbsd-641cab3e3338882b84177e04710102cc952b7f18.tar.xz
wireguard-openbsd-641cab3e3338882b84177e04710102cc952b7f18.zip
The need for separate bpf and routing message buffers was eliminated
when dhclient moved to processing the entire buffer rather than trying to process one packet per dispatch() loop. So use ifi->rbuf for both purposes, resizing it during initialization to accommodate the larger of the routing socket read size or the bpf socket read size. Eliminates a calloc()/free() per routing socket read.
-rw-r--r--sbin/dhclient/dhclient.c40
-rw-r--r--sbin/dhclient/dhcpd.h3
-rw-r--r--sbin/dhclient/dispatch.c3
3 files changed, 25 insertions, 21 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index 6eebe98b658..32e7f3002a1 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhclient.c,v 1.598 2018/12/28 16:01:39 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.599 2019/01/03 16:42:30 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -311,19 +311,16 @@ void
routefd_handler(struct interface_info *ifi, int routefd)
{
struct rt_msghdr *rtm;
- char *buf, *lim, *next;
+ unsigned char *buf = ifi->rbuf;
+ unsigned char *lim, *next;
ssize_t n;
- buf = calloc(1, 2048);
- if (buf == NULL)
- fatal("rtm buf");
-
do {
- n = read(routefd, buf, 2048);
+ n = read(routefd, buf, RT_BUF_SIZE);
} while (n == -1 && errno == EINTR);
if (n == -1) {
log_warn("%s: routing socket", log_procname);
- goto done;
+ return;
}
if (n == 0)
fatalx("%s: routing socket closed", log_procname);
@@ -340,10 +337,6 @@ routefd_handler(struct interface_info *ifi, int routefd)
rtm_dispatch(ifi, rtm);
}
-
-done:
- free(buf);
- return;
}
void
@@ -465,7 +458,9 @@ main(int argc, char *argv[])
struct interface_info *ifi;
struct passwd *pw;
char *ignore_list = NULL;
+ unsigned char *newp;
ssize_t tailn;
+ size_t newsize;
int fd, socket_fd[2];
int rtfilter, ioctlfd, routefd, tailfd;
int ch;
@@ -657,6 +652,12 @@ main(int argc, char *argv[])
sizeof(ifi->rdomain)) == -1)
fatal("setsockopt(ROUTE_TABLEFILTER)");
+ /* Allocate a rbuf large enough to handle routing socket messages. */
+ ifi->rbuf_max = RT_BUF_SIZE;
+ ifi->rbuf = malloc(ifi->rbuf_max);
+ if (ifi->rbuf == NULL)
+ fatal("rbuf");
+
take_charge(ifi, routefd);
if ((fd = open(path_lease_db,
@@ -679,15 +680,16 @@ main(int argc, char *argv[])
fatal("fopen(%s)", path_option_db);
}
- /* Register the interface. */
+ /* Create the udp and bpf sockets, growing rbuf if needed. */
ifi->udpfd = get_udp_sock(ifi->rdomain);
ifi->bpffd = get_bpf_sock(ifi->name);
- ifi->rbuf_max = configure_bpf_sock(ifi->bpffd);
- ifi->rbuf = malloc(ifi->rbuf_max);
- if (ifi->rbuf == NULL)
- fatal("bpf input buffer");
- ifi->rbuf_offset = 0;
- ifi->rbuf_len = 0;
+ newsize = configure_bpf_sock(ifi->bpffd);
+ if (newsize > ifi->rbuf_max) {
+ if ((newp = realloc(ifi->rbuf, newsize)) == NULL)
+ fatal("rbuf");
+ ifi->rbuf = newp;
+ ifi->rbuf_max = newsize;
+ }
if (chroot(_PATH_VAREMPTY) == -1)
fatal("chroot(%s)", _PATH_VAREMPTY);
diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h
index c41af58886a..c2874e89c10 100644
--- a/sbin/dhclient/dhcpd.h
+++ b/sbin/dhclient/dhcpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhcpd.h,v 1.261 2018/12/27 17:33:15 krw Exp $ */
+/* $OpenBSD: dhcpd.h,v 1.262 2019/01/03 16:42:30 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -43,6 +43,7 @@
#define REMOTE_PORT 67
#define INTERNALSIG SIG_ATOMIC_MAX
#define DB_TIMEFMT "%w %Y/%m/%d %T UTC"
+#define RT_BUF_SIZE 2048
struct option_data {
unsigned int len;
diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c
index b0a16406c08..80c081f0c71 100644
--- a/sbin/dhclient/dispatch.c
+++ b/sbin/dhclient/dispatch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dispatch.c,v 1.155 2018/12/27 17:19:56 krw Exp $ */
+/* $OpenBSD: dispatch.c,v 1.156 2019/01/03 16:42:30 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -168,6 +168,7 @@ dispatch(struct interface_info *ifi, int routefd)
continue;
if ((fds[0].revents & POLLIN) != 0) {
+ ifi->rbuf_offset = ifi->rbuf_len = 0;
do {
bpffd_handler(ifi);
} while (quit == 0 && ifi->rbuf_offset < ifi->rbuf_len);