Skip to contentSkip to conversation

monitor: pass config to nlmon_create

PATCH In review James Prestwood 2 participants 4 messages Oct 26, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 monitor: pass config to nlmon_create

Expand nlmon_create to be useful for both pcaps and monitoring. Doing this also lets iwmon filter pcaps based on --no-ies,rtnl,scan etc flags since they are part of the config.

---
 monitor/main.c  |  8 +++++---
 monitor/nlmon.c | 15 +++++++--------
 monitor/nlmon.h |  2 +-
 3 files changed, 13 insertions(+), 12 deletions(-)
monitor/main.c +5 -3
index f0b16bbf..50b3fd27 100644
@@ -569,7 +569,8 @@ done:
 	return exit_status;
 }
 
-static int process_pcap(struct pcap *pcap, uint16_t id)
+static int process_pcap(struct pcap *pcap, uint16_t id,
+			const struct nlmon_config *config)
 {
 	struct nlmon *nlmon = NULL;
 	struct timeval tv;
@@ -586,7 +587,7 @@ static int process_pcap(struct pcap *pcap, uint16_t id)
 		return EXIT_FAILURE;
 	}
 
-	nlmon = nlmon_create(id);
+	nlmon = nlmon_create(id, config);
 
 	while (pcap_read(pcap, &tv, buf, snaplen, &len, &real_len)) {
 		uint16_t arphrd_type;
@@ -797,7 +798,8 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Invalid packet format\n");
 			exit_status = EXIT_FAILURE;
 		} else
-			exit_status = process_pcap(pcap, nl80211_family);
+			exit_status = process_pcap(pcap, nl80211_family,
+							&config);
 
 		pcap_close(pcap);
 
monitor/nlmon.c +7 -8
index 69e86ce3..b58bef05 100644
@@ -7253,7 +7253,7 @@ static void nlmon_message(struct nlmon *nlmon, const struct timeval *tv,
 	}
 }
 
-struct nlmon *nlmon_create(uint16_t id)
+struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config)
 {
 	struct nlmon *nlmon;
 
@@ -7261,6 +7261,10 @@ struct nlmon *nlmon_create(uint16_t id)
 
 	nlmon->id = id;
 	nlmon->req_list = l_queue_new();
+	nlmon->nortnl = config->nortnl;
+	nlmon->nowiphy = config->nowiphy;
+	nlmon->noscan = config->noscan;
+	nlmon->noies = config->noies;
 
 	return nlmon;
 }
@@ -8449,17 +8453,12 @@ struct nlmon *nlmon_open(const char *ifname, uint16_t id, const char *pathname,
 	} else
 		pcap = NULL;
 
-	nlmon = l_new(struct nlmon, 1);
 
-	nlmon->id = id;
+	nlmon = nlmon_create(id, config);
+
 	nlmon->io = io;
 	nlmon->pae_io = pae_io;
-	nlmon->req_list = l_queue_new();
 	nlmon->pcap = pcap;
-	nlmon->nortnl = config->nortnl;
-	nlmon->nowiphy = config->nowiphy;
-	nlmon->noscan = config->noscan;
-	nlmon->noies = config->noies;
 
 	l_io_set_read_handler(nlmon->io, nlmon_receive, nlmon, NULL);
 	l_io_set_read_handler(nlmon->pae_io, pae_receive, nlmon, NULL);
monitor/nlmon.h +1 -1
index ab038fdf..d1092c11 100644
@@ -36,7 +36,7 @@ struct nlmon *nlmon_open(const char *ifname, uint16_t id, const char *pathname,
 				const struct nlmon_config *config);
 void nlmon_close(struct nlmon *nlmon);
 
-struct nlmon *nlmon_create(uint16_t id);
+struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config);
 void nlmon_destroy(struct nlmon *nlmon);
 void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
 					const void *data, uint32_t size);
Signature · 1 line
2.34.3
#1 lore raw

PATCH v2 2/3 monitor: allow parsing pcaps without -F option

The -F option is undocumented but allows you to pass a nl80211 family ID so iwmon doesn't ignore messages which don't match the systems nl80211 family ID (i.e. pcaps from other systems).

This is somewhat of a pain to use since its unclear what the other system's family ID actually is until you run it though something like wireshark. Instead iwmon can ignore the family ID when in read mode which makes reading other systems pcap files automatic.

---
 monitor/main.c  | 1 +
 monitor/nlmon.c | 4 +++-
 monitor/nlmon.h | 1 +
 3 files changed, 5 insertions(+), 1 deletion(-)
monitor/main.c +1 -0
index 50b3fd27..0cbab179 100644
@@ -710,6 +710,7 @@ int main(int argc, char *argv[])
 		switch (opt) {
 		case 'r':
 			reader_path = optarg;
+			config.read_only = true;
 			break;
 		case 'w':
 			writer_path = optarg;
monitor/nlmon.c +3 -1
index b58bef05..d786cc33 100644
@@ -104,6 +104,7 @@ struct nlmon {
 	bool nowiphy;
 	bool noscan;
 	bool noies;
+	bool read;
 };
 
 struct nlmon_req {
@@ -7202,7 +7203,7 @@ static void nlmon_message(struct nlmon *nlmon, const struct timeval *tv,
 		return;
 	}
 
-	if (nlmsg->nlmsg_type != nlmon->id) {
+	if (!nlmon->read && nlmsg->nlmsg_type != nlmon->id) {
 		if (nlmsg->nlmsg_type == GENL_ID_CTRL)
 			store_message(nlmon, tv, nlmsg);
 		return;
@@ -7265,6 +7266,7 @@ struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config)
 	nlmon->nowiphy = config->nowiphy;
 	nlmon->noscan = config->noscan;
 	nlmon->noies = config->noies;
+	nlmon->read = config->read_only;
 
 	return nlmon;
 }
monitor/nlmon.h +1 -0
index d1092c11..96958c25 100644
@@ -30,6 +30,7 @@ struct nlmon_config {
 	bool nowiphy;
 	bool noscan;
 	bool noies;
+	bool read_only;
 };
 
 struct nlmon *nlmon_open(const char *ifname, uint16_t id, const char *pathname,
Signature · 1 line
2.34.3
#2 lore raw

PATCH v2 3/3 monitor: remove -F option

In reply to James Prestwood

This is now handled automatically by setting read_only which bypasses the family ID check.

---
 monitor/main.c | 31 ++++---------------------------
 1 file changed, 4 insertions(+), 27 deletions(-)
monitor/main.c +4 -27
index 0cbab179..0c5f0670 100644
@@ -569,8 +569,7 @@ done:
 	return exit_status;
 }
 
-static int process_pcap(struct pcap *pcap, uint16_t id,
-			const struct nlmon_config *config)
+static int process_pcap(struct pcap *pcap, const struct nlmon_config *config)
 {
 	struct nlmon *nlmon = NULL;
 	struct timeval tv;
@@ -587,7 +586,7 @@ static int process_pcap(struct pcap *pcap, uint16_t id,
 		return EXIT_FAILURE;
 	}
 
-	nlmon = nlmon_create(id, config);
+	nlmon = nlmon_create(0, config);
 
 	while (pcap_read(pcap, &tv, buf, snaplen, &len, &real_len)) {
 		uint16_t arphrd_type;
@@ -696,13 +695,12 @@ int main(int argc, char *argv[])
 	const char *reader_path = NULL;
 	const char *analyze_path = NULL;
 	const char *ifname = NULL;
-	uint16_t nl80211_family = 0;
 	int exit_status;
 
 	for (;;) {
 		int opt;
 
-		opt = getopt_long(argc, argv, "r:w:a:F:i:nvhyse",
+		opt = getopt_long(argc, argv, "r:w:a:i:nvhyse",
 						main_options, NULL);
 		if (opt < 0)
 			break;
@@ -718,26 +716,6 @@ int main(int argc, char *argv[])
 		case 'a':
 			analyze_path = optarg;
 			break;
-		case 'F':
-			if (strlen(optarg) > 3) {
-				if (!strncasecmp(optarg, "0x", 2) &&
-							!isxdigit(optarg[2])) {
-					usage();
-					return EXIT_FAILURE;
-				}
-				nl80211_family = strtoul(optarg + 2, NULL, 16);
-			} else {
-				if (!isdigit(optarg[0])) {
-					usage();
-					return EXIT_FAILURE;
-				}
-				nl80211_family = strtoul(optarg, NULL, 10);
-			}
-			if (nl80211_family == 0) {
-				usage();
-				return EXIT_FAILURE;
-			}
-			break;
 		case 'i':
 			ifname = optarg;
 			break;
@@ -799,8 +777,7 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Invalid packet format\n");
 			exit_status = EXIT_FAILURE;
 		} else
-			exit_status = process_pcap(pcap, nl80211_family,
-							&config);
+			exit_status = process_pcap(pcap, &config);
 
 		pcap_close(pcap);
 
Signature · 1 line
2.34.3
#3 lore raw
In reply to James Prestwood

Hi James,

Quoted 9 lines · James Prestwood

Expand nlmon_create to be useful for both pcaps and monitoring. Doing this also lets iwmon filter pcaps based on --no-ies,rtnl,scan etc flags since they are part of the config.

---
  monitor/main.c  |  8 +++++---
  monitor/nlmon.c | 15 +++++++--------
  monitor/nlmon.h |  2 +-
  3 files changed, 13 insertions(+), 12 deletions(-)

All applied, thanks.

Regards,
-Denis