From 61a9fa8f0cb6eb75a0d5add82b1f5e9eeb932222 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 21 Feb 2021 23:19:26 +0900 Subject: [PATCH 1/3] libsystemd-network: introduce sd_xxx_{set,get}_ifname() --- src/libsystemd-network/dhcp-server-internal.h | 1 + src/libsystemd-network/lldp-internal.h | 1 + src/libsystemd-network/meson.build | 2 + src/libsystemd-network/ndisc-internal.h | 5 ++- src/libsystemd-network/network-common.c | 24 +++++++++++ src/libsystemd-network/network-common.h | 4 ++ src/libsystemd-network/radv-internal.h | 1 + src/libsystemd-network/sd-dhcp-client.c | 20 +++++++++ src/libsystemd-network/sd-dhcp-server.c | 41 ++++++++++++++----- src/libsystemd-network/sd-dhcp6-client.c | 20 +++++++++ src/libsystemd-network/sd-ipv4acd.c | 24 ++++++----- src/libsystemd-network/sd-ipv4ll.c | 7 ++++ src/libsystemd-network/sd-lldp.c | 19 +++++++++ src/libsystemd-network/sd-ndisc.c | 19 +++++++++ src/libsystemd-network/sd-radv.c | 19 +++++++++ src/systemd/sd-dhcp-client.h | 4 ++ src/systemd/sd-dhcp-server.h | 3 ++ src/systemd/sd-dhcp6-client.h | 4 ++ src/systemd/sd-ipv4acd.h | 1 + src/systemd/sd-ipv4ll.h | 1 + src/systemd/sd-lldp.h | 2 + src/systemd/sd-ndisc.h | 2 + src/systemd/sd-radv.h | 2 + 23 files changed, 204 insertions(+), 22 deletions(-) create mode 100644 src/libsystemd-network/network-common.c create mode 100644 src/libsystemd-network/network-common.h diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h index b5293c3ed6..c935f068f1 100644 --- a/src/libsystemd-network/dhcp-server-internal.h +++ b/src/libsystemd-network/dhcp-server-internal.h @@ -48,6 +48,7 @@ struct sd_dhcp_server { int fd_raw; int ifindex; + char *ifname; be32_t address; be32_t netmask; be32_t subnet; diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h index daedbb088f..7ecedc61ec 100644 --- a/src/libsystemd-network/lldp-internal.h +++ b/src/libsystemd-network/lldp-internal.h @@ -12,6 +12,7 @@ struct sd_lldp { unsigned n_ref; int ifindex; + char *ifname; int fd; sd_event *event; diff --git a/src/libsystemd-network/meson.build b/src/libsystemd-network/meson.build index 6bc4a3f54b..ca3393eff3 100644 --- a/src/libsystemd-network/meson.build +++ b/src/libsystemd-network/meson.build @@ -16,6 +16,8 @@ sources = files(''' sd-ipv4acd.c arp-util.h arp-util.c + network-common.c + network-common.h network-internal.c network-internal.h sd-ndisc.c diff --git a/src/libsystemd-network/ndisc-internal.h b/src/libsystemd-network/ndisc-internal.h index 70b254867b..ca90d5c539 100644 --- a/src/libsystemd-network/ndisc-internal.h +++ b/src/libsystemd-network/ndisc-internal.h @@ -5,11 +5,11 @@ Copyright © 2014 Intel Corporation. All rights reserved. ***/ +#include "sd-ndisc.h" + #include "log.h" #include "time-util.h" -#include "sd-ndisc.h" - #define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC) #define NDISC_MAX_ROUTER_SOLICITATION_INTERVAL (3600U * USEC_PER_SEC) #define NDISC_MAX_ROUTER_SOLICITATIONS 3U @@ -18,6 +18,7 @@ struct sd_ndisc { unsigned n_ref; int ifindex; + char *ifname; int fd; sd_event *event; diff --git a/src/libsystemd-network/network-common.c b/src/libsystemd-network/network-common.c new file mode 100644 index 0000000000..9bc0da96cd --- /dev/null +++ b/src/libsystemd-network/network-common.c @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "format-util.h" +#include "network-common.h" +#include "string-util.h" + +const char *get_ifname(int ifindex, char **ifname) { + char buf[IF_NAMESIZE + 1]; + + assert(ifname); + + /* This sets ifname only when it is not set yet. */ + + if (*ifname) + return *ifname; + + if (ifindex <= 0) + return NULL; + + if (!format_ifname(ifindex, buf)) + return NULL; + + return *ifname = strdup(buf); +} diff --git a/src/libsystemd-network/network-common.h b/src/libsystemd-network/network-common.h new file mode 100644 index 0000000000..76a6c4a989 --- /dev/null +++ b/src/libsystemd-network/network-common.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +const char *get_ifname(int ifindex, char **ifname); diff --git a/src/libsystemd-network/radv-internal.h b/src/libsystemd-network/radv-internal.h index 3dbeffe0c6..7668224e80 100644 --- a/src/libsystemd-network/radv-internal.h +++ b/src/libsystemd-network/radv-internal.h @@ -41,6 +41,7 @@ struct sd_radv { RAdvState state; int ifindex; + char *ifname; sd_event *event; int event_priority; diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index dd9cbf6ea5..bf740d01ce 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -24,6 +24,7 @@ #include "hostname-util.h" #include "io-util.h" #include "memory-util.h" +#include "network-common.h" #include "random-util.h" #include "set.h" #include "sort-util.h" @@ -76,6 +77,7 @@ struct sd_dhcp_client { int event_priority; sd_event_source *timeout_resend; int ifindex; + char *ifname; int fd; uint16_t port; union sockaddr_union link; @@ -282,6 +284,23 @@ int sd_dhcp_client_set_ifindex(sd_dhcp_client *client, int ifindex) { return 0; } +int sd_dhcp_client_set_ifname(sd_dhcp_client *client, const char *ifname) { + assert_return(client, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&client->ifname, ifname); +} + +const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client) { + if (!client) + return NULL; + + return get_ifname(client->ifindex, &client->ifname); +} + int sd_dhcp_client_set_mac( sd_dhcp_client *client, const uint8_t *addr, @@ -2205,6 +2224,7 @@ static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) { client->user_class = strv_free(client->user_class); ordered_hashmap_free(client->extra_options); ordered_hashmap_free(client->vendor_options); + free(client->ifname); return mfree(client); } diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index d0e249b104..94b0b572e8 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -15,6 +15,7 @@ #include "fd-util.h" #include "in-addr-util.h" #include "io-util.h" +#include "network-common.h" #include "ordered-set.h" #include "siphash24.h" #include "string-util.h" @@ -158,6 +159,8 @@ static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) { ordered_set_free(server->vendor_options); free(server->bound_leases); + + free(server->ifname); return mfree(server); } @@ -169,29 +172,47 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) { assert_return(ret, -EINVAL); assert_return(ifindex > 0, -EINVAL); - server = new0(sd_dhcp_server, 1); + server = new(sd_dhcp_server, 1); if (!server) return -ENOMEM; - server->n_ref = 1; - server->fd_raw = -1; - server->fd = -1; - server->address = htobe32(INADDR_ANY); - server->netmask = htobe32(INADDR_ANY); - server->ifindex = ifindex; + *server = (sd_dhcp_server) { + .n_ref = 1, + .fd_raw = -1, + .fd = -1, + .address = htobe32(INADDR_ANY), + .netmask = htobe32(INADDR_ANY), + .ifindex = ifindex, + .default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC), + .max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC), + }; server->leases_by_client_id = hashmap_new(&dhcp_lease_hash_ops); if (!server->leases_by_client_id) return -ENOMEM; - server->default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC); - server->max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC); - *ret = TAKE_PTR(server); return 0; } +int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname) { + assert_return(server, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&server->ifname, ifname); +} + +const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server) { + if (!server) + return NULL; + + return get_ifname(server->ifindex, &server->ifname); +} + int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int64_t priority) { int r; diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 410bfda10e..f92f517029 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -21,6 +21,7 @@ #include "hexdecoct.h" #include "hostname-util.h" #include "in-addr-util.h" +#include "network-common.h" #include "random-util.h" #include "socket-util.h" #include "string-table.h" @@ -47,6 +48,7 @@ struct sd_dhcp6_client { sd_event *event; int event_priority; int ifindex; + char *ifname; DHCP6Address hint_pd_prefix; struct in6_addr local_address; uint8_t mac_addr[MAX_MAC_ADDR_LEN]; @@ -165,6 +167,23 @@ int sd_dhcp6_client_set_ifindex(sd_dhcp6_client *client, int ifindex) { return 0; } +int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) { + assert_return(client, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&client->ifname, ifname); +} + +const char *sd_dhcp6_client_get_ifname(sd_dhcp6_client *client) { + if (!client) + return NULL; + + return get_ifname(client->ifindex, &client->ifname); +} + int sd_dhcp6_client_set_local_address( sd_dhcp6_client *client, const struct in6_addr *local_address) { @@ -1787,6 +1806,7 @@ static sd_dhcp6_client *dhcp6_client_free(sd_dhcp6_client *client) { ordered_hashmap_free(client->extra_options); strv_free(client->user_class); strv_free(client->vendor_class); + free(client->ifname); return mfree(client); } diff --git a/src/libsystemd-network/sd-ipv4acd.c b/src/libsystemd-network/sd-ipv4acd.c index defd23d85a..2a85afd58b 100644 --- a/src/libsystemd-network/sd-ipv4acd.c +++ b/src/libsystemd-network/sd-ipv4acd.c @@ -16,9 +16,9 @@ #include "ether-addr-util.h" #include "event-util.h" #include "fd-util.h" -#include "format-util.h" #include "in-addr-util.h" #include "log-link.h" +#include "network-common.h" #include "random-util.h" #include "siphash24.h" #include "string-table.h" @@ -56,7 +56,7 @@ struct sd_ipv4acd { int ifindex; int fd; - char ifname[IF_NAMESIZE + 1]; + char *ifname; unsigned n_iteration; unsigned n_conflict; @@ -125,7 +125,7 @@ static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) { ipv4acd_reset(acd); sd_ipv4acd_detach_event(acd); - + free(acd->ifname); return mfree(acd); } @@ -398,16 +398,10 @@ fail: } int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int ifindex) { - char ifname[IF_NAMESIZE + 1]; - assert_return(acd, -EINVAL); assert_return(ifindex > 0, -EINVAL); assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY); - if (!format_ifname(ifindex, ifname)) - return -ENODEV; - - strcpy(acd->ifname, ifname); acd->ifindex = ifindex; return 0; @@ -420,11 +414,21 @@ int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd) { return acd->ifindex; } +int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *ifname) { + assert_return(acd, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&acd->ifname, ifname); +} + const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd) { if (!acd) return NULL; - return empty_to_null(acd->ifname); + return get_ifname(acd->ifindex, &acd->ifname); } int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) { diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index a83c9b06de..25fa4ab90b 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -112,6 +112,13 @@ int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll) { return sd_ipv4acd_get_ifindex(ll->acd); } +int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *ifname) { + assert_return(ll, -EINVAL); + assert_return(ifname, -EINVAL); + + return sd_ipv4acd_set_ifname(ll->acd, ifname); +} + const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll) { if (!ll) return NULL; diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c index d3848ebf91..1298fea9ff 100644 --- a/src/libsystemd-network/sd-lldp.c +++ b/src/libsystemd-network/sd-lldp.c @@ -14,6 +14,7 @@ #include "lldp-neighbor.h" #include "lldp-network.h" #include "memory-util.h" +#include "network-common.h" #include "socket-util.h" #include "sort-util.h" #include "string-table.h" @@ -343,6 +344,23 @@ _public_ int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex) { return 0; } +int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname) { + assert_return(lldp, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&lldp->ifname, ifname); +} + +const char *sd_lldp_get_ifname(sd_lldp *lldp) { + if (!lldp) + return NULL; + + return get_ifname(lldp->ifindex, &lldp->ifname); +} + static sd_lldp* lldp_free(sd_lldp *lldp) { assert(lldp); @@ -354,6 +372,7 @@ static sd_lldp* lldp_free(sd_lldp *lldp) { hashmap_free(lldp->neighbor_by_id); prioq_free(lldp->neighbor_by_expiry); + free(lldp->ifname); return mfree(lldp); } diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index 8f04001755..8a5f341615 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -16,6 +16,7 @@ #include "memory-util.h" #include "ndisc-internal.h" #include "ndisc-router.h" +#include "network-common.h" #include "random-util.h" #include "socket-util.h" #include "string-table.h" @@ -65,6 +66,23 @@ _public_ int sd_ndisc_set_ifindex(sd_ndisc *nd, int ifindex) { return 0; } +int sd_ndisc_set_ifname(sd_ndisc *nd, const char *ifname) { + assert_return(nd, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&nd->ifname, ifname); +} + +const char *sd_ndisc_get_ifname(sd_ndisc *nd) { + if (!nd) + return NULL; + + return get_ifname(nd->ifindex, &nd->ifname); +} + _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) { assert_return(nd, -EINVAL); @@ -129,6 +147,7 @@ static sd_ndisc *ndisc_free(sd_ndisc *nd) { ndisc_reset(nd); sd_ndisc_detach_event(nd); + free(nd->ifname); return mfree(nd); } diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 164b24c684..531ab7bcd2 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -19,6 +19,7 @@ #include "io-util.h" #include "macro.h" #include "memory-util.h" +#include "network-common.h" #include "radv-internal.h" #include "random-util.h" #include "socket-util.h" @@ -122,6 +123,7 @@ static sd_radv *radv_free(sd_radv *ra) { sd_radv_detach_event(ra); ra->fd = safe_close(ra->fd); + free(ra->ifname); return mfree(ra); } @@ -432,6 +434,23 @@ _public_ int sd_radv_set_ifindex(sd_radv *ra, int ifindex) { return 0; } +int sd_radv_set_ifname(sd_radv *ra, const char *ifname) { + assert_return(ra, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&ra->ifname, ifname); +} + +const char *sd_radv_get_ifname(sd_radv *ra) { + if (!ra) + return NULL; + + return get_ifname(ra->ifindex, &ra->ifname); +} + _public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) { assert_return(ra, -EINVAL); diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 822286919e..c784cbcb9a 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -125,6 +125,10 @@ int sd_dhcp_client_set_request_broadcast( int sd_dhcp_client_set_ifindex( sd_dhcp_client *client, int interface_index); +int sd_dhcp_client_set_ifname( + sd_dhcp_client *client, + const char *interface_name); +const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client); int sd_dhcp_client_set_mac( sd_dhcp_client *client, const uint8_t *addr, diff --git a/src/systemd/sd-dhcp-server.h b/src/systemd/sd-dhcp-server.h index aa49a7434c..e3097ebb31 100644 --- a/src/systemd/sd-dhcp-server.h +++ b/src/systemd/sd-dhcp-server.h @@ -37,6 +37,9 @@ enum { int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex); +int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname); +const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server); + sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server); sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server); diff --git a/src/systemd/sd-dhcp6-client.h b/src/systemd/sd-dhcp6-client.h index 84e3170130..e02d67632a 100644 --- a/src/systemd/sd-dhcp6-client.h +++ b/src/systemd/sd-dhcp6-client.h @@ -91,6 +91,10 @@ int sd_dhcp6_client_set_callback( int sd_dhcp6_client_set_ifindex( sd_dhcp6_client *client, int interface_index); +int sd_dhcp6_client_set_ifname( + sd_dhcp6_client *client, + const char *interface_name); +const char * sd_dhcp6_client_get_ifname(sd_dhcp6_client *client); int sd_dhcp6_client_set_local_address( sd_dhcp6_client *client, const struct in6_addr *local_address); diff --git a/src/systemd/sd-ipv4acd.h b/src/systemd/sd-ipv4acd.h index 2809d8748b..1e89a81b31 100644 --- a/src/systemd/sd-ipv4acd.h +++ b/src/systemd/sd-ipv4acd.h @@ -44,6 +44,7 @@ int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *use int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr); int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index); int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd); +int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name); const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd); int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address); int sd_ipv4acd_is_running(sd_ipv4acd *acd); diff --git a/src/systemd/sd-ipv4ll.h b/src/systemd/sd-ipv4ll.h index aa4d174e4b..bf5596ab61 100644 --- a/src/systemd/sd-ipv4ll.h +++ b/src/systemd/sd-ipv4ll.h @@ -44,6 +44,7 @@ int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdat int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr); int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index); int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll); +int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name); const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll); int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address); int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed); diff --git a/src/systemd/sd-lldp.h b/src/systemd/sd-lldp.h index d1d520afbd..c867a88590 100644 --- a/src/systemd/sd-lldp.h +++ b/src/systemd/sd-lldp.h @@ -147,6 +147,8 @@ sd_event *sd_lldp_get_event(sd_lldp *lldp); int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata); int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex); +int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname); +const char *sd_lldp_get_ifname(sd_lldp *lldp); /* Controls how much and what to store in the neighbors database */ int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t n); diff --git a/src/systemd/sd-ndisc.h b/src/systemd/sd-ndisc.h index 49b127c018..6088def1b6 100644 --- a/src/systemd/sd-ndisc.h +++ b/src/systemd/sd-ndisc.h @@ -78,6 +78,8 @@ sd_event *sd_ndisc_get_event(sd_ndisc *nd); int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata); int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index); +int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name); +const char *sd_ndisc_get_ifname(sd_ndisc *nd); int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr); int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *ret); diff --git a/src/systemd/sd-radv.h b/src/systemd/sd-radv.h index 3f6c149d5e..be5cf1b94e 100644 --- a/src/systemd/sd-radv.h +++ b/src/systemd/sd-radv.h @@ -53,6 +53,8 @@ int sd_radv_stop(sd_radv *ra); int sd_radv_is_running(sd_radv *ra); int sd_radv_set_ifindex(sd_radv *ra, int interface_index); +int sd_radv_set_ifname(sd_radv *ra, const char *interface_name); +const char *sd_radv_get_ifname(sd_radv *ra); int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr); int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu); int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit); From 353887831b20d0fbd500da9e9f30b048e391305f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 24 Feb 2021 18:18:35 +0900 Subject: [PATCH 2/3] libsystemd-network: make log_dhcp_client() or friends include interface name --- src/libsystemd-network/dhcp-internal.h | 15 +- src/libsystemd-network/dhcp-server-internal.h | 18 ++- src/libsystemd-network/dhcp6-internal.h | 28 ++-- src/libsystemd-network/dhcp6-option.c | 82 +++++----- src/libsystemd-network/lldp-internal.h | 18 ++- src/libsystemd-network/lldp-neighbor.c | 149 ++++++++---------- src/libsystemd-network/ndisc-internal.h | 18 ++- src/libsystemd-network/ndisc-router.c | 107 ++++++------- src/libsystemd-network/ndisc-router.h | 2 +- src/libsystemd-network/radv-internal.h | 17 +- src/libsystemd-network/sd-dhcp6-client.c | 4 +- src/libsystemd-network/sd-dhcp6-lease.c | 13 +- src/libsystemd-network/sd-ipv4acd.c | 10 +- src/libsystemd-network/sd-ipv4ll.c | 10 +- src/libsystemd-network/sd-lldp.c | 41 ++--- src/libsystemd-network/sd-ndisc.c | 36 ++--- src/libsystemd-network/sd-radv.c | 58 ++++--- src/libsystemd-network/test-dhcp6-client.c | 18 +-- 18 files changed, 342 insertions(+), 302 deletions(-) diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h index 40e6b1f26f..c5c851c575 100644 --- a/src/libsystemd-network/dhcp-internal.h +++ b/src/libsystemd-network/dhcp-internal.h @@ -12,6 +12,7 @@ #include "sd-dhcp-client.h" #include "dhcp-protocol.h" +#include "log-link.h" #include "socket-util.h" typedef struct sd_dhcp_option { @@ -65,5 +66,15 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, ui #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_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "DHCP CLIENT (0x%x): " fmt, client->xid, ##__VA_ARGS__) -#define log_dhcp_client(client, fmt, ...) log_dhcp_client_errno(client, 0, fmt, ##__VA_ARGS__) +#define log_dhcp_client_errno(client, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_dhcp_client_get_ifname(client), \ + LOG_DEBUG, _e, "DHCPv4 client: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_dhcp_client(client, fmt, ...) \ + log_dhcp_client_errno(client, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h index c935f068f1..33e236627f 100644 --- a/src/libsystemd-network/dhcp-server-internal.h +++ b/src/libsystemd-network/dhcp-server-internal.h @@ -10,7 +10,7 @@ #include "dhcp-internal.h" #include "ordered-set.h" -#include "log.h" +#include "log-link.h" #include "time-util.h" typedef enum DHCPRawOption { @@ -86,9 +86,6 @@ typedef struct DHCPRequest { uint32_t lifetime; } DHCPRequest; -#define log_dhcp_server(client, fmt, ...) log_internal(LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__, "DHCP SERVER: " fmt, ##__VA_ARGS__) -#define log_dhcp_server_errno(client, error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "DHCP SERVER: " fmt, ##__VA_ARGS__) - int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message, size_t length); int dhcp_server_send_packet(sd_dhcp_server *server, @@ -97,3 +94,16 @@ int dhcp_server_send_packet(sd_dhcp_server *server, void client_id_hash_func(const DHCPClientId *p, struct siphash *state); int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b); + +#define log_dhcp_server_errno(server, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_dhcp_server_get_ifname(server), \ + LOG_DEBUG, _e, "DHCPv4 server: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_dhcp_server(server, fmt, ...) \ + log_dhcp_server_errno(server, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/dhcp6-internal.h b/src/libsystemd-network/dhcp6-internal.h index 681c462315..274b14b056 100644 --- a/src/libsystemd-network/dhcp6-internal.h +++ b/src/libsystemd-network/dhcp6-internal.h @@ -9,9 +9,11 @@ #include #include "sd-event.h" +#include "sd-dhcp6-client.h" -#include "list.h" #include "hashmap.h" +#include "list.h" +#include "log-link.h" #include "macro.h" #include "sparse-endian.h" @@ -78,7 +80,7 @@ struct ia_ta { be32_t id; } _packed_; -struct DHCP6IA { +typedef struct DHCP6IA { uint16_t type; union { struct ia_na ia_na; @@ -87,12 +89,7 @@ struct DHCP6IA { }; LIST_HEAD(DHCP6Address, addresses); -}; - -typedef struct DHCP6IA DHCP6IA; - -#define log_dhcp6_client_errno(p, error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "DHCPv6 CLIENT: " fmt, ##__VA_ARGS__) -#define log_dhcp6_client(p, fmt, ...) log_dhcp6_client_errno(p, 0, fmt, ##__VA_ARGS__) +} DHCP6IA; int dhcp6_option_append(uint8_t **buf, size_t *buflen, uint16_t code, size_t optlen, const void *optval); @@ -105,7 +102,7 @@ int dhcp6_option_append_vendor_option(uint8_t **buf, size_t *buflen, OrderedHash int dhcp6_option_parse(uint8_t **buf, size_t *buflen, uint16_t *optcode, size_t *optlen, uint8_t **optvalue); int dhcp6_option_parse_status(DHCP6Option *option, size_t len); -int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_status_code); +int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_status_code); int dhcp6_option_parse_ip6addrs(uint8_t *optval, uint16_t optlen, struct in6_addr **addrs, size_t count, size_t *allocated); @@ -121,3 +118,16 @@ const char *dhcp6_message_type_to_string(int s) _const_; int dhcp6_message_type_from_string(const char *s) _pure_; const char *dhcp6_message_status_to_string(int s) _const_; int dhcp6_message_status_from_string(const char *s) _pure_; + +#define log_dhcp6_client_errno(client, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_dhcp6_client_get_ifname(client), \ + LOG_DEBUG, _e, "DHCPv6 client: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_dhcp6_client(client, fmt, ...) \ + log_dhcp6_client_errno(client, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index 9314016024..cf3c7c287a 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -425,7 +425,7 @@ int dhcp6_option_parse_status(DHCP6Option *option, size_t len) { return be16toh(statusopt->status); } -static int dhcp6_option_parse_address(DHCP6Option *option, DHCP6IA *ia, uint32_t *ret_lifetime_valid) { +static int dhcp6_option_parse_address(sd_dhcp6_client *client, DHCP6Option *option, DHCP6IA *ia, uint32_t *ret_lifetime_valid) { DHCP6AddressOption *addr_option = (DHCP6AddressOption *)option; DHCP6Address *addr; uint32_t lt_valid, lt_pref; @@ -437,23 +437,20 @@ static int dhcp6_option_parse_address(DHCP6Option *option, DHCP6IA *ia, uint32_t lt_valid = be32toh(addr_option->iaaddr.lifetime_valid); lt_pref = be32toh(addr_option->iaaddr.lifetime_preferred); - if (lt_valid == 0 || lt_pref > lt_valid) { - log_dhcp6_client(client, - "Valid lifetime of an IA address is zero or " - "preferred lifetime %"PRIu32" > valid lifetime %"PRIu32, - lt_pref, lt_valid); - return -EINVAL; - } + if (lt_valid == 0 || lt_pref > lt_valid) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "Valid lifetime of an IA address is zero or " + "preferred lifetime %"PRIu32" > valid lifetime %"PRIu32, + lt_pref, lt_valid); if (be16toh(option->len) + offsetof(DHCP6Option, data) > sizeof(*addr_option)) { r = dhcp6_option_parse_status((DHCP6Option *)addr_option->options, be16toh(option->len) + offsetof(DHCP6Option, data) - sizeof(*addr_option)); if (r < 0) return r; - if (r > 0) { - log_dhcp6_client(client, "Non-zero status code '%s' for address is received", - dhcp6_message_status_to_string(r)); - return -EINVAL; - } + if (r > 0) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "Non-zero status code '%s' for address is received", + dhcp6_message_status_to_string(r)); } addr = new0(DHCP6Address, 1); @@ -470,7 +467,7 @@ static int dhcp6_option_parse_address(DHCP6Option *option, DHCP6IA *ia, uint32_t return 0; } -static int dhcp6_option_parse_pdprefix(DHCP6Option *option, DHCP6IA *ia, uint32_t *ret_lifetime_valid) { +static int dhcp6_option_parse_pdprefix(sd_dhcp6_client *client, DHCP6Option *option, DHCP6IA *ia, uint32_t *ret_lifetime_valid) { DHCP6PDPrefixOption *pdprefix_option = (DHCP6PDPrefixOption *)option; DHCP6Address *prefix; uint32_t lt_valid, lt_pref; @@ -482,23 +479,20 @@ static int dhcp6_option_parse_pdprefix(DHCP6Option *option, DHCP6IA *ia, uint32_ lt_valid = be32toh(pdprefix_option->iapdprefix.lifetime_valid); lt_pref = be32toh(pdprefix_option->iapdprefix.lifetime_preferred); - if (lt_valid == 0 || lt_pref > lt_valid) { - log_dhcp6_client(client, - "Valid lifetieme of a PD prefix is zero or " - "preferred lifetime %"PRIu32" > valid lifetime %"PRIu32, - lt_pref, lt_valid); - return -EINVAL; - } + if (lt_valid == 0 || lt_pref > lt_valid) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "Valid lifetieme of a PD prefix is zero or " + "preferred lifetime %"PRIu32" > valid lifetime %"PRIu32, + lt_pref, lt_valid); if (be16toh(option->len) + offsetof(DHCP6Option, data) > sizeof(*pdprefix_option)) { r = dhcp6_option_parse_status((DHCP6Option *)pdprefix_option->options, be16toh(option->len) + offsetof(DHCP6Option, data) - sizeof(*pdprefix_option)); if (r < 0) return r; - if (r > 0) { - log_dhcp6_client(client, "Non-zero status code '%s' for PD prefix is received", - dhcp6_message_status_to_string(r)); - return -EINVAL; - } + if (r > 0) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "Non-zero status code '%s' for PD prefix is received", + dhcp6_message_status_to_string(r)); } prefix = new0(DHCP6Address, 1); @@ -515,7 +509,7 @@ static int dhcp6_option_parse_pdprefix(DHCP6Option *option, DHCP6IA *ia, uint32_ return 0; } -int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_status_code) { +int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_status_code) { uint32_t lt_t1, lt_t2, lt_valid = 0, lt_min = UINT32_MAX; uint16_t iatype, optlen; size_t iaaddr_offset; @@ -541,10 +535,10 @@ int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_stat lt_t1 = be32toh(ia->ia_na.lifetime_t1); lt_t2 = be32toh(ia->ia_na.lifetime_t2); - if (lt_t1 && lt_t2 && lt_t1 > lt_t2) { - log_dhcp6_client(client, "IA NA T1 %"PRIu32"sec > T2 %"PRIu32"sec", lt_t1, lt_t2); - return -EINVAL; - } + if (lt_t1 && lt_t2 && lt_t1 > lt_t2) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "IA NA T1 %"PRIu32"sec > T2 %"PRIu32"sec", + lt_t1, lt_t2); break; @@ -559,10 +553,10 @@ int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_stat lt_t1 = be32toh(ia->ia_pd.lifetime_t1); lt_t2 = be32toh(ia->ia_pd.lifetime_t2); - if (lt_t1 && lt_t2 && lt_t1 > lt_t2) { - log_dhcp6_client(client, "IA PD T1 %"PRIu32"sec > T2 %"PRIu32"sec", lt_t1, lt_t2); - return -EINVAL; - } + if (lt_t1 && lt_t2 && lt_t1 > lt_t2) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "IA PD T1 %"PRIu32"sec > T2 %"PRIu32"sec", + lt_t1, lt_t2); break; @@ -594,12 +588,11 @@ int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_stat switch (opt) { case SD_DHCP6_OPTION_IAADDR: - if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_NA, SD_DHCP6_OPTION_IA_TA)) { - log_dhcp6_client(client, "IA Address option not in IA NA or TA option"); - return -EINVAL; - } + if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_NA, SD_DHCP6_OPTION_IA_TA)) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "IA Address option not in IA NA or TA option"); - r = dhcp6_option_parse_address(option, ia, <_valid); + r = dhcp6_option_parse_address(client, option, ia, <_valid); if (r < 0 && r != -EINVAL) return r; if (r >= 0 && lt_valid < lt_min) @@ -609,12 +602,11 @@ int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_stat case SD_DHCP6_OPTION_IA_PD_PREFIX: - if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_PD)) { - log_dhcp6_client(client, "IA PD Prefix option not in IA PD option"); - return -EINVAL; - } + if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_PD)) + return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), + "IA PD Prefix option not in IA PD option"); - r = dhcp6_option_parse_pdprefix(option, ia, <_valid); + r = dhcp6_option_parse_pdprefix(client, option, ia, <_valid); if (r < 0 && r != -EINVAL) return r; if (r >= 0 && lt_valid < lt_min) diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h index 7ecedc61ec..f13555d35c 100644 --- a/src/libsystemd-network/lldp-internal.h +++ b/src/libsystemd-network/lldp-internal.h @@ -5,7 +5,7 @@ #include "sd-lldp.h" #include "hashmap.h" -#include "log.h" +#include "log-link.h" #include "prioq.h" struct sd_lldp { @@ -33,8 +33,18 @@ struct sd_lldp { struct ether_addr filter_address; }; -#define log_lldp_errno(error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "LLDP: " fmt, ##__VA_ARGS__) -#define log_lldp(fmt, ...) log_lldp_errno(0, fmt, ##__VA_ARGS__) - const char* lldp_event_to_string(sd_lldp_event_t e) _const_; sd_lldp_event_t lldp_event_from_string(const char *s) _pure_; + +#define log_lldp_errno(lldp, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_lldp_get_ifname(lldp), \ + LOG_DEBUG, _e, "LLDP: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_lldp(lldp, fmt, ...) \ + log_lldp_errno(lldp, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c index 546ae1c980..3bd775158e 100644 --- a/src/libsystemd-network/lldp-neighbor.c +++ b/src/libsystemd-network/lldp-neighbor.c @@ -112,7 +112,7 @@ sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size) { return n; } -static int parse_string(char **s, const void *q, size_t n) { +static int parse_string(sd_lldp *lldp, char **s, const void *q, size_t n) { const char *p = q; char *k; @@ -120,7 +120,7 @@ static int parse_string(char **s, const void *q, size_t n) { assert(p || n == 0); if (*s) { - log_lldp("Found duplicate string, ignoring field."); + log_lldp(lldp, "Found duplicate string, ignoring field."); return 0; } @@ -133,14 +133,14 @@ static int parse_string(char **s, const void *q, size_t n) { /* Look for inner NULs */ if (memchr(p, 0, n)) { - log_lldp("Found inner NUL in string, ignoring field."); + log_lldp(lldp, "Found inner NUL in string, ignoring field."); return 0; } /* Let's escape weird chars, for security reasons */ k = cescape_length(p, n); if (!k) - return -ENOMEM; + return log_oom_debug(); free(*s); *s = k; @@ -156,27 +156,24 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) { assert(n); - if (n->raw_size < sizeof(struct ether_header)) { - log_lldp("Received truncated packet, ignoring."); - return -EBADMSG; - } + if (n->raw_size < sizeof(struct ether_header)) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Received truncated packet, ignoring."); memcpy(&h, LLDP_NEIGHBOR_RAW(n), sizeof(h)); - if (h.ether_type != htobe16(ETHERTYPE_LLDP)) { - log_lldp("Received packet with wrong type, ignoring."); - return -EBADMSG; - } + if (h.ether_type != htobe16(ETHERTYPE_LLDP)) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Received packet with wrong type, ignoring."); if (h.ether_dhost[0] != 0x01 || h.ether_dhost[1] != 0x80 || h.ether_dhost[2] != 0xc2 || h.ether_dhost[3] != 0x00 || h.ether_dhost[4] != 0x00 || - !IN_SET(h.ether_dhost[5], 0x00, 0x03, 0x0e)) { - log_lldp("Received packet with wrong destination address, ignoring."); - return -EBADMSG; - } + !IN_SET(h.ether_dhost[5], 0x00, 0x03, 0x0e)) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Received packet with wrong destination address, ignoring."); memcpy(&n->source_address, h.ether_shost, sizeof(struct ether_addr)); memcpy(&n->destination_address, h.ether_dhost, sizeof(struct ether_addr)); @@ -188,27 +185,24 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) { uint8_t type; uint16_t length; - if (left < 2) { - log_lldp("TLV lacks header, ignoring."); - return -EBADMSG; - } + if (left < 2) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "TLV lacks header, ignoring."); type = p[0] >> 1; length = p[1] + (((uint16_t) (p[0] & 1)) << 8); p += 2, left -= 2; - if (left < length) { - log_lldp("TLV truncated, ignoring datagram."); - return -EBADMSG; - } + if (left < length) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "TLV truncated, ignoring datagram."); switch (type) { case SD_LLDP_TYPE_END: - if (length != 0) { - log_lldp("End marker TLV not zero-sized, ignoring datagram."); - return -EBADMSG; - } + if (length != 0) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "End marker TLV not zero-sized, ignoring datagram."); /* Note that after processing the SD_LLDP_TYPE_END left could still be > 0 * as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */ @@ -216,98 +210,93 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) { goto end_marker; case SD_LLDP_TYPE_CHASSIS_ID: - if (length < 2 || length > 256) { /* includes the chassis subtype, hence one extra byte */ - log_lldp("Chassis ID field size out of range, ignoring datagram."); - return -EBADMSG; - } - if (n->id.chassis_id) { - log_lldp("Duplicate chassis ID field, ignoring datagram."); - return -EBADMSG; - } + if (length < 2 || length > 256) + /* includes the chassis subtype, hence one extra byte */ + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Chassis ID field size out of range, ignoring datagram."); + + if (n->id.chassis_id) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Duplicate chassis ID field, ignoring datagram."); n->id.chassis_id = memdup(p, length); if (!n->id.chassis_id) - return -ENOMEM; + return log_oom_debug(); n->id.chassis_id_size = length; break; case SD_LLDP_TYPE_PORT_ID: - if (length < 2 || length > 256) { /* includes the port subtype, hence one extra byte */ - log_lldp("Port ID field size out of range, ignoring datagram."); - return -EBADMSG; - } - if (n->id.port_id) { - log_lldp("Duplicate port ID field, ignoring datagram."); - return -EBADMSG; - } + if (length < 2 || length > 256) + /* includes the port subtype, hence one extra byte */ + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Port ID field size out of range, ignoring datagram."); + + if (n->id.port_id) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Duplicate port ID field, ignoring datagram."); n->id.port_id = memdup(p, length); if (!n->id.port_id) - return -ENOMEM; + return log_oom_debug(); n->id.port_id_size = length; break; case SD_LLDP_TYPE_TTL: - if (length != 2) { - log_lldp("TTL field has wrong size, ignoring datagram."); - return -EBADMSG; - } + if (length != 2) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "TTL field has wrong size, ignoring datagram."); - if (n->has_ttl) { - log_lldp("Duplicate TTL field, ignoring datagram."); - return -EBADMSG; - } + if (n->has_ttl) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Duplicate TTL field, ignoring datagram."); n->ttl = unaligned_read_be16(p); n->has_ttl = true; break; case SD_LLDP_TYPE_PORT_DESCRIPTION: - r = parse_string(&n->port_description, p, length); + r = parse_string(n->lldp, &n->port_description, p, length); if (r < 0) return r; break; case SD_LLDP_TYPE_SYSTEM_NAME: - r = parse_string(&n->system_name, p, length); + r = parse_string(n->lldp, &n->system_name, p, length); if (r < 0) return r; break; case SD_LLDP_TYPE_SYSTEM_DESCRIPTION: - r = parse_string(&n->system_description, p, length); + r = parse_string(n->lldp, &n->system_description, p, length); if (r < 0) return r; break; case SD_LLDP_TYPE_SYSTEM_CAPABILITIES: if (length != 4) - log_lldp("System capabilities field has wrong size, ignoring."); - else { - n->system_capabilities = unaligned_read_be16(p); - n->enabled_capabilities = unaligned_read_be16(p + 2); - n->has_capabilities = true; - } + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "System capabilities field has wrong size."); + n->system_capabilities = unaligned_read_be16(p); + n->enabled_capabilities = unaligned_read_be16(p + 2); + n->has_capabilities = true; break; - case SD_LLDP_TYPE_PRIVATE: { + case SD_LLDP_TYPE_PRIVATE: if (length < 4) - log_lldp("Found private TLV that is too short, ignoring."); - else { - /* RFC 8520: MUD URL */ - if (memcmp(p, SD_LLDP_OUI_MUD, sizeof(SD_LLDP_OUI_MUD)) == 0 && - p[sizeof(SD_LLDP_OUI_MUD)] == SD_LLDP_OUI_SUBTYPE_MUD_USAGE_DESCRIPTION) { - r = parse_string(&n->mud_url, p + sizeof(SD_LLDP_OUI_MUD) + 1, - length - 1 - sizeof(SD_LLDP_OUI_MUD)); - if (r < 0) - return r; - } - } - } + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "Found private TLV that is too short, ignoring."); + /* RFC 8520: MUD URL */ + if (memcmp(p, SD_LLDP_OUI_MUD, sizeof(SD_LLDP_OUI_MUD)) == 0 && + p[sizeof(SD_LLDP_OUI_MUD)] == SD_LLDP_OUI_SUBTYPE_MUD_USAGE_DESCRIPTION) { + r = parse_string(n->lldp, &n->mud_url, p + sizeof(SD_LLDP_OUI_MUD) + 1, + length - 1 - sizeof(SD_LLDP_OUI_MUD)); + if (r < 0) + return r; + } break; } @@ -315,11 +304,9 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) { } end_marker: - if (!n->id.chassis_id || !n->id.port_id || !n->has_ttl) { - log_lldp("One or more mandatory TLV missing in datagram. Ignoring."); - return -EBADMSG; - - } + if (!n->id.chassis_id || !n->id.port_id || !n->has_ttl) + return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG), + "One or more mandatory TLV missing in datagram. Ignoring."); n->rindex = sizeof(struct ether_header); diff --git a/src/libsystemd-network/ndisc-internal.h b/src/libsystemd-network/ndisc-internal.h index ca90d5c539..44a7e76c21 100644 --- a/src/libsystemd-network/ndisc-internal.h +++ b/src/libsystemd-network/ndisc-internal.h @@ -7,7 +7,7 @@ #include "sd-ndisc.h" -#include "log.h" +#include "log-link.h" #include "time-util.h" #define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC) @@ -38,8 +38,18 @@ struct sd_ndisc { void *userdata; }; -#define log_ndisc_errno(error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "NDISC: " fmt, ##__VA_ARGS__) -#define log_ndisc(fmt, ...) log_ndisc_errno(0, fmt, ##__VA_ARGS__) - const char* ndisc_event_to_string(sd_ndisc_event_t e) _const_; sd_ndisc_event_t ndisc_event_from_string(const char *s) _pure_; + +#define log_ndisc_errno(ndisc, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_ndisc_get_ifname(ndisc), \ + LOG_DEBUG, _e, "NDISC: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_ndisc(ndisc, fmt, ...) \ + log_ndisc_errno(ndisc, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/ndisc-router.c b/src/libsystemd-network/ndisc-router.c index c88293a923..46f30332bb 100644 --- a/src/libsystemd-network/ndisc-router.c +++ b/src/libsystemd-network/ndisc-router.c @@ -43,7 +43,7 @@ _public_ int sd_ndisc_router_from_raw(sd_ndisc_router **ret, const void *raw, si return -ENOMEM; memcpy(NDISC_ROUTER_RAW(rt), raw, raw_size); - r = ndisc_router_parse(rt); + r = ndisc_router_parse(NULL, rt); if (r < 0) return r; @@ -87,7 +87,7 @@ _public_ int sd_ndisc_router_get_raw(sd_ndisc_router *rt, const void **ret, size return 0; } -int ndisc_router_parse(sd_ndisc_router *rt) { +int ndisc_router_parse(sd_ndisc *nd, sd_ndisc_router *rt) { struct nd_router_advert *a; const uint8_t *p; bool has_mtu = false, has_flag_extension = false; @@ -95,23 +95,20 @@ int ndisc_router_parse(sd_ndisc_router *rt) { assert(rt); - if (rt->raw_size < sizeof(struct nd_router_advert)) { - log_ndisc("Too small to be a router advertisement, ignoring."); - return -EBADMSG; - } + if (rt->raw_size < sizeof(struct nd_router_advert)) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Too small to be a router advertisement, ignoring."); /* Router advertisement packets are neatly aligned to 64bit boundaries, hence we can access them directly */ a = NDISC_ROUTER_RAW(rt); - if (a->nd_ra_type != ND_ROUTER_ADVERT) { - log_ndisc("Received ND packet that is not a router advertisement, ignoring."); - return -EBADMSG; - } + if (a->nd_ra_type != ND_ROUTER_ADVERT) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Received ND packet that is not a router advertisement, ignoring."); - if (a->nd_ra_code != 0) { - log_ndisc("Received ND packet with wrong RA code, ignoring."); - return -EBADMSG; - } + if (a->nd_ra_code != 0) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Received ND packet with wrong RA code, ignoring."); rt->hop_limit = a->nd_ra_curhoplimit; rt->flags = a->nd_ra_flags_reserved; /* the first 8bit */ @@ -131,36 +128,31 @@ int ndisc_router_parse(sd_ndisc_router *rt) { if (left == 0) break; - if (left < 2) { - log_ndisc("Option lacks header, ignoring datagram."); - return -EBADMSG; - } + if (left < 2) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Option lacks header, ignoring datagram."); type = p[0]; length = p[1] * 8; - if (length == 0) { - log_ndisc("Zero-length option, ignoring datagram."); - return -EBADMSG; - } - if (left < length) { - log_ndisc("Option truncated, ignoring datagram."); - return -EBADMSG; - } + if (length == 0) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Zero-length option, ignoring datagram."); + if (left < length) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Option truncated, ignoring datagram."); switch (type) { case SD_NDISC_OPTION_PREFIX_INFORMATION: - if (length != 4*8) { - log_ndisc("Prefix option of invalid size, ignoring datagram."); - return -EBADMSG; - } + if (length != 4*8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Prefix option of invalid size, ignoring datagram."); - if (p[2] > 128) { - log_ndisc("Bad prefix length, ignoring datagram."); - return -EBADMSG; - } + if (p[2] > 128) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Bad prefix length, ignoring datagram."); break; @@ -168,14 +160,13 @@ int ndisc_router_parse(sd_ndisc_router *rt) { uint32_t m; if (has_mtu) { - log_ndisc("MTU option specified twice, ignoring."); + log_ndisc(nd, "MTU option specified twice, ignoring."); break; } - if (length != 8) { - log_ndisc("MTU option of invalid size, ignoring datagram."); - return -EBADMSG; - } + if (length != 8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "MTU option of invalid size, ignoring datagram."); m = be32toh(*(uint32_t*) (p + 4)); if (m >= IPV6_MIN_MTU) /* ignore invalidly small MTUs */ @@ -186,37 +177,32 @@ int ndisc_router_parse(sd_ndisc_router *rt) { } case SD_NDISC_OPTION_ROUTE_INFORMATION: - if (length < 1*8 || length > 3*8) { - log_ndisc("Route information option of invalid size, ignoring datagram."); - return -EBADMSG; - } + if (length < 1*8 || length > 3*8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Route information option of invalid size, ignoring datagram."); - if (p[2] > 128) { - log_ndisc("Bad route prefix length, ignoring datagram."); - return -EBADMSG; - } + if (p[2] > 128) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Bad route prefix length, ignoring datagram."); break; case SD_NDISC_OPTION_RDNSS: - if (length < 3*8 || (length % (2*8)) != 1*8) { - log_ndisc("RDNSS option has invalid size."); - return -EBADMSG; - } + if (length < 3*8 || (length % (2*8)) != 1*8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), "RDNSS option has invalid size."); break; case SD_NDISC_OPTION_FLAGS_EXTENSION: if (has_flag_extension) { - log_ndisc("Flags extension option specified twice, ignoring."); + log_ndisc(nd, "Flags extension option specified twice, ignoring."); break; } - if (length < 1*8) { - log_ndisc("Flags extension option has invalid size."); - return -EBADMSG; - } + if (length < 1*8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "Flags extension option has invalid size."); /* Add in the additional flags bits */ rt->flags |= @@ -231,10 +217,9 @@ int ndisc_router_parse(sd_ndisc_router *rt) { break; case SD_NDISC_OPTION_DNSSL: - if (length < 2*8) { - log_ndisc("DNSSL option has invalid size."); - return -EBADMSG; - } + if (length < 2*8) + return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), + "DNSSL option has invalid size."); break; } @@ -437,7 +422,7 @@ _public_ int sd_ndisc_router_prefix_get_flags(sd_ndisc_router *rt, uint8_t *ret) flags = pi->nd_opt_pi_flags_reserved; if ((flags & ND_OPT_PI_FLAG_AUTO) && (pi->nd_opt_pi_prefix_len != 64)) { - log_ndisc("Invalid prefix length, ignoring prefix for stateless autoconfiguration."); + log_ndisc(NULL, "Invalid prefix length, ignoring prefix for stateless autoconfiguration."); flags &= ~ND_OPT_PI_FLAG_AUTO; } diff --git a/src/libsystemd-network/ndisc-router.h b/src/libsystemd-network/ndisc-router.h index cb3a56452b..f5293c96e0 100644 --- a/src/libsystemd-network/ndisc-router.h +++ b/src/libsystemd-network/ndisc-router.h @@ -45,4 +45,4 @@ static inline size_t NDISC_ROUTER_OPTION_LENGTH(const sd_ndisc_router *rt) { } sd_ndisc_router *ndisc_router_new(size_t raw_size); -int ndisc_router_parse(sd_ndisc_router *rt); +int ndisc_router_parse(sd_ndisc *nd, sd_ndisc_router *rt); diff --git a/src/libsystemd-network/radv-internal.h b/src/libsystemd-network/radv-internal.h index 7668224e80..fe5d74fda4 100644 --- a/src/libsystemd-network/radv-internal.h +++ b/src/libsystemd-network/radv-internal.h @@ -7,7 +7,7 @@ #include "sd-radv.h" -#include "log.h" +#include "log-link.h" #include "list.h" #include "sparse-endian.h" @@ -125,6 +125,15 @@ struct sd_radv_route_prefix { LIST_FIELDS(struct sd_radv_route_prefix, prefix); }; -#define log_radv_full(level, error, fmt, ...) log_internal(level, error, PROJECT_FILE, __LINE__, __func__, "RADV: " fmt, ##__VA_ARGS__) -#define log_radv_errno(error, fmt, ...) log_radv_full(LOG_DEBUG, error, fmt, ##__VA_ARGS__) -#define log_radv(fmt, ...) log_radv_errno(0, fmt, ##__VA_ARGS__) +#define log_radv_errno(radv, error, fmt, ...) \ + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_radv_get_ifname(radv), \ + LOG_DEBUG, _e, "RADV: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) +#define log_radv(radv, fmt, ...) \ + log_radv_errno(radv, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index f92f517029..c8a4c79ffe 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -1191,7 +1191,7 @@ static int client_parse_message( break; } - r = dhcp6_option_parse_ia(option, &lease->ia, &ia_na_status); + r = dhcp6_option_parse_ia(client, option, &lease->ia, &ia_na_status); if (r < 0 && r != -ENOMSG) return r; @@ -1224,7 +1224,7 @@ static int client_parse_message( break; } - r = dhcp6_option_parse_ia(option, &lease->pd, &ia_pd_status); + r = dhcp6_option_parse_ia(client, option, &lease->pd, &ia_pd_status); if (r < 0 && r != -ENOMSG) return r; diff --git a/src/libsystemd-network/sd-dhcp6-lease.c b/src/libsystemd-network/sd-dhcp6-lease.c index d6f0708c94..aca2c1f7e0 100644 --- a/src/libsystemd-network/sd-dhcp6-lease.c +++ b/src/libsystemd-network/sd-dhcp6-lease.c @@ -206,7 +206,7 @@ int dhcp6_lease_set_dns(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) { lease->dns_count, &lease->dns_allocated); if (r < 0) - return log_dhcp6_client_errno(client, r, "Invalid DNS server option: %m"); + return r; lease->dns_count = r; @@ -321,19 +321,16 @@ int dhcp6_lease_set_sntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) if (!optlen) return 0; - if (lease->ntp || lease->ntp_fqdn) { - log_dhcp6_client(client, "NTP information already provided"); + if (lease->ntp || lease->ntp_fqdn) + return -EEXIST; - return 0; - } - - log_dhcp6_client(client, "Using deprecated SNTP information"); + /* Using deprecated SNTP information */ r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->ntp, lease->ntp_count, &lease->ntp_allocated); if (r < 0) - return log_dhcp6_client_errno(client, r, "Invalid SNTP server option: %m"); + return r; lease->ntp_count = r; diff --git a/src/libsystemd-network/sd-ipv4acd.c b/src/libsystemd-network/sd-ipv4acd.c index 2a85afd58b..643bdc4ba9 100644 --- a/src/libsystemd-network/sd-ipv4acd.c +++ b/src/libsystemd-network/sd-ipv4acd.c @@ -76,7 +76,15 @@ struct sd_ipv4acd { }; #define log_ipv4acd_errno(acd, error, fmt, ...) \ - log_interface_full_errno(sd_ipv4acd_get_ifname(acd), LOG_DEBUG, error, "IPV4ACD: " fmt, ##__VA_ARGS__) + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_ipv4acd_get_ifname(acd), \ + LOG_DEBUG, _e, "IPv4ACD: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) #define log_ipv4acd(acd, fmt, ...) \ log_ipv4acd_errno(acd, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index 25fa4ab90b..49e9350fba 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -50,7 +50,15 @@ struct sd_ipv4ll { }; #define log_ipv4ll_errno(ll, error, fmt, ...) \ - log_interface_full_errno(sd_ipv4ll_get_ifname(ll), LOG_DEBUG, error, "IPV4LL: " fmt, ##__VA_ARGS__) + ({ \ + int _e = (error); \ + if (DEBUG_LOGGING) \ + log_interface_full_errno( \ + sd_ipv4ll_get_ifname(ll), \ + LOG_DEBUG, _e, "IPv4LL: " fmt, \ + ##__VA_ARGS__); \ + -ERRNO_VALUE(_e); \ + }) #define log_ipv4ll(ll, fmt, ...) \ log_ipv4ll_errno(ll, 0, fmt, ##__VA_ARGS__) diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c index 1298fea9ff..f11aeaadf0 100644 --- a/src/libsystemd-network/sd-lldp.c +++ b/src/libsystemd-network/sd-lldp.c @@ -40,12 +40,10 @@ static void lldp_callback(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor assert(lldp); assert(event >= 0 && event < _SD_LLDP_EVENT_MAX); - if (!lldp->callback) { - log_lldp("Received '%s' event.", lldp_event_to_string(event)); - return; - } + if (!lldp->callback) + return (void) log_lldp(lldp, "Received '%s' event.", lldp_event_to_string(event)); - log_lldp("Invoking callback for '%s' event.", lldp_event_to_string(event)); + log_lldp(lldp, "Invoking callback for '%s' event.", lldp_event_to_string(event)); lldp->callback(lldp, event, n, lldp->userdata); } @@ -187,11 +185,11 @@ static int lldp_handle_datagram(sd_lldp *lldp, sd_lldp_neighbor *n) { r = lldp_add_neighbor(lldp, n); if (r < 0) { - log_lldp_errno(r, "Failed to add datagram. Ignoring."); + log_lldp_errno(lldp, r, "Failed to add datagram. Ignoring."); return 0; } - log_lldp("Successfully processed LLDP datagram."); + log_lldp(lldp, "Successfully processed LLDP datagram."); return 0; } @@ -205,8 +203,10 @@ static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, v assert(lldp); space = next_datagram_size_fd(fd); - if (space < 0) - return log_lldp_errno(space, "Failed to determine datagram size to read: %m"); + if (space < 0) { + log_lldp_errno(lldp, space, "Failed to determine datagram size to read, ignoring: %m"); + return 0; + } n = lldp_neighbor_new(space); if (!n) @@ -217,12 +217,13 @@ static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, v if (IN_SET(errno, EAGAIN, EINTR)) return 0; - return log_lldp_errno(errno, "Failed to read LLDP datagram: %m"); + log_lldp_errno(lldp, errno, "Failed to read LLDP datagram, ignoring: %m"); + return 0; } if ((size_t) length != n->raw_size) { - log_lldp("Packet size mismatch."); - return -EINVAL; + log_lldp(lldp, "Packet size mismatch, ignoring"); + return 0; } /* Try to get the timestamp of this packet if it is known */ @@ -268,7 +269,7 @@ _public_ int sd_lldp_start(sd_lldp *lldp) { (void) sd_event_source_set_description(lldp->io_event_source, "lldp-io"); - log_lldp("Started LLDP client"); + log_lldp(lldp, "Started LLDP client"); return 1; fail: @@ -283,7 +284,7 @@ _public_ int sd_lldp_stop(sd_lldp *lldp) { if (lldp->fd < 0) return 0; - log_lldp("Stopping LLDP client"); + log_lldp(lldp, "Stopping LLDP client"); lldp_reset(lldp); lldp_flush_neighbors(lldp); @@ -417,12 +418,16 @@ static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) { int r; r = lldp_make_space(lldp, 0); - if (r < 0) - return log_lldp_errno(r, "Failed to make space: %m"); + if (r < 0) { + log_lldp_errno(lldp, r, "Failed to make space, ignoring: %m"); + return 0; + } r = lldp_start_timer(lldp, NULL); - if (r < 0) - return log_lldp_errno(r, "Failed to restart timer: %m"); + if (r < 0) { + log_lldp_errno(lldp, r, "Failed to restart timer, ignoring: %m"); + return 0; + } return 0; } diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index 8a5f341615..4d5f1b54cd 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -35,12 +35,10 @@ static void ndisc_callback(sd_ndisc *ndisc, sd_ndisc_event_t event, sd_ndisc_rou assert(ndisc); assert(event >= 0 && event < _SD_NDISC_EVENT_MAX); - if (!ndisc->callback) { - log_ndisc("Received '%s' event.", ndisc_event_to_string(event)); - return; - } + if (!ndisc->callback) + return (void) log_ndisc(ndisc, "Received '%s' event.", ndisc_event_to_string(event)); - log_ndisc("Invoking callback for '%s' event.", ndisc_event_to_string(event)); + log_ndisc(ndisc, "Invoking callback for '%s' event.", ndisc_event_to_string(event)); ndisc->callback(ndisc, event, rt, ndisc->userdata); } @@ -200,7 +198,7 @@ static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) { assert(nd); assert(rt); - r = ndisc_router_parse(rt); + r = ndisc_router_parse(nd, rt); if (r == -EBADMSG) /* Bad packet */ return 0; if (r < 0) @@ -212,7 +210,7 @@ static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) { if (rt->hop_limit > 0) nd->hop_limit = rt->hop_limit; - log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16 " sec", + log_ndisc(nd, "Received Router Advertisement: flags %s preference %s lifetime %" PRIu16 " sec", rt->flags & ND_RA_FLAG_MANAGED ? "MANAGED" : rt->flags & ND_RA_FLAG_OTHER ? "OTHER" : "none", rt->preference == SD_NDISC_PREFERENCE_HIGH ? "high" : rt->preference == SD_NDISC_PREFERENCE_LOW ? "low" : "medium", rt->lifetime); @@ -233,8 +231,10 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda assert(nd->event); buflen = next_datagram_size_fd(fd); - if (buflen < 0) - return log_ndisc_errno(buflen, "Failed to determine datagram size to read: %m"); + if (buflen < 0) { + log_ndisc_errno(nd, buflen, "Failed to determine datagram size to read, ignoring: %m"); + return 0; + } rt = ndisc_router_new(buflen); if (!rt) @@ -245,22 +245,22 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda switch (r) { case -EADDRNOTAVAIL: (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &rt->address, &addr); - log_ndisc("Received RA from non-link-local address %s. Ignoring", addr); + log_ndisc(nd, "Received RA from non-link-local address %s. Ignoring", addr); break; case -EMULTIHOP: - log_ndisc("Received RA with invalid hop limit. Ignoring."); + log_ndisc(nd, "Received RA with invalid hop limit. Ignoring."); break; case -EPFNOSUPPORT: - log_ndisc("Received invalid source address from ICMPv6 socket. Ignoring."); + log_ndisc(nd, "Received invalid source address from ICMPv6 socket. Ignoring."); break; case -EAGAIN: /* ignore spurious wakeups */ break; default: - log_ndisc_errno(r, "Unexpected error while reading from ICMPv6, ignoring: %m"); + log_ndisc_errno(nd, r, "Unexpected error while reading from ICMPv6, ignoring: %m"); break; } @@ -309,11 +309,11 @@ static int ndisc_timeout(sd_event_source *s, uint64_t usec, void *userdata) { r = icmp6_send_router_solicitation(nd->fd, &nd->mac_addr); if (r < 0) { - log_ndisc_errno(r, "Error sending Router Solicitation: %m"); + log_ndisc_errno(nd, r, "Error sending Router Solicitation: %m"); goto fail; } - log_ndisc("Sent Router Solicitation, next solicitation in %s", + log_ndisc(nd, "Sent Router Solicitation, next solicitation in %s", format_timespan(time_string, FORMAT_TIMESPAN_MAX, nd->retransmit_time, USEC_PER_SEC)); @@ -330,7 +330,7 @@ static int ndisc_timeout_no_ra(sd_event_source *s, uint64_t usec, void *userdata assert(s); assert(nd); - log_ndisc("No RA received before link confirmation timeout"); + log_ndisc(nd, "No RA received before link confirmation timeout"); (void) event_source_disable(nd->timeout_no_ra); ndisc_callback(nd, SD_NDISC_EVENT_TIMEOUT, NULL); @@ -345,7 +345,7 @@ _public_ int sd_ndisc_stop(sd_ndisc *nd) { if (nd->fd < 0) return 0; - log_ndisc("Stopping IPv6 Router Solicitation client"); + log_ndisc(nd, "Stopping IPv6 Router Solicitation client"); ndisc_reset(nd); return 1; @@ -398,7 +398,7 @@ _public_ int sd_ndisc_start(sd_ndisc *nd) { if (r < 0) goto fail; - log_ndisc("Started IPv6 Router Solicitation client"); + log_ndisc(nd, "Started IPv6 Router Solicitation client"); return 1; fail: diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 531ab7bcd2..857401bf6e 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -247,22 +247,22 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat switch (r) { case -EADDRNOTAVAIL: (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr); - log_radv("Received RS from non-link-local address %s. Ignoring", addr); + log_radv(ra, "Received RS from non-link-local address %s. Ignoring", addr); break; case -EMULTIHOP: - log_radv("Received RS with invalid hop limit. Ignoring."); + log_radv(ra, "Received RS with invalid hop limit. Ignoring."); break; case -EPFNOSUPPORT: - log_radv("Received invalid source address from ICMPv6 socket. Ignoring."); + log_radv(ra, "Received invalid source address from ICMPv6 socket. Ignoring."); break; case -EAGAIN: /* ignore spurious wakeups */ break; default: - log_radv_errno(r, "Unexpected error receiving from ICMPv6 socket: %m"); + log_radv_errno(ra, r, "Unexpected error receiving from ICMPv6 socket, Ignoring: %m"); break; } @@ -270,7 +270,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat } if ((size_t) buflen < sizeof(struct nd_router_solicit)) { - log_radv("Too short packet received"); + log_radv(ra, "Too short packet received, ignoring"); return 0; } @@ -278,9 +278,9 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat r = radv_send(ra, &src, ra->lifetime); if (r < 0) - log_radv_errno(r, "Unable to send solicited Router Advertisement to %s: %m", strnull(addr)); + log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", strnull(addr)); else - log_radv("Sent solicited Router Advertisement to %s", strnull(addr)); + log_radv(ra, "Sent solicited Router Advertisement to %s", strnull(addr)); return 0; } @@ -313,7 +313,7 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) { r = radv_send(ra, NULL, ra->lifetime); if (r < 0) - log_radv_errno(r, "Unable to send Router Advertisement: %m"); + log_radv_errno(ra, r, "Unable to send Router Advertisement: %m"); /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */ if (ra->ra_sent < SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS) { @@ -330,7 +330,7 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) { timeout = radv_compute_timeout(min_timeout, max_timeout); - log_radv("Next Router Advertisement in %s", + log_radv(ra, "Next Router Advertisement in %s", format_timespan(time_string, FORMAT_TIMESPAN_MAX, timeout, USEC_PER_SEC)); @@ -361,13 +361,13 @@ _public_ int sd_radv_stop(sd_radv *ra) { if (ra->state == SD_RADV_STATE_IDLE) return 0; - log_radv("Stopping IPv6 Router Advertisement daemon"); + log_radv(ra, "Stopping IPv6 Router Advertisement daemon"); /* RFC 4861, Section 6.2.5, send at least one Router Advertisement with zero lifetime */ r = radv_send(ra, NULL, 0); if (r < 0) - log_radv_errno(r, "Unable to send last Router Advertisement with router lifetime set to zero: %m"); + log_radv_errno(ra, r, "Unable to send last Router Advertisement with router lifetime set to zero: %m"); radv_reset(ra); ra->fd = safe_close(ra->fd); @@ -412,7 +412,7 @@ _public_ int sd_radv_start(sd_radv *ra) { ra->state = SD_RADV_STATE_ADVERTISING; - log_radv("Started IPv6 Router Advertisement daemon"); + log_radv(ra, "Started IPv6 Router Advertisement daemon"); return 0; @@ -581,10 +581,9 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { (void) in_addr_prefix_to_string(AF_INET6, (const union in_addr_union*) &cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur); - log_radv("IPv6 prefix %s already configured, ignoring %s", - strna(addr_cur), strna(addr_p)); - - return -EEXIST; + return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST), + "IPv6 prefix %s already configured, ignoring %s", + strna(addr_cur), strna(addr_p)); } p = sd_radv_prefix_ref(p); @@ -594,7 +593,7 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { ra->n_prefixes++; if (!dynamic) { - log_radv("Added prefix %s", strna(addr_p)); + log_radv(ra, "Added prefix %s", strna(addr_p)); return 0; } @@ -604,9 +603,9 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { if (ra->ra_sent > 0) { r = radv_send(ra, NULL, ra->lifetime); if (r < 0) - log_radv_errno(r, "Unable to send Router Advertisement for added prefix: %m"); + log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix: %m"); else - log_radv("Sent Router Advertisement for added prefix"); + log_radv(ra, "Sent Router Advertisement for added prefix"); } update: @@ -627,7 +626,7 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { cur->valid_until = valid_until; cur->preferred_until = preferred_until; - log_radv("Updated prefix %s preferred %s valid %s", + log_radv(ra, "Updated prefix %s preferred %s valid %s", strna(addr_p), format_timespan(time_string_preferred, FORMAT_TIMESPAN_MAX, preferred, USEC_PER_SEC), @@ -697,10 +696,9 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int (void) in_addr_prefix_to_string(AF_INET6, (const union in_addr_union*) &cur->opt.in6_addr, cur->opt.prefixlen, &addr); - log_radv("IPv6 route prefix %s already configured, ignoring %s", - strna(addr), strna(pretty)); - - return -EEXIST; + return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST), + "IPv6 route prefix %s already configured, ignoring %s", + strna(addr), strna(pretty)); } p = sd_radv_route_prefix_ref(p); @@ -709,7 +707,7 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int ra->n_route_prefixes++; if (!dynamic) { - log_radv("Added prefix %s", strna(pretty)); + log_radv(ra, "Added prefix %s", strna(pretty)); return 0; } @@ -717,9 +715,9 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int if (ra->ra_sent > 0) { r = radv_send(ra, NULL, ra->lifetime); if (r < 0) - log_radv_errno(r, "Unable to send Router Advertisement for added route prefix: %m"); + log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix: %m"); else - log_radv("Sent Router Advertisement for added route prefix"); + log_radv(ra, "Sent Router Advertisement for added route prefix"); } update: @@ -732,7 +730,7 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int if (valid_until == USEC_INFINITY) return -EOVERFLOW; - log_radv("Updated route prefix %s valid %s", + log_radv(ra, "Updated route prefix %s valid %s", strna(pretty), format_timespan(time_string_valid, FORMAT_TIMESPAN_MAX, valid, USEC_PER_SEC)); @@ -861,7 +859,7 @@ _public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, const struct in6_addr if (prefixlen > 64) /* unusual but allowed, log it */ - log_radv("Unusual prefix length %d greater than 64", prefixlen); + log_radv(NULL, "Unusual prefix length %d greater than 64", prefixlen); p->opt.in6_addr = *in6_addr; p->opt.prefixlen = prefixlen; @@ -951,7 +949,7 @@ _public_ int sd_radv_prefix_set_route_prefix(sd_radv_route_prefix *p, const stru if (prefixlen > 64) /* unusual but allowed, log it */ - log_radv("Unusual prefix length %u greater than 64", prefixlen); + log_radv(NULL, "Unusual prefix length %u greater than 64", prefixlen); p->opt.in6_addr = *in6_addr; p->opt.prefixlen = prefixlen; diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c index cb363b3973..9827f99699 100644 --- a/src/libsystemd-network/test-dhcp6-client.c +++ b/src/libsystemd-network/test-dhcp6-client.c @@ -295,17 +295,17 @@ static int test_option_status(sd_event *e) { option = (DHCP6Option *)option1; assert_se(sizeof(option1) == sizeof(DHCP6Option) + be16toh(option->len)); - r = dhcp6_option_parse_ia(option, &ia, NULL); + r = dhcp6_option_parse_ia(NULL, option, &ia, NULL); assert_se(r == 0); assert_se(ia.addresses == NULL); option->len = htobe16(17); - r = dhcp6_option_parse_ia(option, &ia, NULL); + r = dhcp6_option_parse_ia(NULL, option, &ia, NULL); assert_se(r == -ENOBUFS); assert_se(ia.addresses == NULL); option->len = htobe16(sizeof(DHCP6Option)); - r = dhcp6_option_parse_ia(option, &ia, NULL); + r = dhcp6_option_parse_ia(NULL, option, &ia, NULL); assert_se(r == -ENOBUFS); assert_se(ia.addresses == NULL); @@ -313,7 +313,7 @@ static int test_option_status(sd_event *e) { option = (DHCP6Option *)option2; assert_se(sizeof(option2) == sizeof(DHCP6Option) + be16toh(option->len)); - r = dhcp6_option_parse_ia(option, &ia, NULL); + r = dhcp6_option_parse_ia(NULL, option, &ia, NULL); assert_se(r >= 0); assert_se(ia.addresses == NULL); @@ -321,7 +321,7 @@ static int test_option_status(sd_event *e) { option = (DHCP6Option *)option3; assert_se(sizeof(option3) == sizeof(DHCP6Option) + be16toh(option->len)); - r = dhcp6_option_parse_ia(option, &ia, NULL); + r = dhcp6_option_parse_ia(NULL, option, &ia, NULL); assert_se(r >= 0); assert_se(ia.addresses != NULL); dhcp6_lease_free_ia(&ia); @@ -330,7 +330,7 @@ static int test_option_status(sd_event *e) { option = (DHCP6Option *)option4; assert_se(sizeof(option4) == sizeof(DHCP6Option) + be16toh(option->len)); - r = dhcp6_option_parse_ia(option, &pd, NULL); + r = dhcp6_option_parse_ia(NULL, option, &pd, NULL); assert_se(r >= 0); assert_se(pd.addresses != NULL); assert_se(memcmp(&pd.ia_pd.id, &option4[4], 4) == 0); @@ -342,7 +342,7 @@ static int test_option_status(sd_event *e) { option = (DHCP6Option *)option5; assert_se(sizeof(option5) == sizeof(DHCP6Option) + be16toh(option->len)); - r = dhcp6_option_parse_ia(option, &pd, NULL); + r = dhcp6_option_parse_ia(NULL, option, &pd, NULL); assert_se(r >= 0); assert_se(pd.addresses != NULL); dhcp6_lease_free_ia(&pd); @@ -460,7 +460,7 @@ static int test_advertise_option(sd_event *e) { val = htobe32(120); assert_se(!memcmp(optval + 8, &val, sizeof(val))); - assert_se(dhcp6_option_parse_ia(option, &lease->ia, NULL) >= 0); + assert_se(dhcp6_option_parse_ia(NULL, option, &lease->ia, NULL) >= 0); break; @@ -656,7 +656,7 @@ static int test_client_verify_request(DHCP6Message *request, size_t len) { assert_se(!memcmp(optval + 8, &val, sizeof(val))); /* Then, this should refuse all addresses. */ - assert_se(dhcp6_option_parse_ia(option, &lease->ia, NULL) >= 0); + assert_se(dhcp6_option_parse_ia(NULL, option, &lease->ia, NULL) >= 0); break; From a7d757ec294ed4df272fb99a376faeef03290ce2 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 24 Feb 2021 20:07:04 +0900 Subject: [PATCH 3/3] dhcp6: tighten T1 and T2 value check Only when T1 and T2 are both 0, they are adjusted later based on address or prefix T1 and T2. So the first check must be changed. --- src/libsystemd-network/dhcp6-option.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index cf3c7c287a..daaa940f97 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -535,7 +535,7 @@ int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6I lt_t1 = be32toh(ia->ia_na.lifetime_t1); lt_t2 = be32toh(ia->ia_na.lifetime_t2); - if (lt_t1 && lt_t2 && lt_t1 > lt_t2) + if (lt_t1 > lt_t2) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "IA NA T1 %"PRIu32"sec > T2 %"PRIu32"sec", lt_t1, lt_t2); @@ -553,7 +553,7 @@ int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6I lt_t1 = be32toh(ia->ia_pd.lifetime_t1); lt_t2 = be32toh(ia->ia_pd.lifetime_t2); - if (lt_t1 && lt_t2 && lt_t1 > lt_t2) + if (lt_t1 > lt_t2) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "IA PD T1 %"PRIu32"sec > T2 %"PRIu32"sec", lt_t1, lt_t2); @@ -642,7 +642,7 @@ int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6I switch(iatype) { case SD_DHCP6_OPTION_IA_NA: - if (!ia->ia_na.lifetime_t1 && !ia->ia_na.lifetime_t2 && lt_min != UINT32_MAX) { + if (ia->ia_na.lifetime_t1 == 0 && ia->ia_na.lifetime_t2 == 0 && lt_min != UINT32_MAX) { lt_t1 = lt_min / 2; lt_t2 = lt_min / 10 * 8; ia->ia_na.lifetime_t1 = htobe32(lt_t1); @@ -655,7 +655,7 @@ int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6I break; case SD_DHCP6_OPTION_IA_PD: - if (!ia->ia_pd.lifetime_t1 && !ia->ia_pd.lifetime_t2 && lt_min != UINT32_MAX) { + if (ia->ia_pd.lifetime_t1 == 0 && ia->ia_pd.lifetime_t2 == 0 && lt_min != UINT32_MAX) { lt_t1 = lt_min / 2; lt_t2 = lt_min / 10 * 8; ia->ia_pd.lifetime_t1 = htobe32(lt_t1);