sd-dhcp6-lease.c revision fab15fec2413bbc15dc7c24724ec82e371c966ba
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek/***
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek This file is part of systemd.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Copyright (C) 2014 Tom Gundersen
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Copyright (C) 2014 Intel Corporation. All rights reserved.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek (at your option) any later version.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek***/
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek#include <errno.h>
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek#include "util.h"
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek#include "dhcp6-lease-internal.h"
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringint dhcp6_lease_clear_timers(DHCP6IA *ia) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(ia, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek ia->timeout_t1 = sd_event_source_unref(ia->timeout_t1);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek ia->timeout_t2 = sd_event_source_unref(ia->timeout_t2);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_ia_rebind_expire(const DHCP6IA *ia, uint32_t *expire) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek DHCP6Address *addr;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek uint32_t valid = 0, t;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(ia, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(expire, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek LIST_FOREACH(addresses, addr, ia->addresses) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek t = be32toh(addr->iaaddr.lifetime_valid);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek if (valid < t)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek valid = t;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek }
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek t = be32toh(ia->lifetime_t2);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek if (t > valid)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return -EINVAL;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek *expire = valid - t;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-SzmekDHCP6IA *dhcp6_lease_free_ia(DHCP6IA *ia) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek DHCP6Address *address;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek if (!ia)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return NULL;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek dhcp6_lease_clear_timers(ia);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek while (ia->addresses) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek address = ia->addresses;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek LIST_REMOVE(addresses, ia->addresses, address);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek free(address);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek }
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return NULL;
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmekint dhcp6_lease_set_serverid(sd_dhcp6_lease *lease, const uint8_t *id,
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek size_t len) {
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(id, -EINVAL);
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek free(lease->serverid);
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek lease->serverid = memdup(id, len);
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek if (!lease->serverid)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return -EINVAL;
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek lease->serverid_len = len;
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmekint dhcp6_lease_get_serverid(sd_dhcp6_lease *lease, uint8_t **id, size_t *len) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_return(id, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(len, -EINVAL);
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek *id = lease->serverid;
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek *len = lease->serverid_len;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_set_preference(sd_dhcp6_lease *lease, uint8_t preference) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek lease->preference = preference;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_get_preference(sd_dhcp6_lease *lease, uint8_t *preference) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(preference, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek if (!lease)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return -EINVAL;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek *preference = lease->preference;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_set_rapid_commit(sd_dhcp6_lease *lease) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek lease->rapid_commit = true;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_get_rapid_commit(sd_dhcp6_lease *lease, bool *rapid_commit) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(rapid_commit, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek *rapid_commit = lease->rapid_commit;
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint dhcp6_lease_get_iaid(sd_dhcp6_lease *lease, be32_t *iaid) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(iaid, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek *iaid = lease->ia.id;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint sd_dhcp6_lease_get_next_address(sd_dhcp6_lease *lease,
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek struct in6_addr *addr,
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek uint32_t *lifetime_preferred,
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek uint32_t *lifetime_valid) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lease, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(addr, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lifetime_preferred, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_return(lifetime_valid, -EINVAL);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek if (!lease->addr_iter)
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return -ENOMSG;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
memcpy(addr, &lease->addr_iter->iaaddr.address,
sizeof(struct in6_addr));
*lifetime_preferred =
be32toh(lease->addr_iter->iaaddr.lifetime_preferred);
*lifetime_valid = be32toh(lease->addr_iter->iaaddr.lifetime_valid);
lease->addr_iter = lease->addr_iter->addresses_next;
return 0;
}
int sd_dhcp6_lease_get_first_address(sd_dhcp6_lease *lease,
struct in6_addr *addr,
uint32_t *lifetime_preferred,
uint32_t *lifetime_valid) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
assert_return(lifetime_preferred, -EINVAL);
assert_return(lifetime_valid, -EINVAL);
if (!lease->ia.addresses)
return -ENOMSG;
lease->addr_iter = lease->ia.addresses;
return sd_dhcp6_lease_get_next_address(lease, addr, lifetime_preferred,
lifetime_valid);
}
sd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease) {
if (lease)
assert_se(REFCNT_INC(lease->n_ref) >= 2);
return lease;
}
sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease) {
if (lease && REFCNT_DEC(lease->n_ref) <= 0) {
free(lease->serverid);
dhcp6_lease_free_ia(&lease->ia);
free(lease);
}
return NULL;
}
int dhcp6_lease_new(sd_dhcp6_lease **ret) {
sd_dhcp6_lease *lease;
lease = new0(sd_dhcp6_lease, 1);
if (!lease)
return -ENOMEM;
lease->n_ref = REFCNT_INIT;
LIST_HEAD_INIT(lease->ia.addresses);
*ret = lease;
return 0;
}