Skip to content

Commit 1216f56

Browse files
ordexJenkins-dev
authored andcommitted
sitnl: allow user to configure custom metric for routes
Until now sitnl was just default to metric 0 when installing routes, while ignoring any value that may have been passed by the user. Extend logic to properly accept a user value. Signed-off-by: Antonio Quartulli <antonio@openvpn.net>
1 parent 1e23850 commit 1216f56

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

openvpn/tun/linux/client/tunnetlink.hpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ struct NetlinkRoute4 : public Action
319319
int prefixlen_arg,
320320
IPv4::Addr &gw_arg,
321321
std::string dev_arg,
322+
int metric_arg,
322323
bool add_arg)
323324
: route(dst_arg, prefixlen_arg),
324325
gw(gw_arg),
325326
dev(dev_arg),
327+
metric(metric_arg),
326328
add(add_arg)
327329
{
328330
}
@@ -333,6 +335,7 @@ struct NetlinkRoute4 : public Action
333335
ret->route = route;
334336
ret->gw = gw;
335337
ret->dev = dev;
338+
ret->metric = metric;
336339
return ret;
337340
}
338341

@@ -347,11 +350,11 @@ struct NetlinkRoute4 : public Action
347350
int ret;
348351
if (add)
349352
{
350-
ret = SITNL::net_route_add(route, gw, dev, 0, 0);
353+
ret = SITNL::net_route_add(route, gw, dev, 0, metric);
351354
}
352355
else
353356
{
354-
ret = SITNL::net_route_del(route, gw, dev, 0, 0);
357+
ret = SITNL::net_route_del(route, gw, dev, 0, metric);
355358
}
356359

357360
if (ret)
@@ -365,13 +368,14 @@ struct NetlinkRoute4 : public Action
365368
{
366369
std::ostringstream os;
367370
os << "netlink route " << (add ? "add" : "del") << " dev " << dev << " "
368-
<< route << " via " << gw.to_string();
371+
<< route << " via " << gw.to_string() << " metric " << metric;
369372
return os.str();
370373
}
371374

372375
IP::Route4 route;
373376
IPv4::Addr gw;
374377
std::string dev;
378+
int metric = -1;
375379
bool add = true;
376380
};
377381

@@ -387,10 +391,12 @@ struct NetlinkRoute6 : public Action
387391
int prefixlen_arg,
388392
IPv6::Addr &gw_arg,
389393
std::string dev_arg,
394+
int metric_arg,
390395
bool add_arg)
391396
: route(dst_arg, prefixlen_arg),
392397
gw(gw_arg),
393398
dev(dev_arg),
399+
metric(metric_arg),
394400
add(add_arg)
395401
{
396402
}
@@ -401,6 +407,7 @@ struct NetlinkRoute6 : public Action
401407
ret->route = route;
402408
ret->gw = gw;
403409
ret->dev = dev;
410+
ret->metric = metric;
404411
return ret;
405412
}
406413

@@ -415,11 +422,11 @@ struct NetlinkRoute6 : public Action
415422
int ret;
416423
if (add)
417424
{
418-
ret = SITNL::net_route_add(route, gw, dev, 0, 0);
425+
ret = SITNL::net_route_add(route, gw, dev, 0, metric);
419426
}
420427
else
421428
{
422-
ret = SITNL::net_route_del(route, gw, dev, 0, 0);
429+
ret = SITNL::net_route_del(route, gw, dev, 0, metric);
423430
}
424431

425432
if (ret)
@@ -433,13 +440,14 @@ struct NetlinkRoute6 : public Action
433440
{
434441
std::ostringstream os;
435442
os << "netlink route " << (add ? "add" : "del") << " dev " << dev << " "
436-
<< route << " via " << gw.to_string();
443+
<< route << " via " << gw.to_string() << " metric " << metric;
437444
return os.str();
438445
}
439446

440447
IP::Route6 route;
441448
IPv6::Addr gw;
442449
std::string dev;
450+
int metric = -1;
443451
bool add = true;
444452
};
445453

@@ -517,6 +525,7 @@ inline void add_del_route(const std::string &addr_str,
517525
const int prefix_len,
518526
const std::string &gateway_str,
519527
const std::string &dev,
528+
const int metric,
520529
const unsigned int flags,
521530
std::vector<IP::Route> *rtvec,
522531
Action::Ptr &create,
@@ -536,6 +545,7 @@ inline void add_del_route(const std::string &addr_str,
536545
add->route.prefix_len = prefix_len;
537546
add->gw = IPv6::Addr::from_string(gateway_str);
538547
add->dev = dev;
548+
add->metric = metric;
539549
add->add = true;
540550

541551
create = add;
@@ -562,6 +572,7 @@ inline void add_del_route(const std::string &addr_str,
562572
add->route.prefix_len = prefix_len;
563573
add->gw = IPv4::Addr::from_string(gateway_str);
564574
add->dev = dev;
575+
add->metric = metric;
565576
add->add = true;
566577

567578
create = add;
@@ -580,13 +591,14 @@ inline void add_del_route(const std::string &addr_str,
580591
const int prefix_len,
581592
const std::string &gateway_str,
582593
const std::string &dev,
594+
const int metric,
583595
const unsigned int flags, // add interface route to rtvec if defined
584596
std::vector<IP::Route> *rtvec,
585597
ActionList &create,
586598
ActionList &destroy)
587599
{
588600
Action::Ptr c, d;
589-
add_del_route(addr_str, prefix_len, gateway_str, dev, flags, rtvec, c, d);
601+
add_del_route(addr_str, prefix_len, gateway_str, dev, metric, flags, rtvec, c, d);
590602
create.add(c);
591603
destroy.add(d);
592604
}
@@ -648,6 +660,7 @@ inline void iface_config(const std::string &iface_name,
648660
local4->prefix_length,
649661
local4->address,
650662
iface_name,
663+
0,
651664
R_ADD_DCO,
652665
rtvec,
653666
create,
@@ -675,6 +688,7 @@ inline void iface_config(const std::string &iface_name,
675688
local6->prefix_length,
676689
local6->address,
677690
iface_name,
691+
0,
678692
R_ADD_DCO | R_IPv6,
679693
rtvec,
680694
create,
@@ -711,6 +725,7 @@ struct TunMethods
711725
route.prefix_length,
712726
local6->gateway,
713727
iface_name,
728+
route.metric,
714729
R_ADD_ALL | R_IPv6,
715730
rtvec,
716731
create,
@@ -723,6 +738,7 @@ struct TunMethods
723738
route.prefix_length,
724739
local4->gateway,
725740
iface_name,
741+
route.metric,
726742
R_ADD_ALL,
727743
rtvec,
728744
create,
@@ -751,6 +767,7 @@ struct TunMethods
751767
route.prefix_length,
752768
gw.v4.addr().to_string(),
753769
gw.v4.dev(),
770+
route.metric,
754771
R_ADD_SYS,
755772
rtvec,
756773
create,
@@ -779,8 +796,8 @@ struct TunMethods
779796
destroy);
780797
}
781798

782-
add_del_route("0.0.0.0", 1, local4->gateway, iface_name, R_ADD_ALL, rtvec, create, destroy);
783-
add_del_route("128.0.0.0", 1, local4->gateway, iface_name, R_ADD_ALL, rtvec, create, destroy);
799+
add_del_route("0.0.0.0", 1, local4->gateway, iface_name, 0, R_ADD_ALL, rtvec, create, destroy);
800+
add_del_route("128.0.0.0", 1, local4->gateway, iface_name, 0, R_ADD_ALL, rtvec, create, destroy);
784801
}
785802

786803
// Process IPv6 redirect-gateway
@@ -799,8 +816,8 @@ struct TunMethods
799816
destroy);
800817
}
801818

802-
add_del_route("0000::", 1, local6->gateway, iface_name, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
803-
add_del_route("8000::", 1, local6->gateway, iface_name, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
819+
add_del_route("0000::", 1, local6->gateway, iface_name, 0, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
820+
add_del_route("8000::", 1, local6->gateway, iface_name, 0, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
804821
}
805822
}
806823

@@ -823,6 +840,7 @@ struct TunMethods
823840
32,
824841
gw.v4.addr().to_string(),
825842
gw.dev(),
843+
0,
826844
R_ADD_SYS,
827845
rtvec,
828846
create,
@@ -833,6 +851,7 @@ struct TunMethods
833851
128,
834852
gw.v6.addr().to_string(),
835853
gw.dev(),
854+
0,
836855
R_IPv6 | R_ADD_SYS,
837856
rtvec,
838857
create,

0 commit comments

Comments
 (0)