networkd-ipv4ll.c revision b22d8a00f48f3c5fc4510b4acd3e1a43e731e592
248N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
248N/A
248N/A/***
248N/A This file is part of systemd.
248N/A
248N/A Copyright 2013-2014 Tom Gundersen <teg@jklm.no>
248N/A
248N/A systemd is free software; you can redistribute it and/or modify it
248N/A under the terms of the GNU Lesser General Public License as published by
248N/A the Free Software Foundation; either version 2.1 of the License, or
248N/A (at your option) any later version.
248N/A
248N/A systemd is distributed in the hope that it will be useful, but
248N/A WITHOUT ANY WARRANTY; without even the implied warranty of
248N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
248N/A Lesser General Public License for more details.
248N/A
248N/A You should have received a copy of the GNU Lesser General Public License
248N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
248N/A***/
248N/A
248N/A#include <netinet/ether.h>
3768N/A#include <linux/if.h>
248N/A
248N/A#include "networkd-link.h"
814N/A#include "network-internal.h"
814N/A
1780N/Astatic int ipv4ll_address_lost(Link *link) {
814N/A _cleanup_address_free_ Address *address = NULL;
248N/A _cleanup_route_free_ Route *route = NULL;
248N/A struct in_addr addr;
248N/A int r;
248N/A
248N/A assert(link);
248N/A
248N/A link->ipv4ll_route = false;
844N/A link->ipv4ll_address = false;
844N/A
248N/A r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
1258N/A if (r < 0)
248N/A return 0;
2899N/A
2899N/A log_debug_link(link, "IPv4 link-local release %u.%u.%u.%u",
3817N/A ADDRESS_FMT_VAL(addr));
3817N/A
3817N/A r = address_new_dynamic(&address);
248N/A if (r < 0) {
248N/A log_error_link(link, "Could not allocate address: %s", strerror(-r));
248N/A return r;
248N/A }
4086N/A
248N/A address->family = AF_INET;
248N/A address->in_addr.in = addr;
4086N/A address->prefixlen = 16;
248N/A address->scope = RT_SCOPE_LINK;
248N/A
4086N/A address_drop(address, link, &link_address_drop_handler);
248N/A
248N/A r = route_new_dynamic(&route, RTPROT_UNSPEC);
248N/A if (r < 0) {
248N/A log_error_link(link, "Could not allocate route: %s",
248N/A strerror(-r));
2465N/A return r;
4963N/A }
4963N/A
4963N/A route->family = AF_INET;
248N/A route->scope = RT_SCOPE_LINK;
248N/A route->metrics = IPV4LL_ROUTE_METRIC;
3768N/A
248N/A route_drop(route, link, &link_route_drop_handler);
248N/A
2465N/A link_client_handler(link);
248N/A
248N/A return 0;
248N/A}
248N/A
248N/Astatic int ipv4ll_route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
2375N/A _cleanup_link_unref_ Link *link = userdata;
248N/A int r;
248N/A
3477N/A assert(link);
3477N/A assert(!link->ipv4ll_route);
2465N/A
248N/A r = sd_rtnl_message_get_errno(m);
2465N/A if (r < 0 && r != -EEXIST) {
248N/A log_error_link(link, "could not set ipv4ll route: %s", strerror(-r));
248N/A link_enter_failed(link);
248N/A }
4337N/A
4337N/A link->ipv4ll_route = true;
3817N/A
3817N/A if (link->ipv4ll_address == true)
3817N/A link_client_handler(link);
3817N/A
3817N/A return 1;
3817N/A}
3817N/A
static int ipv4ll_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
_cleanup_link_unref_ Link *link = userdata;
int r;
assert(link);
assert(!link->ipv4ll_address);
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -EEXIST) {
log_error_link(link, "could not set ipv4ll address: %s", strerror(-r));
link_enter_failed(link);
} else if (r >= 0) {
/* calling handler directly so take a ref */
link_ref(link);
link_get_address_handler(rtnl, m, link);
}
link->ipv4ll_address = true;
if (link->ipv4ll_route == true)
link_client_handler(link);
return 1;
}
static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
_cleanup_address_free_ Address *ll_addr = NULL;
_cleanup_route_free_ Route *route = NULL;
struct in_addr address;
int r;
assert(ll);
assert(link);
r = sd_ipv4ll_get_address(ll, &address);
if (r == -ENOENT)
return 0;
else if (r < 0)
return r;
log_debug_link(link, "IPv4 link-local claim %u.%u.%u.%u",
ADDRESS_FMT_VAL(address));
r = address_new_dynamic(&ll_addr);
if (r < 0)
return r;
ll_addr->family = AF_INET;
ll_addr->in_addr.in = address;
ll_addr->prefixlen = 16;
ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
ll_addr->scope = RT_SCOPE_LINK;
r = address_configure(ll_addr, link, ipv4ll_address_handler);
if (r < 0)
return r;
link->ipv4ll_address = false;
r = route_new_dynamic(&route, RTPROT_STATIC);
if (r < 0)
return r;
route->family = AF_INET;
route->scope = RT_SCOPE_LINK;
route->metrics = IPV4LL_ROUTE_METRIC;
r = route_configure(route, link, ipv4ll_route_handler);
if (r < 0)
return r;
link->ipv4ll_route = false;
return 0;
}
static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
Link *link = userdata;
int r;
assert(link);
assert(link->network);
assert(link->manager);
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return;
switch(event) {
case IPV4LL_EVENT_STOP:
case IPV4LL_EVENT_CONFLICT:
r = ipv4ll_address_lost(link);
if (r < 0) {
link_enter_failed(link);
return;
}
break;
case IPV4LL_EVENT_BIND:
r = ipv4ll_address_claimed(ll, link);
if (r < 0) {
link_enter_failed(link);
return;
}
break;
default:
if (event < 0)
log_warning_link(link, "IPv4 link-local error: %s", strerror(-event));
else
log_warning_link(link, "IPv4 link-local unknown event: %d", event);
break;
}
}
int ipv4ll_configure(Link *link) {
uint8_t seed[8];
int r;
assert(link);
assert(link->network);
assert(link->network->ipv4ll);
r = sd_ipv4ll_new(&link->ipv4ll);
if (r < 0)
return r;
if (link->udev_device) {
r = net_get_unique_predictable_data(link->udev_device, seed);
if (r >= 0) {
r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
if (r < 0)
return r;
}
}
r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
if (r < 0)
return r;
r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
if (r < 0)
return r;
r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
if (r < 0)
return r;
r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
if (r < 0)
return r;
return 0;
}