summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2017-08-05 12:35:18 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2017-08-05 12:35:18 +0000
commit75f64a864023d5cdce25bd365f8d2f5205274d93 (patch)
tree68137ff30dfe231ce8ac3ef7df61ff7cbce6a5f9
parentf1b31650f898906824571ad0fadc834bb17a8a00 (diff)
Bring route creation into line with what route(8) does. In
particular stop providing RFA_IFA, and instead supply the relevant interface index in the RTM_ADD message. Various code simplifications as a result.
-rw-r--r--sbin/dhclient/kroute.c143
-rw-r--r--sbin/dhclient/privsep.c5
-rw-r--r--sbin/dhclient/privsep.h6
3 files changed, 59 insertions, 95 deletions
diff --git a/sbin/dhclient/kroute.c b/sbin/dhclient/kroute.c
index 058253153e5..43287828687 100644
--- a/sbin/dhclient/kroute.c
+++ b/sbin/dhclient/kroute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kroute.c,v 1.129 2017/08/05 12:08:33 krw Exp $ */
+/* $OpenBSD: kroute.c,v 1.130 2017/08/05 12:35:17 krw Exp $ */
/*
* Copyright 2012 Kenneth R Westerback <krw@openbsd.org>
@@ -74,8 +74,7 @@ int create_route_label(struct sockaddr_rtlabel *);
int check_route_label(struct sockaddr_rtlabel *);
void populate_rti_info(struct sockaddr **, struct rt_msghdr *);
void delete_route(int, struct rt_msghdr *);
-void add_route(struct in_addr, struct in_addr, struct in_addr,
- struct in_addr, int, int);
+void add_route(struct in_addr, struct in_addr, struct in_addr, int);
void flush_routes(void);
int delete_addresses(char *, struct in_addr, struct in_addr);
@@ -316,9 +315,8 @@ set_routes(struct in_addr addr, struct in_addr addrmask, uint8_t *rtstatic,
* route add -net $dest -netmask $netmask -cloning
* -iface $addr
*/
- add_route(dest, netmask, addr, any,
- RTA_DST | RTA_NETMASK | RTA_GATEWAY,
- RTF_CLONING | RTF_STATIC);
+ add_route(dest, netmask, addr,
+ RTF_STATIC | RTF_CLONING);
} else if (netmask.s_addr == INADDR_ANY) {
/*
* DEFAULT ROUTE
@@ -335,9 +333,8 @@ set_routes(struct in_addr addr, struct in_addr addrmask, uint8_t *rtstatic,
* route add -net $gateway -netmask $addrmask
* -cloning -iface $addr
*/
- add_route(gateway, addrmask, addr, any,
- RTA_DST | RTA_NETMASK | RTA_GATEWAY,
- RTF_CLONING | RTF_STATIC);
+ add_route(gateway, addrmask, addr,
+ RTF_STATIC | RTF_CLONING);
}
if (memcmp(&gateway, &addr, sizeof(addr)) == 0) {
@@ -346,18 +343,15 @@ set_routes(struct in_addr addr, struct in_addr addrmask, uint8_t *rtstatic,
*
* route add default -iface $addr
*/
- add_route(any, any, gateway, addr,
- RTA_DST | RTA_NETMASK | RTA_GATEWAY | RTA_IFA,
- RTF_STATIC);
+ add_route(any, any, gateway, RTF_STATIC);
} else {
/*
* DEFAULT ROUTE IS VIA GATEWAY
*
* route add default $gateway
*/
- add_route(any, any, gateway, addr,
- RTA_DST | RTA_NETMASK | RTA_GATEWAY | RTA_IFA,
- RTF_GATEWAY | RTF_STATIC);
+ add_route(any, any, gateway,
+ RTF_STATIC | RTF_GATEWAY);
}
} else {
/*
@@ -365,9 +359,8 @@ set_routes(struct in_addr addr, struct in_addr addrmask, uint8_t *rtstatic,
*
* route add -net $dest -netmask $netmask $gateway
*/
- add_route(dest, netmask, gateway, addr,
- RTA_DST | RTA_NETMASK | RTA_GATEWAY | RTA_IFA,
- RTF_GATEWAY | RTF_STATIC);
+ add_route(dest, netmask, gateway,
+ RTF_STATIC | RTF_GATEWAY);
}
}
}
@@ -376,8 +369,8 @@ set_routes(struct in_addr addr, struct in_addr addrmask, uint8_t *rtstatic,
* [priv_]add_route() add a single route to the routing table.
*/
void
-add_route(struct in_addr dest, struct in_addr netmask,
- struct in_addr gateway, struct in_addr ifa, int addrs, int flags)
+add_route(struct in_addr dest, struct in_addr netmask, struct in_addr gateway,
+ int flags)
{
struct imsg_add_route imsg;
int rslt;
@@ -385,8 +378,6 @@ add_route(struct in_addr dest, struct in_addr netmask,
imsg.dest = dest;
imsg.gateway = gateway;
imsg.netmask = netmask;
- imsg.ifa = ifa;
- imsg.addrs = addrs;
imsg.flags = flags;
rslt = imsg_compose(unpriv_ibuf, IMSG_ADD_ROUTE, 0, 0, -1,
@@ -398,22 +389,20 @@ add_route(struct in_addr dest, struct in_addr netmask,
}
void
-priv_add_route(int rdomain, int routefd, struct imsg_add_route *imsg)
+priv_add_route(char *name, int rdomain, int routefd,
+ struct imsg_add_route *imsg)
{
char destbuf[INET_ADDRSTRLEN];
- char gatewaybuf[INET_ADDRSTRLEN];
char maskbuf[INET_ADDRSTRLEN];
- char ifabuf[INET_ADDRSTRLEN];
- struct iovec iov[6];
+ struct iovec iov[5];
struct rt_msghdr rtm;
- struct sockaddr_in dest, gateway, mask, ifa;
+ struct sockaddr_in dest, gateway, mask;
struct sockaddr_rtlabel label;
- int i, iovcnt = 0;
+ int i, index, iovcnt = 0;
- memset(destbuf, 0, sizeof(destbuf));
- memset(maskbuf, 0, sizeof(maskbuf));
- memset(gatewaybuf, 0, sizeof(gatewaybuf));
- memset(ifabuf, 0, sizeof(ifabuf));
+ index = if_nametoindex(name);
+ if (index == 0)
+ return;
/* Build RTM header */
@@ -421,71 +410,45 @@ priv_add_route(int rdomain, int routefd, struct imsg_add_route *imsg)
rtm.rtm_version = RTM_VERSION;
rtm.rtm_type = RTM_ADD;
+ rtm.rtm_index = index;
rtm.rtm_tableid = rdomain;
rtm.rtm_priority = RTP_NONE;
- rtm.rtm_msglen = sizeof(rtm);
- rtm.rtm_addrs = imsg->addrs;
+ rtm.rtm_addrs = RTA_DST | RTA_NETMASK | RTA_GATEWAY;
rtm.rtm_flags = imsg->flags;
+ rtm.rtm_msglen = sizeof(rtm);
iov[iovcnt].iov_base = &rtm;
iov[iovcnt++].iov_len = sizeof(rtm);
- if (imsg->addrs & RTA_DST) {
- strlcpy(destbuf, inet_ntoa(imsg->dest), sizeof(destbuf));
- memset(&dest, 0, sizeof(dest));
+ /* Add the destination address. */
+ memset(&dest, 0, sizeof(dest));
+ dest.sin_len = sizeof(dest);
+ dest.sin_family = AF_INET;
+ dest.sin_addr.s_addr = imsg->dest.s_addr;
- dest.sin_len = sizeof(dest);
- dest.sin_family = AF_INET;
- dest.sin_addr.s_addr = imsg->dest.s_addr;
+ rtm.rtm_msglen += sizeof(dest);
+ iov[iovcnt].iov_base = &dest;
+ iov[iovcnt++].iov_len = sizeof(dest);
- rtm.rtm_msglen += sizeof(dest);
-
- iov[iovcnt].iov_base = &dest;
- iov[iovcnt++].iov_len = sizeof(dest);
- }
+ /* Add the gateways address. */
+ memset(&gateway, 0, sizeof(gateway));
+ gateway.sin_len = sizeof(gateway);
+ gateway.sin_family = AF_INET;
+ gateway.sin_addr.s_addr = imsg->gateway.s_addr;
- if (imsg->addrs & RTA_GATEWAY) {
- strlcpy(gatewaybuf, inet_ntoa(imsg->gateway),
- sizeof(gatewaybuf));
- memset(&gateway, 0, sizeof(gateway));
-
- gateway.sin_len = sizeof(gateway);
- gateway.sin_family = AF_INET;
- gateway.sin_addr.s_addr = imsg->gateway.s_addr;
-
- rtm.rtm_msglen += sizeof(gateway);
-
- iov[iovcnt].iov_base = &gateway;
- iov[iovcnt++].iov_len = sizeof(gateway);
- }
+ rtm.rtm_msglen += sizeof(gateway);
+ iov[iovcnt].iov_base = &gateway;
+ iov[iovcnt++].iov_len = sizeof(gateway);
- if (imsg->addrs & RTA_NETMASK) {
- strlcpy(maskbuf, inet_ntoa(imsg->netmask), sizeof(maskbuf));
- memset(&mask, 0, sizeof(mask));
+ /* Add the network mask. */
+ memset(&mask, 0, sizeof(mask));
+ mask.sin_len = sizeof(mask);
+ mask.sin_family = AF_INET;
+ mask.sin_addr.s_addr = imsg->netmask.s_addr;
- mask.sin_len = sizeof(mask);
- mask.sin_family = AF_INET;
- mask.sin_addr.s_addr = imsg->netmask.s_addr;
-
- rtm.rtm_msglen += sizeof(mask);
-
- iov[iovcnt].iov_base = &mask;
- iov[iovcnt++].iov_len = sizeof(mask);
- }
-
- if (imsg->addrs & RTA_IFA) {
- strlcpy(ifabuf, inet_ntoa(imsg->ifa), sizeof(ifabuf));
- memset(&ifa, 0, sizeof(ifa));
-
- ifa.sin_len = sizeof(ifa);
- ifa.sin_family = AF_INET;
- ifa.sin_addr.s_addr = imsg->ifa.s_addr;
-
- rtm.rtm_msglen += sizeof(ifa);
-
- iov[iovcnt].iov_base = &ifa;
- iov[iovcnt++].iov_len = sizeof(ifa);
- }
+ rtm.rtm_msglen += sizeof(mask);
+ iov[iovcnt].iov_base = &mask;
+ iov[iovcnt++].iov_len = sizeof(mask);
/* Add our label so we can identify the route as our creation. */
if (create_route_label(&label) == 0) {
@@ -499,10 +462,12 @@ priv_add_route(int rdomain, int routefd, struct imsg_add_route *imsg)
for (i = 0; i < 5; i++) {
if (writev(routefd, iov, iovcnt) != -1)
break;
- if (i == 4)
- log_warn("failed to add route (%s/%s via %s/%s)",
- destbuf, maskbuf, gatewaybuf, ifabuf);
- else if (errno == EEXIST || errno == ENETUNREACH)
+ if (i == 4) {
+ strlcpy(destbuf, inet_ntoa(imsg->dest), sizeof(destbuf));
+ strlcpy(maskbuf, inet_ntoa(imsg->netmask), sizeof(maskbuf));
+ log_warn("failed to add route (%s/%s via %s)",
+ destbuf, maskbuf, inet_ntoa(imsg->gateway));
+ } else if (errno == EEXIST || errno == ENETUNREACH)
sleep(1);
}
}
diff --git a/sbin/dhclient/privsep.c b/sbin/dhclient/privsep.c
index 0a5bac9fd97..41b3eb7437d 100644
--- a/sbin/dhclient/privsep.c
+++ b/sbin/dhclient/privsep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: privsep.c,v 1.59 2017/07/22 14:56:27 krw Exp $ */
+/* $OpenBSD: privsep.c,v 1.60 2017/08/05 12:35:17 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -78,7 +78,8 @@ 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, routefd, imsg.data);
+ priv_add_route(name, rdomain, routefd,
+ imsg.data);
break;
case IMSG_SET_MTU:
diff --git a/sbin/dhclient/privsep.h b/sbin/dhclient/privsep.h
index 63d88148dd2..33855cacebe 100644
--- a/sbin/dhclient/privsep.h
+++ b/sbin/dhclient/privsep.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: privsep.h,v 1.50 2017/07/30 15:26:46 krw Exp $ */
+/* $OpenBSD: privsep.h,v 1.51 2017/08/05 12:35:17 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -40,8 +40,6 @@ struct imsg_add_route {
struct in_addr dest;
struct in_addr netmask;
struct in_addr gateway;
- struct in_addr ifa;
- int addrs;
int flags;
};
@@ -51,7 +49,7 @@ struct imsg_set_mtu {
int dispatch_imsg(char *, int, int, int, struct imsgbuf *);
-void priv_add_route(int, int, struct imsg_add_route *);
+void priv_add_route(char *, int, int, struct imsg_add_route *);
void priv_flush_routes(char *, int, int);
char *resolv_conf_contents(char *, uint8_t *, unsigned int, uint8_t *,