Commit 16576a03 authored by Alan Brady's avatar Alan Brady Committed by David S. Miller

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: default avatarAlan Brady <alan.brady@intel.com>
Tested-by: default avatarGurucharan <gurucharanx.g@intel.com>
Signed-off-by: default avatarTony Nguyen <anthony.l.nguyen@intel.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c497885e
...@@ -64,6 +64,8 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -64,6 +64,8 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (err) if (err)
return err; return err;
memset(&fl6, 0, sizeof(fl6));
if (msg->msg_name) { if (msg->msg_name) {
DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name); DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name);
if (msg->msg_namelen < sizeof(*u)) if (msg->msg_namelen < sizeof(*u))
...@@ -72,12 +74,15 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -72,12 +74,15 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
} }
daddr = &(u->sin6_addr); daddr = &(u->sin6_addr);
if (np->sndflow)
fl6.flowlabel = u->sin6_flowinfo & IPV6_FLOWINFO_MASK;
if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr))) if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
oif = u->sin6_scope_id; oif = u->sin6_scope_id;
} else { } else {
if (sk->sk_state != TCP_ESTABLISHED) if (sk->sk_state != TCP_ESTABLISHED)
return -EDESTADDRREQ; return -EDESTADDRREQ;
daddr = &sk->sk_v6_daddr; daddr = &sk->sk_v6_daddr;
fl6.flowlabel = np->flow_label;
} }
if (!oif) if (!oif)
...@@ -101,7 +106,6 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -101,7 +106,6 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
ipc6.sockc.tsflags = sk->sk_tsflags; ipc6.sockc.tsflags = sk->sk_tsflags;
ipc6.sockc.mark = sk->sk_mark; ipc6.sockc.mark = sk->sk_mark;
memset(&fl6, 0, sizeof(fl6));
fl6.flowi6_oif = oif; fl6.flowi6_oif = oif;
if (msg->msg_controllen) { if (msg->msg_controllen) {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <linux/icmpv6.h>
#include <linux/in6.h> #include <linux/in6.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
...@@ -29,26 +30,48 @@ ...@@ -29,26 +30,48 @@
#ifndef IPV6_FLOWLABEL_MGR #ifndef IPV6_FLOWLABEL_MGR
#define IPV6_FLOWLABEL_MGR 32 #define IPV6_FLOWLABEL_MGR 32
#endif #endif
#ifndef IPV6_FLOWINFO_SEND
#define IPV6_FLOWINFO_SEND 33
#endif
#define FLOWLABEL_WILDCARD ((uint32_t) -1) #define FLOWLABEL_WILDCARD ((uint32_t) -1)
static const char cfg_data[] = "a"; static const char cfg_data[] = "a";
static uint32_t cfg_label = 1; static uint32_t cfg_label = 1;
static bool use_ping;
static bool use_flowinfo_send;
static struct icmp6hdr icmp6 = {
.icmp6_type = ICMPV6_ECHO_REQUEST
};
static struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
};
static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel) static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
{ {
char control[CMSG_SPACE(sizeof(flowlabel))] = {0}; char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
struct msghdr msg = {0}; struct msghdr msg = {0};
struct iovec iov = {0}; struct iovec iov = {
.iov_base = (char *)cfg_data,
.iov_len = sizeof(cfg_data)
};
int ret; int ret;
iov.iov_base = (char *)cfg_data; if (use_ping) {
iov.iov_len = sizeof(cfg_data); iov.iov_base = &icmp6;
iov.iov_len = sizeof(icmp6);
}
msg.msg_iov = &iov; msg.msg_iov = &iov;
msg.msg_iovlen = 1; msg.msg_iovlen = 1;
if (with_flowlabel) { if (use_flowinfo_send) {
msg.msg_name = &addr;
msg.msg_namelen = sizeof(addr);
} else if (with_flowlabel) {
struct cmsghdr *cm; struct cmsghdr *cm;
cm = (void *)control; cm = (void *)control;
...@@ -94,6 +117,8 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect) ...@@ -94,6 +117,8 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
ret = recvmsg(fd, &msg, 0); ret = recvmsg(fd, &msg, 0);
if (ret == -1) if (ret == -1)
error(1, errno, "recv"); error(1, errno, "recv");
if (use_ping)
goto parse_cmsg;
if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
error(1, 0, "recv: truncated"); error(1, 0, "recv: truncated");
if (ret != sizeof(cfg_data)) if (ret != sizeof(cfg_data))
...@@ -101,6 +126,7 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect) ...@@ -101,6 +126,7 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
if (memcmp(data, cfg_data, sizeof(data))) if (memcmp(data, cfg_data, sizeof(data)))
error(1, 0, "recv: data mismatch"); error(1, 0, "recv: data mismatch");
parse_cmsg:
cm = CMSG_FIRSTHDR(&msg); cm = CMSG_FIRSTHDR(&msg);
if (with_flowlabel) { if (with_flowlabel) {
if (!cm) if (!cm)
...@@ -114,9 +140,11 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect) ...@@ -114,9 +140,11 @@ static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm)); flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
fprintf(stderr, "recv with label %u\n", flowlabel); fprintf(stderr, "recv with label %u\n", flowlabel);
if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) {
fprintf(stderr, "recv: incorrect flowlabel %u != %u\n", fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
flowlabel, expect); flowlabel, expect);
error(1, 0, "recv: flowlabel is wrong");
}
} else { } else {
fprintf(stderr, "recv without label\n"); fprintf(stderr, "recv without label\n");
...@@ -165,11 +193,17 @@ static void parse_opts(int argc, char **argv) ...@@ -165,11 +193,17 @@ static void parse_opts(int argc, char **argv)
{ {
int c; int c;
while ((c = getopt(argc, argv, "l:")) != -1) { while ((c = getopt(argc, argv, "l:ps")) != -1) {
switch (c) { switch (c) {
case 'l': case 'l':
cfg_label = strtoul(optarg, NULL, 0); cfg_label = strtoul(optarg, NULL, 0);
break; break;
case 'p':
use_ping = true;
break;
case 's':
use_flowinfo_send = true;
break;
default: default:
error(1, 0, "%s: parse error", argv[0]); error(1, 0, "%s: parse error", argv[0]);
} }
...@@ -178,27 +212,30 @@ static void parse_opts(int argc, char **argv) ...@@ -178,27 +212,30 @@ static void parse_opts(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(8000),
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
};
const int one = 1; const int one = 1;
int fdt, fdr; int fdt, fdr;
int prot = 0;
addr.sin6_port = htons(8000);
parse_opts(argc, argv); parse_opts(argc, argv);
fdt = socket(PF_INET6, SOCK_DGRAM, 0); if (use_ping) {
fprintf(stderr, "attempting to use ping sockets\n");
prot = IPPROTO_ICMPV6;
}
fdt = socket(PF_INET6, SOCK_DGRAM, prot);
if (fdt == -1) if (fdt == -1)
error(1, errno, "socket t"); error(1, errno, "socket t");
fdr = socket(PF_INET6, SOCK_DGRAM, 0); fdr = use_ping ? fdt : socket(PF_INET6, SOCK_DGRAM, 0);
if (fdr == -1) if (fdr == -1)
error(1, errno, "socket r"); error(1, errno, "socket r");
if (connect(fdt, (void *)&addr, sizeof(addr))) if (connect(fdt, (void *)&addr, sizeof(addr)))
error(1, errno, "connect"); error(1, errno, "connect");
if (bind(fdr, (void *)&addr, sizeof(addr))) if (!use_ping && bind(fdr, (void *)&addr, sizeof(addr)))
error(1, errno, "bind"); error(1, errno, "bind");
flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE); flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
...@@ -216,13 +253,21 @@ int main(int argc, char **argv) ...@@ -216,13 +253,21 @@ int main(int argc, char **argv)
do_recv(fdr, false, 0); do_recv(fdr, false, 0);
} }
if (use_flowinfo_send) {
fprintf(stderr, "using IPV6_FLOWINFO_SEND to send label\n");
addr.sin6_flowinfo = htonl(cfg_label);
if (setsockopt(fdt, SOL_IPV6, IPV6_FLOWINFO_SEND, &one,
sizeof(one)) == -1)
error(1, errno, "setsockopt flowinfo_send");
}
fprintf(stderr, "send label\n"); fprintf(stderr, "send label\n");
do_send(fdt, true, cfg_label); do_send(fdt, true, cfg_label);
do_recv(fdr, true, cfg_label); do_recv(fdr, true, cfg_label);
if (close(fdr)) if (close(fdr))
error(1, errno, "close r"); error(1, errno, "close r");
if (close(fdt)) if (!use_ping && close(fdt))
error(1, errno, "close t"); error(1, errno, "close t");
return 0; return 0;
......
...@@ -18,4 +18,20 @@ echo "TEST datapath (with auto-flowlabels)" ...@@ -18,4 +18,20 @@ echo "TEST datapath (with auto-flowlabels)"
./in_netns.sh \ ./in_netns.sh \
sh -c 'sysctl -q -w net.ipv6.auto_flowlabels=1 && ./ipv6_flowlabel -l 1' sh -c 'sysctl -q -w net.ipv6.auto_flowlabels=1 && ./ipv6_flowlabel -l 1'
echo "TEST datapath (with ping-sockets)"
./in_netns.sh \
sh -c 'sysctl -q -w net.ipv6.flowlabel_reflect=4 && \
sysctl -q -w net.ipv4.ping_group_range="0 2147483647" && \
./ipv6_flowlabel -l 1 -p'
echo "TEST datapath (with flowinfo-send)"
./in_netns.sh \
sh -c './ipv6_flowlabel -l 1 -s'
echo "TEST datapath (with ping-sockets flowinfo-send)"
./in_netns.sh \
sh -c 'sysctl -q -w net.ipv6.flowlabel_reflect=4 && \
sysctl -q -w net.ipv4.ping_group_range="0 2147483647" && \
./ipv6_flowlabel -l 1 -p -s'
echo OK. All tests passed echo OK. All tests passed
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment