3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt/***
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt This file is part of systemd.
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt Copyright (C) 2014 Tom Gundersen
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt Copyright (C) 2014-2015 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
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt#include "dhcp6-lease-internal.h"
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt#include "dhcp6-protocol.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "strv.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "util.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
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flyktint dhcp6_lease_set_dns(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt int r;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt assert_return(lease, -EINVAL);
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt assert_return(optval, -EINVAL);
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt if (!optlen)
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt return 0;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->dns,
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt lease->dns_count,
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt &lease->dns_allocated);
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt if (r < 0) {
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt log_dhcp6_client(client, "Invalid DNS server option: %s",
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt strerror(-r));
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt return r;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt }
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt lease->dns_count = r;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt return 0;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt}
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flyktint sd_dhcp6_lease_get_dns(sd_dhcp6_lease *lease, struct in6_addr **addrs) {
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt assert_return(lease, -EINVAL);
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt assert_return(addrs, -EINVAL);
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt if (lease->dns_count) {
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt *addrs = lease->dns;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt return lease->dns_count;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt }
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt return -ENOENT;
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt}
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flyktint dhcp6_lease_set_domains(sd_dhcp6_lease *lease, uint8_t *optval,
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt size_t optlen) {
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt int r;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt char **domains;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt assert_return(lease, -EINVAL);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt assert_return(optval, -EINVAL);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt if (!optlen)
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt return 0;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt r = dhcp6_option_parse_domainname(optval, optlen, &domains);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt if (r < 0)
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt return 0;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt free(lease->domains);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt lease->domains = domains;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt lease->domains_count = r;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt return r;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt}
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flyktint sd_dhcp6_lease_get_domains(sd_dhcp6_lease *lease, char ***domains) {
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt assert_return(lease, -EINVAL);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt assert_return(domains, -EINVAL);
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt if (lease->domains_count) {
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt *domains = lease->domains;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt return lease->domains_count;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt }
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt return -ENOENT;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt}
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
52efd56a6369e19c2400a42981a197cd2eef924aLennart Poetteringint dhcp6_lease_set_ntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt int r;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt uint16_t subopt;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt size_t sublen;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt uint8_t *subval;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(lease, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(optval, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
63a54aa15edee5ecadd894d2d5c73870d86e8ec4Zbigniew Jędrzejewski-Szmek lease->ntp = mfree(lease->ntp);
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt lease->ntp_count = 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt lease->ntp_allocated = 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt while ((r = dhcp6_option_parse(&optval, &optlen, &subopt, &sublen,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt &subval)) >= 0) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt int s;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt char **servers;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt switch(subopt) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt case DHCP6_NTP_SUBOPTION_SRV_ADDR:
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt case DHCP6_NTP_SUBOPTION_MC_ADDR:
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (sublen != 16)
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return 0;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt s = dhcp6_option_parse_ip6addrs(subval, sublen,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt &lease->ntp,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt lease->ntp_count,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt &lease->ntp_allocated);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (s < 0)
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return s;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt lease->ntp_count = s;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt break;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt case DHCP6_NTP_SUBOPTION_SRV_FQDN:
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt r = dhcp6_option_parse_domainname(subval, sublen,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt &servers);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (r < 0)
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return 0;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt lease->ntp_fqdn = strv_free(lease->ntp_fqdn);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt lease->ntp_fqdn = servers;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt lease->ntp_fqdn_count = r;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt break;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt }
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt }
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (r != -ENOMSG)
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return r;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return 0;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt}
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flyktint dhcp6_lease_set_sntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt int r;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt assert_return(lease, -EINVAL);
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt assert_return(optval, -EINVAL);
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt if (!optlen)
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt return 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt if (lease->ntp || lease->ntp_fqdn) {
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt log_dhcp6_client(client, "NTP information already provided");
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt return 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt }
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt log_dhcp6_client(client, "Using deprecated SNTP information");
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->ntp,
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt lease->ntp_count,
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt &lease->ntp_allocated);
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt if (r < 0) {
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt log_dhcp6_client(client, "Invalid SNTP server option: %s",
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt strerror(-r));
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt return r;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt }
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt lease->ntp_count = r;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt return 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt}
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flyktint sd_dhcp6_lease_get_ntp_addrs(sd_dhcp6_lease *lease,
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt struct in6_addr **addrs) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(lease, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(addrs, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (lease->ntp_count) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt *addrs = lease->ntp;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return lease->ntp_count;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt }
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return -ENOENT;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt}
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flyktint sd_dhcp6_lease_get_ntp_fqdn(sd_dhcp6_lease *lease, char ***ntp_fqdn) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(lease, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt assert_return(ntp_fqdn, -EINVAL);
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt if (lease->ntp_fqdn_count) {
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt *ntp_fqdn = lease->ntp_fqdn;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return lease->ntp_fqdn_count;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt }
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt return -ENOENT;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt}
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktsd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease) {
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering if (!lease)
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering return NULL;
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering assert(lease->n_ref >= 1);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering lease->n_ref++;
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return lease;
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt}
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flyktsd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease) {
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering if (!lease)
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering return NULL;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering assert(lease->n_ref >= 1);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering lease->n_ref--;
5da1b97f3c3d15521f2dcfbc18eccd6580122ebcPatrik Flykt
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering if (lease->n_ref > 0)
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering return NULL;
6599680e2d33597f0f11a99e1c3c957b42418568Patrik Flykt
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering free(lease->serverid);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering dhcp6_lease_free_ia(&lease->ia);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering free(lease->dns);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering lease->domains = strv_free(lease->domains);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering free(lease->ntp);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering lease->ntp_fqdn = strv_free(lease->ntp_fqdn);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering free(lease);
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
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering lease->n_ref = 1;
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt LIST_HEAD_INIT(lease->ia.addresses);
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt *ret = lease;
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt return 0;
3fb2c57038cf8dad396421989f43697fcf4ac4a4Patrik Flykt}