Skip to content

Commit 16576a0

Browse files
atbrady-inteldavem330
authored andcommitted
ping: support ipv6 ping socket flow labels
Ping sockets don't appear to make any attempt to preserve flow labels created and set by userspace using IPV6_FLOWINFO_SEND. Instead they are clobbered by autolabels (if enabled) or zero. Grab the flowlabel out of the msghdr similar to how rawv6_sendmsg does it and move the memset up so it doesn't get zeroed after. Signed-off-by: Alan Brady <[email protected]> Tested-by: Gurucharan <[email protected]> Signed-off-by: Tony Nguyen <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c497885 commit 16576a0

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

net/ipv6/ping.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
6464
if (err)
6565
return err;
6666

67+
memset(&fl6, 0, sizeof(fl6));
68+
6769
if (msg->msg_name) {
6870
DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name);
6971
if (msg->msg_namelen < sizeof(*u))
@@ -72,12 +74,15 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7274
return -EAFNOSUPPORT;
7375
}
7476
daddr = &(u->sin6_addr);
77+
if (np->sndflow)
78+
fl6.flowlabel = u->sin6_flowinfo & IPV6_FLOWINFO_MASK;
7579
if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
7680
oif = u->sin6_scope_id;
7781
} else {
7882
if (sk->sk_state != TCP_ESTABLISHED)
7983
return -EDESTADDRREQ;
8084
daddr = &sk->sk_v6_daddr;
85+
fl6.flowlabel = np->flow_label;
8186
}
8287

8388
if (!oif)
@@ -101,7 +106,6 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
101106
ipc6.sockc.tsflags = sk->sk_tsflags;
102107
ipc6.sockc.mark = sk->sk_mark;
103108

104-
memset(&fl6, 0, sizeof(fl6));
105109
fl6.flowi6_oif = oif;
106110

107111
if (msg->msg_controllen) {

tools/testing/selftests/net/ipv6_flowlabel.c

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <fcntl.h>
1111
#include <limits.h>
12+
#include <linux/icmpv6.h>
1213
#include <linux/in6.h>
1314
#include <stdbool.h>
1415
#include <stdio.h>
@@ -29,26 +30,48 @@
2930
#ifndef IPV6_FLOWLABEL_MGR
3031
#define IPV6_FLOWLABEL_MGR 32
3132
#endif
33+
#ifndef IPV6_FLOWINFO_SEND
34+
#define IPV6_FLOWINFO_SEND 33
35+
#endif
3236

3337
#define FLOWLABEL_WILDCARD ((uint32_t) -1)
3438

3539
static const char cfg_data[] = "a";
3640
static uint32_t cfg_label = 1;
41+
static bool use_ping;
42+
static bool use_flowinfo_send;
43+
44+
static struct icmp6hdr icmp6 = {
45+
.icmp6_type = ICMPV6_ECHO_REQUEST
46+
};
47+
48+
static struct sockaddr_in6 addr = {
49+
.sin6_family = AF_INET6,
50+
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
51+
};
3752

3853
static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
3954
{
4055
char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
4156
struct msghdr msg = {0};
42-
struct iovec iov = {0};
57+
struct iovec iov = {
58+
.iov_base = (char *)cfg_data,
59+
.iov_len = sizeof(cfg_data)
60+
};
4361
int ret;
4462

45-
iov.iov_base = (char *)cfg_data;
46-
iov.iov_len = sizeof(cfg_data);
63+
if (use_ping) {
64+
iov.iov_base = &icmp6;
65+
iov.iov_len = sizeof(icmp6);
66+
}
4767

4868
msg.msg_iov = &iov;
4969
msg.msg_iovlen = 1;
5070

51-
if (with_flowlabel) {
71+
if (use_flowinfo_send) {
72+
msg.msg_name = &addr;
73+
msg.msg_namelen = sizeof(addr);
74+
} else if (with_flowlabel) {
5275
struct cmsghdr *cm;
5376

5477
cm = (void *)control;
@@ -94,13 +117,16 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
94117
ret = recvmsg(fd, &msg, 0);
95118
if (ret == -1)
96119
error(1, errno, "recv");
120+
if (use_ping)
121+
goto parse_cmsg;
97122
if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
98123
error(1, 0, "recv: truncated");
99124
if (ret != sizeof(cfg_data))
100125
error(1, 0, "recv: length mismatch");
101126
if (memcmp(data, cfg_data, sizeof(data)))
102127
error(1, 0, "recv: data mismatch");
103128

129+
parse_cmsg:
104130
cm = CMSG_FIRSTHDR(&msg);
105131
if (with_flowlabel) {
106132
if (!cm)
@@ -114,9 +140,11 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
114140
flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
115141
fprintf(stderr, "recv with label %u\n", flowlabel);
116142

117-
if (expect != FLOWLABEL_WILDCARD && expect != flowlabel)
143+
if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) {
118144
fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
119145
flowlabel, expect);
146+
error(1, 0, "recv: flowlabel is wrong");
147+
}
120148

121149
} else {
122150
fprintf(stderr, "recv without label\n");
@@ -165,11 +193,17 @@ static void parse_opts(int argc, char **argv)
165193
{
166194
int c;
167195

168-
while ((c = getopt(argc, argv, "l:")) != -1) {
196+
while ((c = getopt(argc, argv, "l:ps")) != -1) {
169197
switch (c) {
170198
case 'l':
171199
cfg_label = strtoul(optarg, NULL, 0);
172200
break;
201+
case 'p':
202+
use_ping = true;
203+
break;
204+
case 's':
205+
use_flowinfo_send = true;
206+
break;
173207
default:
174208
error(1, 0, "%s: parse error", argv[0]);
175209
}
@@ -178,27 +212,30 @@ static void parse_opts(int argc, char **argv)
178212

179213
int main(int argc, char **argv)
180214
{
181-
struct sockaddr_in6 addr = {
182-
.sin6_family = AF_INET6,
183-
.sin6_port = htons(8000),
184-
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
185-
};
186215
const int one = 1;
187216
int fdt, fdr;
217+
int prot = 0;
218+
219+
addr.sin6_port = htons(8000);
188220

189221
parse_opts(argc, argv);
190222

191-
fdt = socket(PF_INET6, SOCK_DGRAM, 0);
223+
if (use_ping) {
224+
fprintf(stderr, "attempting to use ping sockets\n");
225+
prot = IPPROTO_ICMPV6;
226+
}
227+
228+
fdt = socket(PF_INET6, SOCK_DGRAM, prot);
192229
if (fdt == -1)
193230
error(1, errno, "socket t");
194231

195-
fdr = socket(PF_INET6, SOCK_DGRAM, 0);
232+
fdr = use_ping ? fdt : socket(PF_INET6, SOCK_DGRAM, 0);
196233
if (fdr == -1)
197234
error(1, errno, "socket r");
198235

199236
if (connect(fdt, (void *)&addr, sizeof(addr)))
200237
error(1, errno, "connect");
201-
if (bind(fdr, (void *)&addr, sizeof(addr)))
238+
if (!use_ping && bind(fdr, (void *)&addr, sizeof(addr)))
202239
error(1, errno, "bind");
203240

204241
flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
@@ -216,13 +253,21 @@ int main(int argc, char **argv)
216253
do_recv(fdr, false, 0);
217254
}
218255

256+
if (use_flowinfo_send) {
257+
fprintf(stderr, "using IPV6_FLOWINFO_SEND to send label\n");
258+
addr.sin6_flowinfo = htonl(cfg_label);
259+
if (setsockopt(fdt, SOL_IPV6, IPV6_FLOWINFO_SEND, &one,
260+
sizeof(one)) == -1)
261+
error(1, errno, "setsockopt flowinfo_send");
262+
}
263+
219264
fprintf(stderr, "send label\n");
220265
do_send(fdt, true, cfg_label);
221266
do_recv(fdr, true, cfg_label);
222267

223268
if (close(fdr))
224269
error(1, errno, "close r");
225-
if (close(fdt))
270+
if (!use_ping && close(fdt))
226271
error(1, errno, "close t");
227272

228273
return 0;

tools/testing/selftests/net/ipv6_flowlabel.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ echo "TEST datapath (with auto-flowlabels)"
1818
./in_netns.sh \
1919
sh -c 'sysctl -q -w net.ipv6.auto_flowlabels=1 && ./ipv6_flowlabel -l 1'
2020

21+
echo "TEST datapath (with ping-sockets)"
22+
./in_netns.sh \
23+
sh -c 'sysctl -q -w net.ipv6.flowlabel_reflect=4 && \
24+
sysctl -q -w net.ipv4.ping_group_range="0 2147483647" && \
25+
./ipv6_flowlabel -l 1 -p'
26+
27+
echo "TEST datapath (with flowinfo-send)"
28+
./in_netns.sh \
29+
sh -c './ipv6_flowlabel -l 1 -s'
30+
31+
echo "TEST datapath (with ping-sockets flowinfo-send)"
32+
./in_netns.sh \
33+
sh -c 'sysctl -q -w net.ipv6.flowlabel_reflect=4 && \
34+
sysctl -q -w net.ipv4.ping_group_range="0 2147483647" && \
35+
./ipv6_flowlabel -l 1 -p -s'
36+
2137
echo OK. All tests passed

0 commit comments

Comments
 (0)