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

allow to setup metric for created routes (linux only) #1455

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions one.cpp
Expand Up @@ -1999,6 +1999,7 @@ static void printHelp(const char *cn,FILE *out)
fprintf(out," -h - Display this help" ZT_EOL_S);
fprintf(out," -v - Show version" ZT_EOL_S);
fprintf(out," -U - Skip privilege check and do not attempt to drop privileges" ZT_EOL_S);
fprintf(out," -m<route_metric> - Route metric (default: 100000, set to 0 to disable metric change)" ZT_EOL_S);
fprintf(out," -p<port> - Port for UDP and TCP/HTTP (default: 9993, 0 for random)" ZT_EOL_S);

#ifdef __UNIX_LIKE__
Expand Down Expand Up @@ -2062,6 +2063,9 @@ class _OneServiceRunner
const std::string &homeDir;
};

// hack
int route_metric=100000;

#ifdef __WINDOWS__
int __cdecl _tmain(int argc, _TCHAR* argv[])
#else
Expand Down Expand Up @@ -2130,6 +2134,10 @@ int main(int argc,char **argv)
if (argv[i][0] == '-') {
switch(argv[i][1]) {

case 'm': // port -- for both UDP and TCP, packets and control plane
route_metric = Utils::strToUInt(argv[i] + 2);
break;

case 'p': // port -- for both UDP and TCP, packets and control plane
port = Utils::strToUInt(argv[i] + 2);
if (port > 0xffff) {
Expand Down
12 changes: 11 additions & 1 deletion osdep/LinuxNetLink.cpp
Expand Up @@ -26,6 +26,8 @@
#define IFNAMSIZ 16
#endif

extern int route_metric;

namespace ZeroTier {

struct nl_route_req {
Expand Down Expand Up @@ -712,7 +714,7 @@ void LinuxNetLink::addRoute(const InetAddress &target, const InetAddress &via, c
char tmp[64];
char tmp2[64];
char tmp3[64];
fprintf(stderr, "Adding Route. target: %s via: %s src: %s iface: %s\n", target.toString(tmp), via.toString(tmp2), src.toString(tmp3), ifaceName);
fprintf(stderr, "Adding Route. target: %s via: %s src: %s iface: %s metric: %d\n", target.toString(tmp), via.toString(tmp2), src.toString(tmp3), ifaceName,route_metric);
#endif

int rtl = sizeof(struct rtmsg);
Expand Down Expand Up @@ -766,6 +768,14 @@ void LinuxNetLink::addRoute(const InetAddress &target, const InetAddress &via, c
}
}

if(route_metric>0) {
rtap = (struct rtattr *) (((char*)rtap) + rtap->rta_len);
rtap->rta_type = RTA_PRIORITY;
rtap->rta_len = RTA_LENGTH(sizeof(int));
memcpy(RTA_DATA(rtap), &route_metric, sizeof(int));
rtl += rtap->rta_len;
}

req.nl.nlmsg_len = NLMSG_LENGTH(rtl);
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE | NLM_F_ACK;
req.nl.nlmsg_type = RTM_NEWROUTE;
Expand Down