summaryrefslogtreecommitdiff
path: root/sbin/dhclient
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2017-07-10 14:11:48 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2017-07-10 14:11:48 +0000
commit84cdb86a3e1f5444033bac17935d5452e62f5c4d (patch)
tree067b70245bca4105ed70110bb3c02fd45bbe4d1d /sbin/dhclient
parent66f1179c18c437005dbf9d2bc5c8a1a7ac81365b (diff)
Use ioctlfd/routefd sockets rather than rolling one's own
for resolving 'egress' and adding a route. Move creation of main()'s ioctlfd up so it can be used for the 'egress' resolution.
Diffstat (limited to 'sbin/dhclient')
-rw-r--r--sbin/dhclient/dhclient.c21
-rw-r--r--sbin/dhclient/kroute.c13
-rw-r--r--sbin/dhclient/privsep.c4
-rw-r--r--sbin/dhclient/privsep.h4
4 files changed, 16 insertions, 26 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index be5559caac2..65cd037a677 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhclient.c,v 1.464 2017/07/10 00:47:47 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.465 2017/07/10 14:11:47 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -120,7 +120,7 @@ int res_hnok_list(const char *dn);
int addressinuse(char *, struct in_addr, char *);
void fork_privchld(struct interface_info *, int, int);
-void get_ifname(struct interface_info *, char *);
+void get_ifname(struct interface_info *, int, char *);
struct client_lease *apply_defaults(struct client_lease *);
struct client_lease *clone_lease(struct client_lease *);
@@ -506,7 +506,9 @@ main(int argc, char *argv[])
ifi = calloc(1, sizeof(struct interface_info));
if (ifi == NULL)
fatalx("ifi calloc");
- get_ifname(ifi, argv[0]);
+ if ((ioctlfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ fatal("Can't create socket to do ioctl");
+ get_ifname(ifi, ioctlfd, argv[0]);
ifi->index = if_nametoindex(ifi->name);
if (ifi->index == 0)
fatalx("%s: no such interface", ifi->name);
@@ -515,8 +517,6 @@ main(int argc, char *argv[])
tzset();
/* Get the ssid if present. */
- if ((ioctlfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- fatalx("Can't create socket to do ioctl");
memset(&ifr, 0, sizeof(ifr));
memset(&nwid, 0, sizeof(nwid));
ifr.ifr_data = (caddr_t)&nwid;
@@ -2119,20 +2119,16 @@ fork_privchld(struct interface_info *ifi, int fd, int fd2)
}
void
-get_ifname(struct interface_info *ifi, char *arg)
+get_ifname(struct interface_info *ifi, int ioctlfd, char *arg)
{
struct ifgroupreq ifgr;
struct ifg_req *ifg;
unsigned int len;
- int s;
if (strcmp(arg, "egress") == 0) {
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s == -1)
- fatalx("socket error");
memset(&ifgr, 0, sizeof(ifgr));
strlcpy(ifgr.ifgr_name, "egress", sizeof(ifgr.ifgr_name));
- if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
+ if (ioctl(ioctlfd, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
if (errno == ENOENT)
fatalx("no interface in group egress found");
fatal("ioctl SIOCGIFGMEMB");
@@ -2140,7 +2136,7 @@ get_ifname(struct interface_info *ifi, char *arg)
len = ifgr.ifgr_len;
if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
fatalx("get_ifname");
- if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
+ if (ioctl(ioctlfd, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
fatal("ioctl SIOCGIFGMEMB");
arg = NULL;
@@ -2156,7 +2152,6 @@ get_ifname(struct interface_info *ifi, char *arg)
fatal("Interface name too long");
free(ifgr.ifgr_groups);
- close(s);
} else if (strlcpy(ifi->name, arg, IFNAMSIZ) >= IFNAMSIZ)
fatalx("Interface name too long");
}
diff --git a/sbin/dhclient/kroute.c b/sbin/dhclient/kroute.c
index b065e98235b..5f8f750a25f 100644
--- a/sbin/dhclient/kroute.c
+++ b/sbin/dhclient/kroute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kroute.c,v 1.106 2017/07/10 00:47:47 krw Exp $ */
+/* $OpenBSD: kroute.c,v 1.107 2017/07/10 14:11:47 krw Exp $ */
/*
* Copyright 2012 Kenneth R Westerback <krw@openbsd.org>
@@ -425,7 +425,7 @@ add_route(struct in_addr dest, struct in_addr netmask,
}
void
-priv_add_route(int rdomain, struct imsg_add_route *imsg)
+priv_add_route(int rdomain, int routefd, struct imsg_add_route *imsg)
{
char destbuf[INET_ADDRSTRLEN];
char gatewaybuf[INET_ADDRSTRLEN];
@@ -435,10 +435,7 @@ priv_add_route(int rdomain, struct imsg_add_route *imsg)
struct rt_msghdr rtm;
struct sockaddr_in dest, gateway, mask, ifa;
struct sockaddr_rtlabel label;
- int s, i, iovcnt = 0;
-
- if ((s = socket(AF_ROUTE, SOCK_RAW, 0)) == -1)
- fatal("Routing Socket open failed");
+ int i, iovcnt = 0;
memset(destbuf, 0, sizeof(destbuf));
memset(maskbuf, 0, sizeof(maskbuf));
@@ -527,7 +524,7 @@ priv_add_route(int rdomain, struct imsg_add_route *imsg)
/* Check for EEXIST since other dhclient may not be done. */
for (i = 0; i < 5; i++) {
- if (writev(s, iov, iovcnt) != -1)
+ if (writev(routefd, iov, iovcnt) != -1)
break;
if (i == 4)
log_warn("failed to add route (%s/%s via %s/%s)",
@@ -535,8 +532,6 @@ priv_add_route(int rdomain, struct imsg_add_route *imsg)
else if (errno == EEXIST || errno == ENETUNREACH)
sleep(1);
}
-
- close(s);
}
/*
diff --git a/sbin/dhclient/privsep.c b/sbin/dhclient/privsep.c
index 4bb01951299..d303e17728c 100644
--- a/sbin/dhclient/privsep.c
+++ b/sbin/dhclient/privsep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: privsep.c,v 1.55 2017/07/07 15:39:30 krw Exp $ */
+/* $OpenBSD: privsep.c,v 1.56 2017/07/10 14:11:47 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -78,7 +78,7 @@ dispatch_imsg(char *name, int rdomain, int ioctlfd, int routefd,
sizeof(struct imsg_add_route))
log_warnx("bad IMSG_ADD_ROUTE");
else
- priv_add_route(rdomain, imsg.data);
+ priv_add_route(rdomain, routefd, imsg.data);
break;
case IMSG_SET_INTERFACE_MTU:
diff --git a/sbin/dhclient/privsep.h b/sbin/dhclient/privsep.h
index d2873c6bad6..15683ae7dc8 100644
--- a/sbin/dhclient/privsep.h
+++ b/sbin/dhclient/privsep.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: privsep.h,v 1.43 2017/07/08 00:36:10 krw Exp $ */
+/* $OpenBSD: privsep.h,v 1.44 2017/07/10 14:11:47 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -55,7 +55,7 @@ void add_direct_route(struct in_addr, struct in_addr, struct in_addr);
void add_default_route(struct in_addr, struct in_addr);
void add_static_routes(struct option_data *, struct in_addr);
void add_classless_static_routes(struct option_data *, struct in_addr);
-void priv_add_route(int, struct imsg_add_route *);
+void priv_add_route(int, int, struct imsg_add_route *);
void priv_flush_routes(char *, int, int);
char *resolv_conf_contents(char *, struct option_data *,