From 489d9ca0be9e824f33c291504b6d6e2dab34d367 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 22 Jul 2008 14:01:01 -0700 Subject: [PATCH] dhcp: Break out netdev configuration from DHCP binding. This allows us to have DHCP clients that don't actually bind or unbind a network device's IP address. This is useful for doing controller discovery without actually reconfiguring a network device. --- include/dhcp-client.h | 4 ++ lib/dhcp-client.c | 94 +++++++++++++++++++++++++--------------- secchan/secchan.c | 1 + tests/test-dhcp-client.c | 6 +++ 4 files changed, 71 insertions(+), 34 deletions(-) diff --git a/include/dhcp-client.h b/include/dhcp-client.h index 960e81ec7..2039d23a3 100644 --- a/include/dhcp-client.h +++ b/include/dhcp-client.h @@ -39,6 +39,7 @@ struct dhclient; struct dhcp_msg; +struct netdev; int dhclient_create(const char *netdev, void (*modify_request)(struct dhcp_msg *, void *aux), bool (*validate_offer)(const struct dhcp_msg *, void *aux), @@ -53,8 +54,11 @@ bool dhclient_changed(struct dhclient *); uint32_t dhclient_get_ip(const struct dhclient *); uint32_t dhclient_get_netmask(const struct dhclient *); +uint32_t dhclient_get_router(const struct dhclient *); const struct dhcp_msg *dhclient_get_config(const struct dhclient *); +int dhclient_configure_netdev(struct dhclient *); + void dhclient_run(struct dhclient *); void dhclient_wait(struct dhclient *); diff --git a/lib/dhcp-client.c b/lib/dhcp-client.c index 4735d597d..f6cc67bf8 100644 --- a/lib/dhcp-client.c +++ b/lib/dhcp-client.c @@ -164,7 +164,6 @@ dhclient_create(const char *netdev_name, bool (*validate_offer)(const struct dhcp_msg *, void *aux), void *aux, struct dhclient **cli_) { - struct in_addr any = { INADDR_ANY }; struct dhclient *cli; struct netdev *netdev; int error; @@ -179,9 +178,9 @@ dhclient_create(const char *netdev_name, return error; } - error = netdev_set_in4(netdev, any, any); + error = netdev_turn_flags_on(netdev, NETDEV_UP, false); if (error) { - VLOG_ERR("could not remove IPv4 address from %s network device: %s", + VLOG_ERR("could not bring %s device up: %s", netdev_name, strerror(error)); netdev_close(netdev); return error; @@ -229,7 +228,6 @@ dhclient_release(struct dhclient *cli) msg.ciaddr = cli->ipaddr; do_send_msg(cli, &msg); dhcp_msg_uninit(&msg); - cli->changed = true; } state_transition(cli, S_RELEASED); cli->min_timeout = UINT_MAX; @@ -320,6 +318,14 @@ dhclient_get_netmask(const struct dhclient *cli) return dhclient_is_bound(cli) ? cli->netmask : 0; } +/* If 'cli' is bound to an IP address and 'cli' has a default gateway, returns + * that default gateway; otherwise, returns 0. */ +uint32_t +dhclient_get_router(const struct dhclient *cli) +{ + return dhclient_is_bound(cli) ? cli->router : 0; +} + /* If 'cli' is bound to an IP address, returns the DHCP message that was * received to obtain that IP address (so that the caller can obtain additional * options from it). Otherwise, returns a null pointer. */ @@ -328,6 +334,48 @@ dhclient_get_config(const struct dhclient *cli) { return dhclient_is_bound(cli) ? cli->binding : NULL; } + +/* Configures the network device backing 'cli' to the network address and other + * parameters obtained via DHCP. If no address is bound on 'cli', removes any + * configured address from 'cli'. + * + * To use a dhclient as a regular DHCP client that binds and unbinds from IP + * addresses in the usual fashion, call this function after dhclient_run() if + * anything has changed, like so: + * + * dhclient_run(cli); + * if (dhclient_changed(cli)) { + * dhclient_configure_netdev(cli); + * } + * + */ +int +dhclient_configure_netdev(struct dhclient *cli) +{ + struct in_addr addr = { dhclient_get_ip(cli) }; + struct in_addr mask = { dhclient_get_netmask(cli) }; + struct in_addr router = { dhclient_get_router(cli) }; + int error; + + error = netdev_set_in4(cli->netdev, addr, mask); + if (error) { + VLOG_ERR("could not set %s address "IP_FMT"/"IP_FMT": %s", + netdev_get_name(cli->netdev), + IP_ARGS(&addr.s_addr), IP_ARGS(&mask.s_addr), + strerror(error)); + } + + if (!error && router.s_addr) { + error = netdev_add_router(cli->netdev, router); + if (error) { + VLOG_ERR("failed to add default route to "IP_FMT" on %s: %s", + IP_ARGS(&router), netdev_get_name(cli->netdev), + strerror(error)); + } + } + + return error; +} /* DHCP protocol. */ @@ -598,45 +646,23 @@ state_transition(struct dhclient *cli, enum dhclient_state state) cli->retransmit = cli->delay = 0; am_bound = dhclient_is_bound(cli); if (was_bound != am_bound) { - struct in_addr addr, mask; - int error; - cli->changed = true; if (am_bound) { - VLOG_WARN("%s: binding to "IP_FMT"/"IP_FMT, + assert(cli->binding != NULL); + VLOG_WARN("%s: obtained address "IP_FMT", netmask "IP_FMT, netdev_get_name(cli->netdev), IP_ARGS(&cli->ipaddr), IP_ARGS(&cli->netmask)); - addr.s_addr = cli->ipaddr; - mask.s_addr = cli->netmask; - } else { - VLOG_WARN("%s: unbinding IPv4 network address", - netdev_get_name(cli->netdev)); - addr.s_addr = mask.s_addr = INADDR_ANY; - } - error = netdev_set_in4(cli->netdev, addr, mask); - if (error) { - VLOG_ERR("could not set %s address "IP_FMT"/"IP_FMT": %s", - netdev_get_name(cli->netdev), - IP_ARGS(&addr.s_addr), IP_ARGS(&mask.s_addr), - strerror(error)); - } - if (am_bound && !error && cli->router) { - struct in_addr router = { cli->router }; - error = netdev_add_router(cli->netdev, router); - VLOG_WARN("%s: configuring router "IP_FMT, - netdev_get_name(cli->netdev), IP_ARGS(cli->router)); - if (error) { - VLOG_ERR("failed to add default route to "IP_FMT" on %s: %s", - IP_ARGS(&router), netdev_get_name(cli->netdev), - strerror(error)); + if (cli->router) { + VLOG_WARN("%s: obtained default gateway "IP_FMT, + netdev_get_name(cli->netdev), IP_ARGS(&cli->router)); } - } - if (am_bound) { - assert(cli->binding != NULL); } else { dhcp_msg_uninit(cli->binding); free(cli->binding); cli->binding = NULL; + + VLOG_WARN("%s: network address unbound", + netdev_get_name(cli->netdev)); } } if (cli->state & (S_SELECTING | S_REQUESTING | S_REBOOTING)) { diff --git a/secchan/secchan.c b/secchan/secchan.c index 3c96312f5..e99499d59 100644 --- a/secchan/secchan.c +++ b/secchan/secchan.c @@ -297,6 +297,7 @@ main(int argc, char *argv[]) } dhclient_run(dhcp); if (dhclient_changed(dhcp)) { + dhclient_configure_netdev(dhcp); free(controller_name); if (dhclient_is_bound(dhcp)) { controller_name = dhcp_msg_get_string( diff --git a/tests/test-dhcp-client.c b/tests/test-dhcp-client.c index 3ed6eb6b6..4bd4913ea 100644 --- a/tests/test-dhcp-client.c +++ b/tests/test-dhcp-client.c @@ -86,6 +86,9 @@ main(int argc, char *argv[]) for (;;) { fatal_signal_block(); dhclient_run(cli); + if (dhclient_changed(cli)) { + dhclient_configure_netdev(cli); + } fatal_signal_unblock(); dhclient_wait(cli); poll_block(); @@ -97,6 +100,9 @@ release(void *cli_) { struct dhclient *cli = cli_; dhclient_release(cli); + if (dhclient_changed(cli)) { + dhclient_configure_netdev(cli); + } } static void -- 2.43.0