X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-link.c;h=452fcd43c2203fa0c6cb7f4eafeefa245eed68e1;hb=d9876a527f62e85c3c972bc5f45ad872cb4958c1;hp=36534268916c0b0370ce34cbfd5501e83e2bf2d0;hpb=512922f8b423eed093918f78461b7bf7c45592cb;p=elogind.git diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 365342689..452fcd43c 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -178,19 +178,6 @@ void link_drop(Link *link) { return; } -static int link_enter_configured(Link *link) { - assert(link); - assert(link->state == LINK_STATE_SETTING_ROUTES); - - log_info_link(link, "link configured"); - - link->state = LINK_STATE_CONFIGURED; - - link_save(link); - - return 0; -} - static void link_enter_unmanaged(Link *link) { assert(link); @@ -231,6 +218,16 @@ static int link_stop_clients(Link *link) { } } + if (link->network->dhcp_server) { + assert(link->dhcp_server); + + k = sd_dhcp_server_stop(link->dhcp_server); + if (k < 0) { + log_warning_link(link, "Could not stop DHCPv4 server: %s", strerror(-r)); + r = k; + } + } + return r; } @@ -249,6 +246,37 @@ static void link_enter_failed(Link *link) { link_save(link); } +static int link_enter_configured(Link *link) { + int r; + + assert(link); + assert(link->network); + assert(link->state == LINK_STATE_SETTING_ROUTES); + + + if (link->network->dhcp_server) { + log_debug_link(link, "offering DHCPv4 leases"); + + r = sd_dhcp_server_start(link->dhcp_server); + if (r < 0) { + log_warning_link(link, "could not start DHCPv4 server " + "instance: %s", strerror(-r)); + + link_enter_failed(link); + + return 0; + } + } + + log_info_link(link, "link configured"); + + link->state = LINK_STATE_CONFIGURED; + + link_save(link); + + return 0; +} + static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { Link *link = userdata; int r; @@ -268,7 +296,7 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { r = sd_rtnl_message_get_errno(m); if (r < 0 && r != -EEXIST) log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not set route: %s", + "MESSAGE=%-*s: could not set route: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -434,7 +462,7 @@ static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) r = sd_rtnl_message_get_errno(m); if (r < 0 && r != -ESRCH) log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not drop route: %s", + "MESSAGE=%-*s: could not drop route: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -466,7 +494,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { r = sd_rtnl_message_get_errno(m); if (r < 0 && r != -EEXIST) log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not set address: %s", + "MESSAGE=%-*s: could not set address: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -613,7 +641,7 @@ static int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userd r = sd_rtnl_message_get_errno(m); if (r < 0 && r != -ENOENT) log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not update address: %s", + "MESSAGE=%-*s: could not update address: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -640,7 +668,7 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat r = sd_rtnl_message_get_errno(m); if (r < 0 && r != -EADDRNOTAVAIL) log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not drop address: %s", + "MESSAGE=%-*s: could not drop address: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -725,8 +753,8 @@ static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { r = sd_rtnl_message_get_errno(m); if (r < 0) log_struct_link(LOG_WARNING, link, - "MESSAGE=%s: could not set MTU: %s", - link->ifname, strerror(-r), + "MESSAGE=%-*s: could not set MTU: %s", + IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, NULL); @@ -894,7 +922,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { if (r >= 0) log_struct_link(LOG_INFO, link, - "MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", + "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", IFNAMSIZ, link->ifname, ADDRESS_FMT_VAL(address), @@ -909,7 +937,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { NULL); else log_struct_link(LOG_INFO, link, - "MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u", + "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u", IFNAMSIZ, link->ifname, ADDRESS_FMT_VAL(address), @@ -1136,7 +1164,7 @@ static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) { return r; log_struct_link(LOG_INFO, link, - "MESSAGE=%*s: IPv4 link-local address %u.%u.%u.%u", + "MESSAGE=%-*s: IPv4 link-local address %u.%u.%u.%u", IFNAMSIZ, link->ifname, ADDRESS_FMT_VAL(address), @@ -1356,7 +1384,7 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { /* we warn but don't fail the link, as it may be brought up later */ log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not bring up interface: %s", + "MESSAGE=%-*s: could not bring up interface: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -1443,7 +1471,7 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { r = sd_rtnl_message_get_errno(m); if (r < 0) { log_struct_link(LOG_ERR, link, - "MESSAGE=%*s: could not enslave: %s", + "MESSAGE=%-*s: could not enslave: %s", IFNAMSIZ, link->ifname, strerror(-r), "ERRNO=%d", -r, @@ -1485,7 +1513,7 @@ static int link_enter_enslave(Link *link) { if (link->network->bond) { log_struct_link(LOG_DEBUG, link, - "MESSAGE=%*s: enslaving by '%s'", + "MESSAGE=%-*s: enslaving by '%s'", IFNAMSIZ, link->ifname, link->network->bond->ifname, NETDEV(link->network->bond), @@ -1494,7 +1522,7 @@ static int link_enter_enslave(Link *link) { r = netdev_enslave(link->network->bond, link, &enslave_handler); if (r < 0) { log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not enslave by '%s': %s", + "MESSAGE=%-*s: could not enslave by '%s': %s", IFNAMSIZ, link->ifname, link->network->bond->ifname, strerror(-r), NETDEV(link->network->bond), @@ -1509,7 +1537,7 @@ static int link_enter_enslave(Link *link) { if (link->network->bridge) { log_struct_link(LOG_DEBUG, link, - "MESSAGE=%*s: enslaving by '%s'", + "MESSAGE=%-*s: enslaving by '%s'", IFNAMSIZ, link->ifname, link->network->bridge->ifname, NETDEV(link->network->bridge), @@ -1518,7 +1546,7 @@ static int link_enter_enslave(Link *link) { r = netdev_enslave(link->network->bridge, link, &enslave_handler); if (r < 0) { log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not enslave by '%s': %s", + "MESSAGE=%-*s: could not enslave by '%s': %s", IFNAMSIZ, link->ifname, link->network->bridge->ifname, strerror(-r), NETDEV(link->network->bridge), @@ -1533,7 +1561,7 @@ static int link_enter_enslave(Link *link) { if (link->network->tunnel) { log_struct_link(LOG_DEBUG, link, - "MESSAGE=%*s: enslaving by '%s'", + "MESSAGE=%-*s: enslaving by '%s'", IFNAMSIZ, link->ifname, link->network->tunnel->ifname, NETDEV(link->network->tunnel), @@ -1542,7 +1570,7 @@ static int link_enter_enslave(Link *link) { r = netdev_enslave(link->network->tunnel, link, &enslave_handler); if (r < 0) { log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not enslave by '%s': %s", + "MESSAGE=%-*s: could not enslave by '%s': %s", IFNAMSIZ, link->ifname, link->network->tunnel->ifname, strerror(-r), NETDEV(link->network->tunnel), @@ -1557,14 +1585,14 @@ static int link_enter_enslave(Link *link) { HASHMAP_FOREACH(vlan, link->network->vlans, i) { log_struct_link(LOG_DEBUG, link, - "MESSAGE=%*s: enslaving by '%s'", + "MESSAGE=%-*s: enslaving by '%s'", IFNAMSIZ, link->ifname, vlan->ifname, NETDEV(vlan), NULL); r = netdev_enslave(vlan, link, &enslave_handler); if (r < 0) { log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not enslave by '%s': %s", + "MESSAGE=%-*s: could not enslave by '%s': %s", IFNAMSIZ, link->ifname, vlan->ifname, strerror(-r), NETDEV(vlan), NULL); @@ -1578,14 +1606,14 @@ static int link_enter_enslave(Link *link) { HASHMAP_FOREACH(macvlan, link->network->macvlans, i) { log_struct_link(LOG_DEBUG, link, - "MESSAGE=%*s: enslaving by '%s'", + "MESSAGE=%-*s: enslaving by '%s'", IFNAMSIZ, link->ifname, macvlan->ifname, NETDEV(macvlan), NULL); r = netdev_enslave(macvlan, link, &enslave_handler); if (r < 0) { log_struct_link(LOG_WARNING, link, - "MESSAGE=%*s: could not enslave by '%s': %s", + "MESSAGE=%-*s: could not enslave by '%s': %s", IFNAMSIZ, link->ifname, macvlan->ifname, strerror(-r), NETDEV(macvlan), NULL); @@ -1667,7 +1695,54 @@ static int link_configure(Link *link) { } } - if (link_has_carrier(link->flags, link->kernel_operstate)) { + if (link->network->dhcp_server) { + Address *address; + + r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); + if (r < 0) + return r; + + r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); + if (r < 0) + return r; + + LIST_FOREACH(addresses, address, + link->network->static_addresses) { + struct in_addr pool_start; + + if (address->family != AF_INET) + continue; + + /* currently this is picked essentially at random */ + r = sd_dhcp_server_set_address(link->dhcp_server, + &address->in_addr.in); + if (r < 0) + return r; + + /* offer 32 addresses starting from the address following the server address */ + pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1); + r = sd_dhcp_server_set_lease_pool(link->dhcp_server, + &pool_start, 32); + if (r < 0) + return r; + + break; + } + + /* TODO: + r = sd_dhcp_server_set_router(link->dhcp_server, + &main_address->in_addr.in); + if (r < 0) + return r; + + r = sd_dhcp_server_set_prefixlen(link->dhcp_server, + main_address->prefixlen); + if (r < 0) + return r; + */ + } + + if (link_has_carrier(link->flags, link->operstate)) { r = link_acquire_conf(link); if (r < 0) return r; @@ -2077,6 +2152,8 @@ int link_save(Link *link) { } if (link->dhcp_lease) { + assert(link->network); + r = dhcp_lease_save(link->dhcp_lease, link->lease_file); if (r < 0) goto finish;