Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <alan.brady@intel.com>
Tested-by: Gurucharan <gurucharanx.g@intel.com>
Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Alan Brady and committed by
David S. Miller
16576a03 c497885e

+81 -16
+5 -1
net/ipv6/ping.c
··· 64 64 if (err) 65 65 return err; 66 66 67 + memset(&fl6, 0, sizeof(fl6)); 68 + 67 69 if (msg->msg_name) { 68 70 DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name); 69 71 if (msg->msg_namelen < sizeof(*u)) ··· 74 72 return -EAFNOSUPPORT; 75 73 } 76 74 daddr = &(u->sin6_addr); 75 + if (np->sndflow) 76 + fl6.flowlabel = u->sin6_flowinfo & IPV6_FLOWINFO_MASK; 77 77 if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr))) 78 78 oif = u->sin6_scope_id; 79 79 } else { 80 80 if (sk->sk_state != TCP_ESTABLISHED) 81 81 return -EDESTADDRREQ; 82 82 daddr = &sk->sk_v6_daddr; 83 + fl6.flowlabel = np->flow_label; 83 84 } 84 85 85 86 if (!oif) ··· 106 101 ipc6.sockc.tsflags = sk->sk_tsflags; 107 102 ipc6.sockc.mark = sk->sk_mark; 108 103 109 - memset(&fl6, 0, sizeof(fl6)); 110 104 fl6.flowi6_oif = oif; 111 105 112 106 if (msg->msg_controllen) {
+60 -15
tools/testing/selftests/net/ipv6_flowlabel.c
··· 9 9 #include <errno.h> 10 10 #include <fcntl.h> 11 11 #include <limits.h> 12 + #include <linux/icmpv6.h> 12 13 #include <linux/in6.h> 13 14 #include <stdbool.h> 14 15 #include <stdio.h> ··· 30 29 #ifndef IPV6_FLOWLABEL_MGR 31 30 #define IPV6_FLOWLABEL_MGR 32 32 31 #endif 32 + #ifndef IPV6_FLOWINFO_SEND 33 + #define IPV6_FLOWINFO_SEND 33 34 + #endif 33 35 34 36 #define FLOWLABEL_WILDCARD ((uint32_t) -1) 35 37 36 38 static const char cfg_data[] = "a"; 37 39 static uint32_t cfg_label = 1; 40 + static bool use_ping; 41 + static bool use_flowinfo_send; 42 + 43 + static struct icmp6hdr icmp6 = { 44 + .icmp6_type = ICMPV6_ECHO_REQUEST 45 + }; 46 + 47 + static struct sockaddr_in6 addr = { 48 + .sin6_family = AF_INET6, 49 + .sin6_addr = IN6ADDR_LOOPBACK_INIT, 50 + }; 38 51 39 52 static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel) 40 53 { 41 54 char control[CMSG_SPACE(sizeof(flowlabel))] = {0}; 42 55 struct msghdr msg = {0}; 43 - struct iovec iov = {0}; 56 + struct iovec iov = { 57 + .iov_base = (char *)cfg_data, 58 + .iov_len = sizeof(cfg_data) 59 + }; 44 60 int ret; 45 61 46 - iov.iov_base = (char *)cfg_data; 47 - iov.iov_len = sizeof(cfg_data); 62 + if (use_ping) { 63 + iov.iov_base = &icmp6; 64 + iov.iov_len = sizeof(icmp6); 65 + } 48 66 49 67 msg.msg_iov = &iov; 50 68 msg.msg_iovlen = 1; 51 69 52 - if (with_flowlabel) { 70 + if (use_flowinfo_send) { 71 + msg.msg_name = &addr; 72 + msg.msg_namelen = sizeof(addr); 73 + } else if (with_flowlabel) { 53 74 struct cmsghdr *cm; 54 75 55 76 cm = (void *)control; ··· 117 94 ret = recvmsg(fd, &msg, 0); 118 95 if (ret == -1) 119 96 error(1, errno, "recv"); 97 + if (use_ping) 98 + goto parse_cmsg; 120 99 if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) 121 100 error(1, 0, "recv: truncated"); 122 101 if (ret != sizeof(cfg_data)) ··· 126 101 if (memcmp(data, cfg_data, sizeof(data))) 127 102 error(1, 0, "recv: data mismatch"); 128 103 104 + parse_cmsg: 129 105 cm = CMSG_FIRSTHDR(&msg); 130 106 if (with_flowlabel) { 131 107 if (!cm) ··· 140 114 flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm)); 141 115 fprintf(stderr, "recv with label %u\n", flowlabel); 142 116 143 - if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) 117 + if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) { 144 118 fprintf(stderr, "recv: incorrect flowlabel %u != %u\n", 145 119 flowlabel, expect); 120 + error(1, 0, "recv: flowlabel is wrong"); 121 + } 146 122 147 123 } else { 148 124 fprintf(stderr, "recv without label\n"); ··· 193 165 { 194 166 int c; 195 167 196 - while ((c = getopt(argc, argv, "l:")) != -1) { 168 + while ((c = getopt(argc, argv, "l:ps")) != -1) { 197 169 switch (c) { 198 170 case 'l': 199 171 cfg_label = strtoul(optarg, NULL, 0); 172 + break; 173 + case 'p': 174 + use_ping = true; 175 + break; 176 + case 's': 177 + use_flowinfo_send = true; 200 178 break; 201 179 default: 202 180 error(1, 0, "%s: parse error", argv[0]); ··· 212 178 213 179 int main(int argc, char **argv) 214 180 { 215 - struct sockaddr_in6 addr = { 216 - .sin6_family = AF_INET6, 217 - .sin6_port = htons(8000), 218 - .sin6_addr = IN6ADDR_LOOPBACK_INIT, 219 - }; 220 181 const int one = 1; 221 182 int fdt, fdr; 183 + int prot = 0; 184 + 185 + addr.sin6_port = htons(8000); 222 186 223 187 parse_opts(argc, argv); 224 188 225 - fdt = socket(PF_INET6, SOCK_DGRAM, 0); 189 + if (use_ping) { 190 + fprintf(stderr, "attempting to use ping sockets\n"); 191 + prot = IPPROTO_ICMPV6; 192 + } 193 + 194 + fdt = socket(PF_INET6, SOCK_DGRAM, prot); 226 195 if (fdt == -1) 227 196 error(1, errno, "socket t"); 228 197 229 - fdr = socket(PF_INET6, SOCK_DGRAM, 0); 198 + fdr = use_ping ? fdt : socket(PF_INET6, SOCK_DGRAM, 0); 230 199 if (fdr == -1) 231 200 error(1, errno, "socket r"); 232 201 233 202 if (connect(fdt, (void *)&addr, sizeof(addr))) 234 203 error(1, errno, "connect"); 235 - if (bind(fdr, (void *)&addr, sizeof(addr))) 204 + if (!use_ping && bind(fdr, (void *)&addr, sizeof(addr))) 236 205 error(1, errno, "bind"); 237 206 238 207 flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE); ··· 253 216 do_recv(fdr, false, 0); 254 217 } 255 218 219 + if (use_flowinfo_send) { 220 + fprintf(stderr, "using IPV6_FLOWINFO_SEND to send label\n"); 221 + addr.sin6_flowinfo = htonl(cfg_label); 222 + if (setsockopt(fdt, SOL_IPV6, IPV6_FLOWINFO_SEND, &one, 223 + sizeof(one)) == -1) 224 + error(1, errno, "setsockopt flowinfo_send"); 225 + } 226 + 256 227 fprintf(stderr, "send label\n"); 257 228 do_send(fdt, true, cfg_label); 258 229 do_recv(fdr, true, cfg_label); 259 230 260 231 if (close(fdr)) 261 232 error(1, errno, "close r"); 262 - if (close(fdt)) 233 + if (!use_ping && close(fdt)) 263 234 error(1, errno, "close t"); 264 235 265 236 return 0;
+16
tools/testing/selftests/net/ipv6_flowlabel.sh
··· 18 18 ./in_netns.sh \ 19 19 sh -c 'sysctl -q -w net.ipv6.auto_flowlabels=1 && ./ipv6_flowlabel -l 1' 20 20 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 + 21 37 echo OK. All tests passed