1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Test IPV6_FLOWINFO_MGR */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #define _GNU_SOURCE
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <arpa/inet.h>
7*4882a593Smuzhiyun #include <error.h>
8*4882a593Smuzhiyun #include <errno.h>
9*4882a593Smuzhiyun #include <limits.h>
10*4882a593Smuzhiyun #include <linux/in6.h>
11*4882a593Smuzhiyun #include <stdbool.h>
12*4882a593Smuzhiyun #include <stdio.h>
13*4882a593Smuzhiyun #include <stdint.h>
14*4882a593Smuzhiyun #include <stdlib.h>
15*4882a593Smuzhiyun #include <string.h>
16*4882a593Smuzhiyun #include <sys/socket.h>
17*4882a593Smuzhiyun #include <sys/stat.h>
18*4882a593Smuzhiyun #include <sys/time.h>
19*4882a593Smuzhiyun #include <sys/types.h>
20*4882a593Smuzhiyun #include <sys/wait.h>
21*4882a593Smuzhiyun #include <unistd.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* uapi/glibc weirdness may leave this undefined */
24*4882a593Smuzhiyun #ifndef IPV6_FLOWLABEL_MGR
25*4882a593Smuzhiyun #define IPV6_FLOWLABEL_MGR 32
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* from net/ipv6/ip6_flowlabel.c */
29*4882a593Smuzhiyun #define FL_MIN_LINGER 6
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define explain(x) \
32*4882a593Smuzhiyun do { if (cfg_verbose) fprintf(stderr, " " x "\n"); } while (0)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define __expect(x) \
35*4882a593Smuzhiyun do { \
36*4882a593Smuzhiyun if (!(x)) \
37*4882a593Smuzhiyun fprintf(stderr, "[OK] " #x "\n"); \
38*4882a593Smuzhiyun else \
39*4882a593Smuzhiyun error(1, 0, "[ERR] " #x " (line %d)", __LINE__); \
40*4882a593Smuzhiyun } while (0)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define expect_pass(x) __expect(x)
43*4882a593Smuzhiyun #define expect_fail(x) __expect(!(x))
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static bool cfg_long_running;
46*4882a593Smuzhiyun static bool cfg_verbose;
47*4882a593Smuzhiyun
flowlabel_get(int fd,uint32_t label,uint8_t share,uint16_t flags)48*4882a593Smuzhiyun static int flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun struct in6_flowlabel_req req = {
51*4882a593Smuzhiyun .flr_action = IPV6_FL_A_GET,
52*4882a593Smuzhiyun .flr_label = htonl(label),
53*4882a593Smuzhiyun .flr_flags = flags,
54*4882a593Smuzhiyun .flr_share = share,
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
58*4882a593Smuzhiyun req.flr_dst.s6_addr[0] = 0xfd;
59*4882a593Smuzhiyun req.flr_dst.s6_addr[15] = 0x1;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun return setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req));
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
flowlabel_put(int fd,uint32_t label)64*4882a593Smuzhiyun static int flowlabel_put(int fd, uint32_t label)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct in6_flowlabel_req req = {
67*4882a593Smuzhiyun .flr_action = IPV6_FL_A_PUT,
68*4882a593Smuzhiyun .flr_label = htonl(label),
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req));
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
run_tests(int fd)74*4882a593Smuzhiyun static void run_tests(int fd)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun int wstatus;
77*4882a593Smuzhiyun pid_t pid;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun explain("cannot get non-existent label");
80*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 1, IPV6_FL_S_ANY, 0));
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun explain("cannot put non-existent label");
83*4882a593Smuzhiyun expect_fail(flowlabel_put(fd, 1));
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun explain("cannot create label greater than 20 bits");
86*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 0x1FFFFF, IPV6_FL_S_ANY,
87*4882a593Smuzhiyun IPV6_FL_F_CREATE));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun explain("create a new label (FL_F_CREATE)");
90*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
91*4882a593Smuzhiyun explain("can get the label (without FL_F_CREATE)");
92*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, 0));
93*4882a593Smuzhiyun explain("can get it again with create flag set, too");
94*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
95*4882a593Smuzhiyun explain("cannot get it again with the exclusive (FL_FL_EXCL) flag");
96*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 1, IPV6_FL_S_ANY,
97*4882a593Smuzhiyun IPV6_FL_F_CREATE | IPV6_FL_F_EXCL));
98*4882a593Smuzhiyun explain("can now put exactly three references");
99*4882a593Smuzhiyun expect_pass(flowlabel_put(fd, 1));
100*4882a593Smuzhiyun expect_pass(flowlabel_put(fd, 1));
101*4882a593Smuzhiyun expect_pass(flowlabel_put(fd, 1));
102*4882a593Smuzhiyun expect_fail(flowlabel_put(fd, 1));
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun explain("create a new exclusive label (FL_S_EXCL)");
105*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 2, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE));
106*4882a593Smuzhiyun explain("cannot get it again in non-exclusive mode");
107*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
108*4882a593Smuzhiyun explain("cannot get it again in exclusive mode either");
109*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE));
110*4882a593Smuzhiyun expect_pass(flowlabel_put(fd, 2));
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (cfg_long_running) {
113*4882a593Smuzhiyun explain("cannot reuse the label, due to linger");
114*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_ANY,
115*4882a593Smuzhiyun IPV6_FL_F_CREATE));
116*4882a593Smuzhiyun explain("after sleep, can reuse");
117*4882a593Smuzhiyun sleep(FL_MIN_LINGER * 2 + 1);
118*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 2, IPV6_FL_S_ANY,
119*4882a593Smuzhiyun IPV6_FL_F_CREATE));
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun explain("create a new user-private label (FL_S_USER)");
123*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, IPV6_FL_F_CREATE));
124*4882a593Smuzhiyun explain("cannot get it again in non-exclusive mode");
125*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_ANY, 0));
126*4882a593Smuzhiyun explain("cannot get it again in exclusive mode");
127*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_EXCL, 0));
128*4882a593Smuzhiyun explain("can get it again in user mode");
129*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
130*4882a593Smuzhiyun explain("child process can get it too, but not after setuid(nobody)");
131*4882a593Smuzhiyun pid = fork();
132*4882a593Smuzhiyun if (pid == -1)
133*4882a593Smuzhiyun error(1, errno, "fork");
134*4882a593Smuzhiyun if (!pid) {
135*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
136*4882a593Smuzhiyun if (setuid(USHRT_MAX))
137*4882a593Smuzhiyun fprintf(stderr, "[INFO] skip setuid child test\n");
138*4882a593Smuzhiyun else
139*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
140*4882a593Smuzhiyun exit(0);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun if (wait(&wstatus) == -1)
143*4882a593Smuzhiyun error(1, errno, "wait");
144*4882a593Smuzhiyun if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
145*4882a593Smuzhiyun error(1, errno, "wait: unexpected child result");
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun explain("create a new process-private label (FL_S_PROCESS)");
148*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, IPV6_FL_F_CREATE));
149*4882a593Smuzhiyun explain("can get it again");
150*4882a593Smuzhiyun expect_pass(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, 0));
151*4882a593Smuzhiyun explain("child process cannot can get it");
152*4882a593Smuzhiyun pid = fork();
153*4882a593Smuzhiyun if (pid == -1)
154*4882a593Smuzhiyun error(1, errno, "fork");
155*4882a593Smuzhiyun if (!pid) {
156*4882a593Smuzhiyun expect_fail(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, 0));
157*4882a593Smuzhiyun exit(0);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun if (wait(&wstatus) == -1)
160*4882a593Smuzhiyun error(1, errno, "wait");
161*4882a593Smuzhiyun if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
162*4882a593Smuzhiyun error(1, errno, "wait: unexpected child result");
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
parse_opts(int argc,char ** argv)165*4882a593Smuzhiyun static void parse_opts(int argc, char **argv)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun int c;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun while ((c = getopt(argc, argv, "lv")) != -1) {
170*4882a593Smuzhiyun switch (c) {
171*4882a593Smuzhiyun case 'l':
172*4882a593Smuzhiyun cfg_long_running = true;
173*4882a593Smuzhiyun break;
174*4882a593Smuzhiyun case 'v':
175*4882a593Smuzhiyun cfg_verbose = true;
176*4882a593Smuzhiyun break;
177*4882a593Smuzhiyun default:
178*4882a593Smuzhiyun error(1, 0, "%s: parse error", argv[0]);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
main(int argc,char ** argv)183*4882a593Smuzhiyun int main(int argc, char **argv)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun int fd;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun parse_opts(argc, argv);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun fd = socket(PF_INET6, SOCK_DGRAM, 0);
190*4882a593Smuzhiyun if (fd == -1)
191*4882a593Smuzhiyun error(1, errno, "socket");
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun run_tests(fd);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (close(fd))
196*4882a593Smuzhiyun error(1, errno, "close");
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return 0;
199*4882a593Smuzhiyun }
200