diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2013-06-09 15:06:35 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2013-06-09 15:06:35 +0000 |
commit | 46b77f2a4f6887df3dc12d49e3073399d57e38b0 (patch) | |
tree | a6b6a80cd2d03b257b52e5e95307d10757486a28 /sbin | |
parent | fadc958e18cdee76325ef4f800a220ce6f003196 (diff) |
Re-apply route adding code refactoring -- it wasn't the problem that
broke populating the egress group.
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/dhclient/dhclient.c | 43 | ||||
-rw-r--r-- | sbin/dhclient/dhcpd.h | 4 | ||||
-rw-r--r-- | sbin/dhclient/kroute.c | 26 | ||||
-rw-r--r-- | sbin/dhclient/privsep.c | 10 | ||||
-rw-r--r-- | sbin/dhclient/privsep.h | 14 |
5 files changed, 70 insertions, 27 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index de84208a406..d7f5a86c7cf 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.250 2013/06/09 00:30:06 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.251 2013/06/09 15:06:34 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -108,6 +108,8 @@ struct client_lease *clone_lease(struct client_lease *); void socket_nonblockmode(int); void apply_ignore_list(char *); +void add_default_route(int, struct in_addr, struct in_addr); + #define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) @@ -2263,3 +2265,42 @@ priv_write_file(struct imsg_write_file *imsg) close(fd); } + +/* + * add_default_route is the equivalent of + * + * route -q $rdomain add default -iface $router + * + * or + * + * route -q $rdomain add default $router + * + * depending on the contents of the gateway parameter. + */ +void +add_default_route(int rdomain, struct in_addr addr, struct in_addr gateway) +{ + struct imsg_add_route imsg; + int rslt; + + memset(&imsg, 0, sizeof(imsg)); + + imsg.rdomain = rdomain; + imsg.dest = addr; + imsg.addrs = RTA_DST | RTA_NETMASK; + + /* + * Set gateway address if and only if non-zero addr supplied. A + * gateway address of 0 implies '-iface'. + */ + if (bcmp(&gateway, &addr, sizeof(addr)) != 0) { + imsg.gateway = gateway; + imsg.addrs |= RTA_GATEWAY; + } + + rslt = imsg_compose(unpriv_ibuf, IMSG_ADD_ROUTE, 0, 0, -1, &imsg, + sizeof(imsg)); + + if (rslt == -1) + warning("add_route: imsg_compose: %s", strerror(errno)); +} diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index 7e6da3e3661..26b15b88a9b 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.119 2013/06/09 00:30:06 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.120 2013/06/09 15:06:34 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -343,4 +343,4 @@ void add_address(char *, int, struct in_addr, struct in_addr); void flush_routes(char *, int); -void add_default_route(int, struct in_addr, struct in_addr); +void add_route(int, struct in_addr, struct in_addr, struct in_addr, int); diff --git a/sbin/dhclient/kroute.c b/sbin/dhclient/kroute.c index 708aa533862..1abef0d79ff 100644 --- a/sbin/dhclient/kroute.c +++ b/sbin/dhclient/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.50 2013/06/09 00:30:06 krw Exp $ */ +/* $OpenBSD: kroute.c,v 1.51 2013/06/09 15:06:34 krw Exp $ */ /* * Copyright 2012 Kenneth R Westerback <krw@openbsd.org> @@ -183,26 +183,29 @@ priv_flush_routes(struct imsg_flush_routes *imsg) * depending on the contents of the gateway parameter. */ void -add_default_route(int rdomain, struct in_addr addr, struct in_addr gateway) +add_route(int rdomain, struct in_addr dest, struct in_addr netmask, + struct in_addr gateway, int addrs) { - struct imsg_add_default_route imsg; - int rslt; + struct imsg_add_route imsg; + int rslt; memset(&imsg, 0, sizeof(imsg)); imsg.rdomain = rdomain; - imsg.addr = addr; + imsg.dest = dest; imsg.gateway = gateway; + imsg.netmask = netmask; + imsg.addrs = addrs; - rslt = imsg_compose(unpriv_ibuf, IMSG_ADD_DEFAULT_ROUTE, 0, 0, -1, + rslt = imsg_compose(unpriv_ibuf, IMSG_ADD_ROUTE, 0, 0, -1, &imsg, sizeof(imsg)); if (rslt == -1) - warning("add_default_route: imsg_compose: %s", strerror(errno)); + warning("add_route: imsg_compose: %s", strerror(errno)); } void -priv_add_default_route(struct imsg_add_default_route *imsg) +priv_add_route(struct imsg_add_route *imsg) { struct rt_msghdr rtm; struct sockaddr_in dest, gateway, mask; @@ -210,10 +213,6 @@ priv_add_default_route(struct imsg_add_default_route *imsg) struct iovec iov[5]; int s, i, iovcnt = 0; - /* - * Add a default route via the specified address. - */ - if ((s = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) error("Routing Socket open failed: %s", strerror(errno)); @@ -249,7 +248,7 @@ priv_add_default_route(struct imsg_add_default_route *imsg) */ memset(&gateway, 0, sizeof(gateway)); - if (bcmp(&imsg->gateway, &imsg->addr, sizeof(imsg->addr)) != 0) { + if ((imsg->addrs & RTA_GATEWAY) != 0) { gateway.sin_len = sizeof(gateway); gateway.sin_family = AF_INET; gateway.sin_addr.s_addr = imsg->gateway.s_addr; @@ -267,6 +266,7 @@ priv_add_default_route(struct imsg_add_default_route *imsg) mask.sin_len = sizeof(mask); mask.sin_family = AF_INET; + mask.sin_addr.s_addr = imsg->netmask.s_addr; rtm.rtm_addrs |= RTA_NETMASK; rtm.rtm_msglen += sizeof(mask); diff --git a/sbin/dhclient/privsep.c b/sbin/dhclient/privsep.c index 38af8edf99a..d3f1391a0e5 100644 --- a/sbin/dhclient/privsep.c +++ b/sbin/dhclient/privsep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.c,v 1.33 2013/06/09 00:30:06 krw Exp $ */ +/* $OpenBSD: privsep.c,v 1.34 2013/06/09 15:06:34 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -63,12 +63,12 @@ dispatch_imsg(struct imsgbuf *ibuf) priv_flush_routes(imsg.data); break; - case IMSG_ADD_DEFAULT_ROUTE: + case IMSG_ADD_ROUTE: if (imsg.hdr.len != IMSG_HEADER_SIZE + - sizeof(struct imsg_add_default_route)) - warning("bad IMSG_ADD_DEFAULT_ROUTE"); + sizeof(struct imsg_add_route)) + warning("bad IMSG_ADD_ROUTE"); else - priv_add_default_route(imsg.data); + priv_add_route(imsg.data); break; case IMSG_HUP: diff --git a/sbin/dhclient/privsep.h b/sbin/dhclient/privsep.h index 0e08cbd7e46..c436fd09d34 100644 --- a/sbin/dhclient/privsep.h +++ b/sbin/dhclient/privsep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.h,v 1.20 2013/06/09 00:30:06 krw Exp $ */ +/* $OpenBSD: privsep.h,v 1.21 2013/06/09 15:06:34 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -25,7 +25,7 @@ enum imsg_code { IMSG_DELETE_ADDRESS, IMSG_ADD_ADDRESS, IMSG_FLUSH_ROUTES, - IMSG_ADD_DEFAULT_ROUTE, + IMSG_ADD_ROUTE, IMSG_HUP, IMSG_WRITE_FILE }; @@ -49,10 +49,12 @@ struct imsg_flush_routes { int zapzombies; }; -struct imsg_add_default_route { - int rdomain; - struct in_addr addr; +struct imsg_add_route { + struct in_addr dest; + struct in_addr netmask; struct in_addr gateway; + int rdomain; + int addrs; }; struct imsg_hup { @@ -76,6 +78,6 @@ void dispatch_imsg(struct imsgbuf *); void priv_delete_address(struct imsg_delete_address *); void priv_add_address(struct imsg_add_address *); void priv_flush_routes(struct imsg_flush_routes *); -void priv_add_default_route(struct imsg_add_default_route *); +void priv_add_route(struct imsg_add_route *); void priv_cleanup(struct imsg_hup *); void priv_write_file(struct imsg_write_file *); |