From: Valentine Krasnobaeva Date: Thu, 25 Apr 2024 16:57:27 +0000 (+0200) Subject: MINOR: sock_set_mark: take sock family in account X-Git-Tag: v3.0-dev10~29 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=772d070ab54a9a5c30bfb5d65178c4bbc978a61c;p=thirdparty%2Fhaproxy.git MINOR: sock_set_mark: take sock family in account SO_MARK, SO_USER_COOKIE, SO_RTABLE socket options (used to set the special mark/ID on socket, in order to perform mark-based routing) are only supported by AF_INET sockets. So, let's check socket address family, when we enter into this function. --- diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h index 8934bb62cd..6ca737c0de 100644 --- a/include/haproxy/connection.h +++ b/include/haproxy/connection.h @@ -433,7 +433,7 @@ static inline void conn_set_mark(const struct connection *conn, int mark) if (!conn || !conn_ctrl_ready(conn) || (conn->flags & CO_FL_FDLESS)) return; - sock_set_mark(conn->handle.fd, mark); + sock_set_mark(conn->handle.fd, conn->ctrl->fam->sock_family, mark); } /* Sets adjust the TCP quick-ack feature on the connection's socket. The diff --git a/include/haproxy/sock.h b/include/haproxy/sock.h index 4f628e8a98..b8bd731b07 100644 --- a/include/haproxy/sock.h +++ b/include/haproxy/sock.h @@ -70,15 +70,19 @@ static inline void sock_set_tos(int fd, struct sockaddr_storage *addr, int tos) } /* Sets mark sockopt on socket */ -static inline void sock_set_mark(int fd, int mark) +static inline void sock_set_mark(int fd, sa_family_t sock_family, int mark) { + if ((sock_family == AF_INET) || (sock_family == AF_INET6)) { #if defined(SO_MARK) - setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); + setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); +/* FreeBSD */ #elif defined(SO_USER_COOKIE) - setsockopt(fd, SOL_SOCKET, SO_USER_COOKIE, &mark, sizeof(mark)); + setsockopt(fd, SOL_SOCKET, SO_USER_COOKIE, &mark, sizeof(mark)); +/* OpenBSD */ #elif defined(SO_RTABLE) - setsockopt(fd, SOL_SOCKET, SO_RTABLE, &mark, sizeof(mark)); + setsockopt(fd, SOL_SOCKET, SO_RTABLE, &mark, sizeof(mark)); #endif + } } #endif /* _HAPROXY_SOCK_H */ diff --git a/src/sock.c b/src/sock.c index 80cddcd63d..215fddb517 100644 --- a/src/sock.c +++ b/src/sock.c @@ -218,7 +218,7 @@ int sock_create_server_socket(struct connection *conn) if (sock_fd == -1) goto end; if (conn->flags & CO_FL_OPT_MARK) - sock_set_mark(sock_fd, conn->mark); + sock_set_mark(sock_fd, conn->ctrl->fam->sock_family, conn->mark); if (conn->flags & CO_FL_OPT_TOS) sock_set_tos(sock_fd, conn->dst, conn->tos);