mark: the rule's connections send with a mark (#846)

mark <number> extends the allow or deny rule above it, as nolog and
weight do: a connection the rule lets through carries the mark on its
outgoing sockets, towards the destination and towards the parents, so
policy routing and firewall rules can tell its packets apart (SO_MARK
of Linux, SO_USER_COOKIE of FreeBSD; on a system with no mark the
command sets it to nothing). The UDP sockets of a session towards a
destination are marked the same, the SOCKS UDP ASSOCIATE relays among
them, for the rule that let the session through is looked up for each
destination. A mark with no rule above it, or not a number, is refused.
This commit is contained in:
Vladimir Dubrovin committed 2026-11-21 16:40:51 +03:00
1 parent eec09dc4e9
commit 51cbdb2fad
9 files changed
+118

No files matched your search

+18
View File
@@ -1250,6 +1250,24 @@ nolog
Weight may be used for different purposes. Weight may be used for different purposes.
.br
.BR mark
\fI<number>\fR
.br
extends last allow or deny command to set a mark for this request: the
sockets it goes out on, towards the destination and towards the parents,
carry the mark (SO_MARK of Linux, SO_USER_COOKIE of FreeBSD), so that
the packets of the request can be told apart by it in policy routing and
firewall rules. A number is written as in C. UDP is marked the same: the
sockets towards a destination, and the SOCKS UDP ASSOCIATE relays. On a
system with no mark the command sets it to nothing. The mark of a kept
alive request follows the rule which allowed the request (#846).
.br
allow * * 192.168.1.0/24
.br
mark 20
.br .br
.B force .B force
.br .br
+1
View File
@@ -364,6 +364,7 @@ int checkACL(struct clientparam * param){
if(ACLmatches(acentry, param)) { if(ACLmatches(acentry, param)) {
param->nolog = acentry->nolog; param->nolog = acentry->nolog;
param->weight = acentry->weight; param->weight = acentry->weight;
param->mark = acentry->mark;
if(acentry->action == 2) { if(acentry->action == 2) {
struct ace dup; struct ace dup;
int res=60,i=0; int res=60,i=0;
+20
View File
@@ -758,6 +758,25 @@ int intbindtodevice(struct clientparam *param, SOCKET sock){
#endif #endif
} }
/* The mark the matched rule gave the connection, set on its outgoing
sockets where the system has one: SO_MARK of Linux, SO_USER_COOKIE of
FreeBSD. Nowhere else it is silently nothing. */
void setmark(struct clientparam *param, SOCKET sock){
#if defined SO_MARK || defined SO_USER_COOKIE
if(param->mark)
so._setsockopt(so.state, sock, SOL_SOCKET,
#ifdef SO_MARK
SO_MARK,
#else
SO_USER_COOKIE,
#endif
(char *)&param->mark, sizeof(param->mark));
#else
(void)param;
(void)sock;
#endif
}
int doconnect(struct clientparam * param){ int doconnect(struct clientparam * param){
SASIZETYPE size; SASIZETYPE size;
@@ -807,6 +826,7 @@ int doconnect(struct clientparam * param){
} }
*SAPORT(&param->sinsl) = 0; *SAPORT(&param->sinsl) = 0;
setopts(param->remsock, &param->srv->srvsockopts, param->srv->maxseg); setopts(param->remsock, &param->srv->srvsockopts, param->srv->maxseg);
setmark(param, param->remsock);
#ifndef _WIN32 #ifndef _WIN32
/* not on Windows, see the accepting side in proxymain.c */ /* not on Windows, see the accepting side in proxymain.c */
+21
View File
@@ -1313,6 +1313,26 @@ static int h_nolog(int argc, unsigned char **argv){
return 0; return 0;
} }
/* mark <number>: the rule above sends with this mark, on its sockets
towards the destination and the parents, where the system has one */
static int h_mark(int argc, unsigned char **argv){
struct ace *acl = NULL;
char *end;
acl = conf->acl;
if(!acl) {
fprintf(stderr, "Chaining error: last ACL entry was not \"allow/deny\" on line %d\n", linenum);
return(1);
}
while(acl->next) acl = acl->next;
acl->mark = (int)strtol((char *)argv[1], &end, 0);
if(end == (char *)argv[1] || *end) {
fprintf(stderr, "Wrong mark, line %d\n", linenum);
return(1);
}
return 0;
}
int scanipl(unsigned char *arg, struct iplist *dst){ int scanipl(unsigned char *arg, struct iplist *dst){
PROXYSOCKADDRTYPE sa; PROXYSOCKADDRTYPE sa;
char * slash, *dash; char * slash, *dash;
@@ -2177,6 +2197,7 @@ struct commands commandhandlers[]={
{NULL, "filtermaxsize", h_filtermaxsize, 2, 2}, {NULL, "filtermaxsize", h_filtermaxsize, 2, 2},
{NULL, "nolog", h_nolog, 1, 1}, {NULL, "nolog", h_nolog, 1, 1},
{NULL, "weight", h_nolog, 2, 2}, {NULL, "weight", h_nolog, 2, 2},
{NULL, "mark", h_mark, 2, 2},
{NULL, "authcache", h_authcache, 2, 4}, {NULL, "authcache", h_authcache, 2, 4},
{NULL, "smtpp", h_proxy, 1, 0}, {NULL, "smtpp", h_proxy, 1, 0},
{NULL, "delimchar",h_delimchar, 2, 2}, {NULL, "delimchar",h_delimchar, 2, 2},
+5
View File
@@ -442,6 +442,10 @@ static void * ef_ace_nolog(struct node * node){
return &((struct ace *)node->value) -> nolog; return &((struct ace *)node->value) -> nolog;
} }
static void * ef_ace_mark(struct node * node){
return &((struct ace *)node->value) -> mark;
}
static void * ef_bandlimit_next(struct node * node){ static void * ef_bandlimit_next(struct node * node){
return ((struct bandlim *)node->value) -> next; return ((struct bandlim *)node->value) -> next;
} }
@@ -858,6 +862,7 @@ static struct property prop_ace[] = {
{"periods", ef_ace_period, TYPE_PERIOD, "time of the day"}, {"periods", ef_ace_period, TYPE_PERIOD, "time of the day"},
{"weight", ef_ace_weight, TYPE_INTEGER, "ace weight"}, {"weight", ef_ace_weight, TYPE_INTEGER, "ace weight"},
{"nolog", ef_ace_nolog, TYPE_INTEGER, "do not log"}, {"nolog", ef_ace_nolog, TYPE_INTEGER, "do not log"},
{"mark", ef_ace_mark, TYPE_INTEGER, "mark the outgoing sockets with"},
{"next", ef_ace_next, TYPE_ACE, "next"} {"next", ef_ace_next, TYPE_ACE, "next"}
}; };
+1
View File
@@ -632,6 +632,7 @@ char * printopts(char *sep);
char * printfeatures(char *sep); char * printfeatures(char *sep);
void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg); void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg);
void maindropreserved(void *listener); void maindropreserved(void *listener);
void setmark(struct clientparam *param, SOCKET sock);
#ifdef _WINCE #ifdef _WINCE
char * CEToUnicode (const char *str); char * CEToUnicode (const char *str);
+2
View File
@@ -449,6 +449,7 @@ struct ace {
int wdays; int wdays;
int weight; int weight;
int nolog; int nolog;
int mark;
struct period *periods; struct period *periods;
struct userlist *users; struct userlist *users;
struct iplist *src, *dst; struct iplist *src, *dst;
@@ -850,6 +851,7 @@ struct clientparam {
int pwtype, int pwtype,
weight, weight,
nolog, nolog,
mark,
nolongdatfilter, nolongdatfilter,
nosplice, /* a TLS socket of the connection is not in the kernel */ nosplice, /* a TLS socket of the connection is not in the kernel */
nooverwritefilter, nooverwritefilter,
+1
View File
@@ -105,6 +105,7 @@ int udpbind(struct clientparam *param)
s = nssocket(param->srv, &param->srv->so, param->sostate, NS_OUT, SASOCK(&param->sinsl), SOCK_DGRAM, IPPROTO_UDP); s = nssocket(param->srv, &param->srv->so, param->sostate, NS_OUT, SASOCK(&param->sinsl), SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET) return 11; if (s == INVALID_SOCKET) return 11;
setmark(param, s);
#ifdef _WIN32 #ifdef _WIN32
{ unsigned long ul = 1; ioctlsocket(s, FIONBIO, &ul); } { unsigned long ul = 1; ioctlsocket(s, FIONBIO, &ul); }
#else #else
+49
View File
@@ -0,0 +1,49 @@
"""The mark command: a rule's connections carry a mark.
mark <number> extends the rule above it like nolog and weight; the
connection it lets through sends with the mark on its sockets towards
the destination. Where the system has no mark (Linux SO_MARK, FreeBSD
SO_USER_COOKIE) the command changes nothing, and what is checked here
is the flow: a rule with a mark serves as one without.
"""
def run(t):
if not t.skip_unless("httpsrv"):
return
origin = t.free_port()
t.start("mark_origin", f"""
log
auth iponly
allow *
http data * /data**
httpsrv -p{origin}
""", ports=[origin])
# a rule with a mark serves the connections it lets through
prox = t.free_port()
t.start("mark_rule", f"""
log
auth iponly
allow * * 192.168.0.0/16
allow * * 127.0.0.0/8
mark 20
proxy -p{prox}
""", ports=[prox])
t.eq(200, t.http(f"http://127.0.0.1:{origin}/data",
proxy=f"127.0.0.1:{prox}").status,
"a request matched by a rule with a mark is served")
# a mark of the rule above only: a rule of its own is refused
out = t.run_config("mark_alone", "log\nmark 20\nnot_a_command\n")
t.contains(out, "'mark' failed",
"a mark with no rule above it is refused")
t.not_contains(out, "'not_a_command'",
"and the file stops at the refused line")
# and so is a mark that is not a number
out = t.run_config("mark_word", "log\nallow *\nmark twenty\nnot_a_command\n")
t.contains(out, "Wrong mark",
"a mark that is not a number is refused")