Skip to content

Commit b4a8e3f

Browse files
jukkarhenrikbrixandersen
authored andcommitted
net: socket: Add support for adjusting IPv4 TTL
The IPv4 TTL could only manipulated via net_context interface. It makes sense to allow the same from socket interface via the setsockopt/getsockopt calls. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 7474007 commit b4a8e3f

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

include/zephyr/net/net_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ enum net_context_option {
11371137
NET_OPT_MCAST_TTL = 13,
11381138
NET_OPT_MCAST_HOP_LIMIT = 14,
11391139
NET_OPT_UNICAST_HOP_LIMIT = 15,
1140+
NET_OPT_TTL = 16,
11401141
};
11411142

11421143
/**

include/zephyr/net/socket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,9 @@ struct ifreq {
11001100
/** sockopt: Set or receive the Type-Of-Service value for an outgoing packet. */
11011101
#define IP_TOS 1
11021102

1103+
/** sockopt: Set or receive the Time-To-Live value for an outgoing packet. */
1104+
#define IP_TTL 2
1105+
11031106
/** sockopt: Pass an IP_PKTINFO ancillary message that contains a
11041107
* pktinfo structure that supplies some information about the
11051108
* incoming packet.

subsys/net/ip/net_context.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ LOG_MODULE_REGISTER(net_ctx, CONFIG_NET_CONTEXT_LOG_LEVEL);
4848
#define INITIAL_MCAST_TTL 1
4949
#endif
5050

51+
#ifdef CONFIG_NET_INITIAL_TTL
52+
#define INITIAL_TTL CONFIG_NET_INITIAL_TTL
53+
#else
54+
#define INITIAL_TTL 1
55+
#endif
56+
5157
#ifdef CONFIG_NET_INITIAL_MCAST_HOP_LIMIT
5258
#define INITIAL_MCAST_HOP_LIMIT CONFIG_NET_INITIAL_MCAST_HOP_LIMIT
5359
#else
@@ -501,6 +507,7 @@ int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t proto,
501507
break;
502508
}
503509

510+
contexts[i].ipv4_ttl = INITIAL_TTL;
504511
contexts[i].ipv4_mcast_ttl = INITIAL_MCAST_TTL;
505512
}
506513
}
@@ -1097,7 +1104,6 @@ int net_context_create_ipv4_new(struct net_context *context,
10971104
}
10981105
}
10991106

1100-
net_pkt_set_ipv4_ttl(pkt, net_context_get_ipv4_ttl(context));
11011107
#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
11021108
net_pkt_set_ip_dscp(pkt, net_ipv4_get_dscp(context->options.dscp_ecn));
11031109
net_pkt_set_ip_ecn(pkt, net_ipv4_get_ecn(context->options.dscp_ecn));
@@ -1565,6 +1571,26 @@ static int get_context_dscp_ecn(struct net_context *context,
15651571
#endif
15661572
}
15671573

1574+
static int get_context_ttl(struct net_context *context,
1575+
void *value, size_t *len)
1576+
{
1577+
#if defined(CONFIG_NET_IPV4)
1578+
*((int *)value) = context->ipv4_ttl;
1579+
1580+
if (len) {
1581+
*len = sizeof(int);
1582+
}
1583+
1584+
return 0;
1585+
#else
1586+
ARG_UNUSED(context);
1587+
ARG_UNUSED(value);
1588+
ARG_UNUSED(len);
1589+
1590+
return -ENOTSUP;
1591+
#endif
1592+
}
1593+
15681594
static int get_context_mcast_ttl(struct net_context *context,
15691595
void *value, size_t *len)
15701596
{
@@ -2793,6 +2819,24 @@ static int set_context_dscp_ecn(struct net_context *context,
27932819
#endif
27942820
}
27952821

2822+
static int set_context_ttl(struct net_context *context,
2823+
const void *value, size_t len)
2824+
{
2825+
#if defined(CONFIG_NET_IPV4)
2826+
uint8_t ttl = *((int *)value);
2827+
2828+
len = sizeof(context->ipv4_ttl);
2829+
2830+
return set_uint8_option(&context->ipv4_ttl, &ttl, len);
2831+
#else
2832+
ARG_UNUSED(context);
2833+
ARG_UNUSED(value);
2834+
ARG_UNUSED(len);
2835+
2836+
return -ENOTSUP;
2837+
#endif
2838+
}
2839+
27962840
static int set_context_mcast_ttl(struct net_context *context,
27972841
const void *value, size_t len)
27982842
{
@@ -2961,6 +3005,9 @@ int net_context_set_option(struct net_context *context,
29613005
case NET_OPT_DSCP_ECN:
29623006
ret = set_context_dscp_ecn(context, value, len);
29633007
break;
3008+
case NET_OPT_TTL:
3009+
ret = set_context_ttl(context, value, len);
3010+
break;
29643011
case NET_OPT_MCAST_TTL:
29653012
ret = set_context_mcast_ttl(context, value, len);
29663013
break;
@@ -3028,6 +3075,9 @@ int net_context_get_option(struct net_context *context,
30283075
case NET_OPT_DSCP_ECN:
30293076
ret = get_context_dscp_ecn(context, value, len);
30303077
break;
3078+
case NET_OPT_TTL:
3079+
ret = get_context_ttl(context, value, len);
3080+
break;
30313081
case NET_OPT_MCAST_TTL:
30323082
ret = get_context_mcast_ttl(context, value, len);
30333083
break;

subsys/net/lib/sockets/sockets.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,16 @@ int zsock_getsockopt_ctx(struct net_context *ctx, int level, int optname,
25182518

25192519
break;
25202520

2521+
case IP_TTL:
2522+
ret = net_context_get_option(ctx, NET_OPT_TTL,
2523+
optval, optlen);
2524+
if (ret < 0) {
2525+
errno = -ret;
2526+
return -1;
2527+
}
2528+
2529+
return 0;
2530+
25212531
case IP_MULTICAST_TTL:
25222532
ret = net_context_get_option(ctx, NET_OPT_MCAST_TTL,
25232533
optval, optlen);
@@ -2926,6 +2936,16 @@ int zsock_setsockopt_ctx(struct net_context *ctx, int level, int optname,
29262936
return -1;
29272937
}
29282938

2939+
return 0;
2940+
2941+
case IP_TTL:
2942+
ret = net_context_set_option(ctx, NET_OPT_TTL,
2943+
optval, optlen);
2944+
if (ret < 0) {
2945+
errno = -ret;
2946+
return -1;
2947+
}
2948+
29292949
return 0;
29302950
}
29312951

0 commit comments

Comments
 (0)