sd-network: add new library
This is similar to sd-login, but exposes the state of networkd rather than logind. Include it in libsystemd-dhcp and rename it to libsystemd-network.
This commit is contained in:
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
@@ -0,0 +1,55 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
Copyright (C) 2014 Tom Gundersen
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <linux/if_packet.h>
|
||||
|
||||
#include "socket-util.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
|
||||
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
|
||||
int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port);
|
||||
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
||||
const void *packet, size_t len);
|
||||
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
|
||||
const void *packet, size_t len);
|
||||
|
||||
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
|
||||
size_t optlen, const void *optval);
|
||||
|
||||
typedef int (*dhcp_option_cb_t)(uint8_t code, uint8_t len,
|
||||
const uint8_t *option, void *user_data);
|
||||
|
||||
int dhcp_option_parse(DHCPMessage *message, size_t len,
|
||||
dhcp_option_cb_t cb, void *user_data);
|
||||
|
||||
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, uint8_t type,
|
||||
uint8_t **opt, size_t *optlen);
|
||||
|
||||
void dhcp_packet_append_ip_headers(DHCPPacket *packet, uint16_t len);
|
||||
|
||||
int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum);
|
||||
|
||||
#define log_dhcp_client(client, fmt, ...) log_meta(LOG_DEBUG, __FILE__, __LINE__, __func__, "DHCP CLIENT: " fmt, ##__VA_ARGS__)
|
||||
@@ -0,0 +1,59 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
Copyright (C) 2014 Tom Gundersen
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <linux/if_packet.h>
|
||||
|
||||
#include "refcnt.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
|
||||
#include "sd-dhcp-client.h"
|
||||
|
||||
struct sd_dhcp_lease {
|
||||
RefCount n_ref;
|
||||
|
||||
uint32_t t1;
|
||||
uint32_t t2;
|
||||
uint32_t lifetime;
|
||||
be32_t address;
|
||||
be32_t server_address;
|
||||
be32_t subnet_mask;
|
||||
be32_t router;
|
||||
struct in_addr *dns;
|
||||
size_t dns_size;
|
||||
uint16_t mtu;
|
||||
char *domainname;
|
||||
char *hostname;
|
||||
};
|
||||
|
||||
int dhcp_lease_new(sd_dhcp_lease **ret);
|
||||
int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
|
||||
void *user_data);
|
||||
|
||||
int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file);
|
||||
int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_lease*, sd_dhcp_lease_unref);
|
||||
#define _cleanup_dhcp_lease_unref_ _cleanup_(sd_dhcp_lease_unrefp)
|
||||
@@ -0,0 +1,109 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <linux/if_packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "socket-util.h"
|
||||
|
||||
#include "dhcp-internal.h"
|
||||
|
||||
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
|
||||
{
|
||||
int s, one = 1;
|
||||
|
||||
assert(index > 0);
|
||||
assert(link);
|
||||
|
||||
s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
||||
htons(ETH_P_IP));
|
||||
if (s < 0)
|
||||
return -errno;
|
||||
|
||||
link->ll.sll_family = AF_PACKET;
|
||||
link->ll.sll_protocol = htons(ETH_P_IP);
|
||||
link->ll.sll_ifindex = index;
|
||||
link->ll.sll_halen = ETH_ALEN;
|
||||
memset(link->ll.sll_addr, 0xff, ETH_ALEN);
|
||||
|
||||
if (setsockopt (s, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)) < 0)
|
||||
return -errno;
|
||||
|
||||
if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
|
||||
close_nointr_nofail(s);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
|
||||
{
|
||||
int s;
|
||||
union sockaddr_union src = {
|
||||
.in.sin_family = AF_INET,
|
||||
.in.sin_port = htobe16(port),
|
||||
.in.sin_addr.s_addr = address,
|
||||
};
|
||||
|
||||
s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (s < 0)
|
||||
return -errno;
|
||||
|
||||
if (bind(s, &src.sa, sizeof(src.in)) < 0) {
|
||||
close_nointr_nofail(s);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
||||
const void *packet, size_t len)
|
||||
{
|
||||
assert(link);
|
||||
assert(packet);
|
||||
assert(len);
|
||||
|
||||
if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
|
||||
return -errno;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
|
||||
const void *packet, size_t len)
|
||||
{
|
||||
union sockaddr_union dest = {
|
||||
.in.sin_family = AF_INET,
|
||||
.in.sin_port = htobe16(port),
|
||||
.in.sin_addr.s_addr = address,
|
||||
};
|
||||
|
||||
if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
|
||||
return -errno;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "dhcp-internal.h"
|
||||
|
||||
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
|
||||
size_t optlen, const void *optval)
|
||||
{
|
||||
if (!buf || !buflen)
|
||||
return -EINVAL;
|
||||
|
||||
switch (code) {
|
||||
|
||||
case DHCP_OPTION_PAD:
|
||||
case DHCP_OPTION_END:
|
||||
if (*buflen < 1)
|
||||
return -ENOBUFS;
|
||||
|
||||
(*buf)[0] = code;
|
||||
*buf += 1;
|
||||
*buflen -= 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (*buflen < optlen + 2)
|
||||
return -ENOBUFS;
|
||||
|
||||
if (!optval)
|
||||
return -EINVAL;
|
||||
|
||||
(*buf)[0] = code;
|
||||
(*buf)[1] = optlen;
|
||||
memcpy(&(*buf)[2], optval, optlen);
|
||||
|
||||
*buf += optlen + 2;
|
||||
*buflen -= (optlen + 2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_options(const uint8_t *buf, size_t buflen, uint8_t *overload,
|
||||
uint8_t *message_type, dhcp_option_cb_t cb,
|
||||
void *user_data)
|
||||
{
|
||||
const uint8_t *code = buf;
|
||||
const uint8_t *len;
|
||||
|
||||
while (buflen > 0) {
|
||||
switch (*code) {
|
||||
case DHCP_OPTION_PAD:
|
||||
buflen -= 1;
|
||||
code++;
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_END:
|
||||
return 0;
|
||||
|
||||
case DHCP_OPTION_MESSAGE_TYPE:
|
||||
if (buflen < 3)
|
||||
return -ENOBUFS;
|
||||
buflen -= 3;
|
||||
|
||||
len = code + 1;
|
||||
if (*len != 1)
|
||||
return -EINVAL;
|
||||
|
||||
if (message_type)
|
||||
*message_type = *(len + 1);
|
||||
|
||||
code += 3;
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_OVERLOAD:
|
||||
if (buflen < 3)
|
||||
return -ENOBUFS;
|
||||
buflen -= 3;
|
||||
|
||||
len = code + 1;
|
||||
if (*len != 1)
|
||||
return -EINVAL;
|
||||
|
||||
if (overload)
|
||||
*overload = *(len + 1);
|
||||
|
||||
code += 3;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if (buflen < 3)
|
||||
return -ENOBUFS;
|
||||
|
||||
len = code + 1;
|
||||
|
||||
if (buflen < (size_t)*len + 2)
|
||||
return -EINVAL;
|
||||
buflen -= *len + 2;
|
||||
|
||||
if (cb)
|
||||
cb(*code, *len, len + 1, user_data);
|
||||
|
||||
code += *len + 2;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (buflen)
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_option_parse(DHCPMessage *message, size_t len,
|
||||
dhcp_option_cb_t cb, void *user_data)
|
||||
{
|
||||
uint8_t overload = 0;
|
||||
uint8_t message_type = 0;
|
||||
uint8_t *opt = (uint8_t *)(message + 1);
|
||||
int res;
|
||||
|
||||
if (!message)
|
||||
return -EINVAL;
|
||||
|
||||
if (len < sizeof(DHCPMessage) + 4)
|
||||
return -EINVAL;
|
||||
|
||||
len -= sizeof(DHCPMessage) + 4;
|
||||
|
||||
if (opt[0] != 0x63 && opt[1] != 0x82 && opt[2] != 0x53 &&
|
||||
opt[3] != 0x63)
|
||||
return -EINVAL;
|
||||
|
||||
res = parse_options(&opt[4], len, &overload, &message_type,
|
||||
cb, user_data);
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
if (overload & DHCP_OVERLOAD_FILE) {
|
||||
res = parse_options(message->file, sizeof(message->file),
|
||||
NULL, &message_type, cb, user_data);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (overload & DHCP_OVERLOAD_SNAME) {
|
||||
res = parse_options(message->sname, sizeof(message->sname),
|
||||
NULL, &message_type, cb, user_data);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (message_type)
|
||||
return message_type;
|
||||
|
||||
return -ENOMSG;
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
Copyright (C) 2014 Tom Gundersen
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "list.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
#include "dhcp-lease-internal.h"
|
||||
#include "dhcp-internal.h"
|
||||
#include "sd-dhcp-lease.h"
|
||||
#include "sd-dhcp-client.h"
|
||||
|
||||
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
|
||||
|
||||
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
|
||||
uint8_t type, uint8_t **opt, size_t *optlen) {
|
||||
int err;
|
||||
|
||||
assert(op == BOOTREQUEST || op == BOOTREPLY);
|
||||
|
||||
*opt = (uint8_t *)(message + 1);
|
||||
|
||||
if (*optlen < 4)
|
||||
return -ENOBUFS;
|
||||
*optlen -= 4;
|
||||
|
||||
message->op = op;
|
||||
message->htype = ARPHRD_ETHER;
|
||||
message->hlen = ETHER_ADDR_LEN;
|
||||
message->xid = htobe32(xid);
|
||||
|
||||
(*opt)[0] = 0x63;
|
||||
(*opt)[1] = 0x82;
|
||||
(*opt)[2] = 0x53;
|
||||
(*opt)[3] = 0x63;
|
||||
|
||||
*opt += 4;
|
||||
|
||||
err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
|
||||
&type);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint16_t dhcp_checksum(void *buf, int len) {
|
||||
uint32_t sum;
|
||||
uint16_t *check;
|
||||
int i;
|
||||
uint8_t *odd;
|
||||
|
||||
sum = 0;
|
||||
check = buf;
|
||||
|
||||
for (i = 0; i < len / 2 ; i++)
|
||||
sum += check[i];
|
||||
|
||||
if (len & 0x01) {
|
||||
odd = buf;
|
||||
sum += odd[len - 1];
|
||||
}
|
||||
|
||||
while (sum >> 16)
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
|
||||
return ~sum;
|
||||
}
|
||||
|
||||
void dhcp_packet_append_ip_headers(DHCPPacket *packet, uint16_t len) {
|
||||
packet->ip.version = IPVERSION;
|
||||
packet->ip.ihl = DHCP_IP_SIZE / 4;
|
||||
packet->ip.tot_len = htobe16(len);
|
||||
|
||||
packet->ip.protocol = IPPROTO_UDP;
|
||||
packet->ip.saddr = INADDR_ANY;
|
||||
packet->ip.daddr = INADDR_BROADCAST;
|
||||
|
||||
packet->udp.source = htobe16(DHCP_PORT_CLIENT);
|
||||
packet->udp.dest = htobe16(DHCP_PORT_SERVER);
|
||||
|
||||
packet->udp.len = htobe16(len - DHCP_IP_SIZE);
|
||||
|
||||
packet->ip.check = packet->udp.len;
|
||||
packet->udp.check = dhcp_checksum(&packet->ip.ttl, len - 8);
|
||||
|
||||
packet->ip.ttl = IPDEFTTL;
|
||||
packet->ip.check = 0;
|
||||
packet->ip.check = dhcp_checksum(&packet->ip, DHCP_IP_SIZE);
|
||||
}
|
||||
|
||||
int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
|
||||
size_t hdrlen;
|
||||
|
||||
assert(packet);
|
||||
|
||||
/* IP */
|
||||
|
||||
if (len < DHCP_IP_SIZE) {
|
||||
log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
|
||||
" smaller than IP header (%u bytes)", len,
|
||||
DHCP_IP_SIZE);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (packet->ip.ihl < 5) {
|
||||
log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid",
|
||||
packet->ip.ihl);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
hdrlen = packet->ip.ihl * 4;
|
||||
if (hdrlen < 20) {
|
||||
log_dhcp_client(client, "ignoring packet: IPv4 IHL (%zu bytes) "
|
||||
"smaller than minimum (20 bytes)", hdrlen);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (len < hdrlen) {
|
||||
log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
|
||||
"smaller than expected (%zu) by IP header", len,
|
||||
hdrlen);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (dhcp_checksum(&packet->ip, hdrlen)) {
|
||||
log_dhcp_client(client, "ignoring packet: invalid IP checksum");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* UDP */
|
||||
|
||||
if (len < DHCP_IP_UDP_SIZE) {
|
||||
log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
|
||||
" smaller than IP+UDP header (%u bytes)", len,
|
||||
DHCP_IP_UDP_SIZE);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (len < hdrlen + be16toh(packet->udp.len)) {
|
||||
log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
|
||||
"smaller than expected (%zu) by UDP header", len,
|
||||
hdrlen + be16toh(packet->udp.len));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (checksum && packet->udp.check) {
|
||||
packet->ip.check = packet->udp.len;
|
||||
packet->ip.ttl = 0;
|
||||
|
||||
if (dhcp_checksum(&packet->ip.ttl,
|
||||
be16toh(packet->udp.len) + 12)) {
|
||||
log_dhcp_client(client, "ignoring packet: invalid UDP checksum");
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
|
||||
log_dhcp_client(client, "ignoring packet: to port %u, which "
|
||||
"is not the DHCP client port (%u)",
|
||||
be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "sparse-endian.h"
|
||||
|
||||
struct DHCPMessage {
|
||||
uint8_t op;
|
||||
uint8_t htype;
|
||||
uint8_t hlen;
|
||||
uint8_t hops;
|
||||
be32_t xid;
|
||||
be16_t secs;
|
||||
be16_t flags;
|
||||
be32_t ciaddr;
|
||||
be32_t yiaddr;
|
||||
be32_t siaddr;
|
||||
be32_t giaddr;
|
||||
uint8_t chaddr[16];
|
||||
uint8_t sname[64];
|
||||
uint8_t file[128];
|
||||
} _packed_;
|
||||
|
||||
typedef struct DHCPMessage DHCPMessage;
|
||||
|
||||
struct DHCPPacket {
|
||||
struct iphdr ip;
|
||||
struct udphdr udp;
|
||||
DHCPMessage dhcp;
|
||||
} _packed_;
|
||||
|
||||
typedef struct DHCPPacket DHCPPacket;
|
||||
|
||||
#define DHCP_IP_SIZE (int32_t)(sizeof(struct iphdr))
|
||||
#define DHCP_IP_UDP_SIZE (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
|
||||
#define DHCP_MESSAGE_SIZE (int32_t)(sizeof(DHCPMessage))
|
||||
#define DHCP_MIN_OPTIONS_SIZE 312
|
||||
|
||||
enum {
|
||||
DHCP_PORT_SERVER = 67,
|
||||
DHCP_PORT_CLIENT = 68,
|
||||
};
|
||||
|
||||
enum DHCPState {
|
||||
DHCP_STATE_INIT = 0,
|
||||
DHCP_STATE_SELECTING = 1,
|
||||
DHCP_STATE_INIT_REBOOT = 2,
|
||||
DHCP_STATE_REBOOTING = 3,
|
||||
DHCP_STATE_REQUESTING = 4,
|
||||
DHCP_STATE_BOUND = 5,
|
||||
DHCP_STATE_RENEWING = 6,
|
||||
DHCP_STATE_REBINDING = 7,
|
||||
};
|
||||
|
||||
typedef enum DHCPState DHCPState;
|
||||
|
||||
enum {
|
||||
BOOTREQUEST = 1,
|
||||
BOOTREPLY = 2,
|
||||
};
|
||||
|
||||
enum {
|
||||
DHCP_DISCOVER = 1,
|
||||
DHCP_OFFER = 2,
|
||||
DHCP_REQUEST = 3,
|
||||
DHCP_DECLINE = 4,
|
||||
DHCP_ACK = 5,
|
||||
DHCP_NAK = 6,
|
||||
DHCP_RELEASE = 7,
|
||||
};
|
||||
|
||||
enum {
|
||||
DHCP_OVERLOAD_FILE = 1,
|
||||
DHCP_OVERLOAD_SNAME = 2,
|
||||
};
|
||||
|
||||
enum {
|
||||
DHCP_OPTION_PAD = 0,
|
||||
DHCP_OPTION_SUBNET_MASK = 1,
|
||||
DHCP_OPTION_ROUTER = 3,
|
||||
DHCP_OPTION_DOMAIN_NAME_SERVER = 6,
|
||||
DHCP_OPTION_HOST_NAME = 12,
|
||||
DHCP_OPTION_DOMAIN_NAME = 15,
|
||||
DHCP_OPTION_INTERFACE_MTU = 26,
|
||||
DHCP_OPTION_NTP_SERVER = 42,
|
||||
DHCP_OPTION_REQUESTED_IP_ADDRESS = 50,
|
||||
DHCP_OPTION_IP_ADDRESS_LEASE_TIME = 51,
|
||||
DHCP_OPTION_OVERLOAD = 52,
|
||||
DHCP_OPTION_MESSAGE_TYPE = 53,
|
||||
DHCP_OPTION_SERVER_IDENTIFIER = 54,
|
||||
DHCP_OPTION_PARAMETER_REQUEST_LIST = 55,
|
||||
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE = 57,
|
||||
DHCP_OPTION_RENEWAL_T1_TIME = 58,
|
||||
DHCP_OPTION_REBINDING_T2_TIME = 59,
|
||||
DHCP_OPTION_CLIENT_IDENTIFIER = 61,
|
||||
DHCP_OPTION_END = 255,
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,401 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
Copyright (C) 2014 Tom Gundersen
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "list.h"
|
||||
#include "mkdir.h"
|
||||
#include "fileio.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
#include "dhcp-internal.h"
|
||||
#include "dhcp-lease-internal.h"
|
||||
#include "sd-dhcp-lease.h"
|
||||
#include "sd-dhcp-client.h"
|
||||
|
||||
int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
|
||||
addr->s_addr = lease->address;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(mtu, -EINVAL);
|
||||
|
||||
if (lease->mtu)
|
||||
*mtu = lease->mtu;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
assert_return(addr_size, -EINVAL);
|
||||
|
||||
if (lease->dns_size) {
|
||||
*addr_size = lease->dns_size;
|
||||
*addr = lease->dns;
|
||||
} else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(domainname, -EINVAL);
|
||||
|
||||
if (lease->domainname)
|
||||
*domainname = lease->domainname;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(hostname, -EINVAL);
|
||||
|
||||
if (lease->hostname)
|
||||
*hostname = lease->hostname;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
|
||||
addr->s_addr = lease->router;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) {
|
||||
assert_return(lease, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
|
||||
addr->s_addr = lease->subnet_mask;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
|
||||
if (lease)
|
||||
assert_se(REFCNT_INC(lease->n_ref) >= 2);
|
||||
|
||||
return lease;
|
||||
}
|
||||
|
||||
sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
|
||||
if (lease && REFCNT_DEC(lease->n_ref) <= 0) {
|
||||
free(lease->hostname);
|
||||
free(lease->domainname);
|
||||
free(lease->dns);
|
||||
free(lease);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
|
||||
void *user_data) {
|
||||
sd_dhcp_lease *lease = user_data;
|
||||
be32_t val;
|
||||
|
||||
switch(code) {
|
||||
|
||||
case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
|
||||
if (len == 4) {
|
||||
memcpy(&val, option, 4);
|
||||
lease->lifetime = be32toh(val);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_SERVER_IDENTIFIER:
|
||||
if (len >= 4)
|
||||
memcpy(&lease->server_address, option, 4);
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_SUBNET_MASK:
|
||||
if (len >= 4)
|
||||
memcpy(&lease->subnet_mask, option, 4);
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_ROUTER:
|
||||
if (len >= 4)
|
||||
memcpy(&lease->router, option, 4);
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_DOMAIN_NAME_SERVER:
|
||||
if (len && !(len % 4)) {
|
||||
unsigned i;
|
||||
|
||||
lease->dns_size = len / 4;
|
||||
|
||||
free(lease->dns);
|
||||
lease->dns = new0(struct in_addr, lease->dns_size);
|
||||
if (!lease->dns)
|
||||
return -ENOMEM;
|
||||
|
||||
for (i = 0; i < lease->dns_size; i++) {
|
||||
memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_INTERFACE_MTU:
|
||||
if (len >= 2) {
|
||||
be16_t mtu;
|
||||
|
||||
memcpy(&mtu, option, 2);
|
||||
lease->mtu = be16toh(mtu);
|
||||
|
||||
if (lease->mtu < 68)
|
||||
lease->mtu = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_DOMAIN_NAME:
|
||||
if (len >= 1) {
|
||||
free(lease->domainname);
|
||||
lease->domainname = strndup((const char *)option, len);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_HOST_NAME:
|
||||
if (len >= 1) {
|
||||
free(lease->hostname);
|
||||
lease->hostname = strndup((const char *)option, len);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_RENEWAL_T1_TIME:
|
||||
if (len == 4) {
|
||||
memcpy(&val, option, 4);
|
||||
lease->t1 = be32toh(val);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_REBINDING_T2_TIME:
|
||||
if (len == 4) {
|
||||
memcpy(&val, option, 4);
|
||||
lease->t2 = be32toh(val);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_lease_new(sd_dhcp_lease **ret) {
|
||||
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
|
||||
|
||||
lease = new0(sd_dhcp_lease, 1);
|
||||
if (!lease)
|
||||
return -ENOMEM;
|
||||
|
||||
lease->n_ref = REFCNT_INIT;
|
||||
|
||||
*ret = lease;
|
||||
lease = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
|
||||
_cleanup_free_ char *temp_path = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
char buf[INET_ADDRSTRLEN];
|
||||
struct in_addr address;
|
||||
const char *string;
|
||||
uint16_t mtu;
|
||||
int r;
|
||||
|
||||
assert(lease);
|
||||
assert(lease_file);
|
||||
|
||||
r = mkdir_safe_label("/run/systemd/network/leases", 0755, 0, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = fopen_temporary(lease_file, &f, &temp_path);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
fchmod(fileno(f), 0644);
|
||||
|
||||
r = sd_dhcp_lease_get_address(lease, &address);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
|
||||
if (!string) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
fprintf(f,
|
||||
"# This is private data. Do not parse.\n"
|
||||
"ADDRESS=%s\n", string);
|
||||
|
||||
r = sd_dhcp_lease_get_router(lease, &address);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
|
||||
if (!string) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
fprintf(f,
|
||||
"ROUTER=%s\n", string);
|
||||
|
||||
r = sd_dhcp_lease_get_netmask(lease, &address);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
|
||||
if (!string) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
fprintf(f,
|
||||
"NETMASK=%s\n", string);
|
||||
|
||||
r = sd_dhcp_lease_get_mtu(lease, &mtu);
|
||||
if (r >= 0)
|
||||
fprintf(f, "MTU=%" PRIu16 "\n", mtu);
|
||||
|
||||
/* TODO: DNS. See resolv.conf writing in network-manager.c */
|
||||
|
||||
r = sd_dhcp_lease_get_domainname(lease, &string);
|
||||
if (r >= 0)
|
||||
fprintf(f, "DOMAINNAME=%s\n", string);
|
||||
|
||||
r = sd_dhcp_lease_get_hostname(lease, &string);
|
||||
if (r >= 0)
|
||||
fprintf(f, "HOSTNAME=%s\n", string);
|
||||
|
||||
r = 0;
|
||||
|
||||
fflush(f);
|
||||
|
||||
if (ferror(f) || rename(temp_path, lease_file) < 0) {
|
||||
r = -errno;
|
||||
unlink(lease_file);
|
||||
unlink(temp_path);
|
||||
}
|
||||
|
||||
finish:
|
||||
if (r < 0)
|
||||
log_error("Failed to save lease data %s: %s", lease_file, strerror(-r));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
|
||||
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
|
||||
_cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
|
||||
*mtu = NULL;
|
||||
struct in_addr addr;
|
||||
int r;
|
||||
|
||||
assert(lease_file);
|
||||
assert(ret);
|
||||
|
||||
r = dhcp_lease_new(&lease);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = parse_env_file(lease_file, NEWLINE,
|
||||
"ADDRESS", &address,
|
||||
"ROUTER", &router,
|
||||
"NETMASK", &netmask,
|
||||
"MTU", &mtu,
|
||||
"DOMAINNAME", &lease->domainname,
|
||||
"HOSTNAME", &lease->hostname,
|
||||
NULL);
|
||||
if (r < 0) {
|
||||
if (r == -ENOENT)
|
||||
return 0;
|
||||
|
||||
log_error("Failed to read %s: %s", lease_file, strerror(-r));
|
||||
return r;
|
||||
}
|
||||
|
||||
r = inet_pton(AF_INET, address, &addr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
lease->address = addr.s_addr;
|
||||
|
||||
r = inet_pton(AF_INET, router, &addr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
lease->router = addr.s_addr;
|
||||
|
||||
r = inet_pton(AF_INET, netmask, &addr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
lease->subnet_mask = addr.s_addr;
|
||||
|
||||
if (mtu) {
|
||||
uint16_t u;
|
||||
if (sscanf(mtu, "%" SCNu16, &u) > 0)
|
||||
lease->mtu = u;
|
||||
}
|
||||
|
||||
*ret = lease;
|
||||
lease = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright (C) 2013 Intel Corporation. All rights reserved.
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "socket-util.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
#include "dhcp-internal.h"
|
||||
#include "sd-dhcp-client.h"
|
||||
|
||||
static struct ether_addr mac_addr = {
|
||||
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
|
||||
};
|
||||
|
||||
typedef int (*test_callback_recv_t)(size_t size, DHCPMessage *dhcp);
|
||||
|
||||
static bool verbose = false;
|
||||
static int test_fd[2];
|
||||
static test_callback_recv_t callback_recv;
|
||||
static be32_t xid;
|
||||
|
||||
static void test_request_basic(sd_event *e)
|
||||
{
|
||||
int r;
|
||||
|
||||
sd_dhcp_client *client;
|
||||
|
||||
if (verbose)
|
||||
printf("* %s\n", __FUNCTION__);
|
||||
|
||||
r = sd_dhcp_client_new(&client);
|
||||
|
||||
assert_se(r >= 0);
|
||||
assert_se(client);
|
||||
|
||||
r = sd_dhcp_client_attach_event(client, e, 0);
|
||||
assert_se(r >= 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_request_option(NULL, 0) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_request_address(NULL, NULL) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_index(NULL, 0) == -EINVAL);
|
||||
|
||||
assert_se(sd_dhcp_client_set_index(client, 15) == 0);
|
||||
assert_se(sd_dhcp_client_set_index(client, -42) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_index(client, -1) == 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_SUBNET_MASK) == -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_ROUTER) == -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_HOST_NAME) == -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_DOMAIN_NAME) == -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_DOMAIN_NAME_SERVER)
|
||||
== -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_NTP_SERVER) == -EEXIST);
|
||||
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_PAD) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_END) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_MESSAGE_TYPE) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_OVERLOAD) == -EINVAL);
|
||||
assert_se(sd_dhcp_client_set_request_option(client,
|
||||
DHCP_OPTION_PARAMETER_REQUEST_LIST)
|
||||
== -EINVAL);
|
||||
|
||||
assert_se(sd_dhcp_client_set_request_option(client, 33) == 0);
|
||||
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
|
||||
assert_se(sd_dhcp_client_set_request_option(client, 44) == 0);
|
||||
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
|
||||
}
|
||||
|
||||
static uint16_t client_checksum(void *buf, int len)
|
||||
{
|
||||
uint32_t sum;
|
||||
uint16_t *check;
|
||||
int i;
|
||||
uint8_t *odd;
|
||||
|
||||
sum = 0;
|
||||
check = buf;
|
||||
|
||||
for (i = 0; i < len / 2 ; i++)
|
||||
sum += check[i];
|
||||
|
||||
if (len & 0x01) {
|
||||
odd = buf;
|
||||
sum += odd[len - 1];
|
||||
}
|
||||
|
||||
while (sum >> 16)
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
|
||||
return ~sum;
|
||||
}
|
||||
|
||||
static void test_checksum(void)
|
||||
{
|
||||
uint8_t buf[20] = {
|
||||
0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
|
||||
0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xff, 0xff, 0xff, 0xff
|
||||
};
|
||||
|
||||
if (verbose)
|
||||
printf("* %s\n", __FUNCTION__);
|
||||
|
||||
assert_se(client_checksum(&buf, 20) == be16toh(0x78ae));
|
||||
}
|
||||
|
||||
static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
|
||||
void *user_data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
||||
const void *packet, size_t len)
|
||||
{
|
||||
size_t size;
|
||||
_cleanup_free_ DHCPPacket *discover;
|
||||
uint16_t ip_check, udp_check;
|
||||
|
||||
assert_se(s >= 0);
|
||||
assert_se(packet);
|
||||
|
||||
size = sizeof(DHCPPacket) + 4;
|
||||
assert_se(len > size);
|
||||
|
||||
discover = memdup(packet, len);
|
||||
|
||||
assert_se(discover->ip.ttl == IPDEFTTL);
|
||||
assert_se(discover->ip.protocol == IPPROTO_UDP);
|
||||
assert_se(discover->ip.saddr == INADDR_ANY);
|
||||
assert_se(discover->ip.daddr == INADDR_BROADCAST);
|
||||
assert_se(discover->udp.source == be16toh(DHCP_PORT_CLIENT));
|
||||
assert_se(discover->udp.dest == be16toh(DHCP_PORT_SERVER));
|
||||
|
||||
ip_check = discover->ip.check;
|
||||
|
||||
discover->ip.ttl = 0;
|
||||
discover->ip.check = discover->udp.len;
|
||||
|
||||
udp_check = ~client_checksum(&discover->ip.ttl, len - 8);
|
||||
assert_se(udp_check == 0xffff);
|
||||
|
||||
discover->ip.ttl = IPDEFTTL;
|
||||
discover->ip.check = ip_check;
|
||||
|
||||
ip_check = ~client_checksum(&discover->ip, sizeof(discover->ip));
|
||||
assert_se(ip_check == 0xffff);
|
||||
|
||||
assert_se(discover->dhcp.xid);
|
||||
assert_se(memcmp(discover->dhcp.chaddr,
|
||||
&mac_addr.ether_addr_octet, 6) == 0);
|
||||
|
||||
size = len - sizeof(struct iphdr) - sizeof(struct udphdr);
|
||||
|
||||
assert_se(callback_recv);
|
||||
callback_recv(size, &discover->dhcp);
|
||||
|
||||
return 575;
|
||||
}
|
||||
|
||||
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
|
||||
{
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0)
|
||||
return -errno;
|
||||
|
||||
return test_fd[0];
|
||||
}
|
||||
|
||||
int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
|
||||
const void *packet, size_t len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = dhcp_option_parse(dhcp, size, check_options, NULL);
|
||||
assert_se(res == DHCP_DISCOVER);
|
||||
|
||||
if (verbose)
|
||||
printf(" recv DHCP Discover 0x%08x\n", be32toh(dhcp->xid));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_discover_message(sd_event *e)
|
||||
{
|
||||
sd_dhcp_client *client;
|
||||
int res, r;
|
||||
|
||||
if (verbose)
|
||||
printf("* %s\n", __FUNCTION__);
|
||||
|
||||
r = sd_dhcp_client_new(&client);
|
||||
assert_se(r >= 0);
|
||||
assert_se(client);
|
||||
|
||||
r = sd_dhcp_client_attach_event(client, e, 0);
|
||||
assert_se(r >= 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_index(client, 42) >= 0);
|
||||
assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_request_option(client, 248) >= 0);
|
||||
|
||||
callback_recv = test_discover_message_verify;
|
||||
|
||||
res = sd_dhcp_client_start(client);
|
||||
|
||||
assert_se(res == 0 || res == -EINPROGRESS);
|
||||
|
||||
sd_event_run(e, (uint64_t) -1);
|
||||
|
||||
sd_dhcp_client_stop(client);
|
||||
sd_dhcp_client_free(client);
|
||||
|
||||
close(test_fd[0]);
|
||||
close(test_fd[1]);
|
||||
|
||||
callback_recv = NULL;
|
||||
}
|
||||
|
||||
static uint8_t test_addr_acq_offer[] = {
|
||||
0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00,
|
||||
0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01,
|
||||
0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44,
|
||||
0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00,
|
||||
0x6f, 0x95, 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf,
|
||||
0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x02, 0x36,
|
||||
0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00,
|
||||
0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff,
|
||||
0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f,
|
||||
0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74,
|
||||
0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01,
|
||||
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
static uint8_t test_addr_acq_ack[] = {
|
||||
0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00,
|
||||
0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01,
|
||||
0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44,
|
||||
0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf,
|
||||
0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36,
|
||||
0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00,
|
||||
0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff,
|
||||
0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f,
|
||||
0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74,
|
||||
0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01,
|
||||
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
static void test_addr_acq_acquired(sd_dhcp_client *client, int event,
|
||||
void *userdata)
|
||||
{
|
||||
sd_event *e = userdata;
|
||||
sd_dhcp_lease *lease;
|
||||
struct in_addr addr;
|
||||
|
||||
assert_se(client);
|
||||
assert_se(event == DHCP_EVENT_IP_ACQUIRE);
|
||||
|
||||
assert_se(sd_dhcp_client_get_lease(client, &lease) >= 0);
|
||||
assert_se(lease);
|
||||
|
||||
assert_se(sd_dhcp_lease_get_address(lease, &addr) >= 0);
|
||||
assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[44],
|
||||
sizeof(addr.s_addr)) == 0);
|
||||
|
||||
assert_se(sd_dhcp_lease_get_netmask(lease, &addr) >= 0);
|
||||
assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[285],
|
||||
sizeof(addr.s_addr)) == 0);
|
||||
|
||||
assert_se(sd_dhcp_lease_get_router(lease, &addr) >= 0);
|
||||
assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[308],
|
||||
sizeof(addr.s_addr)) == 0);
|
||||
|
||||
if (verbose)
|
||||
printf(" DHCP address acquired\n");
|
||||
|
||||
sd_event_exit(e, 0);
|
||||
}
|
||||
|
||||
static int test_addr_acq_recv_request(size_t size, DHCPMessage *request)
|
||||
{
|
||||
uint16_t udp_check = 0;
|
||||
int res;
|
||||
|
||||
res = dhcp_option_parse(request, size, check_options, NULL);
|
||||
assert_se(res == DHCP_REQUEST);
|
||||
assert_se(xid == request->xid);
|
||||
|
||||
if (verbose)
|
||||
printf(" recv DHCP Request 0x%08x\n", be32toh(xid));
|
||||
|
||||
memcpy(&test_addr_acq_ack[26], &udp_check, sizeof(udp_check));
|
||||
memcpy(&test_addr_acq_ack[32], &xid, sizeof(xid));
|
||||
memcpy(&test_addr_acq_ack[56], &mac_addr.ether_addr_octet,
|
||||
ETHER_ADDR_LEN);
|
||||
|
||||
callback_recv = NULL;
|
||||
|
||||
res = write(test_fd[1], test_addr_acq_ack,
|
||||
sizeof(test_addr_acq_ack));
|
||||
assert_se(res == sizeof(test_addr_acq_ack));
|
||||
|
||||
if (verbose)
|
||||
printf(" send DHCP Ack\n");
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int test_addr_acq_recv_discover(size_t size, DHCPMessage *discover)
|
||||
{
|
||||
uint16_t udp_check = 0;
|
||||
int res;
|
||||
|
||||
res = dhcp_option_parse(discover, size, check_options, NULL);
|
||||
assert_se(res == DHCP_DISCOVER);
|
||||
|
||||
xid = discover->xid;
|
||||
|
||||
if (verbose)
|
||||
printf(" recv DHCP Discover 0x%08x\n", be32toh(xid));
|
||||
|
||||
memcpy(&test_addr_acq_offer[26], &udp_check, sizeof(udp_check));
|
||||
memcpy(&test_addr_acq_offer[32], &xid, sizeof(xid));
|
||||
memcpy(&test_addr_acq_offer[56], &mac_addr.ether_addr_octet,
|
||||
ETHER_ADDR_LEN);
|
||||
|
||||
callback_recv = test_addr_acq_recv_request;
|
||||
|
||||
res = write(test_fd[1], test_addr_acq_offer,
|
||||
sizeof(test_addr_acq_offer));
|
||||
assert_se(res == sizeof(test_addr_acq_offer));
|
||||
|
||||
if (verbose)
|
||||
printf(" send DHCP Offer\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_addr_acq(sd_event *e)
|
||||
{
|
||||
sd_dhcp_client *client;
|
||||
int res, r;
|
||||
|
||||
if (verbose)
|
||||
printf("* %s\n", __FUNCTION__);
|
||||
|
||||
r = sd_dhcp_client_new(&client);
|
||||
assert_se(r >= 0);
|
||||
assert_se(client);
|
||||
|
||||
r = sd_dhcp_client_attach_event(client, e, 0);
|
||||
assert_se(r >= 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_index(client, 42) >= 0);
|
||||
assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0);
|
||||
|
||||
assert_se(sd_dhcp_client_set_callback(client, test_addr_acq_acquired, e)
|
||||
>= 0);
|
||||
|
||||
callback_recv = test_addr_acq_recv_discover;
|
||||
|
||||
res = sd_dhcp_client_start(client);
|
||||
assert_se(res == 0 || res == -EINPROGRESS);
|
||||
|
||||
sd_event_loop(e);
|
||||
|
||||
sd_dhcp_client_set_callback(client, NULL, NULL);
|
||||
sd_dhcp_client_stop(client);
|
||||
sd_dhcp_client_free(client);
|
||||
|
||||
close(test_fd[0]);
|
||||
close(test_fd[1]);
|
||||
|
||||
callback_recv = NULL;
|
||||
xid = 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sd_event *e;
|
||||
|
||||
assert_se(sd_event_new(&e) >= 0);
|
||||
|
||||
test_request_basic(e);
|
||||
test_checksum();
|
||||
|
||||
test_discover_message(e);
|
||||
test_addr_acq(e);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "macro.h"
|
||||
|
||||
#include "dhcp-protocol.h"
|
||||
#include "dhcp-internal.h"
|
||||
|
||||
struct option_desc {
|
||||
uint8_t sname[64];
|
||||
int snamelen;
|
||||
uint8_t file[128];
|
||||
int filelen;
|
||||
uint8_t options[128];
|
||||
int len;
|
||||
bool success;
|
||||
int filepos;
|
||||
int snamepos;
|
||||
int pos;
|
||||
};
|
||||
|
||||
static bool verbose = false;
|
||||
|
||||
static struct option_desc option_tests[] = {
|
||||
{ {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69 }, 7, false, },
|
||||
{ {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69, 0, 0,
|
||||
DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 12, true, },
|
||||
{ {}, 0, {}, 0, { 8, 255, 70, 71, 72 }, 5, false, },
|
||||
{ {}, 0, {}, 0, { 0x35, 0x01, 0x05, 0x36, 0x04, 0x01, 0x00, 0xa8,
|
||||
0xc0, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x01,
|
||||
0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0xc0,
|
||||
0xa8, 0x00, 0x01, 0x06, 0x04, 0xc0, 0xa8, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
|
||||
40, true, },
|
||||
{ {}, 0, {}, 0, { DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_OFFER,
|
||||
42, 3, 0, 0, 0 }, 8, true, },
|
||||
{ {}, 0, {}, 0, { 42, 2, 1, 2, 44 }, 5, false, },
|
||||
|
||||
{ {}, 0,
|
||||
{ 222, 3, 1, 2, 3, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_NAK }, 8,
|
||||
{ DHCP_OPTION_OVERLOAD, 1, DHCP_OVERLOAD_FILE }, 3, true, },
|
||||
|
||||
{ { 1, 4, 1, 2, 3, 4, DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 9,
|
||||
{ 222, 3, 1, 2, 3 }, 5,
|
||||
{ DHCP_OPTION_OVERLOAD, 1,
|
||||
DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
|
||||
};
|
||||
|
||||
static const char *dhcp_type(int type)
|
||||
{
|
||||
switch(type) {
|
||||
case DHCP_DISCOVER:
|
||||
return "DHCPDISCOVER";
|
||||
case DHCP_OFFER:
|
||||
return "DHCPOFFER";
|
||||
case DHCP_REQUEST:
|
||||
return "DHCPREQUEST";
|
||||
case DHCP_DECLINE:
|
||||
return "DHCPDECLINE";
|
||||
case DHCP_ACK:
|
||||
return "DHCPACK";
|
||||
case DHCP_NAK:
|
||||
return "DHCPNAK";
|
||||
case DHCP_RELEASE:
|
||||
return "DHCPRELEASE";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static void test_invalid_buffer_length(void)
|
||||
{
|
||||
DHCPMessage message;
|
||||
|
||||
assert_se(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL);
|
||||
assert_se(dhcp_option_parse(&message, sizeof(DHCPMessage), NULL, NULL)
|
||||
== -EINVAL);
|
||||
}
|
||||
|
||||
static void test_cookie(void)
|
||||
{
|
||||
_cleanup_free_ DHCPMessage *message;
|
||||
size_t len = sizeof(DHCPMessage) + 4;
|
||||
uint8_t *opt;
|
||||
|
||||
message = malloc0(len);
|
||||
|
||||
opt = (uint8_t *)(message + 1);
|
||||
opt[0] = 0xff;
|
||||
|
||||
assert_se(dhcp_option_parse(message, len, NULL, NULL) == -EINVAL);
|
||||
|
||||
opt[0] = 99;
|
||||
opt[1] = 130;
|
||||
opt[2] = 83;
|
||||
opt[3] = 99;
|
||||
|
||||
assert_se(dhcp_option_parse(message, len, NULL, NULL) == -ENOMSG);
|
||||
}
|
||||
|
||||
static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
|
||||
uint8_t *file, uint8_t filelen,
|
||||
uint8_t *sname, uint8_t snamelen)
|
||||
{
|
||||
DHCPMessage *message;
|
||||
size_t len = sizeof(DHCPMessage) + 4 + optlen;
|
||||
uint8_t *opt;
|
||||
|
||||
message = malloc0(len);
|
||||
opt = (uint8_t *)(message + 1);
|
||||
|
||||
opt[0] = 99;
|
||||
opt[1] = 130;
|
||||
opt[2] = 83;
|
||||
opt[3] = 99;
|
||||
|
||||
if (options && optlen)
|
||||
memcpy(&opt[4], options, optlen);
|
||||
|
||||
if (file && filelen <= 128)
|
||||
memcpy(&message->file, file, filelen);
|
||||
|
||||
if (sname && snamelen <= 64)
|
||||
memcpy(&message->sname, sname, snamelen);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen)
|
||||
{
|
||||
while (*descpos < *desclen) {
|
||||
switch(descoption[*descpos]) {
|
||||
case DHCP_OPTION_PAD:
|
||||
*descpos += 1;
|
||||
break;
|
||||
|
||||
case DHCP_OPTION_MESSAGE_TYPE:
|
||||
case DHCP_OPTION_OVERLOAD:
|
||||
*descpos += 3;
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int test_options_cb(uint8_t code, uint8_t len, const uint8_t *option,
|
||||
void *user_data)
|
||||
{
|
||||
struct option_desc *desc = user_data;
|
||||
uint8_t *descoption = NULL;
|
||||
int *desclen = NULL, *descpos = NULL;
|
||||
uint8_t optcode = 0;
|
||||
uint8_t optlen = 0;
|
||||
uint8_t i;
|
||||
|
||||
assert_se((!desc && !code && !len) || desc);
|
||||
|
||||
if (!desc)
|
||||
return -EINVAL;
|
||||
|
||||
assert_se(code != DHCP_OPTION_PAD);
|
||||
assert_se(code != DHCP_OPTION_END);
|
||||
assert_se(code != DHCP_OPTION_MESSAGE_TYPE);
|
||||
assert_se(code != DHCP_OPTION_OVERLOAD);
|
||||
|
||||
while (desc->pos >= 0 || desc->filepos >= 0 || desc->snamepos >= 0) {
|
||||
|
||||
if (desc->pos >= 0) {
|
||||
descoption = &desc->options[0];
|
||||
desclen = &desc->len;
|
||||
descpos = &desc->pos;
|
||||
} else if (desc->filepos >= 0) {
|
||||
descoption = &desc->file[0];
|
||||
desclen = &desc->filelen;
|
||||
descpos = &desc->filepos;
|
||||
} else if (desc->snamepos >= 0) {
|
||||
descoption = &desc->sname[0];
|
||||
desclen = &desc->snamelen;
|
||||
descpos = &desc->snamepos;
|
||||
}
|
||||
|
||||
assert_se(descoption && desclen && descpos);
|
||||
|
||||
if (*desclen)
|
||||
test_ignore_opts(descoption, descpos, desclen);
|
||||
|
||||
if (*descpos < *desclen)
|
||||
break;
|
||||
|
||||
if (*descpos == *desclen)
|
||||
*descpos = -1;
|
||||
}
|
||||
|
||||
assert_se(descpos);
|
||||
assert_se(*descpos != -1);
|
||||
|
||||
optcode = descoption[*descpos];
|
||||
optlen = descoption[*descpos + 1];
|
||||
|
||||
if (verbose)
|
||||
printf("DHCP code %2d(%2d) len %2d(%2d) ", code, optcode,
|
||||
len, optlen);
|
||||
|
||||
assert_se(code == optcode);
|
||||
assert_se(len == optlen);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
if (verbose)
|
||||
printf("0x%02x(0x%02x) ", option[i],
|
||||
descoption[*descpos + 2 + i]);
|
||||
|
||||
assert_se(option[i] == descoption[*descpos + 2 + i]);
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
printf("\n");
|
||||
|
||||
*descpos += optlen + 2;
|
||||
|
||||
test_ignore_opts(descoption, descpos, desclen);
|
||||
|
||||
if (desc->pos != -1 && desc->pos == desc->len)
|
||||
desc->pos = -1;
|
||||
|
||||
if (desc->filepos != -1 && desc->filepos == desc->filelen)
|
||||
desc->filepos = -1;
|
||||
|
||||
if (desc->snamepos != -1 && desc->snamepos == desc->snamelen)
|
||||
desc->snamepos = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_options(struct option_desc *desc)
|
||||
{
|
||||
uint8_t *options = NULL;
|
||||
uint8_t *file = NULL;
|
||||
uint8_t *sname = NULL;
|
||||
int optlen = 0;
|
||||
int filelen = 0;
|
||||
int snamelen = 0;
|
||||
int buflen = 0;
|
||||
_cleanup_free_ DHCPMessage *message;
|
||||
int res;
|
||||
|
||||
if (desc) {
|
||||
file = &desc->file[0];
|
||||
filelen = desc->filelen;
|
||||
if (!filelen)
|
||||
desc->filepos = -1;
|
||||
|
||||
sname = &desc->sname[0];
|
||||
snamelen = desc->snamelen;
|
||||
if (!snamelen)
|
||||
desc->snamepos = -1;
|
||||
|
||||
options = &desc->options[0];
|
||||
optlen = desc->len;
|
||||
desc->pos = 0;
|
||||
}
|
||||
message = create_message(options, optlen, file, filelen,
|
||||
sname, snamelen);
|
||||
|
||||
buflen = sizeof(DHCPMessage) + 4 + optlen;
|
||||
|
||||
if (!desc) {
|
||||
assert_se((res = dhcp_option_parse(message, buflen,
|
||||
test_options_cb,
|
||||
NULL)) == -ENOMSG);
|
||||
} else if (desc->success) {
|
||||
assert_se((res = dhcp_option_parse(message, buflen,
|
||||
test_options_cb,
|
||||
desc)) >= 0);
|
||||
assert_se(desc->pos == -1 && desc->filepos == -1 &&
|
||||
desc->snamepos == -1);
|
||||
} else
|
||||
assert_se((res = dhcp_option_parse(message, buflen,
|
||||
test_options_cb,
|
||||
desc)) < 0);
|
||||
|
||||
if (verbose)
|
||||
printf("DHCP type %s\n", dhcp_type(res));
|
||||
}
|
||||
|
||||
static uint8_t result[64] = {
|
||||
'A', 'B', 'C', 'D',
|
||||
};
|
||||
|
||||
static uint8_t options[64] = {
|
||||
'A', 'B', 'C', 'D',
|
||||
160, 2, 0x11, 0x12,
|
||||
0,
|
||||
31, 8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
||||
0,
|
||||
55, 3, 0x51, 0x52, 0x53,
|
||||
17, 7, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
|
||||
255
|
||||
};
|
||||
|
||||
static void test_option_set(void)
|
||||
{
|
||||
size_t len, oldlen;
|
||||
int pos, i;
|
||||
uint8_t *opt;
|
||||
|
||||
assert_se(dhcp_option_append(NULL, NULL, 0, 0, NULL) == -EINVAL);
|
||||
|
||||
len = 0;
|
||||
opt = &result[0];
|
||||
assert_se(dhcp_option_append(&opt, NULL, 0, 0, NULL) == -EINVAL);
|
||||
assert_se(opt == &result[0] && len == 0);
|
||||
|
||||
assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
|
||||
0, NULL) == -ENOBUFS);
|
||||
assert_se(opt == &result[0] && len == 0);
|
||||
|
||||
opt = &result[4];
|
||||
len = 1;
|
||||
assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
|
||||
0, NULL) >= 0);
|
||||
assert_se(opt == &result[5] && len == 0);
|
||||
|
||||
pos = 4;
|
||||
len = 60;
|
||||
while (pos < 64 && options[pos] != DHCP_OPTION_END) {
|
||||
opt = &result[pos];
|
||||
oldlen = len;
|
||||
|
||||
assert_se(dhcp_option_append(&opt, &len, options[pos],
|
||||
options[pos + 1],
|
||||
&options[pos + 2]) >= 0);
|
||||
|
||||
if (options[pos] == DHCP_OPTION_PAD) {
|
||||
assert_se(opt == &result[pos + 1]);
|
||||
assert_se(len == oldlen - 1);
|
||||
pos++;
|
||||
} else {
|
||||
assert_se(opt == &result[pos + 2 + options[pos + 1]]);
|
||||
assert_se(len == oldlen - 2 - options[pos + 1]);
|
||||
pos += 2 + options[pos + 1];
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < pos; i++) {
|
||||
if (verbose)
|
||||
printf("%2d: 0x%02x(0x%02x)\n", i, result[i],
|
||||
options[i]);
|
||||
assert_se(result[i] == options[i]);
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
test_invalid_buffer_length();
|
||||
test_cookie();
|
||||
|
||||
test_options(NULL);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(option_tests); i++)
|
||||
test_options(&option_tests[i]);
|
||||
|
||||
test_option_set();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user