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.
This commit is contained in:
Valentine Krasnobaeva 2024-04-25 18:57:27 +02:00 committed by Willy Tarreau
parent d602d568e0
commit 772d070ab5
3 changed files with 10 additions and 6 deletions

View File

@ -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

View File

@ -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 */

View File

@ -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);