Commit Diff


commit - 4f8b63696d9758baaed91352d95e5d1084674c8b
commit + 80347df32a3fd8cd66aeaf059fbfd16c95a2ffe7
blob - 2e1ef449b045f960e336d64eb66fb6a8b3a51c93
blob + db2ed0f0867cf6c8651003265601b696857dc228
--- Makefile
+++ Makefile
@@ -1,11 +1,12 @@
-#	$OpenBSD: Makefile,v 1.35 2020/10/26 16:52:06 martijn Exp $
+#	$OpenBSD: Makefile,v 1.36 2026/02/18 22:27:03 kirill Exp $
 
 PROG=		relayd
 SRCS=		parse.y
 SRCS+=		agentx_control.c ca.c carp.c check_icmp.c check_script.c \
 		check_tcp.c check_tls.c config.c control.c hce.c log.c \
-		name2id.c pfe.c pfe_filter.c pfe_route.c proc.c relay.c \
-		relay_http.c relay_udp.c relayd.c shuffle.c ssl.c util.c
+		name2id.c pfe.c pfe_filter.c pfe_route.c proc.c \
+		proxy_protocol.c relay.c relay_http.c relay_udp.c relayd.c \
+		shuffle.c ssl.c util.c
 MAN=		relayd.8 relayd.conf.5
 
 LDADD=		-lagentx -levent -ltls -lssl -lcrypto -lutil
blob - fcdfb8e92e3710e8fcc2ed2eb19593d716ab4a9f
blob + b4660f7f1efb3cd0b1b8cd1e3760826ffd2c7e56
--- parse.y
+++ parse.y
@@ -1,4 +1,4 @@
-/*	$OpenBSD: parse.y,v 1.258 2024/10/28 19:56:18 tb Exp $	*/
+/*	$OpenBSD: parse.y,v 1.259 2026/02/18 22:27:03 kirill Exp $	*/
 
 /*
  * Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -179,13 +179,13 @@ typedef struct {
 %token	TIMEOUT TLS TO ROUTER RTLABEL TRANSPARENT URL WITH TTL RTABLE
 %token	MATCH PARAMS RANDOM LEASTSTATES SRCHASH KEY CERTIFICATE PASSWORD ECDHE
 %token	EDH TICKETS CONNECTION CONNECTIONS CONTEXT ERRORS STATE CHANGES CHECKS
-%token	WEBSOCKETS PFLOG CLIENT
+%token	WEBSOCKETS PFLOG CLIENT PROXYPROTO V1 V2
 %token	<v.string>	STRING
 %token  <v.number>	NUMBER
 %type	<v.string>	context hostname interface table value path
 %type	<v.number>	http_type loglevel quick
 %type	<v.number>	dstmode flag forwardmode retry
-%type	<v.number>	opttls opttlsclient
+%type	<v.number>	opttls opttlsclient optproxyproto
 %type	<v.number>	redirect_proto relay_proto match pflog
 %type	<v.number>	action ruleaf key_option
 %type	<v.port>	port
@@ -1103,6 +1103,11 @@ proto		: relay_proto PROTO STRING	{
 		}
 		;
 
+optproxyproto	: /* empty */	{ $$ = 0; }
+		| PROXYPROTO V1	{ $$ = F_PROXYV1; }
+		| PROXYPROTO V2 { $$ = F_PROXYV2; }
+		;
+
 protopts_n	: /* empty */
 		| '{' '}'
 		| '{' optnl protopts_l '}'
@@ -1946,7 +1951,7 @@ relayoptsl	: LISTEN ON STRING port opttls {
 			tableport = h->port.val[0];
 			host_free(&al);
 		}
-		| forwardmode opttlsclient TO forwardspec dstaf {
+		| forwardmode opttlsclient TO forwardspec dstaf optproxyproto {
 			rlay->rl_conf.fwdmode = $1;
 			if ($1 == FWD_ROUTE) {
 				yyerror("no route for relays");
@@ -1956,6 +1961,8 @@ relayoptsl	: LISTEN ON STRING port opttls {
 				rlay->rl_conf.flags |= F_TLSCLIENT;
 				conf->sc_conf.flags |= F_TLSCLIENT;
 			}
+
+			rlay->rl_conf.flags |= $6;
 		}
 		| SESSION TIMEOUT NUMBER		{
 			if ((rlay->rl_conf.timeout.tv_sec = $3) < 0) {
@@ -1983,6 +1990,7 @@ relayoptsl	: LISTEN ON STRING port opttls {
 				free($2);
 				YYERROR;
 			}
+
 			p->flags |= F_USED;
 			rlay->rl_conf.proto = p->id;
 			rlay->rl_proto = p;
@@ -2479,6 +2487,7 @@ lookup(char *s)
 		{ "prefork",		PREFORK },
 		{ "priority",		PRIORITY },
 		{ "protocol",		PROTO },
+		{ "proxy-protocol",	PROXYPROTO },
 		{ "query",		QUERYSTR },
 		{ "quick",		QUICK },
 		{ "random",		RANDOM },
@@ -2518,6 +2527,8 @@ lookup(char *s)
 		{ "transparent",	TRANSPARENT },
 		{ "ttl",		TTL },
 		{ "url",		URL },
+		{ "v1",			V1 },
+		{ "v2",			V2 },
 		{ "value",		VALUE },
 		{ "websockets",		WEBSOCKETS },
 		{ "with",		WITH }
blob - 6d0970802c5edf8374b5aeb69be17b0814676cb9
blob + ff2a679650270ba73327fe9f1b16dcd60498a047
--- relay.c
+++ relay.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: relay.c,v 1.260 2024/10/28 19:56:18 tb Exp $	*/
+/*	$OpenBSD: relay.c,v 1.261 2026/02/18 22:27:03 kirill Exp $	*/
 
 /*
  * Copyright (c) 2006 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -767,6 +767,19 @@ relay_connected(int fd, short sig, void *arg)
 		    "failed to allocate output buffer event", 0);
 		return;
 	}
+
+	error = 0;
+
+	if (rlay->rl_conf.flags & F_PROXYV1)
+		error = proxy_protocol_v1(con, bev->output);
+	else if (rlay->rl_conf.flags & F_PROXYV2)
+		error = proxy_protocol_v2(con, bev->output);
+
+	if (error) {
+		relay_abort_http(con, 500, "failed to write PROXY header", 0);
+		return;
+	}
+
 	/* write pending output buffer now */
 	if (bufferevent_write_buffer(bev, con->se_out.output)) {
 		relay_abort_http(con, 500, strerror(errno), 0);
blob - /dev/null
blob + ef13d7dd770a603d4285dc7713283e88be626228 (mode 644)
--- /dev/null
+++ proxy_protocol.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2026 Christoph Liebender <christoph@liebender.dev>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "relayd.h"
+
+#define PROXY_V2_CMD_PROXY 0x01
+
+#define PROXY_V2_FAM_UNSPEC 0x00
+#define PROXY_V2_FAM_TCP4   0x11
+#define PROXY_V2_FAM_UDP4   0x12
+#define PROXY_V2_FAM_TCP6   0x21
+#define PROXY_V2_FAM_UDP6   0x22
+
+static const u_int8_t PROXY_V2_SIG[12] = {
+	0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A
+};
+
+struct proxy_v2_hdr {
+	u_int8_t sig[12];
+	u_int8_t ver_cmd;
+	u_int8_t fam;
+	u_int16_t len;
+};
+
+union proxy_v2_addr {
+	struct {
+		u_int32_t src_addr;
+		u_int32_t dst_addr;
+		in_port_t src_port;
+		in_port_t dst_port;
+	} ipv4_addr;
+	struct {
+		u_int8_t  src_addr[16];
+		u_int8_t  dst_addr[16];
+		in_port_t src_port;
+		in_port_t dst_port;
+	} ipv6_addr;
+};
+
+int
+proxy_protocol_v1(struct rsession *con, struct evbuffer *dstout)
+{
+	char ibuf[128], obuf[128];
+	const char *proxyproto;
+	int ret;
+
+	bzero(&ibuf, sizeof(ibuf));
+	bzero(&obuf, sizeof(obuf));
+
+	if (print_host(&con->se_in.ss, ibuf, sizeof(ibuf)) == NULL ||
+	    print_host(&con->se_sockname, obuf, sizeof(obuf)) == NULL)
+		return -1;
+
+	if (con->se_relay->rl_conf.flags & F_UDP)
+		proxyproto = "UNKNOWN";
+	else {
+		switch (con->se_in.ss.ss_family) {
+		case AF_INET:
+			proxyproto = "TCP4";
+			break;
+		case AF_INET6:
+			proxyproto = "TCP6";
+			break;
+		default:
+			proxyproto = "UNKNOWN";
+			break;
+		}
+	}
+
+	ret = evbuffer_add_printf(dstout,
+		"PROXY %s %s %s %d %d\r\n", proxyproto, ibuf, obuf,
+		ntohs(con->se_in.port), ntohs(con->se_relay->rl_conf.port));
+
+	return ret == -1 ? -1 : 0;
+}
+
+int
+proxy_protocol_v2(struct rsession *con, struct evbuffer *dstout)
+{
+	union proxy_v2_addr 	       addr;
+	struct proxy_v2_hdr 	       hdr;
+	const struct relay_config     *conf = &con->se_relay->rl_conf;
+	const struct sockaddr_storage *srcss = &con->se_in.ss;
+	const struct sockaddr_storage *dstss = &con->se_sockname;
+	int 			       error;
+	in_port_t		       srcport = con->se_in.port;
+	in_port_t		       dstport = conf->port;
+	u_int16_t 		       len;
+
+	bcopy(PROXY_V2_SIG, hdr.sig, sizeof(hdr.sig));
+	hdr.ver_cmd = 0x20 | PROXY_V2_CMD_PROXY;
+
+	switch (dstss->ss_family) {
+	case AF_INET:
+		hdr.fam = (conf->flags & F_UDP) ?
+			PROXY_V2_FAM_UDP4 : PROXY_V2_FAM_TCP4;
+		len = sizeof(addr.ipv4_addr);
+		addr.ipv4_addr.src_addr =
+		    ((const struct sockaddr_in *)srcss)->sin_addr.s_addr;
+		addr.ipv4_addr.dst_addr =
+		    ((const struct sockaddr_in *)dstss)->sin_addr.s_addr;
+		addr.ipv4_addr.src_port = srcport;
+		addr.ipv4_addr.dst_port = dstport;
+		break;
+	case AF_INET6:
+		hdr.fam = (conf->flags & F_UDP) ?
+			PROXY_V2_FAM_UDP6 : PROXY_V2_FAM_TCP6;
+		len = sizeof(addr.ipv6_addr);
+		bcopy(&((const struct sockaddr_in6 *)srcss)->sin6_addr,
+		    addr.ipv6_addr.src_addr, sizeof(addr.ipv6_addr.src_addr));
+		bcopy(&((const struct sockaddr_in6 *)dstss)->sin6_addr,
+		    addr.ipv6_addr.dst_addr, sizeof(addr.ipv6_addr.dst_addr));
+		addr.ipv6_addr.src_port = srcport;
+		addr.ipv6_addr.dst_port = dstport;
+		break;
+	default:
+		hdr.fam = 0x00;
+		len = 0;
+		break;
+	}
+
+	hdr.len = htons(len);
+
+	if ((error = evbuffer_add(dstout, &hdr, sizeof(hdr))) != 0)
+		return error;
+
+	if (len > 0 && (error = evbuffer_add(dstout, &addr, len)) != 0)
+		return error;
+
+	return 0;
+}
blob - 5ae11c5ac013642d06e2a89205dfc00af698ce5a
blob + a63dd4e3a41c38d9399b068d271ce69a342febce
--- relayd.conf.5
+++ relayd.conf.5
@@ -1,4 +1,4 @@
-.\"	$OpenBSD: relayd.conf.5,v 1.214 2026/01/18 16:38:02 schwarze Exp $
+.\"	$OpenBSD: relayd.conf.5,v 1.215 2026/02/18 22:27:03 kirill Exp $
 .\"
 .\" Copyright (c) 2006 - 2016 Reyk Floeter <reyk@openbsd.org>
 .\" Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -15,7 +15,7 @@
 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 .\"
-.Dd $Mdocdate: January 18 2026 $
+.Dd $Mdocdate: February 18 2026 $
 .Dt RELAYD.CONF 5
 .Os
 .Sh NAME
@@ -673,6 +673,13 @@ option will be used as a tolerance for failed
 host connections; the connection will be retried for
 .Ar number
 more times.
+.It Ic proxy-protocol Pq Ic v1 Ns | Ns Ic v2
+Upon connection to the destination,
+.Xr relayd 8
+will prepend a PROXY protocol header of the specified version to the relayed
+data, with the IP address and port of the remote host as well as the
+.Xr relayd 8
+server.
 .El
 .It Xo
 .Ic forward to
blob - 3b5c3987f93e1a42fd5966a957cae82cf870d35a
blob + 0832e799f107ffbfe4d9e3dc821175cdc3f52cbb
--- relayd.h
+++ relayd.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: relayd.h,v 1.276 2024/10/28 19:56:18 tb Exp $	*/
+/*	$OpenBSD: relayd.h,v 1.277 2026/02/18 22:27:03 kirill Exp $	*/
 
 /*
  * Copyright (c) 2006 - 2016 Reyk Floeter <reyk@openbsd.org>
@@ -402,6 +402,8 @@ union hashkey {
 #define F_HASHKEY		0x08000000
 #define F_AGENTX_TRAPONLY	0x10000000
 #define F_PFLOG			0x20000000
+#define F_PROXYV1		0x40000000
+#define F_PROXYV2		0x80000000
 
 #define F_BITS								\
 	"\10\01DISABLE\02BACKUP\03USED\04DOWN\05ADD\06DEL\07CHANGED"	\
@@ -1476,4 +1478,8 @@ int	 config_getrelay(struct relayd *, struct imsg *);
 int	 config_getrelaytable(struct relayd *, struct imsg *);
 int	 config_getrelayfd(struct relayd *, struct imsg *);
 
+/* proxy_protocol.c */
+int	proxy_protocol_v1(struct rsession *, struct evbuffer *);
+int	proxy_protocol_v2(struct rsession *, struct evbuffer *);
+
 #endif /* RELAYD_H */