1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci * Copyright (c) 2016 Michal Kubecek <mkubecek@suse.cz>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * This is a regression test for kernel commit:
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * 197c949e7798  udp: properly support MSG_PEEK with truncated buffers
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * NOTE: The testcase will hang on upatched stable kernel.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <string.h>
16f08c3bdfSopenharmony_ci#include <stdio.h>
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include <sys/socket.h>
21f08c3bdfSopenharmony_ci#include <netinet/in.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "lapi/socket.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic const char msg[] = "Michael Gilfix was here\341\210\264\r\n";
27f08c3bdfSopenharmony_cistatic const unsigned msglen = ARRAY_SIZE(msg) - 1;
28f08c3bdfSopenharmony_cistatic unsigned char buff[25];
29f08c3bdfSopenharmony_cistatic const int bufflen = ARRAY_SIZE(buff);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic int sdr, sdw;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void verify_recvmsg(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	struct sockaddr_in6 addr_init = {
36f08c3bdfSopenharmony_ci		.sin6_family	= AF_INET6,
37f08c3bdfSopenharmony_ci		.sin6_port	= htons(0),
38f08c3bdfSopenharmony_ci		.sin6_addr	= IN6ADDR_LOOPBACK_INIT,
39f08c3bdfSopenharmony_ci	};
40f08c3bdfSopenharmony_ci	struct sockaddr_in6 addr_r, addr_w, addr_f;
41f08c3bdfSopenharmony_ci	socklen_t addrlen_r, addrlen_w;
42f08c3bdfSopenharmony_ci	struct iovec iov = {
43f08c3bdfSopenharmony_ci		.iov_base	= buff,
44f08c3bdfSopenharmony_ci		.iov_len	= sizeof(buff),
45f08c3bdfSopenharmony_ci	};
46f08c3bdfSopenharmony_ci	struct msghdr msghdr = {
47f08c3bdfSopenharmony_ci		.msg_name	= &addr_f,
48f08c3bdfSopenharmony_ci		.msg_namelen	= sizeof(addr_f),
49f08c3bdfSopenharmony_ci		.msg_iov	= &iov,
50f08c3bdfSopenharmony_ci		.msg_iovlen	= 1,
51f08c3bdfSopenharmony_ci		.msg_control	= NULL,
52f08c3bdfSopenharmony_ci		.msg_controllen	= 0,
53f08c3bdfSopenharmony_ci		.msg_flags	= 0,
54f08c3bdfSopenharmony_ci	};
55f08c3bdfSopenharmony_ci	int R;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	sdr = SAFE_SOCKET(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_IP);
58f08c3bdfSopenharmony_ci	SAFE_BIND(sdr, (struct sockaddr*)&addr_init, sizeof(addr_init));
59f08c3bdfSopenharmony_ci	addrlen_r = sizeof(addr_r);
60f08c3bdfSopenharmony_ci	SAFE_GETSOCKNAME(sdr, (struct sockaddr*)&addr_r, &addrlen_r);
61f08c3bdfSopenharmony_ci	sdw = SAFE_SOCKET(PF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP);
62f08c3bdfSopenharmony_ci	SAFE_BIND(sdw, (struct sockaddr*)&addr_init, sizeof(addr_init));
63f08c3bdfSopenharmony_ci	addrlen_w = sizeof(addr_w);
64f08c3bdfSopenharmony_ci	SAFE_GETSOCKNAME(sdw, (struct sockaddr*)&addr_w, &addrlen_w);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	R = sendto(sdw, msg, msglen, 0, (struct sockaddr*)&addr_r, addrlen_r);
67f08c3bdfSopenharmony_ci	if (R < 0)
68f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "sendto()");
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	R = recvmsg(sdr, &msghdr, MSG_PEEK);
71f08c3bdfSopenharmony_ci	if (R < 0) {
72f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "recvmsg(..., MSG_PEEK)");
73f08c3bdfSopenharmony_ci		return;
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	tst_res(TINFO, "received %d bytes", R);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if ((R == bufflen) && !memcmp(msg, buff, R))
79f08c3bdfSopenharmony_ci		tst_res(TPASS, "recvmsg(..., MSG_PEEK) works fine");
80f08c3bdfSopenharmony_ci	else
81f08c3bdfSopenharmony_ci		tst_res(TPASS, "recvmsg(..., MSG_PEEK) failed");
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	SAFE_CLOSE(sdw);
84f08c3bdfSopenharmony_ci	SAFE_CLOSE(sdr);
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic void cleanup(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci	if (sdw > 0)
90f08c3bdfSopenharmony_ci		SAFE_CLOSE(sdw);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	if (sdr > 0)
93f08c3bdfSopenharmony_ci		SAFE_CLOSE(sdr);
94f08c3bdfSopenharmony_ci}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_cistatic struct tst_test test = {
97f08c3bdfSopenharmony_ci	.test_all = verify_recvmsg,
98f08c3bdfSopenharmony_ci	.cleanup = cleanup,
99f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
100f08c3bdfSopenharmony_ci		{"linux-git", "197c949e7798"},
101f08c3bdfSopenharmony_ci		{}
102f08c3bdfSopenharmony_ci	}
103f08c3bdfSopenharmony_ci};
104