813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt/***
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt This file is part of systemd.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt Copyright (C) 2014 Intel Corporation. All rights reserved.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt systemd is free software; you can redistribute it and/or modify it
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt under the terms of the GNU Lesser General Public License as published by
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt the Free Software Foundation; either version 2.1 of the License, or
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt (at your option) any later version.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt systemd is distributed in the hope that it will be useful, but
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt WITHOUT ANY WARRANTY; without even the implied warranty of
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt Lesser General Public License for more details.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt You should have received a copy of the GNU Lesser General Public License
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt along with systemd; If not, see <http://www.gnu.org/licenses/>.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt***/
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <net/ethernet.h>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include <stdbool.h>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include <stdio.h>
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include <sys/socket.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/types.h>
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include <unistd.h>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "sd-dhcp6-client.h"
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include "sd-event.h"
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt#include "dhcp6-internal.h"
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt#include "dhcp6-lease-internal.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "dhcp6-protocol.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fd-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "macro.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "socket-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "virt.h"
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic struct ether_addr mac_addr = {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt};
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flyktstatic bool verbose = true;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic sd_event_source *hangcheck;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic int test_dhcp_fd[2];
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic int test_index = 42;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int test_client_message_num;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic be32_t test_iaid = 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic uint8_t test_duid[14] = { };
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic int test_client_basic(sd_event *e) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt sd_dhcp6_client *client;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt if (verbose)
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt printf("* %s\n", __FUNCTION__);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_new(&client) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(client);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_set_index(client, 15) == 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_set_index(client, -42) == -EINVAL);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_set_index(client, -1) == 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_set_index(client, 42) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(sd_dhcp6_client_set_mac(client, (const uint8_t *) &mac_addr,
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams sizeof (mac_addr),
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams ARPHRD_ETHER) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_CLIENTID) == -EINVAL);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVERS) == -EEXIST);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NTP_SERVER) == -EEXIST);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_SNTP_SERVERS) == -EEXIST);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DOMAIN_LIST) == -EEXIST);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(sd_dhcp6_client_set_request_option(client, 10) == -EINVAL);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_set_callback(client, NULL, NULL) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_dhcp6_client_detach_event(client) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(!sd_dhcp6_client_unref(client));
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt return 0;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt}
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic int test_option(sd_event *e) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt uint8_t packet[] = {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 'F', 'O', 'O',
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani 0x00, SD_DHCP6_OPTION_ORO, 0x00, 0x07,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 'A', 'B', 'C', 'D', 'E', 'F', 'G',
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani 0x00, SD_DHCP6_OPTION_VENDOR_CLASS, 0x00, 0x09,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt '1', '2', '3', '4', '5', '6', '7', '8', '9',
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 'B', 'A', 'R',
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt };
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt uint8_t result[] = {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 'F', 'O', 'O',
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt 'B', 'A', 'R',
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt };
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt uint16_t optcode;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt size_t optlen;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt uint8_t *optval, *buf, *out;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt size_t zero = 0, pos = 3;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt size_t buflen = sizeof(packet), outlen = sizeof(result);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (verbose)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt printf("* %s\n", __FUNCTION__);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(buflen == outlen);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(dhcp6_option_parse(&buf, &zero, &optcode, &optlen,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt &optval) == -ENOMSG);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt buflen -= 3;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt buf = &packet[3];
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt outlen -= 3;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt out = &result[3];
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(dhcp6_option_parse(&buf, &buflen, &optcode, &optlen,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt &optval) >= 0);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt pos += 4 + optlen;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(buf == &packet[pos]);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(optcode == SD_DHCP6_OPTION_ORO);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(optlen == 7);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(buflen + pos == sizeof(packet));
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(dhcp6_option_append(&out, &outlen, optcode, optlen,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt optval) >= 0);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(out == &result[pos]);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(*out == 0x00);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(dhcp6_option_parse(&buf, &buflen, &optcode, &optlen,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt &optval) >= 0);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt pos += 4 + optlen;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(buf == &packet[pos]);
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(optcode == SD_DHCP6_OPTION_VENDOR_CLASS);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(optlen == 9);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(buflen + pos == sizeof(packet));
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(dhcp6_option_append(&out, &outlen, optcode, optlen,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt optval) >= 0);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(out == &result[pos]);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(*out == 'B');
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert_se(memcmp(packet, result, sizeof(packet)) == 0);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return 0;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt}
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic uint8_t msg_advertise[198] = {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x02, 0x0f, 0xb4, 0xe5, 0x00, 0x01, 0x00, 0x0e,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x01, 0x00, 0x01, 0x1a, 0x6b, 0xf3, 0x30,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x3c, 0x97, 0x0e, 0xcf, 0xa3, 0x7d, 0x00, 0x03,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x5e, 0x0e, 0xcf, 0xa3, 0x7d, 0x00, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x50, 0x00, 0x00, 0x00, 0x78, 0x00, 0x05,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x18, 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0xbe, 0xef, 0x78, 0xee, 0x1c, 0xf3, 0x09, 0x3c,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x55, 0xad, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0xb4, 0x00, 0x0d, 0x00, 0x32, 0x00, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x28,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x65, 0x73, 0x29, 0x20, 0x72, 0x65, 0x6e, 0x65,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x77, 0x65, 0x64, 0x2e, 0x20, 0x47, 0x72, 0x65,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x65, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x20, 0x66,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x72, 0x6f, 0x6d, 0x20, 0x70, 0x6c, 0x61, 0x6e,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x65, 0x74, 0x20, 0x45, 0x61, 0x72, 0x74, 0x68,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x17, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x0b,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x72, 0x61, 0x00, 0x00, 0x1f, 0x00, 0x10, 0x20,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x02, 0x00, 0x0e, 0x00, 0x01, 0x00, 0x01, 0x19,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x40, 0x5c, 0x53, 0x78, 0x2b, 0xcb, 0xb3, 0x6d,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x53, 0x00, 0x07, 0x00, 0x01, 0x00
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt};
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flyktstatic uint8_t msg_reply[173] = {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x07, 0xf7, 0x4e, 0x57, 0x00, 0x02, 0x00, 0x0e,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x01, 0x00, 0x01, 0x19, 0x40, 0x5c, 0x53,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53, 0x00, 0x01,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x0e, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x6b,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0xf3, 0x30, 0x3c, 0x97, 0x0e, 0xcf, 0xa3, 0x7d,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x03, 0x00, 0x4a, 0x0e, 0xcf, 0xa3, 0x7d,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x78,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x05, 0x00, 0x18, 0x20, 0x01, 0x0d, 0xb8,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0xde, 0xad, 0xbe, 0xef, 0x78, 0xee, 0x1c, 0xf3,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x09, 0x3c, 0x55, 0xad, 0x00, 0x00, 0x00, 0x96,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x00, 0x00, 0xb4, 0x00, 0x0d, 0x00, 0x1e,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x00, 0x41, 0x6c, 0x6c, 0x20, 0x61, 0x64,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, 0x73, 0x20,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x77, 0x65, 0x72, 0x65, 0x20, 0x61, 0x73, 0x73,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x69, 0x67, 0x6e, 0x65, 0x64, 0x2e, 0x00, 0x17,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x01, 0x00, 0x18, 0x00, 0x0b, 0x03, 0x6c,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74, 0x72, 0x61,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x00, 0x1f, 0x00, 0x10, 0x20, 0x01, 0x0d,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00,
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt 0x00, 0x00, 0x00, 0x00, 0x01
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt};
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int test_advertise_option(sd_event *e) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt DHCP6Message *advertise = (DHCP6Message *)msg_advertise;
44481a8b537839cd9ffead4d261491641f5b5260Zbigniew Jędrzejewski-Szmek uint8_t *optval, *opt = msg_advertise + sizeof(DHCP6Message);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt uint16_t optcode;
d182960ae974a0074010a058d0d909846a2f3f79Patrik Flykt size_t optlen, len = sizeof(msg_advertise) - sizeof(DHCP6Message);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt be32_t val;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt uint8_t preference = 255;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt struct in6_addr addr;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt uint32_t lt_pref, lt_valid;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt int r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt bool opt_clientid = false;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt struct in6_addr *addrs;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt char **domains;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (verbose)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt printf("* %s\n", __FUNCTION__);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_lease_new(&lease) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(advertise->type == DHCP6_ADVERTISE);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se((be32toh(advertise->transaction_id) & 0x00ffffff) ==
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt 0x0fb4e5);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt while ((r = dhcp6_option_parse(&opt, &len, &optcode, &optlen,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt &optval)) >= 0) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt switch(optcode) {
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_CLIENTID:
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(optlen == 14);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt opt_clientid = true;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt break;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_IA_NA:
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(optlen == 94);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(optval, &msg_advertise[26], optlen));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt val = htobe32(0x0ecfa37d);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(optval, &val, sizeof(val)));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt val = htobe32(80);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(optval + 4, &val, sizeof(val)));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt val = htobe32(120);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(optval + 8, &val, sizeof(val)));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_option_parse_ia(&optval, &optlen,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt optcode,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt &lease->ia) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt break;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_SERVERID:
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(optlen == 14);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(optval, &msg_advertise[179], optlen));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_lease_set_serverid(lease, optval,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt optlen) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt break;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_PREFERENCE:
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(optlen == 1);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!*optval);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_lease_set_preference(lease,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt *optval) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt break;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_ELAPSED_TIME:
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(optlen == 2);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt break;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_DNS_SERVERS:
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(optlen == 16);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(dhcp6_lease_set_dns(lease, optval,
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt optlen) >= 0);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt break;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_DOMAIN_LIST:
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(optlen == 11);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(dhcp6_lease_set_domains(lease, optval,
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt optlen) >= 0);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt break;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_SNTP_SERVERS:
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(optlen == 16);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(dhcp6_lease_set_sntp(lease, optval,
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt optlen) >= 0);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt break;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt default:
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt break;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(r == -ENOMSG);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(opt_clientid);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt sd_dhcp6_lease_reset_address_iter(lease);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(lt_pref == 150);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(lt_valid == 180);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) == -ENOMSG);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt sd_dhcp6_lease_reset_address_iter(lease);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) == -ENOMSG);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt sd_dhcp6_lease_reset_address_iter(lease);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) == -ENOMSG);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_lease_get_serverid(lease, &opt, &len) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(len == 14);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(!memcmp(opt, &msg_advertise[179], len));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(dhcp6_lease_get_preference(lease, &preference) >= 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert_se(preference == 0);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt r = sd_dhcp6_lease_get_dns(lease, &addrs);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(r == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[124], r * 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt r = sd_dhcp6_lease_get_domains(lease, &domains);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(r == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!strcmp("lab.intra", domains[0]));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(domains[1] == NULL);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt r = sd_dhcp6_lease_get_ntp_addrs(lease, &addrs);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(r == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[159], r * 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic int test_hangcheck(sd_event_source *s, uint64_t usec, void *userdata) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_not_reached("Test case should have completed in 2 seconds");
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flyktstatic void test_client_solicit_cb(sd_dhcp6_client *client, int event,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt void *userdata) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt sd_event *e = userdata;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt sd_dhcp6_lease *lease;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt struct in6_addr *addrs;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt char **domains;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(e);
10c9ce615d98e125bc520efa94aebaef250a4061David Herrmann assert_se(event == SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_client_get_lease(client, &lease) >= 0);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_domains(lease, &domains) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!strcmp("lab.intra", domains[0]));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(domains[1] == NULL);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_dns(lease, &addrs) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[124], 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_ntp_addrs(lease, &addrs) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[159], 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVERS) == -EBUSY);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt if (verbose)
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt printf(" got DHCPv6 event %d\n", event);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt sd_event_exit(e, 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int test_client_send_reply(DHCP6Message *request) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt DHCP6Message reply;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt reply.transaction_id = request->transaction_id;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt reply.type = DHCP6_REPLY;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt memcpy(msg_reply, &reply.transaction_id, 4);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt memcpy(&msg_reply[26], test_duid, sizeof(test_duid));
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt memcpy(&msg_reply[44], &test_iaid, sizeof(test_iaid));
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt assert_se(write(test_dhcp_fd[1], msg_reply, sizeof(msg_reply))
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt == sizeof(msg_reply));
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int test_client_verify_request(DHCP6Message *request, uint8_t *option,
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt size_t len) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt uint8_t *optval;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt uint16_t optcode;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt size_t optlen;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt bool found_clientid = false, found_iana = false, found_serverid = false,
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt found_elapsed_time = false;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt int r;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt struct in6_addr addr;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt be32_t val;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt uint32_t lt_pref, lt_valid;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(request->type == DHCP6_REQUEST);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(dhcp6_lease_new(&lease) >= 0);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt while ((r = dhcp6_option_parse(&option, &len,
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt &optcode, &optlen, &optval)) >= 0) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt switch(optcode) {
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_CLIENTID:
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!found_clientid);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt found_clientid = true;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(optval, &test_duid,
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt sizeof(test_duid)));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt break;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_IA_NA:
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!found_iana);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt found_iana = true;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(optlen == 40);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(optval, &test_iaid, sizeof(test_iaid)));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt val = htobe32(80);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(optval + 4, &val, sizeof(val)));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt val = htobe32(120);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(optval + 8, &val, sizeof(val)));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!dhcp6_option_parse_ia(&optval, &optlen,
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt optcode, &lease->ia));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt break;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_SERVERID:
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!found_serverid);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt found_serverid = true;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(optlen == 14);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(&msg_advertise[179], optval, optlen));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt break;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_ELAPSED_TIME:
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(!found_elapsed_time);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt found_elapsed_time = true;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(optlen == 2);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt break;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(r == -ENOMSG);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(found_clientid && found_iana && found_serverid &&
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt found_elapsed_time);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt sd_dhcp6_lease_reset_address_iter(lease);
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) >= 0);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(lt_pref == 150);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(lt_valid == 180);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) == -ENOMSG);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
52efd56a6369e19c2400a42981a197cd2eef924aLennart Poetteringstatic int test_client_send_advertise(DHCP6Message *solicit) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt DHCP6Message advertise;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt advertise.transaction_id = solicit->transaction_id;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt advertise.type = DHCP6_ADVERTISE;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt memcpy(msg_advertise, &advertise.transaction_id, 4);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt memcpy(&msg_advertise[8], test_duid, sizeof(test_duid));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt memcpy(&msg_advertise[26], &test_iaid, sizeof(test_iaid));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(write(test_dhcp_fd[1], msg_advertise, sizeof(msg_advertise))
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt == sizeof(msg_advertise));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int test_client_verify_solicit(DHCP6Message *solicit, uint8_t *option,
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt size_t len) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt uint8_t *optval;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt uint16_t optcode;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt size_t optlen;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt bool found_clientid = false, found_iana = false,
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt found_elapsed_time = false;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt int r;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(solicit->type == DHCP6_SOLICIT);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt while ((r = dhcp6_option_parse(&option, &len,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt &optcode, &optlen, &optval)) >= 0) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt switch(optcode) {
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_CLIENTID:
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(!found_clientid);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt found_clientid = true;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert_se(optlen == sizeof(test_duid));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt memcpy(&test_duid, optval, sizeof(test_duid));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt break;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_IA_NA:
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(!found_iana);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt found_iana = true;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(optlen == 12);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt memcpy(&test_iaid, optval, sizeof(test_iaid));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt break;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_ELAPSED_TIME:
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(!found_elapsed_time);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt found_elapsed_time = true;
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(optlen == 2);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt break;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(r == -ENOMSG);
d63be95a306bf1e262c7e1c7ad4b2c12b49d371ePatrik Flykt assert_se(found_clientid && found_iana && found_elapsed_time);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flyktstatic void test_client_information_cb(sd_dhcp6_client *client, int event,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt void *userdata) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt sd_event *e = userdata;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt sd_dhcp6_lease *lease;
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt struct in6_addr *addrs;
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen struct in6_addr address = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt char **domains;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(e);
10c9ce615d98e125bc520efa94aebaef250a4061David Herrmann assert_se(event == SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_client_get_lease(client, &lease) >= 0);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_domains(lease, &domains) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!strcmp("lab.intra", domains[0]));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(domains[1] == NULL);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_dns(lease, &addrs) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[124], 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(sd_dhcp6_lease_get_ntp_addrs(lease, &addrs) == 1);
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt assert_se(!memcmp(addrs, &msg_advertise[159], 16));
bc152ff879a79412e358daaaa3677a0fc226bcb0Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt if (verbose)
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt printf(" got DHCPv6 event %d\n", event);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
44598572da523f66872f593be7139b9a0f97deeePatrik Flykt assert_se(sd_dhcp6_client_set_information_request(client, false) == -EBUSY);
44598572da523f66872f593be7139b9a0f97deeePatrik Flykt assert_se(sd_dhcp6_client_set_callback(client, NULL, e) >= 0);
44598572da523f66872f593be7139b9a0f97deeePatrik Flykt assert_se(sd_dhcp6_client_stop(client) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_set_information_request(client, false) >= 0);
44598572da523f66872f593be7139b9a0f97deeePatrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_set_callback(client,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt test_client_solicit_cb, e) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen assert_se(sd_dhcp6_client_set_local_address(client, &address) >= 0);
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_start(client) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt}
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flyktstatic int test_client_verify_information_request(DHCP6Message *information_request,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt uint8_t *option, size_t len) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt uint8_t *optval;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt uint16_t optcode;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt size_t optlen;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt bool found_clientid = false, found_elapsed_time = false;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt int r;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt struct in6_addr addr;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt uint32_t lt_pref, lt_valid;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(information_request->type == DHCP6_INFORMATION_REQUEST);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(dhcp6_lease_new(&lease) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt while ((r = dhcp6_option_parse(&option, &len,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt &optcode, &optlen, &optval)) >= 0) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt switch(optcode) {
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_CLIENTID:
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(!found_clientid);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt found_clientid = true;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(optlen == sizeof(test_duid));
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt memcpy(&test_duid, optval, sizeof(test_duid));
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt break;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_IA_NA:
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_not_reached("IA TA option must not be present");
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt break;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_SERVERID:
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_not_reached("Server ID option must not be present");
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt break;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
2c1ab8ca9b5dec48c66eb25dd8af71731e70e517Beniamino Galvani case SD_DHCP6_OPTION_ELAPSED_TIME:
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(!found_elapsed_time);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt found_elapsed_time = true;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(optlen == 2);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt break;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt }
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt }
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(r == -ENOMSG);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(found_clientid && found_elapsed_time);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt sd_dhcp6_lease_reset_address_iter(lease);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt assert_se(sd_dhcp6_lease_get_address(lease, &addr, &lt_pref,
e7504d95479455be5cef120fc8e0a48fd74ad5caPatrik Flykt &lt_valid) == -ENOMSG);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt return 0;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt}
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktint dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt const void *packet, size_t len) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt struct in6_addr mcast =
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt DHCP6Message *message;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt uint8_t *option;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(s == test_dhcp_fd[0]);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(server_address);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(packet);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(len > sizeof(DHCP6Message) + 4);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(IN6_ARE_ADDR_EQUAL(server_address, &mcast));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt message = (DHCP6Message *)packet;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt option = (uint8_t *)(message + 1);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt len -= sizeof(DHCP6Message);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(message->transaction_id & 0x00ffffff);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (test_client_message_num == 0) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt test_client_verify_information_request(message, option, len);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt test_client_send_reply(message);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt test_client_message_num++;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt } else if (test_client_message_num == 1) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_verify_solicit(message, option, len);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_send_advertise(message);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_message_num++;
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt } else if (test_client_message_num == 2) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_verify_request(message, option, len);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_send_reply(message);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt test_client_message_num++;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return len;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flyktint dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(index == test_index);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_dhcp_fd) < 0)
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt return -errno;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt return test_dhcp_fd[0];
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic int test_client_solicit(sd_event *e) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt sd_dhcp6_client *client;
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen usec_t time_now = now(clock_boottime_or_monotonic());
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen struct in6_addr address = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
04c0136989b7eb896bfb0fb176e11233d69e1453Lennart Poettering int val = true;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (verbose)
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt printf("* %s\n", __FUNCTION__);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(sd_dhcp6_client_new(&client) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(client);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(sd_dhcp6_client_set_index(client, test_index) == 0);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(sd_dhcp6_client_set_mac(client, (const uint8_t *) &mac_addr,
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams sizeof (mac_addr),
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams ARPHRD_ETHER) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_get_information_request(client, &val) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(val == false);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_set_information_request(client, true) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(sd_dhcp6_client_get_information_request(client, &val) >= 0);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt assert_se(val == true);
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(sd_dhcp6_client_set_callback(client,
c4e8ceddccfbb14e475e74eb5c57ba32c3c6cf86Patrik Flykt test_client_information_cb, e) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen assert_se(sd_event_add_time(e, &hangcheck, clock_boottime_or_monotonic(),
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt time_now + 2 * USEC_PER_SEC, 0,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt test_hangcheck, NULL) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen assert_se(sd_dhcp6_client_set_local_address(client, &address) >= 0);
c601ebf79f0c54be14d3c16f0f484c0335cdeec4Tom Gundersen
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(sd_dhcp6_client_start(client) >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt sd_event_loop(e);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt hangcheck = sd_event_source_unref(hangcheck);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert_se(!sd_dhcp6_client_unref(client));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt test_dhcp_fd[1] = safe_close(test_dhcp_fd[1]);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktint main(int argc, char *argv[]) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_event_unrefp) sd_event *e;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert_se(sd_event_new(&e) >= 0);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt log_set_max_level(LOG_DEBUG);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt log_parse_environment();
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt log_open();
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt test_client_basic(e);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt test_option(e);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt test_advertise_option(e);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt test_client_solicit(e);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt return 0;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt}