Skip to content

Commit 40e9b70

Browse files
committed
net: filter proxy_protocol per socket
1 parent e443372 commit 40e9b70

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

cfg.lex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ ANYCAST ("anycast"|"ANYCAST")
345345
ACCEPT_SUBDOMAIN ("accept_subdomain"|"ACCEPT_SUBDOMAIN")
346346
FRAG ("frag"|"FRAG")
347347
REUSE_PORT ("reuse_port"|"REUSE_PORT")
348+
PROXY_PROTOCOL (("allow"|"ALLOW")[-_])?("proxy"|"PROXY")([-_]("protocol"|"PROXY_PROTOCOL"))?
348349
349350
350351
COM_LINE #
@@ -620,6 +621,7 @@ SPACE [ ]
620621
<INITIAL>{ANYCAST} { count(); return ANYCAST; }
621622
<INITIAL>{ACCEPT_SUBDOMAIN} { count(); return ACCEPT_SUBDOMAIN; }
622623
<INITIAL>{REUSE_PORT} { count(); return REUSE_PORT; }
624+
<INITIAL>{PROXY_PROTOCOL} { count(); return PROXY_PROTOCOL; }
623625
<INITIAL>{FRAG} { count(); return FRAG; }
624626
<INITIAL>{SLASH} { count(); return SLASH; }
625627
<INITIAL>{SCALE_UP_TO} { count(); return SCALE_UP_TO; }

cfg.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ extern int cfg_parse_only_routes;
471471
%token ACCEPT_SUBDOMAIN
472472
%token FRAG
473473
%token REUSE_PORT
474+
%token PROXY_PROTOCOL
474475
%token SCRIPTVARERR
475476
%token SCALE_UP_TO
476477
%token SCALE_DOWN_TO
@@ -744,6 +745,9 @@ socket_def_param: ANYCAST { IFOR();
744745
| ACCEPT_SUBDOMAIN { IFOR();
745746
p_tmp.flags |= SI_ACCEPT_SUBDOMAIN_ALIAS;
746747
}
748+
| PROXY_PROTOCOL { IFOR();
749+
p_tmp.flags |= SI_PROXY;
750+
}
747751
| USE_WORKERS NUMBER { IFOR();
748752
p_tmp.workers=$2;
749753
}

ip_addr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ struct proxy_protocol {
101101

102102

103103
enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
104-
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64, SI_ACCEPT_SUBDOMAIN_ALIAS=128 };
104+
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64,
105+
SI_ACCEPT_SUBDOMAIN_ALIAS=128, SI_PROXY=256 };
105106

106107
struct receive_info {
107108
struct ip_addr src_ip;

net/proxy_protocol.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "../socket_info.h"
2626
#include "tcp_conn_defs.h"
2727
#include "../ut.h"
28+
#include "../socket_info.h"
2829

2930
#define PROXY_PROTOCOL_V1_HDR "PROXY "
3031
#define PROXY_PROTOCOL_V1_HDR_LEN (sizeof(PROXY_PROTOCOL_V1_HDR) - 1)
@@ -302,9 +303,14 @@ int check_tcp_proxy_protocol(struct tcp_connection *c)
302303
if (c->flags & F_CONN_DATA_READY)
303304
return 1;
304305

306+
if (!c->rcv.bind_address || (c->rcv.bind_address->flags & SI_PROXY) == 0) {
307+
c->flags |= F_CONN_DATA_READY;
308+
return 1;
309+
}
310+
305311
len = tcp_peek(c, pp_buf, PROXY_PROTOCOL_BUF_MAX);
306312
if (len < 0)
307-
return len;
313+
return -1;
308314

309315
switch (is_net_proxy_protocol(pp_buf, len)) {
310316
case -1:
@@ -351,6 +357,10 @@ int check_udp_proxy_protocol(char **buf, int *size, struct receive_info *ri)
351357
return -1;
352358

353359
ri->real_ep.flags = PP_INIT;
360+
361+
if (!ri->bind_address || (ri->bind_address->flags & SI_PROXY) == 0)
362+
return 1;
363+
354364
msg = *buf;
355365
len = *size;
356366

0 commit comments

Comments
 (0)