From f3d84e20c9f1d8892f4f8d336041cacf0b6936c7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 21 Oct 2023 00:28:57 +0900 Subject: [PATCH 1/5] fuzz: include library headers first --- src/libsystemd-network/fuzz-dhcp-client.c | 5 ++--- src/libsystemd-network/fuzz-dhcp-server-relay.c | 4 ++-- src/libsystemd-network/fuzz-dhcp-server.c | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libsystemd-network/fuzz-dhcp-client.c b/src/libsystemd-network/fuzz-dhcp-client.c index 91e2ff7a51..18f9f3dffe 100644 --- a/src/libsystemd-network/fuzz-dhcp-client.c +++ b/src/libsystemd-network/fuzz-dhcp-client.c @@ -4,11 +4,10 @@ #include #include +#include "sd-dhcp-client.c" + #include "alloc-util.h" #include "fuzz.h" -#include "sd-event.h" - -#include "sd-dhcp-client.c" int dhcp_network_bind_raw_socket( int ifindex, diff --git a/src/libsystemd-network/fuzz-dhcp-server-relay.c b/src/libsystemd-network/fuzz-dhcp-server-relay.c index 3241d7c8da..5520003897 100644 --- a/src/libsystemd-network/fuzz-dhcp-server-relay.c +++ b/src/libsystemd-network/fuzz-dhcp-server-relay.c @@ -4,10 +4,10 @@ #include #include -#include "fuzz.h" - #include "sd-dhcp-server.c" +#include "fuzz.h" + ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { return len; } diff --git a/src/libsystemd-network/fuzz-dhcp-server.c b/src/libsystemd-network/fuzz-dhcp-server.c index a294324cde..6be2f4b02a 100644 --- a/src/libsystemd-network/fuzz-dhcp-server.c +++ b/src/libsystemd-network/fuzz-dhcp-server.c @@ -4,10 +4,10 @@ #include #include -#include "fuzz.h" - #include "sd-dhcp-server.c" +#include "fuzz.h" + /* stub out network so that the server doesn't send */ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { return len; From 8320db407cbec711f812238f589a3100ab672514 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 21 Oct 2023 00:46:39 +0900 Subject: [PATCH 2/5] dhcp: move DHCPState to dhcp-client-internal.h All other definitions in dhcp-protocol.h are common for client and server. Let's move DHCP client specific definitions. --- src/libsystemd-network/dhcp-client-internal.h | 20 +++++++++++++++++++ src/libsystemd-network/dhcp-protocol.c | 18 ----------------- src/libsystemd-network/dhcp-protocol.h | 18 ----------------- src/libsystemd-network/meson.build | 1 - src/libsystemd-network/sd-dhcp-client.c | 15 ++++++++++++++ src/network/networkd-dhcp4-bus.c | 1 - 6 files changed, 35 insertions(+), 38 deletions(-) delete mode 100644 src/libsystemd-network/dhcp-protocol.c diff --git a/src/libsystemd-network/dhcp-client-internal.h b/src/libsystemd-network/dhcp-client-internal.h index 6f43975977..073f605518 100644 --- a/src/libsystemd-network/dhcp-client-internal.h +++ b/src/libsystemd-network/dhcp-client-internal.h @@ -1,8 +1,28 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "sd-dhcp-client.h" +#include "macro.h" + +typedef enum DHCPState { + DHCP_STATE_STOPPED, + DHCP_STATE_INIT, + DHCP_STATE_SELECTING, + DHCP_STATE_INIT_REBOOT, + DHCP_STATE_REBOOTING, + DHCP_STATE_REQUESTING, + DHCP_STATE_BOUND, + DHCP_STATE_RENEWING, + DHCP_STATE_REBINDING, + _DHCP_STATE_MAX, + _DHCP_STATE_INVALID = -EINVAL, +} DHCPState; + +const char *dhcp_state_to_string(DHCPState s) _const_; + extern const struct hash_ops dhcp_option_hash_ops; int dhcp_client_set_state_callback( diff --git a/src/libsystemd-network/dhcp-protocol.c b/src/libsystemd-network/dhcp-protocol.c deleted file mode 100644 index 955d08720d..0000000000 --- a/src/libsystemd-network/dhcp-protocol.c +++ /dev/null @@ -1,18 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ - -#include "dhcp-protocol.h" -#include "string-table.h" - -static const char* const dhcp_state_table[_DHCP_STATE_MAX] = { - [DHCP_STATE_STOPPED] = "stopped", - [DHCP_STATE_INIT] = "initialization", - [DHCP_STATE_SELECTING] = "selecting", - [DHCP_STATE_INIT_REBOOT] = "init-reboot", - [DHCP_STATE_REBOOTING] = "rebooting", - [DHCP_STATE_REQUESTING] = "requesting", - [DHCP_STATE_BOUND] = "bound", - [DHCP_STATE_RENEWING] = "renewing", - [DHCP_STATE_REBINDING] = "rebinding", -}; - -DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dhcp_state, DHCPState); diff --git a/src/libsystemd-network/dhcp-protocol.h b/src/libsystemd-network/dhcp-protocol.h index 4bf2296ee3..34ab933360 100644 --- a/src/libsystemd-network/dhcp-protocol.h +++ b/src/libsystemd-network/dhcp-protocol.h @@ -59,22 +59,6 @@ enum { DHCP_PORT_CLIENT = 68, }; -enum DHCPState { - DHCP_STATE_STOPPED, - DHCP_STATE_INIT, - DHCP_STATE_SELECTING, - DHCP_STATE_INIT_REBOOT, - DHCP_STATE_REBOOTING, - DHCP_STATE_REQUESTING, - DHCP_STATE_BOUND, - DHCP_STATE_RENEWING, - DHCP_STATE_REBINDING, - _DHCP_STATE_MAX, - _DHCP_STATE_INVALID = -EINVAL, -}; - -typedef enum DHCPState DHCPState; - enum { BOOTREQUEST = 1, BOOTREPLY = 2, @@ -114,5 +98,3 @@ enum { DHCP_FQDN_FLAG_E = (1 << 2), DHCP_FQDN_FLAG_N = (1 << 3), }; - -const char *dhcp_state_to_string(DHCPState s) _const_; diff --git a/src/libsystemd-network/meson.build b/src/libsystemd-network/meson.build index 3eba57f991..93186e23a1 100644 --- a/src/libsystemd-network/meson.build +++ b/src/libsystemd-network/meson.build @@ -6,7 +6,6 @@ sources = files( 'dhcp-network.c', 'dhcp-option.c', 'dhcp-packet.c', - 'dhcp-protocol.c', 'dhcp6-network.c', 'dhcp6-option.c', 'dhcp6-protocol.c', diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index a74a239e12..011b173e88 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -30,6 +30,7 @@ #include "random-util.h" #include "set.h" #include "sort-util.h" +#include "string-table.h" #include "string-util.h" #include "strv.h" #include "time-util.h" @@ -2425,3 +2426,17 @@ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) { return 0; } + +static const char* const dhcp_state_table[_DHCP_STATE_MAX] = { + [DHCP_STATE_STOPPED] = "stopped", + [DHCP_STATE_INIT] = "initialization", + [DHCP_STATE_SELECTING] = "selecting", + [DHCP_STATE_INIT_REBOOT] = "init-reboot", + [DHCP_STATE_REBOOTING] = "rebooting", + [DHCP_STATE_REQUESTING] = "requesting", + [DHCP_STATE_BOUND] = "bound", + [DHCP_STATE_RENEWING] = "renewing", + [DHCP_STATE_REBINDING] = "rebinding", +}; + +DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dhcp_state, DHCPState); diff --git a/src/network/networkd-dhcp4-bus.c b/src/network/networkd-dhcp4-bus.c index cb88627d20..e00aa0364f 100644 --- a/src/network/networkd-dhcp4-bus.c +++ b/src/network/networkd-dhcp4-bus.c @@ -6,7 +6,6 @@ #include "bus-common-errors.h" #include "bus-util.h" #include "dhcp-client-internal.h" -#include "dhcp-protocol.h" #include "networkd-dhcp4-bus.h" #include "networkd-link-bus.h" #include "networkd-manager.h" From 9bcbb61458af5a257ddfc36d0f8d01e1ba1bb213 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 21 Oct 2023 00:26:38 +0900 Subject: [PATCH 3/5] dhcp: split out dhcp-network.h from dhcp-internal.h --- src/libsystemd-network/dhcp-internal.h | 20 ------------- src/libsystemd-network/dhcp-network.c | 4 +-- src/libsystemd-network/dhcp-network.h | 35 +++++++++++++++++++++++ src/libsystemd-network/fuzz-dhcp-client.c | 1 + src/libsystemd-network/network-internal.c | 1 + src/libsystemd-network/sd-dhcp-client.c | 1 + src/libsystemd-network/sd-dhcp-server.c | 1 + src/libsystemd-network/test-dhcp-client.c | 1 + 8 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 src/libsystemd-network/dhcp-network.h diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h index b027b6faf0..2972a808f9 100644 --- a/src/libsystemd-network/dhcp-internal.h +++ b/src/libsystemd-network/dhcp-internal.h @@ -5,17 +5,13 @@ Copyright © 2013 Intel Corporation. All rights reserved. ***/ -#include -#include #include #include "sd-dhcp-client.h" #include "dhcp-client-internal.h" #include "dhcp-protocol.h" -#include "ether-addr-util.h" #include "network-common.h" -#include "socket-util.h" typedef struct sd_dhcp_option { unsigned n_ref; @@ -32,22 +28,6 @@ typedef struct DHCPServerData { typedef struct sd_dhcp_client sd_dhcp_client; -int dhcp_network_bind_raw_socket( - int ifindex, - union sockaddr_union *link, - uint32_t xid, - const struct hw_addr_data *hw_addr, - const struct hw_addr_data *bcast_addr, - uint16_t arp_type, - uint16_t port, - bool so_priority_set, - int so_priority); -int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port, int ip_service_type); -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(DHCPMessage *message, size_t size, size_t *offset, uint8_t overload, uint8_t code, size_t optlen, const void *optval); int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t wanted_code, size_t *ret_offset); diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c index 9137efa3ee..3d3809c96b 100644 --- a/src/libsystemd-network/dhcp-network.c +++ b/src/libsystemd-network/dhcp-network.c @@ -13,9 +13,9 @@ #include #include -#include "dhcp-internal.h" +#include "dhcp-network.h" +#include "dhcp-protocol.h" #include "fd-util.h" -#include "socket-util.h" #include "unaligned.h" static int _bind_raw_socket( diff --git a/src/libsystemd-network/dhcp-network.h b/src/libsystemd-network/dhcp-network.h new file mode 100644 index 0000000000..eb9dab4453 --- /dev/null +++ b/src/libsystemd-network/dhcp-network.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include +#include + +#include "ether-addr-util.h" +#include "socket-util.h" + +int dhcp_network_bind_raw_socket( + int ifindex, + union sockaddr_union *link, + uint32_t xid, + const struct hw_addr_data *hw_addr, + const struct hw_addr_data *bcast_addr, + uint16_t arp_type, + uint16_t port, + bool so_priority_set, + int so_priority); +int dhcp_network_bind_udp_socket( + int ifindex, + be32_t address, + uint16_t port, + int ip_service_type); +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); diff --git a/src/libsystemd-network/fuzz-dhcp-client.c b/src/libsystemd-network/fuzz-dhcp-client.c index 18f9f3dffe..3e98b683ec 100644 --- a/src/libsystemd-network/fuzz-dhcp-client.c +++ b/src/libsystemd-network/fuzz-dhcp-client.c @@ -7,6 +7,7 @@ #include "sd-dhcp-client.c" #include "alloc-util.h" +#include "dhcp-network.h" #include "fuzz.h" int dhcp_network_bind_raw_socket( diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index cd32a7247a..c8aa021ee0 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -10,6 +10,7 @@ #include "dhcp-lease-internal.h" #include "extract-word.h" #include "hexdecoct.h" +#include "in-addr-util.h" #include "log.h" #include "network-internal.h" #include "parse-util.h" diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 011b173e88..dc61470afc 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -18,6 +18,7 @@ #include "dhcp-identifier.h" #include "dhcp-internal.h" #include "dhcp-lease-internal.h" +#include "dhcp-network.h" #include "dhcp-protocol.h" #include "dns-domain.h" #include "ether-addr-util.h" diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 22a03203a8..814021a2ca 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -11,6 +11,7 @@ #include "alloc-util.h" #include "dhcp-internal.h" +#include "dhcp-network.h" #include "dhcp-server-internal.h" #include "dns-domain.h" #include "fd-util.h" diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 912d4493f5..f8baf2d46d 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -19,6 +19,7 @@ #include "alloc-util.h" #include "dhcp-identifier.h" #include "dhcp-internal.h" +#include "dhcp-network.h" #include "dhcp-protocol.h" #include "ether-addr-util.h" #include "fd-util.h" From 40e4be7e8ee325c0bc140d2af71b3766d6cef4fd Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 21 Oct 2023 01:03:00 +0900 Subject: [PATCH 4/5] dhcp: move DHCP client specific definitions to dhcp-client-internal.h --- src/libsystemd-network/dhcp-client-internal.h | 20 +++++++++++++++++++ src/libsystemd-network/dhcp-internal.h | 20 ------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libsystemd-network/dhcp-client-internal.h b/src/libsystemd-network/dhcp-client-internal.h index 073f605518..cb8e3c3325 100644 --- a/src/libsystemd-network/dhcp-client-internal.h +++ b/src/libsystemd-network/dhcp-client-internal.h @@ -6,6 +6,7 @@ #include "sd-dhcp-client.h" #include "macro.h" +#include "network-common.h" typedef enum DHCPState { DHCP_STATE_STOPPED, @@ -23,6 +24,8 @@ typedef enum DHCPState { const char *dhcp_state_to_string(DHCPState s) _const_; +typedef struct sd_dhcp_client sd_dhcp_client; + extern const struct hash_ops dhcp_option_hash_ops; int dhcp_client_set_state_callback( @@ -30,3 +33,20 @@ int dhcp_client_set_state_callback( sd_dhcp_client_callback_t cb, void *userdata); int dhcp_client_get_state(sd_dhcp_client *client); + +/* If we are invoking callbacks of a dhcp-client, ensure unreffing the + * client from the callback doesn't destroy the object we are working + * on */ +#define DHCP_CLIENT_DONT_DESTROY(client) \ + _cleanup_(sd_dhcp_client_unrefp) _unused_ sd_dhcp_client *_dont_destroy_##client = sd_dhcp_client_ref(client) + +#define log_dhcp_client_errno(client, error, fmt, ...) \ + log_interface_prefix_full_errno( \ + "DHCPv4 client: ", \ + sd_dhcp_client, client, \ + error, fmt, ##__VA_ARGS__) +#define log_dhcp_client(client, fmt, ...) \ + log_interface_prefix_full_errno_zerook( \ + "DHCPv4 client: ", \ + sd_dhcp_client, client, \ + 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h index 2972a808f9..ba6090b982 100644 --- a/src/libsystemd-network/dhcp-internal.h +++ b/src/libsystemd-network/dhcp-internal.h @@ -11,7 +11,6 @@ #include "dhcp-client-internal.h" #include "dhcp-protocol.h" -#include "network-common.h" typedef struct sd_dhcp_option { unsigned n_ref; @@ -26,8 +25,6 @@ typedef struct DHCPServerData { size_t size; } DHCPServerData; -typedef struct sd_dhcp_client sd_dhcp_client; - int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset, uint8_t overload, uint8_t code, size_t optlen, const void *optval); int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t wanted_code, size_t *ret_offset); @@ -51,20 +48,3 @@ void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr, uint16_t destination, uint16_t len, int ip_service_type); int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port); - -/* If we are invoking callbacks of a dhcp-client, ensure unreffing the - * client from the callback doesn't destroy the object we are working - * on */ -#define DHCP_CLIENT_DONT_DESTROY(client) \ - _cleanup_(sd_dhcp_client_unrefp) _unused_ sd_dhcp_client *_dont_destroy_##client = sd_dhcp_client_ref(client) - -#define log_dhcp_client_errno(client, error, fmt, ...) \ - log_interface_prefix_full_errno( \ - "DHCPv4 client: ", \ - sd_dhcp_client, client, \ - error, fmt, ##__VA_ARGS__) -#define log_dhcp_client(client, fmt, ...) \ - log_interface_prefix_full_errno_zerook( \ - "DHCPv4 client: ", \ - sd_dhcp_client, client, \ - 0, fmt, ##__VA_ARGS__) From 8664ded716ac958d14141475be772ce696c12c46 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 21 Oct 2023 01:27:52 +0900 Subject: [PATCH 5/5] dhcp: split dhcp-internal.h into two --- src/libsystemd-network/dhcp-client-internal.h | 2 - src/libsystemd-network/dhcp-internal.h | 50 ------------------- src/libsystemd-network/dhcp-lease-internal.h | 3 +- src/libsystemd-network/dhcp-option.c | 3 +- src/libsystemd-network/dhcp-option.h | 46 +++++++++++++++++ src/libsystemd-network/dhcp-packet.c | 4 +- src/libsystemd-network/dhcp-packet.h | 31 ++++++++++++ src/libsystemd-network/dhcp-protocol.h | 2 + src/libsystemd-network/dhcp-server-internal.h | 2 +- src/libsystemd-network/sd-dhcp-client.c | 5 +- src/libsystemd-network/sd-dhcp-lease.c | 4 +- src/libsystemd-network/sd-dhcp-server.c | 3 +- src/libsystemd-network/test-dhcp-client.c | 4 +- src/libsystemd-network/test-dhcp-option.c | 5 +- src/network/networkd-dhcp-common.c | 2 +- 15 files changed, 98 insertions(+), 68 deletions(-) delete mode 100644 src/libsystemd-network/dhcp-internal.h create mode 100644 src/libsystemd-network/dhcp-option.h create mode 100644 src/libsystemd-network/dhcp-packet.h diff --git a/src/libsystemd-network/dhcp-client-internal.h b/src/libsystemd-network/dhcp-client-internal.h index cb8e3c3325..28ce80cbf2 100644 --- a/src/libsystemd-network/dhcp-client-internal.h +++ b/src/libsystemd-network/dhcp-client-internal.h @@ -26,8 +26,6 @@ const char *dhcp_state_to_string(DHCPState s) _const_; typedef struct sd_dhcp_client sd_dhcp_client; -extern const struct hash_ops dhcp_option_hash_ops; - int dhcp_client_set_state_callback( sd_dhcp_client *client, sd_dhcp_client_callback_t cb, diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h deleted file mode 100644 index ba6090b982..0000000000 --- a/src/libsystemd-network/dhcp-internal.h +++ /dev/null @@ -1,50 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -#pragma once - -/*** - Copyright © 2013 Intel Corporation. All rights reserved. -***/ - -#include - -#include "sd-dhcp-client.h" - -#include "dhcp-client-internal.h" -#include "dhcp-protocol.h" - -typedef struct sd_dhcp_option { - unsigned n_ref; - - uint8_t option; - void *data; - size_t length; -} sd_dhcp_option; - -typedef struct DHCPServerData { - struct in_addr *addr; - size_t size; -} DHCPServerData; - -int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset, uint8_t overload, - uint8_t code, size_t optlen, const void *optval); -int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t wanted_code, size_t *ret_offset); -int dhcp_option_remove_option(uint8_t *options, size_t buflen, uint8_t option_code); - -typedef int (*dhcp_option_callback_t)(uint8_t code, uint8_t len, - const void *option, void *userdata); - -int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t cb, void *userdata, char **ret_error_message); - -int dhcp_option_parse_string(const uint8_t *option, size_t len, char **ret); - -int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, - uint8_t type, uint16_t arp_type, uint8_t hlen, const uint8_t *chaddr, - size_t optlen, size_t *optoffset); - -uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len); - -void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr, - uint16_t source, be32_t destination_addr, - uint16_t destination, uint16_t len, int ip_service_type); - -int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port); diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h index 54facc9c65..ce1342e9fd 100644 --- a/src/libsystemd-network/dhcp-lease-internal.h +++ b/src/libsystemd-network/dhcp-lease-internal.h @@ -8,8 +8,7 @@ #include "sd-dhcp-client.h" #include "alloc-util.h" -#include "dhcp-internal.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" #include "list.h" #include "time-util.h" diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c index b60c7b443e..5e216c5139 100644 --- a/src/libsystemd-network/dhcp-option.c +++ b/src/libsystemd-network/dhcp-option.c @@ -8,9 +8,10 @@ #include #include "alloc-util.h" -#include "dhcp-internal.h" +#include "dhcp-option.h" #include "dhcp-server-internal.h" #include "memory-util.h" +#include "ordered-set.h" #include "strv.h" #include "utf8.h" diff --git a/src/libsystemd-network/dhcp-option.h b/src/libsystemd-network/dhcp-option.h new file mode 100644 index 0000000000..425f5b5016 --- /dev/null +++ b/src/libsystemd-network/dhcp-option.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +#include "sd-dhcp-option.h" + +#include "dhcp-protocol.h" +#include "hash-funcs.h" + +struct sd_dhcp_option { + unsigned n_ref; + + uint8_t option; + void *data; + size_t length; +}; + +extern const struct hash_ops dhcp_option_hash_ops; + +typedef struct DHCPServerData { + struct in_addr *addr; + size_t size; +} DHCPServerData; + +int dhcp_option_append( + DHCPMessage *message, + size_t size, + size_t *offset, + uint8_t overload, + uint8_t code, + size_t optlen, + const void *optval); +int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t wanted_code, size_t *ret_offset); +int dhcp_option_remove_option(uint8_t *options, size_t buflen, uint8_t option_code); + +typedef int (*dhcp_option_callback_t)(uint8_t code, uint8_t len, const void *option, void *userdata); + +int dhcp_option_parse( + DHCPMessage *message, + size_t len, + dhcp_option_callback_t cb, + void *userdata, + char **ret_error_message); + +int dhcp_option_parse_string(const uint8_t *option, size_t len, char **ret); diff --git a/src/libsystemd-network/dhcp-packet.c b/src/libsystemd-network/dhcp-packet.c index b2efd5246d..78eb36c733 100644 --- a/src/libsystemd-network/dhcp-packet.c +++ b/src/libsystemd-network/dhcp-packet.c @@ -8,8 +8,8 @@ #include #include -#include "dhcp-internal.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" +#include "dhcp-packet.h" #include "memory-util.h" #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312 diff --git a/src/libsystemd-network/dhcp-packet.h b/src/libsystemd-network/dhcp-packet.h new file mode 100644 index 0000000000..751321b92f --- /dev/null +++ b/src/libsystemd-network/dhcp-packet.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include +#include + +#include "dhcp-protocol.h" + +int dhcp_message_init( + DHCPMessage *message, + uint8_t op, + uint32_t xid, + uint8_t type, + uint16_t arp_type, + uint8_t hlen, + const uint8_t *chaddr, + size_t optlen, + size_t *optoffset); + +uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len); + +void dhcp_packet_append_ip_headers( + DHCPPacket *packet, + be32_t source_addr, + uint16_t source, + be32_t destination_addr, + uint16_t destination, + uint16_t len, + int ip_service_type); + +int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port); diff --git a/src/libsystemd-network/dhcp-protocol.h b/src/libsystemd-network/dhcp-protocol.h index 34ab933360..d7bb203aab 100644 --- a/src/libsystemd-network/dhcp-protocol.h +++ b/src/libsystemd-network/dhcp-protocol.h @@ -9,6 +9,8 @@ #include #include +#include "sd-dhcp-protocol.h" + #include "macro.h" #include "sparse-endian.h" #include "time-util.h" diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h index ae61cd8479..1879b5b159 100644 --- a/src/libsystemd-network/dhcp-server-internal.h +++ b/src/libsystemd-network/dhcp-server-internal.h @@ -8,7 +8,7 @@ #include "sd-dhcp-server.h" #include "sd-event.h" -#include "dhcp-internal.h" +#include "dhcp-option.h" #include "network-common.h" #include "ordered-set.h" #include "time-util.h" diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index dc61470afc..bc73e87b95 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -15,11 +15,12 @@ #include "alloc-util.h" #include "device-util.h" +#include "dhcp-client-internal.h" #include "dhcp-identifier.h" -#include "dhcp-internal.h" #include "dhcp-lease-internal.h" #include "dhcp-network.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" +#include "dhcp-packet.h" #include "dns-domain.h" #include "ether-addr-util.h" #include "event-util.h" diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index d82ab938f7..e0e8e81750 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -13,9 +13,8 @@ #include "sd-dhcp-lease.h" #include "alloc-util.h" -#include "dhcp-internal.h" #include "dhcp-lease-internal.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" #include "dns-domain.h" #include "env-file.h" #include "fd-util.h" @@ -24,6 +23,7 @@ #include "hexdecoct.h" #include "hostname-util.h" #include "in-addr-util.h" +#include "network-common.h" #include "network-internal.h" #include "parse-util.h" #include "stdio-util.h" diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 814021a2ca..54a659766d 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -10,8 +10,9 @@ #include "sd-id128.h" #include "alloc-util.h" -#include "dhcp-internal.h" #include "dhcp-network.h" +#include "dhcp-option.h" +#include "dhcp-packet.h" #include "dhcp-server-internal.h" #include "dns-domain.h" #include "fd-util.h" diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index f8baf2d46d..e3f148daf5 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -18,9 +18,9 @@ #include "alloc-util.h" #include "dhcp-identifier.h" -#include "dhcp-internal.h" #include "dhcp-network.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" +#include "dhcp-packet.h" #include "ether-addr-util.h" #include "fd-util.h" #include "random-util.h" diff --git a/src/libsystemd-network/test-dhcp-option.c b/src/libsystemd-network/test-dhcp-option.c index 82c5cdb67a..47d799348f 100644 --- a/src/libsystemd-network/test-dhcp-option.c +++ b/src/libsystemd-network/test-dhcp-option.c @@ -7,8 +7,9 @@ #include #include "alloc-util.h" -#include "dhcp-internal.h" -#include "dhcp-protocol.h" +#include "dhcp-option.h" +#include "dhcp-packet.h" +#include "ether-addr-util.h" #include "macro.h" #include "memory-util.h" diff --git a/src/network/networkd-dhcp-common.c b/src/network/networkd-dhcp-common.c index ff0ee717c5..cfc7a388e8 100644 --- a/src/network/networkd-dhcp-common.c +++ b/src/network/networkd-dhcp-common.c @@ -6,7 +6,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "dhcp-identifier.h" -#include "dhcp-client-internal.h" +#include "dhcp-option.h" #include "dhcp6-internal.h" #include "escape.h" #include "hexdecoct.h"