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:53 +03:00
1 parent e193732fcd
commit a2e400622b
9 files changed
+118

No files matched your search

+18
View File
@@ -1269,6 +1269,24 @@ nolog
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
.B force
.br
+1
View File
@@ -364,6 +364,7 @@ int checkACL(struct clientparam * param){
if(ACLmatches(acentry, param)) {
param->nolog = acentry->nolog;
param->weight = acentry->weight;
param->mark = acentry->mark;
if(acentry->action == 2) {
struct ace dup;
int res=60,i=0;
+20
View File
@@ -758,6 +758,25 @@ int intbindtodevice(struct clientparam *param, SOCKET sock){
#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){
SASIZETYPE size;
@@ -802,6 +821,7 @@ int doconnect(struct clientparam * param){
}
*SAPORT(&param->sinsl) = 0;
setopts(param->remsock, &param->srv->srvsockopts, param->srv->maxseg);
setmark(param, param->remsock);
if(extbindtodevice(param, param->remsock)) return 12;
#ifdef IP_BIND_ADDRESS_NO_PORT
+21
View File
@@ -1316,6 +1316,26 @@ static int h_nolog(int argc, unsigned char **argv){
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){
PROXYSOCKADDRTYPE sa;
char * slash, *dash;
@@ -2172,6 +2192,7 @@ struct commands commandhandlers[]={
{NULL, "filtermaxsize", h_filtermaxsize, 2, 2},
{NULL, "nolog", h_nolog, 1, 1},
{NULL, "weight", h_nolog, 2, 2},
{NULL, "mark", h_mark, 2, 2},
{NULL, "authcache", h_authcache, 2, 4},
{NULL, "smtpp", h_proxy, 1, 0},
{NULL, "delimchar",h_delimchar, 2, 2},
+5
View File
@@ -438,6 +438,10 @@ static void * ef_ace_nolog(struct node * node){
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){
return ((struct bandlim *)node->value) -> next;
}
@@ -852,6 +856,7 @@ static struct property prop_ace[] = {
{"periods", ef_ace_period, TYPE_PERIOD, "time of the day"},
{"weight", ef_ace_weight, TYPE_INTEGER, "ace weight"},
{"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"}
};
+1
View File
@@ -646,6 +646,7 @@ char * printopts(char *sep);
char * printfeatures(char *sep);
void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg);
void maindropreserved(void *listener);
void setmark(struct clientparam *param, SOCKET sock);
#define WEBBANNERS 35
+2
View File
@@ -407,6 +407,7 @@ struct ace {
int wdays;
int weight;
int nolog;
int mark;
struct period *periods;
struct userlist *users;
struct iplist *src, *dst;
@@ -819,6 +820,7 @@ struct clientparam {
int pwtype,
weight,
nolog,
mark,
nolongdatfilter,
nosplice, /* a TLS socket of the connection is not in the kernel */
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);
if (s == INVALID_SOCKET) return 11;
setmark(param, s);
#ifdef _WIN32
{ unsigned long ul = 1; ioctlsocket(s, FIONBIO, &ul); }
#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")