Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable support for SO_MARK on Linux via compile-time flag #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ Supported SOCKS5 Features
- IPv4, IPv6, DNS
- TCP (no UDP at this time)

compile time options
--------------------

make CFLAGS=-DSOMARK

microsocks can be compiled with SO_MARK support on linux 2.6.25+. This
enables 'marking' of outgoing packets for use with policy-based routing
which allows to route packets through a non-default interface. E.g.:

ip rule add fwmark 1000 table 200
ip route add default dev tun1 table 200
microsocks -m 1000

will route all connections through device `tun1`
25 changes: 25 additions & 0 deletions sockssrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
#define THREAD_STACK_SIZE 32*1024
#endif

#if defined(SOMARK)
int somark; /* mark outgoing connections' packets for adv. routing */
#endif

static const char* auth_user;
static const char* auth_pass;
static sblist* auth_ips;
Expand Down Expand Up @@ -167,6 +171,11 @@ static int connect_socks_target(unsigned char *buf, size_t n, struct client *cli
}
if(SOCKADDR_UNION_AF(&bind_addr) != AF_UNSPEC && bindtoip(fd, &bind_addr) == -1)
goto eval_errno;
#if defined(SOMARK)
if(somark != 0) {
setsockopt(fd, SOL_SOCKET, SO_MARK, &somark, sizeof(somark));
}
#endif
if(connect(fd, remote->ai_addr, remote->ai_addrlen) == -1)
goto eval_errno;

Expand Down Expand Up @@ -364,10 +373,16 @@ static int usage(void) {
"MicroSocks SOCKS5 Server\n"
"------------------------\n"
"usage: microsocks -1 -i listenip -p port -u user -P password -b bindaddr\n"
#if defined(SOMARK)
" -m mark\n"
#endif
"all arguments are optional.\n"
"by default listenip is 0.0.0.0 and port 1080.\n\n"
"option -b specifies which ip outgoing connections are bound to\n"
"option -1 activates auth_once mode: once a specific ip address\n"
#if defined(SOMARK)
"option -m marks outgoing connections' packets with specified SO_MARK id\n"
#endif
"authed successfully with user/pass, it is added to a whitelist\n"
"and may use the proxy without auth.\n"
"this is handy for programs like firefox that don't support\n"
Expand All @@ -387,7 +402,12 @@ int main(int argc, char** argv) {
int ch;
const char *listenip = "0.0.0.0";
unsigned port = 1080;
#if defined(SOMARK)
somark = 0;
while((ch = getopt(argc, argv, ":1b:i:m:p:u:P:")) != -1) {
#else
while((ch = getopt(argc, argv, ":1b:i:p:u:P:")) != -1) {
#endif
switch(ch) {
case '1':
auth_ips = sblist_new(sizeof(union sockaddr_union), 8);
Expand All @@ -409,6 +429,11 @@ int main(int argc, char** argv) {
case 'p':
port = atoi(optarg);
break;
#if defined(SOMARK)
case 'm':
somark = atoi(optarg);
break;
#endif
case ':':
dprintf(2, "error: option -%c requires an operand\n", optopt);
/* fall through */
Expand Down