summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2019-11-22 10:36:20 +0100
committerBeniamino Galvani <bgalvani@redhat.com>2019-11-22 10:36:20 +0100
commit234cb5a92332fdc91837d23934b960342b500072 (patch)
tree0a603f533b1a15e23a1acae7b0eab96ef233a426
parentd99c91a05a7ab0b90a42bb61cfc4863104fa6469 (diff)
parent97a8785148f903c0bcff5c91cb2a9e52ccdb824d (diff)
downloadNetworkManager-234cb5a92332fdc91837d23934b960342b500072.tar.gz
dhcp: merge branch 'bg/nettools-log'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/337
-rw-r--r--shared/n-dhcp4/src/n-dhcp4-c-connection.c67
-rw-r--r--shared/n-dhcp4/src/n-dhcp4-client.c12
-rw-r--r--shared/n-dhcp4/src/n-dhcp4-private.h24
-rw-r--r--shared/n-dhcp4/src/n-dhcp4.h4
-rw-r--r--shared/nm-glib-aux/nm-logging-fwd.h13
-rw-r--r--src/dhcp/nm-dhcp-nettools.c22
6 files changed, 142 insertions, 0 deletions
diff --git a/shared/n-dhcp4/src/n-dhcp4-c-connection.c b/shared/n-dhcp4/src/n-dhcp4-c-connection.c
index 5c50dacfd8..8a2ddb3636 100644
--- a/shared/n-dhcp4/src/n-dhcp4-c-connection.c
+++ b/shared/n-dhcp4/src/n-dhcp4-c-connection.c
@@ -513,6 +513,7 @@ static int n_dhcp4_c_connection_new_message(NDhcp4CConnection *connection,
n_dhcp4_c_connection_init_header(connection, header);
message->userdata.type = type;
+ message->userdata.message_type = message_type;
/*
* Note that some implementations expect the MESSAGE_TYPE option to be
@@ -690,6 +691,7 @@ int n_dhcp4_c_connection_select_new(NDhcp4CConnection *connection,
*/
message->userdata.start_time = offer->userdata.start_time;
message->userdata.base_time = offer->userdata.base_time;
+ message->userdata.client_addr = client.s_addr;
n_dhcp4_incoming_get_xid(offer, &xid);
n_dhcp4_outgoing_set_xid(message, xid);
@@ -763,6 +765,7 @@ int n_dhcp4_c_connection_renew_new(NDhcp4CConnection *connection,
if (r)
return r;
+ message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@@ -796,6 +799,7 @@ int n_dhcp4_c_connection_rebind_new(NDhcp4CConnection *connection,
if (r)
return r;
+ message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@@ -847,6 +851,7 @@ int n_dhcp4_c_connection_decline_new(NDhcp4CConnection *connection,
return r;
}
+ message->userdata.client_addr = client.s_addr;
*requestp = message;
message = NULL;
return 0;
@@ -889,6 +894,7 @@ int n_dhcp4_c_connection_inform_new(NDhcp4CConnection *connection,
if (r)
return r;
+ message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@@ -955,9 +961,36 @@ int n_dhcp4_c_connection_release_new(NDhcp4CConnection *connection,
return 0;
}
+static const char *message_type_to_str(uint8_t type) {
+ switch (type) {
+ case N_DHCP4_MESSAGE_DISCOVER:
+ return "DISCOVER";
+ case N_DHCP4_MESSAGE_OFFER:
+ return "OFFER";
+ case N_DHCP4_MESSAGE_REQUEST:
+ return "REQUEST";
+ case N_DHCP4_MESSAGE_DECLINE:
+ return "DECLINE";
+ case N_DHCP4_MESSAGE_ACK:
+ return "ACK";
+ case N_DHCP4_MESSAGE_NAK:
+ return "NACK";
+ case N_DHCP4_MESSAGE_RELEASE:
+ return "RELEASE";
+ case N_DHCP4_MESSAGE_INFORM:
+ return "INFORM";
+ case N_DHCP4_MESSAGE_FORCERENEW:
+ return "FORCERENEW";
+ default:
+ return "UNKNOWN";
+ }
+}
+
static int n_dhcp4_c_connection_send_request(NDhcp4CConnection *connection,
NDhcp4Outgoing *request,
uint64_t timestamp) {
+ char server_addr[INET_ADDRSTRLEN];
+ char client_addr[INET_ADDRSTRLEN];
int r;
/*
@@ -1015,6 +1048,22 @@ static int n_dhcp4_c_connection_send_request(NDhcp4CConnection *connection,
c_assert(0);
}
+ if (request->userdata.client_addr == INADDR_ANY) {
+ n_dhcp4_c_log(connection->client_config, LOG_INFO,
+ "sent %s to %s",
+ message_type_to_str(request->userdata.message_type),
+ inet_ntop(AF_INET, &connection->server_ip,
+ server_addr, sizeof(server_addr)));
+ } else {
+ n_dhcp4_c_log(connection->client_config, LOG_INFO,
+ "sent %s of %s to %s",
+ message_type_to_str(request->userdata.message_type),
+ inet_ntop(AF_INET, &request->userdata.client_addr,
+ client_addr, sizeof(client_addr)),
+ inet_ntop(AF_INET, &connection->server_ip,
+ server_addr, sizeof(server_addr)));
+ }
+
++request->userdata.n_send;
return 0;
}
@@ -1066,6 +1115,8 @@ int n_dhcp4_c_connection_dispatch_timer(NDhcp4CConnection *connection,
int n_dhcp4_c_connection_dispatch_io(NDhcp4CConnection *connection,
NDhcp4Incoming **messagep) {
_c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
+ char serv_addr[INET_ADDRSTRLEN];
+ char client_addr[INET_ADDRSTRLEN];
uint8_t type;
int r;
@@ -1118,6 +1169,22 @@ int n_dhcp4_c_connection_dispatch_io(NDhcp4CConnection *connection,
if (r)
return r;
+ if (type == N_DHCP4_MESSAGE_OFFER || type == N_DHCP4_MESSAGE_ACK) {
+ n_dhcp4_c_log(connection->client_config, LOG_INFO,
+ "received %s of %s from %s",
+ message_type_to_str(type),
+ inet_ntop(AF_INET, &message->message.header.yiaddr,
+ client_addr, sizeof(client_addr)),
+ inet_ntop(AF_INET, &message->message.header.siaddr,
+ serv_addr, sizeof(serv_addr)));
+ } else {
+ n_dhcp4_c_log(connection->client_config, LOG_INFO,
+ "received %s from %s",
+ message_type_to_str(type),
+ inet_ntop(AF_INET, &message->message.header.siaddr,
+ serv_addr, sizeof(serv_addr)));
+ }
+
switch (type) {
case N_DHCP4_MESSAGE_OFFER:
case N_DHCP4_MESSAGE_ACK:
diff --git a/shared/n-dhcp4/src/n-dhcp4-client.c b/shared/n-dhcp4/src/n-dhcp4-client.c
index 5f7794fb14..5449106a9b 100644
--- a/shared/n-dhcp4/src/n-dhcp4-client.c
+++ b/shared/n-dhcp4/src/n-dhcp4-client.c
@@ -94,6 +94,9 @@ int n_dhcp4_client_config_dup(NDhcp4ClientConfig *config, NDhcp4ClientConfig **d
dup->n_mac = config->n_mac;
memcpy(dup->broadcast_mac, config->broadcast_mac, sizeof(dup->broadcast_mac));
dup->n_broadcast_mac = config->n_broadcast_mac;
+ dup->log.level = config->log.level;
+ dup->log.func = config->log.func;
+ dup->log.data = config->log.data;
r = n_dhcp4_client_config_set_client_id(dup,
config->client_id,
@@ -248,6 +251,15 @@ _c_public_ int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, c
return 0;
}
+_c_public_ void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level) {
+ config->log.level = level;
+}
+
+_c_public_ void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data) {
+ config->log.func = func;
+ config->log.data = data;
+}
+
/**
* n_dhcp4_c_event_node_new() - allocate new event
* @nodep: output argument for new event
diff --git a/shared/n-dhcp4/src/n-dhcp4-private.h b/shared/n-dhcp4/src/n-dhcp4-private.h
index c38ddbfc80..fa2fda38de 100644
--- a/shared/n-dhcp4/src/n-dhcp4-private.h
+++ b/shared/n-dhcp4/src/n-dhcp4-private.h
@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
+#include <syslog.h>
#include "n-dhcp4.h"
typedef struct NDhcp4CConnection NDhcp4CConnection;
@@ -198,6 +199,8 @@ struct NDhcp4Outgoing {
struct {
uint8_t type;
+ uint8_t message_type;
+ uint32_t client_addr;
uint64_t start_time;
uint64_t base_time;
uint64_t send_time;
@@ -239,6 +242,11 @@ struct NDhcp4ClientConfig {
size_t n_broadcast_mac;
uint8_t *client_id;
size_t n_client_id;
+ struct {
+ int level;
+ NDhcp4LogFunc func;
+ void *data;
+ } log;
};
#define N_DHCP4_CLIENT_CONFIG_NULL(_x) { \
@@ -686,3 +694,19 @@ static inline uint64_t n_dhcp4_gettime(clockid_t clock) {
return ts.tv_sec * 1000ULL * 1000ULL * 1000ULL + ts.tv_nsec;
}
+
+#define n_dhcp4_c_log(_config, _level, ...) \
+ do { \
+ const NDhcp4ClientConfig *__config = _config; \
+ \
+ if (_level <= __config->log.level && __config->log.func) { \
+ if (1) { \
+ _config->log.func(_level, \
+ __config->log.data, \
+ __VA_ARGS__); \
+ } else { \
+ /* To have the compiler check arguments */ \
+ printf(__VA_ARGS__); \
+ } \
+ } \
+ } while (0)
diff --git a/shared/n-dhcp4/src/n-dhcp4.h b/shared/n-dhcp4/src/n-dhcp4.h
index a800352b50..d374766263 100644
--- a/shared/n-dhcp4/src/n-dhcp4.h
+++ b/shared/n-dhcp4/src/n-dhcp4.h
@@ -29,6 +29,8 @@ typedef struct NDhcp4ServerEvent NDhcp4ServerEvent;
typedef struct NDhcp4ServerIp NDhcp4ServerIp;
typedef struct NDhcp4ServerLease NDhcp4ServerLease;
+typedef void (*NDhcp4LogFunc)(int level, void *data, const char *fmt, ...);
+
#define N_DHCP4_CLIENT_START_DELAY_RFC2131 (UINT64_C(9000))
enum {
@@ -111,6 +113,8 @@ void n_dhcp4_client_config_set_request_broadcast(NDhcp4ClientConfig *config, boo
void n_dhcp4_client_config_set_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
void n_dhcp4_client_config_set_broadcast_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, const uint8_t *id, size_t n_id);
+void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level);
+void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data);
/* client-probe configs */
diff --git a/shared/nm-glib-aux/nm-logging-fwd.h b/shared/nm-glib-aux/nm-logging-fwd.h
index eecba8bead..7d76a5b13d 100644
--- a/shared/nm-glib-aux/nm-logging-fwd.h
+++ b/shared/nm-glib-aux/nm-logging-fwd.h
@@ -112,6 +112,19 @@ nm_log_level_from_syslog (int syslog_level)
}
}
+static inline int
+nm_log_level_to_syslog (NMLogLevel nm_level)
+{
+ switch (nm_level) {
+ case LOGL_ERR: return 3; /* LOG_ERR */
+ case LOGL_WARN: return 4; /* LOG_WARN */
+ case LOGL_INFO: return 5; /* LOG_NOTICE */
+ case LOGL_DEBUG: return 6; /* LOG_INFO */
+ case LOGL_TRACE: return 7; /* LOG_DEBUG */
+ default: return 0; /* LOG_EMERG */
+ }
+}
+
/*****************************************************************************/
struct timespec;
diff --git a/src/dhcp/nm-dhcp-nettools.c b/src/dhcp/nm-dhcp-nettools.c
index 8c5da79111..c81823fa23 100644
--- a/src/dhcp/nm-dhcp-nettools.c
+++ b/src/dhcp/nm-dhcp-nettools.c
@@ -1053,6 +1053,26 @@ dhcp4_event_cb (GIOChannel *source,
return G_SOURCE_CONTINUE;
}
+static void
+nettools_log (int level, void *data, const char *fmt, ...)
+{
+ NMDhcpNettools *self = data;
+ NMLogLevel nm_level;
+ gs_free char *msg = NULL;
+ va_list ap;
+
+ nm_level = nm_log_level_from_syslog (level);
+ if (nm_logging_enabled (nm_level, LOGD_DHCP4)) {
+ va_start (ap, fmt);
+ msg = g_strdup_vprintf (fmt, ap);
+ va_end (ap);
+ nm_log (nm_level, LOGD_DHCP4, NULL , NULL,
+ "dhcp4 (%s): %s",
+ nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)),
+ msg);
+ }
+}
+
static gboolean
nettools_create (NMDhcpNettools *self,
const char *dhcp_anycast_addr,
@@ -1122,6 +1142,8 @@ nettools_create (NMDhcpNettools *self,
return FALSE;
}
+ n_dhcp4_client_config_set_log_level (config, nm_log_level_to_syslog (nm_logging_get_level (LOGD_DHCP4)));
+ n_dhcp4_client_config_set_log_func (config, nettools_log, self);
n_dhcp4_client_config_set_ifindex (config, nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)));
n_dhcp4_client_config_set_transport (config, transport);
n_dhcp4_client_config_set_mac (config, hwaddr_arr, hwaddr_len);