Skip to content

Commit 96ac91d

Browse files
jukkarhenrikbrixandersen
authored andcommitted
net: Add support for adjusting IPv6 unicast hop limit
Add option support for adjusting the IPv6 unicast hop limit value. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent e397d19 commit 96ac91d

File tree

4 files changed

+92
-16
lines changed

4 files changed

+92
-16
lines changed

include/zephyr/net/net_context.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,20 +1122,21 @@ int net_context_update_recv_wnd(struct net_context *context,
11221122
int32_t delta);
11231123

11241124
enum net_context_option {
1125-
NET_OPT_PRIORITY = 1,
1126-
NET_OPT_TXTIME = 2,
1127-
NET_OPT_SOCKS5 = 3,
1128-
NET_OPT_RCVTIMEO = 4,
1129-
NET_OPT_SNDTIMEO = 5,
1130-
NET_OPT_RCVBUF = 6,
1131-
NET_OPT_SNDBUF = 7,
1132-
NET_OPT_DSCP_ECN = 8,
1133-
NET_OPT_REUSEADDR = 9,
1134-
NET_OPT_REUSEPORT = 10,
1135-
NET_OPT_IPV6_V6ONLY = 11,
1136-
NET_OPT_RECV_PKTINFO = 12,
1137-
NET_OPT_MCAST_TTL = 13,
1138-
NET_OPT_MCAST_HOP_LIMIT = 14,
1125+
NET_OPT_PRIORITY = 1,
1126+
NET_OPT_TXTIME = 2,
1127+
NET_OPT_SOCKS5 = 3,
1128+
NET_OPT_RCVTIMEO = 4,
1129+
NET_OPT_SNDTIMEO = 5,
1130+
NET_OPT_RCVBUF = 6,
1131+
NET_OPT_SNDBUF = 7,
1132+
NET_OPT_DSCP_ECN = 8,
1133+
NET_OPT_REUSEADDR = 9,
1134+
NET_OPT_REUSEPORT = 10,
1135+
NET_OPT_IPV6_V6ONLY = 11,
1136+
NET_OPT_RECV_PKTINFO = 12,
1137+
NET_OPT_MCAST_TTL = 13,
1138+
NET_OPT_MCAST_HOP_LIMIT = 14,
1139+
NET_OPT_UNICAST_HOP_LIMIT = 15,
11391140
};
11401141

11411142
/**

include/zephyr/net/socket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,9 @@ struct in_pktinfo {
11161116
#define IP_MULTICAST_TTL 33
11171117

11181118
/* Socket options for IPPROTO_IPV6 level */
1119+
/** sockopt: Set the unicast hop limit for the socket. */
1120+
#define IPV6_UNICAST_HOPS 16
1121+
11191122
/** sockopt: Set the multicast hop limit for the socket. */
11201123
#define IPV6_MULTICAST_HOPS 18
11211124

subsys/net/ip/net_context.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ LOG_MODULE_REGISTER(net_ctx, CONFIG_NET_CONTEXT_LOG_LEVEL);
5454
#define INITIAL_MCAST_HOP_LIMIT 1
5555
#endif
5656

57+
#ifdef CONFIG_NET_INITIAL_HOP_LIMIT
58+
#define INITIAL_HOP_LIMIT CONFIG_NET_INITIAL_HOP_LIMIT
59+
#else
60+
#define INITIAL_HOP_LIMIT 1
61+
#endif
62+
5763
#ifndef EPFNOSUPPORT
5864
/* Some old versions of newlib haven't got this defined in errno.h,
5965
* Just use EPROTONOSUPPORT in this case
@@ -481,6 +487,7 @@ int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t proto,
481487
break;
482488
}
483489

490+
contexts[i].ipv6_hop_limit = INITIAL_HOP_LIMIT;
484491
contexts[i].ipv6_mcast_hop_limit = INITIAL_MCAST_HOP_LIMIT;
485492
}
486493
if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) {
@@ -1124,8 +1131,6 @@ int net_context_create_ipv6_new(struct net_context *context,
11241131
(struct in6_addr *)dst);
11251132
}
11261133

1127-
net_pkt_set_ipv6_hop_limit(pkt,
1128-
net_context_get_ipv6_hop_limit(context));
11291134
#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
11301135
net_pkt_set_ip_dscp(pkt, net_ipv6_get_dscp(context->options.dscp_ecn));
11311136
net_pkt_set_ip_ecn(pkt, net_ipv6_get_ecn(context->options.dscp_ecn));
@@ -1600,6 +1605,26 @@ static int get_context_mcast_hop_limit(struct net_context *context,
16001605
#endif
16011606
}
16021607

1608+
static int get_context_unicast_hop_limit(struct net_context *context,
1609+
void *value, size_t *len)
1610+
{
1611+
#if defined(CONFIG_NET_IPV6)
1612+
*((int *)value) = context->ipv6_hop_limit;
1613+
1614+
if (len) {
1615+
*len = sizeof(int);
1616+
}
1617+
1618+
return 0;
1619+
#else
1620+
ARG_UNUSED(context);
1621+
ARG_UNUSED(value);
1622+
ARG_UNUSED(len);
1623+
1624+
return -ENOTSUP;
1625+
#endif
1626+
}
1627+
16031628
static int get_context_reuseaddr(struct net_context *context,
16041629
void *value, size_t *len)
16051630
{
@@ -2822,6 +2847,25 @@ static int set_context_mcast_hop_limit(struct net_context *context,
28222847
#endif
28232848
}
28242849

2850+
static int set_context_unicast_hop_limit(struct net_context *context,
2851+
const void *value, size_t len)
2852+
{
2853+
#if defined(CONFIG_NET_IPV6)
2854+
uint8_t unicast_hop_limit = *((int *)value);
2855+
2856+
len = sizeof(context->ipv6_hop_limit);
2857+
2858+
return set_uint8_option(&context->ipv6_hop_limit,
2859+
&unicast_hop_limit, len);
2860+
#else
2861+
ARG_UNUSED(context);
2862+
ARG_UNUSED(value);
2863+
ARG_UNUSED(len);
2864+
2865+
return -ENOTSUP;
2866+
#endif
2867+
}
2868+
28252869
static int set_context_reuseaddr(struct net_context *context,
28262870
const void *value, size_t len)
28272871
{
@@ -2923,6 +2967,9 @@ int net_context_set_option(struct net_context *context,
29232967
case NET_OPT_MCAST_HOP_LIMIT:
29242968
ret = set_context_mcast_hop_limit(context, value, len);
29252969
break;
2970+
case NET_OPT_UNICAST_HOP_LIMIT:
2971+
ret = set_context_unicast_hop_limit(context, value, len);
2972+
break;
29262973
case NET_OPT_REUSEADDR:
29272974
ret = set_context_reuseaddr(context, value, len);
29282975
break;
@@ -2987,6 +3034,9 @@ int net_context_get_option(struct net_context *context,
29873034
case NET_OPT_MCAST_HOP_LIMIT:
29883035
ret = get_context_mcast_hop_limit(context, value, len);
29893036
break;
3037+
case NET_OPT_UNICAST_HOP_LIMIT:
3038+
ret = get_context_unicast_hop_limit(context, value, len);
3039+
break;
29903040
case NET_OPT_REUSEADDR:
29913041
ret = get_context_reuseaddr(context, value, len);
29923042
break;

subsys/net/lib/sockets/sockets.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,17 @@ int zsock_getsockopt_ctx(struct net_context *ctx, int level, int optname,
25652565

25662566
break;
25672567

2568+
case IPV6_UNICAST_HOPS:
2569+
ret = net_context_get_option(ctx,
2570+
NET_OPT_UNICAST_HOP_LIMIT,
2571+
optval, optlen);
2572+
if (ret < 0) {
2573+
errno = -ret;
2574+
return -1;
2575+
}
2576+
2577+
return 0;
2578+
25682579
case IPV6_MULTICAST_HOPS:
25692580
ret = net_context_get_option(ctx,
25702581
NET_OPT_MCAST_HOP_LIMIT,
@@ -2969,6 +2980,17 @@ int zsock_setsockopt_ctx(struct net_context *ctx, int level, int optname,
29692980

29702981
break;
29712982

2983+
case IPV6_UNICAST_HOPS:
2984+
ret = net_context_set_option(ctx,
2985+
NET_OPT_UNICAST_HOP_LIMIT,
2986+
optval, optlen);
2987+
if (ret < 0) {
2988+
errno = -ret;
2989+
return -1;
2990+
}
2991+
2992+
return 0;
2993+
29722994
case IPV6_MULTICAST_HOPS:
29732995
ret = net_context_set_option(ctx,
29742996
NET_OPT_MCAST_HOP_LIMIT,

0 commit comments

Comments
 (0)