diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 9eacc44..480ea25 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -1269,6 +1269,24 @@ nolog Weight may be used for different purposes. +.br +.BR mark +\fI\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 diff --git a/src/acl.c b/src/acl.c index fec2b46..f5134ca 100644 --- a/src/acl.c +++ b/src/acl.c @@ -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; diff --git a/src/common.c b/src/common.c index 718e3ed..f2d7843 100644 --- a/src/common.c +++ b/src/common.c @@ -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 *)¶m->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(¶m->sinsl) = 0; setopts(param->remsock, ¶m->srv->srvsockopts, param->srv->maxseg); + setmark(param, param->remsock); if(extbindtodevice(param, param->remsock)) return 12; #ifdef IP_BIND_ADDRESS_NO_PORT diff --git a/src/conf.c b/src/conf.c index 180b7bb..221e580 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1305,15 +1305,35 @@ static int h_http(int argc, unsigned char **argv){ static int h_nolog(int argc, unsigned char **argv){ struct ace *acl = NULL; - 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; - if(argc == 1) acl->nolog = 1; - else acl->weight = atoi((char*)argv[1]); - return 0; + 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; + if(argc == 1) acl->nolog = 1; + else acl->weight = atoi((char*)argv[1]); + return 0; +} + +/* mark : 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){ @@ -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}, diff --git a/src/datatypes.c b/src/datatypes.c index 8464a5e..52a707c 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -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"} }; diff --git a/src/proxy.h b/src/proxy.h index 024c8ca..50bb910 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -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 diff --git a/src/structures.h b/src/structures.h index eb6b5d2..7d7182d 100644 --- a/src/structures.h +++ b/src/structures.h @@ -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, diff --git a/src/udpsockmap.c b/src/udpsockmap.c index d55989f..f1a8001 100644 --- a/src/udpsockmap.c +++ b/src/udpsockmap.c @@ -105,6 +105,7 @@ int udpbind(struct clientparam *param) s = nssocket(param->srv, ¶m->srv->so, param->sostate, NS_OUT, SASOCK(¶m->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 diff --git a/tests/cases/aclmark.py b/tests/cases/aclmark.py new file mode 100644 index 0000000..6727f13 --- /dev/null +++ b/tests/cases/aclmark.py @@ -0,0 +1,49 @@ +"""The mark command: a rule's connections carry a mark. + +mark 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")