1 // SPDX-License-Identifier: GPL-2.0
2 /* Test IPV6_FLOWINFO_MGR */
10 #include <linux/in6.h>
16 #include <sys/socket.h>
19 #include <sys/types.h>
23 /* uapi/glibc weirdness may leave this undefined */
24 #ifndef IPV6_FLOWLABEL_MGR
25 #define IPV6_FLOWLABEL_MGR 32
28 /* from net/ipv6/ip6_flowlabel.c */
29 #define FL_MIN_LINGER 6
32 do { if (cfg_verbose) fprintf(stderr, " " x "\n"); } while (0)
37 fprintf(stderr, "[OK] " #x "\n"); \
39 error(1, 0, "[ERR] " #x " (line %d)", __LINE__); \
42 #define expect_pass(x) __expect(x)
43 #define expect_fail(x) __expect(!(x))
45 static bool cfg_long_running;
46 static bool cfg_verbose;
48 static int flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
50 struct in6_flowlabel_req req = {
51 .flr_action = IPV6_FL_A_GET,
52 .flr_label = htonl(label),
57 /* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
58 req.flr_dst.s6_addr[0] = 0xfd;
59 req.flr_dst.s6_addr[15] = 0x1;
61 return setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req));
64 static int flowlabel_put(int fd, uint32_t label)
66 struct in6_flowlabel_req req = {
67 .flr_action = IPV6_FL_A_PUT,
68 .flr_label = htonl(label),
71 return setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req));
74 static void run_tests(int fd)
79 explain("cannot get non-existent label");
80 expect_fail(flowlabel_get(fd, 1, IPV6_FL_S_ANY, 0));
82 explain("cannot put non-existent label");
83 expect_fail(flowlabel_put(fd, 1));
85 explain("cannot create label greater than 20 bits");
86 expect_fail(flowlabel_get(fd, 0x1FFFFF, IPV6_FL_S_ANY,
89 explain("create a new label (FL_F_CREATE)");
90 expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
91 explain("can get the label (without FL_F_CREATE)");
92 expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, 0));
93 explain("can get it again with create flag set, too");
94 expect_pass(flowlabel_get(fd, 1, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
95 explain("cannot get it again with the exclusive (FL_FL_EXCL) flag");
96 expect_fail(flowlabel_get(fd, 1, IPV6_FL_S_ANY,
97 IPV6_FL_F_CREATE | IPV6_FL_F_EXCL));
98 explain("can now put exactly three references");
99 expect_pass(flowlabel_put(fd, 1));
100 expect_pass(flowlabel_put(fd, 1));
101 expect_pass(flowlabel_put(fd, 1));
102 expect_fail(flowlabel_put(fd, 1));
104 explain("create a new exclusive label (FL_S_EXCL)");
105 expect_pass(flowlabel_get(fd, 2, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE));
106 explain("cannot get it again in non-exclusive mode");
107 expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_ANY, IPV6_FL_F_CREATE));
108 explain("cannot get it again in exclusive mode either");
109 expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE));
110 expect_pass(flowlabel_put(fd, 2));
112 if (cfg_long_running) {
113 explain("cannot reuse the label, due to linger");
114 expect_fail(flowlabel_get(fd, 2, IPV6_FL_S_ANY,
116 explain("after sleep, can reuse");
117 sleep(FL_MIN_LINGER * 2 + 1);
118 expect_pass(flowlabel_get(fd, 2, IPV6_FL_S_ANY,
122 explain("create a new user-private label (FL_S_USER)");
123 expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, IPV6_FL_F_CREATE));
124 explain("cannot get it again in non-exclusive mode");
125 expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_ANY, 0));
126 explain("cannot get it again in exclusive mode");
127 expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_EXCL, 0));
128 explain("can get it again in user mode");
129 expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
130 explain("child process can get it too, but not after setuid(nobody)");
133 error(1, errno, "fork");
135 expect_pass(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
136 if (setuid(USHRT_MAX))
137 fprintf(stderr, "[INFO] skip setuid child test\n");
139 expect_fail(flowlabel_get(fd, 3, IPV6_FL_S_USER, 0));
142 if (wait(&wstatus) == -1)
143 error(1, errno, "wait");
144 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
145 error(1, errno, "wait: unexpected child result");
147 explain("create a new process-private label (FL_S_PROCESS)");
148 expect_pass(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, IPV6_FL_F_CREATE));
149 explain("can get it again");
150 expect_pass(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, 0));
151 explain("child process cannot can get it");
154 error(1, errno, "fork");
156 expect_fail(flowlabel_get(fd, 4, IPV6_FL_S_PROCESS, 0));
159 if (wait(&wstatus) == -1)
160 error(1, errno, "wait");
161 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
162 error(1, errno, "wait: unexpected child result");
165 static void parse_opts(int argc, char **argv)
169 while ((c = getopt(argc, argv, "lv")) != -1) {
172 cfg_long_running = true;
178 error(1, 0, "%s: parse error", argv[0]);
183 int main(int argc, char **argv)
187 parse_opts(argc, argv);
189 fd = socket(PF_INET6, SOCK_DGRAM, 0);
191 error(1, errno, "socket");
196 error(1, errno, "close");