Skip to content

Commit 4388cb1

Browse files
Cristib05jhedberg
authored andcommitted
net: context: Add support for setting receive hop limit option
Add support for setting IPV6_RECVHOPLIMIT or IP_RECVTTL socket option. Signed-off-by: Cristian Bulacu <[email protected]>
1 parent d00a734 commit 4388cb1

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

include/zephyr/net/net_context.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Copyright (c) 2016 Intel Corporation
99
* Copyright (c) 2021 Nordic Semiconductor
1010
* Copyright (c) 2025 Aerlync Labs Inc.
11+
* Copyright 2025 NXP
1112
*
1213
* SPDX-License-Identifier: Apache-2.0
1314
*/
@@ -370,6 +371,10 @@ __net_socket struct net_context {
370371
/** Receive network packet information in recvmsg() call */
371372
bool recv_pktinfo;
372373
#endif
374+
#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
375+
/** Receive IPv6 hop limit or IPv4 TTL as ancillary data in recvmsg() call */
376+
bool recv_hoplimit;
377+
#endif
373378
#if defined(CONFIG_NET_IPV6)
374379
/**
375380
* Source address selection preferences. Currently used only for IPv6,
@@ -1399,6 +1404,7 @@ enum net_context_option {
13991404
NET_OPT_LOCAL_PORT_RANGE = 21, /**< Clamp local port range */
14001405
NET_OPT_IPV6_MCAST_LOOP = 22, /**< IPV6 multicast loop */
14011406
NET_OPT_IPV4_MCAST_LOOP = 23, /**< IPV4 multicast loop */
1407+
NET_OPT_RECV_HOPLIMIT = 24, /**< Receive hop limit information */
14021408
};
14031409

14041410
/**

subsys/net/ip/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) 2016 Intel Corporation.
44
# Copyright (c) 2021 Nordic Semiconductor
55
# Copyright (c) 2023 Arm Limited (or its affiliates). All rights reserved.
6+
# Copyright 2025 NXP
67
# SPDX-License-Identifier: Apache-2.0
78

89
menu "IP stack"
@@ -608,6 +609,14 @@ config NET_CONTEXT_RECV_PKTINFO
608609
This way user can get extra information about the received data in the
609610
socket.
610611

612+
config NET_CONTEXT_RECV_HOPLIMIT
613+
bool "Add receive hop limit (IPv6) or TTL (IPv4) support to net_context"
614+
depends on NET_UDP
615+
help
616+
Allow to set the IPV6_RECVHOPLIMIT or IP_RECVTTL flags on a socket.
617+
This way user can get extra information about hop limit (IPv6) or TTL (IPv4) in the
618+
socket.
619+
611620
config NET_CONTEXT_TIMESTAMPING
612621
bool "Add TIMESTAMPING support to net_context"
613622
default y if (NET_UDP && NET_PKT_TIMESTAMP)

subsys/net/ip/net_context.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Copyright (c) 2016 Intel Corporation
99
* Copyright (c) 2021 Nordic Semiconductor
1010
* Copyright (c) 2025 Aerlync Labs Inc.
11+
* Copyright 2025 NXP
1112
*
1213
* SPDX-License-Identifier: Apache-2.0
1314
*/
@@ -130,6 +131,17 @@ bool net_context_is_recv_pktinfo_set(struct net_context *context)
130131
#endif
131132
}
132133

134+
bool net_context_is_recv_hoplimit_set(struct net_context *context)
135+
{
136+
#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
137+
return context->options.recv_hoplimit;
138+
#else
139+
ARG_UNUSED(context);
140+
141+
return false;
142+
#endif
143+
}
144+
133145
bool net_context_is_timestamping_set(struct net_context *context)
134146
{
135147
#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
@@ -1919,6 +1931,21 @@ static int get_context_recv_pktinfo(struct net_context *context,
19191931
#endif
19201932
}
19211933

1934+
static int get_context_recv_hoplimit(struct net_context *context,
1935+
void *value, size_t *len)
1936+
{
1937+
#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
1938+
return get_bool_option(context->options.recv_hoplimit,
1939+
value, len);
1940+
#else
1941+
ARG_UNUSED(context);
1942+
ARG_UNUSED(value);
1943+
ARG_UNUSED(len);
1944+
1945+
return -ENOTSUP;
1946+
#endif
1947+
}
1948+
19221949
static int get_context_addr_preferences(struct net_context *context,
19231950
void *value, size_t *len)
19241951
{
@@ -3653,6 +3680,24 @@ static int set_context_recv_pktinfo(struct net_context *context,
36533680
#endif
36543681
}
36553682

3683+
static int set_context_recv_hoplimit(struct net_context *context,
3684+
const void *value, size_t len)
3685+
{
3686+
#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
3687+
if (net_context_get_type(context) == SOCK_DGRAM) {
3688+
return set_bool_option(&context->options.recv_hoplimit, value, len);
3689+
}
3690+
3691+
return -ENOTSUP;
3692+
#else
3693+
ARG_UNUSED(context);
3694+
ARG_UNUSED(value);
3695+
ARG_UNUSED(len);
3696+
3697+
return -ENOTSUP;
3698+
#endif
3699+
}
3700+
36563701
static int set_context_addr_preferences(struct net_context *context,
36573702
const void *value, size_t len)
36583703
{
@@ -3877,6 +3922,9 @@ int net_context_set_option(struct net_context *context,
38773922
case NET_OPT_IPV4_MCAST_LOOP:
38783923
ret = set_context_ipv4_mcast_loop(context, value, len);
38793924
break;
3925+
case NET_OPT_RECV_HOPLIMIT:
3926+
ret = set_context_recv_hoplimit(context, value, len);
3927+
break;
38803928
}
38813929

38823930
k_mutex_unlock(&context->lock);
@@ -3968,6 +4016,9 @@ int net_context_get_option(struct net_context *context,
39684016
case NET_OPT_IPV4_MCAST_LOOP:
39694017
ret = get_context_ipv4_mcast_loop(context, value, len);
39704018
break;
4019+
case NET_OPT_RECV_HOPLIMIT:
4020+
ret = get_context_recv_hoplimit(context, value, len);
4021+
break;
39714022
}
39724023

39734024
k_mutex_unlock(&context->lock);

subsys/net/ip/net_private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extern bool net_context_is_reuseaddr_set(struct net_context *context);
9696
extern bool net_context_is_reuseport_set(struct net_context *context);
9797
extern bool net_context_is_v6only_set(struct net_context *context);
9898
extern bool net_context_is_recv_pktinfo_set(struct net_context *context);
99+
extern bool net_context_is_recv_hoplimit_set(struct net_context *context);
99100
extern bool net_context_is_timestamping_set(struct net_context *context);
100101
extern void net_pkt_init(void);
101102
int net_context_get_local_addr(struct net_context *context,
@@ -124,6 +125,11 @@ static inline bool net_context_is_recv_pktinfo_set(struct net_context *context)
124125
ARG_UNUSED(context);
125126
return false;
126127
}
128+
static inline bool net_context_is_recv_hoplimit_set(struct net_context *context)
129+
{
130+
ARG_UNUSED(context);
131+
return false;
132+
}
127133
static inline bool net_context_is_timestamping_set(struct net_context *context)
128134
{
129135
ARG_UNUSED(context);

0 commit comments

Comments
 (0)