Skip to content

Commit 4e7bf9e

Browse files
danielinuxdevuan
authored andcommitted
Fixed receiving TTL info OOB in ICMP sockets
1 parent 03c4087 commit 4e7bf9e

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

@@ -2407,21 +2401,18 @@ int wolfIP_sock_setsockopt(struct wolfIP *s, int sockfd, int level, int optname,
24072401
struct tsocket *ts = wolfIP_socket_from_fd(s, sockfd);
24082402
if (!ts)
24092403
return -WOLFIP_EINVAL;
2410-
#ifdef WOLF_POSIX
2411-
if (level == SOL_IP && optname == IP_RECVTTL) {
2404+
if (level == WOLFIP_SOL_IP && optname == WOLFIP_IP_RECVTTL) {
24122405
int enable;
24132406
if (!optval || optlen < (socklen_t)sizeof(int))
24142407
return -WOLFIP_EINVAL;
24152408
memcpy(&enable, optval, sizeof(int));
24162409
ts->recv_ttl = enable ? 1 : 0;
24172410
return 0;
24182411
}
2419-
#else
24202412
(void)level;
24212413
(void)optname;
24222414
(void)optval;
24232415
(void)optlen;
2424-
#endif
24252416
return 0;
24262417
}
24272418

@@ -2442,22 +2433,19 @@ int wolfIP_sock_getsockopt(struct wolfIP *s, int sockfd, int level, int optname,
24422433
struct tsocket *ts = wolfIP_socket_from_fd(s, sockfd);
24432434
if (!ts)
24442435
return -WOLFIP_EINVAL;
2445-
#ifdef WOLF_POSIX
2446-
if (level == SOL_IP && optname == IP_RECVTTL) {
2436+
if (level == WOLFIP_SOL_IP && optname == WOLFIP_IP_RECVTTL) {
24472437
int value;
24482438
if (!optval || !optlen || *optlen < (socklen_t)sizeof(int))
24492439
return -WOLFIP_EINVAL;
2450-
value = ts->recv_ttl ? 1 : 0;
2440+
value = ts->recv_ttl ? ts->last_pkt_ttl : 0;
24512441
memcpy(optval, &value, sizeof(int));
24522442
*optlen = sizeof(int);
24532443
return 0;
24542444
}
2455-
#else
24562445
(void)level;
24572446
(void)optname;
24582447
(void)optval;
24592448
(void)optlen;
2460-
#endif
24612449
return 0;
24622450
}
24632451
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)