net: capture: enable capturing of IPv6/v4 only

Net capturing would not link when either IPv6 or IPv4 was switched off
due to missing symbols. This change allows for capturing a single IP
protocol only.

To reproduce:

samples/net/sockets/http_client/prj.conf
```
-CONFIG_NET_IPV4=y
+CONFIG_NET_IPV4=n
-CONFIG_NET_CONFIG_NEED_IPV4=y
+CONFIG_NET_CONFIG_NEED_IPV4=n
+
+CONFIG_NET_CAPTURE=y
```

This will cause the following linker error:
```
.../subsys/net/lib/capture/capture.c:648:
    undefined reference to `net_if_ipv4_get_ttl'
```

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
This commit is contained in:
Florian Grandel 2024-08-22 00:10:41 +02:00 committed by Carles Cufí
parent c19feadbdb
commit 9d1874f7f9

View file

@ -618,9 +618,9 @@ static int capture_send(const struct device *dev, struct net_if *iface,
return -ENOENT;
}
if (ctx->local.sa_family == AF_INET) {
if (IS_ENABLED(CONFIG_NET_IPV4) && ctx->local.sa_family == AF_INET) {
len = sizeof(struct net_ipv4_hdr);
} else if (ctx->local.sa_family == AF_INET6) {
} else if (IS_ENABLED(CONFIG_NET_IPV6) && ctx->local.sa_family == AF_INET6) {
len = sizeof(struct net_ipv6_hdr);
} else {
return -EINVAL;
@ -644,15 +644,17 @@ static int capture_send(const struct device *dev, struct net_if *iface,
return ret;
}
if (ctx->local.sa_family == AF_INET) {
if (IS_ENABLED(CONFIG_NET_IPV4) && ctx->local.sa_family == AF_INET) {
net_pkt_set_ipv4_ttl(ip,
net_if_ipv4_get_ttl(ctx->tunnel_iface));
ret = net_ipv4_create(ip, &net_sin(&ctx->local)->sin_addr,
&net_sin(&ctx->peer)->sin_addr);
} else {
} else if (IS_ENABLED(CONFIG_NET_IPV6) && ctx->local.sa_family == AF_INET6) {
ret = net_ipv6_create(ip, &net_sin6(&ctx->local)->sin6_addr,
&net_sin6(&ctx->peer)->sin6_addr);
} else {
CODE_UNREACHABLE;
}
if (ret < 0) {
@ -679,16 +681,18 @@ static int capture_send(const struct device *dev, struct net_if *iface,
net_pkt_cursor_init(pkt);
if (ctx->local.sa_family == AF_INET) {
if (IS_ENABLED(CONFIG_NET_IPV4) && ctx->local.sa_family == AF_INET) {
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
net_pkt_set_ipv4_opts_len(pkt, 0);
net_ipv4_finalize(pkt, IPPROTO_UDP);
} else {
} else if (IS_ENABLED(CONFIG_NET_IPV6) && ctx->local.sa_family == AF_INET6) {
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
net_pkt_set_ipv6_ext_opt_len(pkt, 0);
net_ipv6_finalize(pkt, IPPROTO_UDP);
} else {
CODE_UNREACHABLE;
}
if (DEBUG_TX) {