Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic code for native (BSD-compatible) sockets |
| 3 | * |
| 4 | * Copyright 2000-2020 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | #include <sys/param.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 25 | #include <net/if.h> |
| 26 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
| 28 | #include <haproxy/connection.h> |
Willy Tarreau | 063d47d | 2020-08-28 16:29:53 +0200 | [diff] [blame] | 29 | #include <haproxy/listener-t.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 30 | #include <haproxy/namespace.h> |
| 31 | #include <haproxy/sock.h> |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 32 | #include <haproxy/sock_inet.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 33 | #include <haproxy/tools.h> |
| 34 | |
Willy Tarreau | 063d47d | 2020-08-28 16:29:53 +0200 | [diff] [blame] | 35 | /* the list of remaining sockets transferred from an older process */ |
| 36 | struct xfer_sock_list *xfer_sock_list = NULL; |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 37 | |
| 38 | /* Create a socket to connect to the server in conn->dst (which MUST be valid), |
| 39 | * using the configured namespace if needed, or the one passed by the proxy |
| 40 | * protocol if required to do so. It ultimately calls socket() or socketat() |
| 41 | * and returns the FD or error code. |
| 42 | */ |
| 43 | int sock_create_server_socket(struct connection *conn) |
| 44 | { |
| 45 | const struct netns_entry *ns = NULL; |
| 46 | |
| 47 | #ifdef USE_NS |
| 48 | if (objt_server(conn->target)) { |
| 49 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 50 | ns = conn->proxy_netns; |
| 51 | else |
| 52 | ns = __objt_server(conn->target)->netns; |
| 53 | } |
| 54 | #endif |
| 55 | return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0); |
| 56 | } |
| 57 | |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 58 | /* Enables receiving on receiver <rx> once already bound. Does nothing in early |
| 59 | * boot (needs fd_updt). |
| 60 | */ |
| 61 | void sock_enable(struct receiver *rx) |
| 62 | { |
| 63 | if (rx->flags & RX_F_BOUND && fd_updt) |
| 64 | fd_want_recv(rx->fd); |
| 65 | } |
| 66 | |
| 67 | /* Disables receiving on receiver <rx> once already bound. Does nothing in early |
| 68 | * boot (needs fd_updt). |
| 69 | */ |
| 70 | void sock_disable(struct receiver *rx) |
| 71 | { |
| 72 | if (rx->flags & RX_F_BOUND && fd_updt) |
| 73 | fd_stop_recv(rx->fd); |
| 74 | } |
| 75 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 76 | /* stops, unbinds and possibly closes the FD associated with receiver rx */ |
| 77 | void sock_unbind(struct receiver *rx) |
| 78 | { |
| 79 | /* There are a number of situations where we prefer to keep the FD and |
| 80 | * not to close it (unless we're stopping, of course): |
| 81 | * - worker process unbinding from a worker's FD with socket transfer enabled => keep |
| 82 | * - master process unbinding from a master's inherited FD => keep |
| 83 | * - master process unbinding from a master's FD => close |
| 84 | * - master process unbinding from a worker's FD => close |
| 85 | * - worker process unbinding from a master's FD => close |
| 86 | * - worker process unbinding from a worker's FD => close |
| 87 | */ |
| 88 | if (rx->flags & RX_F_BOUND) |
| 89 | rx->proto->rx_disable(rx); |
| 90 | |
| 91 | if (!stopping && !master && |
| 92 | !(rx->flags & RX_F_MWORKER) && |
| 93 | (global.tune.options & GTUNE_SOCKET_TRANSFER)) |
| 94 | return; |
| 95 | |
| 96 | if (!stopping && master && |
| 97 | rx->flags & RX_F_MWORKER && |
| 98 | rx->flags & RX_F_INHERITED) |
| 99 | return; |
| 100 | |
| 101 | rx->flags &= ~RX_F_BOUND; |
| 102 | if (rx->fd != -1) |
| 103 | fd_delete(rx->fd); |
| 104 | rx->fd = -1; |
| 105 | } |
| 106 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 107 | /* |
| 108 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 109 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 110 | * success, -1 in case of error. The socket's source address is stored in |
| 111 | * <sa> for <salen> bytes. |
| 112 | */ |
| 113 | int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 114 | { |
| 115 | if (dir) |
| 116 | return getsockname(fd, sa, &salen); |
| 117 | else |
| 118 | return getpeername(fd, sa, &salen); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 123 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 124 | * case of success, -1 in case of error. The socket's source address is stored |
| 125 | * in <sa> for <salen> bytes. |
| 126 | */ |
| 127 | int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 128 | { |
| 129 | if (dir) |
| 130 | return getpeername(fd, sa, &salen); |
| 131 | else |
| 132 | return getsockname(fd, sa, &salen); |
| 133 | } |
| 134 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 135 | /* Try to retrieve exported sockets from worker at CLI <unixsocket>. These |
| 136 | * ones will be placed into the xfer_sock_list for later use by function |
| 137 | * sock_find_compatible_fd(). Returns 0 on success, -1 on failure. |
| 138 | */ |
| 139 | int sock_get_old_sockets(const char *unixsocket) |
| 140 | { |
| 141 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 142 | int *tmpfd = NULL; |
| 143 | struct sockaddr_un addr; |
| 144 | struct cmsghdr *cmsg; |
| 145 | struct msghdr msghdr; |
| 146 | struct iovec iov; |
| 147 | struct xfer_sock_list *xfer_sock = NULL; |
| 148 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
| 149 | int sock = -1; |
| 150 | int ret = -1; |
| 151 | int ret2 = -1; |
| 152 | int fd_nb; |
| 153 | int got_fd = 0; |
| 154 | int cur_fd = 0; |
| 155 | size_t maxoff = 0, curoff = 0; |
| 156 | |
| 157 | memset(&msghdr, 0, sizeof(msghdr)); |
| 158 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 159 | if (!cmsgbuf) { |
| 160 | ha_warning("Failed to allocate memory to send sockets\n"); |
| 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 165 | if (sock < 0) { |
| 166 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 167 | goto out; |
| 168 | } |
| 169 | |
| 170 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1); |
| 171 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 172 | addr.sun_family = PF_UNIX; |
| 173 | |
| 174 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 175 | if (ret < 0) { |
| 176 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
| 181 | iov.iov_base = &fd_nb; |
| 182 | iov.iov_len = sizeof(fd_nb); |
| 183 | msghdr.msg_iov = &iov; |
| 184 | msghdr.msg_iovlen = 1; |
| 185 | |
| 186 | if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) { |
| 187 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | /* First, get the number of file descriptors to be received */ |
| 192 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
| 193 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | if (fd_nb == 0) { |
| 198 | ret2 = 0; |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
| 203 | if (tmpbuf == NULL) { |
| 204 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 209 | if (tmpfd == NULL) { |
| 210 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | msghdr.msg_control = cmsgbuf; |
| 215 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
| 216 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
| 217 | |
| 218 | do { |
| 219 | int ret3; |
| 220 | |
| 221 | iov.iov_base = tmpbuf + curoff; |
| 222 | |
| 223 | ret = recvmsg(sock, &msghdr, 0); |
| 224 | |
| 225 | if (ret == -1 && errno == EINTR) |
| 226 | continue; |
| 227 | |
| 228 | if (ret <= 0) |
| 229 | break; |
| 230 | |
| 231 | /* Send an ack to let the sender know we got the sockets |
| 232 | * and it can send some more |
| 233 | */ |
| 234 | do { |
| 235 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 236 | } while (ret3 == -1 && errno == EINTR); |
| 237 | |
| 238 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 239 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 240 | size_t totlen = cmsg->cmsg_len - CMSG_LEN(0); |
| 241 | |
| 242 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
| 243 | ha_warning("Got to many sockets !\n"); |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Be paranoid and use memcpy() to avoid any |
| 249 | * potential alignment issue. |
| 250 | */ |
| 251 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 252 | got_fd += totlen / sizeof(int); |
| 253 | } |
| 254 | } |
| 255 | curoff += ret; |
| 256 | } while (got_fd < fd_nb); |
| 257 | |
| 258 | if (got_fd != fd_nb) { |
| 259 | ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 260 | fd_nb, got_fd); |
| 261 | goto out; |
| 262 | } |
| 263 | |
| 264 | maxoff = curoff; |
| 265 | curoff = 0; |
| 266 | |
| 267 | for (cur_fd = 0; cur_fd < got_fd; cur_fd++) { |
| 268 | int fd = tmpfd[cur_fd]; |
| 269 | socklen_t socklen; |
| 270 | int val; |
| 271 | int len; |
| 272 | |
| 273 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 274 | if (!xfer_sock) { |
| 275 | ha_warning("Failed to allocate memory in get_old_sockets() !\n"); |
| 276 | break; |
| 277 | } |
| 278 | xfer_sock->fd = -1; |
| 279 | |
| 280 | socklen = sizeof(xfer_sock->addr); |
| 281 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
| 282 | ha_warning("Failed to get socket address\n"); |
| 283 | free(xfer_sock); |
| 284 | xfer_sock = NULL; |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | if (curoff >= maxoff) { |
| 289 | ha_warning("Inconsistency while transferring sockets\n"); |
| 290 | goto out; |
| 291 | } |
| 292 | |
| 293 | len = tmpbuf[curoff++]; |
| 294 | if (len > 0) { |
| 295 | /* We have a namespace */ |
| 296 | if (curoff + len > maxoff) { |
| 297 | ha_warning("Inconsistency while transferring sockets\n"); |
| 298 | goto out; |
| 299 | } |
| 300 | xfer_sock->namespace = malloc(len + 1); |
| 301 | if (!xfer_sock->namespace) { |
| 302 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 303 | goto out; |
| 304 | } |
| 305 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 306 | xfer_sock->namespace[len] = 0; |
| 307 | xfer_sock->ns_namelen = len; |
| 308 | curoff += len; |
| 309 | } |
| 310 | |
| 311 | if (curoff >= maxoff) { |
| 312 | ha_warning("Inconsistency while transferring sockets\n"); |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | len = tmpbuf[curoff++]; |
| 317 | if (len > 0) { |
| 318 | /* We have an interface */ |
| 319 | if (curoff + len > maxoff) { |
| 320 | ha_warning("Inconsistency while transferring sockets\n"); |
| 321 | goto out; |
| 322 | } |
| 323 | xfer_sock->iface = malloc(len + 1); |
| 324 | if (!xfer_sock->iface) { |
| 325 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 326 | goto out; |
| 327 | } |
| 328 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
| 329 | xfer_sock->iface[len] = 0; |
| 330 | xfer_sock->if_namelen = len; |
| 331 | curoff += len; |
| 332 | } |
| 333 | |
| 334 | if (curoff + sizeof(int) > maxoff) { |
| 335 | ha_warning("Inconsistency while transferring sockets\n"); |
| 336 | goto out; |
| 337 | } |
| 338 | |
| 339 | /* we used to have 32 bits of listener options here but we don't |
| 340 | * use them anymore. |
| 341 | */ |
| 342 | curoff += sizeof(int); |
| 343 | |
| 344 | /* determine the foreign status directly from the socket itself */ |
| 345 | if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family)) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 346 | xfer_sock->options |= SOCK_XFER_OPT_FOREIGN; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 347 | |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 348 | socklen = sizeof(val); |
| 349 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM) |
| 350 | xfer_sock->options |= SOCK_XFER_OPT_DGRAM; |
| 351 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 352 | #if defined(IPV6_V6ONLY) |
| 353 | /* keep only the v6only flag depending on what's currently |
| 354 | * active on the socket, and always drop the v4v6 one. |
| 355 | */ |
| 356 | socklen = sizeof(val); |
| 357 | if (xfer_sock->addr.ss_family == AF_INET6 && |
| 358 | getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 359 | xfer_sock->options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 360 | #endif |
| 361 | |
| 362 | xfer_sock->fd = fd; |
| 363 | if (xfer_sock_list) |
| 364 | xfer_sock_list->prev = xfer_sock; |
| 365 | xfer_sock->next = xfer_sock_list; |
| 366 | xfer_sock->prev = NULL; |
| 367 | xfer_sock_list = xfer_sock; |
| 368 | xfer_sock = NULL; |
| 369 | } |
| 370 | |
| 371 | ret2 = 0; |
| 372 | out: |
| 373 | /* If we failed midway make sure to close the remaining |
| 374 | * file descriptors |
| 375 | */ |
| 376 | if (tmpfd != NULL && cur_fd < got_fd) { |
| 377 | for (; cur_fd < got_fd; cur_fd++) { |
| 378 | close(tmpfd[cur_fd]); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | free(tmpbuf); |
| 383 | free(tmpfd); |
| 384 | free(cmsgbuf); |
| 385 | |
| 386 | if (sock != -1) |
| 387 | close(sock); |
| 388 | |
| 389 | if (xfer_sock) { |
| 390 | free(xfer_sock->namespace); |
| 391 | free(xfer_sock->iface); |
| 392 | if (xfer_sock->fd != -1) |
| 393 | close(xfer_sock->fd); |
| 394 | free(xfer_sock); |
| 395 | } |
| 396 | return (ret2); |
| 397 | } |
| 398 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 399 | /* When binding the receivers, check if a socket has been sent to us by the |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 400 | * previous process that we could reuse, instead of creating a new one. Note |
| 401 | * that some address family-specific options are checked on the listener and |
| 402 | * on the socket. Typically for AF_INET and AF_INET6, we check for transparent |
| 403 | * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused |
| 404 | * socket is automatically removed from the list so that it's not proposed |
| 405 | * anymore. |
| 406 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 407 | int sock_find_compatible_fd(const struct receiver *rx) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 408 | { |
| 409 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 410 | int options = 0; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 411 | int if_namelen = 0; |
| 412 | int ns_namelen = 0; |
| 413 | int ret = -1; |
| 414 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 415 | if (!rx->proto->fam->addrcmp) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 416 | return -1; |
| 417 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 418 | if (rx->proto->sock_type == SOCK_DGRAM) |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 419 | options |= SOCK_XFER_OPT_DGRAM; |
| 420 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 421 | if (rx->settings->options & RX_O_FOREIGN) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 422 | options |= SOCK_XFER_OPT_FOREIGN; |
| 423 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 424 | if (rx->addr.ss_family == AF_INET6) { |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 425 | /* Prepare to match the v6only option against what we really want. Note |
| 426 | * that sadly the two options are not exclusive to each other and that |
| 427 | * v6only is stronger than v4v6. |
| 428 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 429 | if ((rx->settings->options & RX_O_V6ONLY) || |
| 430 | (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6))) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 431 | options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 432 | } |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 433 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 434 | if (rx->settings->interface) |
| 435 | if_namelen = strlen(rx->settings->interface); |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 436 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 437 | if (rx->settings->netns) |
| 438 | ns_namelen = rx->settings->netns->name_len; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 439 | #endif |
| 440 | |
| 441 | while (xfer_sock) { |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 442 | if ((options == xfer_sock->options) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 443 | (if_namelen == xfer_sock->if_namelen) && |
| 444 | (ns_namelen == xfer_sock->ns_namelen) && |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 445 | (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 446 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 447 | (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 448 | #endif |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 449 | rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 450 | break; |
| 451 | xfer_sock = xfer_sock->next; |
| 452 | } |
| 453 | |
| 454 | if (xfer_sock != NULL) { |
| 455 | ret = xfer_sock->fd; |
| 456 | if (xfer_sock == xfer_sock_list) |
| 457 | xfer_sock_list = xfer_sock->next; |
| 458 | if (xfer_sock->prev) |
| 459 | xfer_sock->prev->next = xfer_sock->next; |
| 460 | if (xfer_sock->next) |
| 461 | xfer_sock->next->prev = xfer_sock->prev; |
| 462 | free(xfer_sock->iface); |
| 463 | free(xfer_sock->namespace); |
| 464 | free(xfer_sock); |
| 465 | } |
| 466 | return ret; |
| 467 | } |
| 468 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 469 | /* |
| 470 | * Local variables: |
| 471 | * c-indent-level: 8 |
| 472 | * c-basic-offset: 8 |
| 473 | * End: |
| 474 | */ |