linux/tools/testing/selftests/net/reuseport_addr_any.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2
   3/* Test that sockets listening on a specific address are preferred
   4 * over sockets listening on addr_any.
   5 */
   6
   7#define _GNU_SOURCE
   8
   9#include <arpa/inet.h>
  10#include <errno.h>
  11#include <error.h>
  12#include <linux/dccp.h>
  13#include <linux/in.h>
  14#include <linux/unistd.h>
  15#include <stdbool.h>
  16#include <stdio.h>
  17#include <stdlib.h>
  18#include <string.h>
  19#include <sys/epoll.h>
  20#include <sys/types.h>
  21#include <sys/socket.h>
  22#include <unistd.h>
  23
  24static const char *IP4_ADDR = "127.0.0.1";
  25static const char *IP6_ADDR = "::1";
  26static const char *IP4_MAPPED6 = "::ffff:127.0.0.1";
  27
  28static const int PORT = 8888;
  29
  30static void build_rcv_fd(int family, int proto, int *rcv_fds, int count,
  31                         const char *addr_str)
  32{
  33        struct sockaddr_in  addr4 = {0};
  34        struct sockaddr_in6 addr6 = {0};
  35        struct sockaddr *addr;
  36        int opt, i, sz;
  37
  38        memset(&addr, 0, sizeof(addr));
  39
  40        switch (family) {
  41        case AF_INET:
  42                addr4.sin_family = family;
  43                if (!addr_str)
  44                        addr4.sin_addr.s_addr = htonl(INADDR_ANY);
  45                else if (!inet_pton(family, addr_str, &addr4.sin_addr.s_addr))
  46                        error(1, errno, "inet_pton failed: %s", addr_str);
  47                addr4.sin_port = htons(PORT);
  48                sz = sizeof(addr4);
  49                addr = (struct sockaddr *)&addr4;
  50                break;
  51        case AF_INET6:
  52                addr6.sin6_family = AF_INET6;
  53                if (!addr_str)
  54                        addr6.sin6_addr = in6addr_any;
  55                else if (!inet_pton(family, addr_str, &addr6.sin6_addr))
  56                        error(1, errno, "inet_pton failed: %s", addr_str);
  57                addr6.sin6_port = htons(PORT);
  58                sz = sizeof(addr6);
  59                addr = (struct sockaddr *)&addr6;
  60                break;
  61        default:
  62                error(1, 0, "Unsupported family %d", family);
  63                /* clang does not recognize error() above as terminating
  64                 * the program, so it complains that saddr, sz are
  65                 * not initialized when this code path is taken. Silence it.
  66                 */
  67                return;
  68        }
  69
  70        for (i = 0; i < count; ++i) {
  71                rcv_fds[i] = socket(family, proto, 0);
  72                if (rcv_fds[i] < 0)
  73                        error(1, errno, "failed to create receive socket");
  74
  75                opt = 1;
  76                if (setsockopt(rcv_fds[i], SOL_SOCKET, SO_REUSEPORT, &opt,
  77                               sizeof(opt)))
  78                        error(1, errno, "failed to set SO_REUSEPORT");
  79
  80                if (bind(rcv_fds[i], addr, sz))
  81                        error(1, errno, "failed to bind receive socket");
  82
  83                if (proto == SOCK_STREAM && listen(rcv_fds[i], 10))
  84                        error(1, errno, "tcp: failed to listen on receive port");
  85                else if (proto == SOCK_DCCP) {
  86                        if (setsockopt(rcv_fds[i], SOL_DCCP,
  87                                        DCCP_SOCKOPT_SERVICE,
  88                                        &(int) {htonl(42)}, sizeof(int)))
  89                                error(1, errno, "failed to setsockopt");
  90
  91                        if (listen(rcv_fds[i], 10))
  92                                error(1, errno, "dccp: failed to listen on receive port");
  93                }
  94        }
  95}
  96
  97static int connect_and_send(int family, int proto)
  98{
  99        struct sockaddr_in  saddr4 = {0};
 100        struct sockaddr_in  daddr4 = {0};
 101        struct sockaddr_in6 saddr6 = {0};
 102        struct sockaddr_in6 daddr6 = {0};
 103        struct sockaddr *saddr, *daddr;
 104        int fd, sz;
 105
 106        switch (family) {
 107        case AF_INET:
 108                saddr4.sin_family = AF_INET;
 109                saddr4.sin_addr.s_addr = htonl(INADDR_ANY);
 110                saddr4.sin_port = 0;
 111
 112                daddr4.sin_family = AF_INET;
 113                if (!inet_pton(family, IP4_ADDR, &daddr4.sin_addr.s_addr))
 114                        error(1, errno, "inet_pton failed: %s", IP4_ADDR);
 115                daddr4.sin_port = htons(PORT);
 116
 117                sz = sizeof(saddr4);
 118                saddr = (struct sockaddr *)&saddr4;
 119                daddr = (struct sockaddr *)&daddr4;
 120        break;
 121        case AF_INET6:
 122                saddr6.sin6_family = AF_INET6;
 123                saddr6.sin6_addr = in6addr_any;
 124
 125                daddr6.sin6_family = AF_INET6;
 126                if (!inet_pton(family, IP6_ADDR, &daddr6.sin6_addr))
 127                        error(1, errno, "inet_pton failed: %s", IP6_ADDR);
 128                daddr6.sin6_port = htons(PORT);
 129
 130                sz = sizeof(saddr6);
 131                saddr = (struct sockaddr *)&saddr6;
 132                daddr = (struct sockaddr *)&daddr6;
 133        break;
 134        default:
 135                error(1, 0, "Unsupported family %d", family);
 136                /* clang does not recognize error() above as terminating
 137                 * the program, so it complains that saddr, daddr, sz are
 138                 * not initialized when this code path is taken. Silence it.
 139                 */
 140                return -1;
 141        }
 142
 143        fd = socket(family, proto, 0);
 144        if (fd < 0)
 145                error(1, errno, "failed to create send socket");
 146
 147        if (proto == SOCK_DCCP &&
 148                setsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE,
 149                                &(int){htonl(42)}, sizeof(int)))
 150                error(1, errno, "failed to setsockopt");
 151
 152        if (bind(fd, saddr, sz))
 153                error(1, errno, "failed to bind send socket");
 154
 155        if (connect(fd, daddr, sz))
 156                error(1, errno, "failed to connect send socket");
 157
 158        if (send(fd, "a", 1, 0) < 0)
 159                error(1, errno, "failed to send message");
 160
 161        return fd;
 162}
 163
 164static int receive_once(int epfd, int proto)
 165{
 166        struct epoll_event ev;
 167        int i, fd;
 168        char buf[8];
 169
 170        i = epoll_wait(epfd, &ev, 1, 3);
 171        if (i < 0)
 172                error(1, errno, "epoll_wait failed");
 173
 174        if (proto == SOCK_STREAM || proto == SOCK_DCCP) {
 175                fd = accept(ev.data.fd, NULL, NULL);
 176                if (fd < 0)
 177                        error(1, errno, "failed to accept");
 178                i = recv(fd, buf, sizeof(buf), 0);
 179                close(fd);
 180        } else {
 181                i = recv(ev.data.fd, buf, sizeof(buf), 0);
 182        }
 183
 184        if (i < 0)
 185                error(1, errno, "failed to recv");
 186
 187        return ev.data.fd;
 188}
 189
 190static void test(int *rcv_fds, int count, int family, int proto, int fd)
 191{
 192        struct epoll_event ev;
 193        int epfd, i, send_fd, recv_fd;
 194
 195        epfd = epoll_create(1);
 196        if (epfd < 0)
 197                error(1, errno, "failed to create epoll");
 198
 199        ev.events = EPOLLIN;
 200        for (i = 0; i < count; ++i) {
 201                ev.data.fd = rcv_fds[i];
 202                if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fds[i], &ev))
 203                        error(1, errno, "failed to register sock epoll");
 204        }
 205
 206        send_fd = connect_and_send(family, proto);
 207
 208        recv_fd = receive_once(epfd, proto);
 209        if (recv_fd != fd)
 210                error(1, 0, "received on an unexpected socket");
 211
 212        close(send_fd);
 213        close(epfd);
 214}
 215
 216
 217static void run_one_test(int fam_send, int fam_rcv, int proto,
 218                         const char *addr_str)
 219{
 220        /* Below we test that a socket listening on a specific address
 221         * is always selected in preference over a socket listening
 222         * on addr_any. Bugs where this is not the case often result
 223         * in sockets created first or last to get picked. So below
 224         * we make sure that there are always addr_any sockets created
 225         * before and after a specific socket is created.
 226         */
 227        int rcv_fds[10], i;
 228
 229        build_rcv_fd(AF_INET, proto, rcv_fds, 2, NULL);
 230        build_rcv_fd(AF_INET6, proto, rcv_fds + 2, 2, NULL);
 231        build_rcv_fd(fam_rcv, proto, rcv_fds + 4, 1, addr_str);
 232        build_rcv_fd(AF_INET, proto, rcv_fds + 5, 2, NULL);
 233        build_rcv_fd(AF_INET6, proto, rcv_fds + 7, 2, NULL);
 234        test(rcv_fds, 9, fam_send, proto, rcv_fds[4]);
 235        for (i = 0; i < 9; ++i)
 236                close(rcv_fds[i]);
 237        fprintf(stderr, "pass\n");
 238}
 239
 240static void test_proto(int proto, const char *proto_str)
 241{
 242        if (proto == SOCK_DCCP) {
 243                int test_fd;
 244
 245                test_fd = socket(AF_INET, proto, 0);
 246                if (test_fd < 0) {
 247                        if (errno == ESOCKTNOSUPPORT) {
 248                                fprintf(stderr, "DCCP not supported: skipping DCCP tests\n");
 249                                return;
 250                        } else
 251                                error(1, errno, "failed to create a DCCP socket");
 252                }
 253                close(test_fd);
 254        }
 255
 256        fprintf(stderr, "%s IPv4 ... ", proto_str);
 257        run_one_test(AF_INET, AF_INET, proto, IP4_ADDR);
 258
 259        fprintf(stderr, "%s IPv6 ... ", proto_str);
 260        run_one_test(AF_INET6, AF_INET6, proto, IP6_ADDR);
 261
 262        fprintf(stderr, "%s IPv4 mapped to IPv6 ... ", proto_str);
 263        run_one_test(AF_INET, AF_INET6, proto, IP4_MAPPED6);
 264}
 265
 266int main(void)
 267{
 268        test_proto(SOCK_DGRAM, "UDP");
 269        test_proto(SOCK_STREAM, "TCP");
 270        test_proto(SOCK_DCCP, "DCCP");
 271
 272        fprintf(stderr, "SUCCESS\n");
 273        return 0;
 274}
 275