Skip to content

Commit 3aa7033

Browse files
committed
Fixed receiving TTL info OOB in ICMP sockets
1 parent 1aa4c2d commit 3aa7033

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/port/posix/bsd_socket.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,16 @@ static void wolfip_fill_ttl_control(struct wolfIP *ipstack, int sockfd, struct m
236236
int ttl;
237237
int ttl_status;
238238
struct cmsghdr *cmsg;
239+
socklen_t ctrl_len;
239240

240241
if (!msg)
241242
return;
243+
ctrl_len = msg->msg_controllen;
242244
msg->msg_controllen = 0;
243245
ttl_status = wolfIP_sock_get_recv_ttl(ipstack, sockfd, &ttl);
244246
if (ttl_status <= 0)
245247
return;
246-
if (!msg->msg_control || msg->msg_controllen < (socklen_t)CMSG_SPACE(sizeof(int)))
248+
if (!msg->msg_control || ctrl_len < (socklen_t)CMSG_SPACE(sizeof(int)))
247249
return;
248250
cmsg = (struct cmsghdr *)msg->msg_control;
249251
cmsg->cmsg_level = SOL_IP;

src/wolfip.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
#include <stdint.h>
2323
#include <string.h>
2424
#include <stddef.h>
25-
#include <unistd.h>
26-
#ifdef WOLF_POSIX
27-
#include <poll.h>
28-
#include <sys/socket.h>
29-
#include <netinet/in.h>
30-
#endif
3125
#include "wolfip.h"
3226
#include "config.h"
3327

@@ -2364,21 +2358,18 @@ int wolfIP_sock_setsockopt(struct wolfIP *s, int sockfd, int level, int optname,
23642358
struct tsocket *ts = wolfIP_socket_from_fd(s, sockfd);
23652359
if (!ts)
23662360
return -WOLFIP_EINVAL;
2367-
#ifdef WOLF_POSIX
2368-
if (level == SOL_IP && optname == IP_RECVTTL) {
2361+
if (level == WOLFIP_SOL_IP && optname == WOLFIP_IP_RECVTTL) {
23692362
int enable;
23702363
if (!optval || optlen < (socklen_t)sizeof(int))
23712364
return -WOLFIP_EINVAL;
23722365
memcpy(&enable, optval, sizeof(int));
23732366
ts->recv_ttl = enable ? 1 : 0;
23742367
return 0;
23752368
}
2376-
#else
23772369
(void)level;
23782370
(void)optname;
23792371
(void)optval;
23802372
(void)optlen;
2381-
#endif
23822373
return 0;
23832374
}
23842375

@@ -2399,22 +2390,19 @@ int wolfIP_sock_getsockopt(struct wolfIP *s, int sockfd, int level, int optname,
23992390
struct tsocket *ts = wolfIP_socket_from_fd(s, sockfd);
24002391
if (!ts)
24012392
return -WOLFIP_EINVAL;
2402-
#ifdef WOLF_POSIX
2403-
if (level == SOL_IP && optname == IP_RECVTTL) {
2393+
if (level == WOLFIP_SOL_IP && optname == WOLFIP_IP_RECVTTL) {
24042394
int value;
24052395
if (!optval || !optlen || *optlen < (socklen_t)sizeof(int))
24062396
return -WOLFIP_EINVAL;
2407-
value = ts->recv_ttl ? 1 : 0;
2397+
value = ts->recv_ttl ? ts->last_pkt_ttl : 0;
24082398
memcpy(optval, &value, sizeof(int));
24092399
*optlen = sizeof(int);
24102400
return 0;
24112401
}
2412-
#else
24132402
(void)level;
24142403
(void)optname;
24152404
(void)optval;
24162405
(void)optlen;
2417-
#endif
24182406
return 0;
24192407
}
24202408
int wolfIP_sock_close(struct wolfIP *s, int sockfd)

wolfip.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ typedef unsigned long size_t;
2424
# define __size_t_defined
2525
#endif
2626

27+
#ifndef WOLFIP_SOL_IP
28+
# ifdef SOL_IP
29+
# define WOLFIP_SOL_IP SOL_IP
30+
# else
31+
# define WOLFIP_SOL_IP 0
32+
# endif
33+
#endif
34+
35+
#ifndef WOLFIP_IP_RECVTTL
36+
# ifdef IP_RECVTTL
37+
# define WOLFIP_IP_RECVTTL IP_RECVTTL
38+
# else
39+
# define WOLFIP_IP_RECVTTL 12
40+
# endif
41+
#endif
42+
2743
/* Types */
2844
struct wolfIP;
2945
typedef uint32_t ip4;

0 commit comments

Comments
 (0)