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