summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Sowden <jeremy@azazel.net>2025-05-26 18:19:02 +0100
committerFlorian Westphal <fw@strlen.de>2025-06-01 14:12:47 +0200
commitc0f9d1950e0968688558c2abbaa41cffd03f19de (patch)
tree082d6c952dd36064b2fa1fa6501e0553123eea24
parent6498d29aa15557428aa62f37c478a602e03bd740 (diff)
IP2BIN, IP2HBIN, IP2STR: refactor `interp` call-backs
IP2STR and IP2BIN do all family checks inside the for-loop that converts the address fields, whereas IP2HBIN does the checks once before the loop. Refactor the former to do as the latter. Also, move all the remaining contents of the for-loops, apart from the `pp_is_valid` checks, into `ip2*` functions. Signed-off-by: Jeremy Sowden <jeremy@azazel.net> Signed-off-by: Florian Westphal <fw@strlen.de>
-rw-r--r--filter/ulogd_filter_IP2BIN.c86
-rw-r--r--filter/ulogd_filter_IP2HBIN.c52
-rw-r--r--filter/ulogd_filter_IP2STR.c76
3 files changed, 110 insertions, 104 deletions
diff --git a/filter/ulogd_filter_IP2BIN.c b/filter/ulogd_filter_IP2BIN.c
index f1ca4ee..2667a2a 100644
--- a/filter/ulogd_filter_IP2BIN.c
+++ b/filter/ulogd_filter_IP2BIN.c
@@ -114,14 +114,42 @@ static struct ulogd_key ip2bin_keys[] = {
static char ipbin_array[MAX_KEY - START_KEY + 1][FORMAT_IPV6_BUFSZ];
-static int ip2bin(struct ulogd_key *inp, int index, int oindex)
+static void ip2bin(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+ uint8_t addr_family)
{
- char family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
- char convfamily = family;
- struct in6_addr *addr;
- struct in6_addr ip4_addr;
+ struct in6_addr *addr, ip4_addr;
+
+ switch (addr_family) {
+ case AF_INET6:
+ addr = (struct in6_addr *)ikey_get_u128(&inp[i]);
+ break;
+ case AF_INET:
+ /* Convert IPv4 to IPv4 in IPv6 */
+ addr = &ip4_addr;
+ uint32_to_ipv6(ikey_get_u32(&inp[i]), addr);
+ break;
+ }
+
+ format_ipv6(ipbin_array[o], sizeof(ipbin_array[o]), addr);
+
+ okey_set_ptr(&outp[o], ipbin_array[o]);
+}
+
+static int interp_ip2bin(struct ulogd_pluginstance *pi)
+{
+ struct ulogd_key *outp = pi->output.keys;
+ struct ulogd_key *inp = pi->input.keys;
+ uint8_t proto_family, addr_family;
+ int i, o;
+
+ proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
- if (family == AF_BRIDGE) {
+ switch (proto_family) {
+ case AF_INET6:
+ case AF_INET:
+ addr_family = proto_family;
+ break;
+ case AF_BRIDGE:
if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
ulogd_log(ULOGD_NOTICE,
"No protocol inside AF_BRIDGE packet\n");
@@ -129,56 +157,28 @@ static int ip2bin(struct ulogd_key *inp, int index, int oindex)
}
switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
case ETH_P_IPV6:
- convfamily = AF_INET6;
+ addr_family = AF_INET6;
break;
case ETH_P_IP:
- convfamily = AF_INET;
- break;
case ETH_P_ARP:
- convfamily = AF_INET;
+ addr_family = AF_INET;
break;
default:
ulogd_log(ULOGD_NOTICE,
"Unknown protocol inside AF_BRIDGE packet\n");
return ULOGD_IRET_ERR;
}
+ break;
+ default:
+ /* TODO handle error */
+ ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
+ return ULOGD_IRET_ERR;
}
- switch (convfamily) {
- case AF_INET6:
- addr = (struct in6_addr *)ikey_get_u128(&inp[index]);
- break;
- case AF_INET:
- /* Convert IPv4 to IPv4 in IPv6 */
- addr = &ip4_addr;
- uint32_to_ipv6(ikey_get_u32(&inp[index]), addr);
- break;
- default:
- /* TODO handle error */
- ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
- return ULOGD_IRET_ERR;
- }
-
- format_ipv6(ipbin_array[oindex], sizeof(ipbin_array[oindex]), addr);
-
- return ULOGD_IRET_OK;
-}
-
-static int interp_ip2bin(struct ulogd_pluginstance *pi)
-{
- struct ulogd_key *ret = pi->output.keys;
- struct ulogd_key *inp = pi->input.keys;
- int i;
- int fret;
-
/* Iter on all addr fields */
- for(i = START_KEY; i <= MAX_KEY; i++) {
+ for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
if (pp_is_valid(inp, i)) {
- fret = ip2bin(inp, i, i - START_KEY);
- if (fret != ULOGD_IRET_OK)
- return fret;
- okey_set_ptr(&ret[i - START_KEY],
- ipbin_array[i - START_KEY]);
+ ip2bin(inp, i, outp, o, addr_family);
}
}
diff --git a/filter/ulogd_filter_IP2HBIN.c b/filter/ulogd_filter_IP2HBIN.c
index 48ea6a2..42ffc94 100644
--- a/filter/ulogd_filter_IP2HBIN.c
+++ b/filter/ulogd_filter_IP2HBIN.c
@@ -112,17 +112,32 @@ static struct ulogd_key ip2hbin_keys[] = {
},
};
+static void ip2hbin(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+ uint8_t addr_family)
+{
+ switch (addr_family) {
+ case AF_INET6:
+ okey_set_u128(&outp[o], ikey_get_u128(&inp[i]));
+ break;
+ case AF_INET:
+ okey_set_u32(&outp[o], ntohl(ikey_get_u32(&inp[i])));
+ break;
+ }
+}
+
static int interp_ip2hbin(struct ulogd_pluginstance *pi)
{
- struct ulogd_key *ret = pi->output.keys;
+ struct ulogd_key *outp = pi->output.keys;
struct ulogd_key *inp = pi->input.keys;
- uint8_t family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
- uint8_t convfamily = family;
- int i;
+ uint8_t proto_family, addr_family;
+ int i, o;
- switch (family) {
- case AF_INET:
+ proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
+
+ switch (proto_family) {
case AF_INET6:
+ case AF_INET:
+ addr_family = proto_family;
break;
case AF_BRIDGE:
if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
@@ -132,13 +147,11 @@ static int interp_ip2hbin(struct ulogd_pluginstance *pi)
}
switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
case ETH_P_IPV6:
- convfamily = AF_INET6;
+ addr_family = AF_INET6;
break;
case ETH_P_IP:
- convfamily = AF_INET;
- break;
case ETH_P_ARP:
- convfamily = AF_INET;
+ addr_family = AF_INET;
break;
default:
ulogd_log(ULOGD_NOTICE,
@@ -147,26 +160,15 @@ static int interp_ip2hbin(struct ulogd_pluginstance *pi)
}
break;
default:
- ulogd_log(ULOGD_NOTICE,
- "Unknown protocol inside packet\n");
+ /* TODO handle error */
+ ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
return ULOGD_IRET_ERR;
}
/* Iter on all addr fields */
- for(i = START_KEY; i <= MAX_KEY; i++) {
+ for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
if (pp_is_valid(inp, i)) {
- switch (convfamily) {
- case AF_INET:
- okey_set_u32(&ret[i - START_KEY],
- ntohl(ikey_get_u32(&inp[i])));
- break;
- case AF_INET6:
- okey_set_u128(&ret[i - START_KEY],
- ikey_get_u128(&inp[i]));
- break;
- default:
- break;
- }
+ ip2hbin(inp, i, outp, o, addr_family);
}
}
diff --git a/filter/ulogd_filter_IP2STR.c b/filter/ulogd_filter_IP2STR.c
index fec892a..194a8b1 100644
--- a/filter/ulogd_filter_IP2STR.c
+++ b/filter/ulogd_filter_IP2STR.c
@@ -137,12 +137,44 @@ static struct ulogd_key ip2str_keys[] = {
static char ipstr_array[MAX_KEY - START_KEY + 1][INET6_ADDRSTRLEN];
-static int ip2str(struct ulogd_key *inp, int index, int oindex)
+static void ip2str(struct ulogd_key *inp, int i, struct ulogd_key *outp, int o,
+ uint8_t addr_family)
{
- char family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
- char convfamily = family;
+ union {
+ struct in6_addr in6;
+ struct in_addr in;
+ } addr;
- if (family == AF_BRIDGE) {
+ switch (addr_family) {
+ case AF_INET6:
+ memcpy(addr.in6.s6_addr, ikey_get_u128(&inp[i]),
+ sizeof(addr.in6.s6_addr));
+ break;
+ case AF_INET:
+ addr.in.s_addr = ikey_get_u32(&inp[i]);
+ break;
+ }
+
+ inet_ntop(addr_family, &addr, ipstr_array[o], sizeof(ipstr_array[o]));
+
+ okey_set_ptr(&outp[o], ipstr_array[o]);
+}
+
+static int interp_ip2str(struct ulogd_pluginstance *pi)
+{
+ struct ulogd_key *outp = pi->output.keys;
+ struct ulogd_key *inp = pi->input.keys;
+ uint8_t proto_family, addr_family;
+ int i, o;
+
+ proto_family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
+
+ switch (proto_family) {
+ case AF_INET6:
+ case AF_INET:
+ addr_family = proto_family;
+ break;
+ case AF_BRIDGE:
if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
ulogd_log(ULOGD_NOTICE,
"No protocol inside AF_BRIDGE packet\n");
@@ -150,56 +182,28 @@ static int ip2str(struct ulogd_key *inp, int index, int oindex)
}
switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
case ETH_P_IPV6:
- convfamily = AF_INET6;
+ addr_family = AF_INET6;
break;
case ETH_P_IP:
- convfamily = AF_INET;
- break;
case ETH_P_ARP:
- convfamily = AF_INET;
+ addr_family = AF_INET;
break;
default:
ulogd_log(ULOGD_NOTICE,
"Unknown protocol inside AF_BRIDGE packet\n");
return ULOGD_IRET_ERR;
}
- }
-
- switch (convfamily) {
- uint32_t ip;
- case AF_INET6:
- inet_ntop(AF_INET6,
- ikey_get_u128(&inp[index]),
- ipstr_array[oindex], sizeof(ipstr_array[oindex]));
- break;
- case AF_INET:
- ip = ikey_get_u32(&inp[index]);
- inet_ntop(AF_INET, &ip,
- ipstr_array[oindex], sizeof(ipstr_array[oindex]));
break;
default:
/* TODO error handling */
ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
return ULOGD_IRET_ERR;
}
- return ULOGD_IRET_OK;
-}
-
-static int interp_ip2str(struct ulogd_pluginstance *pi)
-{
- struct ulogd_key *ret = pi->output.keys;
- struct ulogd_key *inp = pi->input.keys;
- int i;
- int fret;
/* Iter on all addr fields */
- for (i = START_KEY; i <= MAX_KEY; i++) {
+ for (i = START_KEY, o = 0; i <= MAX_KEY; i++, o++) {
if (pp_is_valid(inp, i)) {
- fret = ip2str(inp, i, i - START_KEY);
- if (fret != ULOGD_IRET_OK)
- return fret;
- okey_set_ptr(&ret[i - START_KEY],
- ipstr_array[i-START_KEY]);
+ ip2str(inp, i, outp, o, addr_family);
}
}