Skip to content

Commit 8801f15

Browse files
committed
test: add static tests for ICMP socket
Add ZDTM static tests for IP4/ICMP and IP6/ICMP socket feature. Signed-off-by: समीर सिंह Sameer Singh <[email protected]>
1 parent 8d0496c commit 8801f15

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

test/zdtm/static/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ TST_NOFILE := \
3737
socket_udp-corked \
3838
socket6_udp \
3939
socket_udp_shutdown \
40+
socket_icmp \
41+
socket6_icmp \
4042
sk-freebind \
4143
sk-freebind-false \
4244
socket_udplite \

test/zdtm/static/socket6_icmp.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include "zdtmtst.h"
2+
#include "sysctl.h"
3+
4+
const char *test_doc = "static test for IP6/ICMP socket\n";
5+
const char *test_author = "समीर सिंह Sameer Singh <[email protected]>\n";
6+
7+
/* Description:
8+
* Send a ping to localhost using IP6/ICMP socket
9+
*/
10+
11+
#include <string.h>
12+
#include <unistd.h>
13+
#include <sys/socket.h>
14+
#include <netinet/in.h>
15+
#include <netinet/icmp6.h>
16+
#include <arpa/inet.h>
17+
#include <sys/time.h>
18+
#include <netdb.h>
19+
20+
#define PACKET_SIZE 64
21+
#define RECV_TIMEOUT 1
22+
23+
static int echo_id = 1234;
24+
25+
int main(int argc, char **argv)
26+
{
27+
int ret, sock, seq = 0, recv_len = 0;
28+
char packet[PACKET_SIZE], recv_packet[PACKET_SIZE];
29+
30+
struct timeval tv;
31+
struct icmp6_hdr icmp_header, *icmp_reply;
32+
struct sockaddr_in6 addr, recv_addr;
33+
socklen_t addr_len;
34+
35+
test_init(argc, argv);
36+
37+
// Allow GIDs 0-58468 to open an unprivileged ICMP socket
38+
if (sysctl_write_str("/proc/sys/net/ipv4/ping_group_range", "0 58468")) {
39+
pr_perror("sysctl_write_str() failed");
40+
return 1;
41+
}
42+
43+
sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
44+
if (sock < 0) {
45+
pr_perror("Can't create socket");
46+
return 1;
47+
}
48+
49+
tv.tv_sec = RECV_TIMEOUT;
50+
tv.tv_usec = 0;
51+
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
52+
pr_perror("Can't set socket option");
53+
return 1;
54+
}
55+
56+
memset(&addr, 0, sizeof(addr));
57+
addr.sin6_family = AF_INET6;
58+
inet_pton(AF_INET6, "::1", &addr.sin6_addr);
59+
60+
memset(&icmp_header, 0, sizeof(icmp_header));
61+
icmp_header.icmp6_type = ICMP6_ECHO_REQUEST;
62+
icmp_header.icmp6_code = 0;
63+
icmp_header.icmp6_id = echo_id;
64+
icmp_header.icmp6_seq = seq;
65+
66+
memcpy(packet, &icmp_header, sizeof(icmp_header));
67+
memset(packet + sizeof(icmp_header), 0xa5,
68+
PACKET_SIZE - sizeof(icmp_header));
69+
70+
test_daemon();
71+
test_waitsig();
72+
73+
ret = sendto(sock, packet, PACKET_SIZE, 0,
74+
(struct sockaddr *)&addr, sizeof(addr));
75+
76+
if (ret < 0) {
77+
pr_perror("Can't send");
78+
return 1;
79+
}
80+
81+
addr_len = sizeof(recv_addr);
82+
83+
recv_len = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
84+
(struct sockaddr *)&recv_addr, &addr_len);
85+
86+
if (recv_len < 0) {
87+
pr_perror("Can't recv");
88+
return 1;
89+
}
90+
91+
icmp_reply = (struct icmp6_hdr *)recv_packet;
92+
93+
if (icmp_reply->icmp6_type != ICMP6_ECHO_REPLY) {
94+
fail("Got no ICMP_ECHO_REPLY");
95+
return 1;
96+
}
97+
98+
close(sock);
99+
pass();
100+
return 0;
101+
}

test/zdtm/static/socket6_icmp.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'flags': 'suid', 'flavor': 'h ns'}

test/zdtm/static/socket_icmp.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "zdtmtst.h"
2+
#include "sysctl.h"
3+
4+
const char *test_doc = "static test for ICMP socket\n";
5+
const char *test_author = "समीर सिंह Sameer Singh <[email protected]>\n";
6+
7+
/* Description:
8+
* Send a ping to localhost using ICMP socket
9+
*/
10+
11+
#include <string.h>
12+
#include <unistd.h>
13+
#include <sys/socket.h>
14+
#include <netinet/in.h>
15+
#include <netinet/ip_icmp.h>
16+
#include <arpa/inet.h>
17+
#include <sys/time.h>
18+
#include <netdb.h>
19+
20+
#define PACKET_SIZE 64
21+
#define RECV_TIMEOUT 1
22+
23+
static int echo_id = 1234;
24+
25+
int main(int argc, char **argv)
26+
{
27+
int ret, sock, seq = 0;
28+
char packet[PACKET_SIZE], recv_packet[PACKET_SIZE];
29+
30+
struct timeval tv;
31+
struct icmphdr icmp_header, *icmp_reply;
32+
struct sockaddr_in addr, recv_addr;
33+
socklen_t addr_len;
34+
35+
test_init(argc, argv);
36+
37+
// Allow GIDs 0-58468 to open an unprivileged ICMP socket
38+
if (sysctl_write_str("/proc/sys/net/ipv4/ping_group_range", "0 58468")) {
39+
pr_perror("sysctl_write_str() failed");
40+
return 1;
41+
}
42+
43+
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
44+
if (sock < 0) {
45+
pr_perror("Can't create socket");
46+
return 1;
47+
}
48+
49+
tv.tv_sec = RECV_TIMEOUT;
50+
tv.tv_usec = 0;
51+
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
52+
pr_perror("Can't set socket option");
53+
return 1;
54+
}
55+
56+
memset(&addr, 0, sizeof(addr));
57+
addr.sin_family = AF_INET;
58+
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
59+
60+
memset(&icmp_header, 0, sizeof(icmp_header));
61+
icmp_header.type = ICMP_ECHO;
62+
icmp_header.code = 0;
63+
icmp_header.un.echo.id = echo_id;
64+
icmp_header.un.echo.sequence = seq;
65+
66+
memcpy(packet, &icmp_header, sizeof(icmp_header));
67+
memset(packet + sizeof(icmp_header), 0xa5,
68+
PACKET_SIZE - sizeof(icmp_header));
69+
70+
test_daemon();
71+
test_waitsig();
72+
73+
ret = sendto(sock, packet, PACKET_SIZE, 0,
74+
(struct sockaddr *)&addr, sizeof(addr));
75+
76+
if (ret < 0) {
77+
fail("Can't send");
78+
return 1;
79+
}
80+
81+
addr_len = sizeof(recv_addr);
82+
83+
ret = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
84+
(struct sockaddr *)&recv_addr, &addr_len);
85+
86+
if (ret < 0) {
87+
fail("Can't recv");
88+
return 1;
89+
}
90+
91+
icmp_reply = (struct icmphdr *)recv_packet;
92+
93+
if (icmp_reply->type != ICMP_ECHOREPLY) {
94+
fail("Got no ICMP_ECHO_REPLY");
95+
return 1;
96+
}
97+
98+
close(sock);
99+
100+
pass();
101+
return 0;
102+
}

test/zdtm/static/socket_icmp.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'flags': 'suid', 'flavor': 'h ns'}

0 commit comments

Comments
 (0)