root/tools/testing/selftests/net/ipv6_flowlabel.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. do_send
  2. do_recv
  3. get_autoflowlabel_enabled
  4. flowlabel_get
  5. parse_opts
  6. main

   1 // SPDX-License-Identifier: GPL-2.0
   2 /* Test IPV6_FLOWINFO cmsg on send and recv */
   3 
   4 #define _GNU_SOURCE
   5 
   6 #include <arpa/inet.h>
   7 #include <asm/byteorder.h>
   8 #include <error.h>
   9 #include <errno.h>
  10 #include <fcntl.h>
  11 #include <limits.h>
  12 #include <linux/in6.h>
  13 #include <stdbool.h>
  14 #include <stdio.h>
  15 #include <stdint.h>
  16 #include <stdlib.h>
  17 #include <string.h>
  18 #include <sys/socket.h>
  19 #include <sys/stat.h>
  20 #include <sys/time.h>
  21 #include <sys/types.h>
  22 #include <unistd.h>
  23 
  24 /* uapi/glibc weirdness may leave this undefined */
  25 #ifndef IPV6_FLOWINFO
  26 #define IPV6_FLOWINFO 11
  27 #endif
  28 
  29 #ifndef IPV6_FLOWLABEL_MGR
  30 #define IPV6_FLOWLABEL_MGR 32
  31 #endif
  32 
  33 #define FLOWLABEL_WILDCARD      ((uint32_t) -1)
  34 
  35 static const char cfg_data[]    = "a";
  36 static uint32_t cfg_label       = 1;
  37 
  38 static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
  39 {
  40         char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
  41         struct msghdr msg = {0};
  42         struct iovec iov = {0};
  43         int ret;
  44 
  45         iov.iov_base = (char *)cfg_data;
  46         iov.iov_len = sizeof(cfg_data);
  47 
  48         msg.msg_iov = &iov;
  49         msg.msg_iovlen = 1;
  50 
  51         if (with_flowlabel) {
  52                 struct cmsghdr *cm;
  53 
  54                 cm = (void *)control;
  55                 cm->cmsg_len = CMSG_LEN(sizeof(flowlabel));
  56                 cm->cmsg_level = SOL_IPV6;
  57                 cm->cmsg_type = IPV6_FLOWINFO;
  58                 *(uint32_t *)CMSG_DATA(cm) = htonl(flowlabel);
  59 
  60                 msg.msg_control = control;
  61                 msg.msg_controllen = sizeof(control);
  62         }
  63 
  64         ret = sendmsg(fd, &msg, 0);
  65         if (ret == -1)
  66                 error(1, errno, "send");
  67 
  68         if (with_flowlabel)
  69                 fprintf(stderr, "sent with label %u\n", flowlabel);
  70         else
  71                 fprintf(stderr, "sent without label\n");
  72 }
  73 
  74 static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
  75 {
  76         char control[CMSG_SPACE(sizeof(expect))];
  77         char data[sizeof(cfg_data)];
  78         struct msghdr msg = {0};
  79         struct iovec iov = {0};
  80         struct cmsghdr *cm;
  81         uint32_t flowlabel;
  82         int ret;
  83 
  84         iov.iov_base = data;
  85         iov.iov_len = sizeof(data);
  86 
  87         msg.msg_iov = &iov;
  88         msg.msg_iovlen = 1;
  89 
  90         memset(control, 0, sizeof(control));
  91         msg.msg_control = control;
  92         msg.msg_controllen = sizeof(control);
  93 
  94         ret = recvmsg(fd, &msg, 0);
  95         if (ret == -1)
  96                 error(1, errno, "recv");
  97         if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
  98                 error(1, 0, "recv: truncated");
  99         if (ret != sizeof(cfg_data))
 100                 error(1, 0, "recv: length mismatch");
 101         if (memcmp(data, cfg_data, sizeof(data)))
 102                 error(1, 0, "recv: data mismatch");
 103 
 104         cm = CMSG_FIRSTHDR(&msg);
 105         if (with_flowlabel) {
 106                 if (!cm)
 107                         error(1, 0, "recv: missing cmsg");
 108                 if (CMSG_NXTHDR(&msg, cm))
 109                         error(1, 0, "recv: too many cmsg");
 110                 if (cm->cmsg_level != SOL_IPV6 ||
 111                     cm->cmsg_type != IPV6_FLOWINFO)
 112                         error(1, 0, "recv: unexpected cmsg level or type");
 113 
 114                 flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
 115                 fprintf(stderr, "recv with label %u\n", flowlabel);
 116 
 117                 if (expect != FLOWLABEL_WILDCARD && expect != flowlabel)
 118                         fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
 119                                         flowlabel, expect);
 120 
 121         } else {
 122                 fprintf(stderr, "recv without label\n");
 123         }
 124 }
 125 
 126 static bool get_autoflowlabel_enabled(void)
 127 {
 128         int fd, ret;
 129         char val;
 130 
 131         fd = open("/proc/sys/net/ipv6/auto_flowlabels", O_RDONLY);
 132         if (fd == -1)
 133                 error(1, errno, "open sysctl");
 134 
 135         ret = read(fd, &val, 1);
 136         if (ret == -1)
 137                 error(1, errno, "read sysctl");
 138         if (ret == 0)
 139                 error(1, 0, "read sysctl: 0");
 140 
 141         if (close(fd))
 142                 error(1, errno, "close sysctl");
 143 
 144         return val == '1';
 145 }
 146 
 147 static void flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
 148 {
 149         struct in6_flowlabel_req req = {
 150                 .flr_action = IPV6_FL_A_GET,
 151                 .flr_label = htonl(label),
 152                 .flr_flags = flags,
 153                 .flr_share = share,
 154         };
 155 
 156         /* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
 157         req.flr_dst.s6_addr[0] = 0xfd;
 158         req.flr_dst.s6_addr[15] = 0x1;
 159 
 160         if (setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req)))
 161                 error(1, errno, "setsockopt flowlabel get");
 162 }
 163 
 164 static void parse_opts(int argc, char **argv)
 165 {
 166         int c;
 167 
 168         while ((c = getopt(argc, argv, "l:")) != -1) {
 169                 switch (c) {
 170                 case 'l':
 171                         cfg_label = strtoul(optarg, NULL, 0);
 172                         break;
 173                 default:
 174                         error(1, 0, "%s: parse error", argv[0]);
 175                 }
 176         }
 177 }
 178 
 179 int main(int argc, char **argv)
 180 {
 181         struct sockaddr_in6 addr = {
 182                 .sin6_family = AF_INET6,
 183                 .sin6_port = htons(8000),
 184                 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
 185         };
 186         const int one = 1;
 187         int fdt, fdr;
 188 
 189         parse_opts(argc, argv);
 190 
 191         fdt = socket(PF_INET6, SOCK_DGRAM, 0);
 192         if (fdt == -1)
 193                 error(1, errno, "socket t");
 194 
 195         fdr = socket(PF_INET6, SOCK_DGRAM, 0);
 196         if (fdr == -1)
 197                 error(1, errno, "socket r");
 198 
 199         if (connect(fdt, (void *)&addr, sizeof(addr)))
 200                 error(1, errno, "connect");
 201         if (bind(fdr, (void *)&addr, sizeof(addr)))
 202                 error(1, errno, "bind");
 203 
 204         flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
 205 
 206         if (setsockopt(fdr, SOL_IPV6, IPV6_FLOWINFO, &one, sizeof(one)))
 207                 error(1, errno, "setsockopt flowinfo");
 208 
 209         if (get_autoflowlabel_enabled()) {
 210                 fprintf(stderr, "send no label: recv auto flowlabel\n");
 211                 do_send(fdt, false, 0);
 212                 do_recv(fdr, true, FLOWLABEL_WILDCARD);
 213         } else {
 214                 fprintf(stderr, "send no label: recv no label (auto off)\n");
 215                 do_send(fdt, false, 0);
 216                 do_recv(fdr, false, 0);
 217         }
 218 
 219         fprintf(stderr, "send label\n");
 220         do_send(fdt, true, cfg_label);
 221         do_recv(fdr, true, cfg_label);
 222 
 223         if (close(fdr))
 224                 error(1, errno, "close r");
 225         if (close(fdt))
 226                 error(1, errno, "close t");
 227 
 228         return 0;
 229 }

/* [<][>][^][v][top][bottom][index][help] */