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=04a2f8ccbe2c151db0ab3ae7e9972bd524e713fc;hpb=e375dcde7202d5df4e29f5258e0f8c2bcea4535c;p=elogind.git diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 04a2f8ccb..452fcd43c 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -75,12 +75,16 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) { if (!link->ifname) return -ENOMEM; - r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64, + r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); + if (r < 0) + return r; + + r = asprintf(&link->state_file, "/run/systemd/netif/links/%"PRIu64, link->ifindex); if (r < 0) return -ENOMEM; - r = asprintf(&link->lease_file, "/run/systemd/network/leases/%"PRIu64, + r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%"PRIu64, link->ifindex); if (r < 0) return -ENOMEM; @@ -174,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); @@ -227,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; } @@ -245,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; @@ -264,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, @@ -430,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, @@ -462,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, @@ -609,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, @@ -636,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, @@ -721,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); @@ -890,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), @@ -905,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), @@ -1132,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), @@ -1352,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, @@ -1439,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, @@ -1481,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), @@ -1490,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), @@ -1505,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), @@ -1514,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), @@ -1529,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), @@ -1538,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), @@ -1553,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); @@ -1574,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); @@ -1663,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; @@ -1809,12 +1888,17 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *use LIST_PREPEND(addresses, link->addresses, address); address = NULL; + link_save(link); + break; case RTM_DELADDR: - if (address_dropped) + if (address_dropped) { log_debug_link(link, "removed address: %s/%u", buf, address->prefixlen); + link_save(link); + } + break; default: assert_not_reached("Received invalid RTNL message type"); @@ -2068,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;