diff mbox series

[bpf-next,02/15] selftests/bpf: AF_PACKET support for make_sockaddr

Message ID e1ac981f0f30a7d6ed7f57a08e37aeb3f5d2fff5.1721771340.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use network helpers, part 11 | expand

Commit Message

Geliang Tang July 23, 2024, 10:22 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch adds AF_PACKET support for make_sockaddr(), uses sscanf()
to parse the given addr_str string into sll_ifindex, sll_halen and
sll_addr of sockaddr_ll. Other fields of struct sockaddr_ll are not
used in BPF selftests right now.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index a3f0a49fb26f..2a142d713861 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -416,6 +416,21 @@  int make_sockaddr(int family, const char *addr_str, __u16 port,
 		if (len)
 			*len = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(addr_str);
 		return 0;
+	} else if (family == AF_PACKET) {
+		struct sockaddr_ll *sll = (void *)addr;
+
+		memset(addr, 0, sizeof(*sll));
+		sll->sll_family = family;
+		sll->sll_protocol = htons(ETH_P_ALL);
+		if (addr_str &&
+		    sscanf(addr_str, "%d %c %s", &sll->sll_ifindex,
+			   &sll->sll_halen, sll->sll_addr) == -1) {
+			log_err("AF_PACKET sscanf(%s)", addr_str);
+			return -1;
+		}
+		if (len)
+			*len = sizeof(*sll);
+		return 0;
 	}
 	return -1;
 }